1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
3 *******************************************************************************
5 ** Copyright (C) 2005-2010 Red Hat, Inc. All rights reserved.
8 *******************************************************************************
9 ******************************************************************************/
11 /* Central locking logic has four stages:
31 Stage 1 (lock, unlock) is mainly about checking input args and
32 splitting into one of the four main operations:
34 dlm_lock = request_lock
35 dlm_lock+CONVERT = convert_lock
36 dlm_unlock = unlock_lock
37 dlm_unlock+CANCEL = cancel_lock
39 Stage 2, xxxx_lock(), just finds and locks the relevant rsb which is
40 provided to the next stage.
42 Stage 3, _xxxx_lock(), determines if the operation is local or remote.
43 When remote, it calls send_xxxx(), when local it calls do_xxxx().
45 Stage 4, do_xxxx(), is the guts of the operation. It manipulates the
46 given rsb and lkb and queues callbacks.
48 For remote operations, send_xxxx() results in the corresponding do_xxxx()
49 function being executed on the remote node. The connecting send/receive
50 calls on local (L) and remote (R) nodes:
52 L: send_xxxx() -> R: receive_xxxx()
54 L: receive_xxxx_reply() <- R: send_xxxx_reply()
56 #include <trace/events/dlm.h>
58 #include <linux/types.h>
59 #include <linux/rbtree.h>
60 #include <linux/slab.h>
61 #include "dlm_internal.h"
62 #include <linux/dlm_device.h>
65 #include "requestqueue.h"
69 #include "lockspace.h"
74 #include "lvb_table.h"
78 static int send_request(struct dlm_rsb *r, struct dlm_lkb *lkb);
79 static int send_convert(struct dlm_rsb *r, struct dlm_lkb *lkb);
80 static int send_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb);
81 static int send_cancel(struct dlm_rsb *r, struct dlm_lkb *lkb);
82 static int send_grant(struct dlm_rsb *r, struct dlm_lkb *lkb);
83 static int send_bast(struct dlm_rsb *r, struct dlm_lkb *lkb, int mode);
84 static int send_lookup(struct dlm_rsb *r, struct dlm_lkb *lkb);
85 static int send_remove(struct dlm_rsb *r);
86 static int _request_lock(struct dlm_rsb *r, struct dlm_lkb *lkb);
87 static int _cancel_lock(struct dlm_rsb *r, struct dlm_lkb *lkb);
88 static void __receive_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
89 const struct dlm_message *ms, bool local);
90 static int receive_extralen(const struct dlm_message *ms);
91 static void do_purge(struct dlm_ls *ls, int nodeid, int pid);
92 static void deactivate_rsb(struct kref *kref);
95 * Lock compatibilty matrix - thanks Steve
96 * UN = Unlocked state. Not really a state, used as a flag
97 * PD = Padding. Used to make the matrix a nice power of two in size
98 * Other states are the same as the VMS DLM.
99 * Usage: matrix[grmode+1][rqmode+1] (although m[rq+1][gr+1] is the same)
102 static const int __dlm_compat_matrix[8][8] = {
103 /* UN NL CR CW PR PW EX PD */
104 {1, 1, 1, 1, 1, 1, 1, 0}, /* UN */
105 {1, 1, 1, 1, 1, 1, 1, 0}, /* NL */
106 {1, 1, 1, 1, 1, 1, 0, 0}, /* CR */
107 {1, 1, 1, 1, 0, 0, 0, 0}, /* CW */
108 {1, 1, 1, 0, 1, 0, 0, 0}, /* PR */
109 {1, 1, 1, 0, 0, 0, 0, 0}, /* PW */
110 {1, 1, 0, 0, 0, 0, 0, 0}, /* EX */
111 {0, 0, 0, 0, 0, 0, 0, 0} /* PD */
115 * This defines the direction of transfer of LVB data.
116 * Granted mode is the row; requested mode is the column.
117 * Usage: matrix[grmode+1][rqmode+1]
118 * 1 = LVB is returned to the caller
119 * 0 = LVB is written to the resource
120 * -1 = nothing happens to the LVB
123 const int dlm_lvb_operations[8][8] = {
124 /* UN NL CR CW PR PW EX PD*/
125 { -1, 1, 1, 1, 1, 1, 1, -1 }, /* UN */
126 { -1, 1, 1, 1, 1, 1, 1, 0 }, /* NL */
127 { -1, -1, 1, 1, 1, 1, 1, 0 }, /* CR */
128 { -1, -1, -1, 1, 1, 1, 1, 0 }, /* CW */
129 { -1, -1, -1, -1, 1, 1, 1, 0 }, /* PR */
130 { -1, 0, 0, 0, 0, 0, 1, 0 }, /* PW */
131 { -1, 0, 0, 0, 0, 0, 0, 0 }, /* EX */
132 { -1, 0, 0, 0, 0, 0, 0, 0 } /* PD */
135 #define modes_compat(gr, rq) \
136 __dlm_compat_matrix[(gr)->lkb_grmode + 1][(rq)->lkb_rqmode + 1]
138 int dlm_modes_compat(int mode1, int mode2)
140 return __dlm_compat_matrix[mode1 + 1][mode2 + 1];
144 * Compatibility matrix for conversions with QUECVT set.
145 * Granted mode is the row; requested mode is the column.
146 * Usage: matrix[grmode+1][rqmode+1]
149 static const int __quecvt_compat_matrix[8][8] = {
150 /* UN NL CR CW PR PW EX PD */
151 {0, 0, 0, 0, 0, 0, 0, 0}, /* UN */
152 {0, 0, 1, 1, 1, 1, 1, 0}, /* NL */
153 {0, 0, 0, 1, 1, 1, 1, 0}, /* CR */
154 {0, 0, 0, 0, 1, 1, 1, 0}, /* CW */
155 {0, 0, 0, 1, 0, 1, 1, 0}, /* PR */
156 {0, 0, 0, 0, 0, 0, 1, 0}, /* PW */
157 {0, 0, 0, 0, 0, 0, 0, 0}, /* EX */
158 {0, 0, 0, 0, 0, 0, 0, 0} /* PD */
161 void dlm_print_lkb(struct dlm_lkb *lkb)
163 printk(KERN_ERR "lkb: nodeid %d id %x remid %x exflags %x flags %x "
164 "sts %d rq %d gr %d wait_type %d wait_nodeid %d seq %llu\n",
165 lkb->lkb_nodeid, lkb->lkb_id, lkb->lkb_remid, lkb->lkb_exflags,
166 dlm_iflags_val(lkb), lkb->lkb_status, lkb->lkb_rqmode,
167 lkb->lkb_grmode, lkb->lkb_wait_type, lkb->lkb_wait_nodeid,
168 (unsigned long long)lkb->lkb_recover_seq);
171 static void dlm_print_rsb(struct dlm_rsb *r)
173 printk(KERN_ERR "rsb: nodeid %d master %d dir %d flags %lx first %x "
175 r->res_nodeid, r->res_master_nodeid, r->res_dir_nodeid,
176 r->res_flags, r->res_first_lkid, r->res_recover_locks_count,
180 void dlm_dump_rsb(struct dlm_rsb *r)
186 printk(KERN_ERR "rsb: root_list empty %d recover_list empty %d\n",
187 list_empty(&r->res_root_list), list_empty(&r->res_recover_list));
188 printk(KERN_ERR "rsb lookup list\n");
189 list_for_each_entry(lkb, &r->res_lookup, lkb_rsb_lookup)
191 printk(KERN_ERR "rsb grant queue:\n");
192 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue)
194 printk(KERN_ERR "rsb convert queue:\n");
195 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue)
197 printk(KERN_ERR "rsb wait queue:\n");
198 list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue)
202 /* Threads cannot use the lockspace while it's being recovered */
204 void dlm_lock_recovery(struct dlm_ls *ls)
206 down_read(&ls->ls_in_recovery);
209 void dlm_unlock_recovery(struct dlm_ls *ls)
211 up_read(&ls->ls_in_recovery);
214 int dlm_lock_recovery_try(struct dlm_ls *ls)
216 return down_read_trylock(&ls->ls_in_recovery);
219 static inline int can_be_queued(struct dlm_lkb *lkb)
221 return !(lkb->lkb_exflags & DLM_LKF_NOQUEUE);
224 static inline int force_blocking_asts(struct dlm_lkb *lkb)
226 return (lkb->lkb_exflags & DLM_LKF_NOQUEUEBAST);
229 static inline int is_demoted(struct dlm_lkb *lkb)
231 return test_bit(DLM_SBF_DEMOTED_BIT, &lkb->lkb_sbflags);
234 static inline int is_altmode(struct dlm_lkb *lkb)
236 return test_bit(DLM_SBF_ALTMODE_BIT, &lkb->lkb_sbflags);
239 static inline int is_granted(struct dlm_lkb *lkb)
241 return (lkb->lkb_status == DLM_LKSTS_GRANTED);
244 static inline int is_remote(struct dlm_rsb *r)
246 DLM_ASSERT(r->res_nodeid >= 0, dlm_print_rsb(r););
247 return !!r->res_nodeid;
250 static inline int is_process_copy(struct dlm_lkb *lkb)
252 return lkb->lkb_nodeid &&
253 !test_bit(DLM_IFL_MSTCPY_BIT, &lkb->lkb_iflags);
256 static inline int is_master_copy(struct dlm_lkb *lkb)
258 return test_bit(DLM_IFL_MSTCPY_BIT, &lkb->lkb_iflags);
261 static inline int middle_conversion(struct dlm_lkb *lkb)
263 if ((lkb->lkb_grmode==DLM_LOCK_PR && lkb->lkb_rqmode==DLM_LOCK_CW) ||
264 (lkb->lkb_rqmode==DLM_LOCK_PR && lkb->lkb_grmode==DLM_LOCK_CW))
269 static inline int down_conversion(struct dlm_lkb *lkb)
271 return (!middle_conversion(lkb) && lkb->lkb_rqmode < lkb->lkb_grmode);
274 static inline int is_overlap_unlock(struct dlm_lkb *lkb)
276 return test_bit(DLM_IFL_OVERLAP_UNLOCK_BIT, &lkb->lkb_iflags);
279 static inline int is_overlap_cancel(struct dlm_lkb *lkb)
281 return test_bit(DLM_IFL_OVERLAP_CANCEL_BIT, &lkb->lkb_iflags);
284 static inline int is_overlap(struct dlm_lkb *lkb)
286 return test_bit(DLM_IFL_OVERLAP_UNLOCK_BIT, &lkb->lkb_iflags) ||
287 test_bit(DLM_IFL_OVERLAP_CANCEL_BIT, &lkb->lkb_iflags);
290 static void queue_cast(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
292 if (is_master_copy(lkb))
295 DLM_ASSERT(lkb->lkb_lksb, dlm_print_lkb(lkb););
297 if (rv == -DLM_ECANCEL &&
298 test_and_clear_bit(DLM_IFL_DEADLOCK_CANCEL_BIT, &lkb->lkb_iflags))
301 dlm_add_cb(lkb, DLM_CB_CAST, lkb->lkb_grmode, rv, dlm_sbflags_val(lkb));
304 static inline void queue_cast_overlap(struct dlm_rsb *r, struct dlm_lkb *lkb)
307 is_overlap_unlock(lkb) ? -DLM_EUNLOCK : -DLM_ECANCEL);
310 static void queue_bast(struct dlm_rsb *r, struct dlm_lkb *lkb, int rqmode)
312 if (is_master_copy(lkb)) {
313 send_bast(r, lkb, rqmode);
315 dlm_add_cb(lkb, DLM_CB_BAST, rqmode, 0, 0);
320 * Basic operations on rsb's and lkb's
323 static inline unsigned long rsb_toss_jiffies(void)
325 return jiffies + (READ_ONCE(dlm_config.ci_toss_secs) * HZ);
328 /* This is only called to add a reference when the code already holds
329 a valid reference to the rsb, so there's no need for locking. */
331 static inline void hold_rsb(struct dlm_rsb *r)
333 /* inactive rsbs are not ref counted */
334 WARN_ON(rsb_flag(r, RSB_INACTIVE));
335 kref_get(&r->res_ref);
338 void dlm_hold_rsb(struct dlm_rsb *r)
343 /* TODO move this to lib/refcount.c */
344 static __must_check bool
345 dlm_refcount_dec_and_write_lock_bh(refcount_t *r, rwlock_t *lock)
346 __cond_acquires(lock)
348 if (refcount_dec_not_one(r))
352 if (!refcount_dec_and_test(r)) {
353 write_unlock_bh(lock);
360 /* TODO move this to include/linux/kref.h */
361 static inline int dlm_kref_put_write_lock_bh(struct kref *kref,
362 void (*release)(struct kref *kref),
365 if (dlm_refcount_dec_and_write_lock_bh(&kref->refcount, lock)) {
373 static void put_rsb(struct dlm_rsb *r)
375 struct dlm_ls *ls = r->res_ls;
378 rv = dlm_kref_put_write_lock_bh(&r->res_ref, deactivate_rsb,
379 &ls->ls_rsbtbl_lock);
381 write_unlock_bh(&ls->ls_rsbtbl_lock);
384 void dlm_put_rsb(struct dlm_rsb *r)
389 /* connected with timer_delete_sync() in dlm_ls_stop() to stop
390 * new timers when recovery is triggered and don't run them
391 * again until a resume_scan_timer() tries it again.
393 static void enable_scan_timer(struct dlm_ls *ls, unsigned long jiffies)
395 if (!dlm_locking_stopped(ls))
396 mod_timer(&ls->ls_scan_timer, jiffies);
399 /* This function tries to resume the timer callback if a rsb
400 * is on the scan list and no timer is pending. It might that
401 * the first entry is on currently executed as timer callback
402 * but we don't care if a timer queued up again and does
403 * nothing. Should be a rare case.
405 void resume_scan_timer(struct dlm_ls *ls)
409 spin_lock_bh(&ls->ls_scan_lock);
410 r = list_first_entry_or_null(&ls->ls_scan_list, struct dlm_rsb,
412 if (r && !timer_pending(&ls->ls_scan_timer))
413 enable_scan_timer(ls, r->res_toss_time);
414 spin_unlock_bh(&ls->ls_scan_lock);
417 /* ls_rsbtbl_lock must be held */
419 static void del_scan(struct dlm_ls *ls, struct dlm_rsb *r)
421 struct dlm_rsb *first;
423 /* active rsbs should never be on the scan list */
424 WARN_ON(!rsb_flag(r, RSB_INACTIVE));
426 spin_lock_bh(&ls->ls_scan_lock);
427 r->res_toss_time = 0;
429 /* if the rsb is not queued do nothing */
430 if (list_empty(&r->res_scan_list))
433 /* get the first element before delete */
434 first = list_first_entry(&ls->ls_scan_list, struct dlm_rsb,
436 list_del_init(&r->res_scan_list);
437 /* check if the first element was the rsb we deleted */
439 /* try to get the new first element, if the list
440 * is empty now try to delete the timer, if we are
441 * too late we don't care.
443 * if the list isn't empty and a new first element got
444 * in place, set the new timer expire time.
446 first = list_first_entry_or_null(&ls->ls_scan_list, struct dlm_rsb,
449 timer_delete(&ls->ls_scan_timer);
451 enable_scan_timer(ls, first->res_toss_time);
455 spin_unlock_bh(&ls->ls_scan_lock);
458 static void add_scan(struct dlm_ls *ls, struct dlm_rsb *r)
460 int our_nodeid = dlm_our_nodeid();
461 struct dlm_rsb *first;
463 /* A dir record for a remote master rsb should never be on the scan list. */
464 WARN_ON(!dlm_no_directory(ls) &&
465 (r->res_master_nodeid != our_nodeid) &&
466 (dlm_dir_nodeid(r) == our_nodeid));
468 /* An active rsb should never be on the scan list. */
469 WARN_ON(!rsb_flag(r, RSB_INACTIVE));
471 /* An rsb should not already be on the scan list. */
472 WARN_ON(!list_empty(&r->res_scan_list));
474 spin_lock_bh(&ls->ls_scan_lock);
475 /* set the new rsb absolute expire time in the rsb */
476 r->res_toss_time = rsb_toss_jiffies();
477 if (list_empty(&ls->ls_scan_list)) {
478 /* if the queue is empty add the element and it's
479 * our new expire time
481 list_add_tail(&r->res_scan_list, &ls->ls_scan_list);
482 enable_scan_timer(ls, r->res_toss_time);
484 /* try to get the maybe new first element and then add
485 * to this rsb with the oldest expire time to the end
486 * of the queue. If the list was empty before this
487 * rsb expire time is our next expiration if it wasn't
488 * the now new first elemet is our new expiration time
490 first = list_first_entry_or_null(&ls->ls_scan_list, struct dlm_rsb,
492 list_add_tail(&r->res_scan_list, &ls->ls_scan_list);
494 enable_scan_timer(ls, r->res_toss_time);
496 enable_scan_timer(ls, first->res_toss_time);
498 spin_unlock_bh(&ls->ls_scan_lock);
501 /* if we hit contention we do in 250 ms a retry to trylock.
502 * if there is any other mod_timer in between we don't care
503 * about that it expires earlier again this is only for the
504 * unlikely case nothing happened in this time.
506 #define DLM_TOSS_TIMER_RETRY (jiffies + msecs_to_jiffies(250))
508 /* Called by lockspace scan_timer to free unused rsb's. */
510 void dlm_rsb_scan(struct timer_list *timer)
512 struct dlm_ls *ls = from_timer(ls, timer, ls_scan_timer);
513 int our_nodeid = dlm_our_nodeid();
518 /* interrupting point to leave iteration when
519 * recovery waits for timer_delete_sync(), recovery
520 * will take care to delete everything in scan list.
522 if (dlm_locking_stopped(ls))
525 rv = spin_trylock(&ls->ls_scan_lock);
527 /* rearm again try timer */
528 enable_scan_timer(ls, DLM_TOSS_TIMER_RETRY);
532 r = list_first_entry_or_null(&ls->ls_scan_list, struct dlm_rsb,
535 /* the next add_scan will enable the timer again */
536 spin_unlock(&ls->ls_scan_lock);
541 * If the first rsb is not yet expired, then stop because the
542 * list is sorted with nearest expiration first.
544 if (time_before(jiffies, r->res_toss_time)) {
545 /* rearm with the next rsb to expire in the future */
546 enable_scan_timer(ls, r->res_toss_time);
547 spin_unlock(&ls->ls_scan_lock);
551 /* in find_rsb_dir/nodir there is a reverse order of this
552 * lock, however this is only a trylock if we hit some
553 * possible contention we try it again.
555 rv = write_trylock(&ls->ls_rsbtbl_lock);
557 spin_unlock(&ls->ls_scan_lock);
558 /* rearm again try timer */
559 enable_scan_timer(ls, DLM_TOSS_TIMER_RETRY);
563 list_del(&r->res_slow_list);
564 rhashtable_remove_fast(&ls->ls_rsbtbl, &r->res_node,
565 dlm_rhash_rsb_params);
566 rsb_clear_flag(r, RSB_HASHED);
568 /* ls_rsbtbl_lock is not needed when calling send_remove() */
569 write_unlock(&ls->ls_rsbtbl_lock);
571 list_del_init(&r->res_scan_list);
572 spin_unlock(&ls->ls_scan_lock);
574 /* An rsb that is a dir record for a remote master rsb
575 * cannot be removed, and should not have a timer enabled.
577 WARN_ON(!dlm_no_directory(ls) &&
578 (r->res_master_nodeid != our_nodeid) &&
579 (dlm_dir_nodeid(r) == our_nodeid));
581 /* We're the master of this rsb but we're not
582 * the directory record, so we need to tell the
583 * dir node to remove the dir record
585 if (!dlm_no_directory(ls) &&
586 (r->res_master_nodeid == our_nodeid) &&
587 (dlm_dir_nodeid(r) != our_nodeid))
590 free_inactive_rsb(r);
594 /* If ls->ls_new_rsb is empty, return -EAGAIN, so the caller can
595 unlock any spinlocks, go back and call pre_rsb_struct again.
596 Otherwise, take an rsb off the list and return it. */
598 static int get_rsb_struct(struct dlm_ls *ls, const void *name, int len,
599 struct dlm_rsb **r_ret)
603 r = dlm_allocate_rsb(ls);
609 memcpy(r->res_name, name, len);
610 spin_lock_init(&r->res_lock);
612 INIT_LIST_HEAD(&r->res_lookup);
613 INIT_LIST_HEAD(&r->res_grantqueue);
614 INIT_LIST_HEAD(&r->res_convertqueue);
615 INIT_LIST_HEAD(&r->res_waitqueue);
616 INIT_LIST_HEAD(&r->res_root_list);
617 INIT_LIST_HEAD(&r->res_scan_list);
618 INIT_LIST_HEAD(&r->res_recover_list);
619 INIT_LIST_HEAD(&r->res_masters_list);
625 int dlm_search_rsb_tree(struct rhashtable *rhash, const void *name, int len,
626 struct dlm_rsb **r_ret)
628 char key[DLM_RESNAME_MAXLEN] = {};
630 memcpy(key, name, len);
631 *r_ret = rhashtable_lookup_fast(rhash, &key, dlm_rhash_rsb_params);
638 static int rsb_insert(struct dlm_rsb *rsb, struct rhashtable *rhash)
642 rv = rhashtable_insert_fast(rhash, &rsb->res_node,
643 dlm_rhash_rsb_params);
645 rsb_set_flag(rsb, RSB_HASHED);
651 * Find rsb in rsbtbl and potentially create/add one
653 * Delaying the release of rsb's has a similar benefit to applications keeping
654 * NL locks on an rsb, but without the guarantee that the cached master value
655 * will still be valid when the rsb is reused. Apps aren't always smart enough
656 * to keep NL locks on an rsb that they may lock again shortly; this can lead
657 * to excessive master lookups and removals if we don't delay the release.
659 * Searching for an rsb means looking through both the normal list and toss
660 * list. When found on the toss list the rsb is moved to the normal list with
661 * ref count of 1; when found on normal list the ref count is incremented.
663 * rsb's on the keep list are being used locally and refcounted.
664 * rsb's on the toss list are not being used locally, and are not refcounted.
666 * The toss list rsb's were either
667 * - previously used locally but not any more (were on keep list, then
668 * moved to toss list when last refcount dropped)
669 * - created and put on toss list as a directory record for a lookup
670 * (we are the dir node for the res, but are not using the res right now,
671 * but some other node is)
673 * The purpose of find_rsb() is to return a refcounted rsb for local use.
674 * So, if the given rsb is on the toss list, it is moved to the keep list
675 * before being returned.
677 * deactivate_rsb() happens when all local usage of the rsb is done, i.e. no
678 * more refcounts exist, so the rsb is moved from the keep list to the
681 * rsb's on both keep and toss lists are used for doing a name to master
682 * lookups. rsb's that are in use locally (and being refcounted) are on
683 * the keep list, rsb's that are not in use locally (not refcounted) and
684 * only exist for name/master lookups are on the toss list.
686 * rsb's on the toss list who's dir_nodeid is not local can have stale
687 * name/master mappings. So, remote requests on such rsb's can potentially
688 * return with an error, which means the mapping is stale and needs to
689 * be updated with a new lookup. (The idea behind MASTER UNCERTAIN and
690 * first_lkid is to keep only a single outstanding request on an rsb
691 * while that rsb has a potentially stale master.)
694 static int find_rsb_dir(struct dlm_ls *ls, const void *name, int len,
695 uint32_t hash, int dir_nodeid, int from_nodeid,
696 unsigned int flags, struct dlm_rsb **r_ret)
698 struct dlm_rsb *r = NULL;
699 int our_nodeid = dlm_our_nodeid();
706 if (flags & R_RECEIVE_REQUEST) {
707 if (from_nodeid == dir_nodeid)
711 } else if (flags & R_REQUEST) {
716 * flags & R_RECEIVE_RECOVER is from dlm_recover_master_copy, so
717 * from_nodeid has sent us a lock in dlm_recover_locks, believing
718 * we're the new master. Our local recovery may not have set
719 * res_master_nodeid to our_nodeid yet, so allow either. Don't
720 * create the rsb; dlm_recover_process_copy() will handle EBADR
723 * If someone sends us a request, we are the dir node, and we do
724 * not find the rsb anywhere, then recreate it. This happens if
725 * someone sends us a request after we have removed/freed an rsb.
726 * (They sent a request instead of lookup because they are using
727 * an rsb taken from their scan list.)
730 if (from_local || from_dir ||
731 (from_other && (dir_nodeid == our_nodeid))) {
737 /* check if the rsb is active under read lock - likely path */
738 read_lock_bh(&ls->ls_rsbtbl_lock);
739 error = dlm_search_rsb_tree(&ls->ls_rsbtbl, name, len, &r);
741 read_unlock_bh(&ls->ls_rsbtbl_lock);
746 * rsb is active, so we can't check master_nodeid without lock_rsb.
749 if (rsb_flag(r, RSB_INACTIVE)) {
750 read_unlock_bh(&ls->ls_rsbtbl_lock);
754 kref_get(&r->res_ref);
755 read_unlock_bh(&ls->ls_rsbtbl_lock);
760 write_lock_bh(&ls->ls_rsbtbl_lock);
763 * The expectation here is that the rsb will have HASHED and
764 * INACTIVE flags set, and that the rsb can be moved from
765 * inactive back to active again. However, between releasing
766 * the read lock and acquiring the write lock, this rsb could
767 * have been removed from rsbtbl, and had HASHED cleared, to
768 * be freed. To deal with this case, we would normally need
769 * to repeat dlm_search_rsb_tree while holding the write lock,
770 * but rcu allows us to simply check the HASHED flag, because
771 * the rcu read lock means the rsb will not be freed yet.
772 * If the HASHED flag is not set, then the rsb is being freed,
773 * so we add a new rsb struct. If the HASHED flag is set,
774 * and INACTIVE is not set, it means another thread has
775 * made the rsb active, as we're expecting to do here, and
776 * we just repeat the lookup (this will be very unlikely.)
778 if (rsb_flag(r, RSB_HASHED)) {
779 if (!rsb_flag(r, RSB_INACTIVE)) {
780 write_unlock_bh(&ls->ls_rsbtbl_lock);
784 write_unlock_bh(&ls->ls_rsbtbl_lock);
789 * rsb found inactive (master_nodeid may be out of date unless
790 * we are the dir_nodeid or were the master) No other thread
791 * is using this rsb because it's inactive, so we can
792 * look at or update res_master_nodeid without lock_rsb.
795 if ((r->res_master_nodeid != our_nodeid) && from_other) {
796 /* our rsb was not master, and another node (not the dir node)
797 has sent us a request */
798 log_debug(ls, "find_rsb inactive from_other %d master %d dir %d %s",
799 from_nodeid, r->res_master_nodeid, dir_nodeid,
801 write_unlock_bh(&ls->ls_rsbtbl_lock);
806 if ((r->res_master_nodeid != our_nodeid) && from_dir) {
807 /* don't think this should ever happen */
808 log_error(ls, "find_rsb inactive from_dir %d master %d",
809 from_nodeid, r->res_master_nodeid);
811 /* fix it and go on */
812 r->res_master_nodeid = our_nodeid;
814 rsb_clear_flag(r, RSB_MASTER_UNCERTAIN);
815 r->res_first_lkid = 0;
818 if (from_local && (r->res_master_nodeid != our_nodeid)) {
819 /* Because we have held no locks on this rsb,
820 res_master_nodeid could have become stale. */
821 rsb_set_flag(r, RSB_MASTER_UNCERTAIN);
822 r->res_first_lkid = 0;
825 /* A dir record will not be on the scan list. */
826 if (r->res_dir_nodeid != our_nodeid)
828 list_move(&r->res_slow_list, &ls->ls_slow_active);
829 rsb_clear_flag(r, RSB_INACTIVE);
830 kref_init(&r->res_ref); /* ref is now used in active state */
831 write_unlock_bh(&ls->ls_rsbtbl_lock);
841 if (error == -EBADR && !create)
844 error = get_rsb_struct(ls, name, len, &r);
845 if (WARN_ON_ONCE(error))
849 r->res_dir_nodeid = dir_nodeid;
850 kref_init(&r->res_ref);
853 /* want to see how often this happens */
854 log_debug(ls, "find_rsb new from_dir %d recreate %s",
855 from_nodeid, r->res_name);
856 r->res_master_nodeid = our_nodeid;
861 if (from_other && (dir_nodeid != our_nodeid)) {
862 /* should never happen */
863 log_error(ls, "find_rsb new from_other %d dir %d our %d %s",
864 from_nodeid, dir_nodeid, our_nodeid, r->res_name);
872 log_debug(ls, "find_rsb new from_other %d dir %d %s",
873 from_nodeid, dir_nodeid, r->res_name);
876 if (dir_nodeid == our_nodeid) {
877 /* When we are the dir nodeid, we can set the master
879 r->res_master_nodeid = our_nodeid;
882 /* set_master will send_lookup to dir_nodeid */
883 r->res_master_nodeid = 0;
889 write_lock_bh(&ls->ls_rsbtbl_lock);
890 error = rsb_insert(r, &ls->ls_rsbtbl);
891 if (error == -EEXIST) {
892 /* somebody else was faster and it seems the
893 * rsb exists now, we do a whole relookup
895 write_unlock_bh(&ls->ls_rsbtbl_lock);
899 list_add(&r->res_slow_list, &ls->ls_slow_active);
901 write_unlock_bh(&ls->ls_rsbtbl_lock);
907 /* During recovery, other nodes can send us new MSTCPY locks (from
908 dlm_recover_locks) before we've made ourself master (in
909 dlm_recover_masters). */
911 static int find_rsb_nodir(struct dlm_ls *ls, const void *name, int len,
912 uint32_t hash, int dir_nodeid, int from_nodeid,
913 unsigned int flags, struct dlm_rsb **r_ret)
915 struct dlm_rsb *r = NULL;
916 int our_nodeid = dlm_our_nodeid();
917 int recover = (flags & R_RECEIVE_RECOVER);
922 /* check if the rsb is in active state under read lock - likely path */
923 read_lock_bh(&ls->ls_rsbtbl_lock);
924 error = dlm_search_rsb_tree(&ls->ls_rsbtbl, name, len, &r);
926 read_unlock_bh(&ls->ls_rsbtbl_lock);
930 if (rsb_flag(r, RSB_INACTIVE)) {
931 read_unlock_bh(&ls->ls_rsbtbl_lock);
936 * rsb is active, so we can't check master_nodeid without lock_rsb.
939 kref_get(&r->res_ref);
940 read_unlock_bh(&ls->ls_rsbtbl_lock);
946 write_lock_bh(&ls->ls_rsbtbl_lock);
948 /* See comment in find_rsb_dir. */
949 if (rsb_flag(r, RSB_HASHED)) {
950 if (!rsb_flag(r, RSB_INACTIVE)) {
951 write_unlock_bh(&ls->ls_rsbtbl_lock);
955 write_unlock_bh(&ls->ls_rsbtbl_lock);
961 * rsb found inactive. No other thread is using this rsb because
962 * it's inactive, so we can look at or update res_master_nodeid
966 if (!recover && (r->res_master_nodeid != our_nodeid) && from_nodeid) {
967 /* our rsb is not master, and another node has sent us a
968 request; this should never happen */
969 log_error(ls, "find_rsb inactive from_nodeid %d master %d dir %d",
970 from_nodeid, r->res_master_nodeid, dir_nodeid);
972 write_unlock_bh(&ls->ls_rsbtbl_lock);
977 if (!recover && (r->res_master_nodeid != our_nodeid) &&
978 (dir_nodeid == our_nodeid)) {
979 /* our rsb is not master, and we are dir; may as well fix it;
980 this should never happen */
981 log_error(ls, "find_rsb inactive our %d master %d dir %d",
982 our_nodeid, r->res_master_nodeid, dir_nodeid);
984 r->res_master_nodeid = our_nodeid;
988 list_move(&r->res_slow_list, &ls->ls_slow_active);
989 rsb_clear_flag(r, RSB_INACTIVE);
990 kref_init(&r->res_ref);
992 write_unlock_bh(&ls->ls_rsbtbl_lock);
1002 error = get_rsb_struct(ls, name, len, &r);
1003 if (WARN_ON_ONCE(error))
1007 r->res_dir_nodeid = dir_nodeid;
1008 r->res_master_nodeid = dir_nodeid;
1009 r->res_nodeid = (dir_nodeid == our_nodeid) ? 0 : dir_nodeid;
1010 kref_init(&r->res_ref);
1012 write_lock_bh(&ls->ls_rsbtbl_lock);
1013 error = rsb_insert(r, &ls->ls_rsbtbl);
1014 if (error == -EEXIST) {
1015 /* somebody else was faster and it seems the
1016 * rsb exists now, we do a whole relookup
1018 write_unlock_bh(&ls->ls_rsbtbl_lock);
1021 } else if (!error) {
1022 list_add(&r->res_slow_list, &ls->ls_slow_active);
1024 write_unlock_bh(&ls->ls_rsbtbl_lock);
1034 * While rcu read lock is held, the rsb cannot be freed,
1035 * which allows a lookup optimization.
1037 * Two threads are accessing the same rsb concurrently,
1038 * the first (A) is trying to use the rsb, the second (B)
1039 * is trying to free the rsb.
1042 * (trying to use rsb) (trying to free rsb)
1045 * A2. rsbtbl read lock
1046 * A3. look up rsb in rsbtbl
1047 * A4. rsbtbl read unlock
1048 * B1. rsbtbl write lock
1049 * B2. look up rsb in rsbtbl
1050 * B3. remove rsb from rsbtbl
1051 * B4. clear rsb HASHED flag
1052 * B5. rsbtbl write unlock
1053 * B6. begin freeing rsb using rcu...
1055 * (rsb is inactive, so try to make it active again)
1056 * A5. read rsb HASHED flag (safe because rsb is not freed yet)
1057 * A6. the rsb HASHED flag is not set, which it means the rsb
1058 * is being removed from rsbtbl and freed, so don't use it.
1059 * A7. rcu read unlock
1061 * B7. ...finish freeing rsb using rcu
1062 * A8. create a new rsb
1064 * Without the rcu optimization, steps A5-8 would need to do
1065 * an extra rsbtbl lookup:
1066 * A5. rsbtbl write lock
1067 * A6. look up rsb in rsbtbl, not found
1068 * A7. rsbtbl write unlock
1069 * A8. create a new rsb
1072 static int find_rsb(struct dlm_ls *ls, const void *name, int len,
1073 int from_nodeid, unsigned int flags,
1074 struct dlm_rsb **r_ret)
1080 if (len > DLM_RESNAME_MAXLEN)
1083 hash = jhash(name, len, 0);
1084 dir_nodeid = dlm_hash2nodeid(ls, hash);
1087 if (dlm_no_directory(ls))
1088 rv = find_rsb_nodir(ls, name, len, hash, dir_nodeid,
1089 from_nodeid, flags, r_ret);
1091 rv = find_rsb_dir(ls, name, len, hash, dir_nodeid,
1092 from_nodeid, flags, r_ret);
1097 /* we have received a request and found that res_master_nodeid != our_nodeid,
1098 so we need to return an error or make ourself the master */
1100 static int validate_master_nodeid(struct dlm_ls *ls, struct dlm_rsb *r,
1103 if (dlm_no_directory(ls)) {
1104 log_error(ls, "find_rsb keep from_nodeid %d master %d dir %d",
1105 from_nodeid, r->res_master_nodeid,
1111 if (from_nodeid != r->res_dir_nodeid) {
1112 /* our rsb is not master, and another node (not the dir node)
1113 has sent us a request. this is much more common when our
1114 master_nodeid is zero, so limit debug to non-zero. */
1116 if (r->res_master_nodeid) {
1117 log_debug(ls, "validate master from_other %d master %d "
1118 "dir %d first %x %s", from_nodeid,
1119 r->res_master_nodeid, r->res_dir_nodeid,
1120 r->res_first_lkid, r->res_name);
1124 /* our rsb is not master, but the dir nodeid has sent us a
1125 request; this could happen with master 0 / res_nodeid -1 */
1127 if (r->res_master_nodeid) {
1128 log_error(ls, "validate master from_dir %d master %d "
1130 from_nodeid, r->res_master_nodeid,
1131 r->res_first_lkid, r->res_name);
1134 r->res_master_nodeid = dlm_our_nodeid();
1140 static void __dlm_master_lookup(struct dlm_ls *ls, struct dlm_rsb *r, int our_nodeid,
1141 int from_nodeid, bool is_inactive, unsigned int flags,
1142 int *r_nodeid, int *result)
1144 int fix_master = (flags & DLM_LU_RECOVER_MASTER);
1145 int from_master = (flags & DLM_LU_RECOVER_DIR);
1147 if (r->res_dir_nodeid != our_nodeid) {
1148 /* should not happen, but may as well fix it and carry on */
1149 log_error(ls, "%s res_dir %d our %d %s", __func__,
1150 r->res_dir_nodeid, our_nodeid, r->res_name);
1151 r->res_dir_nodeid = our_nodeid;
1154 if (fix_master && dlm_is_removed(ls, r->res_master_nodeid)) {
1155 /* Recovery uses this function to set a new master when
1156 * the previous master failed. Setting NEW_MASTER will
1157 * force dlm_recover_masters to call recover_master on this
1158 * rsb even though the res_nodeid is no longer removed.
1161 r->res_master_nodeid = from_nodeid;
1162 r->res_nodeid = from_nodeid;
1163 rsb_set_flag(r, RSB_NEW_MASTER);
1166 /* I don't think we should ever find it inactive. */
1167 log_error(ls, "%s fix_master inactive", __func__);
1172 if (from_master && (r->res_master_nodeid != from_nodeid)) {
1173 /* this will happen if from_nodeid became master during
1174 * a previous recovery cycle, and we aborted the previous
1175 * cycle before recovering this master value
1178 log_limit(ls, "%s from_master %d master_nodeid %d res_nodeid %d first %x %s",
1179 __func__, from_nodeid, r->res_master_nodeid,
1180 r->res_nodeid, r->res_first_lkid, r->res_name);
1182 if (r->res_master_nodeid == our_nodeid) {
1183 log_error(ls, "from_master %d our_master", from_nodeid);
1188 r->res_master_nodeid = from_nodeid;
1189 r->res_nodeid = from_nodeid;
1190 rsb_set_flag(r, RSB_NEW_MASTER);
1193 if (!r->res_master_nodeid) {
1194 /* this will happen if recovery happens while we're looking
1195 * up the master for this rsb
1198 log_debug(ls, "%s master 0 to %d first %x %s", __func__,
1199 from_nodeid, r->res_first_lkid, r->res_name);
1200 r->res_master_nodeid = from_nodeid;
1201 r->res_nodeid = from_nodeid;
1204 if (!from_master && !fix_master &&
1205 (r->res_master_nodeid == from_nodeid)) {
1206 /* this can happen when the master sends remove, the dir node
1207 * finds the rsb on the active list and ignores the remove,
1208 * and the former master sends a lookup
1211 log_limit(ls, "%s from master %d flags %x first %x %s",
1212 __func__, from_nodeid, flags, r->res_first_lkid,
1217 *r_nodeid = r->res_master_nodeid;
1219 *result = DLM_LU_MATCH;
1223 * We're the dir node for this res and another node wants to know the
1224 * master nodeid. During normal operation (non recovery) this is only
1225 * called from receive_lookup(); master lookups when the local node is
1226 * the dir node are done by find_rsb().
1228 * normal operation, we are the dir node for a resource
1233 * . dlm_master_lookup flags 0
1235 * recover directory, we are rebuilding dir for all resources
1236 * . dlm_recover_directory
1238 * remote node sends back the rsb names it is master of and we are dir of
1239 * . dlm_master_lookup RECOVER_DIR (fix_master 0, from_master 1)
1240 * we either create new rsb setting remote node as master, or find existing
1241 * rsb and set master to be the remote node.
1243 * recover masters, we are finding the new master for resources
1244 * . dlm_recover_masters
1246 * . dlm_send_rcom_lookup
1247 * . receive_rcom_lookup
1248 * . dlm_master_lookup RECOVER_MASTER (fix_master 1, from_master 0)
1251 static int _dlm_master_lookup(struct dlm_ls *ls, int from_nodeid, const char *name,
1252 int len, unsigned int flags, int *r_nodeid, int *result)
1254 struct dlm_rsb *r = NULL;
1256 int our_nodeid = dlm_our_nodeid();
1257 int dir_nodeid, error;
1259 if (len > DLM_RESNAME_MAXLEN)
1262 if (from_nodeid == our_nodeid) {
1263 log_error(ls, "dlm_master_lookup from our_nodeid %d flags %x",
1268 hash = jhash(name, len, 0);
1269 dir_nodeid = dlm_hash2nodeid(ls, hash);
1270 if (dir_nodeid != our_nodeid) {
1271 log_error(ls, "dlm_master_lookup from %d dir %d our %d h %x %d",
1272 from_nodeid, dir_nodeid, our_nodeid, hash,
1280 /* check if the rsb is active under read lock - likely path */
1281 read_lock_bh(&ls->ls_rsbtbl_lock);
1282 error = dlm_search_rsb_tree(&ls->ls_rsbtbl, name, len, &r);
1284 if (rsb_flag(r, RSB_INACTIVE)) {
1285 read_unlock_bh(&ls->ls_rsbtbl_lock);
1289 /* because the rsb is active, we need to lock_rsb before
1290 * checking/changing re_master_nodeid
1294 read_unlock_bh(&ls->ls_rsbtbl_lock);
1297 __dlm_master_lookup(ls, r, our_nodeid, from_nodeid, false,
1298 flags, r_nodeid, result);
1300 /* the rsb was active */
1306 read_unlock_bh(&ls->ls_rsbtbl_lock);
1311 /* unlikely path - relookup under write */
1312 write_lock_bh(&ls->ls_rsbtbl_lock);
1314 error = dlm_search_rsb_tree(&ls->ls_rsbtbl, name, len, &r);
1316 if (!rsb_flag(r, RSB_INACTIVE)) {
1317 write_unlock_bh(&ls->ls_rsbtbl_lock);
1318 /* something as changed, very unlikely but
1324 write_unlock_bh(&ls->ls_rsbtbl_lock);
1328 /* because the rsb is inactive, it's not refcounted and lock_rsb
1329 is not used, but is protected by the rsbtbl lock */
1331 __dlm_master_lookup(ls, r, our_nodeid, from_nodeid, true, flags,
1334 /* A dir record rsb should never be on scan list. */
1335 /* Try to fix this with del_scan? */
1336 WARN_ON(!list_empty(&r->res_scan_list));
1338 write_unlock_bh(&ls->ls_rsbtbl_lock);
1343 error = get_rsb_struct(ls, name, len, &r);
1344 if (WARN_ON_ONCE(error))
1348 r->res_dir_nodeid = our_nodeid;
1349 r->res_master_nodeid = from_nodeid;
1350 r->res_nodeid = from_nodeid;
1351 rsb_set_flag(r, RSB_INACTIVE);
1353 write_lock_bh(&ls->ls_rsbtbl_lock);
1354 error = rsb_insert(r, &ls->ls_rsbtbl);
1355 if (error == -EEXIST) {
1356 /* somebody else was faster and it seems the
1357 * rsb exists now, we do a whole relookup
1359 write_unlock_bh(&ls->ls_rsbtbl_lock);
1363 write_unlock_bh(&ls->ls_rsbtbl_lock);
1364 /* should never happen */
1369 list_add(&r->res_slow_list, &ls->ls_slow_inactive);
1370 write_unlock_bh(&ls->ls_rsbtbl_lock);
1373 *result = DLM_LU_ADD;
1374 *r_nodeid = from_nodeid;
1379 int dlm_master_lookup(struct dlm_ls *ls, int from_nodeid, const char *name,
1380 int len, unsigned int flags, int *r_nodeid, int *result)
1384 rv = _dlm_master_lookup(ls, from_nodeid, name, len, flags, r_nodeid, result);
1389 static void dlm_dump_rsb_hash(struct dlm_ls *ls, uint32_t hash)
1393 read_lock_bh(&ls->ls_rsbtbl_lock);
1394 list_for_each_entry(r, &ls->ls_slow_active, res_slow_list) {
1395 if (r->res_hash == hash)
1398 read_unlock_bh(&ls->ls_rsbtbl_lock);
1401 void dlm_dump_rsb_name(struct dlm_ls *ls, const char *name, int len)
1403 struct dlm_rsb *r = NULL;
1406 read_lock_bh(&ls->ls_rsbtbl_lock);
1407 error = dlm_search_rsb_tree(&ls->ls_rsbtbl, name, len, &r);
1413 read_unlock_bh(&ls->ls_rsbtbl_lock);
1416 static void deactivate_rsb(struct kref *kref)
1418 struct dlm_rsb *r = container_of(kref, struct dlm_rsb, res_ref);
1419 struct dlm_ls *ls = r->res_ls;
1420 int our_nodeid = dlm_our_nodeid();
1422 DLM_ASSERT(list_empty(&r->res_root_list), dlm_print_rsb(r););
1423 rsb_set_flag(r, RSB_INACTIVE);
1424 list_move(&r->res_slow_list, &ls->ls_slow_inactive);
1427 * When the rsb becomes unused:
1428 * - If it's not a dir record for a remote master rsb,
1429 * then it is put on the scan list to be freed.
1430 * - If it's a dir record for a remote master rsb,
1431 * then it is kept in the inactive state until
1432 * receive_remove() from the master node.
1434 if (!dlm_no_directory(ls) &&
1435 (r->res_master_nodeid != our_nodeid) &&
1436 (dlm_dir_nodeid(r) != our_nodeid))
1439 if (r->res_lvbptr) {
1440 dlm_free_lvb(r->res_lvbptr);
1441 r->res_lvbptr = NULL;
1445 /* See comment for unhold_lkb */
1447 static void unhold_rsb(struct dlm_rsb *r)
1451 /* inactive rsbs are not ref counted */
1452 WARN_ON(rsb_flag(r, RSB_INACTIVE));
1453 rv = kref_put(&r->res_ref, deactivate_rsb);
1454 DLM_ASSERT(!rv, dlm_dump_rsb(r););
1457 void free_inactive_rsb(struct dlm_rsb *r)
1459 WARN_ON_ONCE(!rsb_flag(r, RSB_INACTIVE));
1461 DLM_ASSERT(list_empty(&r->res_lookup), dlm_dump_rsb(r););
1462 DLM_ASSERT(list_empty(&r->res_grantqueue), dlm_dump_rsb(r););
1463 DLM_ASSERT(list_empty(&r->res_convertqueue), dlm_dump_rsb(r););
1464 DLM_ASSERT(list_empty(&r->res_waitqueue), dlm_dump_rsb(r););
1465 DLM_ASSERT(list_empty(&r->res_root_list), dlm_dump_rsb(r););
1466 DLM_ASSERT(list_empty(&r->res_scan_list), dlm_dump_rsb(r););
1467 DLM_ASSERT(list_empty(&r->res_recover_list), dlm_dump_rsb(r););
1468 DLM_ASSERT(list_empty(&r->res_masters_list), dlm_dump_rsb(r););
1473 /* Attaching/detaching lkb's from rsb's is for rsb reference counting.
1474 The rsb must exist as long as any lkb's for it do. */
1476 static void attach_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb)
1479 lkb->lkb_resource = r;
1482 static void detach_lkb(struct dlm_lkb *lkb)
1484 if (lkb->lkb_resource) {
1485 put_rsb(lkb->lkb_resource);
1486 lkb->lkb_resource = NULL;
1490 static int _create_lkb(struct dlm_ls *ls, struct dlm_lkb **lkb_ret,
1491 unsigned long start, unsigned long end)
1493 struct xa_limit limit;
1494 struct dlm_lkb *lkb;
1500 lkb = dlm_allocate_lkb(ls);
1504 lkb->lkb_last_bast_cb_mode = DLM_LOCK_IV;
1505 lkb->lkb_last_cast_cb_mode = DLM_LOCK_IV;
1506 lkb->lkb_last_cb_mode = DLM_LOCK_IV;
1507 lkb->lkb_nodeid = -1;
1508 lkb->lkb_grmode = DLM_LOCK_IV;
1509 kref_init(&lkb->lkb_ref);
1510 INIT_LIST_HEAD(&lkb->lkb_ownqueue);
1511 INIT_LIST_HEAD(&lkb->lkb_rsb_lookup);
1513 write_lock_bh(&ls->ls_lkbxa_lock);
1514 rv = xa_alloc(&ls->ls_lkbxa, &lkb->lkb_id, lkb, limit, GFP_ATOMIC);
1515 write_unlock_bh(&ls->ls_lkbxa_lock);
1518 log_error(ls, "create_lkb xa error %d", rv);
1527 static int create_lkb(struct dlm_ls *ls, struct dlm_lkb **lkb_ret)
1529 return _create_lkb(ls, lkb_ret, 1, ULONG_MAX);
1532 static int find_lkb(struct dlm_ls *ls, uint32_t lkid, struct dlm_lkb **lkb_ret)
1534 struct dlm_lkb *lkb;
1536 read_lock_bh(&ls->ls_lkbxa_lock);
1537 lkb = xa_load(&ls->ls_lkbxa, lkid);
1539 kref_get(&lkb->lkb_ref);
1540 read_unlock_bh(&ls->ls_lkbxa_lock);
1543 return lkb ? 0 : -ENOENT;
1546 static void kill_lkb(struct kref *kref)
1548 struct dlm_lkb *lkb = container_of(kref, struct dlm_lkb, lkb_ref);
1550 /* All work is done after the return from kref_put() so we
1551 can release the write_lock before the detach_lkb */
1553 DLM_ASSERT(!lkb->lkb_status, dlm_print_lkb(lkb););
1556 /* __put_lkb() is used when an lkb may not have an rsb attached to
1557 it so we need to provide the lockspace explicitly */
1559 static int __put_lkb(struct dlm_ls *ls, struct dlm_lkb *lkb)
1561 uint32_t lkid = lkb->lkb_id;
1564 rv = dlm_kref_put_write_lock_bh(&lkb->lkb_ref, kill_lkb,
1565 &ls->ls_lkbxa_lock);
1567 xa_erase(&ls->ls_lkbxa, lkid);
1568 write_unlock_bh(&ls->ls_lkbxa_lock);
1572 /* for local/process lkbs, lvbptr points to caller's lksb */
1573 if (lkb->lkb_lvbptr && is_master_copy(lkb))
1574 dlm_free_lvb(lkb->lkb_lvbptr);
1581 int dlm_put_lkb(struct dlm_lkb *lkb)
1585 DLM_ASSERT(lkb->lkb_resource, dlm_print_lkb(lkb););
1586 DLM_ASSERT(lkb->lkb_resource->res_ls, dlm_print_lkb(lkb););
1588 ls = lkb->lkb_resource->res_ls;
1589 return __put_lkb(ls, lkb);
1592 /* This is only called to add a reference when the code already holds
1593 a valid reference to the lkb, so there's no need for locking. */
1595 static inline void hold_lkb(struct dlm_lkb *lkb)
1597 kref_get(&lkb->lkb_ref);
1600 static void unhold_lkb_assert(struct kref *kref)
1602 struct dlm_lkb *lkb = container_of(kref, struct dlm_lkb, lkb_ref);
1604 DLM_ASSERT(false, dlm_print_lkb(lkb););
1607 /* This is called when we need to remove a reference and are certain
1608 it's not the last ref. e.g. del_lkb is always called between a
1609 find_lkb/put_lkb and is always the inverse of a previous add_lkb.
1610 put_lkb would work fine, but would involve unnecessary locking */
1612 static inline void unhold_lkb(struct dlm_lkb *lkb)
1614 kref_put(&lkb->lkb_ref, unhold_lkb_assert);
1617 static void lkb_add_ordered(struct list_head *new, struct list_head *head,
1620 struct dlm_lkb *lkb = NULL, *iter;
1622 list_for_each_entry(iter, head, lkb_statequeue)
1623 if (iter->lkb_rqmode < mode) {
1625 list_add_tail(new, &iter->lkb_statequeue);
1630 list_add_tail(new, head);
1633 /* add/remove lkb to rsb's grant/convert/wait queue */
1635 static void add_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb, int status)
1637 kref_get(&lkb->lkb_ref);
1639 DLM_ASSERT(!lkb->lkb_status, dlm_print_lkb(lkb););
1641 lkb->lkb_timestamp = ktime_get();
1643 lkb->lkb_status = status;
1646 case DLM_LKSTS_WAITING:
1647 if (lkb->lkb_exflags & DLM_LKF_HEADQUE)
1648 list_add(&lkb->lkb_statequeue, &r->res_waitqueue);
1650 list_add_tail(&lkb->lkb_statequeue, &r->res_waitqueue);
1652 case DLM_LKSTS_GRANTED:
1653 /* convention says granted locks kept in order of grmode */
1654 lkb_add_ordered(&lkb->lkb_statequeue, &r->res_grantqueue,
1657 case DLM_LKSTS_CONVERT:
1658 if (lkb->lkb_exflags & DLM_LKF_HEADQUE)
1659 list_add(&lkb->lkb_statequeue, &r->res_convertqueue);
1661 list_add_tail(&lkb->lkb_statequeue,
1662 &r->res_convertqueue);
1665 DLM_ASSERT(0, dlm_print_lkb(lkb); printk("sts=%d\n", status););
1669 static void del_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb)
1671 lkb->lkb_status = 0;
1672 list_del(&lkb->lkb_statequeue);
1676 static void move_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb, int sts)
1680 add_lkb(r, lkb, sts);
1684 static int msg_reply_type(int mstype)
1687 case DLM_MSG_REQUEST:
1688 return DLM_MSG_REQUEST_REPLY;
1689 case DLM_MSG_CONVERT:
1690 return DLM_MSG_CONVERT_REPLY;
1691 case DLM_MSG_UNLOCK:
1692 return DLM_MSG_UNLOCK_REPLY;
1693 case DLM_MSG_CANCEL:
1694 return DLM_MSG_CANCEL_REPLY;
1695 case DLM_MSG_LOOKUP:
1696 return DLM_MSG_LOOKUP_REPLY;
1701 /* add/remove lkb from global waiters list of lkb's waiting for
1702 a reply from a remote node */
1704 static int add_to_waiters(struct dlm_lkb *lkb, int mstype, int to_nodeid)
1706 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1709 spin_lock_bh(&ls->ls_waiters_lock);
1711 if (is_overlap_unlock(lkb) ||
1712 (is_overlap_cancel(lkb) && (mstype == DLM_MSG_CANCEL))) {
1717 if (lkb->lkb_wait_type || is_overlap_cancel(lkb)) {
1719 case DLM_MSG_UNLOCK:
1720 set_bit(DLM_IFL_OVERLAP_UNLOCK_BIT, &lkb->lkb_iflags);
1722 case DLM_MSG_CANCEL:
1723 set_bit(DLM_IFL_OVERLAP_CANCEL_BIT, &lkb->lkb_iflags);
1729 lkb->lkb_wait_count++;
1732 log_debug(ls, "addwait %x cur %d overlap %d count %d f %x",
1733 lkb->lkb_id, lkb->lkb_wait_type, mstype,
1734 lkb->lkb_wait_count, dlm_iflags_val(lkb));
1738 DLM_ASSERT(!lkb->lkb_wait_count,
1740 printk("wait_count %d\n", lkb->lkb_wait_count););
1742 lkb->lkb_wait_count++;
1743 lkb->lkb_wait_type = mstype;
1744 lkb->lkb_wait_nodeid = to_nodeid; /* for debugging */
1746 list_add(&lkb->lkb_wait_reply, &ls->ls_waiters);
1749 log_error(ls, "addwait error %x %d flags %x %d %d %s",
1750 lkb->lkb_id, error, dlm_iflags_val(lkb), mstype,
1751 lkb->lkb_wait_type, lkb->lkb_resource->res_name);
1752 spin_unlock_bh(&ls->ls_waiters_lock);
1756 /* We clear the RESEND flag because we might be taking an lkb off the waiters
1757 list as part of process_requestqueue (e.g. a lookup that has an optimized
1758 request reply on the requestqueue) between dlm_recover_waiters_pre() which
1759 set RESEND and dlm_recover_waiters_post() */
1761 static int _remove_from_waiters(struct dlm_lkb *lkb, int mstype,
1762 const struct dlm_message *ms)
1764 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1765 int overlap_done = 0;
1767 if (mstype == DLM_MSG_UNLOCK_REPLY &&
1768 test_and_clear_bit(DLM_IFL_OVERLAP_UNLOCK_BIT, &lkb->lkb_iflags)) {
1769 log_debug(ls, "remwait %x unlock_reply overlap", lkb->lkb_id);
1774 if (mstype == DLM_MSG_CANCEL_REPLY &&
1775 test_and_clear_bit(DLM_IFL_OVERLAP_CANCEL_BIT, &lkb->lkb_iflags)) {
1776 log_debug(ls, "remwait %x cancel_reply overlap", lkb->lkb_id);
1781 /* Cancel state was preemptively cleared by a successful convert,
1782 see next comment, nothing to do. */
1784 if ((mstype == DLM_MSG_CANCEL_REPLY) &&
1785 (lkb->lkb_wait_type != DLM_MSG_CANCEL)) {
1786 log_debug(ls, "remwait %x cancel_reply wait_type %d",
1787 lkb->lkb_id, lkb->lkb_wait_type);
1791 /* Remove for the convert reply, and premptively remove for the
1792 cancel reply. A convert has been granted while there's still
1793 an outstanding cancel on it (the cancel is moot and the result
1794 in the cancel reply should be 0). We preempt the cancel reply
1795 because the app gets the convert result and then can follow up
1796 with another op, like convert. This subsequent op would see the
1797 lingering state of the cancel and fail with -EBUSY. */
1799 if ((mstype == DLM_MSG_CONVERT_REPLY) &&
1800 (lkb->lkb_wait_type == DLM_MSG_CONVERT) && ms && !ms->m_result &&
1801 test_and_clear_bit(DLM_IFL_OVERLAP_CANCEL_BIT, &lkb->lkb_iflags)) {
1802 log_debug(ls, "remwait %x convert_reply zap overlap_cancel",
1804 lkb->lkb_wait_type = 0;
1805 lkb->lkb_wait_count--;
1810 /* N.B. type of reply may not always correspond to type of original
1811 msg due to lookup->request optimization, verify others? */
1813 if (lkb->lkb_wait_type) {
1814 lkb->lkb_wait_type = 0;
1818 log_error(ls, "remwait error %x remote %d %x msg %d flags %x no wait",
1819 lkb->lkb_id, ms ? le32_to_cpu(ms->m_header.h_nodeid) : 0,
1820 lkb->lkb_remid, mstype, dlm_iflags_val(lkb));
1824 /* the force-unlock/cancel has completed and we haven't recvd a reply
1825 to the op that was in progress prior to the unlock/cancel; we
1826 give up on any reply to the earlier op. FIXME: not sure when/how
1827 this would happen */
1829 if (overlap_done && lkb->lkb_wait_type) {
1830 log_error(ls, "remwait error %x reply %d wait_type %d overlap",
1831 lkb->lkb_id, mstype, lkb->lkb_wait_type);
1832 lkb->lkb_wait_count--;
1834 lkb->lkb_wait_type = 0;
1837 DLM_ASSERT(lkb->lkb_wait_count, dlm_print_lkb(lkb););
1839 clear_bit(DLM_IFL_RESEND_BIT, &lkb->lkb_iflags);
1840 lkb->lkb_wait_count--;
1841 if (!lkb->lkb_wait_count)
1842 list_del_init(&lkb->lkb_wait_reply);
1847 static int remove_from_waiters(struct dlm_lkb *lkb, int mstype)
1849 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1852 spin_lock_bh(&ls->ls_waiters_lock);
1853 error = _remove_from_waiters(lkb, mstype, NULL);
1854 spin_unlock_bh(&ls->ls_waiters_lock);
1858 /* Handles situations where we might be processing a "fake" or "local" reply in
1859 * the recovery context which stops any locking activity. Only debugfs might
1860 * change the lockspace waiters but they will held the recovery lock to ensure
1861 * remove_from_waiters_ms() in local case will be the only user manipulating the
1862 * lockspace waiters in recovery context.
1865 static int remove_from_waiters_ms(struct dlm_lkb *lkb,
1866 const struct dlm_message *ms, bool local)
1868 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1872 spin_lock_bh(&ls->ls_waiters_lock);
1874 WARN_ON_ONCE(!rwsem_is_locked(&ls->ls_in_recovery) ||
1875 !dlm_locking_stopped(ls));
1876 error = _remove_from_waiters(lkb, le32_to_cpu(ms->m_type), ms);
1878 spin_unlock_bh(&ls->ls_waiters_lock);
1882 /* lkb is master or local copy */
1884 static void set_lvb_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1886 int b, len = r->res_ls->ls_lvblen;
1888 /* b=1 lvb returned to caller
1889 b=0 lvb written to rsb or invalidated
1892 b = dlm_lvb_operations[lkb->lkb_grmode + 1][lkb->lkb_rqmode + 1];
1895 if (!lkb->lkb_lvbptr)
1898 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1904 memcpy(lkb->lkb_lvbptr, r->res_lvbptr, len);
1905 lkb->lkb_lvbseq = r->res_lvbseq;
1907 } else if (b == 0) {
1908 if (lkb->lkb_exflags & DLM_LKF_IVVALBLK) {
1909 rsb_set_flag(r, RSB_VALNOTVALID);
1913 if (!lkb->lkb_lvbptr)
1916 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1920 r->res_lvbptr = dlm_allocate_lvb(r->res_ls);
1925 memcpy(r->res_lvbptr, lkb->lkb_lvbptr, len);
1927 lkb->lkb_lvbseq = r->res_lvbseq;
1928 rsb_clear_flag(r, RSB_VALNOTVALID);
1931 if (rsb_flag(r, RSB_VALNOTVALID))
1932 set_bit(DLM_SBF_VALNOTVALID_BIT, &lkb->lkb_sbflags);
1935 static void set_lvb_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1937 if (lkb->lkb_grmode < DLM_LOCK_PW)
1940 if (lkb->lkb_exflags & DLM_LKF_IVVALBLK) {
1941 rsb_set_flag(r, RSB_VALNOTVALID);
1945 if (!lkb->lkb_lvbptr)
1948 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1952 r->res_lvbptr = dlm_allocate_lvb(r->res_ls);
1957 memcpy(r->res_lvbptr, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
1959 rsb_clear_flag(r, RSB_VALNOTVALID);
1962 /* lkb is process copy (pc) */
1964 static void set_lvb_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb,
1965 const struct dlm_message *ms)
1969 if (!lkb->lkb_lvbptr)
1972 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1975 b = dlm_lvb_operations[lkb->lkb_grmode + 1][lkb->lkb_rqmode + 1];
1977 int len = receive_extralen(ms);
1978 if (len > r->res_ls->ls_lvblen)
1979 len = r->res_ls->ls_lvblen;
1980 memcpy(lkb->lkb_lvbptr, ms->m_extra, len);
1981 lkb->lkb_lvbseq = le32_to_cpu(ms->m_lvbseq);
1985 /* Manipulate lkb's on rsb's convert/granted/waiting queues
1986 remove_lock -- used for unlock, removes lkb from granted
1987 revert_lock -- used for cancel, moves lkb from convert to granted
1988 grant_lock -- used for request and convert, adds lkb to granted or
1989 moves lkb from convert or waiting to granted
1991 Each of these is used for master or local copy lkb's. There is
1992 also a _pc() variation used to make the corresponding change on
1993 a process copy (pc) lkb. */
1995 static void _remove_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1998 lkb->lkb_grmode = DLM_LOCK_IV;
1999 /* this unhold undoes the original ref from create_lkb()
2000 so this leads to the lkb being freed */
2004 static void remove_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2006 set_lvb_unlock(r, lkb);
2007 _remove_lock(r, lkb);
2010 static void remove_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb)
2012 _remove_lock(r, lkb);
2015 /* returns: 0 did nothing
2016 1 moved lock to granted
2019 static int revert_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2023 lkb->lkb_rqmode = DLM_LOCK_IV;
2025 switch (lkb->lkb_status) {
2026 case DLM_LKSTS_GRANTED:
2028 case DLM_LKSTS_CONVERT:
2029 move_lkb(r, lkb, DLM_LKSTS_GRANTED);
2032 case DLM_LKSTS_WAITING:
2034 lkb->lkb_grmode = DLM_LOCK_IV;
2035 /* this unhold undoes the original ref from create_lkb()
2036 so this leads to the lkb being freed */
2041 log_print("invalid status for revert %d", lkb->lkb_status);
2046 static int revert_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb)
2048 return revert_lock(r, lkb);
2051 static void _grant_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2053 if (lkb->lkb_grmode != lkb->lkb_rqmode) {
2054 lkb->lkb_grmode = lkb->lkb_rqmode;
2055 if (lkb->lkb_status)
2056 move_lkb(r, lkb, DLM_LKSTS_GRANTED);
2058 add_lkb(r, lkb, DLM_LKSTS_GRANTED);
2061 lkb->lkb_rqmode = DLM_LOCK_IV;
2062 lkb->lkb_highbast = 0;
2065 static void grant_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2067 set_lvb_lock(r, lkb);
2068 _grant_lock(r, lkb);
2071 static void grant_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb,
2072 const struct dlm_message *ms)
2074 set_lvb_lock_pc(r, lkb, ms);
2075 _grant_lock(r, lkb);
2078 /* called by grant_pending_locks() which means an async grant message must
2079 be sent to the requesting node in addition to granting the lock if the
2080 lkb belongs to a remote node. */
2082 static void grant_lock_pending(struct dlm_rsb *r, struct dlm_lkb *lkb)
2085 if (is_master_copy(lkb))
2088 queue_cast(r, lkb, 0);
2091 /* The special CONVDEADLK, ALTPR and ALTCW flags allow the master to
2092 change the granted/requested modes. We're munging things accordingly in
2094 CONVDEADLK: our grmode may have been forced down to NL to resolve a
2096 ALTPR/ALTCW: our rqmode may have been changed to PR or CW to become
2097 compatible with other granted locks */
2099 static void munge_demoted(struct dlm_lkb *lkb)
2101 if (lkb->lkb_rqmode == DLM_LOCK_IV || lkb->lkb_grmode == DLM_LOCK_IV) {
2102 log_print("munge_demoted %x invalid modes gr %d rq %d",
2103 lkb->lkb_id, lkb->lkb_grmode, lkb->lkb_rqmode);
2107 lkb->lkb_grmode = DLM_LOCK_NL;
2110 static void munge_altmode(struct dlm_lkb *lkb, const struct dlm_message *ms)
2112 if (ms->m_type != cpu_to_le32(DLM_MSG_REQUEST_REPLY) &&
2113 ms->m_type != cpu_to_le32(DLM_MSG_GRANT)) {
2114 log_print("munge_altmode %x invalid reply type %d",
2115 lkb->lkb_id, le32_to_cpu(ms->m_type));
2119 if (lkb->lkb_exflags & DLM_LKF_ALTPR)
2120 lkb->lkb_rqmode = DLM_LOCK_PR;
2121 else if (lkb->lkb_exflags & DLM_LKF_ALTCW)
2122 lkb->lkb_rqmode = DLM_LOCK_CW;
2124 log_print("munge_altmode invalid exflags %x", lkb->lkb_exflags);
2129 static inline int first_in_list(struct dlm_lkb *lkb, struct list_head *head)
2131 struct dlm_lkb *first = list_entry(head->next, struct dlm_lkb,
2133 if (lkb->lkb_id == first->lkb_id)
2139 /* Check if the given lkb conflicts with another lkb on the queue. */
2141 static int queue_conflict(struct list_head *head, struct dlm_lkb *lkb)
2143 struct dlm_lkb *this;
2145 list_for_each_entry(this, head, lkb_statequeue) {
2148 if (!modes_compat(this, lkb))
2155 * "A conversion deadlock arises with a pair of lock requests in the converting
2156 * queue for one resource. The granted mode of each lock blocks the requested
2157 * mode of the other lock."
2159 * Part 2: if the granted mode of lkb is preventing an earlier lkb in the
2160 * convert queue from being granted, then deadlk/demote lkb.
2163 * Granted Queue: empty
2164 * Convert Queue: NL->EX (first lock)
2165 * PR->EX (second lock)
2167 * The first lock can't be granted because of the granted mode of the second
2168 * lock and the second lock can't be granted because it's not first in the
2169 * list. We either cancel lkb's conversion (PR->EX) and return EDEADLK, or we
2170 * demote the granted mode of lkb (from PR to NL) if it has the CONVDEADLK
2171 * flag set and return DEMOTED in the lksb flags.
2173 * Originally, this function detected conv-deadlk in a more limited scope:
2174 * - if !modes_compat(lkb1, lkb2) && !modes_compat(lkb2, lkb1), or
2175 * - if lkb1 was the first entry in the queue (not just earlier), and was
2176 * blocked by the granted mode of lkb2, and there was nothing on the
2177 * granted queue preventing lkb1 from being granted immediately, i.e.
2178 * lkb2 was the only thing preventing lkb1 from being granted.
2180 * That second condition meant we'd only say there was conv-deadlk if
2181 * resolving it (by demotion) would lead to the first lock on the convert
2182 * queue being granted right away. It allowed conversion deadlocks to exist
2183 * between locks on the convert queue while they couldn't be granted anyway.
2185 * Now, we detect and take action on conversion deadlocks immediately when
2186 * they're created, even if they may not be immediately consequential. If
2187 * lkb1 exists anywhere in the convert queue and lkb2 comes in with a granted
2188 * mode that would prevent lkb1's conversion from being granted, we do a
2189 * deadlk/demote on lkb2 right away and don't let it onto the convert queue.
2190 * I think this means that the lkb_is_ahead condition below should always
2191 * be zero, i.e. there will never be conv-deadlk between two locks that are
2192 * both already on the convert queue.
2195 static int conversion_deadlock_detect(struct dlm_rsb *r, struct dlm_lkb *lkb2)
2197 struct dlm_lkb *lkb1;
2198 int lkb_is_ahead = 0;
2200 list_for_each_entry(lkb1, &r->res_convertqueue, lkb_statequeue) {
2206 if (!lkb_is_ahead) {
2207 if (!modes_compat(lkb2, lkb1))
2210 if (!modes_compat(lkb2, lkb1) &&
2211 !modes_compat(lkb1, lkb2))
2219 * Return 1 if the lock can be granted, 0 otherwise.
2220 * Also detect and resolve conversion deadlocks.
2222 * lkb is the lock to be granted
2224 * now is 1 if the function is being called in the context of the
2225 * immediate request, it is 0 if called later, after the lock has been
2228 * recover is 1 if dlm_recover_grant() is trying to grant conversions
2231 * References are from chapter 6 of "VAXcluster Principles" by Roy Davis
2234 static int _can_be_granted(struct dlm_rsb *r, struct dlm_lkb *lkb, int now,
2237 int8_t conv = (lkb->lkb_grmode != DLM_LOCK_IV);
2240 * 6-10: Version 5.4 introduced an option to address the phenomenon of
2241 * a new request for a NL mode lock being blocked.
2243 * 6-11: If the optional EXPEDITE flag is used with the new NL mode
2244 * request, then it would be granted. In essence, the use of this flag
2245 * tells the Lock Manager to expedite theis request by not considering
2246 * what may be in the CONVERTING or WAITING queues... As of this
2247 * writing, the EXPEDITE flag can be used only with new requests for NL
2248 * mode locks. This flag is not valid for conversion requests.
2250 * A shortcut. Earlier checks return an error if EXPEDITE is used in a
2251 * conversion or used with a non-NL requested mode. We also know an
2252 * EXPEDITE request is always granted immediately, so now must always
2253 * be 1. The full condition to grant an expedite request: (now &&
2254 * !conv && lkb->rqmode == DLM_LOCK_NL && (flags & EXPEDITE)) can
2255 * therefore be shortened to just checking the flag.
2258 if (lkb->lkb_exflags & DLM_LKF_EXPEDITE)
2262 * A shortcut. Without this, !queue_conflict(grantqueue, lkb) would be
2263 * added to the remaining conditions.
2266 if (queue_conflict(&r->res_grantqueue, lkb))
2270 * 6-3: By default, a conversion request is immediately granted if the
2271 * requested mode is compatible with the modes of all other granted
2275 if (queue_conflict(&r->res_convertqueue, lkb))
2279 * The RECOVER_GRANT flag means dlm_recover_grant() is granting
2280 * locks for a recovered rsb, on which lkb's have been rebuilt.
2281 * The lkb's may have been rebuilt on the queues in a different
2282 * order than they were in on the previous master. So, granting
2283 * queued conversions in order after recovery doesn't make sense
2284 * since the order hasn't been preserved anyway. The new order
2285 * could also have created a new "in place" conversion deadlock.
2286 * (e.g. old, failed master held granted EX, with PR->EX, NL->EX.
2287 * After recovery, there would be no granted locks, and possibly
2288 * NL->EX, PR->EX, an in-place conversion deadlock.) So, after
2289 * recovery, grant conversions without considering order.
2292 if (conv && recover)
2296 * 6-5: But the default algorithm for deciding whether to grant or
2297 * queue conversion requests does not by itself guarantee that such
2298 * requests are serviced on a "first come first serve" basis. This, in
2299 * turn, can lead to a phenomenon known as "indefinate postponement".
2301 * 6-7: This issue is dealt with by using the optional QUECVT flag with
2302 * the system service employed to request a lock conversion. This flag
2303 * forces certain conversion requests to be queued, even if they are
2304 * compatible with the granted modes of other locks on the same
2305 * resource. Thus, the use of this flag results in conversion requests
2306 * being ordered on a "first come first servce" basis.
2308 * DCT: This condition is all about new conversions being able to occur
2309 * "in place" while the lock remains on the granted queue (assuming
2310 * nothing else conflicts.) IOW if QUECVT isn't set, a conversion
2311 * doesn't _have_ to go onto the convert queue where it's processed in
2312 * order. The "now" variable is necessary to distinguish converts
2313 * being received and processed for the first time now, because once a
2314 * convert is moved to the conversion queue the condition below applies
2315 * requiring fifo granting.
2318 if (now && conv && !(lkb->lkb_exflags & DLM_LKF_QUECVT))
2322 * Even if the convert is compat with all granted locks,
2323 * QUECVT forces it behind other locks on the convert queue.
2326 if (now && conv && (lkb->lkb_exflags & DLM_LKF_QUECVT)) {
2327 if (list_empty(&r->res_convertqueue))
2334 * The NOORDER flag is set to avoid the standard vms rules on grant
2338 if (lkb->lkb_exflags & DLM_LKF_NOORDER)
2342 * 6-3: Once in that queue [CONVERTING], a conversion request cannot be
2343 * granted until all other conversion requests ahead of it are granted
2347 if (!now && conv && first_in_list(lkb, &r->res_convertqueue))
2351 * 6-4: By default, a new request is immediately granted only if all
2352 * three of the following conditions are satisfied when the request is
2354 * - The queue of ungranted conversion requests for the resource is
2356 * - The queue of ungranted new requests for the resource is empty.
2357 * - The mode of the new request is compatible with the most
2358 * restrictive mode of all granted locks on the resource.
2361 if (now && !conv && list_empty(&r->res_convertqueue) &&
2362 list_empty(&r->res_waitqueue))
2366 * 6-4: Once a lock request is in the queue of ungranted new requests,
2367 * it cannot be granted until the queue of ungranted conversion
2368 * requests is empty, all ungranted new requests ahead of it are
2369 * granted and/or canceled, and it is compatible with the granted mode
2370 * of the most restrictive lock granted on the resource.
2373 if (!now && !conv && list_empty(&r->res_convertqueue) &&
2374 first_in_list(lkb, &r->res_waitqueue))
2380 static int can_be_granted(struct dlm_rsb *r, struct dlm_lkb *lkb, int now,
2381 int recover, int *err)
2384 int8_t alt = 0, rqmode = lkb->lkb_rqmode;
2385 int8_t is_convert = (lkb->lkb_grmode != DLM_LOCK_IV);
2390 rv = _can_be_granted(r, lkb, now, recover);
2395 * The CONVDEADLK flag is non-standard and tells the dlm to resolve
2396 * conversion deadlocks by demoting grmode to NL, otherwise the dlm
2397 * cancels one of the locks.
2400 if (is_convert && can_be_queued(lkb) &&
2401 conversion_deadlock_detect(r, lkb)) {
2402 if (lkb->lkb_exflags & DLM_LKF_CONVDEADLK) {
2403 lkb->lkb_grmode = DLM_LOCK_NL;
2404 set_bit(DLM_SBF_DEMOTED_BIT, &lkb->lkb_sbflags);
2408 log_print("can_be_granted deadlock %x now %d",
2416 * The ALTPR and ALTCW flags are non-standard and tell the dlm to try
2417 * to grant a request in a mode other than the normal rqmode. It's a
2418 * simple way to provide a big optimization to applications that can
2422 if (rqmode != DLM_LOCK_PR && (lkb->lkb_exflags & DLM_LKF_ALTPR))
2424 else if (rqmode != DLM_LOCK_CW && (lkb->lkb_exflags & DLM_LKF_ALTCW))
2428 lkb->lkb_rqmode = alt;
2429 rv = _can_be_granted(r, lkb, now, 0);
2431 set_bit(DLM_SBF_ALTMODE_BIT, &lkb->lkb_sbflags);
2433 lkb->lkb_rqmode = rqmode;
2439 /* Returns the highest requested mode of all blocked conversions; sets
2440 cw if there's a blocked conversion to DLM_LOCK_CW. */
2442 static int grant_pending_convert(struct dlm_rsb *r, int high, int *cw,
2443 unsigned int *count)
2445 struct dlm_lkb *lkb, *s;
2446 int recover = rsb_flag(r, RSB_RECOVER_GRANT);
2447 int hi, demoted, quit, grant_restart, demote_restart;
2456 list_for_each_entry_safe(lkb, s, &r->res_convertqueue, lkb_statequeue) {
2457 demoted = is_demoted(lkb);
2460 if (can_be_granted(r, lkb, 0, recover, &deadlk)) {
2461 grant_lock_pending(r, lkb);
2468 if (!demoted && is_demoted(lkb)) {
2469 log_print("WARN: pending demoted %x node %d %s",
2470 lkb->lkb_id, lkb->lkb_nodeid, r->res_name);
2477 * If DLM_LKB_NODLKWT flag is set and conversion
2478 * deadlock is detected, we request blocking AST and
2479 * down (or cancel) conversion.
2481 if (lkb->lkb_exflags & DLM_LKF_NODLCKWT) {
2482 if (lkb->lkb_highbast < lkb->lkb_rqmode) {
2483 queue_bast(r, lkb, lkb->lkb_rqmode);
2484 lkb->lkb_highbast = lkb->lkb_rqmode;
2487 log_print("WARN: pending deadlock %x node %d %s",
2488 lkb->lkb_id, lkb->lkb_nodeid,
2495 hi = max_t(int, lkb->lkb_rqmode, hi);
2497 if (cw && lkb->lkb_rqmode == DLM_LOCK_CW)
2503 if (demote_restart && !quit) {
2508 return max_t(int, high, hi);
2511 static int grant_pending_wait(struct dlm_rsb *r, int high, int *cw,
2512 unsigned int *count)
2514 struct dlm_lkb *lkb, *s;
2516 list_for_each_entry_safe(lkb, s, &r->res_waitqueue, lkb_statequeue) {
2517 if (can_be_granted(r, lkb, 0, 0, NULL)) {
2518 grant_lock_pending(r, lkb);
2522 high = max_t(int, lkb->lkb_rqmode, high);
2523 if (lkb->lkb_rqmode == DLM_LOCK_CW)
2531 /* cw of 1 means there's a lock with a rqmode of DLM_LOCK_CW that's blocked
2532 on either the convert or waiting queue.
2533 high is the largest rqmode of all locks blocked on the convert or
2536 static int lock_requires_bast(struct dlm_lkb *gr, int high, int cw)
2538 if (gr->lkb_grmode == DLM_LOCK_PR && cw) {
2539 if (gr->lkb_highbast < DLM_LOCK_EX)
2544 if (gr->lkb_highbast < high &&
2545 !__dlm_compat_matrix[gr->lkb_grmode+1][high+1])
2550 static void grant_pending_locks(struct dlm_rsb *r, unsigned int *count)
2552 struct dlm_lkb *lkb, *s;
2553 int high = DLM_LOCK_IV;
2556 if (!is_master(r)) {
2557 log_print("grant_pending_locks r nodeid %d", r->res_nodeid);
2562 high = grant_pending_convert(r, high, &cw, count);
2563 high = grant_pending_wait(r, high, &cw, count);
2565 if (high == DLM_LOCK_IV)
2569 * If there are locks left on the wait/convert queue then send blocking
2570 * ASTs to granted locks based on the largest requested mode (high)
2574 list_for_each_entry_safe(lkb, s, &r->res_grantqueue, lkb_statequeue) {
2575 if (lkb->lkb_bastfn && lock_requires_bast(lkb, high, cw)) {
2576 if (cw && high == DLM_LOCK_PR &&
2577 lkb->lkb_grmode == DLM_LOCK_PR)
2578 queue_bast(r, lkb, DLM_LOCK_CW);
2580 queue_bast(r, lkb, high);
2581 lkb->lkb_highbast = high;
2586 static int modes_require_bast(struct dlm_lkb *gr, struct dlm_lkb *rq)
2588 if ((gr->lkb_grmode == DLM_LOCK_PR && rq->lkb_rqmode == DLM_LOCK_CW) ||
2589 (gr->lkb_grmode == DLM_LOCK_CW && rq->lkb_rqmode == DLM_LOCK_PR)) {
2590 if (gr->lkb_highbast < DLM_LOCK_EX)
2595 if (gr->lkb_highbast < rq->lkb_rqmode && !modes_compat(gr, rq))
2600 static void send_bast_queue(struct dlm_rsb *r, struct list_head *head,
2601 struct dlm_lkb *lkb)
2605 list_for_each_entry(gr, head, lkb_statequeue) {
2606 /* skip self when sending basts to convertqueue */
2609 if (gr->lkb_bastfn && modes_require_bast(gr, lkb)) {
2610 queue_bast(r, gr, lkb->lkb_rqmode);
2611 gr->lkb_highbast = lkb->lkb_rqmode;
2616 static void send_blocking_asts(struct dlm_rsb *r, struct dlm_lkb *lkb)
2618 send_bast_queue(r, &r->res_grantqueue, lkb);
2621 static void send_blocking_asts_all(struct dlm_rsb *r, struct dlm_lkb *lkb)
2623 send_bast_queue(r, &r->res_grantqueue, lkb);
2624 send_bast_queue(r, &r->res_convertqueue, lkb);
2627 /* set_master(r, lkb) -- set the master nodeid of a resource
2629 The purpose of this function is to set the nodeid field in the given
2630 lkb using the nodeid field in the given rsb. If the rsb's nodeid is
2631 known, it can just be copied to the lkb and the function will return
2632 0. If the rsb's nodeid is _not_ known, it needs to be looked up
2633 before it can be copied to the lkb.
2635 When the rsb nodeid is being looked up remotely, the initial lkb
2636 causing the lookup is kept on the ls_waiters list waiting for the
2637 lookup reply. Other lkb's waiting for the same rsb lookup are kept
2638 on the rsb's res_lookup list until the master is verified.
2641 0: nodeid is set in rsb/lkb and the caller should go ahead and use it
2642 1: the rsb master is not available and the lkb has been placed on
2646 static int set_master(struct dlm_rsb *r, struct dlm_lkb *lkb)
2648 int our_nodeid = dlm_our_nodeid();
2650 if (rsb_flag(r, RSB_MASTER_UNCERTAIN)) {
2651 rsb_clear_flag(r, RSB_MASTER_UNCERTAIN);
2652 r->res_first_lkid = lkb->lkb_id;
2653 lkb->lkb_nodeid = r->res_nodeid;
2657 if (r->res_first_lkid && r->res_first_lkid != lkb->lkb_id) {
2658 list_add_tail(&lkb->lkb_rsb_lookup, &r->res_lookup);
2662 if (r->res_master_nodeid == our_nodeid) {
2663 lkb->lkb_nodeid = 0;
2667 if (r->res_master_nodeid) {
2668 lkb->lkb_nodeid = r->res_master_nodeid;
2672 if (dlm_dir_nodeid(r) == our_nodeid) {
2673 /* This is a somewhat unusual case; find_rsb will usually
2674 have set res_master_nodeid when dir nodeid is local, but
2675 there are cases where we become the dir node after we've
2676 past find_rsb and go through _request_lock again.
2677 confirm_master() or process_lookup_list() needs to be
2678 called after this. */
2679 log_debug(r->res_ls, "set_master %x self master %d dir %d %s",
2680 lkb->lkb_id, r->res_master_nodeid, r->res_dir_nodeid,
2682 r->res_master_nodeid = our_nodeid;
2684 lkb->lkb_nodeid = 0;
2688 r->res_first_lkid = lkb->lkb_id;
2689 send_lookup(r, lkb);
2693 static void process_lookup_list(struct dlm_rsb *r)
2695 struct dlm_lkb *lkb, *safe;
2697 list_for_each_entry_safe(lkb, safe, &r->res_lookup, lkb_rsb_lookup) {
2698 list_del_init(&lkb->lkb_rsb_lookup);
2699 _request_lock(r, lkb);
2703 /* confirm_master -- confirm (or deny) an rsb's master nodeid */
2705 static void confirm_master(struct dlm_rsb *r, int error)
2707 struct dlm_lkb *lkb;
2709 if (!r->res_first_lkid)
2715 r->res_first_lkid = 0;
2716 process_lookup_list(r);
2722 /* the remote request failed and won't be retried (it was
2723 a NOQUEUE, or has been canceled/unlocked); make a waiting
2724 lkb the first_lkid */
2726 r->res_first_lkid = 0;
2728 if (!list_empty(&r->res_lookup)) {
2729 lkb = list_entry(r->res_lookup.next, struct dlm_lkb,
2731 list_del_init(&lkb->lkb_rsb_lookup);
2732 r->res_first_lkid = lkb->lkb_id;
2733 _request_lock(r, lkb);
2738 log_error(r->res_ls, "confirm_master unknown error %d", error);
2742 static int set_lock_args(int mode, struct dlm_lksb *lksb, uint32_t flags,
2743 int namelen, void (*ast)(void *astparam),
2745 void (*bast)(void *astparam, int mode),
2746 struct dlm_args *args)
2750 /* check for invalid arg usage */
2752 if (mode < 0 || mode > DLM_LOCK_EX)
2755 if (!(flags & DLM_LKF_CONVERT) && (namelen > DLM_RESNAME_MAXLEN))
2758 if (flags & DLM_LKF_CANCEL)
2761 if (flags & DLM_LKF_QUECVT && !(flags & DLM_LKF_CONVERT))
2764 if (flags & DLM_LKF_CONVDEADLK && !(flags & DLM_LKF_CONVERT))
2767 if (flags & DLM_LKF_CONVDEADLK && flags & DLM_LKF_NOQUEUE)
2770 if (flags & DLM_LKF_EXPEDITE && flags & DLM_LKF_CONVERT)
2773 if (flags & DLM_LKF_EXPEDITE && flags & DLM_LKF_QUECVT)
2776 if (flags & DLM_LKF_EXPEDITE && flags & DLM_LKF_NOQUEUE)
2779 if (flags & DLM_LKF_EXPEDITE && mode != DLM_LOCK_NL)
2785 if (flags & DLM_LKF_VALBLK && !lksb->sb_lvbptr)
2788 if (flags & DLM_LKF_CONVERT && !lksb->sb_lkid)
2791 /* these args will be copied to the lkb in validate_lock_args,
2792 it cannot be done now because when converting locks, fields in
2793 an active lkb cannot be modified before locking the rsb */
2795 args->flags = flags;
2797 args->astparam = astparam;
2798 args->bastfn = bast;
2806 static int set_unlock_args(uint32_t flags, void *astarg, struct dlm_args *args)
2808 if (flags & ~(DLM_LKF_CANCEL | DLM_LKF_VALBLK | DLM_LKF_IVVALBLK |
2809 DLM_LKF_FORCEUNLOCK))
2812 if (flags & DLM_LKF_CANCEL && flags & DLM_LKF_FORCEUNLOCK)
2815 args->flags = flags;
2816 args->astparam = astarg;
2820 static int validate_lock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
2821 struct dlm_args *args)
2825 if (args->flags & DLM_LKF_CONVERT) {
2826 if (lkb->lkb_status != DLM_LKSTS_GRANTED)
2829 /* lock not allowed if there's any op in progress */
2830 if (lkb->lkb_wait_type || lkb->lkb_wait_count)
2833 if (is_overlap(lkb))
2837 if (test_bit(DLM_IFL_MSTCPY_BIT, &lkb->lkb_iflags))
2840 if (args->flags & DLM_LKF_QUECVT &&
2841 !__quecvt_compat_matrix[lkb->lkb_grmode+1][args->mode+1])
2845 lkb->lkb_exflags = args->flags;
2846 dlm_set_sbflags_val(lkb, 0);
2847 lkb->lkb_astfn = args->astfn;
2848 lkb->lkb_astparam = args->astparam;
2849 lkb->lkb_bastfn = args->bastfn;
2850 lkb->lkb_rqmode = args->mode;
2851 lkb->lkb_lksb = args->lksb;
2852 lkb->lkb_lvbptr = args->lksb->sb_lvbptr;
2853 lkb->lkb_ownpid = (int) current->pid;
2860 /* annoy the user because dlm usage is wrong */
2862 log_error(ls, "%s %d %x %x %x %d %d %s", __func__,
2863 rv, lkb->lkb_id, dlm_iflags_val(lkb), args->flags,
2864 lkb->lkb_status, lkb->lkb_wait_type,
2865 lkb->lkb_resource->res_name);
2868 log_debug(ls, "%s %d %x %x %x %d %d %s", __func__,
2869 rv, lkb->lkb_id, dlm_iflags_val(lkb), args->flags,
2870 lkb->lkb_status, lkb->lkb_wait_type,
2871 lkb->lkb_resource->res_name);
2878 /* when dlm_unlock() sees -EBUSY with CANCEL/FORCEUNLOCK it returns 0
2881 /* note: it's valid for lkb_nodeid/res_nodeid to be -1 when we get here
2882 because there may be a lookup in progress and it's valid to do
2883 cancel/unlockf on it */
2885 static int validate_unlock_args(struct dlm_lkb *lkb, struct dlm_args *args)
2887 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
2890 /* normal unlock not allowed if there's any op in progress */
2891 if (!(args->flags & (DLM_LKF_CANCEL | DLM_LKF_FORCEUNLOCK)) &&
2892 (lkb->lkb_wait_type || lkb->lkb_wait_count))
2895 /* an lkb may be waiting for an rsb lookup to complete where the
2896 lookup was initiated by another lock */
2898 if (!list_empty(&lkb->lkb_rsb_lookup)) {
2899 if (args->flags & (DLM_LKF_CANCEL | DLM_LKF_FORCEUNLOCK)) {
2900 log_debug(ls, "unlock on rsb_lookup %x", lkb->lkb_id);
2901 list_del_init(&lkb->lkb_rsb_lookup);
2902 queue_cast(lkb->lkb_resource, lkb,
2903 args->flags & DLM_LKF_CANCEL ?
2904 -DLM_ECANCEL : -DLM_EUNLOCK);
2905 unhold_lkb(lkb); /* undoes create_lkb() */
2907 /* caller changes -EBUSY to 0 for CANCEL and FORCEUNLOCK */
2912 if (test_bit(DLM_IFL_MSTCPY_BIT, &lkb->lkb_iflags)) {
2913 log_error(ls, "unlock on MSTCPY %x", lkb->lkb_id);
2918 /* an lkb may still exist even though the lock is EOL'ed due to a
2919 * cancel, unlock or failed noqueue request; an app can't use these
2920 * locks; return same error as if the lkid had not been found at all
2923 if (test_bit(DLM_IFL_ENDOFLIFE_BIT, &lkb->lkb_iflags)) {
2924 log_debug(ls, "unlock on ENDOFLIFE %x", lkb->lkb_id);
2929 /* cancel not allowed with another cancel/unlock in progress */
2931 if (args->flags & DLM_LKF_CANCEL) {
2932 if (lkb->lkb_exflags & DLM_LKF_CANCEL)
2935 if (is_overlap(lkb))
2938 if (test_bit(DLM_IFL_RESEND_BIT, &lkb->lkb_iflags)) {
2939 set_bit(DLM_IFL_OVERLAP_CANCEL_BIT, &lkb->lkb_iflags);
2944 /* there's nothing to cancel */
2945 if (lkb->lkb_status == DLM_LKSTS_GRANTED &&
2946 !lkb->lkb_wait_type) {
2951 switch (lkb->lkb_wait_type) {
2952 case DLM_MSG_LOOKUP:
2953 case DLM_MSG_REQUEST:
2954 set_bit(DLM_IFL_OVERLAP_CANCEL_BIT, &lkb->lkb_iflags);
2957 case DLM_MSG_UNLOCK:
2958 case DLM_MSG_CANCEL:
2961 /* add_to_waiters() will set OVERLAP_CANCEL */
2965 /* do we need to allow a force-unlock if there's a normal unlock
2966 already in progress? in what conditions could the normal unlock
2967 fail such that we'd want to send a force-unlock to be sure? */
2969 if (args->flags & DLM_LKF_FORCEUNLOCK) {
2970 if (lkb->lkb_exflags & DLM_LKF_FORCEUNLOCK)
2973 if (is_overlap_unlock(lkb))
2976 if (test_bit(DLM_IFL_RESEND_BIT, &lkb->lkb_iflags)) {
2977 set_bit(DLM_IFL_OVERLAP_UNLOCK_BIT, &lkb->lkb_iflags);
2982 switch (lkb->lkb_wait_type) {
2983 case DLM_MSG_LOOKUP:
2984 case DLM_MSG_REQUEST:
2985 set_bit(DLM_IFL_OVERLAP_UNLOCK_BIT, &lkb->lkb_iflags);
2988 case DLM_MSG_UNLOCK:
2991 /* add_to_waiters() will set OVERLAP_UNLOCK */
2995 /* an overlapping op shouldn't blow away exflags from other op */
2996 lkb->lkb_exflags |= args->flags;
2997 dlm_set_sbflags_val(lkb, 0);
2998 lkb->lkb_astparam = args->astparam;
3005 /* annoy the user because dlm usage is wrong */
3007 log_error(ls, "%s %d %x %x %x %x %d %s", __func__, rv,
3008 lkb->lkb_id, dlm_iflags_val(lkb), lkb->lkb_exflags,
3009 args->flags, lkb->lkb_wait_type,
3010 lkb->lkb_resource->res_name);
3013 log_debug(ls, "%s %d %x %x %x %x %d %s", __func__, rv,
3014 lkb->lkb_id, dlm_iflags_val(lkb), lkb->lkb_exflags,
3015 args->flags, lkb->lkb_wait_type,
3016 lkb->lkb_resource->res_name);
3024 * Four stage 4 varieties:
3025 * do_request(), do_convert(), do_unlock(), do_cancel()
3026 * These are called on the master node for the given lock and
3027 * from the central locking logic.
3030 static int do_request(struct dlm_rsb *r, struct dlm_lkb *lkb)
3034 if (can_be_granted(r, lkb, 1, 0, NULL)) {
3036 queue_cast(r, lkb, 0);
3040 if (can_be_queued(lkb)) {
3041 error = -EINPROGRESS;
3042 add_lkb(r, lkb, DLM_LKSTS_WAITING);
3047 queue_cast(r, lkb, -EAGAIN);
3052 static void do_request_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
3057 if (force_blocking_asts(lkb))
3058 send_blocking_asts_all(r, lkb);
3061 send_blocking_asts(r, lkb);
3066 static int do_convert(struct dlm_rsb *r, struct dlm_lkb *lkb)
3071 /* changing an existing lock may allow others to be granted */
3073 if (can_be_granted(r, lkb, 1, 0, &deadlk)) {
3075 queue_cast(r, lkb, 0);
3079 /* can_be_granted() detected that this lock would block in a conversion
3080 deadlock, so we leave it on the granted queue and return EDEADLK in
3081 the ast for the convert. */
3083 if (deadlk && !(lkb->lkb_exflags & DLM_LKF_NODLCKWT)) {
3084 /* it's left on the granted queue */
3085 revert_lock(r, lkb);
3086 queue_cast(r, lkb, -EDEADLK);
3091 /* is_demoted() means the can_be_granted() above set the grmode
3092 to NL, and left us on the granted queue. This auto-demotion
3093 (due to CONVDEADLK) might mean other locks, and/or this lock, are
3094 now grantable. We have to try to grant other converting locks
3095 before we try again to grant this one. */
3097 if (is_demoted(lkb)) {
3098 grant_pending_convert(r, DLM_LOCK_IV, NULL, NULL);
3099 if (_can_be_granted(r, lkb, 1, 0)) {
3101 queue_cast(r, lkb, 0);
3104 /* else fall through and move to convert queue */
3107 if (can_be_queued(lkb)) {
3108 error = -EINPROGRESS;
3110 add_lkb(r, lkb, DLM_LKSTS_CONVERT);
3115 queue_cast(r, lkb, -EAGAIN);
3120 static void do_convert_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
3125 grant_pending_locks(r, NULL);
3126 /* grant_pending_locks also sends basts */
3129 if (force_blocking_asts(lkb))
3130 send_blocking_asts_all(r, lkb);
3133 send_blocking_asts(r, lkb);
3138 static int do_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
3140 remove_lock(r, lkb);
3141 queue_cast(r, lkb, -DLM_EUNLOCK);
3142 return -DLM_EUNLOCK;
3145 static void do_unlock_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
3148 grant_pending_locks(r, NULL);
3151 /* returns: 0 did nothing, -DLM_ECANCEL canceled lock */
3153 static int do_cancel(struct dlm_rsb *r, struct dlm_lkb *lkb)
3157 error = revert_lock(r, lkb);
3159 queue_cast(r, lkb, -DLM_ECANCEL);
3160 return -DLM_ECANCEL;
3165 static void do_cancel_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
3169 grant_pending_locks(r, NULL);
3173 * Four stage 3 varieties:
3174 * _request_lock(), _convert_lock(), _unlock_lock(), _cancel_lock()
3177 /* add a new lkb to a possibly new rsb, called by requesting process */
3179 static int _request_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
3183 /* set_master: sets lkb nodeid from r */
3185 error = set_master(r, lkb);
3194 /* receive_request() calls do_request() on remote node */
3195 error = send_request(r, lkb);
3197 error = do_request(r, lkb);
3198 /* for remote locks the request_reply is sent
3199 between do_request and do_request_effects */
3200 do_request_effects(r, lkb, error);
3206 /* change some property of an existing lkb, e.g. mode */
3208 static int _convert_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
3213 /* receive_convert() calls do_convert() on remote node */
3214 error = send_convert(r, lkb);
3216 error = do_convert(r, lkb);
3217 /* for remote locks the convert_reply is sent
3218 between do_convert and do_convert_effects */
3219 do_convert_effects(r, lkb, error);
3225 /* remove an existing lkb from the granted queue */
3227 static int _unlock_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
3232 /* receive_unlock() calls do_unlock() on remote node */
3233 error = send_unlock(r, lkb);
3235 error = do_unlock(r, lkb);
3236 /* for remote locks the unlock_reply is sent
3237 between do_unlock and do_unlock_effects */
3238 do_unlock_effects(r, lkb, error);
3244 /* remove an existing lkb from the convert or wait queue */
3246 static int _cancel_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
3251 /* receive_cancel() calls do_cancel() on remote node */
3252 error = send_cancel(r, lkb);
3254 error = do_cancel(r, lkb);
3255 /* for remote locks the cancel_reply is sent
3256 between do_cancel and do_cancel_effects */
3257 do_cancel_effects(r, lkb, error);
3264 * Four stage 2 varieties:
3265 * request_lock(), convert_lock(), unlock_lock(), cancel_lock()
3268 static int request_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
3269 const void *name, int len,
3270 struct dlm_args *args)
3275 error = validate_lock_args(ls, lkb, args);
3279 error = find_rsb(ls, name, len, 0, R_REQUEST, &r);
3286 lkb->lkb_lksb->sb_lkid = lkb->lkb_id;
3288 error = _request_lock(r, lkb);
3295 static int convert_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
3296 struct dlm_args *args)
3301 r = lkb->lkb_resource;
3306 error = validate_lock_args(ls, lkb, args);
3310 error = _convert_lock(r, lkb);
3317 static int unlock_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
3318 struct dlm_args *args)
3323 r = lkb->lkb_resource;
3328 error = validate_unlock_args(lkb, args);
3332 error = _unlock_lock(r, lkb);
3339 static int cancel_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
3340 struct dlm_args *args)
3345 r = lkb->lkb_resource;
3350 error = validate_unlock_args(lkb, args);
3354 error = _cancel_lock(r, lkb);
3362 * Two stage 1 varieties: dlm_lock() and dlm_unlock()
3365 int dlm_lock(dlm_lockspace_t *lockspace,
3367 struct dlm_lksb *lksb,
3370 unsigned int namelen,
3371 uint32_t parent_lkid,
3372 void (*ast) (void *astarg),
3374 void (*bast) (void *astarg, int mode))
3377 struct dlm_lkb *lkb;
3378 struct dlm_args args;
3379 int error, convert = flags & DLM_LKF_CONVERT;
3381 ls = dlm_find_lockspace_local(lockspace);
3385 dlm_lock_recovery(ls);
3388 error = find_lkb(ls, lksb->sb_lkid, &lkb);
3390 error = create_lkb(ls, &lkb);
3395 trace_dlm_lock_start(ls, lkb, name, namelen, mode, flags);
3397 error = set_lock_args(mode, lksb, flags, namelen, ast, astarg, bast,
3403 error = convert_lock(ls, lkb, &args);
3405 error = request_lock(ls, lkb, name, namelen, &args);
3407 if (error == -EINPROGRESS)
3410 trace_dlm_lock_end(ls, lkb, name, namelen, mode, flags, error, true);
3412 if (convert || error)
3414 if (error == -EAGAIN || error == -EDEADLK)
3417 dlm_unlock_recovery(ls);
3418 dlm_put_lockspace(ls);
3422 int dlm_unlock(dlm_lockspace_t *lockspace,
3425 struct dlm_lksb *lksb,
3429 struct dlm_lkb *lkb;
3430 struct dlm_args args;
3433 ls = dlm_find_lockspace_local(lockspace);
3437 dlm_lock_recovery(ls);
3439 error = find_lkb(ls, lkid, &lkb);
3443 trace_dlm_unlock_start(ls, lkb, flags);
3445 error = set_unlock_args(flags, astarg, &args);
3449 if (flags & DLM_LKF_CANCEL)
3450 error = cancel_lock(ls, lkb, &args);
3452 error = unlock_lock(ls, lkb, &args);
3454 if (error == -DLM_EUNLOCK || error == -DLM_ECANCEL)
3456 if (error == -EBUSY && (flags & (DLM_LKF_CANCEL | DLM_LKF_FORCEUNLOCK)))
3459 trace_dlm_unlock_end(ls, lkb, flags, error);
3463 dlm_unlock_recovery(ls);
3464 dlm_put_lockspace(ls);
3469 * send/receive routines for remote operations and replies
3473 * send_request receive_request
3474 * send_convert receive_convert
3475 * send_unlock receive_unlock
3476 * send_cancel receive_cancel
3477 * send_grant receive_grant
3478 * send_bast receive_bast
3479 * send_lookup receive_lookup
3480 * send_remove receive_remove
3483 * receive_request_reply send_request_reply
3484 * receive_convert_reply send_convert_reply
3485 * receive_unlock_reply send_unlock_reply
3486 * receive_cancel_reply send_cancel_reply
3487 * receive_lookup_reply send_lookup_reply
3490 static int _create_message(struct dlm_ls *ls, int mb_len,
3491 int to_nodeid, int mstype,
3492 struct dlm_message **ms_ret,
3493 struct dlm_mhandle **mh_ret)
3495 struct dlm_message *ms;
3496 struct dlm_mhandle *mh;
3499 /* get_buffer gives us a message handle (mh) that we need to
3500 pass into midcomms_commit and a message buffer (mb) that we
3501 write our data into */
3503 mh = dlm_midcomms_get_mhandle(to_nodeid, mb_len, &mb);
3507 ms = (struct dlm_message *) mb;
3509 ms->m_header.h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
3510 ms->m_header.u.h_lockspace = cpu_to_le32(ls->ls_global_id);
3511 ms->m_header.h_nodeid = cpu_to_le32(dlm_our_nodeid());
3512 ms->m_header.h_length = cpu_to_le16(mb_len);
3513 ms->m_header.h_cmd = DLM_MSG;
3515 ms->m_type = cpu_to_le32(mstype);
3522 static int create_message(struct dlm_rsb *r, struct dlm_lkb *lkb,
3523 int to_nodeid, int mstype,
3524 struct dlm_message **ms_ret,
3525 struct dlm_mhandle **mh_ret)
3527 int mb_len = sizeof(struct dlm_message);
3530 case DLM_MSG_REQUEST:
3531 case DLM_MSG_LOOKUP:
3532 case DLM_MSG_REMOVE:
3533 mb_len += r->res_length;
3535 case DLM_MSG_CONVERT:
3536 case DLM_MSG_UNLOCK:
3537 case DLM_MSG_REQUEST_REPLY:
3538 case DLM_MSG_CONVERT_REPLY:
3540 if (lkb && lkb->lkb_lvbptr && (lkb->lkb_exflags & DLM_LKF_VALBLK))
3541 mb_len += r->res_ls->ls_lvblen;
3545 return _create_message(r->res_ls, mb_len, to_nodeid, mstype,
3549 /* further lowcomms enhancements or alternate implementations may make
3550 the return value from this function useful at some point */
3552 static int send_message(struct dlm_mhandle *mh, struct dlm_message *ms,
3553 const void *name, int namelen)
3555 dlm_midcomms_commit_mhandle(mh, name, namelen);
3559 static void send_args(struct dlm_rsb *r, struct dlm_lkb *lkb,
3560 struct dlm_message *ms)
3562 ms->m_nodeid = cpu_to_le32(lkb->lkb_nodeid);
3563 ms->m_pid = cpu_to_le32(lkb->lkb_ownpid);
3564 ms->m_lkid = cpu_to_le32(lkb->lkb_id);
3565 ms->m_remid = cpu_to_le32(lkb->lkb_remid);
3566 ms->m_exflags = cpu_to_le32(lkb->lkb_exflags);
3567 ms->m_sbflags = cpu_to_le32(dlm_sbflags_val(lkb));
3568 ms->m_flags = cpu_to_le32(dlm_dflags_val(lkb));
3569 ms->m_lvbseq = cpu_to_le32(lkb->lkb_lvbseq);
3570 ms->m_status = cpu_to_le32(lkb->lkb_status);
3571 ms->m_grmode = cpu_to_le32(lkb->lkb_grmode);
3572 ms->m_rqmode = cpu_to_le32(lkb->lkb_rqmode);
3573 ms->m_hash = cpu_to_le32(r->res_hash);
3575 /* m_result and m_bastmode are set from function args,
3576 not from lkb fields */
3578 if (lkb->lkb_bastfn)
3579 ms->m_asts |= cpu_to_le32(DLM_CB_BAST);
3581 ms->m_asts |= cpu_to_le32(DLM_CB_CAST);
3583 /* compare with switch in create_message; send_remove() doesn't
3586 switch (ms->m_type) {
3587 case cpu_to_le32(DLM_MSG_REQUEST):
3588 case cpu_to_le32(DLM_MSG_LOOKUP):
3589 memcpy(ms->m_extra, r->res_name, r->res_length);
3591 case cpu_to_le32(DLM_MSG_CONVERT):
3592 case cpu_to_le32(DLM_MSG_UNLOCK):
3593 case cpu_to_le32(DLM_MSG_REQUEST_REPLY):
3594 case cpu_to_le32(DLM_MSG_CONVERT_REPLY):
3595 case cpu_to_le32(DLM_MSG_GRANT):
3596 if (!lkb->lkb_lvbptr || !(lkb->lkb_exflags & DLM_LKF_VALBLK))
3598 memcpy(ms->m_extra, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
3603 static int send_common(struct dlm_rsb *r, struct dlm_lkb *lkb, int mstype)
3605 struct dlm_message *ms;
3606 struct dlm_mhandle *mh;
3607 int to_nodeid, error;
3609 to_nodeid = r->res_nodeid;
3611 error = add_to_waiters(lkb, mstype, to_nodeid);
3615 error = create_message(r, lkb, to_nodeid, mstype, &ms, &mh);
3619 send_args(r, lkb, ms);
3621 error = send_message(mh, ms, r->res_name, r->res_length);
3627 remove_from_waiters(lkb, msg_reply_type(mstype));
3631 static int send_request(struct dlm_rsb *r, struct dlm_lkb *lkb)
3633 return send_common(r, lkb, DLM_MSG_REQUEST);
3636 static int send_convert(struct dlm_rsb *r, struct dlm_lkb *lkb)
3640 error = send_common(r, lkb, DLM_MSG_CONVERT);
3642 /* down conversions go without a reply from the master */
3643 if (!error && down_conversion(lkb)) {
3644 remove_from_waiters(lkb, DLM_MSG_CONVERT_REPLY);
3645 r->res_ls->ls_local_ms.m_type = cpu_to_le32(DLM_MSG_CONVERT_REPLY);
3646 r->res_ls->ls_local_ms.m_result = 0;
3647 __receive_convert_reply(r, lkb, &r->res_ls->ls_local_ms, true);
3653 /* FIXME: if this lkb is the only lock we hold on the rsb, then set
3654 MASTER_UNCERTAIN to force the next request on the rsb to confirm
3655 that the master is still correct. */
3657 static int send_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
3659 return send_common(r, lkb, DLM_MSG_UNLOCK);
3662 static int send_cancel(struct dlm_rsb *r, struct dlm_lkb *lkb)
3664 return send_common(r, lkb, DLM_MSG_CANCEL);
3667 static int send_grant(struct dlm_rsb *r, struct dlm_lkb *lkb)
3669 struct dlm_message *ms;
3670 struct dlm_mhandle *mh;
3671 int to_nodeid, error;
3673 to_nodeid = lkb->lkb_nodeid;
3675 error = create_message(r, lkb, to_nodeid, DLM_MSG_GRANT, &ms, &mh);
3679 send_args(r, lkb, ms);
3683 error = send_message(mh, ms, r->res_name, r->res_length);
3688 static int send_bast(struct dlm_rsb *r, struct dlm_lkb *lkb, int mode)
3690 struct dlm_message *ms;
3691 struct dlm_mhandle *mh;
3692 int to_nodeid, error;
3694 to_nodeid = lkb->lkb_nodeid;
3696 error = create_message(r, NULL, to_nodeid, DLM_MSG_BAST, &ms, &mh);
3700 send_args(r, lkb, ms);
3702 ms->m_bastmode = cpu_to_le32(mode);
3704 error = send_message(mh, ms, r->res_name, r->res_length);
3709 static int send_lookup(struct dlm_rsb *r, struct dlm_lkb *lkb)
3711 struct dlm_message *ms;
3712 struct dlm_mhandle *mh;
3713 int to_nodeid, error;
3715 to_nodeid = dlm_dir_nodeid(r);
3717 error = add_to_waiters(lkb, DLM_MSG_LOOKUP, to_nodeid);
3721 error = create_message(r, NULL, to_nodeid, DLM_MSG_LOOKUP, &ms, &mh);
3725 send_args(r, lkb, ms);
3727 error = send_message(mh, ms, r->res_name, r->res_length);
3733 remove_from_waiters(lkb, DLM_MSG_LOOKUP_REPLY);
3737 static int send_remove(struct dlm_rsb *r)
3739 struct dlm_message *ms;
3740 struct dlm_mhandle *mh;
3741 int to_nodeid, error;
3743 to_nodeid = dlm_dir_nodeid(r);
3745 error = create_message(r, NULL, to_nodeid, DLM_MSG_REMOVE, &ms, &mh);
3749 memcpy(ms->m_extra, r->res_name, r->res_length);
3750 ms->m_hash = cpu_to_le32(r->res_hash);
3752 error = send_message(mh, ms, r->res_name, r->res_length);
3757 static int send_common_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
3760 struct dlm_message *ms;
3761 struct dlm_mhandle *mh;
3762 int to_nodeid, error;
3764 to_nodeid = lkb->lkb_nodeid;
3766 error = create_message(r, lkb, to_nodeid, mstype, &ms, &mh);
3770 send_args(r, lkb, ms);
3772 ms->m_result = cpu_to_le32(to_dlm_errno(rv));
3774 error = send_message(mh, ms, r->res_name, r->res_length);
3779 static int send_request_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
3781 return send_common_reply(r, lkb, DLM_MSG_REQUEST_REPLY, rv);
3784 static int send_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
3786 return send_common_reply(r, lkb, DLM_MSG_CONVERT_REPLY, rv);
3789 static int send_unlock_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
3791 return send_common_reply(r, lkb, DLM_MSG_UNLOCK_REPLY, rv);
3794 static int send_cancel_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
3796 return send_common_reply(r, lkb, DLM_MSG_CANCEL_REPLY, rv);
3799 static int send_lookup_reply(struct dlm_ls *ls,
3800 const struct dlm_message *ms_in, int ret_nodeid,
3803 struct dlm_rsb *r = &ls->ls_local_rsb;
3804 struct dlm_message *ms;
3805 struct dlm_mhandle *mh;
3806 int error, nodeid = le32_to_cpu(ms_in->m_header.h_nodeid);
3808 error = create_message(r, NULL, nodeid, DLM_MSG_LOOKUP_REPLY, &ms, &mh);
3812 ms->m_lkid = ms_in->m_lkid;
3813 ms->m_result = cpu_to_le32(to_dlm_errno(rv));
3814 ms->m_nodeid = cpu_to_le32(ret_nodeid);
3816 error = send_message(mh, ms, ms_in->m_extra, receive_extralen(ms_in));
3821 /* which args we save from a received message depends heavily on the type
3822 of message, unlike the send side where we can safely send everything about
3823 the lkb for any type of message */
3825 static void receive_flags(struct dlm_lkb *lkb, const struct dlm_message *ms)
3827 lkb->lkb_exflags = le32_to_cpu(ms->m_exflags);
3828 dlm_set_sbflags_val(lkb, le32_to_cpu(ms->m_sbflags));
3829 dlm_set_dflags_val(lkb, le32_to_cpu(ms->m_flags));
3832 static void receive_flags_reply(struct dlm_lkb *lkb,
3833 const struct dlm_message *ms,
3839 dlm_set_sbflags_val(lkb, le32_to_cpu(ms->m_sbflags));
3840 dlm_set_dflags_val(lkb, le32_to_cpu(ms->m_flags));
3843 static int receive_extralen(const struct dlm_message *ms)
3845 return (le16_to_cpu(ms->m_header.h_length) -
3846 sizeof(struct dlm_message));
3849 static int receive_lvb(struct dlm_ls *ls, struct dlm_lkb *lkb,
3850 const struct dlm_message *ms)
3854 if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
3855 if (!lkb->lkb_lvbptr)
3856 lkb->lkb_lvbptr = dlm_allocate_lvb(ls);
3857 if (!lkb->lkb_lvbptr)
3859 len = receive_extralen(ms);
3860 if (len > ls->ls_lvblen)
3861 len = ls->ls_lvblen;
3862 memcpy(lkb->lkb_lvbptr, ms->m_extra, len);
3867 static void fake_bastfn(void *astparam, int mode)
3869 log_print("fake_bastfn should not be called");
3872 static void fake_astfn(void *astparam)
3874 log_print("fake_astfn should not be called");
3877 static int receive_request_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
3878 const struct dlm_message *ms)
3880 lkb->lkb_nodeid = le32_to_cpu(ms->m_header.h_nodeid);
3881 lkb->lkb_ownpid = le32_to_cpu(ms->m_pid);
3882 lkb->lkb_remid = le32_to_cpu(ms->m_lkid);
3883 lkb->lkb_grmode = DLM_LOCK_IV;
3884 lkb->lkb_rqmode = le32_to_cpu(ms->m_rqmode);
3886 lkb->lkb_bastfn = (ms->m_asts & cpu_to_le32(DLM_CB_BAST)) ? &fake_bastfn : NULL;
3887 lkb->lkb_astfn = (ms->m_asts & cpu_to_le32(DLM_CB_CAST)) ? &fake_astfn : NULL;
3889 if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
3890 /* lkb was just created so there won't be an lvb yet */
3891 lkb->lkb_lvbptr = dlm_allocate_lvb(ls);
3892 if (!lkb->lkb_lvbptr)
3899 static int receive_convert_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
3900 const struct dlm_message *ms)
3902 if (lkb->lkb_status != DLM_LKSTS_GRANTED)
3905 if (receive_lvb(ls, lkb, ms))
3908 lkb->lkb_rqmode = le32_to_cpu(ms->m_rqmode);
3909 lkb->lkb_lvbseq = le32_to_cpu(ms->m_lvbseq);
3914 static int receive_unlock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
3915 const struct dlm_message *ms)
3917 if (receive_lvb(ls, lkb, ms))
3922 /* We fill in the local-lkb fields with the info that send_xxxx_reply()
3923 uses to send a reply and that the remote end uses to process the reply. */
3925 static void setup_local_lkb(struct dlm_ls *ls, const struct dlm_message *ms)
3927 struct dlm_lkb *lkb = &ls->ls_local_lkb;
3928 lkb->lkb_nodeid = le32_to_cpu(ms->m_header.h_nodeid);
3929 lkb->lkb_remid = le32_to_cpu(ms->m_lkid);
3932 /* This is called after the rsb is locked so that we can safely inspect
3933 fields in the lkb. */
3935 static int validate_message(struct dlm_lkb *lkb, const struct dlm_message *ms)
3937 int from = le32_to_cpu(ms->m_header.h_nodeid);
3940 /* currently mixing of user/kernel locks are not supported */
3941 if (ms->m_flags & cpu_to_le32(BIT(DLM_DFL_USER_BIT)) &&
3942 !test_bit(DLM_DFL_USER_BIT, &lkb->lkb_dflags)) {
3943 log_error(lkb->lkb_resource->res_ls,
3944 "got user dlm message for a kernel lock");
3949 switch (ms->m_type) {
3950 case cpu_to_le32(DLM_MSG_CONVERT):
3951 case cpu_to_le32(DLM_MSG_UNLOCK):
3952 case cpu_to_le32(DLM_MSG_CANCEL):
3953 if (!is_master_copy(lkb) || lkb->lkb_nodeid != from)
3957 case cpu_to_le32(DLM_MSG_CONVERT_REPLY):
3958 case cpu_to_le32(DLM_MSG_UNLOCK_REPLY):
3959 case cpu_to_le32(DLM_MSG_CANCEL_REPLY):
3960 case cpu_to_le32(DLM_MSG_GRANT):
3961 case cpu_to_le32(DLM_MSG_BAST):
3962 if (!is_process_copy(lkb) || lkb->lkb_nodeid != from)
3966 case cpu_to_le32(DLM_MSG_REQUEST_REPLY):
3967 if (!is_process_copy(lkb))
3969 else if (lkb->lkb_nodeid != -1 && lkb->lkb_nodeid != from)
3979 log_error(lkb->lkb_resource->res_ls,
3980 "ignore invalid message %d from %d %x %x %x %d",
3981 le32_to_cpu(ms->m_type), from, lkb->lkb_id,
3982 lkb->lkb_remid, dlm_iflags_val(lkb),
3987 static int receive_request(struct dlm_ls *ls, const struct dlm_message *ms)
3989 struct dlm_lkb *lkb;
3992 int error, namelen = 0;
3994 from_nodeid = le32_to_cpu(ms->m_header.h_nodeid);
3996 error = create_lkb(ls, &lkb);
4000 receive_flags(lkb, ms);
4001 set_bit(DLM_IFL_MSTCPY_BIT, &lkb->lkb_iflags);
4002 error = receive_request_args(ls, lkb, ms);
4008 /* The dir node is the authority on whether we are the master
4009 for this rsb or not, so if the master sends us a request, we should
4010 recreate the rsb if we've destroyed it. This race happens when we
4011 send a remove message to the dir node at the same time that the dir
4012 node sends us a request for the rsb. */
4014 namelen = receive_extralen(ms);
4016 error = find_rsb(ls, ms->m_extra, namelen, from_nodeid,
4017 R_RECEIVE_REQUEST, &r);
4025 if (r->res_master_nodeid != dlm_our_nodeid()) {
4026 error = validate_master_nodeid(ls, r, from_nodeid);
4036 error = do_request(r, lkb);
4037 send_request_reply(r, lkb, error);
4038 do_request_effects(r, lkb, error);
4043 if (error == -EINPROGRESS)
4050 /* TODO: instead of returning ENOTBLK, add the lkb to res_lookup
4051 and do this receive_request again from process_lookup_list once
4052 we get the lookup reply. This would avoid a many repeated
4053 ENOTBLK request failures when the lookup reply designating us
4054 as master is delayed. */
4056 if (error != -ENOTBLK) {
4057 log_limit(ls, "receive_request %x from %d %d",
4058 le32_to_cpu(ms->m_lkid), from_nodeid, error);
4061 setup_local_lkb(ls, ms);
4062 send_request_reply(&ls->ls_local_rsb, &ls->ls_local_lkb, error);
4066 static int receive_convert(struct dlm_ls *ls, const struct dlm_message *ms)
4068 struct dlm_lkb *lkb;
4070 int error, reply = 1;
4072 error = find_lkb(ls, le32_to_cpu(ms->m_remid), &lkb);
4076 if (lkb->lkb_remid != le32_to_cpu(ms->m_lkid)) {
4077 log_error(ls, "receive_convert %x remid %x recover_seq %llu "
4078 "remote %d %x", lkb->lkb_id, lkb->lkb_remid,
4079 (unsigned long long)lkb->lkb_recover_seq,
4080 le32_to_cpu(ms->m_header.h_nodeid),
4081 le32_to_cpu(ms->m_lkid));
4087 r = lkb->lkb_resource;
4092 error = validate_message(lkb, ms);
4096 receive_flags(lkb, ms);
4098 error = receive_convert_args(ls, lkb, ms);
4100 send_convert_reply(r, lkb, error);
4104 reply = !down_conversion(lkb);
4106 error = do_convert(r, lkb);
4108 send_convert_reply(r, lkb, error);
4109 do_convert_effects(r, lkb, error);
4117 setup_local_lkb(ls, ms);
4118 send_convert_reply(&ls->ls_local_rsb, &ls->ls_local_lkb, error);
4122 static int receive_unlock(struct dlm_ls *ls, const struct dlm_message *ms)
4124 struct dlm_lkb *lkb;
4128 error = find_lkb(ls, le32_to_cpu(ms->m_remid), &lkb);
4132 if (lkb->lkb_remid != le32_to_cpu(ms->m_lkid)) {
4133 log_error(ls, "receive_unlock %x remid %x remote %d %x",
4134 lkb->lkb_id, lkb->lkb_remid,
4135 le32_to_cpu(ms->m_header.h_nodeid),
4136 le32_to_cpu(ms->m_lkid));
4142 r = lkb->lkb_resource;
4147 error = validate_message(lkb, ms);
4151 receive_flags(lkb, ms);
4153 error = receive_unlock_args(ls, lkb, ms);
4155 send_unlock_reply(r, lkb, error);
4159 error = do_unlock(r, lkb);
4160 send_unlock_reply(r, lkb, error);
4161 do_unlock_effects(r, lkb, error);
4169 setup_local_lkb(ls, ms);
4170 send_unlock_reply(&ls->ls_local_rsb, &ls->ls_local_lkb, error);
4174 static int receive_cancel(struct dlm_ls *ls, const struct dlm_message *ms)
4176 struct dlm_lkb *lkb;
4180 error = find_lkb(ls, le32_to_cpu(ms->m_remid), &lkb);
4184 receive_flags(lkb, ms);
4186 r = lkb->lkb_resource;
4191 error = validate_message(lkb, ms);
4195 error = do_cancel(r, lkb);
4196 send_cancel_reply(r, lkb, error);
4197 do_cancel_effects(r, lkb, error);
4205 setup_local_lkb(ls, ms);
4206 send_cancel_reply(&ls->ls_local_rsb, &ls->ls_local_lkb, error);
4210 static int receive_grant(struct dlm_ls *ls, const struct dlm_message *ms)
4212 struct dlm_lkb *lkb;
4216 error = find_lkb(ls, le32_to_cpu(ms->m_remid), &lkb);
4220 r = lkb->lkb_resource;
4225 error = validate_message(lkb, ms);
4229 receive_flags_reply(lkb, ms, false);
4230 if (is_altmode(lkb))
4231 munge_altmode(lkb, ms);
4232 grant_lock_pc(r, lkb, ms);
4233 queue_cast(r, lkb, 0);
4241 static int receive_bast(struct dlm_ls *ls, const struct dlm_message *ms)
4243 struct dlm_lkb *lkb;
4247 error = find_lkb(ls, le32_to_cpu(ms->m_remid), &lkb);
4251 r = lkb->lkb_resource;
4256 error = validate_message(lkb, ms);
4260 queue_bast(r, lkb, le32_to_cpu(ms->m_bastmode));
4261 lkb->lkb_highbast = le32_to_cpu(ms->m_bastmode);
4269 static void receive_lookup(struct dlm_ls *ls, const struct dlm_message *ms)
4271 int len, error, ret_nodeid, from_nodeid, our_nodeid;
4273 from_nodeid = le32_to_cpu(ms->m_header.h_nodeid);
4274 our_nodeid = dlm_our_nodeid();
4276 len = receive_extralen(ms);
4278 error = dlm_master_lookup(ls, from_nodeid, ms->m_extra, len, 0,
4281 /* Optimization: we're master so treat lookup as a request */
4282 if (!error && ret_nodeid == our_nodeid) {
4283 receive_request(ls, ms);
4286 send_lookup_reply(ls, ms, ret_nodeid, error);
4289 static void receive_remove(struct dlm_ls *ls, const struct dlm_message *ms)
4291 char name[DLM_RESNAME_MAXLEN+1];
4293 int rv, len, dir_nodeid, from_nodeid;
4295 from_nodeid = le32_to_cpu(ms->m_header.h_nodeid);
4297 len = receive_extralen(ms);
4299 if (len > DLM_RESNAME_MAXLEN) {
4300 log_error(ls, "receive_remove from %d bad len %d",
4305 dir_nodeid = dlm_hash2nodeid(ls, le32_to_cpu(ms->m_hash));
4306 if (dir_nodeid != dlm_our_nodeid()) {
4307 log_error(ls, "receive_remove from %d bad nodeid %d",
4308 from_nodeid, dir_nodeid);
4313 * Look for inactive rsb, if it's there, free it.
4314 * If the rsb is active, it's being used, and we should ignore this
4315 * message. This is an expected race between the dir node sending a
4316 * request to the master node at the same time as the master node sends
4317 * a remove to the dir node. The resolution to that race is for the
4318 * dir node to ignore the remove message, and the master node to
4319 * recreate the master rsb when it gets a request from the dir node for
4320 * an rsb it doesn't have.
4323 memset(name, 0, sizeof(name));
4324 memcpy(name, ms->m_extra, len);
4326 write_lock_bh(&ls->ls_rsbtbl_lock);
4328 rv = dlm_search_rsb_tree(&ls->ls_rsbtbl, name, len, &r);
4330 /* should not happen */
4331 log_error(ls, "%s from %d not found %s", __func__,
4333 write_unlock_bh(&ls->ls_rsbtbl_lock);
4337 if (!rsb_flag(r, RSB_INACTIVE)) {
4338 if (r->res_master_nodeid != from_nodeid) {
4339 /* should not happen */
4340 log_error(ls, "receive_remove on active rsb from %d master %d",
4341 from_nodeid, r->res_master_nodeid);
4343 write_unlock_bh(&ls->ls_rsbtbl_lock);
4347 /* Ignore the remove message, see race comment above. */
4349 log_debug(ls, "receive_remove from %d master %d first %x %s",
4350 from_nodeid, r->res_master_nodeid, r->res_first_lkid,
4352 write_unlock_bh(&ls->ls_rsbtbl_lock);
4356 if (r->res_master_nodeid != from_nodeid) {
4357 log_error(ls, "receive_remove inactive from %d master %d",
4358 from_nodeid, r->res_master_nodeid);
4360 write_unlock_bh(&ls->ls_rsbtbl_lock);
4364 list_del(&r->res_slow_list);
4365 rhashtable_remove_fast(&ls->ls_rsbtbl, &r->res_node,
4366 dlm_rhash_rsb_params);
4367 rsb_clear_flag(r, RSB_HASHED);
4368 write_unlock_bh(&ls->ls_rsbtbl_lock);
4370 free_inactive_rsb(r);
4373 static void receive_purge(struct dlm_ls *ls, const struct dlm_message *ms)
4375 do_purge(ls, le32_to_cpu(ms->m_nodeid), le32_to_cpu(ms->m_pid));
4378 static int receive_request_reply(struct dlm_ls *ls,
4379 const struct dlm_message *ms)
4381 struct dlm_lkb *lkb;
4383 int error, mstype, result;
4384 int from_nodeid = le32_to_cpu(ms->m_header.h_nodeid);
4386 error = find_lkb(ls, le32_to_cpu(ms->m_remid), &lkb);
4390 r = lkb->lkb_resource;
4394 error = validate_message(lkb, ms);
4398 mstype = lkb->lkb_wait_type;
4399 error = remove_from_waiters(lkb, DLM_MSG_REQUEST_REPLY);
4401 log_error(ls, "receive_request_reply %x remote %d %x result %d",
4402 lkb->lkb_id, from_nodeid, le32_to_cpu(ms->m_lkid),
4403 from_dlm_errno(le32_to_cpu(ms->m_result)));
4408 /* Optimization: the dir node was also the master, so it took our
4409 lookup as a request and sent request reply instead of lookup reply */
4410 if (mstype == DLM_MSG_LOOKUP) {
4411 r->res_master_nodeid = from_nodeid;
4412 r->res_nodeid = from_nodeid;
4413 lkb->lkb_nodeid = from_nodeid;
4416 /* this is the value returned from do_request() on the master */
4417 result = from_dlm_errno(le32_to_cpu(ms->m_result));
4421 /* request would block (be queued) on remote master */
4422 queue_cast(r, lkb, -EAGAIN);
4423 confirm_master(r, -EAGAIN);
4424 unhold_lkb(lkb); /* undoes create_lkb() */
4429 /* request was queued or granted on remote master */
4430 receive_flags_reply(lkb, ms, false);
4431 lkb->lkb_remid = le32_to_cpu(ms->m_lkid);
4432 if (is_altmode(lkb))
4433 munge_altmode(lkb, ms);
4435 add_lkb(r, lkb, DLM_LKSTS_WAITING);
4437 grant_lock_pc(r, lkb, ms);
4438 queue_cast(r, lkb, 0);
4440 confirm_master(r, result);
4445 /* find_rsb failed to find rsb or rsb wasn't master */
4446 log_limit(ls, "receive_request_reply %x from %d %d "
4447 "master %d dir %d first %x %s", lkb->lkb_id,
4448 from_nodeid, result, r->res_master_nodeid,
4449 r->res_dir_nodeid, r->res_first_lkid, r->res_name);
4451 if (r->res_dir_nodeid != dlm_our_nodeid() &&
4452 r->res_master_nodeid != dlm_our_nodeid()) {
4453 /* cause _request_lock->set_master->send_lookup */
4454 r->res_master_nodeid = 0;
4456 lkb->lkb_nodeid = -1;
4459 if (is_overlap(lkb)) {
4460 /* we'll ignore error in cancel/unlock reply */
4461 queue_cast_overlap(r, lkb);
4462 confirm_master(r, result);
4463 unhold_lkb(lkb); /* undoes create_lkb() */
4465 _request_lock(r, lkb);
4467 if (r->res_master_nodeid == dlm_our_nodeid())
4468 confirm_master(r, 0);
4473 log_error(ls, "receive_request_reply %x error %d",
4474 lkb->lkb_id, result);
4477 if ((result == 0 || result == -EINPROGRESS) &&
4478 test_and_clear_bit(DLM_IFL_OVERLAP_UNLOCK_BIT, &lkb->lkb_iflags)) {
4479 log_debug(ls, "receive_request_reply %x result %d unlock",
4480 lkb->lkb_id, result);
4481 clear_bit(DLM_IFL_OVERLAP_CANCEL_BIT, &lkb->lkb_iflags);
4482 send_unlock(r, lkb);
4483 } else if ((result == -EINPROGRESS) &&
4484 test_and_clear_bit(DLM_IFL_OVERLAP_CANCEL_BIT,
4485 &lkb->lkb_iflags)) {
4486 log_debug(ls, "receive_request_reply %x cancel", lkb->lkb_id);
4487 clear_bit(DLM_IFL_OVERLAP_UNLOCK_BIT, &lkb->lkb_iflags);
4488 send_cancel(r, lkb);
4490 clear_bit(DLM_IFL_OVERLAP_CANCEL_BIT, &lkb->lkb_iflags);
4491 clear_bit(DLM_IFL_OVERLAP_UNLOCK_BIT, &lkb->lkb_iflags);
4500 static void __receive_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
4501 const struct dlm_message *ms, bool local)
4503 /* this is the value returned from do_convert() on the master */
4504 switch (from_dlm_errno(le32_to_cpu(ms->m_result))) {
4506 /* convert would block (be queued) on remote master */
4507 queue_cast(r, lkb, -EAGAIN);
4511 receive_flags_reply(lkb, ms, local);
4512 revert_lock_pc(r, lkb);
4513 queue_cast(r, lkb, -EDEADLK);
4517 /* convert was queued on remote master */
4518 receive_flags_reply(lkb, ms, local);
4519 if (is_demoted(lkb))
4522 add_lkb(r, lkb, DLM_LKSTS_CONVERT);
4526 /* convert was granted on remote master */
4527 receive_flags_reply(lkb, ms, local);
4528 if (is_demoted(lkb))
4530 grant_lock_pc(r, lkb, ms);
4531 queue_cast(r, lkb, 0);
4535 log_error(r->res_ls, "receive_convert_reply %x remote %d %x %d",
4536 lkb->lkb_id, le32_to_cpu(ms->m_header.h_nodeid),
4537 le32_to_cpu(ms->m_lkid),
4538 from_dlm_errno(le32_to_cpu(ms->m_result)));
4544 static void _receive_convert_reply(struct dlm_lkb *lkb,
4545 const struct dlm_message *ms, bool local)
4547 struct dlm_rsb *r = lkb->lkb_resource;
4553 error = validate_message(lkb, ms);
4557 error = remove_from_waiters_ms(lkb, ms, local);
4561 __receive_convert_reply(r, lkb, ms, local);
4567 static int receive_convert_reply(struct dlm_ls *ls,
4568 const struct dlm_message *ms)
4570 struct dlm_lkb *lkb;
4573 error = find_lkb(ls, le32_to_cpu(ms->m_remid), &lkb);
4577 _receive_convert_reply(lkb, ms, false);
4582 static void _receive_unlock_reply(struct dlm_lkb *lkb,
4583 const struct dlm_message *ms, bool local)
4585 struct dlm_rsb *r = lkb->lkb_resource;
4591 error = validate_message(lkb, ms);
4595 error = remove_from_waiters_ms(lkb, ms, local);
4599 /* this is the value returned from do_unlock() on the master */
4601 switch (from_dlm_errno(le32_to_cpu(ms->m_result))) {
4603 receive_flags_reply(lkb, ms, local);
4604 remove_lock_pc(r, lkb);
4605 queue_cast(r, lkb, -DLM_EUNLOCK);
4610 log_error(r->res_ls, "receive_unlock_reply %x error %d",
4611 lkb->lkb_id, from_dlm_errno(le32_to_cpu(ms->m_result)));
4618 static int receive_unlock_reply(struct dlm_ls *ls,
4619 const struct dlm_message *ms)
4621 struct dlm_lkb *lkb;
4624 error = find_lkb(ls, le32_to_cpu(ms->m_remid), &lkb);
4628 _receive_unlock_reply(lkb, ms, false);
4633 static void _receive_cancel_reply(struct dlm_lkb *lkb,
4634 const struct dlm_message *ms, bool local)
4636 struct dlm_rsb *r = lkb->lkb_resource;
4642 error = validate_message(lkb, ms);
4646 error = remove_from_waiters_ms(lkb, ms, local);
4650 /* this is the value returned from do_cancel() on the master */
4652 switch (from_dlm_errno(le32_to_cpu(ms->m_result))) {
4654 receive_flags_reply(lkb, ms, local);
4655 revert_lock_pc(r, lkb);
4656 queue_cast(r, lkb, -DLM_ECANCEL);
4661 log_error(r->res_ls, "receive_cancel_reply %x error %d",
4663 from_dlm_errno(le32_to_cpu(ms->m_result)));
4670 static int receive_cancel_reply(struct dlm_ls *ls,
4671 const struct dlm_message *ms)
4673 struct dlm_lkb *lkb;
4676 error = find_lkb(ls, le32_to_cpu(ms->m_remid), &lkb);
4680 _receive_cancel_reply(lkb, ms, false);
4685 static void receive_lookup_reply(struct dlm_ls *ls,
4686 const struct dlm_message *ms)
4688 struct dlm_lkb *lkb;
4690 int error, ret_nodeid;
4691 int do_lookup_list = 0;
4693 error = find_lkb(ls, le32_to_cpu(ms->m_lkid), &lkb);
4695 log_error(ls, "%s no lkid %x", __func__,
4696 le32_to_cpu(ms->m_lkid));
4700 /* ms->m_result is the value returned by dlm_master_lookup on dir node
4701 FIXME: will a non-zero error ever be returned? */
4703 r = lkb->lkb_resource;
4707 error = remove_from_waiters(lkb, DLM_MSG_LOOKUP_REPLY);
4711 ret_nodeid = le32_to_cpu(ms->m_nodeid);
4713 /* We sometimes receive a request from the dir node for this
4714 rsb before we've received the dir node's loookup_reply for it.
4715 The request from the dir node implies we're the master, so we set
4716 ourself as master in receive_request_reply, and verify here that
4717 we are indeed the master. */
4719 if (r->res_master_nodeid && (r->res_master_nodeid != ret_nodeid)) {
4720 /* This should never happen */
4721 log_error(ls, "receive_lookup_reply %x from %d ret %d "
4722 "master %d dir %d our %d first %x %s",
4723 lkb->lkb_id, le32_to_cpu(ms->m_header.h_nodeid),
4724 ret_nodeid, r->res_master_nodeid, r->res_dir_nodeid,
4725 dlm_our_nodeid(), r->res_first_lkid, r->res_name);
4728 if (ret_nodeid == dlm_our_nodeid()) {
4729 r->res_master_nodeid = ret_nodeid;
4732 r->res_first_lkid = 0;
4733 } else if (ret_nodeid == -1) {
4734 /* the remote node doesn't believe it's the dir node */
4735 log_error(ls, "receive_lookup_reply %x from %d bad ret_nodeid",
4736 lkb->lkb_id, le32_to_cpu(ms->m_header.h_nodeid));
4737 r->res_master_nodeid = 0;
4739 lkb->lkb_nodeid = -1;
4741 /* set_master() will set lkb_nodeid from r */
4742 r->res_master_nodeid = ret_nodeid;
4743 r->res_nodeid = ret_nodeid;
4746 if (is_overlap(lkb)) {
4747 log_debug(ls, "receive_lookup_reply %x unlock %x",
4748 lkb->lkb_id, dlm_iflags_val(lkb));
4749 queue_cast_overlap(r, lkb);
4750 unhold_lkb(lkb); /* undoes create_lkb() */
4754 _request_lock(r, lkb);
4758 process_lookup_list(r);
4765 static void _receive_message(struct dlm_ls *ls, const struct dlm_message *ms,
4768 int error = 0, noent = 0;
4770 if (WARN_ON_ONCE(!dlm_is_member(ls, le32_to_cpu(ms->m_header.h_nodeid)))) {
4771 log_limit(ls, "receive %d from non-member %d %x %x %d",
4772 le32_to_cpu(ms->m_type),
4773 le32_to_cpu(ms->m_header.h_nodeid),
4774 le32_to_cpu(ms->m_lkid), le32_to_cpu(ms->m_remid),
4775 from_dlm_errno(le32_to_cpu(ms->m_result)));
4779 switch (ms->m_type) {
4781 /* messages sent to a master node */
4783 case cpu_to_le32(DLM_MSG_REQUEST):
4784 error = receive_request(ls, ms);
4787 case cpu_to_le32(DLM_MSG_CONVERT):
4788 error = receive_convert(ls, ms);
4791 case cpu_to_le32(DLM_MSG_UNLOCK):
4792 error = receive_unlock(ls, ms);
4795 case cpu_to_le32(DLM_MSG_CANCEL):
4797 error = receive_cancel(ls, ms);
4800 /* messages sent from a master node (replies to above) */
4802 case cpu_to_le32(DLM_MSG_REQUEST_REPLY):
4803 error = receive_request_reply(ls, ms);
4806 case cpu_to_le32(DLM_MSG_CONVERT_REPLY):
4807 error = receive_convert_reply(ls, ms);
4810 case cpu_to_le32(DLM_MSG_UNLOCK_REPLY):
4811 error = receive_unlock_reply(ls, ms);
4814 case cpu_to_le32(DLM_MSG_CANCEL_REPLY):
4815 error = receive_cancel_reply(ls, ms);
4818 /* messages sent from a master node (only two types of async msg) */
4820 case cpu_to_le32(DLM_MSG_GRANT):
4822 error = receive_grant(ls, ms);
4825 case cpu_to_le32(DLM_MSG_BAST):
4827 error = receive_bast(ls, ms);
4830 /* messages sent to a dir node */
4832 case cpu_to_le32(DLM_MSG_LOOKUP):
4833 receive_lookup(ls, ms);
4836 case cpu_to_le32(DLM_MSG_REMOVE):
4837 receive_remove(ls, ms);
4840 /* messages sent from a dir node (remove has no reply) */
4842 case cpu_to_le32(DLM_MSG_LOOKUP_REPLY):
4843 receive_lookup_reply(ls, ms);
4846 /* other messages */
4848 case cpu_to_le32(DLM_MSG_PURGE):
4849 receive_purge(ls, ms);
4853 log_error(ls, "unknown message type %d",
4854 le32_to_cpu(ms->m_type));
4858 * When checking for ENOENT, we're checking the result of
4859 * find_lkb(m_remid):
4861 * The lock id referenced in the message wasn't found. This may
4862 * happen in normal usage for the async messages and cancel, so
4863 * only use log_debug for them.
4865 * Some errors are expected and normal.
4868 if (error == -ENOENT && noent) {
4869 log_debug(ls, "receive %d no %x remote %d %x saved_seq %u",
4870 le32_to_cpu(ms->m_type), le32_to_cpu(ms->m_remid),
4871 le32_to_cpu(ms->m_header.h_nodeid),
4872 le32_to_cpu(ms->m_lkid), saved_seq);
4873 } else if (error == -ENOENT) {
4874 log_error(ls, "receive %d no %x remote %d %x saved_seq %u",
4875 le32_to_cpu(ms->m_type), le32_to_cpu(ms->m_remid),
4876 le32_to_cpu(ms->m_header.h_nodeid),
4877 le32_to_cpu(ms->m_lkid), saved_seq);
4879 if (ms->m_type == cpu_to_le32(DLM_MSG_CONVERT))
4880 dlm_dump_rsb_hash(ls, le32_to_cpu(ms->m_hash));
4883 if (error == -EINVAL) {
4884 log_error(ls, "receive %d inval from %d lkid %x remid %x "
4886 le32_to_cpu(ms->m_type),
4887 le32_to_cpu(ms->m_header.h_nodeid),
4888 le32_to_cpu(ms->m_lkid), le32_to_cpu(ms->m_remid),
4893 /* If the lockspace is in recovery mode (locking stopped), then normal
4894 messages are saved on the requestqueue for processing after recovery is
4895 done. When not in recovery mode, we wait for dlm_recoverd to drain saved
4896 messages off the requestqueue before we process new ones. This occurs right
4897 after recovery completes when we transition from saving all messages on
4898 requestqueue, to processing all the saved messages, to processing new
4899 messages as they arrive. */
4901 static void dlm_receive_message(struct dlm_ls *ls, const struct dlm_message *ms,
4905 read_lock_bh(&ls->ls_requestqueue_lock);
4906 if (test_bit(LSFL_RECV_MSG_BLOCKED, &ls->ls_flags)) {
4907 /* If we were a member of this lockspace, left, and rejoined,
4908 other nodes may still be sending us messages from the
4909 lockspace generation before we left. */
4910 if (WARN_ON_ONCE(!ls->ls_generation)) {
4911 read_unlock_bh(&ls->ls_requestqueue_lock);
4912 log_limit(ls, "receive %d from %d ignore old gen",
4913 le32_to_cpu(ms->m_type), nodeid);
4917 read_unlock_bh(&ls->ls_requestqueue_lock);
4918 write_lock_bh(&ls->ls_requestqueue_lock);
4919 /* recheck because we hold writelock now */
4920 if (!test_bit(LSFL_RECV_MSG_BLOCKED, &ls->ls_flags)) {
4921 write_unlock_bh(&ls->ls_requestqueue_lock);
4925 dlm_add_requestqueue(ls, nodeid, ms);
4926 write_unlock_bh(&ls->ls_requestqueue_lock);
4928 _receive_message(ls, ms, 0);
4929 read_unlock_bh(&ls->ls_requestqueue_lock);
4933 /* This is called by dlm_recoverd to process messages that were saved on
4934 the requestqueue. */
4936 void dlm_receive_message_saved(struct dlm_ls *ls, const struct dlm_message *ms,
4939 _receive_message(ls, ms, saved_seq);
4942 /* This is called by the midcomms layer when something is received for
4943 the lockspace. It could be either a MSG (normal message sent as part of
4944 standard locking activity) or an RCOM (recovery message sent as part of
4945 lockspace recovery). */
4947 void dlm_receive_buffer(const union dlm_packet *p, int nodeid)
4949 const struct dlm_header *hd = &p->header;
4953 switch (hd->h_cmd) {
4955 type = le32_to_cpu(p->message.m_type);
4958 type = le32_to_cpu(p->rcom.rc_type);
4961 log_print("invalid h_cmd %d from %u", hd->h_cmd, nodeid);
4965 if (le32_to_cpu(hd->h_nodeid) != nodeid) {
4966 log_print("invalid h_nodeid %d from %d lockspace %x",
4967 le32_to_cpu(hd->h_nodeid), nodeid,
4968 le32_to_cpu(hd->u.h_lockspace));
4972 ls = dlm_find_lockspace_global(le32_to_cpu(hd->u.h_lockspace));
4974 if (dlm_config.ci_log_debug) {
4975 printk_ratelimited(KERN_DEBUG "dlm: invalid lockspace "
4976 "%u from %d cmd %d type %d\n",
4977 le32_to_cpu(hd->u.h_lockspace), nodeid,
4981 if (hd->h_cmd == DLM_RCOM && type == DLM_RCOM_STATUS)
4982 dlm_send_ls_not_ready(nodeid, &p->rcom);
4986 /* this rwsem allows dlm_ls_stop() to wait for all dlm_recv threads to
4987 be inactive (in this ls) before transitioning to recovery mode */
4989 read_lock_bh(&ls->ls_recv_active);
4990 if (hd->h_cmd == DLM_MSG)
4991 dlm_receive_message(ls, &p->message, nodeid);
4992 else if (hd->h_cmd == DLM_RCOM)
4993 dlm_receive_rcom(ls, &p->rcom, nodeid);
4995 log_error(ls, "invalid h_cmd %d from %d lockspace %x",
4996 hd->h_cmd, nodeid, le32_to_cpu(hd->u.h_lockspace));
4997 read_unlock_bh(&ls->ls_recv_active);
4999 dlm_put_lockspace(ls);
5002 static void recover_convert_waiter(struct dlm_ls *ls, struct dlm_lkb *lkb,
5003 struct dlm_message *ms_local)
5005 if (middle_conversion(lkb)) {
5007 memset(ms_local, 0, sizeof(struct dlm_message));
5008 ms_local->m_type = cpu_to_le32(DLM_MSG_CONVERT_REPLY);
5009 ms_local->m_result = cpu_to_le32(to_dlm_errno(-EINPROGRESS));
5010 ms_local->m_header.h_nodeid = cpu_to_le32(lkb->lkb_nodeid);
5011 _receive_convert_reply(lkb, ms_local, true);
5013 /* Same special case as in receive_rcom_lock_args() */
5014 lkb->lkb_grmode = DLM_LOCK_IV;
5015 rsb_set_flag(lkb->lkb_resource, RSB_RECOVER_CONVERT);
5018 } else if (lkb->lkb_rqmode >= lkb->lkb_grmode) {
5019 set_bit(DLM_IFL_RESEND_BIT, &lkb->lkb_iflags);
5022 /* lkb->lkb_rqmode < lkb->lkb_grmode shouldn't happen since down
5023 conversions are async; there's no reply from the remote master */
5026 /* A waiting lkb needs recovery if the master node has failed, or
5027 the master node is changing (only when no directory is used) */
5029 static int waiter_needs_recovery(struct dlm_ls *ls, struct dlm_lkb *lkb,
5032 if (dlm_no_directory(ls))
5035 if (dlm_is_removed(ls, lkb->lkb_wait_nodeid))
5041 /* Recovery for locks that are waiting for replies from nodes that are now
5042 gone. We can just complete unlocks and cancels by faking a reply from the
5043 dead node. Requests and up-conversions we flag to be resent after
5044 recovery. Down-conversions can just be completed with a fake reply like
5045 unlocks. Conversions between PR and CW need special attention. */
5047 void dlm_recover_waiters_pre(struct dlm_ls *ls)
5049 struct dlm_lkb *lkb, *safe;
5050 struct dlm_message *ms_local;
5051 int wait_type, local_unlock_result, local_cancel_result;
5054 ms_local = kmalloc(sizeof(*ms_local), GFP_KERNEL);
5058 list_for_each_entry_safe(lkb, safe, &ls->ls_waiters, lkb_wait_reply) {
5060 dir_nodeid = dlm_dir_nodeid(lkb->lkb_resource);
5062 /* exclude debug messages about unlocks because there can be so
5063 many and they aren't very interesting */
5065 if (lkb->lkb_wait_type != DLM_MSG_UNLOCK) {
5066 log_debug(ls, "waiter %x remote %x msg %d r_nodeid %d "
5067 "lkb_nodeid %d wait_nodeid %d dir_nodeid %d",
5071 lkb->lkb_resource->res_nodeid,
5073 lkb->lkb_wait_nodeid,
5077 /* all outstanding lookups, regardless of destination will be
5078 resent after recovery is done */
5080 if (lkb->lkb_wait_type == DLM_MSG_LOOKUP) {
5081 set_bit(DLM_IFL_RESEND_BIT, &lkb->lkb_iflags);
5085 if (!waiter_needs_recovery(ls, lkb, dir_nodeid))
5088 wait_type = lkb->lkb_wait_type;
5089 local_unlock_result = -DLM_EUNLOCK;
5090 local_cancel_result = -DLM_ECANCEL;
5092 /* Main reply may have been received leaving a zero wait_type,
5093 but a reply for the overlapping op may not have been
5094 received. In that case we need to fake the appropriate
5095 reply for the overlap op. */
5098 if (is_overlap_cancel(lkb)) {
5099 wait_type = DLM_MSG_CANCEL;
5100 if (lkb->lkb_grmode == DLM_LOCK_IV)
5101 local_cancel_result = 0;
5103 if (is_overlap_unlock(lkb)) {
5104 wait_type = DLM_MSG_UNLOCK;
5105 if (lkb->lkb_grmode == DLM_LOCK_IV)
5106 local_unlock_result = -ENOENT;
5109 log_debug(ls, "rwpre overlap %x %x %d %d %d",
5110 lkb->lkb_id, dlm_iflags_val(lkb), wait_type,
5111 local_cancel_result, local_unlock_result);
5114 switch (wait_type) {
5116 case DLM_MSG_REQUEST:
5117 set_bit(DLM_IFL_RESEND_BIT, &lkb->lkb_iflags);
5120 case DLM_MSG_CONVERT:
5121 recover_convert_waiter(ls, lkb, ms_local);
5124 case DLM_MSG_UNLOCK:
5126 memset(ms_local, 0, sizeof(struct dlm_message));
5127 ms_local->m_type = cpu_to_le32(DLM_MSG_UNLOCK_REPLY);
5128 ms_local->m_result = cpu_to_le32(to_dlm_errno(local_unlock_result));
5129 ms_local->m_header.h_nodeid = cpu_to_le32(lkb->lkb_nodeid);
5130 _receive_unlock_reply(lkb, ms_local, true);
5134 case DLM_MSG_CANCEL:
5136 memset(ms_local, 0, sizeof(struct dlm_message));
5137 ms_local->m_type = cpu_to_le32(DLM_MSG_CANCEL_REPLY);
5138 ms_local->m_result = cpu_to_le32(to_dlm_errno(local_cancel_result));
5139 ms_local->m_header.h_nodeid = cpu_to_le32(lkb->lkb_nodeid);
5140 _receive_cancel_reply(lkb, ms_local, true);
5145 log_error(ls, "invalid lkb wait_type %d %d",
5146 lkb->lkb_wait_type, wait_type);
5153 static struct dlm_lkb *find_resend_waiter(struct dlm_ls *ls)
5155 struct dlm_lkb *lkb = NULL, *iter;
5157 spin_lock_bh(&ls->ls_waiters_lock);
5158 list_for_each_entry(iter, &ls->ls_waiters, lkb_wait_reply) {
5159 if (test_bit(DLM_IFL_RESEND_BIT, &iter->lkb_iflags)) {
5165 spin_unlock_bh(&ls->ls_waiters_lock);
5171 * Forced state reset for locks that were in the middle of remote operations
5172 * when recovery happened (i.e. lkbs that were on the waiters list, waiting
5173 * for a reply from a remote operation.) The lkbs remaining on the waiters
5174 * list need to be reevaluated; some may need resending to a different node
5175 * than previously, and some may now need local handling rather than remote.
5177 * First, the lkb state for the voided remote operation is forcibly reset,
5178 * equivalent to what remove_from_waiters() would normally do:
5179 * . lkb removed from ls_waiters list
5180 * . lkb wait_type cleared
5181 * . lkb waiters_count cleared
5182 * . lkb ref count decremented for each waiters_count (almost always 1,
5183 * but possibly 2 in case of cancel/unlock overlapping, which means
5184 * two remote replies were being expected for the lkb.)
5186 * Second, the lkb is reprocessed like an original operation would be,
5187 * by passing it to _request_lock or _convert_lock, which will either
5188 * process the lkb operation locally, or send it to a remote node again
5189 * and put the lkb back onto the waiters list.
5191 * When reprocessing the lkb, we may find that it's flagged for an overlapping
5192 * force-unlock or cancel, either from before recovery began, or after recovery
5193 * finished. If this is the case, the unlock/cancel is done directly, and the
5194 * original operation is not initiated again (no _request_lock/_convert_lock.)
5197 int dlm_recover_waiters_post(struct dlm_ls *ls)
5199 struct dlm_lkb *lkb;
5201 int error = 0, mstype, err, oc, ou;
5204 if (dlm_locking_stopped(ls)) {
5205 log_debug(ls, "recover_waiters_post aborted");
5211 * Find an lkb from the waiters list that's been affected by
5212 * recovery node changes, and needs to be reprocessed. Does
5213 * hold_lkb(), adding a refcount.
5215 lkb = find_resend_waiter(ls);
5219 r = lkb->lkb_resource;
5224 * If the lkb has been flagged for a force unlock or cancel,
5225 * then the reprocessing below will be replaced by just doing
5226 * the unlock/cancel directly.
5228 mstype = lkb->lkb_wait_type;
5229 oc = test_and_clear_bit(DLM_IFL_OVERLAP_CANCEL_BIT,
5231 ou = test_and_clear_bit(DLM_IFL_OVERLAP_UNLOCK_BIT,
5235 log_debug(ls, "waiter %x remote %x msg %d r_nodeid %d "
5236 "lkb_nodeid %d wait_nodeid %d dir_nodeid %d "
5237 "overlap %d %d", lkb->lkb_id, lkb->lkb_remid, mstype,
5238 r->res_nodeid, lkb->lkb_nodeid, lkb->lkb_wait_nodeid,
5239 dlm_dir_nodeid(r), oc, ou);
5242 * No reply to the pre-recovery operation will now be received,
5243 * so a forced equivalent of remove_from_waiters() is needed to
5244 * reset the waiters state that was in place before recovery.
5247 clear_bit(DLM_IFL_RESEND_BIT, &lkb->lkb_iflags);
5249 /* Forcibly clear wait_type */
5250 lkb->lkb_wait_type = 0;
5253 * Forcibly reset wait_count and associated refcount. The
5254 * wait_count will almost always be 1, but in case of an
5255 * overlapping unlock/cancel it could be 2: see where
5256 * add_to_waiters() finds the lkb is already on the waiters
5257 * list and does lkb_wait_count++; hold_lkb().
5259 while (lkb->lkb_wait_count) {
5260 lkb->lkb_wait_count--;
5264 /* Forcibly remove from waiters list */
5265 spin_lock_bh(&ls->ls_waiters_lock);
5266 list_del_init(&lkb->lkb_wait_reply);
5267 spin_unlock_bh(&ls->ls_waiters_lock);
5270 * The lkb is now clear of all prior waiters state and can be
5271 * processed locally, or sent to remote node again, or directly
5272 * cancelled/unlocked.
5276 /* do an unlock or cancel instead of resending */
5278 case DLM_MSG_LOOKUP:
5279 case DLM_MSG_REQUEST:
5280 queue_cast(r, lkb, ou ? -DLM_EUNLOCK :
5282 unhold_lkb(lkb); /* undoes create_lkb() */
5284 case DLM_MSG_CONVERT:
5286 queue_cast(r, lkb, -DLM_ECANCEL);
5288 lkb->lkb_exflags |= DLM_LKF_FORCEUNLOCK;
5289 _unlock_lock(r, lkb);
5297 case DLM_MSG_LOOKUP:
5298 case DLM_MSG_REQUEST:
5299 _request_lock(r, lkb);
5301 confirm_master(r, 0);
5303 case DLM_MSG_CONVERT:
5304 _convert_lock(r, lkb);
5312 log_error(ls, "waiter %x msg %d r_nodeid %d "
5313 "dir_nodeid %d overlap %d %d",
5314 lkb->lkb_id, mstype, r->res_nodeid,
5315 dlm_dir_nodeid(r), oc, ou);
5325 static void purge_mstcpy_list(struct dlm_ls *ls, struct dlm_rsb *r,
5326 struct list_head *list)
5328 struct dlm_lkb *lkb, *safe;
5330 list_for_each_entry_safe(lkb, safe, list, lkb_statequeue) {
5331 if (!is_master_copy(lkb))
5334 /* don't purge lkbs we've added in recover_master_copy for
5335 the current recovery seq */
5337 if (lkb->lkb_recover_seq == ls->ls_recover_seq)
5342 /* this put should free the lkb */
5343 if (!dlm_put_lkb(lkb))
5344 log_error(ls, "purged mstcpy lkb not released");
5348 void dlm_purge_mstcpy_locks(struct dlm_rsb *r)
5350 struct dlm_ls *ls = r->res_ls;
5352 purge_mstcpy_list(ls, r, &r->res_grantqueue);
5353 purge_mstcpy_list(ls, r, &r->res_convertqueue);
5354 purge_mstcpy_list(ls, r, &r->res_waitqueue);
5357 static void purge_dead_list(struct dlm_ls *ls, struct dlm_rsb *r,
5358 struct list_head *list,
5359 int nodeid_gone, unsigned int *count)
5361 struct dlm_lkb *lkb, *safe;
5363 list_for_each_entry_safe(lkb, safe, list, lkb_statequeue) {
5364 if (!is_master_copy(lkb))
5367 if ((lkb->lkb_nodeid == nodeid_gone) ||
5368 dlm_is_removed(ls, lkb->lkb_nodeid)) {
5370 /* tell recover_lvb to invalidate the lvb
5371 because a node holding EX/PW failed */
5372 if ((lkb->lkb_exflags & DLM_LKF_VALBLK) &&
5373 (lkb->lkb_grmode >= DLM_LOCK_PW)) {
5374 rsb_set_flag(r, RSB_RECOVER_LVB_INVAL);
5379 /* this put should free the lkb */
5380 if (!dlm_put_lkb(lkb))
5381 log_error(ls, "purged dead lkb not released");
5383 rsb_set_flag(r, RSB_RECOVER_GRANT);
5390 /* Get rid of locks held by nodes that are gone. */
5392 void dlm_recover_purge(struct dlm_ls *ls, const struct list_head *root_list)
5395 struct dlm_member *memb;
5396 int nodes_count = 0;
5397 int nodeid_gone = 0;
5398 unsigned int lkb_count = 0;
5400 /* cache one removed nodeid to optimize the common
5401 case of a single node removed */
5403 list_for_each_entry(memb, &ls->ls_nodes_gone, list) {
5405 nodeid_gone = memb->nodeid;
5411 list_for_each_entry(r, root_list, res_root_list) {
5415 purge_dead_list(ls, r, &r->res_grantqueue,
5416 nodeid_gone, &lkb_count);
5417 purge_dead_list(ls, r, &r->res_convertqueue,
5418 nodeid_gone, &lkb_count);
5419 purge_dead_list(ls, r, &r->res_waitqueue,
5420 nodeid_gone, &lkb_count);
5428 log_rinfo(ls, "dlm_recover_purge %u locks for %u nodes",
5429 lkb_count, nodes_count);
5432 static struct dlm_rsb *find_grant_rsb(struct dlm_ls *ls)
5436 read_lock_bh(&ls->ls_rsbtbl_lock);
5437 list_for_each_entry(r, &ls->ls_slow_active, res_slow_list) {
5438 if (!rsb_flag(r, RSB_RECOVER_GRANT))
5440 if (!is_master(r)) {
5441 rsb_clear_flag(r, RSB_RECOVER_GRANT);
5445 read_unlock_bh(&ls->ls_rsbtbl_lock);
5448 read_unlock_bh(&ls->ls_rsbtbl_lock);
5453 * Attempt to grant locks on resources that we are the master of.
5454 * Locks may have become grantable during recovery because locks
5455 * from departed nodes have been purged (or not rebuilt), allowing
5456 * previously blocked locks to now be granted. The subset of rsb's
5457 * we are interested in are those with lkb's on either the convert or
5460 * Simplest would be to go through each master rsb and check for non-empty
5461 * convert or waiting queues, and attempt to grant on those rsbs.
5462 * Checking the queues requires lock_rsb, though, for which we'd need
5463 * to release the rsbtbl lock. This would make iterating through all
5464 * rsb's very inefficient. So, we rely on earlier recovery routines
5465 * to set RECOVER_GRANT on any rsb's that we should attempt to grant
5469 void dlm_recover_grant(struct dlm_ls *ls)
5472 unsigned int count = 0;
5473 unsigned int rsb_count = 0;
5474 unsigned int lkb_count = 0;
5477 r = find_grant_rsb(ls);
5484 /* the RECOVER_GRANT flag is checked in the grant path */
5485 grant_pending_locks(r, &count);
5486 rsb_clear_flag(r, RSB_RECOVER_GRANT);
5488 confirm_master(r, 0);
5495 log_rinfo(ls, "dlm_recover_grant %u locks on %u resources",
5496 lkb_count, rsb_count);
5499 static struct dlm_lkb *search_remid_list(struct list_head *head, int nodeid,
5502 struct dlm_lkb *lkb;
5504 list_for_each_entry(lkb, head, lkb_statequeue) {
5505 if (lkb->lkb_nodeid == nodeid && lkb->lkb_remid == remid)
5511 static struct dlm_lkb *search_remid(struct dlm_rsb *r, int nodeid,
5514 struct dlm_lkb *lkb;
5516 lkb = search_remid_list(&r->res_grantqueue, nodeid, remid);
5519 lkb = search_remid_list(&r->res_convertqueue, nodeid, remid);
5522 lkb = search_remid_list(&r->res_waitqueue, nodeid, remid);
5528 /* needs at least dlm_rcom + rcom_lock */
5529 static int receive_rcom_lock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
5530 struct dlm_rsb *r, const struct dlm_rcom *rc)
5532 struct rcom_lock *rl = (struct rcom_lock *) rc->rc_buf;
5534 lkb->lkb_nodeid = le32_to_cpu(rc->rc_header.h_nodeid);
5535 lkb->lkb_ownpid = le32_to_cpu(rl->rl_ownpid);
5536 lkb->lkb_remid = le32_to_cpu(rl->rl_lkid);
5537 lkb->lkb_exflags = le32_to_cpu(rl->rl_exflags);
5538 dlm_set_dflags_val(lkb, le32_to_cpu(rl->rl_flags));
5539 set_bit(DLM_IFL_MSTCPY_BIT, &lkb->lkb_iflags);
5540 lkb->lkb_lvbseq = le32_to_cpu(rl->rl_lvbseq);
5541 lkb->lkb_rqmode = rl->rl_rqmode;
5542 lkb->lkb_grmode = rl->rl_grmode;
5543 /* don't set lkb_status because add_lkb wants to itself */
5545 lkb->lkb_bastfn = (rl->rl_asts & DLM_CB_BAST) ? &fake_bastfn : NULL;
5546 lkb->lkb_astfn = (rl->rl_asts & DLM_CB_CAST) ? &fake_astfn : NULL;
5548 if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
5549 int lvblen = le16_to_cpu(rc->rc_header.h_length) -
5550 sizeof(struct dlm_rcom) - sizeof(struct rcom_lock);
5551 if (lvblen > ls->ls_lvblen)
5553 lkb->lkb_lvbptr = dlm_allocate_lvb(ls);
5554 if (!lkb->lkb_lvbptr)
5556 memcpy(lkb->lkb_lvbptr, rl->rl_lvb, lvblen);
5559 /* Conversions between PR and CW (middle modes) need special handling.
5560 The real granted mode of these converting locks cannot be determined
5561 until all locks have been rebuilt on the rsb (recover_conversion) */
5563 if (rl->rl_wait_type == cpu_to_le16(DLM_MSG_CONVERT) &&
5564 middle_conversion(lkb)) {
5565 rl->rl_status = DLM_LKSTS_CONVERT;
5566 lkb->lkb_grmode = DLM_LOCK_IV;
5567 rsb_set_flag(r, RSB_RECOVER_CONVERT);
5573 /* This lkb may have been recovered in a previous aborted recovery so we need
5574 to check if the rsb already has an lkb with the given remote nodeid/lkid.
5575 If so we just send back a standard reply. If not, we create a new lkb with
5576 the given values and send back our lkid. We send back our lkid by sending
5577 back the rcom_lock struct we got but with the remid field filled in. */
5579 /* needs at least dlm_rcom + rcom_lock */
5580 int dlm_recover_master_copy(struct dlm_ls *ls, const struct dlm_rcom *rc,
5581 __le32 *rl_remid, __le32 *rl_result)
5583 struct rcom_lock *rl = (struct rcom_lock *) rc->rc_buf;
5585 struct dlm_lkb *lkb;
5587 int from_nodeid = le32_to_cpu(rc->rc_header.h_nodeid);
5590 /* init rl_remid with rcom lock rl_remid */
5591 *rl_remid = rl->rl_remid;
5593 if (rl->rl_parent_lkid) {
5594 error = -EOPNOTSUPP;
5598 remid = le32_to_cpu(rl->rl_lkid);
5600 /* In general we expect the rsb returned to be R_MASTER, but we don't
5601 have to require it. Recovery of masters on one node can overlap
5602 recovery of locks on another node, so one node can send us MSTCPY
5603 locks before we've made ourselves master of this rsb. We can still
5604 add new MSTCPY locks that we receive here without any harm; when
5605 we make ourselves master, dlm_recover_masters() won't touch the
5606 MSTCPY locks we've received early. */
5608 error = find_rsb(ls, rl->rl_name, le16_to_cpu(rl->rl_namelen),
5609 from_nodeid, R_RECEIVE_RECOVER, &r);
5615 if (dlm_no_directory(ls) && (dlm_dir_nodeid(r) != dlm_our_nodeid())) {
5616 log_error(ls, "dlm_recover_master_copy remote %d %x not dir",
5617 from_nodeid, remid);
5622 lkb = search_remid(r, from_nodeid, remid);
5628 error = create_lkb(ls, &lkb);
5632 error = receive_rcom_lock_args(ls, lkb, r, rc);
5639 add_lkb(r, lkb, rl->rl_status);
5640 ls->ls_recover_locks_in++;
5642 if (!list_empty(&r->res_waitqueue) || !list_empty(&r->res_convertqueue))
5643 rsb_set_flag(r, RSB_RECOVER_GRANT);
5646 /* this is the new value returned to the lock holder for
5647 saving in its process-copy lkb */
5648 *rl_remid = cpu_to_le32(lkb->lkb_id);
5650 lkb->lkb_recover_seq = ls->ls_recover_seq;
5656 if (error && error != -EEXIST)
5657 log_rinfo(ls, "dlm_recover_master_copy remote %d %x error %d",
5658 from_nodeid, remid, error);
5659 *rl_result = cpu_to_le32(error);
5663 /* needs at least dlm_rcom + rcom_lock */
5664 int dlm_recover_process_copy(struct dlm_ls *ls, const struct dlm_rcom *rc,
5667 struct rcom_lock *rl = (struct rcom_lock *) rc->rc_buf;
5669 struct dlm_lkb *lkb;
5670 uint32_t lkid, remid;
5673 lkid = le32_to_cpu(rl->rl_lkid);
5674 remid = le32_to_cpu(rl->rl_remid);
5675 result = le32_to_cpu(rl->rl_result);
5677 error = find_lkb(ls, lkid, &lkb);
5679 log_error(ls, "dlm_recover_process_copy no %x remote %d %x %d",
5680 lkid, le32_to_cpu(rc->rc_header.h_nodeid), remid,
5685 r = lkb->lkb_resource;
5689 if (!is_process_copy(lkb)) {
5690 log_error(ls, "dlm_recover_process_copy bad %x remote %d %x %d",
5691 lkid, le32_to_cpu(rc->rc_header.h_nodeid), remid,
5702 /* There's a chance the new master received our lock before
5703 dlm_recover_master_reply(), this wouldn't happen if we did
5704 a barrier between recover_masters and recover_locks. */
5706 log_debug(ls, "dlm_recover_process_copy %x remote %d %x %d",
5707 lkid, le32_to_cpu(rc->rc_header.h_nodeid), remid,
5710 dlm_send_rcom_lock(r, lkb, seq);
5714 lkb->lkb_remid = remid;
5717 log_error(ls, "dlm_recover_process_copy %x remote %d %x %d unk",
5718 lkid, le32_to_cpu(rc->rc_header.h_nodeid), remid,
5722 /* an ack for dlm_recover_locks() which waits for replies from
5723 all the locks it sends to new masters */
5724 dlm_recovered_lock(r);
5733 int dlm_user_request(struct dlm_ls *ls, struct dlm_user_args *ua,
5734 int mode, uint32_t flags, void *name, unsigned int namelen)
5736 struct dlm_lkb *lkb;
5737 struct dlm_args args;
5741 dlm_lock_recovery(ls);
5743 error = create_lkb(ls, &lkb);
5749 trace_dlm_lock_start(ls, lkb, name, namelen, mode, flags);
5751 if (flags & DLM_LKF_VALBLK) {
5752 ua->lksb.sb_lvbptr = kzalloc(DLM_USER_LVB_LEN, GFP_NOFS);
5753 if (!ua->lksb.sb_lvbptr) {
5759 error = set_lock_args(mode, &ua->lksb, flags, namelen, fake_astfn, ua,
5760 fake_bastfn, &args);
5762 kfree(ua->lksb.sb_lvbptr);
5763 ua->lksb.sb_lvbptr = NULL;
5768 /* After ua is attached to lkb it will be freed by dlm_free_lkb().
5769 When DLM_DFL_USER_BIT is set, the dlm knows that this is a userspace
5770 lock and that lkb_astparam is the dlm_user_args structure. */
5771 set_bit(DLM_DFL_USER_BIT, &lkb->lkb_dflags);
5772 error = request_lock(ls, lkb, name, namelen, &args);
5787 /* add this new lkb to the per-process list of locks */
5788 spin_lock_bh(&ua->proc->locks_spin);
5790 list_add_tail(&lkb->lkb_ownqueue, &ua->proc->locks);
5791 spin_unlock_bh(&ua->proc->locks_spin);
5794 trace_dlm_lock_end(ls, lkb, name, namelen, mode, flags, error, false);
5798 dlm_unlock_recovery(ls);
5802 int dlm_user_convert(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
5803 int mode, uint32_t flags, uint32_t lkid, char *lvb_in)
5805 struct dlm_lkb *lkb;
5806 struct dlm_args args;
5807 struct dlm_user_args *ua;
5810 dlm_lock_recovery(ls);
5812 error = find_lkb(ls, lkid, &lkb);
5816 trace_dlm_lock_start(ls, lkb, NULL, 0, mode, flags);
5818 /* user can change the params on its lock when it converts it, or
5819 add an lvb that didn't exist before */
5823 if (flags & DLM_LKF_VALBLK && !ua->lksb.sb_lvbptr) {
5824 ua->lksb.sb_lvbptr = kzalloc(DLM_USER_LVB_LEN, GFP_NOFS);
5825 if (!ua->lksb.sb_lvbptr) {
5830 if (lvb_in && ua->lksb.sb_lvbptr)
5831 memcpy(ua->lksb.sb_lvbptr, lvb_in, DLM_USER_LVB_LEN);
5833 ua->xid = ua_tmp->xid;
5834 ua->castparam = ua_tmp->castparam;
5835 ua->castaddr = ua_tmp->castaddr;
5836 ua->bastparam = ua_tmp->bastparam;
5837 ua->bastaddr = ua_tmp->bastaddr;
5838 ua->user_lksb = ua_tmp->user_lksb;
5840 error = set_lock_args(mode, &ua->lksb, flags, 0, fake_astfn, ua,
5841 fake_bastfn, &args);
5845 error = convert_lock(ls, lkb, &args);
5847 if (error == -EINPROGRESS || error == -EAGAIN || error == -EDEADLK)
5850 trace_dlm_lock_end(ls, lkb, NULL, 0, mode, flags, error, false);
5853 dlm_unlock_recovery(ls);
5859 * The caller asks for an orphan lock on a given resource with a given mode.
5860 * If a matching lock exists, it's moved to the owner's list of locks and
5861 * the lkid is returned.
5864 int dlm_user_adopt_orphan(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
5865 int mode, uint32_t flags, void *name, unsigned int namelen,
5868 struct dlm_lkb *lkb = NULL, *iter;
5869 struct dlm_user_args *ua;
5870 int found_other_mode = 0;
5873 spin_lock_bh(&ls->ls_orphans_lock);
5874 list_for_each_entry(iter, &ls->ls_orphans, lkb_ownqueue) {
5875 if (iter->lkb_resource->res_length != namelen)
5877 if (memcmp(iter->lkb_resource->res_name, name, namelen))
5879 if (iter->lkb_grmode != mode) {
5880 found_other_mode = 1;
5885 list_del_init(&iter->lkb_ownqueue);
5886 clear_bit(DLM_DFL_ORPHAN_BIT, &iter->lkb_dflags);
5887 *lkid = iter->lkb_id;
5890 spin_unlock_bh(&ls->ls_orphans_lock);
5892 if (!lkb && found_other_mode) {
5902 lkb->lkb_exflags = flags;
5903 lkb->lkb_ownpid = (int) current->pid;
5907 ua->proc = ua_tmp->proc;
5908 ua->xid = ua_tmp->xid;
5909 ua->castparam = ua_tmp->castparam;
5910 ua->castaddr = ua_tmp->castaddr;
5911 ua->bastparam = ua_tmp->bastparam;
5912 ua->bastaddr = ua_tmp->bastaddr;
5913 ua->user_lksb = ua_tmp->user_lksb;
5916 * The lkb reference from the ls_orphans list was not
5917 * removed above, and is now considered the reference
5918 * for the proc locks list.
5921 spin_lock_bh(&ua->proc->locks_spin);
5922 list_add_tail(&lkb->lkb_ownqueue, &ua->proc->locks);
5923 spin_unlock_bh(&ua->proc->locks_spin);
5929 int dlm_user_unlock(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
5930 uint32_t flags, uint32_t lkid, char *lvb_in)
5932 struct dlm_lkb *lkb;
5933 struct dlm_args args;
5934 struct dlm_user_args *ua;
5937 dlm_lock_recovery(ls);
5939 error = find_lkb(ls, lkid, &lkb);
5943 trace_dlm_unlock_start(ls, lkb, flags);
5947 if (lvb_in && ua->lksb.sb_lvbptr)
5948 memcpy(ua->lksb.sb_lvbptr, lvb_in, DLM_USER_LVB_LEN);
5949 if (ua_tmp->castparam)
5950 ua->castparam = ua_tmp->castparam;
5951 ua->user_lksb = ua_tmp->user_lksb;
5953 error = set_unlock_args(flags, ua, &args);
5957 error = unlock_lock(ls, lkb, &args);
5959 if (error == -DLM_EUNLOCK)
5961 /* from validate_unlock_args() */
5962 if (error == -EBUSY && (flags & DLM_LKF_FORCEUNLOCK))
5967 spin_lock_bh(&ua->proc->locks_spin);
5968 /* dlm_user_add_cb() may have already taken lkb off the proc list */
5969 if (!list_empty(&lkb->lkb_ownqueue))
5970 list_move(&lkb->lkb_ownqueue, &ua->proc->unlocking);
5971 spin_unlock_bh(&ua->proc->locks_spin);
5973 trace_dlm_unlock_end(ls, lkb, flags, error);
5976 dlm_unlock_recovery(ls);
5981 int dlm_user_cancel(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
5982 uint32_t flags, uint32_t lkid)
5984 struct dlm_lkb *lkb;
5985 struct dlm_args args;
5986 struct dlm_user_args *ua;
5989 dlm_lock_recovery(ls);
5991 error = find_lkb(ls, lkid, &lkb);
5995 trace_dlm_unlock_start(ls, lkb, flags);
5998 if (ua_tmp->castparam)
5999 ua->castparam = ua_tmp->castparam;
6000 ua->user_lksb = ua_tmp->user_lksb;
6002 error = set_unlock_args(flags, ua, &args);
6006 error = cancel_lock(ls, lkb, &args);
6008 if (error == -DLM_ECANCEL)
6010 /* from validate_unlock_args() */
6011 if (error == -EBUSY)
6014 trace_dlm_unlock_end(ls, lkb, flags, error);
6017 dlm_unlock_recovery(ls);
6022 int dlm_user_deadlock(struct dlm_ls *ls, uint32_t flags, uint32_t lkid)
6024 struct dlm_lkb *lkb;
6025 struct dlm_args args;
6026 struct dlm_user_args *ua;
6030 dlm_lock_recovery(ls);
6032 error = find_lkb(ls, lkid, &lkb);
6036 trace_dlm_unlock_start(ls, lkb, flags);
6040 error = set_unlock_args(flags, ua, &args);
6044 /* same as cancel_lock(), but set DEADLOCK_CANCEL after lock_rsb */
6046 r = lkb->lkb_resource;
6050 error = validate_unlock_args(lkb, &args);
6053 set_bit(DLM_IFL_DEADLOCK_CANCEL_BIT, &lkb->lkb_iflags);
6055 error = _cancel_lock(r, lkb);
6060 if (error == -DLM_ECANCEL)
6062 /* from validate_unlock_args() */
6063 if (error == -EBUSY)
6066 trace_dlm_unlock_end(ls, lkb, flags, error);
6069 dlm_unlock_recovery(ls);
6073 /* lkb's that are removed from the waiters list by revert are just left on the
6074 orphans list with the granted orphan locks, to be freed by purge */
6076 static int orphan_proc_lock(struct dlm_ls *ls, struct dlm_lkb *lkb)
6078 struct dlm_args args;
6081 hold_lkb(lkb); /* reference for the ls_orphans list */
6082 spin_lock_bh(&ls->ls_orphans_lock);
6083 list_add_tail(&lkb->lkb_ownqueue, &ls->ls_orphans);
6084 spin_unlock_bh(&ls->ls_orphans_lock);
6086 set_unlock_args(0, lkb->lkb_ua, &args);
6088 error = cancel_lock(ls, lkb, &args);
6089 if (error == -DLM_ECANCEL)
6094 /* The FORCEUNLOCK flag allows the unlock to go ahead even if the lkb isn't
6095 granted. Regardless of what rsb queue the lock is on, it's removed and
6096 freed. The IVVALBLK flag causes the lvb on the resource to be invalidated
6097 if our lock is PW/EX (it's ignored if our granted mode is smaller.) */
6099 static int unlock_proc_lock(struct dlm_ls *ls, struct dlm_lkb *lkb)
6101 struct dlm_args args;
6104 set_unlock_args(DLM_LKF_FORCEUNLOCK | DLM_LKF_IVVALBLK,
6105 lkb->lkb_ua, &args);
6107 error = unlock_lock(ls, lkb, &args);
6108 if (error == -DLM_EUNLOCK)
6113 /* We have to release clear_proc_locks mutex before calling unlock_proc_lock()
6114 (which does lock_rsb) due to deadlock with receiving a message that does
6115 lock_rsb followed by dlm_user_add_cb() */
6117 static struct dlm_lkb *del_proc_lock(struct dlm_ls *ls,
6118 struct dlm_user_proc *proc)
6120 struct dlm_lkb *lkb = NULL;
6122 spin_lock_bh(&ls->ls_clear_proc_locks);
6123 if (list_empty(&proc->locks))
6126 lkb = list_entry(proc->locks.next, struct dlm_lkb, lkb_ownqueue);
6127 list_del_init(&lkb->lkb_ownqueue);
6129 if (lkb->lkb_exflags & DLM_LKF_PERSISTENT)
6130 set_bit(DLM_DFL_ORPHAN_BIT, &lkb->lkb_dflags);
6132 set_bit(DLM_IFL_DEAD_BIT, &lkb->lkb_iflags);
6134 spin_unlock_bh(&ls->ls_clear_proc_locks);
6138 /* The ls_clear_proc_locks mutex protects against dlm_user_add_cb() which
6139 1) references lkb->ua which we free here and 2) adds lkbs to proc->asts,
6140 which we clear here. */
6142 /* proc CLOSING flag is set so no more device_reads should look at proc->asts
6143 list, and no more device_writes should add lkb's to proc->locks list; so we
6144 shouldn't need to take asts_spin or locks_spin here. this assumes that
6145 device reads/writes/closes are serialized -- FIXME: we may need to serialize
6148 void dlm_clear_proc_locks(struct dlm_ls *ls, struct dlm_user_proc *proc)
6150 struct dlm_callback *cb, *cb_safe;
6151 struct dlm_lkb *lkb, *safe;
6153 dlm_lock_recovery(ls);
6156 lkb = del_proc_lock(ls, proc);
6159 if (lkb->lkb_exflags & DLM_LKF_PERSISTENT)
6160 orphan_proc_lock(ls, lkb);
6162 unlock_proc_lock(ls, lkb);
6164 /* this removes the reference for the proc->locks list
6165 added by dlm_user_request, it may result in the lkb
6171 spin_lock_bh(&ls->ls_clear_proc_locks);
6173 /* in-progress unlocks */
6174 list_for_each_entry_safe(lkb, safe, &proc->unlocking, lkb_ownqueue) {
6175 list_del_init(&lkb->lkb_ownqueue);
6176 set_bit(DLM_IFL_DEAD_BIT, &lkb->lkb_iflags);
6180 list_for_each_entry_safe(cb, cb_safe, &proc->asts, list) {
6181 list_del(&cb->list);
6185 spin_unlock_bh(&ls->ls_clear_proc_locks);
6186 dlm_unlock_recovery(ls);
6189 static void purge_proc_locks(struct dlm_ls *ls, struct dlm_user_proc *proc)
6191 struct dlm_callback *cb, *cb_safe;
6192 struct dlm_lkb *lkb, *safe;
6196 spin_lock_bh(&proc->locks_spin);
6197 if (!list_empty(&proc->locks)) {
6198 lkb = list_entry(proc->locks.next, struct dlm_lkb,
6200 list_del_init(&lkb->lkb_ownqueue);
6202 spin_unlock_bh(&proc->locks_spin);
6207 set_bit(DLM_IFL_DEAD_BIT, &lkb->lkb_iflags);
6208 unlock_proc_lock(ls, lkb);
6209 dlm_put_lkb(lkb); /* ref from proc->locks list */
6212 spin_lock_bh(&proc->locks_spin);
6213 list_for_each_entry_safe(lkb, safe, &proc->unlocking, lkb_ownqueue) {
6214 list_del_init(&lkb->lkb_ownqueue);
6215 set_bit(DLM_IFL_DEAD_BIT, &lkb->lkb_iflags);
6218 spin_unlock_bh(&proc->locks_spin);
6220 spin_lock_bh(&proc->asts_spin);
6221 list_for_each_entry_safe(cb, cb_safe, &proc->asts, list) {
6222 list_del(&cb->list);
6225 spin_unlock_bh(&proc->asts_spin);
6228 /* pid of 0 means purge all orphans */
6230 static void do_purge(struct dlm_ls *ls, int nodeid, int pid)
6232 struct dlm_lkb *lkb, *safe;
6234 spin_lock_bh(&ls->ls_orphans_lock);
6235 list_for_each_entry_safe(lkb, safe, &ls->ls_orphans, lkb_ownqueue) {
6236 if (pid && lkb->lkb_ownpid != pid)
6238 unlock_proc_lock(ls, lkb);
6239 list_del_init(&lkb->lkb_ownqueue);
6242 spin_unlock_bh(&ls->ls_orphans_lock);
6245 static int send_purge(struct dlm_ls *ls, int nodeid, int pid)
6247 struct dlm_message *ms;
6248 struct dlm_mhandle *mh;
6251 error = _create_message(ls, sizeof(struct dlm_message), nodeid,
6252 DLM_MSG_PURGE, &ms, &mh);
6255 ms->m_nodeid = cpu_to_le32(nodeid);
6256 ms->m_pid = cpu_to_le32(pid);
6258 return send_message(mh, ms, NULL, 0);
6261 int dlm_user_purge(struct dlm_ls *ls, struct dlm_user_proc *proc,
6262 int nodeid, int pid)
6266 if (nodeid && (nodeid != dlm_our_nodeid())) {
6267 error = send_purge(ls, nodeid, pid);
6269 dlm_lock_recovery(ls);
6270 if (pid == current->pid)
6271 purge_proc_locks(ls, proc);
6273 do_purge(ls, nodeid, pid);
6274 dlm_unlock_recovery(ls);
6279 /* debug functionality */
6280 int dlm_debug_add_lkb(struct dlm_ls *ls, uint32_t lkb_id, char *name, int len,
6281 int lkb_nodeid, unsigned int lkb_dflags, int lkb_status)
6283 struct dlm_lksb *lksb;
6284 struct dlm_lkb *lkb;
6288 /* we currently can't set a valid user lock */
6289 if (lkb_dflags & BIT(DLM_DFL_USER_BIT))
6292 lksb = kzalloc(sizeof(*lksb), GFP_NOFS);
6296 error = _create_lkb(ls, &lkb, lkb_id, lkb_id + 1);
6302 dlm_set_dflags_val(lkb, lkb_dflags);
6303 lkb->lkb_nodeid = lkb_nodeid;
6304 lkb->lkb_lksb = lksb;
6305 /* user specific pointer, just don't have it NULL for kernel locks */
6306 if (~lkb_dflags & BIT(DLM_DFL_USER_BIT))
6307 lkb->lkb_astparam = (void *)0xDEADBEEF;
6309 error = find_rsb(ls, name, len, 0, R_REQUEST, &r);
6318 add_lkb(r, lkb, lkb_status);
6325 int dlm_debug_add_lkb_to_waiters(struct dlm_ls *ls, uint32_t lkb_id,
6326 int mstype, int to_nodeid)
6328 struct dlm_lkb *lkb;
6331 error = find_lkb(ls, lkb_id, &lkb);
6335 error = add_to_waiters(lkb, mstype, to_nodeid);