1 /******************************************************************************
2 *******************************************************************************
4 ** Copyright (C) 2005-2010 Red Hat, Inc. All rights reserved.
6 ** This copyrighted material is made available to anyone wishing to use,
7 ** modify, copy, or redistribute it subject to the terms and conditions
8 ** of the GNU General Public License v.2.
10 *******************************************************************************
11 ******************************************************************************/
13 /* Central locking logic has four stages:
33 Stage 1 (lock, unlock) is mainly about checking input args and
34 splitting into one of the four main operations:
36 dlm_lock = request_lock
37 dlm_lock+CONVERT = convert_lock
38 dlm_unlock = unlock_lock
39 dlm_unlock+CANCEL = cancel_lock
41 Stage 2, xxxx_lock(), just finds and locks the relevant rsb which is
42 provided to the next stage.
44 Stage 3, _xxxx_lock(), determines if the operation is local or remote.
45 When remote, it calls send_xxxx(), when local it calls do_xxxx().
47 Stage 4, do_xxxx(), is the guts of the operation. It manipulates the
48 given rsb and lkb and queues callbacks.
50 For remote operations, send_xxxx() results in the corresponding do_xxxx()
51 function being executed on the remote node. The connecting send/receive
52 calls on local (L) and remote (R) nodes:
54 L: send_xxxx() -> R: receive_xxxx()
56 L: receive_xxxx_reply() <- R: send_xxxx_reply()
58 #include <linux/types.h>
59 #include <linux/rbtree.h>
60 #include <linux/slab.h>
61 #include "dlm_internal.h"
62 #include <linux/dlm_device.h>
65 #include "requestqueue.h"
69 #include "lockspace.h"
74 #include "lvb_table.h"
78 static int send_request(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
);
79 static int send_convert(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
);
80 static int send_unlock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
);
81 static int send_cancel(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
);
82 static int send_grant(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
);
83 static int send_bast(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int mode
);
84 static int send_lookup(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
);
85 static int send_remove(struct dlm_rsb
*r
);
86 static int _request_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
);
87 static int _cancel_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
);
88 static void __receive_convert_reply(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
89 struct dlm_message
*ms
);
90 static int receive_extralen(struct dlm_message
*ms
);
91 static void do_purge(struct dlm_ls
*ls
, int nodeid
, int pid
);
92 static void del_timeout(struct dlm_lkb
*lkb
);
95 * Lock compatibilty matrix - thanks Steve
96 * UN = Unlocked state. Not really a state, used as a flag
97 * PD = Padding. Used to make the matrix a nice power of two in size
98 * Other states are the same as the VMS DLM.
99 * Usage: matrix[grmode+1][rqmode+1] (although m[rq+1][gr+1] is the same)
102 static const int __dlm_compat_matrix
[8][8] = {
103 /* UN NL CR CW PR PW EX PD */
104 {1, 1, 1, 1, 1, 1, 1, 0}, /* UN */
105 {1, 1, 1, 1, 1, 1, 1, 0}, /* NL */
106 {1, 1, 1, 1, 1, 1, 0, 0}, /* CR */
107 {1, 1, 1, 1, 0, 0, 0, 0}, /* CW */
108 {1, 1, 1, 0, 1, 0, 0, 0}, /* PR */
109 {1, 1, 1, 0, 0, 0, 0, 0}, /* PW */
110 {1, 1, 0, 0, 0, 0, 0, 0}, /* EX */
111 {0, 0, 0, 0, 0, 0, 0, 0} /* PD */
115 * This defines the direction of transfer of LVB data.
116 * Granted mode is the row; requested mode is the column.
117 * Usage: matrix[grmode+1][rqmode+1]
118 * 1 = LVB is returned to the caller
119 * 0 = LVB is written to the resource
120 * -1 = nothing happens to the LVB
123 const int dlm_lvb_operations
[8][8] = {
124 /* UN NL CR CW PR PW EX PD*/
125 { -1, 1, 1, 1, 1, 1, 1, -1 }, /* UN */
126 { -1, 1, 1, 1, 1, 1, 1, 0 }, /* NL */
127 { -1, -1, 1, 1, 1, 1, 1, 0 }, /* CR */
128 { -1, -1, -1, 1, 1, 1, 1, 0 }, /* CW */
129 { -1, -1, -1, -1, 1, 1, 1, 0 }, /* PR */
130 { -1, 0, 0, 0, 0, 0, 1, 0 }, /* PW */
131 { -1, 0, 0, 0, 0, 0, 0, 0 }, /* EX */
132 { -1, 0, 0, 0, 0, 0, 0, 0 } /* PD */
135 #define modes_compat(gr, rq) \
136 __dlm_compat_matrix[(gr)->lkb_grmode + 1][(rq)->lkb_rqmode + 1]
138 int dlm_modes_compat(int mode1
, int mode2
)
140 return __dlm_compat_matrix
[mode1
+ 1][mode2
+ 1];
144 * Compatibility matrix for conversions with QUECVT set.
145 * Granted mode is the row; requested mode is the column.
146 * Usage: matrix[grmode+1][rqmode+1]
149 static const int __quecvt_compat_matrix
[8][8] = {
150 /* UN NL CR CW PR PW EX PD */
151 {0, 0, 0, 0, 0, 0, 0, 0}, /* UN */
152 {0, 0, 1, 1, 1, 1, 1, 0}, /* NL */
153 {0, 0, 0, 1, 1, 1, 1, 0}, /* CR */
154 {0, 0, 0, 0, 1, 1, 1, 0}, /* CW */
155 {0, 0, 0, 1, 0, 1, 1, 0}, /* PR */
156 {0, 0, 0, 0, 0, 0, 1, 0}, /* PW */
157 {0, 0, 0, 0, 0, 0, 0, 0}, /* EX */
158 {0, 0, 0, 0, 0, 0, 0, 0} /* PD */
161 void dlm_print_lkb(struct dlm_lkb
*lkb
)
163 printk(KERN_ERR
"lkb: nodeid %d id %x remid %x exflags %x flags %x\n"
164 " status %d rqmode %d grmode %d wait_type %d\n",
165 lkb
->lkb_nodeid
, lkb
->lkb_id
, lkb
->lkb_remid
, lkb
->lkb_exflags
,
166 lkb
->lkb_flags
, lkb
->lkb_status
, lkb
->lkb_rqmode
,
167 lkb
->lkb_grmode
, lkb
->lkb_wait_type
);
170 static void dlm_print_rsb(struct dlm_rsb
*r
)
172 printk(KERN_ERR
"rsb: nodeid %d flags %lx first %x rlc %d name %s\n",
173 r
->res_nodeid
, r
->res_flags
, r
->res_first_lkid
,
174 r
->res_recover_locks_count
, r
->res_name
);
177 void dlm_dump_rsb(struct dlm_rsb
*r
)
183 printk(KERN_ERR
"rsb: root_list empty %d recover_list empty %d\n",
184 list_empty(&r
->res_root_list
), list_empty(&r
->res_recover_list
));
185 printk(KERN_ERR
"rsb lookup list\n");
186 list_for_each_entry(lkb
, &r
->res_lookup
, lkb_rsb_lookup
)
188 printk(KERN_ERR
"rsb grant queue:\n");
189 list_for_each_entry(lkb
, &r
->res_grantqueue
, lkb_statequeue
)
191 printk(KERN_ERR
"rsb convert queue:\n");
192 list_for_each_entry(lkb
, &r
->res_convertqueue
, lkb_statequeue
)
194 printk(KERN_ERR
"rsb wait queue:\n");
195 list_for_each_entry(lkb
, &r
->res_waitqueue
, lkb_statequeue
)
199 /* Threads cannot use the lockspace while it's being recovered */
201 static inline void dlm_lock_recovery(struct dlm_ls
*ls
)
203 down_read(&ls
->ls_in_recovery
);
206 void dlm_unlock_recovery(struct dlm_ls
*ls
)
208 up_read(&ls
->ls_in_recovery
);
211 int dlm_lock_recovery_try(struct dlm_ls
*ls
)
213 return down_read_trylock(&ls
->ls_in_recovery
);
216 static inline int can_be_queued(struct dlm_lkb
*lkb
)
218 return !(lkb
->lkb_exflags
& DLM_LKF_NOQUEUE
);
221 static inline int force_blocking_asts(struct dlm_lkb
*lkb
)
223 return (lkb
->lkb_exflags
& DLM_LKF_NOQUEUEBAST
);
226 static inline int is_demoted(struct dlm_lkb
*lkb
)
228 return (lkb
->lkb_sbflags
& DLM_SBF_DEMOTED
);
231 static inline int is_altmode(struct dlm_lkb
*lkb
)
233 return (lkb
->lkb_sbflags
& DLM_SBF_ALTMODE
);
236 static inline int is_granted(struct dlm_lkb
*lkb
)
238 return (lkb
->lkb_status
== DLM_LKSTS_GRANTED
);
241 static inline int is_remote(struct dlm_rsb
*r
)
243 DLM_ASSERT(r
->res_nodeid
>= 0, dlm_print_rsb(r
););
244 return !!r
->res_nodeid
;
247 static inline int is_process_copy(struct dlm_lkb
*lkb
)
249 return (lkb
->lkb_nodeid
&& !(lkb
->lkb_flags
& DLM_IFL_MSTCPY
));
252 static inline int is_master_copy(struct dlm_lkb
*lkb
)
254 if (lkb
->lkb_flags
& DLM_IFL_MSTCPY
)
255 DLM_ASSERT(lkb
->lkb_nodeid
, dlm_print_lkb(lkb
););
256 return (lkb
->lkb_flags
& DLM_IFL_MSTCPY
) ? 1 : 0;
259 static inline int middle_conversion(struct dlm_lkb
*lkb
)
261 if ((lkb
->lkb_grmode
==DLM_LOCK_PR
&& lkb
->lkb_rqmode
==DLM_LOCK_CW
) ||
262 (lkb
->lkb_rqmode
==DLM_LOCK_PR
&& lkb
->lkb_grmode
==DLM_LOCK_CW
))
267 static inline int down_conversion(struct dlm_lkb
*lkb
)
269 return (!middle_conversion(lkb
) && lkb
->lkb_rqmode
< lkb
->lkb_grmode
);
272 static inline int is_overlap_unlock(struct dlm_lkb
*lkb
)
274 return lkb
->lkb_flags
& DLM_IFL_OVERLAP_UNLOCK
;
277 static inline int is_overlap_cancel(struct dlm_lkb
*lkb
)
279 return lkb
->lkb_flags
& DLM_IFL_OVERLAP_CANCEL
;
282 static inline int is_overlap(struct dlm_lkb
*lkb
)
284 return (lkb
->lkb_flags
& (DLM_IFL_OVERLAP_UNLOCK
|
285 DLM_IFL_OVERLAP_CANCEL
));
288 static void queue_cast(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int rv
)
290 if (is_master_copy(lkb
))
295 DLM_ASSERT(lkb
->lkb_lksb
, dlm_print_lkb(lkb
););
297 /* if the operation was a cancel, then return -DLM_ECANCEL, if a
298 timeout caused the cancel then return -ETIMEDOUT */
299 if (rv
== -DLM_ECANCEL
&& (lkb
->lkb_flags
& DLM_IFL_TIMEOUT_CANCEL
)) {
300 lkb
->lkb_flags
&= ~DLM_IFL_TIMEOUT_CANCEL
;
304 if (rv
== -DLM_ECANCEL
&& (lkb
->lkb_flags
& DLM_IFL_DEADLOCK_CANCEL
)) {
305 lkb
->lkb_flags
&= ~DLM_IFL_DEADLOCK_CANCEL
;
309 dlm_add_cb(lkb
, DLM_CB_CAST
, lkb
->lkb_grmode
, rv
, lkb
->lkb_sbflags
);
312 static inline void queue_cast_overlap(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
315 is_overlap_unlock(lkb
) ? -DLM_EUNLOCK
: -DLM_ECANCEL
);
318 static void queue_bast(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int rqmode
)
320 if (is_master_copy(lkb
)) {
321 send_bast(r
, lkb
, rqmode
);
323 dlm_add_cb(lkb
, DLM_CB_BAST
, rqmode
, 0, 0);
328 * Basic operations on rsb's and lkb's
331 static int pre_rsb_struct(struct dlm_ls
*ls
)
333 struct dlm_rsb
*r1
, *r2
;
336 spin_lock(&ls
->ls_new_rsb_spin
);
337 if (ls
->ls_new_rsb_count
> dlm_config
.ci_new_rsb_count
/ 2) {
338 spin_unlock(&ls
->ls_new_rsb_spin
);
341 spin_unlock(&ls
->ls_new_rsb_spin
);
343 r1
= dlm_allocate_rsb(ls
);
344 r2
= dlm_allocate_rsb(ls
);
346 spin_lock(&ls
->ls_new_rsb_spin
);
348 list_add(&r1
->res_hashchain
, &ls
->ls_new_rsb
);
349 ls
->ls_new_rsb_count
++;
352 list_add(&r2
->res_hashchain
, &ls
->ls_new_rsb
);
353 ls
->ls_new_rsb_count
++;
355 count
= ls
->ls_new_rsb_count
;
356 spin_unlock(&ls
->ls_new_rsb_spin
);
363 /* If ls->ls_new_rsb is empty, return -EAGAIN, so the caller can
364 unlock any spinlocks, go back and call pre_rsb_struct again.
365 Otherwise, take an rsb off the list and return it. */
367 static int get_rsb_struct(struct dlm_ls
*ls
, char *name
, int len
,
368 struct dlm_rsb
**r_ret
)
373 spin_lock(&ls
->ls_new_rsb_spin
);
374 if (list_empty(&ls
->ls_new_rsb
)) {
375 count
= ls
->ls_new_rsb_count
;
376 spin_unlock(&ls
->ls_new_rsb_spin
);
377 log_debug(ls
, "find_rsb retry %d %d %s",
378 count
, dlm_config
.ci_new_rsb_count
, name
);
382 r
= list_first_entry(&ls
->ls_new_rsb
, struct dlm_rsb
, res_hashchain
);
383 list_del(&r
->res_hashchain
);
384 /* Convert the empty list_head to a NULL rb_node for tree usage: */
385 memset(&r
->res_hashnode
, 0, sizeof(struct rb_node
));
386 ls
->ls_new_rsb_count
--;
387 spin_unlock(&ls
->ls_new_rsb_spin
);
391 memcpy(r
->res_name
, name
, len
);
392 mutex_init(&r
->res_mutex
);
394 INIT_LIST_HEAD(&r
->res_lookup
);
395 INIT_LIST_HEAD(&r
->res_grantqueue
);
396 INIT_LIST_HEAD(&r
->res_convertqueue
);
397 INIT_LIST_HEAD(&r
->res_waitqueue
);
398 INIT_LIST_HEAD(&r
->res_root_list
);
399 INIT_LIST_HEAD(&r
->res_recover_list
);
405 static int rsb_cmp(struct dlm_rsb
*r
, const char *name
, int nlen
)
407 char maxname
[DLM_RESNAME_MAXLEN
];
409 memset(maxname
, 0, DLM_RESNAME_MAXLEN
);
410 memcpy(maxname
, name
, nlen
);
411 return memcmp(r
->res_name
, maxname
, DLM_RESNAME_MAXLEN
);
414 static int search_rsb_tree(struct rb_root
*tree
, char *name
, int len
,
415 unsigned int flags
, struct dlm_rsb
**r_ret
)
417 struct rb_node
*node
= tree
->rb_node
;
423 r
= rb_entry(node
, struct dlm_rsb
, res_hashnode
);
424 rc
= rsb_cmp(r
, name
, len
);
426 node
= node
->rb_left
;
428 node
= node
->rb_right
;
436 if (r
->res_nodeid
&& (flags
& R_MASTER
))
442 static int rsb_insert(struct dlm_rsb
*rsb
, struct rb_root
*tree
)
444 struct rb_node
**newn
= &tree
->rb_node
;
445 struct rb_node
*parent
= NULL
;
449 struct dlm_rsb
*cur
= rb_entry(*newn
, struct dlm_rsb
,
453 rc
= rsb_cmp(cur
, rsb
->res_name
, rsb
->res_length
);
455 newn
= &parent
->rb_left
;
457 newn
= &parent
->rb_right
;
459 log_print("rsb_insert match");
466 rb_link_node(&rsb
->res_hashnode
, parent
, newn
);
467 rb_insert_color(&rsb
->res_hashnode
, tree
);
471 static int _search_rsb(struct dlm_ls
*ls
, char *name
, int len
, int b
,
472 unsigned int flags
, struct dlm_rsb
**r_ret
)
477 error
= search_rsb_tree(&ls
->ls_rsbtbl
[b
].keep
, name
, len
, flags
, &r
);
479 kref_get(&r
->res_ref
);
482 error
= search_rsb_tree(&ls
->ls_rsbtbl
[b
].toss
, name
, len
, flags
, &r
);
486 rb_erase(&r
->res_hashnode
, &ls
->ls_rsbtbl
[b
].toss
);
487 error
= rsb_insert(r
, &ls
->ls_rsbtbl
[b
].keep
);
491 if (dlm_no_directory(ls
))
494 if (r
->res_nodeid
== -1) {
495 rsb_clear_flag(r
, RSB_MASTER_UNCERTAIN
);
496 r
->res_first_lkid
= 0;
497 } else if (r
->res_nodeid
> 0) {
498 rsb_set_flag(r
, RSB_MASTER_UNCERTAIN
);
499 r
->res_first_lkid
= 0;
501 DLM_ASSERT(r
->res_nodeid
== 0, dlm_print_rsb(r
););
502 DLM_ASSERT(!rsb_flag(r
, RSB_MASTER_UNCERTAIN
),);
510 * Find rsb in rsbtbl and potentially create/add one
512 * Delaying the release of rsb's has a similar benefit to applications keeping
513 * NL locks on an rsb, but without the guarantee that the cached master value
514 * will still be valid when the rsb is reused. Apps aren't always smart enough
515 * to keep NL locks on an rsb that they may lock again shortly; this can lead
516 * to excessive master lookups and removals if we don't delay the release.
518 * Searching for an rsb means looking through both the normal list and toss
519 * list. When found on the toss list the rsb is moved to the normal list with
520 * ref count of 1; when found on normal list the ref count is incremented.
523 static int find_rsb(struct dlm_ls
*ls
, char *name
, int namelen
,
524 unsigned int flags
, struct dlm_rsb
**r_ret
)
526 struct dlm_rsb
*r
= NULL
;
527 uint32_t hash
, bucket
;
530 if (namelen
> DLM_RESNAME_MAXLEN
) {
535 if (dlm_no_directory(ls
))
538 hash
= jhash(name
, namelen
, 0);
539 bucket
= hash
& (ls
->ls_rsbtbl_size
- 1);
542 if (flags
& R_CREATE
) {
543 error
= pre_rsb_struct(ls
);
548 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
550 error
= _search_rsb(ls
, name
, namelen
, bucket
, flags
, &r
);
554 if (error
== -EBADR
&& !(flags
& R_CREATE
))
557 /* the rsb was found but wasn't a master copy */
558 if (error
== -ENOTBLK
)
561 error
= get_rsb_struct(ls
, name
, namelen
, &r
);
562 if (error
== -EAGAIN
) {
563 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
570 r
->res_bucket
= bucket
;
572 kref_init(&r
->res_ref
);
574 /* With no directory, the master can be set immediately */
575 if (dlm_no_directory(ls
)) {
576 int nodeid
= dlm_dir_nodeid(r
);
577 if (nodeid
== dlm_our_nodeid())
579 r
->res_nodeid
= nodeid
;
581 error
= rsb_insert(r
, &ls
->ls_rsbtbl
[bucket
].keep
);
583 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
589 /* This is only called to add a reference when the code already holds
590 a valid reference to the rsb, so there's no need for locking. */
592 static inline void hold_rsb(struct dlm_rsb
*r
)
594 kref_get(&r
->res_ref
);
597 void dlm_hold_rsb(struct dlm_rsb
*r
)
602 static void toss_rsb(struct kref
*kref
)
604 struct dlm_rsb
*r
= container_of(kref
, struct dlm_rsb
, res_ref
);
605 struct dlm_ls
*ls
= r
->res_ls
;
607 DLM_ASSERT(list_empty(&r
->res_root_list
), dlm_print_rsb(r
););
608 kref_init(&r
->res_ref
);
609 rb_erase(&r
->res_hashnode
, &ls
->ls_rsbtbl
[r
->res_bucket
].keep
);
610 rsb_insert(r
, &ls
->ls_rsbtbl
[r
->res_bucket
].toss
);
611 r
->res_toss_time
= jiffies
;
613 dlm_free_lvb(r
->res_lvbptr
);
614 r
->res_lvbptr
= NULL
;
618 /* When all references to the rsb are gone it's transferred to
619 the tossed list for later disposal. */
621 static void put_rsb(struct dlm_rsb
*r
)
623 struct dlm_ls
*ls
= r
->res_ls
;
624 uint32_t bucket
= r
->res_bucket
;
626 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
627 kref_put(&r
->res_ref
, toss_rsb
);
628 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
631 void dlm_put_rsb(struct dlm_rsb
*r
)
636 /* See comment for unhold_lkb */
638 static void unhold_rsb(struct dlm_rsb
*r
)
641 rv
= kref_put(&r
->res_ref
, toss_rsb
);
642 DLM_ASSERT(!rv
, dlm_dump_rsb(r
););
645 static void kill_rsb(struct kref
*kref
)
647 struct dlm_rsb
*r
= container_of(kref
, struct dlm_rsb
, res_ref
);
649 /* All work is done after the return from kref_put() so we
650 can release the write_lock before the remove and free. */
652 DLM_ASSERT(list_empty(&r
->res_lookup
), dlm_dump_rsb(r
););
653 DLM_ASSERT(list_empty(&r
->res_grantqueue
), dlm_dump_rsb(r
););
654 DLM_ASSERT(list_empty(&r
->res_convertqueue
), dlm_dump_rsb(r
););
655 DLM_ASSERT(list_empty(&r
->res_waitqueue
), dlm_dump_rsb(r
););
656 DLM_ASSERT(list_empty(&r
->res_root_list
), dlm_dump_rsb(r
););
657 DLM_ASSERT(list_empty(&r
->res_recover_list
), dlm_dump_rsb(r
););
660 /* Attaching/detaching lkb's from rsb's is for rsb reference counting.
661 The rsb must exist as long as any lkb's for it do. */
663 static void attach_lkb(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
666 lkb
->lkb_resource
= r
;
669 static void detach_lkb(struct dlm_lkb
*lkb
)
671 if (lkb
->lkb_resource
) {
672 put_rsb(lkb
->lkb_resource
);
673 lkb
->lkb_resource
= NULL
;
677 static int create_lkb(struct dlm_ls
*ls
, struct dlm_lkb
**lkb_ret
)
682 lkb
= dlm_allocate_lkb(ls
);
686 lkb
->lkb_nodeid
= -1;
687 lkb
->lkb_grmode
= DLM_LOCK_IV
;
688 kref_init(&lkb
->lkb_ref
);
689 INIT_LIST_HEAD(&lkb
->lkb_ownqueue
);
690 INIT_LIST_HEAD(&lkb
->lkb_rsb_lookup
);
691 INIT_LIST_HEAD(&lkb
->lkb_time_list
);
692 INIT_LIST_HEAD(&lkb
->lkb_cb_list
);
693 mutex_init(&lkb
->lkb_cb_mutex
);
694 INIT_WORK(&lkb
->lkb_cb_work
, dlm_callback_work
);
697 rv
= idr_pre_get(&ls
->ls_lkbidr
, GFP_NOFS
);
701 spin_lock(&ls
->ls_lkbidr_spin
);
702 rv
= idr_get_new_above(&ls
->ls_lkbidr
, lkb
, 1, &id
);
705 spin_unlock(&ls
->ls_lkbidr_spin
);
711 log_error(ls
, "create_lkb idr error %d", rv
);
719 static int find_lkb(struct dlm_ls
*ls
, uint32_t lkid
, struct dlm_lkb
**lkb_ret
)
723 spin_lock(&ls
->ls_lkbidr_spin
);
724 lkb
= idr_find(&ls
->ls_lkbidr
, lkid
);
726 kref_get(&lkb
->lkb_ref
);
727 spin_unlock(&ls
->ls_lkbidr_spin
);
730 return lkb
? 0 : -ENOENT
;
733 static void kill_lkb(struct kref
*kref
)
735 struct dlm_lkb
*lkb
= container_of(kref
, struct dlm_lkb
, lkb_ref
);
737 /* All work is done after the return from kref_put() so we
738 can release the write_lock before the detach_lkb */
740 DLM_ASSERT(!lkb
->lkb_status
, dlm_print_lkb(lkb
););
743 /* __put_lkb() is used when an lkb may not have an rsb attached to
744 it so we need to provide the lockspace explicitly */
746 static int __put_lkb(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
)
748 uint32_t lkid
= lkb
->lkb_id
;
750 spin_lock(&ls
->ls_lkbidr_spin
);
751 if (kref_put(&lkb
->lkb_ref
, kill_lkb
)) {
752 idr_remove(&ls
->ls_lkbidr
, lkid
);
753 spin_unlock(&ls
->ls_lkbidr_spin
);
757 /* for local/process lkbs, lvbptr points to caller's lksb */
758 if (lkb
->lkb_lvbptr
&& is_master_copy(lkb
))
759 dlm_free_lvb(lkb
->lkb_lvbptr
);
763 spin_unlock(&ls
->ls_lkbidr_spin
);
768 int dlm_put_lkb(struct dlm_lkb
*lkb
)
772 DLM_ASSERT(lkb
->lkb_resource
, dlm_print_lkb(lkb
););
773 DLM_ASSERT(lkb
->lkb_resource
->res_ls
, dlm_print_lkb(lkb
););
775 ls
= lkb
->lkb_resource
->res_ls
;
776 return __put_lkb(ls
, lkb
);
779 /* This is only called to add a reference when the code already holds
780 a valid reference to the lkb, so there's no need for locking. */
782 static inline void hold_lkb(struct dlm_lkb
*lkb
)
784 kref_get(&lkb
->lkb_ref
);
787 /* This is called when we need to remove a reference and are certain
788 it's not the last ref. e.g. del_lkb is always called between a
789 find_lkb/put_lkb and is always the inverse of a previous add_lkb.
790 put_lkb would work fine, but would involve unnecessary locking */
792 static inline void unhold_lkb(struct dlm_lkb
*lkb
)
795 rv
= kref_put(&lkb
->lkb_ref
, kill_lkb
);
796 DLM_ASSERT(!rv
, dlm_print_lkb(lkb
););
799 static void lkb_add_ordered(struct list_head
*new, struct list_head
*head
,
802 struct dlm_lkb
*lkb
= NULL
;
804 list_for_each_entry(lkb
, head
, lkb_statequeue
)
805 if (lkb
->lkb_rqmode
< mode
)
808 __list_add(new, lkb
->lkb_statequeue
.prev
, &lkb
->lkb_statequeue
);
811 /* add/remove lkb to rsb's grant/convert/wait queue */
813 static void add_lkb(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int status
)
815 kref_get(&lkb
->lkb_ref
);
817 DLM_ASSERT(!lkb
->lkb_status
, dlm_print_lkb(lkb
););
819 lkb
->lkb_timestamp
= ktime_get();
821 lkb
->lkb_status
= status
;
824 case DLM_LKSTS_WAITING
:
825 if (lkb
->lkb_exflags
& DLM_LKF_HEADQUE
)
826 list_add(&lkb
->lkb_statequeue
, &r
->res_waitqueue
);
828 list_add_tail(&lkb
->lkb_statequeue
, &r
->res_waitqueue
);
830 case DLM_LKSTS_GRANTED
:
831 /* convention says granted locks kept in order of grmode */
832 lkb_add_ordered(&lkb
->lkb_statequeue
, &r
->res_grantqueue
,
835 case DLM_LKSTS_CONVERT
:
836 if (lkb
->lkb_exflags
& DLM_LKF_HEADQUE
)
837 list_add(&lkb
->lkb_statequeue
, &r
->res_convertqueue
);
839 list_add_tail(&lkb
->lkb_statequeue
,
840 &r
->res_convertqueue
);
843 DLM_ASSERT(0, dlm_print_lkb(lkb
); printk("sts=%d\n", status
););
847 static void del_lkb(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
850 list_del(&lkb
->lkb_statequeue
);
854 static void move_lkb(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int sts
)
858 add_lkb(r
, lkb
, sts
);
862 static int msg_reply_type(int mstype
)
865 case DLM_MSG_REQUEST
:
866 return DLM_MSG_REQUEST_REPLY
;
867 case DLM_MSG_CONVERT
:
868 return DLM_MSG_CONVERT_REPLY
;
870 return DLM_MSG_UNLOCK_REPLY
;
872 return DLM_MSG_CANCEL_REPLY
;
874 return DLM_MSG_LOOKUP_REPLY
;
879 static int nodeid_warned(int nodeid
, int num_nodes
, int *warned
)
883 for (i
= 0; i
< num_nodes
; i
++) {
888 if (warned
[i
] == nodeid
)
894 void dlm_scan_waiters(struct dlm_ls
*ls
)
897 ktime_t zero
= ktime_set(0, 0);
900 u32 debug_scanned
= 0;
901 u32 debug_expired
= 0;
905 if (!dlm_config
.ci_waitwarn_us
)
908 mutex_lock(&ls
->ls_waiters_mutex
);
910 list_for_each_entry(lkb
, &ls
->ls_waiters
, lkb_wait_reply
) {
911 if (ktime_equal(lkb
->lkb_wait_time
, zero
))
916 us
= ktime_to_us(ktime_sub(ktime_get(), lkb
->lkb_wait_time
));
918 if (us
< dlm_config
.ci_waitwarn_us
)
921 lkb
->lkb_wait_time
= zero
;
924 if (us
> debug_maxus
)
928 num_nodes
= ls
->ls_num_nodes
;
929 warned
= kzalloc(num_nodes
* sizeof(int), GFP_KERNEL
);
933 if (nodeid_warned(lkb
->lkb_wait_nodeid
, num_nodes
, warned
))
936 log_error(ls
, "waitwarn %x %lld %d us check connection to "
937 "node %d", lkb
->lkb_id
, (long long)us
,
938 dlm_config
.ci_waitwarn_us
, lkb
->lkb_wait_nodeid
);
940 mutex_unlock(&ls
->ls_waiters_mutex
);
944 log_debug(ls
, "scan_waiters %u warn %u over %d us max %lld us",
945 debug_scanned
, debug_expired
,
946 dlm_config
.ci_waitwarn_us
, (long long)debug_maxus
);
949 /* add/remove lkb from global waiters list of lkb's waiting for
950 a reply from a remote node */
952 static int add_to_waiters(struct dlm_lkb
*lkb
, int mstype
, int to_nodeid
)
954 struct dlm_ls
*ls
= lkb
->lkb_resource
->res_ls
;
957 mutex_lock(&ls
->ls_waiters_mutex
);
959 if (is_overlap_unlock(lkb
) ||
960 (is_overlap_cancel(lkb
) && (mstype
== DLM_MSG_CANCEL
))) {
965 if (lkb
->lkb_wait_type
|| is_overlap_cancel(lkb
)) {
968 lkb
->lkb_flags
|= DLM_IFL_OVERLAP_UNLOCK
;
971 lkb
->lkb_flags
|= DLM_IFL_OVERLAP_CANCEL
;
977 lkb
->lkb_wait_count
++;
980 log_debug(ls
, "addwait %x cur %d overlap %d count %d f %x",
981 lkb
->lkb_id
, lkb
->lkb_wait_type
, mstype
,
982 lkb
->lkb_wait_count
, lkb
->lkb_flags
);
986 DLM_ASSERT(!lkb
->lkb_wait_count
,
988 printk("wait_count %d\n", lkb
->lkb_wait_count
););
990 lkb
->lkb_wait_count
++;
991 lkb
->lkb_wait_type
= mstype
;
992 lkb
->lkb_wait_time
= ktime_get();
993 lkb
->lkb_wait_nodeid
= to_nodeid
; /* for debugging */
995 list_add(&lkb
->lkb_wait_reply
, &ls
->ls_waiters
);
998 log_error(ls
, "addwait error %x %d flags %x %d %d %s",
999 lkb
->lkb_id
, error
, lkb
->lkb_flags
, mstype
,
1000 lkb
->lkb_wait_type
, lkb
->lkb_resource
->res_name
);
1001 mutex_unlock(&ls
->ls_waiters_mutex
);
1005 /* We clear the RESEND flag because we might be taking an lkb off the waiters
1006 list as part of process_requestqueue (e.g. a lookup that has an optimized
1007 request reply on the requestqueue) between dlm_recover_waiters_pre() which
1008 set RESEND and dlm_recover_waiters_post() */
1010 static int _remove_from_waiters(struct dlm_lkb
*lkb
, int mstype
,
1011 struct dlm_message
*ms
)
1013 struct dlm_ls
*ls
= lkb
->lkb_resource
->res_ls
;
1014 int overlap_done
= 0;
1016 if (is_overlap_unlock(lkb
) && (mstype
== DLM_MSG_UNLOCK_REPLY
)) {
1017 log_debug(ls
, "remwait %x unlock_reply overlap", lkb
->lkb_id
);
1018 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_UNLOCK
;
1023 if (is_overlap_cancel(lkb
) && (mstype
== DLM_MSG_CANCEL_REPLY
)) {
1024 log_debug(ls
, "remwait %x cancel_reply overlap", lkb
->lkb_id
);
1025 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_CANCEL
;
1030 /* Cancel state was preemptively cleared by a successful convert,
1031 see next comment, nothing to do. */
1033 if ((mstype
== DLM_MSG_CANCEL_REPLY
) &&
1034 (lkb
->lkb_wait_type
!= DLM_MSG_CANCEL
)) {
1035 log_debug(ls
, "remwait %x cancel_reply wait_type %d",
1036 lkb
->lkb_id
, lkb
->lkb_wait_type
);
1040 /* Remove for the convert reply, and premptively remove for the
1041 cancel reply. A convert has been granted while there's still
1042 an outstanding cancel on it (the cancel is moot and the result
1043 in the cancel reply should be 0). We preempt the cancel reply
1044 because the app gets the convert result and then can follow up
1045 with another op, like convert. This subsequent op would see the
1046 lingering state of the cancel and fail with -EBUSY. */
1048 if ((mstype
== DLM_MSG_CONVERT_REPLY
) &&
1049 (lkb
->lkb_wait_type
== DLM_MSG_CONVERT
) &&
1050 is_overlap_cancel(lkb
) && ms
&& !ms
->m_result
) {
1051 log_debug(ls
, "remwait %x convert_reply zap overlap_cancel",
1053 lkb
->lkb_wait_type
= 0;
1054 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_CANCEL
;
1055 lkb
->lkb_wait_count
--;
1059 /* N.B. type of reply may not always correspond to type of original
1060 msg due to lookup->request optimization, verify others? */
1062 if (lkb
->lkb_wait_type
) {
1063 lkb
->lkb_wait_type
= 0;
1067 log_error(ls
, "remwait error %x reply %d flags %x no wait_type",
1068 lkb
->lkb_id
, mstype
, lkb
->lkb_flags
);
1072 /* the force-unlock/cancel has completed and we haven't recvd a reply
1073 to the op that was in progress prior to the unlock/cancel; we
1074 give up on any reply to the earlier op. FIXME: not sure when/how
1075 this would happen */
1077 if (overlap_done
&& lkb
->lkb_wait_type
) {
1078 log_error(ls
, "remwait error %x reply %d wait_type %d overlap",
1079 lkb
->lkb_id
, mstype
, lkb
->lkb_wait_type
);
1080 lkb
->lkb_wait_count
--;
1081 lkb
->lkb_wait_type
= 0;
1084 DLM_ASSERT(lkb
->lkb_wait_count
, dlm_print_lkb(lkb
););
1086 lkb
->lkb_flags
&= ~DLM_IFL_RESEND
;
1087 lkb
->lkb_wait_count
--;
1088 if (!lkb
->lkb_wait_count
)
1089 list_del_init(&lkb
->lkb_wait_reply
);
1094 static int remove_from_waiters(struct dlm_lkb
*lkb
, int mstype
)
1096 struct dlm_ls
*ls
= lkb
->lkb_resource
->res_ls
;
1099 mutex_lock(&ls
->ls_waiters_mutex
);
1100 error
= _remove_from_waiters(lkb
, mstype
, NULL
);
1101 mutex_unlock(&ls
->ls_waiters_mutex
);
1105 /* Handles situations where we might be processing a "fake" or "stub" reply in
1106 which we can't try to take waiters_mutex again. */
1108 static int remove_from_waiters_ms(struct dlm_lkb
*lkb
, struct dlm_message
*ms
)
1110 struct dlm_ls
*ls
= lkb
->lkb_resource
->res_ls
;
1113 if (ms
->m_flags
!= DLM_IFL_STUB_MS
)
1114 mutex_lock(&ls
->ls_waiters_mutex
);
1115 error
= _remove_from_waiters(lkb
, ms
->m_type
, ms
);
1116 if (ms
->m_flags
!= DLM_IFL_STUB_MS
)
1117 mutex_unlock(&ls
->ls_waiters_mutex
);
1121 static void dir_remove(struct dlm_rsb
*r
)
1125 if (dlm_no_directory(r
->res_ls
))
1128 to_nodeid
= dlm_dir_nodeid(r
);
1129 if (to_nodeid
!= dlm_our_nodeid())
1132 dlm_dir_remove_entry(r
->res_ls
, to_nodeid
,
1133 r
->res_name
, r
->res_length
);
1136 /* FIXME: make this more efficient */
1138 static int shrink_bucket(struct dlm_ls
*ls
, int b
)
1142 int count
= 0, found
;
1146 spin_lock(&ls
->ls_rsbtbl
[b
].lock
);
1147 for (n
= rb_first(&ls
->ls_rsbtbl
[b
].toss
); n
; n
= rb_next(n
)) {
1148 r
= rb_entry(n
, struct dlm_rsb
, res_hashnode
);
1149 if (!time_after_eq(jiffies
, r
->res_toss_time
+
1150 dlm_config
.ci_toss_secs
* HZ
))
1157 spin_unlock(&ls
->ls_rsbtbl
[b
].lock
);
1161 if (kref_put(&r
->res_ref
, kill_rsb
)) {
1162 rb_erase(&r
->res_hashnode
, &ls
->ls_rsbtbl
[b
].toss
);
1163 spin_unlock(&ls
->ls_rsbtbl
[b
].lock
);
1170 spin_unlock(&ls
->ls_rsbtbl
[b
].lock
);
1171 log_error(ls
, "tossed rsb in use %s", r
->res_name
);
1178 void dlm_scan_rsbs(struct dlm_ls
*ls
)
1182 for (i
= 0; i
< ls
->ls_rsbtbl_size
; i
++) {
1183 shrink_bucket(ls
, i
);
1184 if (dlm_locking_stopped(ls
))
1190 static void add_timeout(struct dlm_lkb
*lkb
)
1192 struct dlm_ls
*ls
= lkb
->lkb_resource
->res_ls
;
1194 if (is_master_copy(lkb
))
1197 if (test_bit(LSFL_TIMEWARN
, &ls
->ls_flags
) &&
1198 !(lkb
->lkb_exflags
& DLM_LKF_NODLCKWT
)) {
1199 lkb
->lkb_flags
|= DLM_IFL_WATCH_TIMEWARN
;
1202 if (lkb
->lkb_exflags
& DLM_LKF_TIMEOUT
)
1207 DLM_ASSERT(list_empty(&lkb
->lkb_time_list
), dlm_print_lkb(lkb
););
1208 mutex_lock(&ls
->ls_timeout_mutex
);
1210 list_add_tail(&lkb
->lkb_time_list
, &ls
->ls_timeout
);
1211 mutex_unlock(&ls
->ls_timeout_mutex
);
1214 static void del_timeout(struct dlm_lkb
*lkb
)
1216 struct dlm_ls
*ls
= lkb
->lkb_resource
->res_ls
;
1218 mutex_lock(&ls
->ls_timeout_mutex
);
1219 if (!list_empty(&lkb
->lkb_time_list
)) {
1220 list_del_init(&lkb
->lkb_time_list
);
1223 mutex_unlock(&ls
->ls_timeout_mutex
);
1226 /* FIXME: is it safe to look at lkb_exflags, lkb_flags, lkb_timestamp, and
1227 lkb_lksb_timeout without lock_rsb? Note: we can't lock timeout_mutex
1228 and then lock rsb because of lock ordering in add_timeout. We may need
1229 to specify some special timeout-related bits in the lkb that are just to
1230 be accessed under the timeout_mutex. */
1232 void dlm_scan_timeout(struct dlm_ls
*ls
)
1235 struct dlm_lkb
*lkb
;
1236 int do_cancel
, do_warn
;
1240 if (dlm_locking_stopped(ls
))
1245 mutex_lock(&ls
->ls_timeout_mutex
);
1246 list_for_each_entry(lkb
, &ls
->ls_timeout
, lkb_time_list
) {
1248 wait_us
= ktime_to_us(ktime_sub(ktime_get(),
1249 lkb
->lkb_timestamp
));
1251 if ((lkb
->lkb_exflags
& DLM_LKF_TIMEOUT
) &&
1252 wait_us
>= (lkb
->lkb_timeout_cs
* 10000))
1255 if ((lkb
->lkb_flags
& DLM_IFL_WATCH_TIMEWARN
) &&
1256 wait_us
>= dlm_config
.ci_timewarn_cs
* 10000)
1259 if (!do_cancel
&& !do_warn
)
1264 mutex_unlock(&ls
->ls_timeout_mutex
);
1266 if (!do_cancel
&& !do_warn
)
1269 r
= lkb
->lkb_resource
;
1274 /* clear flag so we only warn once */
1275 lkb
->lkb_flags
&= ~DLM_IFL_WATCH_TIMEWARN
;
1276 if (!(lkb
->lkb_exflags
& DLM_LKF_TIMEOUT
))
1278 dlm_timeout_warn(lkb
);
1282 log_debug(ls
, "timeout cancel %x node %d %s",
1283 lkb
->lkb_id
, lkb
->lkb_nodeid
, r
->res_name
);
1284 lkb
->lkb_flags
&= ~DLM_IFL_WATCH_TIMEWARN
;
1285 lkb
->lkb_flags
|= DLM_IFL_TIMEOUT_CANCEL
;
1287 _cancel_lock(r
, lkb
);
1296 /* This is only called by dlm_recoverd, and we rely on dlm_ls_stop() stopping
1297 dlm_recoverd before checking/setting ls_recover_begin. */
1299 void dlm_adjust_timeouts(struct dlm_ls
*ls
)
1301 struct dlm_lkb
*lkb
;
1302 u64 adj_us
= jiffies_to_usecs(jiffies
- ls
->ls_recover_begin
);
1304 ls
->ls_recover_begin
= 0;
1305 mutex_lock(&ls
->ls_timeout_mutex
);
1306 list_for_each_entry(lkb
, &ls
->ls_timeout
, lkb_time_list
)
1307 lkb
->lkb_timestamp
= ktime_add_us(lkb
->lkb_timestamp
, adj_us
);
1308 mutex_unlock(&ls
->ls_timeout_mutex
);
1310 if (!dlm_config
.ci_waitwarn_us
)
1313 mutex_lock(&ls
->ls_waiters_mutex
);
1314 list_for_each_entry(lkb
, &ls
->ls_waiters
, lkb_wait_reply
) {
1315 if (ktime_to_us(lkb
->lkb_wait_time
))
1316 lkb
->lkb_wait_time
= ktime_get();
1318 mutex_unlock(&ls
->ls_waiters_mutex
);
1321 /* lkb is master or local copy */
1323 static void set_lvb_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1325 int b
, len
= r
->res_ls
->ls_lvblen
;
1327 /* b=1 lvb returned to caller
1328 b=0 lvb written to rsb or invalidated
1331 b
= dlm_lvb_operations
[lkb
->lkb_grmode
+ 1][lkb
->lkb_rqmode
+ 1];
1334 if (!lkb
->lkb_lvbptr
)
1337 if (!(lkb
->lkb_exflags
& DLM_LKF_VALBLK
))
1343 memcpy(lkb
->lkb_lvbptr
, r
->res_lvbptr
, len
);
1344 lkb
->lkb_lvbseq
= r
->res_lvbseq
;
1346 } else if (b
== 0) {
1347 if (lkb
->lkb_exflags
& DLM_LKF_IVVALBLK
) {
1348 rsb_set_flag(r
, RSB_VALNOTVALID
);
1352 if (!lkb
->lkb_lvbptr
)
1355 if (!(lkb
->lkb_exflags
& DLM_LKF_VALBLK
))
1359 r
->res_lvbptr
= dlm_allocate_lvb(r
->res_ls
);
1364 memcpy(r
->res_lvbptr
, lkb
->lkb_lvbptr
, len
);
1366 lkb
->lkb_lvbseq
= r
->res_lvbseq
;
1367 rsb_clear_flag(r
, RSB_VALNOTVALID
);
1370 if (rsb_flag(r
, RSB_VALNOTVALID
))
1371 lkb
->lkb_sbflags
|= DLM_SBF_VALNOTVALID
;
1374 static void set_lvb_unlock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1376 if (lkb
->lkb_grmode
< DLM_LOCK_PW
)
1379 if (lkb
->lkb_exflags
& DLM_LKF_IVVALBLK
) {
1380 rsb_set_flag(r
, RSB_VALNOTVALID
);
1384 if (!lkb
->lkb_lvbptr
)
1387 if (!(lkb
->lkb_exflags
& DLM_LKF_VALBLK
))
1391 r
->res_lvbptr
= dlm_allocate_lvb(r
->res_ls
);
1396 memcpy(r
->res_lvbptr
, lkb
->lkb_lvbptr
, r
->res_ls
->ls_lvblen
);
1398 rsb_clear_flag(r
, RSB_VALNOTVALID
);
1401 /* lkb is process copy (pc) */
1403 static void set_lvb_lock_pc(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
1404 struct dlm_message
*ms
)
1408 if (!lkb
->lkb_lvbptr
)
1411 if (!(lkb
->lkb_exflags
& DLM_LKF_VALBLK
))
1414 b
= dlm_lvb_operations
[lkb
->lkb_grmode
+ 1][lkb
->lkb_rqmode
+ 1];
1416 int len
= receive_extralen(ms
);
1417 if (len
> DLM_RESNAME_MAXLEN
)
1418 len
= DLM_RESNAME_MAXLEN
;
1419 memcpy(lkb
->lkb_lvbptr
, ms
->m_extra
, len
);
1420 lkb
->lkb_lvbseq
= ms
->m_lvbseq
;
1424 /* Manipulate lkb's on rsb's convert/granted/waiting queues
1425 remove_lock -- used for unlock, removes lkb from granted
1426 revert_lock -- used for cancel, moves lkb from convert to granted
1427 grant_lock -- used for request and convert, adds lkb to granted or
1428 moves lkb from convert or waiting to granted
1430 Each of these is used for master or local copy lkb's. There is
1431 also a _pc() variation used to make the corresponding change on
1432 a process copy (pc) lkb. */
1434 static void _remove_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1437 lkb
->lkb_grmode
= DLM_LOCK_IV
;
1438 /* this unhold undoes the original ref from create_lkb()
1439 so this leads to the lkb being freed */
1443 static void remove_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1445 set_lvb_unlock(r
, lkb
);
1446 _remove_lock(r
, lkb
);
1449 static void remove_lock_pc(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1451 _remove_lock(r
, lkb
);
1454 /* returns: 0 did nothing
1455 1 moved lock to granted
1458 static int revert_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1462 lkb
->lkb_rqmode
= DLM_LOCK_IV
;
1464 switch (lkb
->lkb_status
) {
1465 case DLM_LKSTS_GRANTED
:
1467 case DLM_LKSTS_CONVERT
:
1468 move_lkb(r
, lkb
, DLM_LKSTS_GRANTED
);
1471 case DLM_LKSTS_WAITING
:
1473 lkb
->lkb_grmode
= DLM_LOCK_IV
;
1474 /* this unhold undoes the original ref from create_lkb()
1475 so this leads to the lkb being freed */
1480 log_print("invalid status for revert %d", lkb
->lkb_status
);
1485 static int revert_lock_pc(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1487 return revert_lock(r
, lkb
);
1490 static void _grant_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1492 if (lkb
->lkb_grmode
!= lkb
->lkb_rqmode
) {
1493 lkb
->lkb_grmode
= lkb
->lkb_rqmode
;
1494 if (lkb
->lkb_status
)
1495 move_lkb(r
, lkb
, DLM_LKSTS_GRANTED
);
1497 add_lkb(r
, lkb
, DLM_LKSTS_GRANTED
);
1500 lkb
->lkb_rqmode
= DLM_LOCK_IV
;
1503 static void grant_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1505 set_lvb_lock(r
, lkb
);
1506 _grant_lock(r
, lkb
);
1507 lkb
->lkb_highbast
= 0;
1510 static void grant_lock_pc(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
1511 struct dlm_message
*ms
)
1513 set_lvb_lock_pc(r
, lkb
, ms
);
1514 _grant_lock(r
, lkb
);
1517 /* called by grant_pending_locks() which means an async grant message must
1518 be sent to the requesting node in addition to granting the lock if the
1519 lkb belongs to a remote node. */
1521 static void grant_lock_pending(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1524 if (is_master_copy(lkb
))
1527 queue_cast(r
, lkb
, 0);
1530 /* The special CONVDEADLK, ALTPR and ALTCW flags allow the master to
1531 change the granted/requested modes. We're munging things accordingly in
1533 CONVDEADLK: our grmode may have been forced down to NL to resolve a
1535 ALTPR/ALTCW: our rqmode may have been changed to PR or CW to become
1536 compatible with other granted locks */
1538 static void munge_demoted(struct dlm_lkb
*lkb
)
1540 if (lkb
->lkb_rqmode
== DLM_LOCK_IV
|| lkb
->lkb_grmode
== DLM_LOCK_IV
) {
1541 log_print("munge_demoted %x invalid modes gr %d rq %d",
1542 lkb
->lkb_id
, lkb
->lkb_grmode
, lkb
->lkb_rqmode
);
1546 lkb
->lkb_grmode
= DLM_LOCK_NL
;
1549 static void munge_altmode(struct dlm_lkb
*lkb
, struct dlm_message
*ms
)
1551 if (ms
->m_type
!= DLM_MSG_REQUEST_REPLY
&&
1552 ms
->m_type
!= DLM_MSG_GRANT
) {
1553 log_print("munge_altmode %x invalid reply type %d",
1554 lkb
->lkb_id
, ms
->m_type
);
1558 if (lkb
->lkb_exflags
& DLM_LKF_ALTPR
)
1559 lkb
->lkb_rqmode
= DLM_LOCK_PR
;
1560 else if (lkb
->lkb_exflags
& DLM_LKF_ALTCW
)
1561 lkb
->lkb_rqmode
= DLM_LOCK_CW
;
1563 log_print("munge_altmode invalid exflags %x", lkb
->lkb_exflags
);
1568 static inline int first_in_list(struct dlm_lkb
*lkb
, struct list_head
*head
)
1570 struct dlm_lkb
*first
= list_entry(head
->next
, struct dlm_lkb
,
1572 if (lkb
->lkb_id
== first
->lkb_id
)
1578 /* Check if the given lkb conflicts with another lkb on the queue. */
1580 static int queue_conflict(struct list_head
*head
, struct dlm_lkb
*lkb
)
1582 struct dlm_lkb
*this;
1584 list_for_each_entry(this, head
, lkb_statequeue
) {
1587 if (!modes_compat(this, lkb
))
1594 * "A conversion deadlock arises with a pair of lock requests in the converting
1595 * queue for one resource. The granted mode of each lock blocks the requested
1596 * mode of the other lock."
1598 * Part 2: if the granted mode of lkb is preventing an earlier lkb in the
1599 * convert queue from being granted, then deadlk/demote lkb.
1602 * Granted Queue: empty
1603 * Convert Queue: NL->EX (first lock)
1604 * PR->EX (second lock)
1606 * The first lock can't be granted because of the granted mode of the second
1607 * lock and the second lock can't be granted because it's not first in the
1608 * list. We either cancel lkb's conversion (PR->EX) and return EDEADLK, or we
1609 * demote the granted mode of lkb (from PR to NL) if it has the CONVDEADLK
1610 * flag set and return DEMOTED in the lksb flags.
1612 * Originally, this function detected conv-deadlk in a more limited scope:
1613 * - if !modes_compat(lkb1, lkb2) && !modes_compat(lkb2, lkb1), or
1614 * - if lkb1 was the first entry in the queue (not just earlier), and was
1615 * blocked by the granted mode of lkb2, and there was nothing on the
1616 * granted queue preventing lkb1 from being granted immediately, i.e.
1617 * lkb2 was the only thing preventing lkb1 from being granted.
1619 * That second condition meant we'd only say there was conv-deadlk if
1620 * resolving it (by demotion) would lead to the first lock on the convert
1621 * queue being granted right away. It allowed conversion deadlocks to exist
1622 * between locks on the convert queue while they couldn't be granted anyway.
1624 * Now, we detect and take action on conversion deadlocks immediately when
1625 * they're created, even if they may not be immediately consequential. If
1626 * lkb1 exists anywhere in the convert queue and lkb2 comes in with a granted
1627 * mode that would prevent lkb1's conversion from being granted, we do a
1628 * deadlk/demote on lkb2 right away and don't let it onto the convert queue.
1629 * I think this means that the lkb_is_ahead condition below should always
1630 * be zero, i.e. there will never be conv-deadlk between two locks that are
1631 * both already on the convert queue.
1634 static int conversion_deadlock_detect(struct dlm_rsb
*r
, struct dlm_lkb
*lkb2
)
1636 struct dlm_lkb
*lkb1
;
1637 int lkb_is_ahead
= 0;
1639 list_for_each_entry(lkb1
, &r
->res_convertqueue
, lkb_statequeue
) {
1645 if (!lkb_is_ahead
) {
1646 if (!modes_compat(lkb2
, lkb1
))
1649 if (!modes_compat(lkb2
, lkb1
) &&
1650 !modes_compat(lkb1
, lkb2
))
1658 * Return 1 if the lock can be granted, 0 otherwise.
1659 * Also detect and resolve conversion deadlocks.
1661 * lkb is the lock to be granted
1663 * now is 1 if the function is being called in the context of the
1664 * immediate request, it is 0 if called later, after the lock has been
1667 * References are from chapter 6 of "VAXcluster Principles" by Roy Davis
1670 static int _can_be_granted(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int now
)
1672 int8_t conv
= (lkb
->lkb_grmode
!= DLM_LOCK_IV
);
1675 * 6-10: Version 5.4 introduced an option to address the phenomenon of
1676 * a new request for a NL mode lock being blocked.
1678 * 6-11: If the optional EXPEDITE flag is used with the new NL mode
1679 * request, then it would be granted. In essence, the use of this flag
1680 * tells the Lock Manager to expedite theis request by not considering
1681 * what may be in the CONVERTING or WAITING queues... As of this
1682 * writing, the EXPEDITE flag can be used only with new requests for NL
1683 * mode locks. This flag is not valid for conversion requests.
1685 * A shortcut. Earlier checks return an error if EXPEDITE is used in a
1686 * conversion or used with a non-NL requested mode. We also know an
1687 * EXPEDITE request is always granted immediately, so now must always
1688 * be 1. The full condition to grant an expedite request: (now &&
1689 * !conv && lkb->rqmode == DLM_LOCK_NL && (flags & EXPEDITE)) can
1690 * therefore be shortened to just checking the flag.
1693 if (lkb
->lkb_exflags
& DLM_LKF_EXPEDITE
)
1697 * A shortcut. Without this, !queue_conflict(grantqueue, lkb) would be
1698 * added to the remaining conditions.
1701 if (queue_conflict(&r
->res_grantqueue
, lkb
))
1705 * 6-3: By default, a conversion request is immediately granted if the
1706 * requested mode is compatible with the modes of all other granted
1710 if (queue_conflict(&r
->res_convertqueue
, lkb
))
1714 * 6-5: But the default algorithm for deciding whether to grant or
1715 * queue conversion requests does not by itself guarantee that such
1716 * requests are serviced on a "first come first serve" basis. This, in
1717 * turn, can lead to a phenomenon known as "indefinate postponement".
1719 * 6-7: This issue is dealt with by using the optional QUECVT flag with
1720 * the system service employed to request a lock conversion. This flag
1721 * forces certain conversion requests to be queued, even if they are
1722 * compatible with the granted modes of other locks on the same
1723 * resource. Thus, the use of this flag results in conversion requests
1724 * being ordered on a "first come first servce" basis.
1726 * DCT: This condition is all about new conversions being able to occur
1727 * "in place" while the lock remains on the granted queue (assuming
1728 * nothing else conflicts.) IOW if QUECVT isn't set, a conversion
1729 * doesn't _have_ to go onto the convert queue where it's processed in
1730 * order. The "now" variable is necessary to distinguish converts
1731 * being received and processed for the first time now, because once a
1732 * convert is moved to the conversion queue the condition below applies
1733 * requiring fifo granting.
1736 if (now
&& conv
&& !(lkb
->lkb_exflags
& DLM_LKF_QUECVT
))
1740 * The NOORDER flag is set to avoid the standard vms rules on grant
1744 if (lkb
->lkb_exflags
& DLM_LKF_NOORDER
)
1748 * 6-3: Once in that queue [CONVERTING], a conversion request cannot be
1749 * granted until all other conversion requests ahead of it are granted
1753 if (!now
&& conv
&& first_in_list(lkb
, &r
->res_convertqueue
))
1757 * 6-4: By default, a new request is immediately granted only if all
1758 * three of the following conditions are satisfied when the request is
1760 * - The queue of ungranted conversion requests for the resource is
1762 * - The queue of ungranted new requests for the resource is empty.
1763 * - The mode of the new request is compatible with the most
1764 * restrictive mode of all granted locks on the resource.
1767 if (now
&& !conv
&& list_empty(&r
->res_convertqueue
) &&
1768 list_empty(&r
->res_waitqueue
))
1772 * 6-4: Once a lock request is in the queue of ungranted new requests,
1773 * it cannot be granted until the queue of ungranted conversion
1774 * requests is empty, all ungranted new requests ahead of it are
1775 * granted and/or canceled, and it is compatible with the granted mode
1776 * of the most restrictive lock granted on the resource.
1779 if (!now
&& !conv
&& list_empty(&r
->res_convertqueue
) &&
1780 first_in_list(lkb
, &r
->res_waitqueue
))
1786 static int can_be_granted(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int now
,
1790 int8_t alt
= 0, rqmode
= lkb
->lkb_rqmode
;
1791 int8_t is_convert
= (lkb
->lkb_grmode
!= DLM_LOCK_IV
);
1796 rv
= _can_be_granted(r
, lkb
, now
);
1801 * The CONVDEADLK flag is non-standard and tells the dlm to resolve
1802 * conversion deadlocks by demoting grmode to NL, otherwise the dlm
1803 * cancels one of the locks.
1806 if (is_convert
&& can_be_queued(lkb
) &&
1807 conversion_deadlock_detect(r
, lkb
)) {
1808 if (lkb
->lkb_exflags
& DLM_LKF_CONVDEADLK
) {
1809 lkb
->lkb_grmode
= DLM_LOCK_NL
;
1810 lkb
->lkb_sbflags
|= DLM_SBF_DEMOTED
;
1811 } else if (!(lkb
->lkb_exflags
& DLM_LKF_NODLCKWT
)) {
1815 log_print("can_be_granted deadlock %x now %d",
1824 * The ALTPR and ALTCW flags are non-standard and tell the dlm to try
1825 * to grant a request in a mode other than the normal rqmode. It's a
1826 * simple way to provide a big optimization to applications that can
1830 if (rqmode
!= DLM_LOCK_PR
&& (lkb
->lkb_exflags
& DLM_LKF_ALTPR
))
1832 else if (rqmode
!= DLM_LOCK_CW
&& (lkb
->lkb_exflags
& DLM_LKF_ALTCW
))
1836 lkb
->lkb_rqmode
= alt
;
1837 rv
= _can_be_granted(r
, lkb
, now
);
1839 lkb
->lkb_sbflags
|= DLM_SBF_ALTMODE
;
1841 lkb
->lkb_rqmode
= rqmode
;
1847 /* FIXME: I don't think that can_be_granted() can/will demote or find deadlock
1848 for locks pending on the convert list. Once verified (watch for these
1849 log_prints), we should be able to just call _can_be_granted() and not
1850 bother with the demote/deadlk cases here (and there's no easy way to deal
1851 with a deadlk here, we'd have to generate something like grant_lock with
1852 the deadlk error.) */
1854 /* Returns the highest requested mode of all blocked conversions; sets
1855 cw if there's a blocked conversion to DLM_LOCK_CW. */
1857 static int grant_pending_convert(struct dlm_rsb
*r
, int high
, int *cw
)
1859 struct dlm_lkb
*lkb
, *s
;
1860 int hi
, demoted
, quit
, grant_restart
, demote_restart
;
1869 list_for_each_entry_safe(lkb
, s
, &r
->res_convertqueue
, lkb_statequeue
) {
1870 demoted
= is_demoted(lkb
);
1873 if (can_be_granted(r
, lkb
, 0, &deadlk
)) {
1874 grant_lock_pending(r
, lkb
);
1879 if (!demoted
&& is_demoted(lkb
)) {
1880 log_print("WARN: pending demoted %x node %d %s",
1881 lkb
->lkb_id
, lkb
->lkb_nodeid
, r
->res_name
);
1887 log_print("WARN: pending deadlock %x node %d %s",
1888 lkb
->lkb_id
, lkb
->lkb_nodeid
, r
->res_name
);
1893 hi
= max_t(int, lkb
->lkb_rqmode
, hi
);
1895 if (cw
&& lkb
->lkb_rqmode
== DLM_LOCK_CW
)
1901 if (demote_restart
&& !quit
) {
1906 return max_t(int, high
, hi
);
1909 static int grant_pending_wait(struct dlm_rsb
*r
, int high
, int *cw
)
1911 struct dlm_lkb
*lkb
, *s
;
1913 list_for_each_entry_safe(lkb
, s
, &r
->res_waitqueue
, lkb_statequeue
) {
1914 if (can_be_granted(r
, lkb
, 0, NULL
))
1915 grant_lock_pending(r
, lkb
);
1917 high
= max_t(int, lkb
->lkb_rqmode
, high
);
1918 if (lkb
->lkb_rqmode
== DLM_LOCK_CW
)
1926 /* cw of 1 means there's a lock with a rqmode of DLM_LOCK_CW that's blocked
1927 on either the convert or waiting queue.
1928 high is the largest rqmode of all locks blocked on the convert or
1931 static int lock_requires_bast(struct dlm_lkb
*gr
, int high
, int cw
)
1933 if (gr
->lkb_grmode
== DLM_LOCK_PR
&& cw
) {
1934 if (gr
->lkb_highbast
< DLM_LOCK_EX
)
1939 if (gr
->lkb_highbast
< high
&&
1940 !__dlm_compat_matrix
[gr
->lkb_grmode
+1][high
+1])
1945 static void grant_pending_locks(struct dlm_rsb
*r
)
1947 struct dlm_lkb
*lkb
, *s
;
1948 int high
= DLM_LOCK_IV
;
1951 DLM_ASSERT(is_master(r
), dlm_dump_rsb(r
););
1953 high
= grant_pending_convert(r
, high
, &cw
);
1954 high
= grant_pending_wait(r
, high
, &cw
);
1956 if (high
== DLM_LOCK_IV
)
1960 * If there are locks left on the wait/convert queue then send blocking
1961 * ASTs to granted locks based on the largest requested mode (high)
1965 list_for_each_entry_safe(lkb
, s
, &r
->res_grantqueue
, lkb_statequeue
) {
1966 if (lkb
->lkb_bastfn
&& lock_requires_bast(lkb
, high
, cw
)) {
1967 if (cw
&& high
== DLM_LOCK_PR
&&
1968 lkb
->lkb_grmode
== DLM_LOCK_PR
)
1969 queue_bast(r
, lkb
, DLM_LOCK_CW
);
1971 queue_bast(r
, lkb
, high
);
1972 lkb
->lkb_highbast
= high
;
1977 static int modes_require_bast(struct dlm_lkb
*gr
, struct dlm_lkb
*rq
)
1979 if ((gr
->lkb_grmode
== DLM_LOCK_PR
&& rq
->lkb_rqmode
== DLM_LOCK_CW
) ||
1980 (gr
->lkb_grmode
== DLM_LOCK_CW
&& rq
->lkb_rqmode
== DLM_LOCK_PR
)) {
1981 if (gr
->lkb_highbast
< DLM_LOCK_EX
)
1986 if (gr
->lkb_highbast
< rq
->lkb_rqmode
&& !modes_compat(gr
, rq
))
1991 static void send_bast_queue(struct dlm_rsb
*r
, struct list_head
*head
,
1992 struct dlm_lkb
*lkb
)
1996 list_for_each_entry(gr
, head
, lkb_statequeue
) {
1997 /* skip self when sending basts to convertqueue */
2000 if (gr
->lkb_bastfn
&& modes_require_bast(gr
, lkb
)) {
2001 queue_bast(r
, gr
, lkb
->lkb_rqmode
);
2002 gr
->lkb_highbast
= lkb
->lkb_rqmode
;
2007 static void send_blocking_asts(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2009 send_bast_queue(r
, &r
->res_grantqueue
, lkb
);
2012 static void send_blocking_asts_all(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2014 send_bast_queue(r
, &r
->res_grantqueue
, lkb
);
2015 send_bast_queue(r
, &r
->res_convertqueue
, lkb
);
2018 /* set_master(r, lkb) -- set the master nodeid of a resource
2020 The purpose of this function is to set the nodeid field in the given
2021 lkb using the nodeid field in the given rsb. If the rsb's nodeid is
2022 known, it can just be copied to the lkb and the function will return
2023 0. If the rsb's nodeid is _not_ known, it needs to be looked up
2024 before it can be copied to the lkb.
2026 When the rsb nodeid is being looked up remotely, the initial lkb
2027 causing the lookup is kept on the ls_waiters list waiting for the
2028 lookup reply. Other lkb's waiting for the same rsb lookup are kept
2029 on the rsb's res_lookup list until the master is verified.
2032 0: nodeid is set in rsb/lkb and the caller should go ahead and use it
2033 1: the rsb master is not available and the lkb has been placed on
2037 static int set_master(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2039 struct dlm_ls
*ls
= r
->res_ls
;
2040 int i
, error
, dir_nodeid
, ret_nodeid
, our_nodeid
= dlm_our_nodeid();
2042 if (rsb_flag(r
, RSB_MASTER_UNCERTAIN
)) {
2043 rsb_clear_flag(r
, RSB_MASTER_UNCERTAIN
);
2044 r
->res_first_lkid
= lkb
->lkb_id
;
2045 lkb
->lkb_nodeid
= r
->res_nodeid
;
2049 if (r
->res_first_lkid
&& r
->res_first_lkid
!= lkb
->lkb_id
) {
2050 list_add_tail(&lkb
->lkb_rsb_lookup
, &r
->res_lookup
);
2054 if (r
->res_nodeid
== 0) {
2055 lkb
->lkb_nodeid
= 0;
2059 if (r
->res_nodeid
> 0) {
2060 lkb
->lkb_nodeid
= r
->res_nodeid
;
2064 DLM_ASSERT(r
->res_nodeid
== -1, dlm_dump_rsb(r
););
2066 dir_nodeid
= dlm_dir_nodeid(r
);
2068 if (dir_nodeid
!= our_nodeid
) {
2069 r
->res_first_lkid
= lkb
->lkb_id
;
2070 send_lookup(r
, lkb
);
2074 for (i
= 0; i
< 2; i
++) {
2075 /* It's possible for dlm_scand to remove an old rsb for
2076 this same resource from the toss list, us to create
2077 a new one, look up the master locally, and find it
2078 already exists just before dlm_scand does the
2079 dir_remove() on the previous rsb. */
2081 error
= dlm_dir_lookup(ls
, our_nodeid
, r
->res_name
,
2082 r
->res_length
, &ret_nodeid
);
2085 log_debug(ls
, "dir_lookup error %d %s", error
, r
->res_name
);
2088 if (error
&& error
!= -EEXIST
)
2091 if (ret_nodeid
== our_nodeid
) {
2092 r
->res_first_lkid
= 0;
2094 lkb
->lkb_nodeid
= 0;
2096 r
->res_first_lkid
= lkb
->lkb_id
;
2097 r
->res_nodeid
= ret_nodeid
;
2098 lkb
->lkb_nodeid
= ret_nodeid
;
2103 static void process_lookup_list(struct dlm_rsb
*r
)
2105 struct dlm_lkb
*lkb
, *safe
;
2107 list_for_each_entry_safe(lkb
, safe
, &r
->res_lookup
, lkb_rsb_lookup
) {
2108 list_del_init(&lkb
->lkb_rsb_lookup
);
2109 _request_lock(r
, lkb
);
2114 /* confirm_master -- confirm (or deny) an rsb's master nodeid */
2116 static void confirm_master(struct dlm_rsb
*r
, int error
)
2118 struct dlm_lkb
*lkb
;
2120 if (!r
->res_first_lkid
)
2126 r
->res_first_lkid
= 0;
2127 process_lookup_list(r
);
2133 /* the remote request failed and won't be retried (it was
2134 a NOQUEUE, or has been canceled/unlocked); make a waiting
2135 lkb the first_lkid */
2137 r
->res_first_lkid
= 0;
2139 if (!list_empty(&r
->res_lookup
)) {
2140 lkb
= list_entry(r
->res_lookup
.next
, struct dlm_lkb
,
2142 list_del_init(&lkb
->lkb_rsb_lookup
);
2143 r
->res_first_lkid
= lkb
->lkb_id
;
2144 _request_lock(r
, lkb
);
2149 log_error(r
->res_ls
, "confirm_master unknown error %d", error
);
2153 static int set_lock_args(int mode
, struct dlm_lksb
*lksb
, uint32_t flags
,
2154 int namelen
, unsigned long timeout_cs
,
2155 void (*ast
) (void *astparam
),
2157 void (*bast
) (void *astparam
, int mode
),
2158 struct dlm_args
*args
)
2162 /* check for invalid arg usage */
2164 if (mode
< 0 || mode
> DLM_LOCK_EX
)
2167 if (!(flags
& DLM_LKF_CONVERT
) && (namelen
> DLM_RESNAME_MAXLEN
))
2170 if (flags
& DLM_LKF_CANCEL
)
2173 if (flags
& DLM_LKF_QUECVT
&& !(flags
& DLM_LKF_CONVERT
))
2176 if (flags
& DLM_LKF_CONVDEADLK
&& !(flags
& DLM_LKF_CONVERT
))
2179 if (flags
& DLM_LKF_CONVDEADLK
&& flags
& DLM_LKF_NOQUEUE
)
2182 if (flags
& DLM_LKF_EXPEDITE
&& flags
& DLM_LKF_CONVERT
)
2185 if (flags
& DLM_LKF_EXPEDITE
&& flags
& DLM_LKF_QUECVT
)
2188 if (flags
& DLM_LKF_EXPEDITE
&& flags
& DLM_LKF_NOQUEUE
)
2191 if (flags
& DLM_LKF_EXPEDITE
&& mode
!= DLM_LOCK_NL
)
2197 if (flags
& DLM_LKF_VALBLK
&& !lksb
->sb_lvbptr
)
2200 if (flags
& DLM_LKF_CONVERT
&& !lksb
->sb_lkid
)
2203 /* these args will be copied to the lkb in validate_lock_args,
2204 it cannot be done now because when converting locks, fields in
2205 an active lkb cannot be modified before locking the rsb */
2207 args
->flags
= flags
;
2209 args
->astparam
= astparam
;
2210 args
->bastfn
= bast
;
2211 args
->timeout
= timeout_cs
;
2219 static int set_unlock_args(uint32_t flags
, void *astarg
, struct dlm_args
*args
)
2221 if (flags
& ~(DLM_LKF_CANCEL
| DLM_LKF_VALBLK
| DLM_LKF_IVVALBLK
|
2222 DLM_LKF_FORCEUNLOCK
))
2225 if (flags
& DLM_LKF_CANCEL
&& flags
& DLM_LKF_FORCEUNLOCK
)
2228 args
->flags
= flags
;
2229 args
->astparam
= astarg
;
2233 static int validate_lock_args(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
,
2234 struct dlm_args
*args
)
2238 if (args
->flags
& DLM_LKF_CONVERT
) {
2239 if (lkb
->lkb_flags
& DLM_IFL_MSTCPY
)
2242 if (args
->flags
& DLM_LKF_QUECVT
&&
2243 !__quecvt_compat_matrix
[lkb
->lkb_grmode
+1][args
->mode
+1])
2247 if (lkb
->lkb_status
!= DLM_LKSTS_GRANTED
)
2250 if (lkb
->lkb_wait_type
)
2253 if (is_overlap(lkb
))
2257 lkb
->lkb_exflags
= args
->flags
;
2258 lkb
->lkb_sbflags
= 0;
2259 lkb
->lkb_astfn
= args
->astfn
;
2260 lkb
->lkb_astparam
= args
->astparam
;
2261 lkb
->lkb_bastfn
= args
->bastfn
;
2262 lkb
->lkb_rqmode
= args
->mode
;
2263 lkb
->lkb_lksb
= args
->lksb
;
2264 lkb
->lkb_lvbptr
= args
->lksb
->sb_lvbptr
;
2265 lkb
->lkb_ownpid
= (int) current
->pid
;
2266 lkb
->lkb_timeout_cs
= args
->timeout
;
2270 log_debug(ls
, "validate_lock_args %d %x %x %x %d %d %s",
2271 rv
, lkb
->lkb_id
, lkb
->lkb_flags
, args
->flags
,
2272 lkb
->lkb_status
, lkb
->lkb_wait_type
,
2273 lkb
->lkb_resource
->res_name
);
2277 /* when dlm_unlock() sees -EBUSY with CANCEL/FORCEUNLOCK it returns 0
2280 /* note: it's valid for lkb_nodeid/res_nodeid to be -1 when we get here
2281 because there may be a lookup in progress and it's valid to do
2282 cancel/unlockf on it */
2284 static int validate_unlock_args(struct dlm_lkb
*lkb
, struct dlm_args
*args
)
2286 struct dlm_ls
*ls
= lkb
->lkb_resource
->res_ls
;
2289 if (lkb
->lkb_flags
& DLM_IFL_MSTCPY
) {
2290 log_error(ls
, "unlock on MSTCPY %x", lkb
->lkb_id
);
2295 /* an lkb may still exist even though the lock is EOL'ed due to a
2296 cancel, unlock or failed noqueue request; an app can't use these
2297 locks; return same error as if the lkid had not been found at all */
2299 if (lkb
->lkb_flags
& DLM_IFL_ENDOFLIFE
) {
2300 log_debug(ls
, "unlock on ENDOFLIFE %x", lkb
->lkb_id
);
2305 /* an lkb may be waiting for an rsb lookup to complete where the
2306 lookup was initiated by another lock */
2308 if (!list_empty(&lkb
->lkb_rsb_lookup
)) {
2309 if (args
->flags
& (DLM_LKF_CANCEL
| DLM_LKF_FORCEUNLOCK
)) {
2310 log_debug(ls
, "unlock on rsb_lookup %x", lkb
->lkb_id
);
2311 list_del_init(&lkb
->lkb_rsb_lookup
);
2312 queue_cast(lkb
->lkb_resource
, lkb
,
2313 args
->flags
& DLM_LKF_CANCEL
?
2314 -DLM_ECANCEL
: -DLM_EUNLOCK
);
2315 unhold_lkb(lkb
); /* undoes create_lkb() */
2317 /* caller changes -EBUSY to 0 for CANCEL and FORCEUNLOCK */
2322 /* cancel not allowed with another cancel/unlock in progress */
2324 if (args
->flags
& DLM_LKF_CANCEL
) {
2325 if (lkb
->lkb_exflags
& DLM_LKF_CANCEL
)
2328 if (is_overlap(lkb
))
2331 /* don't let scand try to do a cancel */
2334 if (lkb
->lkb_flags
& DLM_IFL_RESEND
) {
2335 lkb
->lkb_flags
|= DLM_IFL_OVERLAP_CANCEL
;
2340 /* there's nothing to cancel */
2341 if (lkb
->lkb_status
== DLM_LKSTS_GRANTED
&&
2342 !lkb
->lkb_wait_type
) {
2347 switch (lkb
->lkb_wait_type
) {
2348 case DLM_MSG_LOOKUP
:
2349 case DLM_MSG_REQUEST
:
2350 lkb
->lkb_flags
|= DLM_IFL_OVERLAP_CANCEL
;
2353 case DLM_MSG_UNLOCK
:
2354 case DLM_MSG_CANCEL
:
2357 /* add_to_waiters() will set OVERLAP_CANCEL */
2361 /* do we need to allow a force-unlock if there's a normal unlock
2362 already in progress? in what conditions could the normal unlock
2363 fail such that we'd want to send a force-unlock to be sure? */
2365 if (args
->flags
& DLM_LKF_FORCEUNLOCK
) {
2366 if (lkb
->lkb_exflags
& DLM_LKF_FORCEUNLOCK
)
2369 if (is_overlap_unlock(lkb
))
2372 /* don't let scand try to do a cancel */
2375 if (lkb
->lkb_flags
& DLM_IFL_RESEND
) {
2376 lkb
->lkb_flags
|= DLM_IFL_OVERLAP_UNLOCK
;
2381 switch (lkb
->lkb_wait_type
) {
2382 case DLM_MSG_LOOKUP
:
2383 case DLM_MSG_REQUEST
:
2384 lkb
->lkb_flags
|= DLM_IFL_OVERLAP_UNLOCK
;
2387 case DLM_MSG_UNLOCK
:
2390 /* add_to_waiters() will set OVERLAP_UNLOCK */
2394 /* normal unlock not allowed if there's any op in progress */
2396 if (lkb
->lkb_wait_type
|| lkb
->lkb_wait_count
)
2400 /* an overlapping op shouldn't blow away exflags from other op */
2401 lkb
->lkb_exflags
|= args
->flags
;
2402 lkb
->lkb_sbflags
= 0;
2403 lkb
->lkb_astparam
= args
->astparam
;
2407 log_debug(ls
, "validate_unlock_args %d %x %x %x %x %d %s", rv
,
2408 lkb
->lkb_id
, lkb
->lkb_flags
, lkb
->lkb_exflags
,
2409 args
->flags
, lkb
->lkb_wait_type
,
2410 lkb
->lkb_resource
->res_name
);
2415 * Four stage 4 varieties:
2416 * do_request(), do_convert(), do_unlock(), do_cancel()
2417 * These are called on the master node for the given lock and
2418 * from the central locking logic.
2421 static int do_request(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2425 if (can_be_granted(r
, lkb
, 1, NULL
)) {
2427 queue_cast(r
, lkb
, 0);
2431 if (can_be_queued(lkb
)) {
2432 error
= -EINPROGRESS
;
2433 add_lkb(r
, lkb
, DLM_LKSTS_WAITING
);
2439 queue_cast(r
, lkb
, -EAGAIN
);
2444 static void do_request_effects(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
2449 if (force_blocking_asts(lkb
))
2450 send_blocking_asts_all(r
, lkb
);
2453 send_blocking_asts(r
, lkb
);
2458 static int do_convert(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2463 /* changing an existing lock may allow others to be granted */
2465 if (can_be_granted(r
, lkb
, 1, &deadlk
)) {
2467 queue_cast(r
, lkb
, 0);
2471 /* can_be_granted() detected that this lock would block in a conversion
2472 deadlock, so we leave it on the granted queue and return EDEADLK in
2473 the ast for the convert. */
2476 /* it's left on the granted queue */
2477 revert_lock(r
, lkb
);
2478 queue_cast(r
, lkb
, -EDEADLK
);
2483 /* is_demoted() means the can_be_granted() above set the grmode
2484 to NL, and left us on the granted queue. This auto-demotion
2485 (due to CONVDEADLK) might mean other locks, and/or this lock, are
2486 now grantable. We have to try to grant other converting locks
2487 before we try again to grant this one. */
2489 if (is_demoted(lkb
)) {
2490 grant_pending_convert(r
, DLM_LOCK_IV
, NULL
);
2491 if (_can_be_granted(r
, lkb
, 1)) {
2493 queue_cast(r
, lkb
, 0);
2496 /* else fall through and move to convert queue */
2499 if (can_be_queued(lkb
)) {
2500 error
= -EINPROGRESS
;
2502 add_lkb(r
, lkb
, DLM_LKSTS_CONVERT
);
2508 queue_cast(r
, lkb
, -EAGAIN
);
2513 static void do_convert_effects(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
2518 grant_pending_locks(r
);
2519 /* grant_pending_locks also sends basts */
2522 if (force_blocking_asts(lkb
))
2523 send_blocking_asts_all(r
, lkb
);
2526 send_blocking_asts(r
, lkb
);
2531 static int do_unlock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2533 remove_lock(r
, lkb
);
2534 queue_cast(r
, lkb
, -DLM_EUNLOCK
);
2535 return -DLM_EUNLOCK
;
2538 static void do_unlock_effects(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
2541 grant_pending_locks(r
);
2544 /* returns: 0 did nothing, -DLM_ECANCEL canceled lock */
2546 static int do_cancel(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2550 error
= revert_lock(r
, lkb
);
2552 queue_cast(r
, lkb
, -DLM_ECANCEL
);
2553 return -DLM_ECANCEL
;
2558 static void do_cancel_effects(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
2562 grant_pending_locks(r
);
2566 * Four stage 3 varieties:
2567 * _request_lock(), _convert_lock(), _unlock_lock(), _cancel_lock()
2570 /* add a new lkb to a possibly new rsb, called by requesting process */
2572 static int _request_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2576 /* set_master: sets lkb nodeid from r */
2578 error
= set_master(r
, lkb
);
2587 /* receive_request() calls do_request() on remote node */
2588 error
= send_request(r
, lkb
);
2590 error
= do_request(r
, lkb
);
2591 /* for remote locks the request_reply is sent
2592 between do_request and do_request_effects */
2593 do_request_effects(r
, lkb
, error
);
2599 /* change some property of an existing lkb, e.g. mode */
2601 static int _convert_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2606 /* receive_convert() calls do_convert() on remote node */
2607 error
= send_convert(r
, lkb
);
2609 error
= do_convert(r
, lkb
);
2610 /* for remote locks the convert_reply is sent
2611 between do_convert and do_convert_effects */
2612 do_convert_effects(r
, lkb
, error
);
2618 /* remove an existing lkb from the granted queue */
2620 static int _unlock_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2625 /* receive_unlock() calls do_unlock() on remote node */
2626 error
= send_unlock(r
, lkb
);
2628 error
= do_unlock(r
, lkb
);
2629 /* for remote locks the unlock_reply is sent
2630 between do_unlock and do_unlock_effects */
2631 do_unlock_effects(r
, lkb
, error
);
2637 /* remove an existing lkb from the convert or wait queue */
2639 static int _cancel_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2644 /* receive_cancel() calls do_cancel() on remote node */
2645 error
= send_cancel(r
, lkb
);
2647 error
= do_cancel(r
, lkb
);
2648 /* for remote locks the cancel_reply is sent
2649 between do_cancel and do_cancel_effects */
2650 do_cancel_effects(r
, lkb
, error
);
2657 * Four stage 2 varieties:
2658 * request_lock(), convert_lock(), unlock_lock(), cancel_lock()
2661 static int request_lock(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
, char *name
,
2662 int len
, struct dlm_args
*args
)
2667 error
= validate_lock_args(ls
, lkb
, args
);
2671 error
= find_rsb(ls
, name
, len
, R_CREATE
, &r
);
2678 lkb
->lkb_lksb
->sb_lkid
= lkb
->lkb_id
;
2680 error
= _request_lock(r
, lkb
);
2689 static int convert_lock(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
,
2690 struct dlm_args
*args
)
2695 r
= lkb
->lkb_resource
;
2700 error
= validate_lock_args(ls
, lkb
, args
);
2704 error
= _convert_lock(r
, lkb
);
2711 static int unlock_lock(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
,
2712 struct dlm_args
*args
)
2717 r
= lkb
->lkb_resource
;
2722 error
= validate_unlock_args(lkb
, args
);
2726 error
= _unlock_lock(r
, lkb
);
2733 static int cancel_lock(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
,
2734 struct dlm_args
*args
)
2739 r
= lkb
->lkb_resource
;
2744 error
= validate_unlock_args(lkb
, args
);
2748 error
= _cancel_lock(r
, lkb
);
2756 * Two stage 1 varieties: dlm_lock() and dlm_unlock()
2759 int dlm_lock(dlm_lockspace_t
*lockspace
,
2761 struct dlm_lksb
*lksb
,
2764 unsigned int namelen
,
2765 uint32_t parent_lkid
,
2766 void (*ast
) (void *astarg
),
2768 void (*bast
) (void *astarg
, int mode
))
2771 struct dlm_lkb
*lkb
;
2772 struct dlm_args args
;
2773 int error
, convert
= flags
& DLM_LKF_CONVERT
;
2775 ls
= dlm_find_lockspace_local(lockspace
);
2779 dlm_lock_recovery(ls
);
2782 error
= find_lkb(ls
, lksb
->sb_lkid
, &lkb
);
2784 error
= create_lkb(ls
, &lkb
);
2789 error
= set_lock_args(mode
, lksb
, flags
, namelen
, 0, ast
,
2790 astarg
, bast
, &args
);
2795 error
= convert_lock(ls
, lkb
, &args
);
2797 error
= request_lock(ls
, lkb
, name
, namelen
, &args
);
2799 if (error
== -EINPROGRESS
)
2802 if (convert
|| error
)
2804 if (error
== -EAGAIN
|| error
== -EDEADLK
)
2807 dlm_unlock_recovery(ls
);
2808 dlm_put_lockspace(ls
);
2812 int dlm_unlock(dlm_lockspace_t
*lockspace
,
2815 struct dlm_lksb
*lksb
,
2819 struct dlm_lkb
*lkb
;
2820 struct dlm_args args
;
2823 ls
= dlm_find_lockspace_local(lockspace
);
2827 dlm_lock_recovery(ls
);
2829 error
= find_lkb(ls
, lkid
, &lkb
);
2833 error
= set_unlock_args(flags
, astarg
, &args
);
2837 if (flags
& DLM_LKF_CANCEL
)
2838 error
= cancel_lock(ls
, lkb
, &args
);
2840 error
= unlock_lock(ls
, lkb
, &args
);
2842 if (error
== -DLM_EUNLOCK
|| error
== -DLM_ECANCEL
)
2844 if (error
== -EBUSY
&& (flags
& (DLM_LKF_CANCEL
| DLM_LKF_FORCEUNLOCK
)))
2849 dlm_unlock_recovery(ls
);
2850 dlm_put_lockspace(ls
);
2855 * send/receive routines for remote operations and replies
2859 * send_request receive_request
2860 * send_convert receive_convert
2861 * send_unlock receive_unlock
2862 * send_cancel receive_cancel
2863 * send_grant receive_grant
2864 * send_bast receive_bast
2865 * send_lookup receive_lookup
2866 * send_remove receive_remove
2869 * receive_request_reply send_request_reply
2870 * receive_convert_reply send_convert_reply
2871 * receive_unlock_reply send_unlock_reply
2872 * receive_cancel_reply send_cancel_reply
2873 * receive_lookup_reply send_lookup_reply
2876 static int _create_message(struct dlm_ls
*ls
, int mb_len
,
2877 int to_nodeid
, int mstype
,
2878 struct dlm_message
**ms_ret
,
2879 struct dlm_mhandle
**mh_ret
)
2881 struct dlm_message
*ms
;
2882 struct dlm_mhandle
*mh
;
2885 /* get_buffer gives us a message handle (mh) that we need to
2886 pass into lowcomms_commit and a message buffer (mb) that we
2887 write our data into */
2889 mh
= dlm_lowcomms_get_buffer(to_nodeid
, mb_len
, GFP_NOFS
, &mb
);
2893 memset(mb
, 0, mb_len
);
2895 ms
= (struct dlm_message
*) mb
;
2897 ms
->m_header
.h_version
= (DLM_HEADER_MAJOR
| DLM_HEADER_MINOR
);
2898 ms
->m_header
.h_lockspace
= ls
->ls_global_id
;
2899 ms
->m_header
.h_nodeid
= dlm_our_nodeid();
2900 ms
->m_header
.h_length
= mb_len
;
2901 ms
->m_header
.h_cmd
= DLM_MSG
;
2903 ms
->m_type
= mstype
;
2910 static int create_message(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
2911 int to_nodeid
, int mstype
,
2912 struct dlm_message
**ms_ret
,
2913 struct dlm_mhandle
**mh_ret
)
2915 int mb_len
= sizeof(struct dlm_message
);
2918 case DLM_MSG_REQUEST
:
2919 case DLM_MSG_LOOKUP
:
2920 case DLM_MSG_REMOVE
:
2921 mb_len
+= r
->res_length
;
2923 case DLM_MSG_CONVERT
:
2924 case DLM_MSG_UNLOCK
:
2925 case DLM_MSG_REQUEST_REPLY
:
2926 case DLM_MSG_CONVERT_REPLY
:
2928 if (lkb
&& lkb
->lkb_lvbptr
)
2929 mb_len
+= r
->res_ls
->ls_lvblen
;
2933 return _create_message(r
->res_ls
, mb_len
, to_nodeid
, mstype
,
2937 /* further lowcomms enhancements or alternate implementations may make
2938 the return value from this function useful at some point */
2940 static int send_message(struct dlm_mhandle
*mh
, struct dlm_message
*ms
)
2942 dlm_message_out(ms
);
2943 dlm_lowcomms_commit_buffer(mh
);
2947 static void send_args(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
2948 struct dlm_message
*ms
)
2950 ms
->m_nodeid
= lkb
->lkb_nodeid
;
2951 ms
->m_pid
= lkb
->lkb_ownpid
;
2952 ms
->m_lkid
= lkb
->lkb_id
;
2953 ms
->m_remid
= lkb
->lkb_remid
;
2954 ms
->m_exflags
= lkb
->lkb_exflags
;
2955 ms
->m_sbflags
= lkb
->lkb_sbflags
;
2956 ms
->m_flags
= lkb
->lkb_flags
;
2957 ms
->m_lvbseq
= lkb
->lkb_lvbseq
;
2958 ms
->m_status
= lkb
->lkb_status
;
2959 ms
->m_grmode
= lkb
->lkb_grmode
;
2960 ms
->m_rqmode
= lkb
->lkb_rqmode
;
2961 ms
->m_hash
= r
->res_hash
;
2963 /* m_result and m_bastmode are set from function args,
2964 not from lkb fields */
2966 if (lkb
->lkb_bastfn
)
2967 ms
->m_asts
|= DLM_CB_BAST
;
2969 ms
->m_asts
|= DLM_CB_CAST
;
2971 /* compare with switch in create_message; send_remove() doesn't
2974 switch (ms
->m_type
) {
2975 case DLM_MSG_REQUEST
:
2976 case DLM_MSG_LOOKUP
:
2977 memcpy(ms
->m_extra
, r
->res_name
, r
->res_length
);
2979 case DLM_MSG_CONVERT
:
2980 case DLM_MSG_UNLOCK
:
2981 case DLM_MSG_REQUEST_REPLY
:
2982 case DLM_MSG_CONVERT_REPLY
:
2984 if (!lkb
->lkb_lvbptr
)
2986 memcpy(ms
->m_extra
, lkb
->lkb_lvbptr
, r
->res_ls
->ls_lvblen
);
2991 static int send_common(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int mstype
)
2993 struct dlm_message
*ms
;
2994 struct dlm_mhandle
*mh
;
2995 int to_nodeid
, error
;
2997 to_nodeid
= r
->res_nodeid
;
2999 error
= add_to_waiters(lkb
, mstype
, to_nodeid
);
3003 error
= create_message(r
, lkb
, to_nodeid
, mstype
, &ms
, &mh
);
3007 send_args(r
, lkb
, ms
);
3009 error
= send_message(mh
, ms
);
3015 remove_from_waiters(lkb
, msg_reply_type(mstype
));
3019 static int send_request(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
3021 return send_common(r
, lkb
, DLM_MSG_REQUEST
);
3024 static int send_convert(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
3028 error
= send_common(r
, lkb
, DLM_MSG_CONVERT
);
3030 /* down conversions go without a reply from the master */
3031 if (!error
&& down_conversion(lkb
)) {
3032 remove_from_waiters(lkb
, DLM_MSG_CONVERT_REPLY
);
3033 r
->res_ls
->ls_stub_ms
.m_flags
= DLM_IFL_STUB_MS
;
3034 r
->res_ls
->ls_stub_ms
.m_type
= DLM_MSG_CONVERT_REPLY
;
3035 r
->res_ls
->ls_stub_ms
.m_result
= 0;
3036 __receive_convert_reply(r
, lkb
, &r
->res_ls
->ls_stub_ms
);
3042 /* FIXME: if this lkb is the only lock we hold on the rsb, then set
3043 MASTER_UNCERTAIN to force the next request on the rsb to confirm
3044 that the master is still correct. */
3046 static int send_unlock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
3048 return send_common(r
, lkb
, DLM_MSG_UNLOCK
);
3051 static int send_cancel(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
3053 return send_common(r
, lkb
, DLM_MSG_CANCEL
);
3056 static int send_grant(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
3058 struct dlm_message
*ms
;
3059 struct dlm_mhandle
*mh
;
3060 int to_nodeid
, error
;
3062 to_nodeid
= lkb
->lkb_nodeid
;
3064 error
= create_message(r
, lkb
, to_nodeid
, DLM_MSG_GRANT
, &ms
, &mh
);
3068 send_args(r
, lkb
, ms
);
3072 error
= send_message(mh
, ms
);
3077 static int send_bast(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int mode
)
3079 struct dlm_message
*ms
;
3080 struct dlm_mhandle
*mh
;
3081 int to_nodeid
, error
;
3083 to_nodeid
= lkb
->lkb_nodeid
;
3085 error
= create_message(r
, NULL
, to_nodeid
, DLM_MSG_BAST
, &ms
, &mh
);
3089 send_args(r
, lkb
, ms
);
3091 ms
->m_bastmode
= mode
;
3093 error
= send_message(mh
, ms
);
3098 static int send_lookup(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
3100 struct dlm_message
*ms
;
3101 struct dlm_mhandle
*mh
;
3102 int to_nodeid
, error
;
3104 to_nodeid
= dlm_dir_nodeid(r
);
3106 error
= add_to_waiters(lkb
, DLM_MSG_LOOKUP
, to_nodeid
);
3110 error
= create_message(r
, NULL
, to_nodeid
, DLM_MSG_LOOKUP
, &ms
, &mh
);
3114 send_args(r
, lkb
, ms
);
3116 error
= send_message(mh
, ms
);
3122 remove_from_waiters(lkb
, DLM_MSG_LOOKUP_REPLY
);
3126 static int send_remove(struct dlm_rsb
*r
)
3128 struct dlm_message
*ms
;
3129 struct dlm_mhandle
*mh
;
3130 int to_nodeid
, error
;
3132 to_nodeid
= dlm_dir_nodeid(r
);
3134 error
= create_message(r
, NULL
, to_nodeid
, DLM_MSG_REMOVE
, &ms
, &mh
);
3138 memcpy(ms
->m_extra
, r
->res_name
, r
->res_length
);
3139 ms
->m_hash
= r
->res_hash
;
3141 error
= send_message(mh
, ms
);
3146 static int send_common_reply(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
3149 struct dlm_message
*ms
;
3150 struct dlm_mhandle
*mh
;
3151 int to_nodeid
, error
;
3153 to_nodeid
= lkb
->lkb_nodeid
;
3155 error
= create_message(r
, lkb
, to_nodeid
, mstype
, &ms
, &mh
);
3159 send_args(r
, lkb
, ms
);
3163 error
= send_message(mh
, ms
);
3168 static int send_request_reply(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int rv
)
3170 return send_common_reply(r
, lkb
, DLM_MSG_REQUEST_REPLY
, rv
);
3173 static int send_convert_reply(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int rv
)
3175 return send_common_reply(r
, lkb
, DLM_MSG_CONVERT_REPLY
, rv
);
3178 static int send_unlock_reply(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int rv
)
3180 return send_common_reply(r
, lkb
, DLM_MSG_UNLOCK_REPLY
, rv
);
3183 static int send_cancel_reply(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int rv
)
3185 return send_common_reply(r
, lkb
, DLM_MSG_CANCEL_REPLY
, rv
);
3188 static int send_lookup_reply(struct dlm_ls
*ls
, struct dlm_message
*ms_in
,
3189 int ret_nodeid
, int rv
)
3191 struct dlm_rsb
*r
= &ls
->ls_stub_rsb
;
3192 struct dlm_message
*ms
;
3193 struct dlm_mhandle
*mh
;
3194 int error
, nodeid
= ms_in
->m_header
.h_nodeid
;
3196 error
= create_message(r
, NULL
, nodeid
, DLM_MSG_LOOKUP_REPLY
, &ms
, &mh
);
3200 ms
->m_lkid
= ms_in
->m_lkid
;
3202 ms
->m_nodeid
= ret_nodeid
;
3204 error
= send_message(mh
, ms
);
3209 /* which args we save from a received message depends heavily on the type
3210 of message, unlike the send side where we can safely send everything about
3211 the lkb for any type of message */
3213 static void receive_flags(struct dlm_lkb
*lkb
, struct dlm_message
*ms
)
3215 lkb
->lkb_exflags
= ms
->m_exflags
;
3216 lkb
->lkb_sbflags
= ms
->m_sbflags
;
3217 lkb
->lkb_flags
= (lkb
->lkb_flags
& 0xFFFF0000) |
3218 (ms
->m_flags
& 0x0000FFFF);
3221 static void receive_flags_reply(struct dlm_lkb
*lkb
, struct dlm_message
*ms
)
3223 if (ms
->m_flags
== DLM_IFL_STUB_MS
)
3226 lkb
->lkb_sbflags
= ms
->m_sbflags
;
3227 lkb
->lkb_flags
= (lkb
->lkb_flags
& 0xFFFF0000) |
3228 (ms
->m_flags
& 0x0000FFFF);
3231 static int receive_extralen(struct dlm_message
*ms
)
3233 return (ms
->m_header
.h_length
- sizeof(struct dlm_message
));
3236 static int receive_lvb(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
,
3237 struct dlm_message
*ms
)
3241 if (lkb
->lkb_exflags
& DLM_LKF_VALBLK
) {
3242 if (!lkb
->lkb_lvbptr
)
3243 lkb
->lkb_lvbptr
= dlm_allocate_lvb(ls
);
3244 if (!lkb
->lkb_lvbptr
)
3246 len
= receive_extralen(ms
);
3247 if (len
> DLM_RESNAME_MAXLEN
)
3248 len
= DLM_RESNAME_MAXLEN
;
3249 memcpy(lkb
->lkb_lvbptr
, ms
->m_extra
, len
);
3254 static void fake_bastfn(void *astparam
, int mode
)
3256 log_print("fake_bastfn should not be called");
3259 static void fake_astfn(void *astparam
)
3261 log_print("fake_astfn should not be called");
3264 static int receive_request_args(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
,
3265 struct dlm_message
*ms
)
3267 lkb
->lkb_nodeid
= ms
->m_header
.h_nodeid
;
3268 lkb
->lkb_ownpid
= ms
->m_pid
;
3269 lkb
->lkb_remid
= ms
->m_lkid
;
3270 lkb
->lkb_grmode
= DLM_LOCK_IV
;
3271 lkb
->lkb_rqmode
= ms
->m_rqmode
;
3273 lkb
->lkb_bastfn
= (ms
->m_asts
& DLM_CB_BAST
) ? &fake_bastfn
: NULL
;
3274 lkb
->lkb_astfn
= (ms
->m_asts
& DLM_CB_CAST
) ? &fake_astfn
: NULL
;
3276 if (lkb
->lkb_exflags
& DLM_LKF_VALBLK
) {
3277 /* lkb was just created so there won't be an lvb yet */
3278 lkb
->lkb_lvbptr
= dlm_allocate_lvb(ls
);
3279 if (!lkb
->lkb_lvbptr
)
3286 static int receive_convert_args(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
,
3287 struct dlm_message
*ms
)
3289 if (lkb
->lkb_status
!= DLM_LKSTS_GRANTED
)
3292 if (receive_lvb(ls
, lkb
, ms
))
3295 lkb
->lkb_rqmode
= ms
->m_rqmode
;
3296 lkb
->lkb_lvbseq
= ms
->m_lvbseq
;
3301 static int receive_unlock_args(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
,
3302 struct dlm_message
*ms
)
3304 if (receive_lvb(ls
, lkb
, ms
))
3309 /* We fill in the stub-lkb fields with the info that send_xxxx_reply()
3310 uses to send a reply and that the remote end uses to process the reply. */
3312 static void setup_stub_lkb(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3314 struct dlm_lkb
*lkb
= &ls
->ls_stub_lkb
;
3315 lkb
->lkb_nodeid
= ms
->m_header
.h_nodeid
;
3316 lkb
->lkb_remid
= ms
->m_lkid
;
3319 /* This is called after the rsb is locked so that we can safely inspect
3320 fields in the lkb. */
3322 static int validate_message(struct dlm_lkb
*lkb
, struct dlm_message
*ms
)
3324 int from
= ms
->m_header
.h_nodeid
;
3327 switch (ms
->m_type
) {
3328 case DLM_MSG_CONVERT
:
3329 case DLM_MSG_UNLOCK
:
3330 case DLM_MSG_CANCEL
:
3331 if (!is_master_copy(lkb
) || lkb
->lkb_nodeid
!= from
)
3335 case DLM_MSG_CONVERT_REPLY
:
3336 case DLM_MSG_UNLOCK_REPLY
:
3337 case DLM_MSG_CANCEL_REPLY
:
3340 if (!is_process_copy(lkb
) || lkb
->lkb_nodeid
!= from
)
3344 case DLM_MSG_REQUEST_REPLY
:
3345 if (!is_process_copy(lkb
))
3347 else if (lkb
->lkb_nodeid
!= -1 && lkb
->lkb_nodeid
!= from
)
3356 log_error(lkb
->lkb_resource
->res_ls
,
3357 "ignore invalid message %d from %d %x %x %x %d",
3358 ms
->m_type
, from
, lkb
->lkb_id
, lkb
->lkb_remid
,
3359 lkb
->lkb_flags
, lkb
->lkb_nodeid
);
3363 static void receive_request(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3365 struct dlm_lkb
*lkb
;
3369 error
= create_lkb(ls
, &lkb
);
3373 receive_flags(lkb
, ms
);
3374 lkb
->lkb_flags
|= DLM_IFL_MSTCPY
;
3375 error
= receive_request_args(ls
, lkb
, ms
);
3381 namelen
= receive_extralen(ms
);
3383 error
= find_rsb(ls
, ms
->m_extra
, namelen
, R_MASTER
, &r
);
3392 error
= do_request(r
, lkb
);
3393 send_request_reply(r
, lkb
, error
);
3394 do_request_effects(r
, lkb
, error
);
3399 if (error
== -EINPROGRESS
)
3406 setup_stub_lkb(ls
, ms
);
3407 send_request_reply(&ls
->ls_stub_rsb
, &ls
->ls_stub_lkb
, error
);
3410 static void receive_convert(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3412 struct dlm_lkb
*lkb
;
3414 int error
, reply
= 1;
3416 error
= find_lkb(ls
, ms
->m_remid
, &lkb
);
3420 r
= lkb
->lkb_resource
;
3425 error
= validate_message(lkb
, ms
);
3429 receive_flags(lkb
, ms
);
3431 error
= receive_convert_args(ls
, lkb
, ms
);
3433 send_convert_reply(r
, lkb
, error
);
3437 reply
= !down_conversion(lkb
);
3439 error
= do_convert(r
, lkb
);
3441 send_convert_reply(r
, lkb
, error
);
3442 do_convert_effects(r
, lkb
, error
);
3450 setup_stub_lkb(ls
, ms
);
3451 send_convert_reply(&ls
->ls_stub_rsb
, &ls
->ls_stub_lkb
, error
);
3454 static void receive_unlock(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3456 struct dlm_lkb
*lkb
;
3460 error
= find_lkb(ls
, ms
->m_remid
, &lkb
);
3464 r
= lkb
->lkb_resource
;
3469 error
= validate_message(lkb
, ms
);
3473 receive_flags(lkb
, ms
);
3475 error
= receive_unlock_args(ls
, lkb
, ms
);
3477 send_unlock_reply(r
, lkb
, error
);
3481 error
= do_unlock(r
, lkb
);
3482 send_unlock_reply(r
, lkb
, error
);
3483 do_unlock_effects(r
, lkb
, error
);
3491 setup_stub_lkb(ls
, ms
);
3492 send_unlock_reply(&ls
->ls_stub_rsb
, &ls
->ls_stub_lkb
, error
);
3495 static void receive_cancel(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3497 struct dlm_lkb
*lkb
;
3501 error
= find_lkb(ls
, ms
->m_remid
, &lkb
);
3505 receive_flags(lkb
, ms
);
3507 r
= lkb
->lkb_resource
;
3512 error
= validate_message(lkb
, ms
);
3516 error
= do_cancel(r
, lkb
);
3517 send_cancel_reply(r
, lkb
, error
);
3518 do_cancel_effects(r
, lkb
, error
);
3526 setup_stub_lkb(ls
, ms
);
3527 send_cancel_reply(&ls
->ls_stub_rsb
, &ls
->ls_stub_lkb
, error
);
3530 static void receive_grant(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3532 struct dlm_lkb
*lkb
;
3536 error
= find_lkb(ls
, ms
->m_remid
, &lkb
);
3538 log_debug(ls
, "receive_grant from %d no lkb %x",
3539 ms
->m_header
.h_nodeid
, ms
->m_remid
);
3543 r
= lkb
->lkb_resource
;
3548 error
= validate_message(lkb
, ms
);
3552 receive_flags_reply(lkb
, ms
);
3553 if (is_altmode(lkb
))
3554 munge_altmode(lkb
, ms
);
3555 grant_lock_pc(r
, lkb
, ms
);
3556 queue_cast(r
, lkb
, 0);
3563 static void receive_bast(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3565 struct dlm_lkb
*lkb
;
3569 error
= find_lkb(ls
, ms
->m_remid
, &lkb
);
3571 log_debug(ls
, "receive_bast from %d no lkb %x",
3572 ms
->m_header
.h_nodeid
, ms
->m_remid
);
3576 r
= lkb
->lkb_resource
;
3581 error
= validate_message(lkb
, ms
);
3585 queue_bast(r
, lkb
, ms
->m_bastmode
);
3592 static void receive_lookup(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3594 int len
, error
, ret_nodeid
, dir_nodeid
, from_nodeid
, our_nodeid
;
3596 from_nodeid
= ms
->m_header
.h_nodeid
;
3597 our_nodeid
= dlm_our_nodeid();
3599 len
= receive_extralen(ms
);
3601 dir_nodeid
= dlm_hash2nodeid(ls
, ms
->m_hash
);
3602 if (dir_nodeid
!= our_nodeid
) {
3603 log_error(ls
, "lookup dir_nodeid %d from %d",
3604 dir_nodeid
, from_nodeid
);
3610 error
= dlm_dir_lookup(ls
, from_nodeid
, ms
->m_extra
, len
, &ret_nodeid
);
3612 /* Optimization: we're master so treat lookup as a request */
3613 if (!error
&& ret_nodeid
== our_nodeid
) {
3614 receive_request(ls
, ms
);
3618 send_lookup_reply(ls
, ms
, ret_nodeid
, error
);
3621 static void receive_remove(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3623 int len
, dir_nodeid
, from_nodeid
;
3625 from_nodeid
= ms
->m_header
.h_nodeid
;
3627 len
= receive_extralen(ms
);
3629 dir_nodeid
= dlm_hash2nodeid(ls
, ms
->m_hash
);
3630 if (dir_nodeid
!= dlm_our_nodeid()) {
3631 log_error(ls
, "remove dir entry dir_nodeid %d from %d",
3632 dir_nodeid
, from_nodeid
);
3636 dlm_dir_remove_entry(ls
, from_nodeid
, ms
->m_extra
, len
);
3639 static void receive_purge(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3641 do_purge(ls
, ms
->m_nodeid
, ms
->m_pid
);
3644 static void receive_request_reply(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3646 struct dlm_lkb
*lkb
;
3648 int error
, mstype
, result
;
3650 error
= find_lkb(ls
, ms
->m_remid
, &lkb
);
3652 log_debug(ls
, "receive_request_reply from %d no lkb %x",
3653 ms
->m_header
.h_nodeid
, ms
->m_remid
);
3657 r
= lkb
->lkb_resource
;
3661 error
= validate_message(lkb
, ms
);
3665 mstype
= lkb
->lkb_wait_type
;
3666 error
= remove_from_waiters(lkb
, DLM_MSG_REQUEST_REPLY
);
3670 /* Optimization: the dir node was also the master, so it took our
3671 lookup as a request and sent request reply instead of lookup reply */
3672 if (mstype
== DLM_MSG_LOOKUP
) {
3673 r
->res_nodeid
= ms
->m_header
.h_nodeid
;
3674 lkb
->lkb_nodeid
= r
->res_nodeid
;
3677 /* this is the value returned from do_request() on the master */
3678 result
= ms
->m_result
;
3682 /* request would block (be queued) on remote master */
3683 queue_cast(r
, lkb
, -EAGAIN
);
3684 confirm_master(r
, -EAGAIN
);
3685 unhold_lkb(lkb
); /* undoes create_lkb() */
3690 /* request was queued or granted on remote master */
3691 receive_flags_reply(lkb
, ms
);
3692 lkb
->lkb_remid
= ms
->m_lkid
;
3693 if (is_altmode(lkb
))
3694 munge_altmode(lkb
, ms
);
3696 add_lkb(r
, lkb
, DLM_LKSTS_WAITING
);
3699 grant_lock_pc(r
, lkb
, ms
);
3700 queue_cast(r
, lkb
, 0);
3702 confirm_master(r
, result
);
3707 /* find_rsb failed to find rsb or rsb wasn't master */
3708 log_debug(ls
, "receive_request_reply %x %x master diff %d %d",
3709 lkb
->lkb_id
, lkb
->lkb_flags
, r
->res_nodeid
, result
);
3711 lkb
->lkb_nodeid
= -1;
3713 if (is_overlap(lkb
)) {
3714 /* we'll ignore error in cancel/unlock reply */
3715 queue_cast_overlap(r
, lkb
);
3716 confirm_master(r
, result
);
3717 unhold_lkb(lkb
); /* undoes create_lkb() */
3719 _request_lock(r
, lkb
);
3723 log_error(ls
, "receive_request_reply %x error %d",
3724 lkb
->lkb_id
, result
);
3727 if (is_overlap_unlock(lkb
) && (result
== 0 || result
== -EINPROGRESS
)) {
3728 log_debug(ls
, "receive_request_reply %x result %d unlock",
3729 lkb
->lkb_id
, result
);
3730 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_UNLOCK
;
3731 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_CANCEL
;
3732 send_unlock(r
, lkb
);
3733 } else if (is_overlap_cancel(lkb
) && (result
== -EINPROGRESS
)) {
3734 log_debug(ls
, "receive_request_reply %x cancel", lkb
->lkb_id
);
3735 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_UNLOCK
;
3736 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_CANCEL
;
3737 send_cancel(r
, lkb
);
3739 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_CANCEL
;
3740 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_UNLOCK
;
3748 static void __receive_convert_reply(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
3749 struct dlm_message
*ms
)
3751 /* this is the value returned from do_convert() on the master */
3752 switch (ms
->m_result
) {
3754 /* convert would block (be queued) on remote master */
3755 queue_cast(r
, lkb
, -EAGAIN
);
3759 receive_flags_reply(lkb
, ms
);
3760 revert_lock_pc(r
, lkb
);
3761 queue_cast(r
, lkb
, -EDEADLK
);
3765 /* convert was queued on remote master */
3766 receive_flags_reply(lkb
, ms
);
3767 if (is_demoted(lkb
))
3770 add_lkb(r
, lkb
, DLM_LKSTS_CONVERT
);
3775 /* convert was granted on remote master */
3776 receive_flags_reply(lkb
, ms
);
3777 if (is_demoted(lkb
))
3779 grant_lock_pc(r
, lkb
, ms
);
3780 queue_cast(r
, lkb
, 0);
3784 log_error(r
->res_ls
, "receive_convert_reply %x error %d",
3785 lkb
->lkb_id
, ms
->m_result
);
3789 static void _receive_convert_reply(struct dlm_lkb
*lkb
, struct dlm_message
*ms
)
3791 struct dlm_rsb
*r
= lkb
->lkb_resource
;
3797 error
= validate_message(lkb
, ms
);
3801 /* stub reply can happen with waiters_mutex held */
3802 error
= remove_from_waiters_ms(lkb
, ms
);
3806 __receive_convert_reply(r
, lkb
, ms
);
3812 static void receive_convert_reply(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3814 struct dlm_lkb
*lkb
;
3817 error
= find_lkb(ls
, ms
->m_remid
, &lkb
);
3819 log_debug(ls
, "receive_convert_reply from %d no lkb %x",
3820 ms
->m_header
.h_nodeid
, ms
->m_remid
);
3824 _receive_convert_reply(lkb
, ms
);
3828 static void _receive_unlock_reply(struct dlm_lkb
*lkb
, struct dlm_message
*ms
)
3830 struct dlm_rsb
*r
= lkb
->lkb_resource
;
3836 error
= validate_message(lkb
, ms
);
3840 /* stub reply can happen with waiters_mutex held */
3841 error
= remove_from_waiters_ms(lkb
, ms
);
3845 /* this is the value returned from do_unlock() on the master */
3847 switch (ms
->m_result
) {
3849 receive_flags_reply(lkb
, ms
);
3850 remove_lock_pc(r
, lkb
);
3851 queue_cast(r
, lkb
, -DLM_EUNLOCK
);
3856 log_error(r
->res_ls
, "receive_unlock_reply %x error %d",
3857 lkb
->lkb_id
, ms
->m_result
);
3864 static void receive_unlock_reply(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3866 struct dlm_lkb
*lkb
;
3869 error
= find_lkb(ls
, ms
->m_remid
, &lkb
);
3871 log_debug(ls
, "receive_unlock_reply from %d no lkb %x",
3872 ms
->m_header
.h_nodeid
, ms
->m_remid
);
3876 _receive_unlock_reply(lkb
, ms
);
3880 static void _receive_cancel_reply(struct dlm_lkb
*lkb
, struct dlm_message
*ms
)
3882 struct dlm_rsb
*r
= lkb
->lkb_resource
;
3888 error
= validate_message(lkb
, ms
);
3892 /* stub reply can happen with waiters_mutex held */
3893 error
= remove_from_waiters_ms(lkb
, ms
);
3897 /* this is the value returned from do_cancel() on the master */
3899 switch (ms
->m_result
) {
3901 receive_flags_reply(lkb
, ms
);
3902 revert_lock_pc(r
, lkb
);
3903 queue_cast(r
, lkb
, -DLM_ECANCEL
);
3908 log_error(r
->res_ls
, "receive_cancel_reply %x error %d",
3909 lkb
->lkb_id
, ms
->m_result
);
3916 static void receive_cancel_reply(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3918 struct dlm_lkb
*lkb
;
3921 error
= find_lkb(ls
, ms
->m_remid
, &lkb
);
3923 log_debug(ls
, "receive_cancel_reply from %d no lkb %x",
3924 ms
->m_header
.h_nodeid
, ms
->m_remid
);
3928 _receive_cancel_reply(lkb
, ms
);
3932 static void receive_lookup_reply(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3934 struct dlm_lkb
*lkb
;
3936 int error
, ret_nodeid
;
3938 error
= find_lkb(ls
, ms
->m_lkid
, &lkb
);
3940 log_error(ls
, "receive_lookup_reply no lkb");
3944 /* ms->m_result is the value returned by dlm_dir_lookup on dir node
3945 FIXME: will a non-zero error ever be returned? */
3947 r
= lkb
->lkb_resource
;
3951 error
= remove_from_waiters(lkb
, DLM_MSG_LOOKUP_REPLY
);
3955 ret_nodeid
= ms
->m_nodeid
;
3956 if (ret_nodeid
== dlm_our_nodeid()) {
3959 r
->res_first_lkid
= 0;
3961 /* set_master() will copy res_nodeid to lkb_nodeid */
3962 r
->res_nodeid
= ret_nodeid
;
3965 if (is_overlap(lkb
)) {
3966 log_debug(ls
, "receive_lookup_reply %x unlock %x",
3967 lkb
->lkb_id
, lkb
->lkb_flags
);
3968 queue_cast_overlap(r
, lkb
);
3969 unhold_lkb(lkb
); /* undoes create_lkb() */
3973 _request_lock(r
, lkb
);
3977 process_lookup_list(r
);
3984 static void _receive_message(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3986 if (!dlm_is_member(ls
, ms
->m_header
.h_nodeid
)) {
3987 log_debug(ls
, "ignore non-member message %d from %d %x %x %d",
3988 ms
->m_type
, ms
->m_header
.h_nodeid
, ms
->m_lkid
,
3989 ms
->m_remid
, ms
->m_result
);
3993 switch (ms
->m_type
) {
3995 /* messages sent to a master node */
3997 case DLM_MSG_REQUEST
:
3998 receive_request(ls
, ms
);
4001 case DLM_MSG_CONVERT
:
4002 receive_convert(ls
, ms
);
4005 case DLM_MSG_UNLOCK
:
4006 receive_unlock(ls
, ms
);
4009 case DLM_MSG_CANCEL
:
4010 receive_cancel(ls
, ms
);
4013 /* messages sent from a master node (replies to above) */
4015 case DLM_MSG_REQUEST_REPLY
:
4016 receive_request_reply(ls
, ms
);
4019 case DLM_MSG_CONVERT_REPLY
:
4020 receive_convert_reply(ls
, ms
);
4023 case DLM_MSG_UNLOCK_REPLY
:
4024 receive_unlock_reply(ls
, ms
);
4027 case DLM_MSG_CANCEL_REPLY
:
4028 receive_cancel_reply(ls
, ms
);
4031 /* messages sent from a master node (only two types of async msg) */
4034 receive_grant(ls
, ms
);
4038 receive_bast(ls
, ms
);
4041 /* messages sent to a dir node */
4043 case DLM_MSG_LOOKUP
:
4044 receive_lookup(ls
, ms
);
4047 case DLM_MSG_REMOVE
:
4048 receive_remove(ls
, ms
);
4051 /* messages sent from a dir node (remove has no reply) */
4053 case DLM_MSG_LOOKUP_REPLY
:
4054 receive_lookup_reply(ls
, ms
);
4057 /* other messages */
4060 receive_purge(ls
, ms
);
4064 log_error(ls
, "unknown message type %d", ms
->m_type
);
4068 /* If the lockspace is in recovery mode (locking stopped), then normal
4069 messages are saved on the requestqueue for processing after recovery is
4070 done. When not in recovery mode, we wait for dlm_recoverd to drain saved
4071 messages off the requestqueue before we process new ones. This occurs right
4072 after recovery completes when we transition from saving all messages on
4073 requestqueue, to processing all the saved messages, to processing new
4074 messages as they arrive. */
4076 static void dlm_receive_message(struct dlm_ls
*ls
, struct dlm_message
*ms
,
4079 if (dlm_locking_stopped(ls
)) {
4080 dlm_add_requestqueue(ls
, nodeid
, ms
);
4082 dlm_wait_requestqueue(ls
);
4083 _receive_message(ls
, ms
);
4087 /* This is called by dlm_recoverd to process messages that were saved on
4088 the requestqueue. */
4090 void dlm_receive_message_saved(struct dlm_ls
*ls
, struct dlm_message
*ms
)
4092 _receive_message(ls
, ms
);
4095 /* This is called by the midcomms layer when something is received for
4096 the lockspace. It could be either a MSG (normal message sent as part of
4097 standard locking activity) or an RCOM (recovery message sent as part of
4098 lockspace recovery). */
4100 void dlm_receive_buffer(union dlm_packet
*p
, int nodeid
)
4102 struct dlm_header
*hd
= &p
->header
;
4106 switch (hd
->h_cmd
) {
4108 dlm_message_in(&p
->message
);
4109 type
= p
->message
.m_type
;
4112 dlm_rcom_in(&p
->rcom
);
4113 type
= p
->rcom
.rc_type
;
4116 log_print("invalid h_cmd %d from %u", hd
->h_cmd
, nodeid
);
4120 if (hd
->h_nodeid
!= nodeid
) {
4121 log_print("invalid h_nodeid %d from %d lockspace %x",
4122 hd
->h_nodeid
, nodeid
, hd
->h_lockspace
);
4126 ls
= dlm_find_lockspace_global(hd
->h_lockspace
);
4128 if (dlm_config
.ci_log_debug
)
4129 log_print("invalid lockspace %x from %d cmd %d type %d",
4130 hd
->h_lockspace
, nodeid
, hd
->h_cmd
, type
);
4132 if (hd
->h_cmd
== DLM_RCOM
&& type
== DLM_RCOM_STATUS
)
4133 dlm_send_ls_not_ready(nodeid
, &p
->rcom
);
4137 /* this rwsem allows dlm_ls_stop() to wait for all dlm_recv threads to
4138 be inactive (in this ls) before transitioning to recovery mode */
4140 down_read(&ls
->ls_recv_active
);
4141 if (hd
->h_cmd
== DLM_MSG
)
4142 dlm_receive_message(ls
, &p
->message
, nodeid
);
4144 dlm_receive_rcom(ls
, &p
->rcom
, nodeid
);
4145 up_read(&ls
->ls_recv_active
);
4147 dlm_put_lockspace(ls
);
4150 static void recover_convert_waiter(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
,
4151 struct dlm_message
*ms_stub
)
4153 if (middle_conversion(lkb
)) {
4155 memset(ms_stub
, 0, sizeof(struct dlm_message
));
4156 ms_stub
->m_flags
= DLM_IFL_STUB_MS
;
4157 ms_stub
->m_type
= DLM_MSG_CONVERT_REPLY
;
4158 ms_stub
->m_result
= -EINPROGRESS
;
4159 ms_stub
->m_header
.h_nodeid
= lkb
->lkb_nodeid
;
4160 _receive_convert_reply(lkb
, ms_stub
);
4162 /* Same special case as in receive_rcom_lock_args() */
4163 lkb
->lkb_grmode
= DLM_LOCK_IV
;
4164 rsb_set_flag(lkb
->lkb_resource
, RSB_RECOVER_CONVERT
);
4167 } else if (lkb
->lkb_rqmode
>= lkb
->lkb_grmode
) {
4168 lkb
->lkb_flags
|= DLM_IFL_RESEND
;
4171 /* lkb->lkb_rqmode < lkb->lkb_grmode shouldn't happen since down
4172 conversions are async; there's no reply from the remote master */
4175 /* A waiting lkb needs recovery if the master node has failed, or
4176 the master node is changing (only when no directory is used) */
4178 static int waiter_needs_recovery(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
)
4180 if (dlm_is_removed(ls
, lkb
->lkb_nodeid
))
4183 if (!dlm_no_directory(ls
))
4186 if (dlm_dir_nodeid(lkb
->lkb_resource
) != lkb
->lkb_nodeid
)
4192 /* Recovery for locks that are waiting for replies from nodes that are now
4193 gone. We can just complete unlocks and cancels by faking a reply from the
4194 dead node. Requests and up-conversions we flag to be resent after
4195 recovery. Down-conversions can just be completed with a fake reply like
4196 unlocks. Conversions between PR and CW need special attention. */
4198 void dlm_recover_waiters_pre(struct dlm_ls
*ls
)
4200 struct dlm_lkb
*lkb
, *safe
;
4201 struct dlm_message
*ms_stub
;
4202 int wait_type
, stub_unlock_result
, stub_cancel_result
;
4204 ms_stub
= kmalloc(sizeof(struct dlm_message
), GFP_KERNEL
);
4206 log_error(ls
, "dlm_recover_waiters_pre no mem");
4210 mutex_lock(&ls
->ls_waiters_mutex
);
4212 list_for_each_entry_safe(lkb
, safe
, &ls
->ls_waiters
, lkb_wait_reply
) {
4214 /* exclude debug messages about unlocks because there can be so
4215 many and they aren't very interesting */
4217 if (lkb
->lkb_wait_type
!= DLM_MSG_UNLOCK
) {
4218 log_debug(ls
, "recover_waiter %x nodeid %d "
4219 "msg %d to %d", lkb
->lkb_id
, lkb
->lkb_nodeid
,
4220 lkb
->lkb_wait_type
, lkb
->lkb_wait_nodeid
);
4223 /* all outstanding lookups, regardless of destination will be
4224 resent after recovery is done */
4226 if (lkb
->lkb_wait_type
== DLM_MSG_LOOKUP
) {
4227 lkb
->lkb_flags
|= DLM_IFL_RESEND
;
4231 if (!waiter_needs_recovery(ls
, lkb
))
4234 wait_type
= lkb
->lkb_wait_type
;
4235 stub_unlock_result
= -DLM_EUNLOCK
;
4236 stub_cancel_result
= -DLM_ECANCEL
;
4238 /* Main reply may have been received leaving a zero wait_type,
4239 but a reply for the overlapping op may not have been
4240 received. In that case we need to fake the appropriate
4241 reply for the overlap op. */
4244 if (is_overlap_cancel(lkb
)) {
4245 wait_type
= DLM_MSG_CANCEL
;
4246 if (lkb
->lkb_grmode
== DLM_LOCK_IV
)
4247 stub_cancel_result
= 0;
4249 if (is_overlap_unlock(lkb
)) {
4250 wait_type
= DLM_MSG_UNLOCK
;
4251 if (lkb
->lkb_grmode
== DLM_LOCK_IV
)
4252 stub_unlock_result
= -ENOENT
;
4255 log_debug(ls
, "rwpre overlap %x %x %d %d %d",
4256 lkb
->lkb_id
, lkb
->lkb_flags
, wait_type
,
4257 stub_cancel_result
, stub_unlock_result
);
4260 switch (wait_type
) {
4262 case DLM_MSG_REQUEST
:
4263 lkb
->lkb_flags
|= DLM_IFL_RESEND
;
4266 case DLM_MSG_CONVERT
:
4267 recover_convert_waiter(ls
, lkb
, ms_stub
);
4270 case DLM_MSG_UNLOCK
:
4272 memset(ms_stub
, 0, sizeof(struct dlm_message
));
4273 ms_stub
->m_flags
= DLM_IFL_STUB_MS
;
4274 ms_stub
->m_type
= DLM_MSG_UNLOCK_REPLY
;
4275 ms_stub
->m_result
= stub_unlock_result
;
4276 ms_stub
->m_header
.h_nodeid
= lkb
->lkb_nodeid
;
4277 _receive_unlock_reply(lkb
, ms_stub
);
4281 case DLM_MSG_CANCEL
:
4283 memset(ms_stub
, 0, sizeof(struct dlm_message
));
4284 ms_stub
->m_flags
= DLM_IFL_STUB_MS
;
4285 ms_stub
->m_type
= DLM_MSG_CANCEL_REPLY
;
4286 ms_stub
->m_result
= stub_cancel_result
;
4287 ms_stub
->m_header
.h_nodeid
= lkb
->lkb_nodeid
;
4288 _receive_cancel_reply(lkb
, ms_stub
);
4293 log_error(ls
, "invalid lkb wait_type %d %d",
4294 lkb
->lkb_wait_type
, wait_type
);
4298 mutex_unlock(&ls
->ls_waiters_mutex
);
4302 static struct dlm_lkb
*find_resend_waiter(struct dlm_ls
*ls
)
4304 struct dlm_lkb
*lkb
;
4307 mutex_lock(&ls
->ls_waiters_mutex
);
4308 list_for_each_entry(lkb
, &ls
->ls_waiters
, lkb_wait_reply
) {
4309 if (lkb
->lkb_flags
& DLM_IFL_RESEND
) {
4315 mutex_unlock(&ls
->ls_waiters_mutex
);
4322 /* Deal with lookups and lkb's marked RESEND from _pre. We may now be the
4323 master or dir-node for r. Processing the lkb may result in it being placed
4326 /* We do this after normal locking has been enabled and any saved messages
4327 (in requestqueue) have been processed. We should be confident that at
4328 this point we won't get or process a reply to any of these waiting
4329 operations. But, new ops may be coming in on the rsbs/locks here from
4330 userspace or remotely. */
4332 /* there may have been an overlap unlock/cancel prior to recovery or after
4333 recovery. if before, the lkb may still have a pos wait_count; if after, the
4334 overlap flag would just have been set and nothing new sent. we can be
4335 confident here than any replies to either the initial op or overlap ops
4336 prior to recovery have been received. */
4338 int dlm_recover_waiters_post(struct dlm_ls
*ls
)
4340 struct dlm_lkb
*lkb
;
4342 int error
= 0, mstype
, err
, oc
, ou
;
4345 if (dlm_locking_stopped(ls
)) {
4346 log_debug(ls
, "recover_waiters_post aborted");
4351 lkb
= find_resend_waiter(ls
);
4355 r
= lkb
->lkb_resource
;
4359 mstype
= lkb
->lkb_wait_type
;
4360 oc
= is_overlap_cancel(lkb
);
4361 ou
= is_overlap_unlock(lkb
);
4364 log_debug(ls
, "recover_waiter %x nodeid %d msg %d r_nodeid %d",
4365 lkb
->lkb_id
, lkb
->lkb_nodeid
, mstype
, r
->res_nodeid
);
4367 /* At this point we assume that we won't get a reply to any
4368 previous op or overlap op on this lock. First, do a big
4369 remove_from_waiters() for all previous ops. */
4371 lkb
->lkb_flags
&= ~DLM_IFL_RESEND
;
4372 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_UNLOCK
;
4373 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_CANCEL
;
4374 lkb
->lkb_wait_type
= 0;
4375 lkb
->lkb_wait_count
= 0;
4376 mutex_lock(&ls
->ls_waiters_mutex
);
4377 list_del_init(&lkb
->lkb_wait_reply
);
4378 mutex_unlock(&ls
->ls_waiters_mutex
);
4379 unhold_lkb(lkb
); /* for waiters list */
4382 /* do an unlock or cancel instead of resending */
4384 case DLM_MSG_LOOKUP
:
4385 case DLM_MSG_REQUEST
:
4386 queue_cast(r
, lkb
, ou
? -DLM_EUNLOCK
:
4388 unhold_lkb(lkb
); /* undoes create_lkb() */
4390 case DLM_MSG_CONVERT
:
4392 queue_cast(r
, lkb
, -DLM_ECANCEL
);
4394 lkb
->lkb_exflags
|= DLM_LKF_FORCEUNLOCK
;
4395 _unlock_lock(r
, lkb
);
4403 case DLM_MSG_LOOKUP
:
4404 case DLM_MSG_REQUEST
:
4405 _request_lock(r
, lkb
);
4407 confirm_master(r
, 0);
4409 case DLM_MSG_CONVERT
:
4410 _convert_lock(r
, lkb
);
4418 log_error(ls
, "recover_waiters_post %x %d %x %d %d",
4419 lkb
->lkb_id
, mstype
, lkb
->lkb_flags
, oc
, ou
);
4428 static void purge_queue(struct dlm_rsb
*r
, struct list_head
*queue
,
4429 int (*test
)(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
))
4431 struct dlm_ls
*ls
= r
->res_ls
;
4432 struct dlm_lkb
*lkb
, *safe
;
4434 list_for_each_entry_safe(lkb
, safe
, queue
, lkb_statequeue
) {
4435 if (test(ls
, lkb
)) {
4436 rsb_set_flag(r
, RSB_LOCKS_PURGED
);
4438 /* this put should free the lkb */
4439 if (!dlm_put_lkb(lkb
))
4440 log_error(ls
, "purged lkb not released");
4445 static int purge_dead_test(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
)
4447 return (is_master_copy(lkb
) && dlm_is_removed(ls
, lkb
->lkb_nodeid
));
4450 static int purge_mstcpy_test(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
)
4452 return is_master_copy(lkb
);
4455 static void purge_dead_locks(struct dlm_rsb
*r
)
4457 purge_queue(r
, &r
->res_grantqueue
, &purge_dead_test
);
4458 purge_queue(r
, &r
->res_convertqueue
, &purge_dead_test
);
4459 purge_queue(r
, &r
->res_waitqueue
, &purge_dead_test
);
4462 void dlm_purge_mstcpy_locks(struct dlm_rsb
*r
)
4464 purge_queue(r
, &r
->res_grantqueue
, &purge_mstcpy_test
);
4465 purge_queue(r
, &r
->res_convertqueue
, &purge_mstcpy_test
);
4466 purge_queue(r
, &r
->res_waitqueue
, &purge_mstcpy_test
);
4469 /* Get rid of locks held by nodes that are gone. */
4471 int dlm_purge_locks(struct dlm_ls
*ls
)
4475 log_debug(ls
, "dlm_purge_locks");
4477 down_write(&ls
->ls_root_sem
);
4478 list_for_each_entry(r
, &ls
->ls_root_list
, res_root_list
) {
4482 purge_dead_locks(r
);
4488 up_write(&ls
->ls_root_sem
);
4493 static struct dlm_rsb
*find_purged_rsb(struct dlm_ls
*ls
, int bucket
)
4496 struct dlm_rsb
*r
, *r_ret
= NULL
;
4498 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
4499 for (n
= rb_first(&ls
->ls_rsbtbl
[bucket
].keep
); n
; n
= rb_next(n
)) {
4500 r
= rb_entry(n
, struct dlm_rsb
, res_hashnode
);
4501 if (!rsb_flag(r
, RSB_LOCKS_PURGED
))
4504 rsb_clear_flag(r
, RSB_LOCKS_PURGED
);
4508 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
4512 void dlm_grant_after_purge(struct dlm_ls
*ls
)
4518 r
= find_purged_rsb(ls
, bucket
);
4520 if (bucket
== ls
->ls_rsbtbl_size
- 1)
4527 grant_pending_locks(r
);
4528 confirm_master(r
, 0);
4536 static struct dlm_lkb
*search_remid_list(struct list_head
*head
, int nodeid
,
4539 struct dlm_lkb
*lkb
;
4541 list_for_each_entry(lkb
, head
, lkb_statequeue
) {
4542 if (lkb
->lkb_nodeid
== nodeid
&& lkb
->lkb_remid
== remid
)
4548 static struct dlm_lkb
*search_remid(struct dlm_rsb
*r
, int nodeid
,
4551 struct dlm_lkb
*lkb
;
4553 lkb
= search_remid_list(&r
->res_grantqueue
, nodeid
, remid
);
4556 lkb
= search_remid_list(&r
->res_convertqueue
, nodeid
, remid
);
4559 lkb
= search_remid_list(&r
->res_waitqueue
, nodeid
, remid
);
4565 /* needs at least dlm_rcom + rcom_lock */
4566 static int receive_rcom_lock_args(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
,
4567 struct dlm_rsb
*r
, struct dlm_rcom
*rc
)
4569 struct rcom_lock
*rl
= (struct rcom_lock
*) rc
->rc_buf
;
4571 lkb
->lkb_nodeid
= rc
->rc_header
.h_nodeid
;
4572 lkb
->lkb_ownpid
= le32_to_cpu(rl
->rl_ownpid
);
4573 lkb
->lkb_remid
= le32_to_cpu(rl
->rl_lkid
);
4574 lkb
->lkb_exflags
= le32_to_cpu(rl
->rl_exflags
);
4575 lkb
->lkb_flags
= le32_to_cpu(rl
->rl_flags
) & 0x0000FFFF;
4576 lkb
->lkb_flags
|= DLM_IFL_MSTCPY
;
4577 lkb
->lkb_lvbseq
= le32_to_cpu(rl
->rl_lvbseq
);
4578 lkb
->lkb_rqmode
= rl
->rl_rqmode
;
4579 lkb
->lkb_grmode
= rl
->rl_grmode
;
4580 /* don't set lkb_status because add_lkb wants to itself */
4582 lkb
->lkb_bastfn
= (rl
->rl_asts
& DLM_CB_BAST
) ? &fake_bastfn
: NULL
;
4583 lkb
->lkb_astfn
= (rl
->rl_asts
& DLM_CB_CAST
) ? &fake_astfn
: NULL
;
4585 if (lkb
->lkb_exflags
& DLM_LKF_VALBLK
) {
4586 int lvblen
= rc
->rc_header
.h_length
- sizeof(struct dlm_rcom
) -
4587 sizeof(struct rcom_lock
);
4588 if (lvblen
> ls
->ls_lvblen
)
4590 lkb
->lkb_lvbptr
= dlm_allocate_lvb(ls
);
4591 if (!lkb
->lkb_lvbptr
)
4593 memcpy(lkb
->lkb_lvbptr
, rl
->rl_lvb
, lvblen
);
4596 /* Conversions between PR and CW (middle modes) need special handling.
4597 The real granted mode of these converting locks cannot be determined
4598 until all locks have been rebuilt on the rsb (recover_conversion) */
4600 if (rl
->rl_wait_type
== cpu_to_le16(DLM_MSG_CONVERT
) &&
4601 middle_conversion(lkb
)) {
4602 rl
->rl_status
= DLM_LKSTS_CONVERT
;
4603 lkb
->lkb_grmode
= DLM_LOCK_IV
;
4604 rsb_set_flag(r
, RSB_RECOVER_CONVERT
);
4610 /* This lkb may have been recovered in a previous aborted recovery so we need
4611 to check if the rsb already has an lkb with the given remote nodeid/lkid.
4612 If so we just send back a standard reply. If not, we create a new lkb with
4613 the given values and send back our lkid. We send back our lkid by sending
4614 back the rcom_lock struct we got but with the remid field filled in. */
4616 /* needs at least dlm_rcom + rcom_lock */
4617 int dlm_recover_master_copy(struct dlm_ls
*ls
, struct dlm_rcom
*rc
)
4619 struct rcom_lock
*rl
= (struct rcom_lock
*) rc
->rc_buf
;
4621 struct dlm_lkb
*lkb
;
4624 if (rl
->rl_parent_lkid
) {
4625 error
= -EOPNOTSUPP
;
4629 error
= find_rsb(ls
, rl
->rl_name
, le16_to_cpu(rl
->rl_namelen
),
4636 lkb
= search_remid(r
, rc
->rc_header
.h_nodeid
, le32_to_cpu(rl
->rl_lkid
));
4642 error
= create_lkb(ls
, &lkb
);
4646 error
= receive_rcom_lock_args(ls
, lkb
, r
, rc
);
4653 add_lkb(r
, lkb
, rl
->rl_status
);
4657 /* this is the new value returned to the lock holder for
4658 saving in its process-copy lkb */
4659 rl
->rl_remid
= cpu_to_le32(lkb
->lkb_id
);
4666 log_debug(ls
, "recover_master_copy %d %x", error
,
4667 le32_to_cpu(rl
->rl_lkid
));
4668 rl
->rl_result
= cpu_to_le32(error
);
4672 /* needs at least dlm_rcom + rcom_lock */
4673 int dlm_recover_process_copy(struct dlm_ls
*ls
, struct dlm_rcom
*rc
)
4675 struct rcom_lock
*rl
= (struct rcom_lock
*) rc
->rc_buf
;
4677 struct dlm_lkb
*lkb
;
4680 error
= find_lkb(ls
, le32_to_cpu(rl
->rl_lkid
), &lkb
);
4682 log_error(ls
, "recover_process_copy no lkid %x",
4683 le32_to_cpu(rl
->rl_lkid
));
4687 DLM_ASSERT(is_process_copy(lkb
), dlm_print_lkb(lkb
););
4689 error
= le32_to_cpu(rl
->rl_result
);
4691 r
= lkb
->lkb_resource
;
4697 /* There's a chance the new master received our lock before
4698 dlm_recover_master_reply(), this wouldn't happen if we did
4699 a barrier between recover_masters and recover_locks. */
4700 log_debug(ls
, "master copy not ready %x r %lx %s", lkb
->lkb_id
,
4701 (unsigned long)r
, r
->res_name
);
4702 dlm_send_rcom_lock(r
, lkb
);
4705 log_debug(ls
, "master copy exists %x", lkb
->lkb_id
);
4708 lkb
->lkb_remid
= le32_to_cpu(rl
->rl_remid
);
4711 log_error(ls
, "dlm_recover_process_copy unknown error %d %x",
4712 error
, lkb
->lkb_id
);
4715 /* an ack for dlm_recover_locks() which waits for replies from
4716 all the locks it sends to new masters */
4717 dlm_recovered_lock(r
);
4726 int dlm_user_request(struct dlm_ls
*ls
, struct dlm_user_args
*ua
,
4727 int mode
, uint32_t flags
, void *name
, unsigned int namelen
,
4728 unsigned long timeout_cs
)
4730 struct dlm_lkb
*lkb
;
4731 struct dlm_args args
;
4734 dlm_lock_recovery(ls
);
4736 error
= create_lkb(ls
, &lkb
);
4742 if (flags
& DLM_LKF_VALBLK
) {
4743 ua
->lksb
.sb_lvbptr
= kzalloc(DLM_USER_LVB_LEN
, GFP_NOFS
);
4744 if (!ua
->lksb
.sb_lvbptr
) {
4752 /* After ua is attached to lkb it will be freed by dlm_free_lkb().
4753 When DLM_IFL_USER is set, the dlm knows that this is a userspace
4754 lock and that lkb_astparam is the dlm_user_args structure. */
4756 error
= set_lock_args(mode
, &ua
->lksb
, flags
, namelen
, timeout_cs
,
4757 fake_astfn
, ua
, fake_bastfn
, &args
);
4758 lkb
->lkb_flags
|= DLM_IFL_USER
;
4765 error
= request_lock(ls
, lkb
, name
, namelen
, &args
);
4781 /* add this new lkb to the per-process list of locks */
4782 spin_lock(&ua
->proc
->locks_spin
);
4784 list_add_tail(&lkb
->lkb_ownqueue
, &ua
->proc
->locks
);
4785 spin_unlock(&ua
->proc
->locks_spin
);
4787 dlm_unlock_recovery(ls
);
4791 int dlm_user_convert(struct dlm_ls
*ls
, struct dlm_user_args
*ua_tmp
,
4792 int mode
, uint32_t flags
, uint32_t lkid
, char *lvb_in
,
4793 unsigned long timeout_cs
)
4795 struct dlm_lkb
*lkb
;
4796 struct dlm_args args
;
4797 struct dlm_user_args
*ua
;
4800 dlm_lock_recovery(ls
);
4802 error
= find_lkb(ls
, lkid
, &lkb
);
4806 /* user can change the params on its lock when it converts it, or
4807 add an lvb that didn't exist before */
4811 if (flags
& DLM_LKF_VALBLK
&& !ua
->lksb
.sb_lvbptr
) {
4812 ua
->lksb
.sb_lvbptr
= kzalloc(DLM_USER_LVB_LEN
, GFP_NOFS
);
4813 if (!ua
->lksb
.sb_lvbptr
) {
4818 if (lvb_in
&& ua
->lksb
.sb_lvbptr
)
4819 memcpy(ua
->lksb
.sb_lvbptr
, lvb_in
, DLM_USER_LVB_LEN
);
4821 ua
->xid
= ua_tmp
->xid
;
4822 ua
->castparam
= ua_tmp
->castparam
;
4823 ua
->castaddr
= ua_tmp
->castaddr
;
4824 ua
->bastparam
= ua_tmp
->bastparam
;
4825 ua
->bastaddr
= ua_tmp
->bastaddr
;
4826 ua
->user_lksb
= ua_tmp
->user_lksb
;
4828 error
= set_lock_args(mode
, &ua
->lksb
, flags
, 0, timeout_cs
,
4829 fake_astfn
, ua
, fake_bastfn
, &args
);
4833 error
= convert_lock(ls
, lkb
, &args
);
4835 if (error
== -EINPROGRESS
|| error
== -EAGAIN
|| error
== -EDEADLK
)
4840 dlm_unlock_recovery(ls
);
4845 int dlm_user_unlock(struct dlm_ls
*ls
, struct dlm_user_args
*ua_tmp
,
4846 uint32_t flags
, uint32_t lkid
, char *lvb_in
)
4848 struct dlm_lkb
*lkb
;
4849 struct dlm_args args
;
4850 struct dlm_user_args
*ua
;
4853 dlm_lock_recovery(ls
);
4855 error
= find_lkb(ls
, lkid
, &lkb
);
4861 if (lvb_in
&& ua
->lksb
.sb_lvbptr
)
4862 memcpy(ua
->lksb
.sb_lvbptr
, lvb_in
, DLM_USER_LVB_LEN
);
4863 if (ua_tmp
->castparam
)
4864 ua
->castparam
= ua_tmp
->castparam
;
4865 ua
->user_lksb
= ua_tmp
->user_lksb
;
4867 error
= set_unlock_args(flags
, ua
, &args
);
4871 error
= unlock_lock(ls
, lkb
, &args
);
4873 if (error
== -DLM_EUNLOCK
)
4875 /* from validate_unlock_args() */
4876 if (error
== -EBUSY
&& (flags
& DLM_LKF_FORCEUNLOCK
))
4881 spin_lock(&ua
->proc
->locks_spin
);
4882 /* dlm_user_add_cb() may have already taken lkb off the proc list */
4883 if (!list_empty(&lkb
->lkb_ownqueue
))
4884 list_move(&lkb
->lkb_ownqueue
, &ua
->proc
->unlocking
);
4885 spin_unlock(&ua
->proc
->locks_spin
);
4889 dlm_unlock_recovery(ls
);
4894 int dlm_user_cancel(struct dlm_ls
*ls
, struct dlm_user_args
*ua_tmp
,
4895 uint32_t flags
, uint32_t lkid
)
4897 struct dlm_lkb
*lkb
;
4898 struct dlm_args args
;
4899 struct dlm_user_args
*ua
;
4902 dlm_lock_recovery(ls
);
4904 error
= find_lkb(ls
, lkid
, &lkb
);
4909 if (ua_tmp
->castparam
)
4910 ua
->castparam
= ua_tmp
->castparam
;
4911 ua
->user_lksb
= ua_tmp
->user_lksb
;
4913 error
= set_unlock_args(flags
, ua
, &args
);
4917 error
= cancel_lock(ls
, lkb
, &args
);
4919 if (error
== -DLM_ECANCEL
)
4921 /* from validate_unlock_args() */
4922 if (error
== -EBUSY
)
4927 dlm_unlock_recovery(ls
);
4932 int dlm_user_deadlock(struct dlm_ls
*ls
, uint32_t flags
, uint32_t lkid
)
4934 struct dlm_lkb
*lkb
;
4935 struct dlm_args args
;
4936 struct dlm_user_args
*ua
;
4940 dlm_lock_recovery(ls
);
4942 error
= find_lkb(ls
, lkid
, &lkb
);
4948 error
= set_unlock_args(flags
, ua
, &args
);
4952 /* same as cancel_lock(), but set DEADLOCK_CANCEL after lock_rsb */
4954 r
= lkb
->lkb_resource
;
4958 error
= validate_unlock_args(lkb
, &args
);
4961 lkb
->lkb_flags
|= DLM_IFL_DEADLOCK_CANCEL
;
4963 error
= _cancel_lock(r
, lkb
);
4968 if (error
== -DLM_ECANCEL
)
4970 /* from validate_unlock_args() */
4971 if (error
== -EBUSY
)
4976 dlm_unlock_recovery(ls
);
4980 /* lkb's that are removed from the waiters list by revert are just left on the
4981 orphans list with the granted orphan locks, to be freed by purge */
4983 static int orphan_proc_lock(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
)
4985 struct dlm_args args
;
4989 mutex_lock(&ls
->ls_orphans_mutex
);
4990 list_add_tail(&lkb
->lkb_ownqueue
, &ls
->ls_orphans
);
4991 mutex_unlock(&ls
->ls_orphans_mutex
);
4993 set_unlock_args(0, lkb
->lkb_ua
, &args
);
4995 error
= cancel_lock(ls
, lkb
, &args
);
4996 if (error
== -DLM_ECANCEL
)
5001 /* The force flag allows the unlock to go ahead even if the lkb isn't granted.
5002 Regardless of what rsb queue the lock is on, it's removed and freed. */
5004 static int unlock_proc_lock(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
)
5006 struct dlm_args args
;
5009 set_unlock_args(DLM_LKF_FORCEUNLOCK
, lkb
->lkb_ua
, &args
);
5011 error
= unlock_lock(ls
, lkb
, &args
);
5012 if (error
== -DLM_EUNLOCK
)
5017 /* We have to release clear_proc_locks mutex before calling unlock_proc_lock()
5018 (which does lock_rsb) due to deadlock with receiving a message that does
5019 lock_rsb followed by dlm_user_add_cb() */
5021 static struct dlm_lkb
*del_proc_lock(struct dlm_ls
*ls
,
5022 struct dlm_user_proc
*proc
)
5024 struct dlm_lkb
*lkb
= NULL
;
5026 mutex_lock(&ls
->ls_clear_proc_locks
);
5027 if (list_empty(&proc
->locks
))
5030 lkb
= list_entry(proc
->locks
.next
, struct dlm_lkb
, lkb_ownqueue
);
5031 list_del_init(&lkb
->lkb_ownqueue
);
5033 if (lkb
->lkb_exflags
& DLM_LKF_PERSISTENT
)
5034 lkb
->lkb_flags
|= DLM_IFL_ORPHAN
;
5036 lkb
->lkb_flags
|= DLM_IFL_DEAD
;
5038 mutex_unlock(&ls
->ls_clear_proc_locks
);
5042 /* The ls_clear_proc_locks mutex protects against dlm_user_add_cb() which
5043 1) references lkb->ua which we free here and 2) adds lkbs to proc->asts,
5044 which we clear here. */
5046 /* proc CLOSING flag is set so no more device_reads should look at proc->asts
5047 list, and no more device_writes should add lkb's to proc->locks list; so we
5048 shouldn't need to take asts_spin or locks_spin here. this assumes that
5049 device reads/writes/closes are serialized -- FIXME: we may need to serialize
5052 void dlm_clear_proc_locks(struct dlm_ls
*ls
, struct dlm_user_proc
*proc
)
5054 struct dlm_lkb
*lkb
, *safe
;
5056 dlm_lock_recovery(ls
);
5059 lkb
= del_proc_lock(ls
, proc
);
5063 if (lkb
->lkb_exflags
& DLM_LKF_PERSISTENT
)
5064 orphan_proc_lock(ls
, lkb
);
5066 unlock_proc_lock(ls
, lkb
);
5068 /* this removes the reference for the proc->locks list
5069 added by dlm_user_request, it may result in the lkb
5075 mutex_lock(&ls
->ls_clear_proc_locks
);
5077 /* in-progress unlocks */
5078 list_for_each_entry_safe(lkb
, safe
, &proc
->unlocking
, lkb_ownqueue
) {
5079 list_del_init(&lkb
->lkb_ownqueue
);
5080 lkb
->lkb_flags
|= DLM_IFL_DEAD
;
5084 list_for_each_entry_safe(lkb
, safe
, &proc
->asts
, lkb_cb_list
) {
5085 memset(&lkb
->lkb_callbacks
, 0,
5086 sizeof(struct dlm_callback
) * DLM_CALLBACKS_SIZE
);
5087 list_del_init(&lkb
->lkb_cb_list
);
5091 mutex_unlock(&ls
->ls_clear_proc_locks
);
5092 dlm_unlock_recovery(ls
);
5095 static void purge_proc_locks(struct dlm_ls
*ls
, struct dlm_user_proc
*proc
)
5097 struct dlm_lkb
*lkb
, *safe
;
5101 spin_lock(&proc
->locks_spin
);
5102 if (!list_empty(&proc
->locks
)) {
5103 lkb
= list_entry(proc
->locks
.next
, struct dlm_lkb
,
5105 list_del_init(&lkb
->lkb_ownqueue
);
5107 spin_unlock(&proc
->locks_spin
);
5112 lkb
->lkb_flags
|= DLM_IFL_DEAD
;
5113 unlock_proc_lock(ls
, lkb
);
5114 dlm_put_lkb(lkb
); /* ref from proc->locks list */
5117 spin_lock(&proc
->locks_spin
);
5118 list_for_each_entry_safe(lkb
, safe
, &proc
->unlocking
, lkb_ownqueue
) {
5119 list_del_init(&lkb
->lkb_ownqueue
);
5120 lkb
->lkb_flags
|= DLM_IFL_DEAD
;
5123 spin_unlock(&proc
->locks_spin
);
5125 spin_lock(&proc
->asts_spin
);
5126 list_for_each_entry_safe(lkb
, safe
, &proc
->asts
, lkb_cb_list
) {
5127 memset(&lkb
->lkb_callbacks
, 0,
5128 sizeof(struct dlm_callback
) * DLM_CALLBACKS_SIZE
);
5129 list_del_init(&lkb
->lkb_cb_list
);
5132 spin_unlock(&proc
->asts_spin
);
5135 /* pid of 0 means purge all orphans */
5137 static void do_purge(struct dlm_ls
*ls
, int nodeid
, int pid
)
5139 struct dlm_lkb
*lkb
, *safe
;
5141 mutex_lock(&ls
->ls_orphans_mutex
);
5142 list_for_each_entry_safe(lkb
, safe
, &ls
->ls_orphans
, lkb_ownqueue
) {
5143 if (pid
&& lkb
->lkb_ownpid
!= pid
)
5145 unlock_proc_lock(ls
, lkb
);
5146 list_del_init(&lkb
->lkb_ownqueue
);
5149 mutex_unlock(&ls
->ls_orphans_mutex
);
5152 static int send_purge(struct dlm_ls
*ls
, int nodeid
, int pid
)
5154 struct dlm_message
*ms
;
5155 struct dlm_mhandle
*mh
;
5158 error
= _create_message(ls
, sizeof(struct dlm_message
), nodeid
,
5159 DLM_MSG_PURGE
, &ms
, &mh
);
5162 ms
->m_nodeid
= nodeid
;
5165 return send_message(mh
, ms
);
5168 int dlm_user_purge(struct dlm_ls
*ls
, struct dlm_user_proc
*proc
,
5169 int nodeid
, int pid
)
5173 if (nodeid
!= dlm_our_nodeid()) {
5174 error
= send_purge(ls
, nodeid
, pid
);
5176 dlm_lock_recovery(ls
);
5177 if (pid
== current
->pid
)
5178 purge_proc_locks(ls
, proc
);
5180 do_purge(ls
, nodeid
, pid
);
5181 dlm_unlock_recovery(ls
);