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/slab.h>
60 #include "dlm_internal.h"
61 #include <linux/dlm_device.h>
64 #include "requestqueue.h"
68 #include "lockspace.h"
73 #include "lvb_table.h"
77 static int send_request(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
);
78 static int send_convert(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
);
79 static int send_unlock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
);
80 static int send_cancel(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
);
81 static int send_grant(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
);
82 static int send_bast(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int mode
);
83 static int send_lookup(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
);
84 static int send_remove(struct dlm_rsb
*r
);
85 static int _request_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
);
86 static int _cancel_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
);
87 static void __receive_convert_reply(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
88 struct dlm_message
*ms
);
89 static int receive_extralen(struct dlm_message
*ms
);
90 static void do_purge(struct dlm_ls
*ls
, int nodeid
, int pid
);
91 static void del_timeout(struct dlm_lkb
*lkb
);
94 * Lock compatibilty matrix - thanks Steve
95 * UN = Unlocked state. Not really a state, used as a flag
96 * PD = Padding. Used to make the matrix a nice power of two in size
97 * Other states are the same as the VMS DLM.
98 * Usage: matrix[grmode+1][rqmode+1] (although m[rq+1][gr+1] is the same)
101 static const int __dlm_compat_matrix
[8][8] = {
102 /* UN NL CR CW PR PW EX PD */
103 {1, 1, 1, 1, 1, 1, 1, 0}, /* UN */
104 {1, 1, 1, 1, 1, 1, 1, 0}, /* NL */
105 {1, 1, 1, 1, 1, 1, 0, 0}, /* CR */
106 {1, 1, 1, 1, 0, 0, 0, 0}, /* CW */
107 {1, 1, 1, 0, 1, 0, 0, 0}, /* PR */
108 {1, 1, 1, 0, 0, 0, 0, 0}, /* PW */
109 {1, 1, 0, 0, 0, 0, 0, 0}, /* EX */
110 {0, 0, 0, 0, 0, 0, 0, 0} /* PD */
114 * This defines the direction of transfer of LVB data.
115 * Granted mode is the row; requested mode is the column.
116 * Usage: matrix[grmode+1][rqmode+1]
117 * 1 = LVB is returned to the caller
118 * 0 = LVB is written to the resource
119 * -1 = nothing happens to the LVB
122 const int dlm_lvb_operations
[8][8] = {
123 /* UN NL CR CW PR PW EX PD*/
124 { -1, 1, 1, 1, 1, 1, 1, -1 }, /* UN */
125 { -1, 1, 1, 1, 1, 1, 1, 0 }, /* NL */
126 { -1, -1, 1, 1, 1, 1, 1, 0 }, /* CR */
127 { -1, -1, -1, 1, 1, 1, 1, 0 }, /* CW */
128 { -1, -1, -1, -1, 1, 1, 1, 0 }, /* PR */
129 { -1, 0, 0, 0, 0, 0, 1, 0 }, /* PW */
130 { -1, 0, 0, 0, 0, 0, 0, 0 }, /* EX */
131 { -1, 0, 0, 0, 0, 0, 0, 0 } /* PD */
134 #define modes_compat(gr, rq) \
135 __dlm_compat_matrix[(gr)->lkb_grmode + 1][(rq)->lkb_rqmode + 1]
137 int dlm_modes_compat(int mode1
, int mode2
)
139 return __dlm_compat_matrix
[mode1
+ 1][mode2
+ 1];
143 * Compatibility matrix for conversions with QUECVT set.
144 * Granted mode is the row; requested mode is the column.
145 * Usage: matrix[grmode+1][rqmode+1]
148 static const int __quecvt_compat_matrix
[8][8] = {
149 /* UN NL CR CW PR PW EX PD */
150 {0, 0, 0, 0, 0, 0, 0, 0}, /* UN */
151 {0, 0, 1, 1, 1, 1, 1, 0}, /* NL */
152 {0, 0, 0, 1, 1, 1, 1, 0}, /* CR */
153 {0, 0, 0, 0, 1, 1, 1, 0}, /* CW */
154 {0, 0, 0, 1, 0, 1, 1, 0}, /* PR */
155 {0, 0, 0, 0, 0, 0, 1, 0}, /* PW */
156 {0, 0, 0, 0, 0, 0, 0, 0}, /* EX */
157 {0, 0, 0, 0, 0, 0, 0, 0} /* PD */
160 void dlm_print_lkb(struct dlm_lkb
*lkb
)
162 printk(KERN_ERR
"lkb: nodeid %d id %x remid %x exflags %x flags %x\n"
163 " status %d rqmode %d grmode %d wait_type %d\n",
164 lkb
->lkb_nodeid
, lkb
->lkb_id
, lkb
->lkb_remid
, lkb
->lkb_exflags
,
165 lkb
->lkb_flags
, lkb
->lkb_status
, lkb
->lkb_rqmode
,
166 lkb
->lkb_grmode
, lkb
->lkb_wait_type
);
169 static void dlm_print_rsb(struct dlm_rsb
*r
)
171 printk(KERN_ERR
"rsb: nodeid %d flags %lx first %x rlc %d name %s\n",
172 r
->res_nodeid
, r
->res_flags
, r
->res_first_lkid
,
173 r
->res_recover_locks_count
, r
->res_name
);
176 void dlm_dump_rsb(struct dlm_rsb
*r
)
182 printk(KERN_ERR
"rsb: root_list empty %d recover_list empty %d\n",
183 list_empty(&r
->res_root_list
), list_empty(&r
->res_recover_list
));
184 printk(KERN_ERR
"rsb lookup list\n");
185 list_for_each_entry(lkb
, &r
->res_lookup
, lkb_rsb_lookup
)
187 printk(KERN_ERR
"rsb grant queue:\n");
188 list_for_each_entry(lkb
, &r
->res_grantqueue
, lkb_statequeue
)
190 printk(KERN_ERR
"rsb convert queue:\n");
191 list_for_each_entry(lkb
, &r
->res_convertqueue
, lkb_statequeue
)
193 printk(KERN_ERR
"rsb wait queue:\n");
194 list_for_each_entry(lkb
, &r
->res_waitqueue
, lkb_statequeue
)
198 /* Threads cannot use the lockspace while it's being recovered */
200 static inline void dlm_lock_recovery(struct dlm_ls
*ls
)
202 down_read(&ls
->ls_in_recovery
);
205 void dlm_unlock_recovery(struct dlm_ls
*ls
)
207 up_read(&ls
->ls_in_recovery
);
210 int dlm_lock_recovery_try(struct dlm_ls
*ls
)
212 return down_read_trylock(&ls
->ls_in_recovery
);
215 static inline int can_be_queued(struct dlm_lkb
*lkb
)
217 return !(lkb
->lkb_exflags
& DLM_LKF_NOQUEUE
);
220 static inline int force_blocking_asts(struct dlm_lkb
*lkb
)
222 return (lkb
->lkb_exflags
& DLM_LKF_NOQUEUEBAST
);
225 static inline int is_demoted(struct dlm_lkb
*lkb
)
227 return (lkb
->lkb_sbflags
& DLM_SBF_DEMOTED
);
230 static inline int is_altmode(struct dlm_lkb
*lkb
)
232 return (lkb
->lkb_sbflags
& DLM_SBF_ALTMODE
);
235 static inline int is_granted(struct dlm_lkb
*lkb
)
237 return (lkb
->lkb_status
== DLM_LKSTS_GRANTED
);
240 static inline int is_remote(struct dlm_rsb
*r
)
242 DLM_ASSERT(r
->res_nodeid
>= 0, dlm_print_rsb(r
););
243 return !!r
->res_nodeid
;
246 static inline int is_process_copy(struct dlm_lkb
*lkb
)
248 return (lkb
->lkb_nodeid
&& !(lkb
->lkb_flags
& DLM_IFL_MSTCPY
));
251 static inline int is_master_copy(struct dlm_lkb
*lkb
)
253 if (lkb
->lkb_flags
& DLM_IFL_MSTCPY
)
254 DLM_ASSERT(lkb
->lkb_nodeid
, dlm_print_lkb(lkb
););
255 return (lkb
->lkb_flags
& DLM_IFL_MSTCPY
) ? 1 : 0;
258 static inline int middle_conversion(struct dlm_lkb
*lkb
)
260 if ((lkb
->lkb_grmode
==DLM_LOCK_PR
&& lkb
->lkb_rqmode
==DLM_LOCK_CW
) ||
261 (lkb
->lkb_rqmode
==DLM_LOCK_PR
&& lkb
->lkb_grmode
==DLM_LOCK_CW
))
266 static inline int down_conversion(struct dlm_lkb
*lkb
)
268 return (!middle_conversion(lkb
) && lkb
->lkb_rqmode
< lkb
->lkb_grmode
);
271 static inline int is_overlap_unlock(struct dlm_lkb
*lkb
)
273 return lkb
->lkb_flags
& DLM_IFL_OVERLAP_UNLOCK
;
276 static inline int is_overlap_cancel(struct dlm_lkb
*lkb
)
278 return lkb
->lkb_flags
& DLM_IFL_OVERLAP_CANCEL
;
281 static inline int is_overlap(struct dlm_lkb
*lkb
)
283 return (lkb
->lkb_flags
& (DLM_IFL_OVERLAP_UNLOCK
|
284 DLM_IFL_OVERLAP_CANCEL
));
287 static void queue_cast(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int rv
)
289 if (is_master_copy(lkb
))
294 DLM_ASSERT(lkb
->lkb_lksb
, dlm_print_lkb(lkb
););
296 /* if the operation was a cancel, then return -DLM_ECANCEL, if a
297 timeout caused the cancel then return -ETIMEDOUT */
298 if (rv
== -DLM_ECANCEL
&& (lkb
->lkb_flags
& DLM_IFL_TIMEOUT_CANCEL
)) {
299 lkb
->lkb_flags
&= ~DLM_IFL_TIMEOUT_CANCEL
;
303 if (rv
== -DLM_ECANCEL
&& (lkb
->lkb_flags
& DLM_IFL_DEADLOCK_CANCEL
)) {
304 lkb
->lkb_flags
&= ~DLM_IFL_DEADLOCK_CANCEL
;
308 dlm_add_ast(lkb
, DLM_CB_CAST
, lkb
->lkb_grmode
, rv
, lkb
->lkb_sbflags
);
311 static inline void queue_cast_overlap(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
314 is_overlap_unlock(lkb
) ? -DLM_EUNLOCK
: -DLM_ECANCEL
);
317 static void queue_bast(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int rqmode
)
319 if (is_master_copy(lkb
)) {
320 send_bast(r
, lkb
, rqmode
);
322 dlm_add_ast(lkb
, DLM_CB_BAST
, rqmode
, 0, 0);
327 * Basic operations on rsb's and lkb's
330 static struct dlm_rsb
*create_rsb(struct dlm_ls
*ls
, char *name
, int len
)
334 r
= dlm_allocate_rsb(ls
, len
);
340 memcpy(r
->res_name
, name
, len
);
341 mutex_init(&r
->res_mutex
);
343 INIT_LIST_HEAD(&r
->res_lookup
);
344 INIT_LIST_HEAD(&r
->res_grantqueue
);
345 INIT_LIST_HEAD(&r
->res_convertqueue
);
346 INIT_LIST_HEAD(&r
->res_waitqueue
);
347 INIT_LIST_HEAD(&r
->res_root_list
);
348 INIT_LIST_HEAD(&r
->res_recover_list
);
353 static int search_rsb_list(struct list_head
*head
, char *name
, int len
,
354 unsigned int flags
, struct dlm_rsb
**r_ret
)
359 list_for_each_entry(r
, head
, res_hashchain
) {
360 if (len
== r
->res_length
&& !memcmp(name
, r
->res_name
, len
))
367 if (r
->res_nodeid
&& (flags
& R_MASTER
))
373 static int _search_rsb(struct dlm_ls
*ls
, char *name
, int len
, int b
,
374 unsigned int flags
, struct dlm_rsb
**r_ret
)
379 error
= search_rsb_list(&ls
->ls_rsbtbl
[b
].list
, name
, len
, flags
, &r
);
381 kref_get(&r
->res_ref
);
384 error
= search_rsb_list(&ls
->ls_rsbtbl
[b
].toss
, name
, len
, flags
, &r
);
388 list_move(&r
->res_hashchain
, &ls
->ls_rsbtbl
[b
].list
);
390 if (dlm_no_directory(ls
))
393 if (r
->res_nodeid
== -1) {
394 rsb_clear_flag(r
, RSB_MASTER_UNCERTAIN
);
395 r
->res_first_lkid
= 0;
396 } else if (r
->res_nodeid
> 0) {
397 rsb_set_flag(r
, RSB_MASTER_UNCERTAIN
);
398 r
->res_first_lkid
= 0;
400 DLM_ASSERT(r
->res_nodeid
== 0, dlm_print_rsb(r
););
401 DLM_ASSERT(!rsb_flag(r
, RSB_MASTER_UNCERTAIN
),);
408 static int search_rsb(struct dlm_ls
*ls
, char *name
, int len
, int b
,
409 unsigned int flags
, struct dlm_rsb
**r_ret
)
412 spin_lock(&ls
->ls_rsbtbl
[b
].lock
);
413 error
= _search_rsb(ls
, name
, len
, b
, flags
, r_ret
);
414 spin_unlock(&ls
->ls_rsbtbl
[b
].lock
);
419 * Find rsb in rsbtbl and potentially create/add one
421 * Delaying the release of rsb's has a similar benefit to applications keeping
422 * NL locks on an rsb, but without the guarantee that the cached master value
423 * will still be valid when the rsb is reused. Apps aren't always smart enough
424 * to keep NL locks on an rsb that they may lock again shortly; this can lead
425 * to excessive master lookups and removals if we don't delay the release.
427 * Searching for an rsb means looking through both the normal list and toss
428 * list. When found on the toss list the rsb is moved to the normal list with
429 * ref count of 1; when found on normal list the ref count is incremented.
432 static int find_rsb(struct dlm_ls
*ls
, char *name
, int namelen
,
433 unsigned int flags
, struct dlm_rsb
**r_ret
)
435 struct dlm_rsb
*r
= NULL
, *tmp
;
436 uint32_t hash
, bucket
;
439 if (namelen
> DLM_RESNAME_MAXLEN
)
442 if (dlm_no_directory(ls
))
446 hash
= jhash(name
, namelen
, 0);
447 bucket
= hash
& (ls
->ls_rsbtbl_size
- 1);
449 error
= search_rsb(ls
, name
, namelen
, bucket
, flags
, &r
);
453 if (error
== -EBADR
&& !(flags
& R_CREATE
))
456 /* the rsb was found but wasn't a master copy */
457 if (error
== -ENOTBLK
)
461 r
= create_rsb(ls
, name
, namelen
);
466 r
->res_bucket
= bucket
;
468 kref_init(&r
->res_ref
);
470 /* With no directory, the master can be set immediately */
471 if (dlm_no_directory(ls
)) {
472 int nodeid
= dlm_dir_nodeid(r
);
473 if (nodeid
== dlm_our_nodeid())
475 r
->res_nodeid
= nodeid
;
478 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
479 error
= _search_rsb(ls
, name
, namelen
, bucket
, 0, &tmp
);
481 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
486 list_add(&r
->res_hashchain
, &ls
->ls_rsbtbl
[bucket
].list
);
487 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
494 /* This is only called to add a reference when the code already holds
495 a valid reference to the rsb, so there's no need for locking. */
497 static inline void hold_rsb(struct dlm_rsb
*r
)
499 kref_get(&r
->res_ref
);
502 void dlm_hold_rsb(struct dlm_rsb
*r
)
507 static void toss_rsb(struct kref
*kref
)
509 struct dlm_rsb
*r
= container_of(kref
, struct dlm_rsb
, res_ref
);
510 struct dlm_ls
*ls
= r
->res_ls
;
512 DLM_ASSERT(list_empty(&r
->res_root_list
), dlm_print_rsb(r
););
513 kref_init(&r
->res_ref
);
514 list_move(&r
->res_hashchain
, &ls
->ls_rsbtbl
[r
->res_bucket
].toss
);
515 r
->res_toss_time
= jiffies
;
517 dlm_free_lvb(r
->res_lvbptr
);
518 r
->res_lvbptr
= NULL
;
522 /* When all references to the rsb are gone it's transfered to
523 the tossed list for later disposal. */
525 static void put_rsb(struct dlm_rsb
*r
)
527 struct dlm_ls
*ls
= r
->res_ls
;
528 uint32_t bucket
= r
->res_bucket
;
530 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
531 kref_put(&r
->res_ref
, toss_rsb
);
532 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
535 void dlm_put_rsb(struct dlm_rsb
*r
)
540 /* See comment for unhold_lkb */
542 static void unhold_rsb(struct dlm_rsb
*r
)
545 rv
= kref_put(&r
->res_ref
, toss_rsb
);
546 DLM_ASSERT(!rv
, dlm_dump_rsb(r
););
549 static void kill_rsb(struct kref
*kref
)
551 struct dlm_rsb
*r
= container_of(kref
, struct dlm_rsb
, res_ref
);
553 /* All work is done after the return from kref_put() so we
554 can release the write_lock before the remove and free. */
556 DLM_ASSERT(list_empty(&r
->res_lookup
), dlm_dump_rsb(r
););
557 DLM_ASSERT(list_empty(&r
->res_grantqueue
), dlm_dump_rsb(r
););
558 DLM_ASSERT(list_empty(&r
->res_convertqueue
), dlm_dump_rsb(r
););
559 DLM_ASSERT(list_empty(&r
->res_waitqueue
), dlm_dump_rsb(r
););
560 DLM_ASSERT(list_empty(&r
->res_root_list
), dlm_dump_rsb(r
););
561 DLM_ASSERT(list_empty(&r
->res_recover_list
), dlm_dump_rsb(r
););
564 /* Attaching/detaching lkb's from rsb's is for rsb reference counting.
565 The rsb must exist as long as any lkb's for it do. */
567 static void attach_lkb(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
570 lkb
->lkb_resource
= r
;
573 static void detach_lkb(struct dlm_lkb
*lkb
)
575 if (lkb
->lkb_resource
) {
576 put_rsb(lkb
->lkb_resource
);
577 lkb
->lkb_resource
= NULL
;
581 static int create_lkb(struct dlm_ls
*ls
, struct dlm_lkb
**lkb_ret
)
583 struct dlm_lkb
*lkb
, *tmp
;
587 lkb
= dlm_allocate_lkb(ls
);
591 lkb
->lkb_nodeid
= -1;
592 lkb
->lkb_grmode
= DLM_LOCK_IV
;
593 kref_init(&lkb
->lkb_ref
);
594 INIT_LIST_HEAD(&lkb
->lkb_ownqueue
);
595 INIT_LIST_HEAD(&lkb
->lkb_rsb_lookup
);
596 INIT_LIST_HEAD(&lkb
->lkb_time_list
);
597 INIT_LIST_HEAD(&lkb
->lkb_astqueue
);
599 get_random_bytes(&bucket
, sizeof(bucket
));
600 bucket
&= (ls
->ls_lkbtbl_size
- 1);
602 write_lock(&ls
->ls_lkbtbl
[bucket
].lock
);
604 /* counter can roll over so we must verify lkid is not in use */
607 lkid
= (bucket
<< 16) | ls
->ls_lkbtbl
[bucket
].counter
++;
609 list_for_each_entry(tmp
, &ls
->ls_lkbtbl
[bucket
].list
,
611 if (tmp
->lkb_id
!= lkid
)
619 list_add(&lkb
->lkb_idtbl_list
, &ls
->ls_lkbtbl
[bucket
].list
);
620 write_unlock(&ls
->ls_lkbtbl
[bucket
].lock
);
626 static struct dlm_lkb
*__find_lkb(struct dlm_ls
*ls
, uint32_t lkid
)
629 uint16_t bucket
= (lkid
>> 16);
631 list_for_each_entry(lkb
, &ls
->ls_lkbtbl
[bucket
].list
, lkb_idtbl_list
) {
632 if (lkb
->lkb_id
== lkid
)
638 static int find_lkb(struct dlm_ls
*ls
, uint32_t lkid
, struct dlm_lkb
**lkb_ret
)
641 uint16_t bucket
= (lkid
>> 16);
643 if (bucket
>= ls
->ls_lkbtbl_size
)
646 read_lock(&ls
->ls_lkbtbl
[bucket
].lock
);
647 lkb
= __find_lkb(ls
, lkid
);
649 kref_get(&lkb
->lkb_ref
);
650 read_unlock(&ls
->ls_lkbtbl
[bucket
].lock
);
653 return lkb
? 0 : -ENOENT
;
656 static void kill_lkb(struct kref
*kref
)
658 struct dlm_lkb
*lkb
= container_of(kref
, struct dlm_lkb
, lkb_ref
);
660 /* All work is done after the return from kref_put() so we
661 can release the write_lock before the detach_lkb */
663 DLM_ASSERT(!lkb
->lkb_status
, dlm_print_lkb(lkb
););
666 /* __put_lkb() is used when an lkb may not have an rsb attached to
667 it so we need to provide the lockspace explicitly */
669 static int __put_lkb(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
)
671 uint16_t bucket
= (lkb
->lkb_id
>> 16);
673 write_lock(&ls
->ls_lkbtbl
[bucket
].lock
);
674 if (kref_put(&lkb
->lkb_ref
, kill_lkb
)) {
675 list_del(&lkb
->lkb_idtbl_list
);
676 write_unlock(&ls
->ls_lkbtbl
[bucket
].lock
);
680 /* for local/process lkbs, lvbptr points to caller's lksb */
681 if (lkb
->lkb_lvbptr
&& is_master_copy(lkb
))
682 dlm_free_lvb(lkb
->lkb_lvbptr
);
686 write_unlock(&ls
->ls_lkbtbl
[bucket
].lock
);
691 int dlm_put_lkb(struct dlm_lkb
*lkb
)
695 DLM_ASSERT(lkb
->lkb_resource
, dlm_print_lkb(lkb
););
696 DLM_ASSERT(lkb
->lkb_resource
->res_ls
, dlm_print_lkb(lkb
););
698 ls
= lkb
->lkb_resource
->res_ls
;
699 return __put_lkb(ls
, lkb
);
702 /* This is only called to add a reference when the code already holds
703 a valid reference to the lkb, so there's no need for locking. */
705 static inline void hold_lkb(struct dlm_lkb
*lkb
)
707 kref_get(&lkb
->lkb_ref
);
710 /* This is called when we need to remove a reference and are certain
711 it's not the last ref. e.g. del_lkb is always called between a
712 find_lkb/put_lkb and is always the inverse of a previous add_lkb.
713 put_lkb would work fine, but would involve unnecessary locking */
715 static inline void unhold_lkb(struct dlm_lkb
*lkb
)
718 rv
= kref_put(&lkb
->lkb_ref
, kill_lkb
);
719 DLM_ASSERT(!rv
, dlm_print_lkb(lkb
););
722 static void lkb_add_ordered(struct list_head
*new, struct list_head
*head
,
725 struct dlm_lkb
*lkb
= NULL
;
727 list_for_each_entry(lkb
, head
, lkb_statequeue
)
728 if (lkb
->lkb_rqmode
< mode
)
731 __list_add(new, lkb
->lkb_statequeue
.prev
, &lkb
->lkb_statequeue
);
734 /* add/remove lkb to rsb's grant/convert/wait queue */
736 static void add_lkb(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int status
)
738 kref_get(&lkb
->lkb_ref
);
740 DLM_ASSERT(!lkb
->lkb_status
, dlm_print_lkb(lkb
););
742 lkb
->lkb_timestamp
= ktime_get();
744 lkb
->lkb_status
= status
;
747 case DLM_LKSTS_WAITING
:
748 if (lkb
->lkb_exflags
& DLM_LKF_HEADQUE
)
749 list_add(&lkb
->lkb_statequeue
, &r
->res_waitqueue
);
751 list_add_tail(&lkb
->lkb_statequeue
, &r
->res_waitqueue
);
753 case DLM_LKSTS_GRANTED
:
754 /* convention says granted locks kept in order of grmode */
755 lkb_add_ordered(&lkb
->lkb_statequeue
, &r
->res_grantqueue
,
758 case DLM_LKSTS_CONVERT
:
759 if (lkb
->lkb_exflags
& DLM_LKF_HEADQUE
)
760 list_add(&lkb
->lkb_statequeue
, &r
->res_convertqueue
);
762 list_add_tail(&lkb
->lkb_statequeue
,
763 &r
->res_convertqueue
);
766 DLM_ASSERT(0, dlm_print_lkb(lkb
); printk("sts=%d\n", status
););
770 static void del_lkb(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
773 list_del(&lkb
->lkb_statequeue
);
777 static void move_lkb(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int sts
)
781 add_lkb(r
, lkb
, sts
);
785 static int msg_reply_type(int mstype
)
788 case DLM_MSG_REQUEST
:
789 return DLM_MSG_REQUEST_REPLY
;
790 case DLM_MSG_CONVERT
:
791 return DLM_MSG_CONVERT_REPLY
;
793 return DLM_MSG_UNLOCK_REPLY
;
795 return DLM_MSG_CANCEL_REPLY
;
797 return DLM_MSG_LOOKUP_REPLY
;
802 /* add/remove lkb from global waiters list of lkb's waiting for
803 a reply from a remote node */
805 static int add_to_waiters(struct dlm_lkb
*lkb
, int mstype
)
807 struct dlm_ls
*ls
= lkb
->lkb_resource
->res_ls
;
810 mutex_lock(&ls
->ls_waiters_mutex
);
812 if (is_overlap_unlock(lkb
) ||
813 (is_overlap_cancel(lkb
) && (mstype
== DLM_MSG_CANCEL
))) {
818 if (lkb
->lkb_wait_type
|| is_overlap_cancel(lkb
)) {
821 lkb
->lkb_flags
|= DLM_IFL_OVERLAP_UNLOCK
;
824 lkb
->lkb_flags
|= DLM_IFL_OVERLAP_CANCEL
;
830 lkb
->lkb_wait_count
++;
833 log_debug(ls
, "addwait %x cur %d overlap %d count %d f %x",
834 lkb
->lkb_id
, lkb
->lkb_wait_type
, mstype
,
835 lkb
->lkb_wait_count
, lkb
->lkb_flags
);
839 DLM_ASSERT(!lkb
->lkb_wait_count
,
841 printk("wait_count %d\n", lkb
->lkb_wait_count
););
843 lkb
->lkb_wait_count
++;
844 lkb
->lkb_wait_type
= mstype
;
846 list_add(&lkb
->lkb_wait_reply
, &ls
->ls_waiters
);
849 log_error(ls
, "addwait error %x %d flags %x %d %d %s",
850 lkb
->lkb_id
, error
, lkb
->lkb_flags
, mstype
,
851 lkb
->lkb_wait_type
, lkb
->lkb_resource
->res_name
);
852 mutex_unlock(&ls
->ls_waiters_mutex
);
856 /* We clear the RESEND flag because we might be taking an lkb off the waiters
857 list as part of process_requestqueue (e.g. a lookup that has an optimized
858 request reply on the requestqueue) between dlm_recover_waiters_pre() which
859 set RESEND and dlm_recover_waiters_post() */
861 static int _remove_from_waiters(struct dlm_lkb
*lkb
, int mstype
,
862 struct dlm_message
*ms
)
864 struct dlm_ls
*ls
= lkb
->lkb_resource
->res_ls
;
865 int overlap_done
= 0;
867 if (is_overlap_unlock(lkb
) && (mstype
== DLM_MSG_UNLOCK_REPLY
)) {
868 log_debug(ls
, "remwait %x unlock_reply overlap", lkb
->lkb_id
);
869 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_UNLOCK
;
874 if (is_overlap_cancel(lkb
) && (mstype
== DLM_MSG_CANCEL_REPLY
)) {
875 log_debug(ls
, "remwait %x cancel_reply overlap", lkb
->lkb_id
);
876 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_CANCEL
;
881 /* Cancel state was preemptively cleared by a successful convert,
882 see next comment, nothing to do. */
884 if ((mstype
== DLM_MSG_CANCEL_REPLY
) &&
885 (lkb
->lkb_wait_type
!= DLM_MSG_CANCEL
)) {
886 log_debug(ls
, "remwait %x cancel_reply wait_type %d",
887 lkb
->lkb_id
, lkb
->lkb_wait_type
);
891 /* Remove for the convert reply, and premptively remove for the
892 cancel reply. A convert has been granted while there's still
893 an outstanding cancel on it (the cancel is moot and the result
894 in the cancel reply should be 0). We preempt the cancel reply
895 because the app gets the convert result and then can follow up
896 with another op, like convert. This subsequent op would see the
897 lingering state of the cancel and fail with -EBUSY. */
899 if ((mstype
== DLM_MSG_CONVERT_REPLY
) &&
900 (lkb
->lkb_wait_type
== DLM_MSG_CONVERT
) &&
901 is_overlap_cancel(lkb
) && ms
&& !ms
->m_result
) {
902 log_debug(ls
, "remwait %x convert_reply zap overlap_cancel",
904 lkb
->lkb_wait_type
= 0;
905 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_CANCEL
;
906 lkb
->lkb_wait_count
--;
910 /* N.B. type of reply may not always correspond to type of original
911 msg due to lookup->request optimization, verify others? */
913 if (lkb
->lkb_wait_type
) {
914 lkb
->lkb_wait_type
= 0;
918 log_error(ls
, "remwait error %x reply %d flags %x no wait_type",
919 lkb
->lkb_id
, mstype
, lkb
->lkb_flags
);
923 /* the force-unlock/cancel has completed and we haven't recvd a reply
924 to the op that was in progress prior to the unlock/cancel; we
925 give up on any reply to the earlier op. FIXME: not sure when/how
928 if (overlap_done
&& lkb
->lkb_wait_type
) {
929 log_error(ls
, "remwait error %x reply %d wait_type %d overlap",
930 lkb
->lkb_id
, mstype
, lkb
->lkb_wait_type
);
931 lkb
->lkb_wait_count
--;
932 lkb
->lkb_wait_type
= 0;
935 DLM_ASSERT(lkb
->lkb_wait_count
, dlm_print_lkb(lkb
););
937 lkb
->lkb_flags
&= ~DLM_IFL_RESEND
;
938 lkb
->lkb_wait_count
--;
939 if (!lkb
->lkb_wait_count
)
940 list_del_init(&lkb
->lkb_wait_reply
);
945 static int remove_from_waiters(struct dlm_lkb
*lkb
, int mstype
)
947 struct dlm_ls
*ls
= lkb
->lkb_resource
->res_ls
;
950 mutex_lock(&ls
->ls_waiters_mutex
);
951 error
= _remove_from_waiters(lkb
, mstype
, NULL
);
952 mutex_unlock(&ls
->ls_waiters_mutex
);
956 /* Handles situations where we might be processing a "fake" or "stub" reply in
957 which we can't try to take waiters_mutex again. */
959 static int remove_from_waiters_ms(struct dlm_lkb
*lkb
, struct dlm_message
*ms
)
961 struct dlm_ls
*ls
= lkb
->lkb_resource
->res_ls
;
964 if (ms
!= &ls
->ls_stub_ms
)
965 mutex_lock(&ls
->ls_waiters_mutex
);
966 error
= _remove_from_waiters(lkb
, ms
->m_type
, ms
);
967 if (ms
!= &ls
->ls_stub_ms
)
968 mutex_unlock(&ls
->ls_waiters_mutex
);
972 static void dir_remove(struct dlm_rsb
*r
)
976 if (dlm_no_directory(r
->res_ls
))
979 to_nodeid
= dlm_dir_nodeid(r
);
980 if (to_nodeid
!= dlm_our_nodeid())
983 dlm_dir_remove_entry(r
->res_ls
, to_nodeid
,
984 r
->res_name
, r
->res_length
);
987 /* FIXME: shouldn't this be able to exit as soon as one non-due rsb is
988 found since they are in order of newest to oldest? */
990 static int shrink_bucket(struct dlm_ls
*ls
, int b
)
993 int count
= 0, found
;
997 spin_lock(&ls
->ls_rsbtbl
[b
].lock
);
998 list_for_each_entry_reverse(r
, &ls
->ls_rsbtbl
[b
].toss
,
1000 if (!time_after_eq(jiffies
, r
->res_toss_time
+
1001 dlm_config
.ci_toss_secs
* HZ
))
1008 spin_unlock(&ls
->ls_rsbtbl
[b
].lock
);
1012 if (kref_put(&r
->res_ref
, kill_rsb
)) {
1013 list_del(&r
->res_hashchain
);
1014 spin_unlock(&ls
->ls_rsbtbl
[b
].lock
);
1021 spin_unlock(&ls
->ls_rsbtbl
[b
].lock
);
1022 log_error(ls
, "tossed rsb in use %s", r
->res_name
);
1029 void dlm_scan_rsbs(struct dlm_ls
*ls
)
1033 for (i
= 0; i
< ls
->ls_rsbtbl_size
; i
++) {
1034 shrink_bucket(ls
, i
);
1035 if (dlm_locking_stopped(ls
))
1041 static void add_timeout(struct dlm_lkb
*lkb
)
1043 struct dlm_ls
*ls
= lkb
->lkb_resource
->res_ls
;
1045 if (is_master_copy(lkb
))
1048 if (test_bit(LSFL_TIMEWARN
, &ls
->ls_flags
) &&
1049 !(lkb
->lkb_exflags
& DLM_LKF_NODLCKWT
)) {
1050 lkb
->lkb_flags
|= DLM_IFL_WATCH_TIMEWARN
;
1053 if (lkb
->lkb_exflags
& DLM_LKF_TIMEOUT
)
1058 DLM_ASSERT(list_empty(&lkb
->lkb_time_list
), dlm_print_lkb(lkb
););
1059 mutex_lock(&ls
->ls_timeout_mutex
);
1061 list_add_tail(&lkb
->lkb_time_list
, &ls
->ls_timeout
);
1062 mutex_unlock(&ls
->ls_timeout_mutex
);
1065 static void del_timeout(struct dlm_lkb
*lkb
)
1067 struct dlm_ls
*ls
= lkb
->lkb_resource
->res_ls
;
1069 mutex_lock(&ls
->ls_timeout_mutex
);
1070 if (!list_empty(&lkb
->lkb_time_list
)) {
1071 list_del_init(&lkb
->lkb_time_list
);
1074 mutex_unlock(&ls
->ls_timeout_mutex
);
1077 /* FIXME: is it safe to look at lkb_exflags, lkb_flags, lkb_timestamp, and
1078 lkb_lksb_timeout without lock_rsb? Note: we can't lock timeout_mutex
1079 and then lock rsb because of lock ordering in add_timeout. We may need
1080 to specify some special timeout-related bits in the lkb that are just to
1081 be accessed under the timeout_mutex. */
1083 void dlm_scan_timeout(struct dlm_ls
*ls
)
1086 struct dlm_lkb
*lkb
;
1087 int do_cancel
, do_warn
;
1091 if (dlm_locking_stopped(ls
))
1096 mutex_lock(&ls
->ls_timeout_mutex
);
1097 list_for_each_entry(lkb
, &ls
->ls_timeout
, lkb_time_list
) {
1099 wait_us
= ktime_to_us(ktime_sub(ktime_get(),
1100 lkb
->lkb_timestamp
));
1102 if ((lkb
->lkb_exflags
& DLM_LKF_TIMEOUT
) &&
1103 wait_us
>= (lkb
->lkb_timeout_cs
* 10000))
1106 if ((lkb
->lkb_flags
& DLM_IFL_WATCH_TIMEWARN
) &&
1107 wait_us
>= dlm_config
.ci_timewarn_cs
* 10000)
1110 if (!do_cancel
&& !do_warn
)
1115 mutex_unlock(&ls
->ls_timeout_mutex
);
1117 if (!do_cancel
&& !do_warn
)
1120 r
= lkb
->lkb_resource
;
1125 /* clear flag so we only warn once */
1126 lkb
->lkb_flags
&= ~DLM_IFL_WATCH_TIMEWARN
;
1127 if (!(lkb
->lkb_exflags
& DLM_LKF_TIMEOUT
))
1129 dlm_timeout_warn(lkb
);
1133 log_debug(ls
, "timeout cancel %x node %d %s",
1134 lkb
->lkb_id
, lkb
->lkb_nodeid
, r
->res_name
);
1135 lkb
->lkb_flags
&= ~DLM_IFL_WATCH_TIMEWARN
;
1136 lkb
->lkb_flags
|= DLM_IFL_TIMEOUT_CANCEL
;
1138 _cancel_lock(r
, lkb
);
1147 /* This is only called by dlm_recoverd, and we rely on dlm_ls_stop() stopping
1148 dlm_recoverd before checking/setting ls_recover_begin. */
1150 void dlm_adjust_timeouts(struct dlm_ls
*ls
)
1152 struct dlm_lkb
*lkb
;
1153 u64 adj_us
= jiffies_to_usecs(jiffies
- ls
->ls_recover_begin
);
1155 ls
->ls_recover_begin
= 0;
1156 mutex_lock(&ls
->ls_timeout_mutex
);
1157 list_for_each_entry(lkb
, &ls
->ls_timeout
, lkb_time_list
)
1158 lkb
->lkb_timestamp
= ktime_add_us(lkb
->lkb_timestamp
, adj_us
);
1159 mutex_unlock(&ls
->ls_timeout_mutex
);
1162 /* lkb is master or local copy */
1164 static void set_lvb_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1166 int b
, len
= r
->res_ls
->ls_lvblen
;
1168 /* b=1 lvb returned to caller
1169 b=0 lvb written to rsb or invalidated
1172 b
= dlm_lvb_operations
[lkb
->lkb_grmode
+ 1][lkb
->lkb_rqmode
+ 1];
1175 if (!lkb
->lkb_lvbptr
)
1178 if (!(lkb
->lkb_exflags
& DLM_LKF_VALBLK
))
1184 memcpy(lkb
->lkb_lvbptr
, r
->res_lvbptr
, len
);
1185 lkb
->lkb_lvbseq
= r
->res_lvbseq
;
1187 } else if (b
== 0) {
1188 if (lkb
->lkb_exflags
& DLM_LKF_IVVALBLK
) {
1189 rsb_set_flag(r
, RSB_VALNOTVALID
);
1193 if (!lkb
->lkb_lvbptr
)
1196 if (!(lkb
->lkb_exflags
& DLM_LKF_VALBLK
))
1200 r
->res_lvbptr
= dlm_allocate_lvb(r
->res_ls
);
1205 memcpy(r
->res_lvbptr
, lkb
->lkb_lvbptr
, len
);
1207 lkb
->lkb_lvbseq
= r
->res_lvbseq
;
1208 rsb_clear_flag(r
, RSB_VALNOTVALID
);
1211 if (rsb_flag(r
, RSB_VALNOTVALID
))
1212 lkb
->lkb_sbflags
|= DLM_SBF_VALNOTVALID
;
1215 static void set_lvb_unlock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1217 if (lkb
->lkb_grmode
< DLM_LOCK_PW
)
1220 if (lkb
->lkb_exflags
& DLM_LKF_IVVALBLK
) {
1221 rsb_set_flag(r
, RSB_VALNOTVALID
);
1225 if (!lkb
->lkb_lvbptr
)
1228 if (!(lkb
->lkb_exflags
& DLM_LKF_VALBLK
))
1232 r
->res_lvbptr
= dlm_allocate_lvb(r
->res_ls
);
1237 memcpy(r
->res_lvbptr
, lkb
->lkb_lvbptr
, r
->res_ls
->ls_lvblen
);
1239 rsb_clear_flag(r
, RSB_VALNOTVALID
);
1242 /* lkb is process copy (pc) */
1244 static void set_lvb_lock_pc(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
1245 struct dlm_message
*ms
)
1249 if (!lkb
->lkb_lvbptr
)
1252 if (!(lkb
->lkb_exflags
& DLM_LKF_VALBLK
))
1255 b
= dlm_lvb_operations
[lkb
->lkb_grmode
+ 1][lkb
->lkb_rqmode
+ 1];
1257 int len
= receive_extralen(ms
);
1258 if (len
> DLM_RESNAME_MAXLEN
)
1259 len
= DLM_RESNAME_MAXLEN
;
1260 memcpy(lkb
->lkb_lvbptr
, ms
->m_extra
, len
);
1261 lkb
->lkb_lvbseq
= ms
->m_lvbseq
;
1265 /* Manipulate lkb's on rsb's convert/granted/waiting queues
1266 remove_lock -- used for unlock, removes lkb from granted
1267 revert_lock -- used for cancel, moves lkb from convert to granted
1268 grant_lock -- used for request and convert, adds lkb to granted or
1269 moves lkb from convert or waiting to granted
1271 Each of these is used for master or local copy lkb's. There is
1272 also a _pc() variation used to make the corresponding change on
1273 a process copy (pc) lkb. */
1275 static void _remove_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1278 lkb
->lkb_grmode
= DLM_LOCK_IV
;
1279 /* this unhold undoes the original ref from create_lkb()
1280 so this leads to the lkb being freed */
1284 static void remove_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1286 set_lvb_unlock(r
, lkb
);
1287 _remove_lock(r
, lkb
);
1290 static void remove_lock_pc(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1292 _remove_lock(r
, lkb
);
1295 /* returns: 0 did nothing
1296 1 moved lock to granted
1299 static int revert_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1303 lkb
->lkb_rqmode
= DLM_LOCK_IV
;
1305 switch (lkb
->lkb_status
) {
1306 case DLM_LKSTS_GRANTED
:
1308 case DLM_LKSTS_CONVERT
:
1309 move_lkb(r
, lkb
, DLM_LKSTS_GRANTED
);
1312 case DLM_LKSTS_WAITING
:
1314 lkb
->lkb_grmode
= DLM_LOCK_IV
;
1315 /* this unhold undoes the original ref from create_lkb()
1316 so this leads to the lkb being freed */
1321 log_print("invalid status for revert %d", lkb
->lkb_status
);
1326 static int revert_lock_pc(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1328 return revert_lock(r
, lkb
);
1331 static void _grant_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1333 if (lkb
->lkb_grmode
!= lkb
->lkb_rqmode
) {
1334 lkb
->lkb_grmode
= lkb
->lkb_rqmode
;
1335 if (lkb
->lkb_status
)
1336 move_lkb(r
, lkb
, DLM_LKSTS_GRANTED
);
1338 add_lkb(r
, lkb
, DLM_LKSTS_GRANTED
);
1341 lkb
->lkb_rqmode
= DLM_LOCK_IV
;
1344 static void grant_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1346 set_lvb_lock(r
, lkb
);
1347 _grant_lock(r
, lkb
);
1348 lkb
->lkb_highbast
= 0;
1351 static void grant_lock_pc(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
1352 struct dlm_message
*ms
)
1354 set_lvb_lock_pc(r
, lkb
, ms
);
1355 _grant_lock(r
, lkb
);
1358 /* called by grant_pending_locks() which means an async grant message must
1359 be sent to the requesting node in addition to granting the lock if the
1360 lkb belongs to a remote node. */
1362 static void grant_lock_pending(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1365 if (is_master_copy(lkb
))
1368 queue_cast(r
, lkb
, 0);
1371 /* The special CONVDEADLK, ALTPR and ALTCW flags allow the master to
1372 change the granted/requested modes. We're munging things accordingly in
1374 CONVDEADLK: our grmode may have been forced down to NL to resolve a
1376 ALTPR/ALTCW: our rqmode may have been changed to PR or CW to become
1377 compatible with other granted locks */
1379 static void munge_demoted(struct dlm_lkb
*lkb
, struct dlm_message
*ms
)
1381 if (ms
->m_type
!= DLM_MSG_CONVERT_REPLY
) {
1382 log_print("munge_demoted %x invalid reply type %d",
1383 lkb
->lkb_id
, ms
->m_type
);
1387 if (lkb
->lkb_rqmode
== DLM_LOCK_IV
|| lkb
->lkb_grmode
== DLM_LOCK_IV
) {
1388 log_print("munge_demoted %x invalid modes gr %d rq %d",
1389 lkb
->lkb_id
, lkb
->lkb_grmode
, lkb
->lkb_rqmode
);
1393 lkb
->lkb_grmode
= DLM_LOCK_NL
;
1396 static void munge_altmode(struct dlm_lkb
*lkb
, struct dlm_message
*ms
)
1398 if (ms
->m_type
!= DLM_MSG_REQUEST_REPLY
&&
1399 ms
->m_type
!= DLM_MSG_GRANT
) {
1400 log_print("munge_altmode %x invalid reply type %d",
1401 lkb
->lkb_id
, ms
->m_type
);
1405 if (lkb
->lkb_exflags
& DLM_LKF_ALTPR
)
1406 lkb
->lkb_rqmode
= DLM_LOCK_PR
;
1407 else if (lkb
->lkb_exflags
& DLM_LKF_ALTCW
)
1408 lkb
->lkb_rqmode
= DLM_LOCK_CW
;
1410 log_print("munge_altmode invalid exflags %x", lkb
->lkb_exflags
);
1415 static inline int first_in_list(struct dlm_lkb
*lkb
, struct list_head
*head
)
1417 struct dlm_lkb
*first
= list_entry(head
->next
, struct dlm_lkb
,
1419 if (lkb
->lkb_id
== first
->lkb_id
)
1425 /* Check if the given lkb conflicts with another lkb on the queue. */
1427 static int queue_conflict(struct list_head
*head
, struct dlm_lkb
*lkb
)
1429 struct dlm_lkb
*this;
1431 list_for_each_entry(this, head
, lkb_statequeue
) {
1434 if (!modes_compat(this, lkb
))
1441 * "A conversion deadlock arises with a pair of lock requests in the converting
1442 * queue for one resource. The granted mode of each lock blocks the requested
1443 * mode of the other lock."
1445 * Part 2: if the granted mode of lkb is preventing an earlier lkb in the
1446 * convert queue from being granted, then deadlk/demote lkb.
1449 * Granted Queue: empty
1450 * Convert Queue: NL->EX (first lock)
1451 * PR->EX (second lock)
1453 * The first lock can't be granted because of the granted mode of the second
1454 * lock and the second lock can't be granted because it's not first in the
1455 * list. We either cancel lkb's conversion (PR->EX) and return EDEADLK, or we
1456 * demote the granted mode of lkb (from PR to NL) if it has the CONVDEADLK
1457 * flag set and return DEMOTED in the lksb flags.
1459 * Originally, this function detected conv-deadlk in a more limited scope:
1460 * - if !modes_compat(lkb1, lkb2) && !modes_compat(lkb2, lkb1), or
1461 * - if lkb1 was the first entry in the queue (not just earlier), and was
1462 * blocked by the granted mode of lkb2, and there was nothing on the
1463 * granted queue preventing lkb1 from being granted immediately, i.e.
1464 * lkb2 was the only thing preventing lkb1 from being granted.
1466 * That second condition meant we'd only say there was conv-deadlk if
1467 * resolving it (by demotion) would lead to the first lock on the convert
1468 * queue being granted right away. It allowed conversion deadlocks to exist
1469 * between locks on the convert queue while they couldn't be granted anyway.
1471 * Now, we detect and take action on conversion deadlocks immediately when
1472 * they're created, even if they may not be immediately consequential. If
1473 * lkb1 exists anywhere in the convert queue and lkb2 comes in with a granted
1474 * mode that would prevent lkb1's conversion from being granted, we do a
1475 * deadlk/demote on lkb2 right away and don't let it onto the convert queue.
1476 * I think this means that the lkb_is_ahead condition below should always
1477 * be zero, i.e. there will never be conv-deadlk between two locks that are
1478 * both already on the convert queue.
1481 static int conversion_deadlock_detect(struct dlm_rsb
*r
, struct dlm_lkb
*lkb2
)
1483 struct dlm_lkb
*lkb1
;
1484 int lkb_is_ahead
= 0;
1486 list_for_each_entry(lkb1
, &r
->res_convertqueue
, lkb_statequeue
) {
1492 if (!lkb_is_ahead
) {
1493 if (!modes_compat(lkb2
, lkb1
))
1496 if (!modes_compat(lkb2
, lkb1
) &&
1497 !modes_compat(lkb1
, lkb2
))
1505 * Return 1 if the lock can be granted, 0 otherwise.
1506 * Also detect and resolve conversion deadlocks.
1508 * lkb is the lock to be granted
1510 * now is 1 if the function is being called in the context of the
1511 * immediate request, it is 0 if called later, after the lock has been
1514 * References are from chapter 6 of "VAXcluster Principles" by Roy Davis
1517 static int _can_be_granted(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int now
)
1519 int8_t conv
= (lkb
->lkb_grmode
!= DLM_LOCK_IV
);
1522 * 6-10: Version 5.4 introduced an option to address the phenomenon of
1523 * a new request for a NL mode lock being blocked.
1525 * 6-11: If the optional EXPEDITE flag is used with the new NL mode
1526 * request, then it would be granted. In essence, the use of this flag
1527 * tells the Lock Manager to expedite theis request by not considering
1528 * what may be in the CONVERTING or WAITING queues... As of this
1529 * writing, the EXPEDITE flag can be used only with new requests for NL
1530 * mode locks. This flag is not valid for conversion requests.
1532 * A shortcut. Earlier checks return an error if EXPEDITE is used in a
1533 * conversion or used with a non-NL requested mode. We also know an
1534 * EXPEDITE request is always granted immediately, so now must always
1535 * be 1. The full condition to grant an expedite request: (now &&
1536 * !conv && lkb->rqmode == DLM_LOCK_NL && (flags & EXPEDITE)) can
1537 * therefore be shortened to just checking the flag.
1540 if (lkb
->lkb_exflags
& DLM_LKF_EXPEDITE
)
1544 * A shortcut. Without this, !queue_conflict(grantqueue, lkb) would be
1545 * added to the remaining conditions.
1548 if (queue_conflict(&r
->res_grantqueue
, lkb
))
1552 * 6-3: By default, a conversion request is immediately granted if the
1553 * requested mode is compatible with the modes of all other granted
1557 if (queue_conflict(&r
->res_convertqueue
, lkb
))
1561 * 6-5: But the default algorithm for deciding whether to grant or
1562 * queue conversion requests does not by itself guarantee that such
1563 * requests are serviced on a "first come first serve" basis. This, in
1564 * turn, can lead to a phenomenon known as "indefinate postponement".
1566 * 6-7: This issue is dealt with by using the optional QUECVT flag with
1567 * the system service employed to request a lock conversion. This flag
1568 * forces certain conversion requests to be queued, even if they are
1569 * compatible with the granted modes of other locks on the same
1570 * resource. Thus, the use of this flag results in conversion requests
1571 * being ordered on a "first come first servce" basis.
1573 * DCT: This condition is all about new conversions being able to occur
1574 * "in place" while the lock remains on the granted queue (assuming
1575 * nothing else conflicts.) IOW if QUECVT isn't set, a conversion
1576 * doesn't _have_ to go onto the convert queue where it's processed in
1577 * order. The "now" variable is necessary to distinguish converts
1578 * being received and processed for the first time now, because once a
1579 * convert is moved to the conversion queue the condition below applies
1580 * requiring fifo granting.
1583 if (now
&& conv
&& !(lkb
->lkb_exflags
& DLM_LKF_QUECVT
))
1587 * The NOORDER flag is set to avoid the standard vms rules on grant
1591 if (lkb
->lkb_exflags
& DLM_LKF_NOORDER
)
1595 * 6-3: Once in that queue [CONVERTING], a conversion request cannot be
1596 * granted until all other conversion requests ahead of it are granted
1600 if (!now
&& conv
&& first_in_list(lkb
, &r
->res_convertqueue
))
1604 * 6-4: By default, a new request is immediately granted only if all
1605 * three of the following conditions are satisfied when the request is
1607 * - The queue of ungranted conversion requests for the resource is
1609 * - The queue of ungranted new requests for the resource is empty.
1610 * - The mode of the new request is compatible with the most
1611 * restrictive mode of all granted locks on the resource.
1614 if (now
&& !conv
&& list_empty(&r
->res_convertqueue
) &&
1615 list_empty(&r
->res_waitqueue
))
1619 * 6-4: Once a lock request is in the queue of ungranted new requests,
1620 * it cannot be granted until the queue of ungranted conversion
1621 * requests is empty, all ungranted new requests ahead of it are
1622 * granted and/or canceled, and it is compatible with the granted mode
1623 * of the most restrictive lock granted on the resource.
1626 if (!now
&& !conv
&& list_empty(&r
->res_convertqueue
) &&
1627 first_in_list(lkb
, &r
->res_waitqueue
))
1633 static int can_be_granted(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int now
,
1637 int8_t alt
= 0, rqmode
= lkb
->lkb_rqmode
;
1638 int8_t is_convert
= (lkb
->lkb_grmode
!= DLM_LOCK_IV
);
1643 rv
= _can_be_granted(r
, lkb
, now
);
1648 * The CONVDEADLK flag is non-standard and tells the dlm to resolve
1649 * conversion deadlocks by demoting grmode to NL, otherwise the dlm
1650 * cancels one of the locks.
1653 if (is_convert
&& can_be_queued(lkb
) &&
1654 conversion_deadlock_detect(r
, lkb
)) {
1655 if (lkb
->lkb_exflags
& DLM_LKF_CONVDEADLK
) {
1656 lkb
->lkb_grmode
= DLM_LOCK_NL
;
1657 lkb
->lkb_sbflags
|= DLM_SBF_DEMOTED
;
1658 } else if (!(lkb
->lkb_exflags
& DLM_LKF_NODLCKWT
)) {
1662 log_print("can_be_granted deadlock %x now %d",
1671 * The ALTPR and ALTCW flags are non-standard and tell the dlm to try
1672 * to grant a request in a mode other than the normal rqmode. It's a
1673 * simple way to provide a big optimization to applications that can
1677 if (rqmode
!= DLM_LOCK_PR
&& (lkb
->lkb_exflags
& DLM_LKF_ALTPR
))
1679 else if (rqmode
!= DLM_LOCK_CW
&& (lkb
->lkb_exflags
& DLM_LKF_ALTCW
))
1683 lkb
->lkb_rqmode
= alt
;
1684 rv
= _can_be_granted(r
, lkb
, now
);
1686 lkb
->lkb_sbflags
|= DLM_SBF_ALTMODE
;
1688 lkb
->lkb_rqmode
= rqmode
;
1694 /* FIXME: I don't think that can_be_granted() can/will demote or find deadlock
1695 for locks pending on the convert list. Once verified (watch for these
1696 log_prints), we should be able to just call _can_be_granted() and not
1697 bother with the demote/deadlk cases here (and there's no easy way to deal
1698 with a deadlk here, we'd have to generate something like grant_lock with
1699 the deadlk error.) */
1701 /* Returns the highest requested mode of all blocked conversions; sets
1702 cw if there's a blocked conversion to DLM_LOCK_CW. */
1704 static int grant_pending_convert(struct dlm_rsb
*r
, int high
, int *cw
)
1706 struct dlm_lkb
*lkb
, *s
;
1707 int hi
, demoted
, quit
, grant_restart
, demote_restart
;
1716 list_for_each_entry_safe(lkb
, s
, &r
->res_convertqueue
, lkb_statequeue
) {
1717 demoted
= is_demoted(lkb
);
1720 if (can_be_granted(r
, lkb
, 0, &deadlk
)) {
1721 grant_lock_pending(r
, lkb
);
1726 if (!demoted
&& is_demoted(lkb
)) {
1727 log_print("WARN: pending demoted %x node %d %s",
1728 lkb
->lkb_id
, lkb
->lkb_nodeid
, r
->res_name
);
1734 log_print("WARN: pending deadlock %x node %d %s",
1735 lkb
->lkb_id
, lkb
->lkb_nodeid
, r
->res_name
);
1740 hi
= max_t(int, lkb
->lkb_rqmode
, hi
);
1742 if (cw
&& lkb
->lkb_rqmode
== DLM_LOCK_CW
)
1748 if (demote_restart
&& !quit
) {
1753 return max_t(int, high
, hi
);
1756 static int grant_pending_wait(struct dlm_rsb
*r
, int high
, int *cw
)
1758 struct dlm_lkb
*lkb
, *s
;
1760 list_for_each_entry_safe(lkb
, s
, &r
->res_waitqueue
, lkb_statequeue
) {
1761 if (can_be_granted(r
, lkb
, 0, NULL
))
1762 grant_lock_pending(r
, lkb
);
1764 high
= max_t(int, lkb
->lkb_rqmode
, high
);
1765 if (lkb
->lkb_rqmode
== DLM_LOCK_CW
)
1773 /* cw of 1 means there's a lock with a rqmode of DLM_LOCK_CW that's blocked
1774 on either the convert or waiting queue.
1775 high is the largest rqmode of all locks blocked on the convert or
1778 static int lock_requires_bast(struct dlm_lkb
*gr
, int high
, int cw
)
1780 if (gr
->lkb_grmode
== DLM_LOCK_PR
&& cw
) {
1781 if (gr
->lkb_highbast
< DLM_LOCK_EX
)
1786 if (gr
->lkb_highbast
< high
&&
1787 !__dlm_compat_matrix
[gr
->lkb_grmode
+1][high
+1])
1792 static void grant_pending_locks(struct dlm_rsb
*r
)
1794 struct dlm_lkb
*lkb
, *s
;
1795 int high
= DLM_LOCK_IV
;
1798 DLM_ASSERT(is_master(r
), dlm_dump_rsb(r
););
1800 high
= grant_pending_convert(r
, high
, &cw
);
1801 high
= grant_pending_wait(r
, high
, &cw
);
1803 if (high
== DLM_LOCK_IV
)
1807 * If there are locks left on the wait/convert queue then send blocking
1808 * ASTs to granted locks based on the largest requested mode (high)
1812 list_for_each_entry_safe(lkb
, s
, &r
->res_grantqueue
, lkb_statequeue
) {
1813 if (lkb
->lkb_bastfn
&& lock_requires_bast(lkb
, high
, cw
)) {
1814 if (cw
&& high
== DLM_LOCK_PR
&&
1815 lkb
->lkb_grmode
== DLM_LOCK_PR
)
1816 queue_bast(r
, lkb
, DLM_LOCK_CW
);
1818 queue_bast(r
, lkb
, high
);
1819 lkb
->lkb_highbast
= high
;
1824 static int modes_require_bast(struct dlm_lkb
*gr
, struct dlm_lkb
*rq
)
1826 if ((gr
->lkb_grmode
== DLM_LOCK_PR
&& rq
->lkb_rqmode
== DLM_LOCK_CW
) ||
1827 (gr
->lkb_grmode
== DLM_LOCK_CW
&& rq
->lkb_rqmode
== DLM_LOCK_PR
)) {
1828 if (gr
->lkb_highbast
< DLM_LOCK_EX
)
1833 if (gr
->lkb_highbast
< rq
->lkb_rqmode
&& !modes_compat(gr
, rq
))
1838 static void send_bast_queue(struct dlm_rsb
*r
, struct list_head
*head
,
1839 struct dlm_lkb
*lkb
)
1843 list_for_each_entry(gr
, head
, lkb_statequeue
) {
1844 /* skip self when sending basts to convertqueue */
1847 if (gr
->lkb_bastfn
&& modes_require_bast(gr
, lkb
)) {
1848 queue_bast(r
, gr
, lkb
->lkb_rqmode
);
1849 gr
->lkb_highbast
= lkb
->lkb_rqmode
;
1854 static void send_blocking_asts(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1856 send_bast_queue(r
, &r
->res_grantqueue
, lkb
);
1859 static void send_blocking_asts_all(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1861 send_bast_queue(r
, &r
->res_grantqueue
, lkb
);
1862 send_bast_queue(r
, &r
->res_convertqueue
, lkb
);
1865 /* set_master(r, lkb) -- set the master nodeid of a resource
1867 The purpose of this function is to set the nodeid field in the given
1868 lkb using the nodeid field in the given rsb. If the rsb's nodeid is
1869 known, it can just be copied to the lkb and the function will return
1870 0. If the rsb's nodeid is _not_ known, it needs to be looked up
1871 before it can be copied to the lkb.
1873 When the rsb nodeid is being looked up remotely, the initial lkb
1874 causing the lookup is kept on the ls_waiters list waiting for the
1875 lookup reply. Other lkb's waiting for the same rsb lookup are kept
1876 on the rsb's res_lookup list until the master is verified.
1879 0: nodeid is set in rsb/lkb and the caller should go ahead and use it
1880 1: the rsb master is not available and the lkb has been placed on
1884 static int set_master(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
1886 struct dlm_ls
*ls
= r
->res_ls
;
1887 int i
, error
, dir_nodeid
, ret_nodeid
, our_nodeid
= dlm_our_nodeid();
1889 if (rsb_flag(r
, RSB_MASTER_UNCERTAIN
)) {
1890 rsb_clear_flag(r
, RSB_MASTER_UNCERTAIN
);
1891 r
->res_first_lkid
= lkb
->lkb_id
;
1892 lkb
->lkb_nodeid
= r
->res_nodeid
;
1896 if (r
->res_first_lkid
&& r
->res_first_lkid
!= lkb
->lkb_id
) {
1897 list_add_tail(&lkb
->lkb_rsb_lookup
, &r
->res_lookup
);
1901 if (r
->res_nodeid
== 0) {
1902 lkb
->lkb_nodeid
= 0;
1906 if (r
->res_nodeid
> 0) {
1907 lkb
->lkb_nodeid
= r
->res_nodeid
;
1911 DLM_ASSERT(r
->res_nodeid
== -1, dlm_dump_rsb(r
););
1913 dir_nodeid
= dlm_dir_nodeid(r
);
1915 if (dir_nodeid
!= our_nodeid
) {
1916 r
->res_first_lkid
= lkb
->lkb_id
;
1917 send_lookup(r
, lkb
);
1921 for (i
= 0; i
< 2; i
++) {
1922 /* It's possible for dlm_scand to remove an old rsb for
1923 this same resource from the toss list, us to create
1924 a new one, look up the master locally, and find it
1925 already exists just before dlm_scand does the
1926 dir_remove() on the previous rsb. */
1928 error
= dlm_dir_lookup(ls
, our_nodeid
, r
->res_name
,
1929 r
->res_length
, &ret_nodeid
);
1932 log_debug(ls
, "dir_lookup error %d %s", error
, r
->res_name
);
1935 if (error
&& error
!= -EEXIST
)
1938 if (ret_nodeid
== our_nodeid
) {
1939 r
->res_first_lkid
= 0;
1941 lkb
->lkb_nodeid
= 0;
1943 r
->res_first_lkid
= lkb
->lkb_id
;
1944 r
->res_nodeid
= ret_nodeid
;
1945 lkb
->lkb_nodeid
= ret_nodeid
;
1950 static void process_lookup_list(struct dlm_rsb
*r
)
1952 struct dlm_lkb
*lkb
, *safe
;
1954 list_for_each_entry_safe(lkb
, safe
, &r
->res_lookup
, lkb_rsb_lookup
) {
1955 list_del_init(&lkb
->lkb_rsb_lookup
);
1956 _request_lock(r
, lkb
);
1961 /* confirm_master -- confirm (or deny) an rsb's master nodeid */
1963 static void confirm_master(struct dlm_rsb
*r
, int error
)
1965 struct dlm_lkb
*lkb
;
1967 if (!r
->res_first_lkid
)
1973 r
->res_first_lkid
= 0;
1974 process_lookup_list(r
);
1980 /* the remote request failed and won't be retried (it was
1981 a NOQUEUE, or has been canceled/unlocked); make a waiting
1982 lkb the first_lkid */
1984 r
->res_first_lkid
= 0;
1986 if (!list_empty(&r
->res_lookup
)) {
1987 lkb
= list_entry(r
->res_lookup
.next
, struct dlm_lkb
,
1989 list_del_init(&lkb
->lkb_rsb_lookup
);
1990 r
->res_first_lkid
= lkb
->lkb_id
;
1991 _request_lock(r
, lkb
);
1996 log_error(r
->res_ls
, "confirm_master unknown error %d", error
);
2000 static int set_lock_args(int mode
, struct dlm_lksb
*lksb
, uint32_t flags
,
2001 int namelen
, unsigned long timeout_cs
,
2002 void (*ast
) (void *astparam
),
2004 void (*bast
) (void *astparam
, int mode
),
2005 struct dlm_args
*args
)
2009 /* check for invalid arg usage */
2011 if (mode
< 0 || mode
> DLM_LOCK_EX
)
2014 if (!(flags
& DLM_LKF_CONVERT
) && (namelen
> DLM_RESNAME_MAXLEN
))
2017 if (flags
& DLM_LKF_CANCEL
)
2020 if (flags
& DLM_LKF_QUECVT
&& !(flags
& DLM_LKF_CONVERT
))
2023 if (flags
& DLM_LKF_CONVDEADLK
&& !(flags
& DLM_LKF_CONVERT
))
2026 if (flags
& DLM_LKF_CONVDEADLK
&& flags
& DLM_LKF_NOQUEUE
)
2029 if (flags
& DLM_LKF_EXPEDITE
&& flags
& DLM_LKF_CONVERT
)
2032 if (flags
& DLM_LKF_EXPEDITE
&& flags
& DLM_LKF_QUECVT
)
2035 if (flags
& DLM_LKF_EXPEDITE
&& flags
& DLM_LKF_NOQUEUE
)
2038 if (flags
& DLM_LKF_EXPEDITE
&& mode
!= DLM_LOCK_NL
)
2044 if (flags
& DLM_LKF_VALBLK
&& !lksb
->sb_lvbptr
)
2047 if (flags
& DLM_LKF_CONVERT
&& !lksb
->sb_lkid
)
2050 /* these args will be copied to the lkb in validate_lock_args,
2051 it cannot be done now because when converting locks, fields in
2052 an active lkb cannot be modified before locking the rsb */
2054 args
->flags
= flags
;
2056 args
->astparam
= astparam
;
2057 args
->bastfn
= bast
;
2058 args
->timeout
= timeout_cs
;
2066 static int set_unlock_args(uint32_t flags
, void *astarg
, struct dlm_args
*args
)
2068 if (flags
& ~(DLM_LKF_CANCEL
| DLM_LKF_VALBLK
| DLM_LKF_IVVALBLK
|
2069 DLM_LKF_FORCEUNLOCK
))
2072 if (flags
& DLM_LKF_CANCEL
&& flags
& DLM_LKF_FORCEUNLOCK
)
2075 args
->flags
= flags
;
2076 args
->astparam
= astarg
;
2080 static int validate_lock_args(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
,
2081 struct dlm_args
*args
)
2085 if (args
->flags
& DLM_LKF_CONVERT
) {
2086 if (lkb
->lkb_flags
& DLM_IFL_MSTCPY
)
2089 if (args
->flags
& DLM_LKF_QUECVT
&&
2090 !__quecvt_compat_matrix
[lkb
->lkb_grmode
+1][args
->mode
+1])
2094 if (lkb
->lkb_status
!= DLM_LKSTS_GRANTED
)
2097 if (lkb
->lkb_wait_type
)
2100 if (is_overlap(lkb
))
2104 lkb
->lkb_exflags
= args
->flags
;
2105 lkb
->lkb_sbflags
= 0;
2106 lkb
->lkb_astfn
= args
->astfn
;
2107 lkb
->lkb_astparam
= args
->astparam
;
2108 lkb
->lkb_bastfn
= args
->bastfn
;
2109 lkb
->lkb_rqmode
= args
->mode
;
2110 lkb
->lkb_lksb
= args
->lksb
;
2111 lkb
->lkb_lvbptr
= args
->lksb
->sb_lvbptr
;
2112 lkb
->lkb_ownpid
= (int) current
->pid
;
2113 lkb
->lkb_timeout_cs
= args
->timeout
;
2117 log_debug(ls
, "validate_lock_args %d %x %x %x %d %d %s",
2118 rv
, lkb
->lkb_id
, lkb
->lkb_flags
, args
->flags
,
2119 lkb
->lkb_status
, lkb
->lkb_wait_type
,
2120 lkb
->lkb_resource
->res_name
);
2124 /* when dlm_unlock() sees -EBUSY with CANCEL/FORCEUNLOCK it returns 0
2127 /* note: it's valid for lkb_nodeid/res_nodeid to be -1 when we get here
2128 because there may be a lookup in progress and it's valid to do
2129 cancel/unlockf on it */
2131 static int validate_unlock_args(struct dlm_lkb
*lkb
, struct dlm_args
*args
)
2133 struct dlm_ls
*ls
= lkb
->lkb_resource
->res_ls
;
2136 if (lkb
->lkb_flags
& DLM_IFL_MSTCPY
) {
2137 log_error(ls
, "unlock on MSTCPY %x", lkb
->lkb_id
);
2142 /* an lkb may still exist even though the lock is EOL'ed due to a
2143 cancel, unlock or failed noqueue request; an app can't use these
2144 locks; return same error as if the lkid had not been found at all */
2146 if (lkb
->lkb_flags
& DLM_IFL_ENDOFLIFE
) {
2147 log_debug(ls
, "unlock on ENDOFLIFE %x", lkb
->lkb_id
);
2152 /* an lkb may be waiting for an rsb lookup to complete where the
2153 lookup was initiated by another lock */
2155 if (!list_empty(&lkb
->lkb_rsb_lookup
)) {
2156 if (args
->flags
& (DLM_LKF_CANCEL
| DLM_LKF_FORCEUNLOCK
)) {
2157 log_debug(ls
, "unlock on rsb_lookup %x", lkb
->lkb_id
);
2158 list_del_init(&lkb
->lkb_rsb_lookup
);
2159 queue_cast(lkb
->lkb_resource
, lkb
,
2160 args
->flags
& DLM_LKF_CANCEL
?
2161 -DLM_ECANCEL
: -DLM_EUNLOCK
);
2162 unhold_lkb(lkb
); /* undoes create_lkb() */
2164 /* caller changes -EBUSY to 0 for CANCEL and FORCEUNLOCK */
2169 /* cancel not allowed with another cancel/unlock in progress */
2171 if (args
->flags
& DLM_LKF_CANCEL
) {
2172 if (lkb
->lkb_exflags
& DLM_LKF_CANCEL
)
2175 if (is_overlap(lkb
))
2178 /* don't let scand try to do a cancel */
2181 if (lkb
->lkb_flags
& DLM_IFL_RESEND
) {
2182 lkb
->lkb_flags
|= DLM_IFL_OVERLAP_CANCEL
;
2187 /* there's nothing to cancel */
2188 if (lkb
->lkb_status
== DLM_LKSTS_GRANTED
&&
2189 !lkb
->lkb_wait_type
) {
2194 switch (lkb
->lkb_wait_type
) {
2195 case DLM_MSG_LOOKUP
:
2196 case DLM_MSG_REQUEST
:
2197 lkb
->lkb_flags
|= DLM_IFL_OVERLAP_CANCEL
;
2200 case DLM_MSG_UNLOCK
:
2201 case DLM_MSG_CANCEL
:
2204 /* add_to_waiters() will set OVERLAP_CANCEL */
2208 /* do we need to allow a force-unlock if there's a normal unlock
2209 already in progress? in what conditions could the normal unlock
2210 fail such that we'd want to send a force-unlock to be sure? */
2212 if (args
->flags
& DLM_LKF_FORCEUNLOCK
) {
2213 if (lkb
->lkb_exflags
& DLM_LKF_FORCEUNLOCK
)
2216 if (is_overlap_unlock(lkb
))
2219 /* don't let scand try to do a cancel */
2222 if (lkb
->lkb_flags
& DLM_IFL_RESEND
) {
2223 lkb
->lkb_flags
|= DLM_IFL_OVERLAP_UNLOCK
;
2228 switch (lkb
->lkb_wait_type
) {
2229 case DLM_MSG_LOOKUP
:
2230 case DLM_MSG_REQUEST
:
2231 lkb
->lkb_flags
|= DLM_IFL_OVERLAP_UNLOCK
;
2234 case DLM_MSG_UNLOCK
:
2237 /* add_to_waiters() will set OVERLAP_UNLOCK */
2241 /* normal unlock not allowed if there's any op in progress */
2243 if (lkb
->lkb_wait_type
|| lkb
->lkb_wait_count
)
2247 /* an overlapping op shouldn't blow away exflags from other op */
2248 lkb
->lkb_exflags
|= args
->flags
;
2249 lkb
->lkb_sbflags
= 0;
2250 lkb
->lkb_astparam
= args
->astparam
;
2254 log_debug(ls
, "validate_unlock_args %d %x %x %x %x %d %s", rv
,
2255 lkb
->lkb_id
, lkb
->lkb_flags
, lkb
->lkb_exflags
,
2256 args
->flags
, lkb
->lkb_wait_type
,
2257 lkb
->lkb_resource
->res_name
);
2262 * Four stage 4 varieties:
2263 * do_request(), do_convert(), do_unlock(), do_cancel()
2264 * These are called on the master node for the given lock and
2265 * from the central locking logic.
2268 static int do_request(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2272 if (can_be_granted(r
, lkb
, 1, NULL
)) {
2274 queue_cast(r
, lkb
, 0);
2278 if (can_be_queued(lkb
)) {
2279 error
= -EINPROGRESS
;
2280 add_lkb(r
, lkb
, DLM_LKSTS_WAITING
);
2286 queue_cast(r
, lkb
, -EAGAIN
);
2291 static void do_request_effects(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
2296 if (force_blocking_asts(lkb
))
2297 send_blocking_asts_all(r
, lkb
);
2300 send_blocking_asts(r
, lkb
);
2305 static int do_convert(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2310 /* changing an existing lock may allow others to be granted */
2312 if (can_be_granted(r
, lkb
, 1, &deadlk
)) {
2314 queue_cast(r
, lkb
, 0);
2318 /* can_be_granted() detected that this lock would block in a conversion
2319 deadlock, so we leave it on the granted queue and return EDEADLK in
2320 the ast for the convert. */
2323 /* it's left on the granted queue */
2324 log_debug(r
->res_ls
, "deadlock %x node %d sts%d g%d r%d %s",
2325 lkb
->lkb_id
, lkb
->lkb_nodeid
, lkb
->lkb_status
,
2326 lkb
->lkb_grmode
, lkb
->lkb_rqmode
, r
->res_name
);
2327 revert_lock(r
, lkb
);
2328 queue_cast(r
, lkb
, -EDEADLK
);
2333 /* is_demoted() means the can_be_granted() above set the grmode
2334 to NL, and left us on the granted queue. This auto-demotion
2335 (due to CONVDEADLK) might mean other locks, and/or this lock, are
2336 now grantable. We have to try to grant other converting locks
2337 before we try again to grant this one. */
2339 if (is_demoted(lkb
)) {
2340 grant_pending_convert(r
, DLM_LOCK_IV
, NULL
);
2341 if (_can_be_granted(r
, lkb
, 1)) {
2343 queue_cast(r
, lkb
, 0);
2346 /* else fall through and move to convert queue */
2349 if (can_be_queued(lkb
)) {
2350 error
= -EINPROGRESS
;
2352 add_lkb(r
, lkb
, DLM_LKSTS_CONVERT
);
2358 queue_cast(r
, lkb
, -EAGAIN
);
2363 static void do_convert_effects(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
2368 grant_pending_locks(r
);
2369 /* grant_pending_locks also sends basts */
2372 if (force_blocking_asts(lkb
))
2373 send_blocking_asts_all(r
, lkb
);
2376 send_blocking_asts(r
, lkb
);
2381 static int do_unlock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2383 remove_lock(r
, lkb
);
2384 queue_cast(r
, lkb
, -DLM_EUNLOCK
);
2385 return -DLM_EUNLOCK
;
2388 static void do_unlock_effects(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
2391 grant_pending_locks(r
);
2394 /* returns: 0 did nothing, -DLM_ECANCEL canceled lock */
2396 static int do_cancel(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2400 error
= revert_lock(r
, lkb
);
2402 queue_cast(r
, lkb
, -DLM_ECANCEL
);
2403 return -DLM_ECANCEL
;
2408 static void do_cancel_effects(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
2412 grant_pending_locks(r
);
2416 * Four stage 3 varieties:
2417 * _request_lock(), _convert_lock(), _unlock_lock(), _cancel_lock()
2420 /* add a new lkb to a possibly new rsb, called by requesting process */
2422 static int _request_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2426 /* set_master: sets lkb nodeid from r */
2428 error
= set_master(r
, lkb
);
2437 /* receive_request() calls do_request() on remote node */
2438 error
= send_request(r
, lkb
);
2440 error
= do_request(r
, lkb
);
2441 /* for remote locks the request_reply is sent
2442 between do_request and do_request_effects */
2443 do_request_effects(r
, lkb
, error
);
2449 /* change some property of an existing lkb, e.g. mode */
2451 static int _convert_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2456 /* receive_convert() calls do_convert() on remote node */
2457 error
= send_convert(r
, lkb
);
2459 error
= do_convert(r
, lkb
);
2460 /* for remote locks the convert_reply is sent
2461 between do_convert and do_convert_effects */
2462 do_convert_effects(r
, lkb
, error
);
2468 /* remove an existing lkb from the granted queue */
2470 static int _unlock_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2475 /* receive_unlock() calls do_unlock() on remote node */
2476 error
= send_unlock(r
, lkb
);
2478 error
= do_unlock(r
, lkb
);
2479 /* for remote locks the unlock_reply is sent
2480 between do_unlock and do_unlock_effects */
2481 do_unlock_effects(r
, lkb
, error
);
2487 /* remove an existing lkb from the convert or wait queue */
2489 static int _cancel_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2494 /* receive_cancel() calls do_cancel() on remote node */
2495 error
= send_cancel(r
, lkb
);
2497 error
= do_cancel(r
, lkb
);
2498 /* for remote locks the cancel_reply is sent
2499 between do_cancel and do_cancel_effects */
2500 do_cancel_effects(r
, lkb
, error
);
2507 * Four stage 2 varieties:
2508 * request_lock(), convert_lock(), unlock_lock(), cancel_lock()
2511 static int request_lock(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
, char *name
,
2512 int len
, struct dlm_args
*args
)
2517 error
= validate_lock_args(ls
, lkb
, args
);
2521 error
= find_rsb(ls
, name
, len
, R_CREATE
, &r
);
2528 lkb
->lkb_lksb
->sb_lkid
= lkb
->lkb_id
;
2530 error
= _request_lock(r
, lkb
);
2539 static int convert_lock(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
,
2540 struct dlm_args
*args
)
2545 r
= lkb
->lkb_resource
;
2550 error
= validate_lock_args(ls
, lkb
, args
);
2554 error
= _convert_lock(r
, lkb
);
2561 static int unlock_lock(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
,
2562 struct dlm_args
*args
)
2567 r
= lkb
->lkb_resource
;
2572 error
= validate_unlock_args(lkb
, args
);
2576 error
= _unlock_lock(r
, lkb
);
2583 static int cancel_lock(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
,
2584 struct dlm_args
*args
)
2589 r
= lkb
->lkb_resource
;
2594 error
= validate_unlock_args(lkb
, args
);
2598 error
= _cancel_lock(r
, lkb
);
2606 * Two stage 1 varieties: dlm_lock() and dlm_unlock()
2609 int dlm_lock(dlm_lockspace_t
*lockspace
,
2611 struct dlm_lksb
*lksb
,
2614 unsigned int namelen
,
2615 uint32_t parent_lkid
,
2616 void (*ast
) (void *astarg
),
2618 void (*bast
) (void *astarg
, int mode
))
2621 struct dlm_lkb
*lkb
;
2622 struct dlm_args args
;
2623 int error
, convert
= flags
& DLM_LKF_CONVERT
;
2625 ls
= dlm_find_lockspace_local(lockspace
);
2629 dlm_lock_recovery(ls
);
2632 error
= find_lkb(ls
, lksb
->sb_lkid
, &lkb
);
2634 error
= create_lkb(ls
, &lkb
);
2639 error
= set_lock_args(mode
, lksb
, flags
, namelen
, 0, ast
,
2640 astarg
, bast
, &args
);
2645 error
= convert_lock(ls
, lkb
, &args
);
2647 error
= request_lock(ls
, lkb
, name
, namelen
, &args
);
2649 if (error
== -EINPROGRESS
)
2652 if (convert
|| error
)
2654 if (error
== -EAGAIN
|| error
== -EDEADLK
)
2657 dlm_unlock_recovery(ls
);
2658 dlm_put_lockspace(ls
);
2662 int dlm_unlock(dlm_lockspace_t
*lockspace
,
2665 struct dlm_lksb
*lksb
,
2669 struct dlm_lkb
*lkb
;
2670 struct dlm_args args
;
2673 ls
= dlm_find_lockspace_local(lockspace
);
2677 dlm_lock_recovery(ls
);
2679 error
= find_lkb(ls
, lkid
, &lkb
);
2683 error
= set_unlock_args(flags
, astarg
, &args
);
2687 if (flags
& DLM_LKF_CANCEL
)
2688 error
= cancel_lock(ls
, lkb
, &args
);
2690 error
= unlock_lock(ls
, lkb
, &args
);
2692 if (error
== -DLM_EUNLOCK
|| error
== -DLM_ECANCEL
)
2694 if (error
== -EBUSY
&& (flags
& (DLM_LKF_CANCEL
| DLM_LKF_FORCEUNLOCK
)))
2699 dlm_unlock_recovery(ls
);
2700 dlm_put_lockspace(ls
);
2705 * send/receive routines for remote operations and replies
2709 * send_request receive_request
2710 * send_convert receive_convert
2711 * send_unlock receive_unlock
2712 * send_cancel receive_cancel
2713 * send_grant receive_grant
2714 * send_bast receive_bast
2715 * send_lookup receive_lookup
2716 * send_remove receive_remove
2719 * receive_request_reply send_request_reply
2720 * receive_convert_reply send_convert_reply
2721 * receive_unlock_reply send_unlock_reply
2722 * receive_cancel_reply send_cancel_reply
2723 * receive_lookup_reply send_lookup_reply
2726 static int _create_message(struct dlm_ls
*ls
, int mb_len
,
2727 int to_nodeid
, int mstype
,
2728 struct dlm_message
**ms_ret
,
2729 struct dlm_mhandle
**mh_ret
)
2731 struct dlm_message
*ms
;
2732 struct dlm_mhandle
*mh
;
2735 /* get_buffer gives us a message handle (mh) that we need to
2736 pass into lowcomms_commit and a message buffer (mb) that we
2737 write our data into */
2739 mh
= dlm_lowcomms_get_buffer(to_nodeid
, mb_len
, GFP_NOFS
, &mb
);
2743 memset(mb
, 0, mb_len
);
2745 ms
= (struct dlm_message
*) mb
;
2747 ms
->m_header
.h_version
= (DLM_HEADER_MAJOR
| DLM_HEADER_MINOR
);
2748 ms
->m_header
.h_lockspace
= ls
->ls_global_id
;
2749 ms
->m_header
.h_nodeid
= dlm_our_nodeid();
2750 ms
->m_header
.h_length
= mb_len
;
2751 ms
->m_header
.h_cmd
= DLM_MSG
;
2753 ms
->m_type
= mstype
;
2760 static int create_message(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
2761 int to_nodeid
, int mstype
,
2762 struct dlm_message
**ms_ret
,
2763 struct dlm_mhandle
**mh_ret
)
2765 int mb_len
= sizeof(struct dlm_message
);
2768 case DLM_MSG_REQUEST
:
2769 case DLM_MSG_LOOKUP
:
2770 case DLM_MSG_REMOVE
:
2771 mb_len
+= r
->res_length
;
2773 case DLM_MSG_CONVERT
:
2774 case DLM_MSG_UNLOCK
:
2775 case DLM_MSG_REQUEST_REPLY
:
2776 case DLM_MSG_CONVERT_REPLY
:
2778 if (lkb
&& lkb
->lkb_lvbptr
)
2779 mb_len
+= r
->res_ls
->ls_lvblen
;
2783 return _create_message(r
->res_ls
, mb_len
, to_nodeid
, mstype
,
2787 /* further lowcomms enhancements or alternate implementations may make
2788 the return value from this function useful at some point */
2790 static int send_message(struct dlm_mhandle
*mh
, struct dlm_message
*ms
)
2792 dlm_message_out(ms
);
2793 dlm_lowcomms_commit_buffer(mh
);
2797 static void send_args(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
2798 struct dlm_message
*ms
)
2800 ms
->m_nodeid
= lkb
->lkb_nodeid
;
2801 ms
->m_pid
= lkb
->lkb_ownpid
;
2802 ms
->m_lkid
= lkb
->lkb_id
;
2803 ms
->m_remid
= lkb
->lkb_remid
;
2804 ms
->m_exflags
= lkb
->lkb_exflags
;
2805 ms
->m_sbflags
= lkb
->lkb_sbflags
;
2806 ms
->m_flags
= lkb
->lkb_flags
;
2807 ms
->m_lvbseq
= lkb
->lkb_lvbseq
;
2808 ms
->m_status
= lkb
->lkb_status
;
2809 ms
->m_grmode
= lkb
->lkb_grmode
;
2810 ms
->m_rqmode
= lkb
->lkb_rqmode
;
2811 ms
->m_hash
= r
->res_hash
;
2813 /* m_result and m_bastmode are set from function args,
2814 not from lkb fields */
2816 if (lkb
->lkb_bastfn
)
2817 ms
->m_asts
|= DLM_CB_BAST
;
2819 ms
->m_asts
|= DLM_CB_CAST
;
2821 /* compare with switch in create_message; send_remove() doesn't
2824 switch (ms
->m_type
) {
2825 case DLM_MSG_REQUEST
:
2826 case DLM_MSG_LOOKUP
:
2827 memcpy(ms
->m_extra
, r
->res_name
, r
->res_length
);
2829 case DLM_MSG_CONVERT
:
2830 case DLM_MSG_UNLOCK
:
2831 case DLM_MSG_REQUEST_REPLY
:
2832 case DLM_MSG_CONVERT_REPLY
:
2834 if (!lkb
->lkb_lvbptr
)
2836 memcpy(ms
->m_extra
, lkb
->lkb_lvbptr
, r
->res_ls
->ls_lvblen
);
2841 static int send_common(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int mstype
)
2843 struct dlm_message
*ms
;
2844 struct dlm_mhandle
*mh
;
2845 int to_nodeid
, error
;
2847 error
= add_to_waiters(lkb
, mstype
);
2851 to_nodeid
= r
->res_nodeid
;
2853 error
= create_message(r
, lkb
, to_nodeid
, mstype
, &ms
, &mh
);
2857 send_args(r
, lkb
, ms
);
2859 error
= send_message(mh
, ms
);
2865 remove_from_waiters(lkb
, msg_reply_type(mstype
));
2869 static int send_request(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2871 return send_common(r
, lkb
, DLM_MSG_REQUEST
);
2874 static int send_convert(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2878 error
= send_common(r
, lkb
, DLM_MSG_CONVERT
);
2880 /* down conversions go without a reply from the master */
2881 if (!error
&& down_conversion(lkb
)) {
2882 remove_from_waiters(lkb
, DLM_MSG_CONVERT_REPLY
);
2883 r
->res_ls
->ls_stub_ms
.m_type
= DLM_MSG_CONVERT_REPLY
;
2884 r
->res_ls
->ls_stub_ms
.m_result
= 0;
2885 r
->res_ls
->ls_stub_ms
.m_flags
= lkb
->lkb_flags
;
2886 __receive_convert_reply(r
, lkb
, &r
->res_ls
->ls_stub_ms
);
2892 /* FIXME: if this lkb is the only lock we hold on the rsb, then set
2893 MASTER_UNCERTAIN to force the next request on the rsb to confirm
2894 that the master is still correct. */
2896 static int send_unlock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2898 return send_common(r
, lkb
, DLM_MSG_UNLOCK
);
2901 static int send_cancel(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2903 return send_common(r
, lkb
, DLM_MSG_CANCEL
);
2906 static int send_grant(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2908 struct dlm_message
*ms
;
2909 struct dlm_mhandle
*mh
;
2910 int to_nodeid
, error
;
2912 to_nodeid
= lkb
->lkb_nodeid
;
2914 error
= create_message(r
, lkb
, to_nodeid
, DLM_MSG_GRANT
, &ms
, &mh
);
2918 send_args(r
, lkb
, ms
);
2922 error
= send_message(mh
, ms
);
2927 static int send_bast(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int mode
)
2929 struct dlm_message
*ms
;
2930 struct dlm_mhandle
*mh
;
2931 int to_nodeid
, error
;
2933 to_nodeid
= lkb
->lkb_nodeid
;
2935 error
= create_message(r
, NULL
, to_nodeid
, DLM_MSG_BAST
, &ms
, &mh
);
2939 send_args(r
, lkb
, ms
);
2941 ms
->m_bastmode
= mode
;
2943 error
= send_message(mh
, ms
);
2948 static int send_lookup(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
2950 struct dlm_message
*ms
;
2951 struct dlm_mhandle
*mh
;
2952 int to_nodeid
, error
;
2954 error
= add_to_waiters(lkb
, DLM_MSG_LOOKUP
);
2958 to_nodeid
= dlm_dir_nodeid(r
);
2960 error
= create_message(r
, NULL
, to_nodeid
, DLM_MSG_LOOKUP
, &ms
, &mh
);
2964 send_args(r
, lkb
, ms
);
2966 error
= send_message(mh
, ms
);
2972 remove_from_waiters(lkb
, DLM_MSG_LOOKUP_REPLY
);
2976 static int send_remove(struct dlm_rsb
*r
)
2978 struct dlm_message
*ms
;
2979 struct dlm_mhandle
*mh
;
2980 int to_nodeid
, error
;
2982 to_nodeid
= dlm_dir_nodeid(r
);
2984 error
= create_message(r
, NULL
, to_nodeid
, DLM_MSG_REMOVE
, &ms
, &mh
);
2988 memcpy(ms
->m_extra
, r
->res_name
, r
->res_length
);
2989 ms
->m_hash
= r
->res_hash
;
2991 error
= send_message(mh
, ms
);
2996 static int send_common_reply(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
2999 struct dlm_message
*ms
;
3000 struct dlm_mhandle
*mh
;
3001 int to_nodeid
, error
;
3003 to_nodeid
= lkb
->lkb_nodeid
;
3005 error
= create_message(r
, lkb
, to_nodeid
, mstype
, &ms
, &mh
);
3009 send_args(r
, lkb
, ms
);
3013 error
= send_message(mh
, ms
);
3018 static int send_request_reply(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int rv
)
3020 return send_common_reply(r
, lkb
, DLM_MSG_REQUEST_REPLY
, rv
);
3023 static int send_convert_reply(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int rv
)
3025 return send_common_reply(r
, lkb
, DLM_MSG_CONVERT_REPLY
, rv
);
3028 static int send_unlock_reply(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int rv
)
3030 return send_common_reply(r
, lkb
, DLM_MSG_UNLOCK_REPLY
, rv
);
3033 static int send_cancel_reply(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
, int rv
)
3035 return send_common_reply(r
, lkb
, DLM_MSG_CANCEL_REPLY
, rv
);
3038 static int send_lookup_reply(struct dlm_ls
*ls
, struct dlm_message
*ms_in
,
3039 int ret_nodeid
, int rv
)
3041 struct dlm_rsb
*r
= &ls
->ls_stub_rsb
;
3042 struct dlm_message
*ms
;
3043 struct dlm_mhandle
*mh
;
3044 int error
, nodeid
= ms_in
->m_header
.h_nodeid
;
3046 error
= create_message(r
, NULL
, nodeid
, DLM_MSG_LOOKUP_REPLY
, &ms
, &mh
);
3050 ms
->m_lkid
= ms_in
->m_lkid
;
3052 ms
->m_nodeid
= ret_nodeid
;
3054 error
= send_message(mh
, ms
);
3059 /* which args we save from a received message depends heavily on the type
3060 of message, unlike the send side where we can safely send everything about
3061 the lkb for any type of message */
3063 static void receive_flags(struct dlm_lkb
*lkb
, struct dlm_message
*ms
)
3065 lkb
->lkb_exflags
= ms
->m_exflags
;
3066 lkb
->lkb_sbflags
= ms
->m_sbflags
;
3067 lkb
->lkb_flags
= (lkb
->lkb_flags
& 0xFFFF0000) |
3068 (ms
->m_flags
& 0x0000FFFF);
3071 static void receive_flags_reply(struct dlm_lkb
*lkb
, struct dlm_message
*ms
)
3073 lkb
->lkb_sbflags
= ms
->m_sbflags
;
3074 lkb
->lkb_flags
= (lkb
->lkb_flags
& 0xFFFF0000) |
3075 (ms
->m_flags
& 0x0000FFFF);
3078 static int receive_extralen(struct dlm_message
*ms
)
3080 return (ms
->m_header
.h_length
- sizeof(struct dlm_message
));
3083 static int receive_lvb(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
,
3084 struct dlm_message
*ms
)
3088 if (lkb
->lkb_exflags
& DLM_LKF_VALBLK
) {
3089 if (!lkb
->lkb_lvbptr
)
3090 lkb
->lkb_lvbptr
= dlm_allocate_lvb(ls
);
3091 if (!lkb
->lkb_lvbptr
)
3093 len
= receive_extralen(ms
);
3094 if (len
> DLM_RESNAME_MAXLEN
)
3095 len
= DLM_RESNAME_MAXLEN
;
3096 memcpy(lkb
->lkb_lvbptr
, ms
->m_extra
, len
);
3101 static void fake_bastfn(void *astparam
, int mode
)
3103 log_print("fake_bastfn should not be called");
3106 static void fake_astfn(void *astparam
)
3108 log_print("fake_astfn should not be called");
3111 static int receive_request_args(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
,
3112 struct dlm_message
*ms
)
3114 lkb
->lkb_nodeid
= ms
->m_header
.h_nodeid
;
3115 lkb
->lkb_ownpid
= ms
->m_pid
;
3116 lkb
->lkb_remid
= ms
->m_lkid
;
3117 lkb
->lkb_grmode
= DLM_LOCK_IV
;
3118 lkb
->lkb_rqmode
= ms
->m_rqmode
;
3120 lkb
->lkb_bastfn
= (ms
->m_asts
& DLM_CB_BAST
) ? &fake_bastfn
: NULL
;
3121 lkb
->lkb_astfn
= (ms
->m_asts
& DLM_CB_CAST
) ? &fake_astfn
: NULL
;
3123 if (lkb
->lkb_exflags
& DLM_LKF_VALBLK
) {
3124 /* lkb was just created so there won't be an lvb yet */
3125 lkb
->lkb_lvbptr
= dlm_allocate_lvb(ls
);
3126 if (!lkb
->lkb_lvbptr
)
3133 static int receive_convert_args(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
,
3134 struct dlm_message
*ms
)
3136 if (lkb
->lkb_status
!= DLM_LKSTS_GRANTED
)
3139 if (receive_lvb(ls
, lkb
, ms
))
3142 lkb
->lkb_rqmode
= ms
->m_rqmode
;
3143 lkb
->lkb_lvbseq
= ms
->m_lvbseq
;
3148 static int receive_unlock_args(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
,
3149 struct dlm_message
*ms
)
3151 if (receive_lvb(ls
, lkb
, ms
))
3156 /* We fill in the stub-lkb fields with the info that send_xxxx_reply()
3157 uses to send a reply and that the remote end uses to process the reply. */
3159 static void setup_stub_lkb(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3161 struct dlm_lkb
*lkb
= &ls
->ls_stub_lkb
;
3162 lkb
->lkb_nodeid
= ms
->m_header
.h_nodeid
;
3163 lkb
->lkb_remid
= ms
->m_lkid
;
3166 /* This is called after the rsb is locked so that we can safely inspect
3167 fields in the lkb. */
3169 static int validate_message(struct dlm_lkb
*lkb
, struct dlm_message
*ms
)
3171 int from
= ms
->m_header
.h_nodeid
;
3174 switch (ms
->m_type
) {
3175 case DLM_MSG_CONVERT
:
3176 case DLM_MSG_UNLOCK
:
3177 case DLM_MSG_CANCEL
:
3178 if (!is_master_copy(lkb
) || lkb
->lkb_nodeid
!= from
)
3182 case DLM_MSG_CONVERT_REPLY
:
3183 case DLM_MSG_UNLOCK_REPLY
:
3184 case DLM_MSG_CANCEL_REPLY
:
3187 if (!is_process_copy(lkb
) || lkb
->lkb_nodeid
!= from
)
3191 case DLM_MSG_REQUEST_REPLY
:
3192 if (!is_process_copy(lkb
))
3194 else if (lkb
->lkb_nodeid
!= -1 && lkb
->lkb_nodeid
!= from
)
3203 log_error(lkb
->lkb_resource
->res_ls
,
3204 "ignore invalid message %d from %d %x %x %x %d",
3205 ms
->m_type
, from
, lkb
->lkb_id
, lkb
->lkb_remid
,
3206 lkb
->lkb_flags
, lkb
->lkb_nodeid
);
3210 static void receive_request(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3212 struct dlm_lkb
*lkb
;
3216 error
= create_lkb(ls
, &lkb
);
3220 receive_flags(lkb
, ms
);
3221 lkb
->lkb_flags
|= DLM_IFL_MSTCPY
;
3222 error
= receive_request_args(ls
, lkb
, ms
);
3228 namelen
= receive_extralen(ms
);
3230 error
= find_rsb(ls
, ms
->m_extra
, namelen
, R_MASTER
, &r
);
3239 error
= do_request(r
, lkb
);
3240 send_request_reply(r
, lkb
, error
);
3241 do_request_effects(r
, lkb
, error
);
3246 if (error
== -EINPROGRESS
)
3253 setup_stub_lkb(ls
, ms
);
3254 send_request_reply(&ls
->ls_stub_rsb
, &ls
->ls_stub_lkb
, error
);
3257 static void receive_convert(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3259 struct dlm_lkb
*lkb
;
3261 int error
, reply
= 1;
3263 error
= find_lkb(ls
, ms
->m_remid
, &lkb
);
3267 r
= lkb
->lkb_resource
;
3272 error
= validate_message(lkb
, ms
);
3276 receive_flags(lkb
, ms
);
3278 error
= receive_convert_args(ls
, lkb
, ms
);
3280 send_convert_reply(r
, lkb
, error
);
3284 reply
= !down_conversion(lkb
);
3286 error
= do_convert(r
, lkb
);
3288 send_convert_reply(r
, lkb
, error
);
3289 do_convert_effects(r
, lkb
, error
);
3297 setup_stub_lkb(ls
, ms
);
3298 send_convert_reply(&ls
->ls_stub_rsb
, &ls
->ls_stub_lkb
, error
);
3301 static void receive_unlock(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3303 struct dlm_lkb
*lkb
;
3307 error
= find_lkb(ls
, ms
->m_remid
, &lkb
);
3311 r
= lkb
->lkb_resource
;
3316 error
= validate_message(lkb
, ms
);
3320 receive_flags(lkb
, ms
);
3322 error
= receive_unlock_args(ls
, lkb
, ms
);
3324 send_unlock_reply(r
, lkb
, error
);
3328 error
= do_unlock(r
, lkb
);
3329 send_unlock_reply(r
, lkb
, error
);
3330 do_unlock_effects(r
, lkb
, error
);
3338 setup_stub_lkb(ls
, ms
);
3339 send_unlock_reply(&ls
->ls_stub_rsb
, &ls
->ls_stub_lkb
, error
);
3342 static void receive_cancel(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3344 struct dlm_lkb
*lkb
;
3348 error
= find_lkb(ls
, ms
->m_remid
, &lkb
);
3352 receive_flags(lkb
, ms
);
3354 r
= lkb
->lkb_resource
;
3359 error
= validate_message(lkb
, ms
);
3363 error
= do_cancel(r
, lkb
);
3364 send_cancel_reply(r
, lkb
, error
);
3365 do_cancel_effects(r
, lkb
, error
);
3373 setup_stub_lkb(ls
, ms
);
3374 send_cancel_reply(&ls
->ls_stub_rsb
, &ls
->ls_stub_lkb
, error
);
3377 static void receive_grant(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3379 struct dlm_lkb
*lkb
;
3383 error
= find_lkb(ls
, ms
->m_remid
, &lkb
);
3385 log_debug(ls
, "receive_grant from %d no lkb %x",
3386 ms
->m_header
.h_nodeid
, ms
->m_remid
);
3390 r
= lkb
->lkb_resource
;
3395 error
= validate_message(lkb
, ms
);
3399 receive_flags_reply(lkb
, ms
);
3400 if (is_altmode(lkb
))
3401 munge_altmode(lkb
, ms
);
3402 grant_lock_pc(r
, lkb
, ms
);
3403 queue_cast(r
, lkb
, 0);
3410 static void receive_bast(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3412 struct dlm_lkb
*lkb
;
3416 error
= find_lkb(ls
, ms
->m_remid
, &lkb
);
3418 log_debug(ls
, "receive_bast from %d no lkb %x",
3419 ms
->m_header
.h_nodeid
, ms
->m_remid
);
3423 r
= lkb
->lkb_resource
;
3428 error
= validate_message(lkb
, ms
);
3432 queue_bast(r
, lkb
, ms
->m_bastmode
);
3439 static void receive_lookup(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3441 int len
, error
, ret_nodeid
, dir_nodeid
, from_nodeid
, our_nodeid
;
3443 from_nodeid
= ms
->m_header
.h_nodeid
;
3444 our_nodeid
= dlm_our_nodeid();
3446 len
= receive_extralen(ms
);
3448 dir_nodeid
= dlm_hash2nodeid(ls
, ms
->m_hash
);
3449 if (dir_nodeid
!= our_nodeid
) {
3450 log_error(ls
, "lookup dir_nodeid %d from %d",
3451 dir_nodeid
, from_nodeid
);
3457 error
= dlm_dir_lookup(ls
, from_nodeid
, ms
->m_extra
, len
, &ret_nodeid
);
3459 /* Optimization: we're master so treat lookup as a request */
3460 if (!error
&& ret_nodeid
== our_nodeid
) {
3461 receive_request(ls
, ms
);
3465 send_lookup_reply(ls
, ms
, ret_nodeid
, error
);
3468 static void receive_remove(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3470 int len
, dir_nodeid
, from_nodeid
;
3472 from_nodeid
= ms
->m_header
.h_nodeid
;
3474 len
= receive_extralen(ms
);
3476 dir_nodeid
= dlm_hash2nodeid(ls
, ms
->m_hash
);
3477 if (dir_nodeid
!= dlm_our_nodeid()) {
3478 log_error(ls
, "remove dir entry dir_nodeid %d from %d",
3479 dir_nodeid
, from_nodeid
);
3483 dlm_dir_remove_entry(ls
, from_nodeid
, ms
->m_extra
, len
);
3486 static void receive_purge(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3488 do_purge(ls
, ms
->m_nodeid
, ms
->m_pid
);
3491 static void receive_request_reply(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3493 struct dlm_lkb
*lkb
;
3495 int error
, mstype
, result
;
3497 error
= find_lkb(ls
, ms
->m_remid
, &lkb
);
3499 log_debug(ls
, "receive_request_reply from %d no lkb %x",
3500 ms
->m_header
.h_nodeid
, ms
->m_remid
);
3504 r
= lkb
->lkb_resource
;
3508 error
= validate_message(lkb
, ms
);
3512 mstype
= lkb
->lkb_wait_type
;
3513 error
= remove_from_waiters(lkb
, DLM_MSG_REQUEST_REPLY
);
3517 /* Optimization: the dir node was also the master, so it took our
3518 lookup as a request and sent request reply instead of lookup reply */
3519 if (mstype
== DLM_MSG_LOOKUP
) {
3520 r
->res_nodeid
= ms
->m_header
.h_nodeid
;
3521 lkb
->lkb_nodeid
= r
->res_nodeid
;
3524 /* this is the value returned from do_request() on the master */
3525 result
= ms
->m_result
;
3529 /* request would block (be queued) on remote master */
3530 queue_cast(r
, lkb
, -EAGAIN
);
3531 confirm_master(r
, -EAGAIN
);
3532 unhold_lkb(lkb
); /* undoes create_lkb() */
3537 /* request was queued or granted on remote master */
3538 receive_flags_reply(lkb
, ms
);
3539 lkb
->lkb_remid
= ms
->m_lkid
;
3540 if (is_altmode(lkb
))
3541 munge_altmode(lkb
, ms
);
3543 add_lkb(r
, lkb
, DLM_LKSTS_WAITING
);
3546 grant_lock_pc(r
, lkb
, ms
);
3547 queue_cast(r
, lkb
, 0);
3549 confirm_master(r
, result
);
3554 /* find_rsb failed to find rsb or rsb wasn't master */
3555 log_debug(ls
, "receive_request_reply %x %x master diff %d %d",
3556 lkb
->lkb_id
, lkb
->lkb_flags
, r
->res_nodeid
, result
);
3558 lkb
->lkb_nodeid
= -1;
3560 if (is_overlap(lkb
)) {
3561 /* we'll ignore error in cancel/unlock reply */
3562 queue_cast_overlap(r
, lkb
);
3563 confirm_master(r
, result
);
3564 unhold_lkb(lkb
); /* undoes create_lkb() */
3566 _request_lock(r
, lkb
);
3570 log_error(ls
, "receive_request_reply %x error %d",
3571 lkb
->lkb_id
, result
);
3574 if (is_overlap_unlock(lkb
) && (result
== 0 || result
== -EINPROGRESS
)) {
3575 log_debug(ls
, "receive_request_reply %x result %d unlock",
3576 lkb
->lkb_id
, result
);
3577 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_UNLOCK
;
3578 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_CANCEL
;
3579 send_unlock(r
, lkb
);
3580 } else if (is_overlap_cancel(lkb
) && (result
== -EINPROGRESS
)) {
3581 log_debug(ls
, "receive_request_reply %x cancel", lkb
->lkb_id
);
3582 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_UNLOCK
;
3583 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_CANCEL
;
3584 send_cancel(r
, lkb
);
3586 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_CANCEL
;
3587 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_UNLOCK
;
3595 static void __receive_convert_reply(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
3596 struct dlm_message
*ms
)
3598 /* this is the value returned from do_convert() on the master */
3599 switch (ms
->m_result
) {
3601 /* convert would block (be queued) on remote master */
3602 queue_cast(r
, lkb
, -EAGAIN
);
3606 receive_flags_reply(lkb
, ms
);
3607 revert_lock_pc(r
, lkb
);
3608 queue_cast(r
, lkb
, -EDEADLK
);
3612 /* convert was queued on remote master */
3613 receive_flags_reply(lkb
, ms
);
3614 if (is_demoted(lkb
))
3615 munge_demoted(lkb
, ms
);
3617 add_lkb(r
, lkb
, DLM_LKSTS_CONVERT
);
3622 /* convert was granted on remote master */
3623 receive_flags_reply(lkb
, ms
);
3624 if (is_demoted(lkb
))
3625 munge_demoted(lkb
, ms
);
3626 grant_lock_pc(r
, lkb
, ms
);
3627 queue_cast(r
, lkb
, 0);
3631 log_error(r
->res_ls
, "receive_convert_reply %x error %d",
3632 lkb
->lkb_id
, ms
->m_result
);
3636 static void _receive_convert_reply(struct dlm_lkb
*lkb
, struct dlm_message
*ms
)
3638 struct dlm_rsb
*r
= lkb
->lkb_resource
;
3644 error
= validate_message(lkb
, ms
);
3648 /* stub reply can happen with waiters_mutex held */
3649 error
= remove_from_waiters_ms(lkb
, ms
);
3653 __receive_convert_reply(r
, lkb
, ms
);
3659 static void receive_convert_reply(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3661 struct dlm_lkb
*lkb
;
3664 error
= find_lkb(ls
, ms
->m_remid
, &lkb
);
3666 log_debug(ls
, "receive_convert_reply from %d no lkb %x",
3667 ms
->m_header
.h_nodeid
, ms
->m_remid
);
3671 _receive_convert_reply(lkb
, ms
);
3675 static void _receive_unlock_reply(struct dlm_lkb
*lkb
, struct dlm_message
*ms
)
3677 struct dlm_rsb
*r
= lkb
->lkb_resource
;
3683 error
= validate_message(lkb
, ms
);
3687 /* stub reply can happen with waiters_mutex held */
3688 error
= remove_from_waiters_ms(lkb
, ms
);
3692 /* this is the value returned from do_unlock() on the master */
3694 switch (ms
->m_result
) {
3696 receive_flags_reply(lkb
, ms
);
3697 remove_lock_pc(r
, lkb
);
3698 queue_cast(r
, lkb
, -DLM_EUNLOCK
);
3703 log_error(r
->res_ls
, "receive_unlock_reply %x error %d",
3704 lkb
->lkb_id
, ms
->m_result
);
3711 static void receive_unlock_reply(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3713 struct dlm_lkb
*lkb
;
3716 error
= find_lkb(ls
, ms
->m_remid
, &lkb
);
3718 log_debug(ls
, "receive_unlock_reply from %d no lkb %x",
3719 ms
->m_header
.h_nodeid
, ms
->m_remid
);
3723 _receive_unlock_reply(lkb
, ms
);
3727 static void _receive_cancel_reply(struct dlm_lkb
*lkb
, struct dlm_message
*ms
)
3729 struct dlm_rsb
*r
= lkb
->lkb_resource
;
3735 error
= validate_message(lkb
, ms
);
3739 /* stub reply can happen with waiters_mutex held */
3740 error
= remove_from_waiters_ms(lkb
, ms
);
3744 /* this is the value returned from do_cancel() on the master */
3746 switch (ms
->m_result
) {
3748 receive_flags_reply(lkb
, ms
);
3749 revert_lock_pc(r
, lkb
);
3750 queue_cast(r
, lkb
, -DLM_ECANCEL
);
3755 log_error(r
->res_ls
, "receive_cancel_reply %x error %d",
3756 lkb
->lkb_id
, ms
->m_result
);
3763 static void receive_cancel_reply(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3765 struct dlm_lkb
*lkb
;
3768 error
= find_lkb(ls
, ms
->m_remid
, &lkb
);
3770 log_debug(ls
, "receive_cancel_reply from %d no lkb %x",
3771 ms
->m_header
.h_nodeid
, ms
->m_remid
);
3775 _receive_cancel_reply(lkb
, ms
);
3779 static void receive_lookup_reply(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3781 struct dlm_lkb
*lkb
;
3783 int error
, ret_nodeid
;
3785 error
= find_lkb(ls
, ms
->m_lkid
, &lkb
);
3787 log_error(ls
, "receive_lookup_reply no lkb");
3791 /* ms->m_result is the value returned by dlm_dir_lookup on dir node
3792 FIXME: will a non-zero error ever be returned? */
3794 r
= lkb
->lkb_resource
;
3798 error
= remove_from_waiters(lkb
, DLM_MSG_LOOKUP_REPLY
);
3802 ret_nodeid
= ms
->m_nodeid
;
3803 if (ret_nodeid
== dlm_our_nodeid()) {
3806 r
->res_first_lkid
= 0;
3808 /* set_master() will copy res_nodeid to lkb_nodeid */
3809 r
->res_nodeid
= ret_nodeid
;
3812 if (is_overlap(lkb
)) {
3813 log_debug(ls
, "receive_lookup_reply %x unlock %x",
3814 lkb
->lkb_id
, lkb
->lkb_flags
);
3815 queue_cast_overlap(r
, lkb
);
3816 unhold_lkb(lkb
); /* undoes create_lkb() */
3820 _request_lock(r
, lkb
);
3824 process_lookup_list(r
);
3831 static void _receive_message(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3833 if (!dlm_is_member(ls
, ms
->m_header
.h_nodeid
)) {
3834 log_debug(ls
, "ignore non-member message %d from %d %x %x %d",
3835 ms
->m_type
, ms
->m_header
.h_nodeid
, ms
->m_lkid
,
3836 ms
->m_remid
, ms
->m_result
);
3840 switch (ms
->m_type
) {
3842 /* messages sent to a master node */
3844 case DLM_MSG_REQUEST
:
3845 receive_request(ls
, ms
);
3848 case DLM_MSG_CONVERT
:
3849 receive_convert(ls
, ms
);
3852 case DLM_MSG_UNLOCK
:
3853 receive_unlock(ls
, ms
);
3856 case DLM_MSG_CANCEL
:
3857 receive_cancel(ls
, ms
);
3860 /* messages sent from a master node (replies to above) */
3862 case DLM_MSG_REQUEST_REPLY
:
3863 receive_request_reply(ls
, ms
);
3866 case DLM_MSG_CONVERT_REPLY
:
3867 receive_convert_reply(ls
, ms
);
3870 case DLM_MSG_UNLOCK_REPLY
:
3871 receive_unlock_reply(ls
, ms
);
3874 case DLM_MSG_CANCEL_REPLY
:
3875 receive_cancel_reply(ls
, ms
);
3878 /* messages sent from a master node (only two types of async msg) */
3881 receive_grant(ls
, ms
);
3885 receive_bast(ls
, ms
);
3888 /* messages sent to a dir node */
3890 case DLM_MSG_LOOKUP
:
3891 receive_lookup(ls
, ms
);
3894 case DLM_MSG_REMOVE
:
3895 receive_remove(ls
, ms
);
3898 /* messages sent from a dir node (remove has no reply) */
3900 case DLM_MSG_LOOKUP_REPLY
:
3901 receive_lookup_reply(ls
, ms
);
3904 /* other messages */
3907 receive_purge(ls
, ms
);
3911 log_error(ls
, "unknown message type %d", ms
->m_type
);
3917 /* If the lockspace is in recovery mode (locking stopped), then normal
3918 messages are saved on the requestqueue for processing after recovery is
3919 done. When not in recovery mode, we wait for dlm_recoverd to drain saved
3920 messages off the requestqueue before we process new ones. This occurs right
3921 after recovery completes when we transition from saving all messages on
3922 requestqueue, to processing all the saved messages, to processing new
3923 messages as they arrive. */
3925 static void dlm_receive_message(struct dlm_ls
*ls
, struct dlm_message
*ms
,
3928 if (dlm_locking_stopped(ls
)) {
3929 dlm_add_requestqueue(ls
, nodeid
, ms
);
3931 dlm_wait_requestqueue(ls
);
3932 _receive_message(ls
, ms
);
3936 /* This is called by dlm_recoverd to process messages that were saved on
3937 the requestqueue. */
3939 void dlm_receive_message_saved(struct dlm_ls
*ls
, struct dlm_message
*ms
)
3941 _receive_message(ls
, ms
);
3944 /* This is called by the midcomms layer when something is received for
3945 the lockspace. It could be either a MSG (normal message sent as part of
3946 standard locking activity) or an RCOM (recovery message sent as part of
3947 lockspace recovery). */
3949 void dlm_receive_buffer(union dlm_packet
*p
, int nodeid
)
3951 struct dlm_header
*hd
= &p
->header
;
3955 switch (hd
->h_cmd
) {
3957 dlm_message_in(&p
->message
);
3958 type
= p
->message
.m_type
;
3961 dlm_rcom_in(&p
->rcom
);
3962 type
= p
->rcom
.rc_type
;
3965 log_print("invalid h_cmd %d from %u", hd
->h_cmd
, nodeid
);
3969 if (hd
->h_nodeid
!= nodeid
) {
3970 log_print("invalid h_nodeid %d from %d lockspace %x",
3971 hd
->h_nodeid
, nodeid
, hd
->h_lockspace
);
3975 ls
= dlm_find_lockspace_global(hd
->h_lockspace
);
3977 if (dlm_config
.ci_log_debug
)
3978 log_print("invalid lockspace %x from %d cmd %d type %d",
3979 hd
->h_lockspace
, nodeid
, hd
->h_cmd
, type
);
3981 if (hd
->h_cmd
== DLM_RCOM
&& type
== DLM_RCOM_STATUS
)
3982 dlm_send_ls_not_ready(nodeid
, &p
->rcom
);
3986 /* this rwsem allows dlm_ls_stop() to wait for all dlm_recv threads to
3987 be inactive (in this ls) before transitioning to recovery mode */
3989 down_read(&ls
->ls_recv_active
);
3990 if (hd
->h_cmd
== DLM_MSG
)
3991 dlm_receive_message(ls
, &p
->message
, nodeid
);
3993 dlm_receive_rcom(ls
, &p
->rcom
, nodeid
);
3994 up_read(&ls
->ls_recv_active
);
3996 dlm_put_lockspace(ls
);
3999 static void recover_convert_waiter(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
)
4001 if (middle_conversion(lkb
)) {
4003 ls
->ls_stub_ms
.m_type
= DLM_MSG_CONVERT_REPLY
;
4004 ls
->ls_stub_ms
.m_result
= -EINPROGRESS
;
4005 ls
->ls_stub_ms
.m_flags
= lkb
->lkb_flags
;
4006 ls
->ls_stub_ms
.m_header
.h_nodeid
= lkb
->lkb_nodeid
;
4007 _receive_convert_reply(lkb
, &ls
->ls_stub_ms
);
4009 /* Same special case as in receive_rcom_lock_args() */
4010 lkb
->lkb_grmode
= DLM_LOCK_IV
;
4011 rsb_set_flag(lkb
->lkb_resource
, RSB_RECOVER_CONVERT
);
4014 } else if (lkb
->lkb_rqmode
>= lkb
->lkb_grmode
) {
4015 lkb
->lkb_flags
|= DLM_IFL_RESEND
;
4018 /* lkb->lkb_rqmode < lkb->lkb_grmode shouldn't happen since down
4019 conversions are async; there's no reply from the remote master */
4022 /* A waiting lkb needs recovery if the master node has failed, or
4023 the master node is changing (only when no directory is used) */
4025 static int waiter_needs_recovery(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
)
4027 if (dlm_is_removed(ls
, lkb
->lkb_nodeid
))
4030 if (!dlm_no_directory(ls
))
4033 if (dlm_dir_nodeid(lkb
->lkb_resource
) != lkb
->lkb_nodeid
)
4039 /* Recovery for locks that are waiting for replies from nodes that are now
4040 gone. We can just complete unlocks and cancels by faking a reply from the
4041 dead node. Requests and up-conversions we flag to be resent after
4042 recovery. Down-conversions can just be completed with a fake reply like
4043 unlocks. Conversions between PR and CW need special attention. */
4045 void dlm_recover_waiters_pre(struct dlm_ls
*ls
)
4047 struct dlm_lkb
*lkb
, *safe
;
4048 int wait_type
, stub_unlock_result
, stub_cancel_result
;
4050 mutex_lock(&ls
->ls_waiters_mutex
);
4052 list_for_each_entry_safe(lkb
, safe
, &ls
->ls_waiters
, lkb_wait_reply
) {
4053 log_debug(ls
, "pre recover waiter lkid %x type %d flags %x",
4054 lkb
->lkb_id
, lkb
->lkb_wait_type
, lkb
->lkb_flags
);
4056 /* all outstanding lookups, regardless of destination will be
4057 resent after recovery is done */
4059 if (lkb
->lkb_wait_type
== DLM_MSG_LOOKUP
) {
4060 lkb
->lkb_flags
|= DLM_IFL_RESEND
;
4064 if (!waiter_needs_recovery(ls
, lkb
))
4067 wait_type
= lkb
->lkb_wait_type
;
4068 stub_unlock_result
= -DLM_EUNLOCK
;
4069 stub_cancel_result
= -DLM_ECANCEL
;
4071 /* Main reply may have been received leaving a zero wait_type,
4072 but a reply for the overlapping op may not have been
4073 received. In that case we need to fake the appropriate
4074 reply for the overlap op. */
4077 if (is_overlap_cancel(lkb
)) {
4078 wait_type
= DLM_MSG_CANCEL
;
4079 if (lkb
->lkb_grmode
== DLM_LOCK_IV
)
4080 stub_cancel_result
= 0;
4082 if (is_overlap_unlock(lkb
)) {
4083 wait_type
= DLM_MSG_UNLOCK
;
4084 if (lkb
->lkb_grmode
== DLM_LOCK_IV
)
4085 stub_unlock_result
= -ENOENT
;
4088 log_debug(ls
, "rwpre overlap %x %x %d %d %d",
4089 lkb
->lkb_id
, lkb
->lkb_flags
, wait_type
,
4090 stub_cancel_result
, stub_unlock_result
);
4093 switch (wait_type
) {
4095 case DLM_MSG_REQUEST
:
4096 lkb
->lkb_flags
|= DLM_IFL_RESEND
;
4099 case DLM_MSG_CONVERT
:
4100 recover_convert_waiter(ls
, lkb
);
4103 case DLM_MSG_UNLOCK
:
4105 ls
->ls_stub_ms
.m_type
= DLM_MSG_UNLOCK_REPLY
;
4106 ls
->ls_stub_ms
.m_result
= stub_unlock_result
;
4107 ls
->ls_stub_ms
.m_flags
= lkb
->lkb_flags
;
4108 ls
->ls_stub_ms
.m_header
.h_nodeid
= lkb
->lkb_nodeid
;
4109 _receive_unlock_reply(lkb
, &ls
->ls_stub_ms
);
4113 case DLM_MSG_CANCEL
:
4115 ls
->ls_stub_ms
.m_type
= DLM_MSG_CANCEL_REPLY
;
4116 ls
->ls_stub_ms
.m_result
= stub_cancel_result
;
4117 ls
->ls_stub_ms
.m_flags
= lkb
->lkb_flags
;
4118 ls
->ls_stub_ms
.m_header
.h_nodeid
= lkb
->lkb_nodeid
;
4119 _receive_cancel_reply(lkb
, &ls
->ls_stub_ms
);
4124 log_error(ls
, "invalid lkb wait_type %d %d",
4125 lkb
->lkb_wait_type
, wait_type
);
4129 mutex_unlock(&ls
->ls_waiters_mutex
);
4132 static struct dlm_lkb
*find_resend_waiter(struct dlm_ls
*ls
)
4134 struct dlm_lkb
*lkb
;
4137 mutex_lock(&ls
->ls_waiters_mutex
);
4138 list_for_each_entry(lkb
, &ls
->ls_waiters
, lkb_wait_reply
) {
4139 if (lkb
->lkb_flags
& DLM_IFL_RESEND
) {
4145 mutex_unlock(&ls
->ls_waiters_mutex
);
4152 /* Deal with lookups and lkb's marked RESEND from _pre. We may now be the
4153 master or dir-node for r. Processing the lkb may result in it being placed
4156 /* We do this after normal locking has been enabled and any saved messages
4157 (in requestqueue) have been processed. We should be confident that at
4158 this point we won't get or process a reply to any of these waiting
4159 operations. But, new ops may be coming in on the rsbs/locks here from
4160 userspace or remotely. */
4162 /* there may have been an overlap unlock/cancel prior to recovery or after
4163 recovery. if before, the lkb may still have a pos wait_count; if after, the
4164 overlap flag would just have been set and nothing new sent. we can be
4165 confident here than any replies to either the initial op or overlap ops
4166 prior to recovery have been received. */
4168 int dlm_recover_waiters_post(struct dlm_ls
*ls
)
4170 struct dlm_lkb
*lkb
;
4172 int error
= 0, mstype
, err
, oc
, ou
;
4175 if (dlm_locking_stopped(ls
)) {
4176 log_debug(ls
, "recover_waiters_post aborted");
4181 lkb
= find_resend_waiter(ls
);
4185 r
= lkb
->lkb_resource
;
4189 mstype
= lkb
->lkb_wait_type
;
4190 oc
= is_overlap_cancel(lkb
);
4191 ou
= is_overlap_unlock(lkb
);
4194 log_debug(ls
, "recover_waiters_post %x type %d flags %x %s",
4195 lkb
->lkb_id
, mstype
, lkb
->lkb_flags
, r
->res_name
);
4197 /* At this point we assume that we won't get a reply to any
4198 previous op or overlap op on this lock. First, do a big
4199 remove_from_waiters() for all previous ops. */
4201 lkb
->lkb_flags
&= ~DLM_IFL_RESEND
;
4202 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_UNLOCK
;
4203 lkb
->lkb_flags
&= ~DLM_IFL_OVERLAP_CANCEL
;
4204 lkb
->lkb_wait_type
= 0;
4205 lkb
->lkb_wait_count
= 0;
4206 mutex_lock(&ls
->ls_waiters_mutex
);
4207 list_del_init(&lkb
->lkb_wait_reply
);
4208 mutex_unlock(&ls
->ls_waiters_mutex
);
4209 unhold_lkb(lkb
); /* for waiters list */
4212 /* do an unlock or cancel instead of resending */
4214 case DLM_MSG_LOOKUP
:
4215 case DLM_MSG_REQUEST
:
4216 queue_cast(r
, lkb
, ou
? -DLM_EUNLOCK
:
4218 unhold_lkb(lkb
); /* undoes create_lkb() */
4220 case DLM_MSG_CONVERT
:
4222 queue_cast(r
, lkb
, -DLM_ECANCEL
);
4224 lkb
->lkb_exflags
|= DLM_LKF_FORCEUNLOCK
;
4225 _unlock_lock(r
, lkb
);
4233 case DLM_MSG_LOOKUP
:
4234 case DLM_MSG_REQUEST
:
4235 _request_lock(r
, lkb
);
4237 confirm_master(r
, 0);
4239 case DLM_MSG_CONVERT
:
4240 _convert_lock(r
, lkb
);
4248 log_error(ls
, "recover_waiters_post %x %d %x %d %d",
4249 lkb
->lkb_id
, mstype
, lkb
->lkb_flags
, oc
, ou
);
4258 static void purge_queue(struct dlm_rsb
*r
, struct list_head
*queue
,
4259 int (*test
)(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
))
4261 struct dlm_ls
*ls
= r
->res_ls
;
4262 struct dlm_lkb
*lkb
, *safe
;
4264 list_for_each_entry_safe(lkb
, safe
, queue
, lkb_statequeue
) {
4265 if (test(ls
, lkb
)) {
4266 rsb_set_flag(r
, RSB_LOCKS_PURGED
);
4268 /* this put should free the lkb */
4269 if (!dlm_put_lkb(lkb
))
4270 log_error(ls
, "purged lkb not released");
4275 static int purge_dead_test(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
)
4277 return (is_master_copy(lkb
) && dlm_is_removed(ls
, lkb
->lkb_nodeid
));
4280 static int purge_mstcpy_test(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
)
4282 return is_master_copy(lkb
);
4285 static void purge_dead_locks(struct dlm_rsb
*r
)
4287 purge_queue(r
, &r
->res_grantqueue
, &purge_dead_test
);
4288 purge_queue(r
, &r
->res_convertqueue
, &purge_dead_test
);
4289 purge_queue(r
, &r
->res_waitqueue
, &purge_dead_test
);
4292 void dlm_purge_mstcpy_locks(struct dlm_rsb
*r
)
4294 purge_queue(r
, &r
->res_grantqueue
, &purge_mstcpy_test
);
4295 purge_queue(r
, &r
->res_convertqueue
, &purge_mstcpy_test
);
4296 purge_queue(r
, &r
->res_waitqueue
, &purge_mstcpy_test
);
4299 /* Get rid of locks held by nodes that are gone. */
4301 int dlm_purge_locks(struct dlm_ls
*ls
)
4305 log_debug(ls
, "dlm_purge_locks");
4307 down_write(&ls
->ls_root_sem
);
4308 list_for_each_entry(r
, &ls
->ls_root_list
, res_root_list
) {
4312 purge_dead_locks(r
);
4318 up_write(&ls
->ls_root_sem
);
4323 static struct dlm_rsb
*find_purged_rsb(struct dlm_ls
*ls
, int bucket
)
4325 struct dlm_rsb
*r
, *r_ret
= NULL
;
4327 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
4328 list_for_each_entry(r
, &ls
->ls_rsbtbl
[bucket
].list
, res_hashchain
) {
4329 if (!rsb_flag(r
, RSB_LOCKS_PURGED
))
4332 rsb_clear_flag(r
, RSB_LOCKS_PURGED
);
4336 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
4340 void dlm_grant_after_purge(struct dlm_ls
*ls
)
4346 r
= find_purged_rsb(ls
, bucket
);
4348 if (bucket
== ls
->ls_rsbtbl_size
- 1)
4355 grant_pending_locks(r
);
4356 confirm_master(r
, 0);
4364 static struct dlm_lkb
*search_remid_list(struct list_head
*head
, int nodeid
,
4367 struct dlm_lkb
*lkb
;
4369 list_for_each_entry(lkb
, head
, lkb_statequeue
) {
4370 if (lkb
->lkb_nodeid
== nodeid
&& lkb
->lkb_remid
== remid
)
4376 static struct dlm_lkb
*search_remid(struct dlm_rsb
*r
, int nodeid
,
4379 struct dlm_lkb
*lkb
;
4381 lkb
= search_remid_list(&r
->res_grantqueue
, nodeid
, remid
);
4384 lkb
= search_remid_list(&r
->res_convertqueue
, nodeid
, remid
);
4387 lkb
= search_remid_list(&r
->res_waitqueue
, nodeid
, remid
);
4393 /* needs at least dlm_rcom + rcom_lock */
4394 static int receive_rcom_lock_args(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
,
4395 struct dlm_rsb
*r
, struct dlm_rcom
*rc
)
4397 struct rcom_lock
*rl
= (struct rcom_lock
*) rc
->rc_buf
;
4399 lkb
->lkb_nodeid
= rc
->rc_header
.h_nodeid
;
4400 lkb
->lkb_ownpid
= le32_to_cpu(rl
->rl_ownpid
);
4401 lkb
->lkb_remid
= le32_to_cpu(rl
->rl_lkid
);
4402 lkb
->lkb_exflags
= le32_to_cpu(rl
->rl_exflags
);
4403 lkb
->lkb_flags
= le32_to_cpu(rl
->rl_flags
) & 0x0000FFFF;
4404 lkb
->lkb_flags
|= DLM_IFL_MSTCPY
;
4405 lkb
->lkb_lvbseq
= le32_to_cpu(rl
->rl_lvbseq
);
4406 lkb
->lkb_rqmode
= rl
->rl_rqmode
;
4407 lkb
->lkb_grmode
= rl
->rl_grmode
;
4408 /* don't set lkb_status because add_lkb wants to itself */
4410 lkb
->lkb_bastfn
= (rl
->rl_asts
& DLM_CB_BAST
) ? &fake_bastfn
: NULL
;
4411 lkb
->lkb_astfn
= (rl
->rl_asts
& DLM_CB_CAST
) ? &fake_astfn
: NULL
;
4413 if (lkb
->lkb_exflags
& DLM_LKF_VALBLK
) {
4414 int lvblen
= rc
->rc_header
.h_length
- sizeof(struct dlm_rcom
) -
4415 sizeof(struct rcom_lock
);
4416 if (lvblen
> ls
->ls_lvblen
)
4418 lkb
->lkb_lvbptr
= dlm_allocate_lvb(ls
);
4419 if (!lkb
->lkb_lvbptr
)
4421 memcpy(lkb
->lkb_lvbptr
, rl
->rl_lvb
, lvblen
);
4424 /* Conversions between PR and CW (middle modes) need special handling.
4425 The real granted mode of these converting locks cannot be determined
4426 until all locks have been rebuilt on the rsb (recover_conversion) */
4428 if (rl
->rl_wait_type
== cpu_to_le16(DLM_MSG_CONVERT
) &&
4429 middle_conversion(lkb
)) {
4430 rl
->rl_status
= DLM_LKSTS_CONVERT
;
4431 lkb
->lkb_grmode
= DLM_LOCK_IV
;
4432 rsb_set_flag(r
, RSB_RECOVER_CONVERT
);
4438 /* This lkb may have been recovered in a previous aborted recovery so we need
4439 to check if the rsb already has an lkb with the given remote nodeid/lkid.
4440 If so we just send back a standard reply. If not, we create a new lkb with
4441 the given values and send back our lkid. We send back our lkid by sending
4442 back the rcom_lock struct we got but with the remid field filled in. */
4444 /* needs at least dlm_rcom + rcom_lock */
4445 int dlm_recover_master_copy(struct dlm_ls
*ls
, struct dlm_rcom
*rc
)
4447 struct rcom_lock
*rl
= (struct rcom_lock
*) rc
->rc_buf
;
4449 struct dlm_lkb
*lkb
;
4452 if (rl
->rl_parent_lkid
) {
4453 error
= -EOPNOTSUPP
;
4457 error
= find_rsb(ls
, rl
->rl_name
, le16_to_cpu(rl
->rl_namelen
),
4464 lkb
= search_remid(r
, rc
->rc_header
.h_nodeid
, le32_to_cpu(rl
->rl_lkid
));
4470 error
= create_lkb(ls
, &lkb
);
4474 error
= receive_rcom_lock_args(ls
, lkb
, r
, rc
);
4481 add_lkb(r
, lkb
, rl
->rl_status
);
4485 /* this is the new value returned to the lock holder for
4486 saving in its process-copy lkb */
4487 rl
->rl_remid
= cpu_to_le32(lkb
->lkb_id
);
4494 log_debug(ls
, "recover_master_copy %d %x", error
,
4495 le32_to_cpu(rl
->rl_lkid
));
4496 rl
->rl_result
= cpu_to_le32(error
);
4500 /* needs at least dlm_rcom + rcom_lock */
4501 int dlm_recover_process_copy(struct dlm_ls
*ls
, struct dlm_rcom
*rc
)
4503 struct rcom_lock
*rl
= (struct rcom_lock
*) rc
->rc_buf
;
4505 struct dlm_lkb
*lkb
;
4508 error
= find_lkb(ls
, le32_to_cpu(rl
->rl_lkid
), &lkb
);
4510 log_error(ls
, "recover_process_copy no lkid %x",
4511 le32_to_cpu(rl
->rl_lkid
));
4515 DLM_ASSERT(is_process_copy(lkb
), dlm_print_lkb(lkb
););
4517 error
= le32_to_cpu(rl
->rl_result
);
4519 r
= lkb
->lkb_resource
;
4525 /* There's a chance the new master received our lock before
4526 dlm_recover_master_reply(), this wouldn't happen if we did
4527 a barrier between recover_masters and recover_locks. */
4528 log_debug(ls
, "master copy not ready %x r %lx %s", lkb
->lkb_id
,
4529 (unsigned long)r
, r
->res_name
);
4530 dlm_send_rcom_lock(r
, lkb
);
4533 log_debug(ls
, "master copy exists %x", lkb
->lkb_id
);
4536 lkb
->lkb_remid
= le32_to_cpu(rl
->rl_remid
);
4539 log_error(ls
, "dlm_recover_process_copy unknown error %d %x",
4540 error
, lkb
->lkb_id
);
4543 /* an ack for dlm_recover_locks() which waits for replies from
4544 all the locks it sends to new masters */
4545 dlm_recovered_lock(r
);
4554 int dlm_user_request(struct dlm_ls
*ls
, struct dlm_user_args
*ua
,
4555 int mode
, uint32_t flags
, void *name
, unsigned int namelen
,
4556 unsigned long timeout_cs
)
4558 struct dlm_lkb
*lkb
;
4559 struct dlm_args args
;
4562 dlm_lock_recovery(ls
);
4564 error
= create_lkb(ls
, &lkb
);
4570 if (flags
& DLM_LKF_VALBLK
) {
4571 ua
->lksb
.sb_lvbptr
= kzalloc(DLM_USER_LVB_LEN
, GFP_NOFS
);
4572 if (!ua
->lksb
.sb_lvbptr
) {
4580 /* After ua is attached to lkb it will be freed by dlm_free_lkb().
4581 When DLM_IFL_USER is set, the dlm knows that this is a userspace
4582 lock and that lkb_astparam is the dlm_user_args structure. */
4584 error
= set_lock_args(mode
, &ua
->lksb
, flags
, namelen
, timeout_cs
,
4585 fake_astfn
, ua
, fake_bastfn
, &args
);
4586 lkb
->lkb_flags
|= DLM_IFL_USER
;
4593 error
= request_lock(ls
, lkb
, name
, namelen
, &args
);
4609 /* add this new lkb to the per-process list of locks */
4610 spin_lock(&ua
->proc
->locks_spin
);
4612 list_add_tail(&lkb
->lkb_ownqueue
, &ua
->proc
->locks
);
4613 spin_unlock(&ua
->proc
->locks_spin
);
4615 dlm_unlock_recovery(ls
);
4619 int dlm_user_convert(struct dlm_ls
*ls
, struct dlm_user_args
*ua_tmp
,
4620 int mode
, uint32_t flags
, uint32_t lkid
, char *lvb_in
,
4621 unsigned long timeout_cs
)
4623 struct dlm_lkb
*lkb
;
4624 struct dlm_args args
;
4625 struct dlm_user_args
*ua
;
4628 dlm_lock_recovery(ls
);
4630 error
= find_lkb(ls
, lkid
, &lkb
);
4634 /* user can change the params on its lock when it converts it, or
4635 add an lvb that didn't exist before */
4639 if (flags
& DLM_LKF_VALBLK
&& !ua
->lksb
.sb_lvbptr
) {
4640 ua
->lksb
.sb_lvbptr
= kzalloc(DLM_USER_LVB_LEN
, GFP_NOFS
);
4641 if (!ua
->lksb
.sb_lvbptr
) {
4646 if (lvb_in
&& ua
->lksb
.sb_lvbptr
)
4647 memcpy(ua
->lksb
.sb_lvbptr
, lvb_in
, DLM_USER_LVB_LEN
);
4649 ua
->xid
= ua_tmp
->xid
;
4650 ua
->castparam
= ua_tmp
->castparam
;
4651 ua
->castaddr
= ua_tmp
->castaddr
;
4652 ua
->bastparam
= ua_tmp
->bastparam
;
4653 ua
->bastaddr
= ua_tmp
->bastaddr
;
4654 ua
->user_lksb
= ua_tmp
->user_lksb
;
4656 error
= set_lock_args(mode
, &ua
->lksb
, flags
, 0, timeout_cs
,
4657 fake_astfn
, ua
, fake_bastfn
, &args
);
4661 error
= convert_lock(ls
, lkb
, &args
);
4663 if (error
== -EINPROGRESS
|| error
== -EAGAIN
|| error
== -EDEADLK
)
4668 dlm_unlock_recovery(ls
);
4673 int dlm_user_unlock(struct dlm_ls
*ls
, struct dlm_user_args
*ua_tmp
,
4674 uint32_t flags
, uint32_t lkid
, char *lvb_in
)
4676 struct dlm_lkb
*lkb
;
4677 struct dlm_args args
;
4678 struct dlm_user_args
*ua
;
4681 dlm_lock_recovery(ls
);
4683 error
= find_lkb(ls
, lkid
, &lkb
);
4689 if (lvb_in
&& ua
->lksb
.sb_lvbptr
)
4690 memcpy(ua
->lksb
.sb_lvbptr
, lvb_in
, DLM_USER_LVB_LEN
);
4691 if (ua_tmp
->castparam
)
4692 ua
->castparam
= ua_tmp
->castparam
;
4693 ua
->user_lksb
= ua_tmp
->user_lksb
;
4695 error
= set_unlock_args(flags
, ua
, &args
);
4699 error
= unlock_lock(ls
, lkb
, &args
);
4701 if (error
== -DLM_EUNLOCK
)
4703 /* from validate_unlock_args() */
4704 if (error
== -EBUSY
&& (flags
& DLM_LKF_FORCEUNLOCK
))
4709 spin_lock(&ua
->proc
->locks_spin
);
4710 /* dlm_user_add_ast() may have already taken lkb off the proc list */
4711 if (!list_empty(&lkb
->lkb_ownqueue
))
4712 list_move(&lkb
->lkb_ownqueue
, &ua
->proc
->unlocking
);
4713 spin_unlock(&ua
->proc
->locks_spin
);
4717 dlm_unlock_recovery(ls
);
4722 int dlm_user_cancel(struct dlm_ls
*ls
, struct dlm_user_args
*ua_tmp
,
4723 uint32_t flags
, uint32_t lkid
)
4725 struct dlm_lkb
*lkb
;
4726 struct dlm_args args
;
4727 struct dlm_user_args
*ua
;
4730 dlm_lock_recovery(ls
);
4732 error
= find_lkb(ls
, lkid
, &lkb
);
4737 if (ua_tmp
->castparam
)
4738 ua
->castparam
= ua_tmp
->castparam
;
4739 ua
->user_lksb
= ua_tmp
->user_lksb
;
4741 error
= set_unlock_args(flags
, ua
, &args
);
4745 error
= cancel_lock(ls
, lkb
, &args
);
4747 if (error
== -DLM_ECANCEL
)
4749 /* from validate_unlock_args() */
4750 if (error
== -EBUSY
)
4755 dlm_unlock_recovery(ls
);
4760 int dlm_user_deadlock(struct dlm_ls
*ls
, uint32_t flags
, uint32_t lkid
)
4762 struct dlm_lkb
*lkb
;
4763 struct dlm_args args
;
4764 struct dlm_user_args
*ua
;
4768 dlm_lock_recovery(ls
);
4770 error
= find_lkb(ls
, lkid
, &lkb
);
4776 error
= set_unlock_args(flags
, ua
, &args
);
4780 /* same as cancel_lock(), but set DEADLOCK_CANCEL after lock_rsb */
4782 r
= lkb
->lkb_resource
;
4786 error
= validate_unlock_args(lkb
, &args
);
4789 lkb
->lkb_flags
|= DLM_IFL_DEADLOCK_CANCEL
;
4791 error
= _cancel_lock(r
, lkb
);
4796 if (error
== -DLM_ECANCEL
)
4798 /* from validate_unlock_args() */
4799 if (error
== -EBUSY
)
4804 dlm_unlock_recovery(ls
);
4808 /* lkb's that are removed from the waiters list by revert are just left on the
4809 orphans list with the granted orphan locks, to be freed by purge */
4811 static int orphan_proc_lock(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
)
4813 struct dlm_args args
;
4817 mutex_lock(&ls
->ls_orphans_mutex
);
4818 list_add_tail(&lkb
->lkb_ownqueue
, &ls
->ls_orphans
);
4819 mutex_unlock(&ls
->ls_orphans_mutex
);
4821 set_unlock_args(0, lkb
->lkb_ua
, &args
);
4823 error
= cancel_lock(ls
, lkb
, &args
);
4824 if (error
== -DLM_ECANCEL
)
4829 /* The force flag allows the unlock to go ahead even if the lkb isn't granted.
4830 Regardless of what rsb queue the lock is on, it's removed and freed. */
4832 static int unlock_proc_lock(struct dlm_ls
*ls
, struct dlm_lkb
*lkb
)
4834 struct dlm_args args
;
4837 set_unlock_args(DLM_LKF_FORCEUNLOCK
, lkb
->lkb_ua
, &args
);
4839 error
= unlock_lock(ls
, lkb
, &args
);
4840 if (error
== -DLM_EUNLOCK
)
4845 /* We have to release clear_proc_locks mutex before calling unlock_proc_lock()
4846 (which does lock_rsb) due to deadlock with receiving a message that does
4847 lock_rsb followed by dlm_user_add_ast() */
4849 static struct dlm_lkb
*del_proc_lock(struct dlm_ls
*ls
,
4850 struct dlm_user_proc
*proc
)
4852 struct dlm_lkb
*lkb
= NULL
;
4854 mutex_lock(&ls
->ls_clear_proc_locks
);
4855 if (list_empty(&proc
->locks
))
4858 lkb
= list_entry(proc
->locks
.next
, struct dlm_lkb
, lkb_ownqueue
);
4859 list_del_init(&lkb
->lkb_ownqueue
);
4861 if (lkb
->lkb_exflags
& DLM_LKF_PERSISTENT
)
4862 lkb
->lkb_flags
|= DLM_IFL_ORPHAN
;
4864 lkb
->lkb_flags
|= DLM_IFL_DEAD
;
4866 mutex_unlock(&ls
->ls_clear_proc_locks
);
4870 /* The ls_clear_proc_locks mutex protects against dlm_user_add_asts() which
4871 1) references lkb->ua which we free here and 2) adds lkbs to proc->asts,
4872 which we clear here. */
4874 /* proc CLOSING flag is set so no more device_reads should look at proc->asts
4875 list, and no more device_writes should add lkb's to proc->locks list; so we
4876 shouldn't need to take asts_spin or locks_spin here. this assumes that
4877 device reads/writes/closes are serialized -- FIXME: we may need to serialize
4880 void dlm_clear_proc_locks(struct dlm_ls
*ls
, struct dlm_user_proc
*proc
)
4882 struct dlm_lkb
*lkb
, *safe
;
4884 dlm_lock_recovery(ls
);
4887 lkb
= del_proc_lock(ls
, proc
);
4891 if (lkb
->lkb_exflags
& DLM_LKF_PERSISTENT
)
4892 orphan_proc_lock(ls
, lkb
);
4894 unlock_proc_lock(ls
, lkb
);
4896 /* this removes the reference for the proc->locks list
4897 added by dlm_user_request, it may result in the lkb
4903 mutex_lock(&ls
->ls_clear_proc_locks
);
4905 /* in-progress unlocks */
4906 list_for_each_entry_safe(lkb
, safe
, &proc
->unlocking
, lkb_ownqueue
) {
4907 list_del_init(&lkb
->lkb_ownqueue
);
4908 lkb
->lkb_flags
|= DLM_IFL_DEAD
;
4912 list_for_each_entry_safe(lkb
, safe
, &proc
->asts
, lkb_astqueue
) {
4913 memset(&lkb
->lkb_callbacks
, 0,
4914 sizeof(struct dlm_callback
) * DLM_CALLBACKS_SIZE
);
4915 list_del_init(&lkb
->lkb_astqueue
);
4919 mutex_unlock(&ls
->ls_clear_proc_locks
);
4920 dlm_unlock_recovery(ls
);
4923 static void purge_proc_locks(struct dlm_ls
*ls
, struct dlm_user_proc
*proc
)
4925 struct dlm_lkb
*lkb
, *safe
;
4929 spin_lock(&proc
->locks_spin
);
4930 if (!list_empty(&proc
->locks
)) {
4931 lkb
= list_entry(proc
->locks
.next
, struct dlm_lkb
,
4933 list_del_init(&lkb
->lkb_ownqueue
);
4935 spin_unlock(&proc
->locks_spin
);
4940 lkb
->lkb_flags
|= DLM_IFL_DEAD
;
4941 unlock_proc_lock(ls
, lkb
);
4942 dlm_put_lkb(lkb
); /* ref from proc->locks list */
4945 spin_lock(&proc
->locks_spin
);
4946 list_for_each_entry_safe(lkb
, safe
, &proc
->unlocking
, lkb_ownqueue
) {
4947 list_del_init(&lkb
->lkb_ownqueue
);
4948 lkb
->lkb_flags
|= DLM_IFL_DEAD
;
4951 spin_unlock(&proc
->locks_spin
);
4953 spin_lock(&proc
->asts_spin
);
4954 list_for_each_entry_safe(lkb
, safe
, &proc
->asts
, lkb_astqueue
) {
4955 memset(&lkb
->lkb_callbacks
, 0,
4956 sizeof(struct dlm_callback
) * DLM_CALLBACKS_SIZE
);
4957 list_del_init(&lkb
->lkb_astqueue
);
4960 spin_unlock(&proc
->asts_spin
);
4963 /* pid of 0 means purge all orphans */
4965 static void do_purge(struct dlm_ls
*ls
, int nodeid
, int pid
)
4967 struct dlm_lkb
*lkb
, *safe
;
4969 mutex_lock(&ls
->ls_orphans_mutex
);
4970 list_for_each_entry_safe(lkb
, safe
, &ls
->ls_orphans
, lkb_ownqueue
) {
4971 if (pid
&& lkb
->lkb_ownpid
!= pid
)
4973 unlock_proc_lock(ls
, lkb
);
4974 list_del_init(&lkb
->lkb_ownqueue
);
4977 mutex_unlock(&ls
->ls_orphans_mutex
);
4980 static int send_purge(struct dlm_ls
*ls
, int nodeid
, int pid
)
4982 struct dlm_message
*ms
;
4983 struct dlm_mhandle
*mh
;
4986 error
= _create_message(ls
, sizeof(struct dlm_message
), nodeid
,
4987 DLM_MSG_PURGE
, &ms
, &mh
);
4990 ms
->m_nodeid
= nodeid
;
4993 return send_message(mh
, ms
);
4996 int dlm_user_purge(struct dlm_ls
*ls
, struct dlm_user_proc
*proc
,
4997 int nodeid
, int pid
)
5001 if (nodeid
!= dlm_our_nodeid()) {
5002 error
= send_purge(ls
, nodeid
, pid
);
5004 dlm_lock_recovery(ls
);
5005 if (pid
== current
->pid
)
5006 purge_proc_locks(ls
, proc
);
5008 do_purge(ls
, nodeid
, pid
);
5009 dlm_unlock_recovery(ls
);