1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * underlying calls for lock creation
7 * Copyright (C) 2004 Oracle. All rights reserved.
11 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/slab.h>
15 #include <linux/highmem.h>
16 #include <linux/init.h>
17 #include <linux/sysctl.h>
18 #include <linux/random.h>
19 #include <linux/blkdev.h>
20 #include <linux/socket.h>
21 #include <linux/inet.h>
22 #include <linux/spinlock.h>
23 #include <linux/delay.h>
26 #include "../cluster/heartbeat.h"
27 #include "../cluster/nodemanager.h"
28 #include "../cluster/tcp.h"
31 #include "dlmcommon.h"
33 #include "dlmconvert.h"
35 #define MLOG_MASK_PREFIX ML_DLM
36 #include "../cluster/masklog.h"
38 static struct kmem_cache
*dlm_lock_cache
;
40 static DEFINE_SPINLOCK(dlm_cookie_lock
);
41 static u64 dlm_next_cookie
= 1;
43 static enum dlm_status
dlm_send_remote_lock_request(struct dlm_ctxt
*dlm
,
44 struct dlm_lock_resource
*res
,
45 struct dlm_lock
*lock
, int flags
);
46 static void dlm_init_lock(struct dlm_lock
*newlock
, int type
,
48 static void dlm_lock_release(struct kref
*kref
);
49 static void dlm_lock_detach_lockres(struct dlm_lock
*lock
);
51 int dlm_init_lock_cache(void)
53 dlm_lock_cache
= kmem_cache_create("o2dlm_lock",
54 sizeof(struct dlm_lock
),
55 0, SLAB_HWCACHE_ALIGN
, NULL
);
56 if (dlm_lock_cache
== NULL
)
61 void dlm_destroy_lock_cache(void)
63 kmem_cache_destroy(dlm_lock_cache
);
66 /* Tell us whether we can grant a new lock request.
68 * caller needs: res->spinlock
71 * returns: 1 if the lock can be granted, 0 otherwise.
73 static int dlm_can_grant_new_lock(struct dlm_lock_resource
*res
,
74 struct dlm_lock
*lock
)
76 struct dlm_lock
*tmplock
;
78 list_for_each_entry(tmplock
, &res
->granted
, list
) {
79 if (!dlm_lock_compatible(tmplock
->ml
.type
, lock
->ml
.type
))
83 list_for_each_entry(tmplock
, &res
->converting
, list
) {
84 if (!dlm_lock_compatible(tmplock
->ml
.type
, lock
->ml
.type
))
86 if (!dlm_lock_compatible(tmplock
->ml
.convert_type
,
94 /* performs lock creation at the lockres master site
97 * taken: takes and drops res->spinlock
99 * returns: DLM_NORMAL, DLM_NOTQUEUED
101 static enum dlm_status
dlmlock_master(struct dlm_ctxt
*dlm
,
102 struct dlm_lock_resource
*res
,
103 struct dlm_lock
*lock
, int flags
)
105 int call_ast
= 0, kick_thread
= 0;
106 enum dlm_status status
= DLM_NORMAL
;
108 mlog(0, "type=%d\n", lock
->ml
.type
);
110 spin_lock(&res
->spinlock
);
111 /* if called from dlm_create_lock_handler, need to
112 * ensure it will not sleep in dlm_wait_on_lockres */
113 status
= __dlm_lockres_state_to_status(res
);
114 if (status
!= DLM_NORMAL
&&
115 lock
->ml
.node
!= dlm
->node_num
) {
116 /* erf. state changed after lock was dropped. */
117 spin_unlock(&res
->spinlock
);
121 __dlm_wait_on_lockres(res
);
122 __dlm_lockres_reserve_ast(res
);
124 if (dlm_can_grant_new_lock(res
, lock
)) {
125 mlog(0, "I can grant this lock right away\n");
126 /* got it right away */
127 lock
->lksb
->status
= DLM_NORMAL
;
130 list_add_tail(&lock
->list
, &res
->granted
);
132 /* for the recovery lock, we can't allow the ast
133 * to be queued since the dlmthread is already
134 * frozen. but the recovery lock is always locked
135 * with LKM_NOQUEUE so we do not need the ast in
136 * this special case */
137 if (!dlm_is_recovery_lock(res
->lockname
.name
,
138 res
->lockname
.len
)) {
142 mlog(0, "%s: returning DLM_NORMAL to "
143 "node %u for reco lock\n", dlm
->name
,
147 /* for NOQUEUE request, unless we get the
148 * lock right away, return DLM_NOTQUEUED */
149 if (flags
& LKM_NOQUEUE
) {
150 status
= DLM_NOTQUEUED
;
151 if (dlm_is_recovery_lock(res
->lockname
.name
,
152 res
->lockname
.len
)) {
153 mlog(0, "%s: returning NOTQUEUED to "
154 "node %u for reco lock\n", dlm
->name
,
160 list_add_tail(&lock
->list
, &res
->blocked
);
165 spin_unlock(&res
->spinlock
);
168 /* either queue the ast or release it */
170 dlm_queue_ast(dlm
, lock
);
172 dlm_lockres_release_ast(dlm
, res
);
174 dlm_lockres_calc_usage(dlm
, res
);
176 dlm_kick_thread(dlm
, res
);
181 void dlm_revert_pending_lock(struct dlm_lock_resource
*res
,
182 struct dlm_lock
*lock
)
184 /* remove from local queue if it failed */
185 list_del_init(&lock
->list
);
186 lock
->lksb
->flags
&= ~DLM_LKSB_GET_LVB
;
193 * taken: takes and drops res->spinlock
195 * returns: DLM_DENIED, DLM_RECOVERING, or net status
197 static enum dlm_status
dlmlock_remote(struct dlm_ctxt
*dlm
,
198 struct dlm_lock_resource
*res
,
199 struct dlm_lock
*lock
, int flags
)
201 enum dlm_status status
= DLM_DENIED
;
202 int lockres_changed
= 1;
204 mlog(0, "type=%d, lockres %.*s, flags = 0x%x\n",
205 lock
->ml
.type
, res
->lockname
.len
,
206 res
->lockname
.name
, flags
);
209 * Wait if resource is getting recovered, remastered, etc.
210 * If the resource was remastered and new owner is self, then exit.
212 spin_lock(&res
->spinlock
);
213 __dlm_wait_on_lockres(res
);
214 if (res
->owner
== dlm
->node_num
) {
215 spin_unlock(&res
->spinlock
);
216 return DLM_RECOVERING
;
218 res
->state
|= DLM_LOCK_RES_IN_PROGRESS
;
220 /* add lock to local (secondary) queue */
222 list_add_tail(&lock
->list
, &res
->blocked
);
223 lock
->lock_pending
= 1;
224 spin_unlock(&res
->spinlock
);
226 /* spec seems to say that you will get DLM_NORMAL when the lock
227 * has been queued, meaning we need to wait for a reply here. */
228 status
= dlm_send_remote_lock_request(dlm
, res
, lock
, flags
);
230 spin_lock(&res
->spinlock
);
231 res
->state
&= ~DLM_LOCK_RES_IN_PROGRESS
;
232 lock
->lock_pending
= 0;
233 if (status
!= DLM_NORMAL
) {
234 if (status
== DLM_RECOVERING
&&
235 dlm_is_recovery_lock(res
->lockname
.name
,
236 res
->lockname
.len
)) {
237 /* recovery lock was mastered by dead node.
238 * we need to have calc_usage shoot down this
239 * lockres and completely remaster it. */
240 mlog(0, "%s: recovery lock was owned by "
241 "dead node %u, remaster it now.\n",
242 dlm
->name
, res
->owner
);
243 } else if (status
!= DLM_NOTQUEUED
) {
245 * DO NOT call calc_usage, as this would unhash
246 * the remote lockres before we ever get to use
247 * it. treat as if we never made any change to
253 dlm_revert_pending_lock(res
, lock
);
255 } else if (dlm_is_recovery_lock(res
->lockname
.name
,
256 res
->lockname
.len
)) {
257 /* special case for the $RECOVERY lock.
258 * there will never be an AST delivered to put
259 * this lock on the proper secondary queue
260 * (granted), so do it manually. */
261 mlog(0, "%s: $RECOVERY lock for this node (%u) is "
262 "mastered by %u; got lock, manually granting (no ast)\n",
263 dlm
->name
, dlm
->node_num
, res
->owner
);
264 list_move_tail(&lock
->list
, &res
->granted
);
266 spin_unlock(&res
->spinlock
);
269 dlm_lockres_calc_usage(dlm
, res
);
276 /* for remote lock creation.
278 * caller needs: none, but need res->state & DLM_LOCK_RES_IN_PROGRESS
281 * returns: DLM_NOLOCKMGR, or net status
283 static enum dlm_status
dlm_send_remote_lock_request(struct dlm_ctxt
*dlm
,
284 struct dlm_lock_resource
*res
,
285 struct dlm_lock
*lock
, int flags
)
287 struct dlm_create_lock create
;
288 int tmpret
, status
= 0;
291 memset(&create
, 0, sizeof(create
));
292 create
.node_idx
= dlm
->node_num
;
293 create
.requested_type
= lock
->ml
.type
;
294 create
.cookie
= lock
->ml
.cookie
;
295 create
.namelen
= res
->lockname
.len
;
296 create
.flags
= cpu_to_be32(flags
);
297 memcpy(create
.name
, res
->lockname
.name
, create
.namelen
);
299 tmpret
= o2net_send_message(DLM_CREATE_LOCK_MSG
, dlm
->key
, &create
,
300 sizeof(create
), res
->owner
, &status
);
303 if (ret
== DLM_REJECTED
) {
304 mlog(ML_ERROR
, "%s: res %.*s, Stale lockres no longer "
305 "owned by node %u. That node is coming back up "
306 "currently.\n", dlm
->name
, create
.namelen
,
307 create
.name
, res
->owner
);
308 dlm_print_one_lock_resource(res
);
312 mlog(ML_ERROR
, "%s: res %.*s, Error %d send CREATE LOCK to "
313 "node %u\n", dlm
->name
, create
.namelen
, create
.name
,
315 if (dlm_is_host_down(tmpret
))
316 ret
= DLM_RECOVERING
;
318 ret
= dlm_err_to_dlm_status(tmpret
);
324 void dlm_lock_get(struct dlm_lock
*lock
)
326 kref_get(&lock
->lock_refs
);
329 void dlm_lock_put(struct dlm_lock
*lock
)
331 kref_put(&lock
->lock_refs
, dlm_lock_release
);
334 static void dlm_lock_release(struct kref
*kref
)
336 struct dlm_lock
*lock
;
338 lock
= container_of(kref
, struct dlm_lock
, lock_refs
);
340 BUG_ON(!list_empty(&lock
->list
));
341 BUG_ON(!list_empty(&lock
->ast_list
));
342 BUG_ON(!list_empty(&lock
->bast_list
));
343 BUG_ON(lock
->ast_pending
);
344 BUG_ON(lock
->bast_pending
);
346 dlm_lock_detach_lockres(lock
);
348 if (lock
->lksb_kernel_allocated
) {
349 mlog(0, "freeing kernel-allocated lksb\n");
352 kmem_cache_free(dlm_lock_cache
, lock
);
355 /* associate a lock with it's lockres, getting a ref on the lockres */
356 void dlm_lock_attach_lockres(struct dlm_lock
*lock
,
357 struct dlm_lock_resource
*res
)
359 dlm_lockres_get(res
);
363 /* drop ref on lockres, if there is still one associated with lock */
364 static void dlm_lock_detach_lockres(struct dlm_lock
*lock
)
366 struct dlm_lock_resource
*res
;
370 lock
->lockres
= NULL
;
371 mlog(0, "removing lock's lockres reference\n");
372 dlm_lockres_put(res
);
376 static void dlm_init_lock(struct dlm_lock
*newlock
, int type
,
379 INIT_LIST_HEAD(&newlock
->list
);
380 INIT_LIST_HEAD(&newlock
->ast_list
);
381 INIT_LIST_HEAD(&newlock
->bast_list
);
382 spin_lock_init(&newlock
->spinlock
);
383 newlock
->ml
.type
= type
;
384 newlock
->ml
.convert_type
= LKM_IVMODE
;
385 newlock
->ml
.highest_blocked
= LKM_IVMODE
;
386 newlock
->ml
.node
= node
;
387 newlock
->ml
.pad1
= 0;
388 newlock
->ml
.list
= 0;
389 newlock
->ml
.flags
= 0;
391 newlock
->bast
= NULL
;
392 newlock
->astdata
= NULL
;
393 newlock
->ml
.cookie
= cpu_to_be64(cookie
);
394 newlock
->ast_pending
= 0;
395 newlock
->bast_pending
= 0;
396 newlock
->convert_pending
= 0;
397 newlock
->lock_pending
= 0;
398 newlock
->unlock_pending
= 0;
399 newlock
->cancel_pending
= 0;
400 newlock
->lksb_kernel_allocated
= 0;
402 kref_init(&newlock
->lock_refs
);
405 struct dlm_lock
* dlm_new_lock(int type
, u8 node
, u64 cookie
,
406 struct dlm_lockstatus
*lksb
)
408 struct dlm_lock
*lock
;
409 int kernel_allocated
= 0;
411 lock
= kmem_cache_zalloc(dlm_lock_cache
, GFP_NOFS
);
416 /* zero memory only if kernel-allocated */
417 lksb
= kzalloc(sizeof(*lksb
), GFP_NOFS
);
419 kmem_cache_free(dlm_lock_cache
, lock
);
422 kernel_allocated
= 1;
425 dlm_init_lock(lock
, type
, node
, cookie
);
426 if (kernel_allocated
)
427 lock
->lksb_kernel_allocated
= 1;
433 /* handler for lock creation net message
436 * taken: takes and drops res->spinlock
438 * returns: DLM_NORMAL, DLM_SYSERR, DLM_IVLOCKID, DLM_NOTQUEUED
440 int dlm_create_lock_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
443 struct dlm_ctxt
*dlm
= data
;
444 struct dlm_create_lock
*create
= (struct dlm_create_lock
*)msg
->buf
;
445 struct dlm_lock_resource
*res
= NULL
;
446 struct dlm_lock
*newlock
= NULL
;
447 struct dlm_lockstatus
*lksb
= NULL
;
448 enum dlm_status status
= DLM_NORMAL
;
450 unsigned int namelen
;
458 namelen
= create
->namelen
;
459 status
= DLM_REJECTED
;
460 if (!dlm_domain_fully_joined(dlm
)) {
461 mlog(ML_ERROR
, "Domain %s not fully joined, but node %u is "
462 "sending a create_lock message for lock %.*s!\n",
463 dlm
->name
, create
->node_idx
, namelen
, name
);
468 status
= DLM_IVBUFLEN
;
469 if (namelen
> DLM_LOCKID_NAME_MAX
) {
475 newlock
= dlm_new_lock(create
->requested_type
,
477 be64_to_cpu(create
->cookie
), NULL
);
483 lksb
= newlock
->lksb
;
485 if (be32_to_cpu(create
->flags
) & LKM_GET_LVB
) {
486 lksb
->flags
|= DLM_LKSB_GET_LVB
;
487 mlog(0, "set DLM_LKSB_GET_LVB flag\n");
490 status
= DLM_IVLOCKID
;
491 res
= dlm_lookup_lockres(dlm
, name
, namelen
);
497 spin_lock(&res
->spinlock
);
498 status
= __dlm_lockres_state_to_status(res
);
499 spin_unlock(&res
->spinlock
);
501 if (status
!= DLM_NORMAL
) {
502 mlog(0, "lockres recovering/migrating/in-progress\n");
506 dlm_lock_attach_lockres(newlock
, res
);
508 status
= dlmlock_master(dlm
, res
, newlock
, be32_to_cpu(create
->flags
));
510 if (status
!= DLM_NORMAL
)
512 dlm_lock_put(newlock
);
515 dlm_lockres_put(res
);
523 /* fetch next node-local (u8 nodenum + u56 cookie) into u64 */
524 static inline void dlm_get_next_cookie(u8 node_num
, u64
*cookie
)
526 u64 tmpnode
= node_num
;
528 /* shift single byte of node num into top 8 bits */
531 spin_lock(&dlm_cookie_lock
);
532 *cookie
= (dlm_next_cookie
| tmpnode
);
533 if (++dlm_next_cookie
& 0xff00000000000000ull
) {
534 mlog(0, "This node's cookie will now wrap!\n");
537 spin_unlock(&dlm_cookie_lock
);
540 enum dlm_status
dlmlock(struct dlm_ctxt
*dlm
, int mode
,
541 struct dlm_lockstatus
*lksb
, int flags
,
542 const char *name
, int namelen
, dlm_astlockfunc_t
*ast
,
543 void *data
, dlm_bastlockfunc_t
*bast
)
545 enum dlm_status status
;
546 struct dlm_lock_resource
*res
= NULL
;
547 struct dlm_lock
*lock
= NULL
;
548 int convert
= 0, recovery
= 0;
550 /* yes this function is a mess.
551 * TODO: clean this up. lots of common code in the
552 * lock and convert paths, especially in the retry blocks */
554 dlm_error(DLM_BADARGS
);
558 status
= DLM_BADPARAM
;
559 if (mode
!= LKM_EXMODE
&& mode
!= LKM_PRMODE
&& mode
!= LKM_NLMODE
) {
564 if (flags
& ~LKM_VALID_FLAGS
) {
569 convert
= (flags
& LKM_CONVERT
);
570 recovery
= (flags
& LKM_RECOVERY
);
573 (!dlm_is_recovery_lock(name
, namelen
) || convert
) ) {
577 if (convert
&& (flags
& LKM_LOCAL
)) {
578 mlog(ML_ERROR
, "strange LOCAL convert request!\n");
583 /* CONVERT request */
585 /* if converting, must pass in a valid dlm_lock */
588 mlog(ML_ERROR
, "NULL lock pointer in convert "
595 mlog(ML_ERROR
, "NULL lockres pointer in convert "
599 dlm_lockres_get(res
);
601 /* XXX: for ocfs2 purposes, the ast/bast/astdata/lksb are
602 * static after the original lock call. convert requests will
603 * ensure that everything is the same, or return DLM_BADARGS.
604 * this means that DLM_DENIED_NOASTS will never be returned.
606 if (lock
->lksb
!= lksb
|| lock
->ast
!= ast
||
607 lock
->bast
!= bast
|| lock
->astdata
!= data
) {
608 status
= DLM_BADARGS
;
609 mlog(ML_ERROR
, "new args: lksb=%p, ast=%p, bast=%p, "
610 "astdata=%p\n", lksb
, ast
, bast
, data
);
611 mlog(ML_ERROR
, "orig args: lksb=%p, ast=%p, bast=%p, "
612 "astdata=%p\n", lock
->lksb
, lock
->ast
,
613 lock
->bast
, lock
->astdata
);
617 dlm_wait_for_recovery(dlm
);
619 if (res
->owner
== dlm
->node_num
)
620 status
= dlmconvert_master(dlm
, res
, lock
, flags
, mode
);
622 status
= dlmconvert_remote(dlm
, res
, lock
, flags
, mode
);
623 if (status
== DLM_RECOVERING
|| status
== DLM_MIGRATING
||
624 status
== DLM_FORWARD
) {
625 /* for now, see how this works without sleeping
626 * and just retry right away. I suspect the reco
627 * or migration will complete fast enough that
628 * no waiting will be necessary */
629 mlog(0, "retrying convert with migration/recovery/"
638 status
= DLM_BADARGS
;
644 status
= DLM_IVBUFLEN
;
645 if (namelen
> DLM_LOCKID_NAME_MAX
|| namelen
< 1) {
650 dlm_get_next_cookie(dlm
->node_num
, &tmpcookie
);
651 lock
= dlm_new_lock(mode
, dlm
->node_num
, tmpcookie
, lksb
);
658 dlm_wait_for_recovery(dlm
);
660 /* find or create the lock resource */
661 res
= dlm_get_lock_resource(dlm
, name
, namelen
, flags
);
663 status
= DLM_IVLOCKID
;
668 mlog(0, "type=%d, flags = 0x%x\n", mode
, flags
);
669 mlog(0, "creating lock: lock=%p res=%p\n", lock
, res
);
671 dlm_lock_attach_lockres(lock
, res
);
674 lock
->astdata
= data
;
677 if (flags
& LKM_VALBLK
) {
678 mlog(0, "LKM_VALBLK passed by caller\n");
680 /* LVB requests for non PR, PW or EX locks are
682 if (mode
< LKM_PRMODE
)
683 flags
&= ~LKM_VALBLK
;
685 flags
|= LKM_GET_LVB
;
686 lock
->lksb
->flags
|= DLM_LKSB_GET_LVB
;
690 if (res
->owner
== dlm
->node_num
)
691 status
= dlmlock_master(dlm
, res
, lock
, flags
);
693 status
= dlmlock_remote(dlm
, res
, lock
, flags
);
695 if (status
== DLM_RECOVERING
|| status
== DLM_MIGRATING
||
696 status
== DLM_FORWARD
) {
699 if (status
!= DLM_RECOVERING
)
701 /* wait to see the node go down, then
702 * drop down and allow the lockres to
703 * get cleaned up. need to remaster. */
704 dlm_wait_for_node_death(dlm
, res
->owner
,
705 DLM_NODE_DEATH_WAIT_MAX
);
707 dlm_wait_for_recovery(dlm
);
712 /* Inflight taken in dlm_get_lock_resource() is dropped here */
713 spin_lock(&res
->spinlock
);
714 dlm_lockres_drop_inflight_ref(dlm
, res
);
715 spin_unlock(&res
->spinlock
);
717 dlm_lockres_calc_usage(dlm
, res
);
718 dlm_kick_thread(dlm
, res
);
720 if (status
!= DLM_NORMAL
) {
721 lock
->lksb
->flags
&= ~DLM_LKSB_GET_LVB
;
722 if (status
!= DLM_NOTQUEUED
)
729 if (status
!= DLM_NORMAL
) {
730 if (lock
&& !convert
)
732 // this is kind of unnecessary
733 lksb
->status
= status
;
736 /* put lockres ref from the convert path
737 * or from dlm_get_lock_resource */
739 dlm_lockres_put(res
);
743 EXPORT_SYMBOL_GPL(dlmlock
);