[ARM] 4531/1: remove is_in_rom() protptype
[linux-2.6/kvm.git] / fs / dlm / lock.c
blobb455919c19984ad408d4ca498ad72f40a7d33d1f
1 /******************************************************************************
2 *******************************************************************************
3 **
4 ** Copyright (C) 2005-2007 Red Hat, Inc. All rights reserved.
5 **
6 ** This copyrighted material is made available to anyone wishing to use,
7 ** modify, copy, or redistribute it subject to the terms and conditions
8 ** of the GNU General Public License v.2.
9 **
10 *******************************************************************************
11 ******************************************************************************/
13 /* Central locking logic has four stages:
15 dlm_lock()
16 dlm_unlock()
18 request_lock(ls, lkb)
19 convert_lock(ls, lkb)
20 unlock_lock(ls, lkb)
21 cancel_lock(ls, lkb)
23 _request_lock(r, lkb)
24 _convert_lock(r, lkb)
25 _unlock_lock(r, lkb)
26 _cancel_lock(r, lkb)
28 do_request(r, lkb)
29 do_convert(r, lkb)
30 do_unlock(r, lkb)
31 do_cancel(r, lkb)
33 Stage 1 (lock, unlock) is mainly about checking input args and
34 splitting into one of the four main operations:
36 dlm_lock = request_lock
37 dlm_lock+CONVERT = convert_lock
38 dlm_unlock = unlock_lock
39 dlm_unlock+CANCEL = cancel_lock
41 Stage 2, xxxx_lock(), just finds and locks the relevant rsb which is
42 provided to the next stage.
44 Stage 3, _xxxx_lock(), determines if the operation is local or remote.
45 When remote, it calls send_xxxx(), when local it calls do_xxxx().
47 Stage 4, do_xxxx(), is the guts of the operation. It manipulates the
48 given rsb and lkb and queues callbacks.
50 For remote operations, send_xxxx() results in the corresponding do_xxxx()
51 function being executed on the remote node. The connecting send/receive
52 calls on local (L) and remote (R) nodes:
54 L: send_xxxx() -> R: receive_xxxx()
55 R: do_xxxx()
56 L: receive_xxxx_reply() <- R: send_xxxx_reply()
58 #include <linux/types.h>
59 #include "dlm_internal.h"
60 #include <linux/dlm_device.h>
61 #include "memory.h"
62 #include "lowcomms.h"
63 #include "requestqueue.h"
64 #include "util.h"
65 #include "dir.h"
66 #include "member.h"
67 #include "lockspace.h"
68 #include "ast.h"
69 #include "lock.h"
70 #include "rcom.h"
71 #include "recover.h"
72 #include "lvb_table.h"
73 #include "user.h"
74 #include "config.h"
76 static int send_request(struct dlm_rsb *r, struct dlm_lkb *lkb);
77 static int send_convert(struct dlm_rsb *r, struct dlm_lkb *lkb);
78 static int send_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb);
79 static int send_cancel(struct dlm_rsb *r, struct dlm_lkb *lkb);
80 static int send_grant(struct dlm_rsb *r, struct dlm_lkb *lkb);
81 static int send_bast(struct dlm_rsb *r, struct dlm_lkb *lkb, int mode);
82 static int send_lookup(struct dlm_rsb *r, struct dlm_lkb *lkb);
83 static int send_remove(struct dlm_rsb *r);
84 static int _request_lock(struct dlm_rsb *r, struct dlm_lkb *lkb);
85 static int _cancel_lock(struct dlm_rsb *r, struct dlm_lkb *lkb);
86 static void __receive_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
87 struct dlm_message *ms);
88 static int receive_extralen(struct dlm_message *ms);
89 static void do_purge(struct dlm_ls *ls, int nodeid, int pid);
90 static void del_timeout(struct dlm_lkb *lkb);
91 void dlm_timeout_warn(struct dlm_lkb *lkb);
94 * Lock compatibilty matrix - thanks Steve
95 * UN = Unlocked state. Not really a state, used as a flag
96 * PD = Padding. Used to make the matrix a nice power of two in size
97 * Other states are the same as the VMS DLM.
98 * Usage: matrix[grmode+1][rqmode+1] (although m[rq+1][gr+1] is the same)
101 static const int __dlm_compat_matrix[8][8] = {
102 /* UN NL CR CW PR PW EX PD */
103 {1, 1, 1, 1, 1, 1, 1, 0}, /* UN */
104 {1, 1, 1, 1, 1, 1, 1, 0}, /* NL */
105 {1, 1, 1, 1, 1, 1, 0, 0}, /* CR */
106 {1, 1, 1, 1, 0, 0, 0, 0}, /* CW */
107 {1, 1, 1, 0, 1, 0, 0, 0}, /* PR */
108 {1, 1, 1, 0, 0, 0, 0, 0}, /* PW */
109 {1, 1, 0, 0, 0, 0, 0, 0}, /* EX */
110 {0, 0, 0, 0, 0, 0, 0, 0} /* PD */
114 * This defines the direction of transfer of LVB data.
115 * Granted mode is the row; requested mode is the column.
116 * Usage: matrix[grmode+1][rqmode+1]
117 * 1 = LVB is returned to the caller
118 * 0 = LVB is written to the resource
119 * -1 = nothing happens to the LVB
122 const int dlm_lvb_operations[8][8] = {
123 /* UN NL CR CW PR PW EX PD*/
124 { -1, 1, 1, 1, 1, 1, 1, -1 }, /* UN */
125 { -1, 1, 1, 1, 1, 1, 1, 0 }, /* NL */
126 { -1, -1, 1, 1, 1, 1, 1, 0 }, /* CR */
127 { -1, -1, -1, 1, 1, 1, 1, 0 }, /* CW */
128 { -1, -1, -1, -1, 1, 1, 1, 0 }, /* PR */
129 { -1, 0, 0, 0, 0, 0, 1, 0 }, /* PW */
130 { -1, 0, 0, 0, 0, 0, 0, 0 }, /* EX */
131 { -1, 0, 0, 0, 0, 0, 0, 0 } /* PD */
134 #define modes_compat(gr, rq) \
135 __dlm_compat_matrix[(gr)->lkb_grmode + 1][(rq)->lkb_rqmode + 1]
137 int dlm_modes_compat(int mode1, int mode2)
139 return __dlm_compat_matrix[mode1 + 1][mode2 + 1];
143 * Compatibility matrix for conversions with QUECVT set.
144 * Granted mode is the row; requested mode is the column.
145 * Usage: matrix[grmode+1][rqmode+1]
148 static const int __quecvt_compat_matrix[8][8] = {
149 /* UN NL CR CW PR PW EX PD */
150 {0, 0, 0, 0, 0, 0, 0, 0}, /* UN */
151 {0, 0, 1, 1, 1, 1, 1, 0}, /* NL */
152 {0, 0, 0, 1, 1, 1, 1, 0}, /* CR */
153 {0, 0, 0, 0, 1, 1, 1, 0}, /* CW */
154 {0, 0, 0, 1, 0, 1, 1, 0}, /* PR */
155 {0, 0, 0, 0, 0, 0, 1, 0}, /* PW */
156 {0, 0, 0, 0, 0, 0, 0, 0}, /* EX */
157 {0, 0, 0, 0, 0, 0, 0, 0} /* PD */
160 void dlm_print_lkb(struct dlm_lkb *lkb)
162 printk(KERN_ERR "lkb: nodeid %d id %x remid %x exflags %x flags %x\n"
163 " status %d rqmode %d grmode %d wait_type %d ast_type %d\n",
164 lkb->lkb_nodeid, lkb->lkb_id, lkb->lkb_remid, lkb->lkb_exflags,
165 lkb->lkb_flags, lkb->lkb_status, lkb->lkb_rqmode,
166 lkb->lkb_grmode, lkb->lkb_wait_type, lkb->lkb_ast_type);
169 void dlm_print_rsb(struct dlm_rsb *r)
171 printk(KERN_ERR "rsb: nodeid %d flags %lx first %x rlc %d name %s\n",
172 r->res_nodeid, r->res_flags, r->res_first_lkid,
173 r->res_recover_locks_count, r->res_name);
176 void dlm_dump_rsb(struct dlm_rsb *r)
178 struct dlm_lkb *lkb;
180 dlm_print_rsb(r);
182 printk(KERN_ERR "rsb: root_list empty %d recover_list empty %d\n",
183 list_empty(&r->res_root_list), list_empty(&r->res_recover_list));
184 printk(KERN_ERR "rsb lookup list\n");
185 list_for_each_entry(lkb, &r->res_lookup, lkb_rsb_lookup)
186 dlm_print_lkb(lkb);
187 printk(KERN_ERR "rsb grant queue:\n");
188 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue)
189 dlm_print_lkb(lkb);
190 printk(KERN_ERR "rsb convert queue:\n");
191 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue)
192 dlm_print_lkb(lkb);
193 printk(KERN_ERR "rsb wait queue:\n");
194 list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue)
195 dlm_print_lkb(lkb);
198 /* Threads cannot use the lockspace while it's being recovered */
200 static inline void dlm_lock_recovery(struct dlm_ls *ls)
202 down_read(&ls->ls_in_recovery);
205 void dlm_unlock_recovery(struct dlm_ls *ls)
207 up_read(&ls->ls_in_recovery);
210 int dlm_lock_recovery_try(struct dlm_ls *ls)
212 return down_read_trylock(&ls->ls_in_recovery);
215 static inline int can_be_queued(struct dlm_lkb *lkb)
217 return !(lkb->lkb_exflags & DLM_LKF_NOQUEUE);
220 static inline int force_blocking_asts(struct dlm_lkb *lkb)
222 return (lkb->lkb_exflags & DLM_LKF_NOQUEUEBAST);
225 static inline int is_demoted(struct dlm_lkb *lkb)
227 return (lkb->lkb_sbflags & DLM_SBF_DEMOTED);
230 static inline int is_altmode(struct dlm_lkb *lkb)
232 return (lkb->lkb_sbflags & DLM_SBF_ALTMODE);
235 static inline int is_granted(struct dlm_lkb *lkb)
237 return (lkb->lkb_status == DLM_LKSTS_GRANTED);
240 static inline int is_remote(struct dlm_rsb *r)
242 DLM_ASSERT(r->res_nodeid >= 0, dlm_print_rsb(r););
243 return !!r->res_nodeid;
246 static inline int is_process_copy(struct dlm_lkb *lkb)
248 return (lkb->lkb_nodeid && !(lkb->lkb_flags & DLM_IFL_MSTCPY));
251 static inline int is_master_copy(struct dlm_lkb *lkb)
253 if (lkb->lkb_flags & DLM_IFL_MSTCPY)
254 DLM_ASSERT(lkb->lkb_nodeid, dlm_print_lkb(lkb););
255 return (lkb->lkb_flags & DLM_IFL_MSTCPY) ? 1 : 0;
258 static inline int middle_conversion(struct dlm_lkb *lkb)
260 if ((lkb->lkb_grmode==DLM_LOCK_PR && lkb->lkb_rqmode==DLM_LOCK_CW) ||
261 (lkb->lkb_rqmode==DLM_LOCK_PR && lkb->lkb_grmode==DLM_LOCK_CW))
262 return 1;
263 return 0;
266 static inline int down_conversion(struct dlm_lkb *lkb)
268 return (!middle_conversion(lkb) && lkb->lkb_rqmode < lkb->lkb_grmode);
271 static inline int is_overlap_unlock(struct dlm_lkb *lkb)
273 return lkb->lkb_flags & DLM_IFL_OVERLAP_UNLOCK;
276 static inline int is_overlap_cancel(struct dlm_lkb *lkb)
278 return lkb->lkb_flags & DLM_IFL_OVERLAP_CANCEL;
281 static inline int is_overlap(struct dlm_lkb *lkb)
283 return (lkb->lkb_flags & (DLM_IFL_OVERLAP_UNLOCK |
284 DLM_IFL_OVERLAP_CANCEL));
287 static void queue_cast(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
289 if (is_master_copy(lkb))
290 return;
292 del_timeout(lkb);
294 DLM_ASSERT(lkb->lkb_lksb, dlm_print_lkb(lkb););
296 /* if the operation was a cancel, then return -DLM_ECANCEL, if a
297 timeout caused the cancel then return -ETIMEDOUT */
298 if (rv == -DLM_ECANCEL && (lkb->lkb_flags & DLM_IFL_TIMEOUT_CANCEL)) {
299 lkb->lkb_flags &= ~DLM_IFL_TIMEOUT_CANCEL;
300 rv = -ETIMEDOUT;
303 if (rv == -DLM_ECANCEL && (lkb->lkb_flags & DLM_IFL_DEADLOCK_CANCEL)) {
304 lkb->lkb_flags &= ~DLM_IFL_DEADLOCK_CANCEL;
305 rv = -EDEADLK;
308 lkb->lkb_lksb->sb_status = rv;
309 lkb->lkb_lksb->sb_flags = lkb->lkb_sbflags;
311 dlm_add_ast(lkb, AST_COMP);
314 static inline void queue_cast_overlap(struct dlm_rsb *r, struct dlm_lkb *lkb)
316 queue_cast(r, lkb,
317 is_overlap_unlock(lkb) ? -DLM_EUNLOCK : -DLM_ECANCEL);
320 static void queue_bast(struct dlm_rsb *r, struct dlm_lkb *lkb, int rqmode)
322 if (is_master_copy(lkb))
323 send_bast(r, lkb, rqmode);
324 else {
325 lkb->lkb_bastmode = rqmode;
326 dlm_add_ast(lkb, AST_BAST);
331 * Basic operations on rsb's and lkb's
334 static struct dlm_rsb *create_rsb(struct dlm_ls *ls, char *name, int len)
336 struct dlm_rsb *r;
338 r = allocate_rsb(ls, len);
339 if (!r)
340 return NULL;
342 r->res_ls = ls;
343 r->res_length = len;
344 memcpy(r->res_name, name, len);
345 mutex_init(&r->res_mutex);
347 INIT_LIST_HEAD(&r->res_lookup);
348 INIT_LIST_HEAD(&r->res_grantqueue);
349 INIT_LIST_HEAD(&r->res_convertqueue);
350 INIT_LIST_HEAD(&r->res_waitqueue);
351 INIT_LIST_HEAD(&r->res_root_list);
352 INIT_LIST_HEAD(&r->res_recover_list);
354 return r;
357 static int search_rsb_list(struct list_head *head, char *name, int len,
358 unsigned int flags, struct dlm_rsb **r_ret)
360 struct dlm_rsb *r;
361 int error = 0;
363 list_for_each_entry(r, head, res_hashchain) {
364 if (len == r->res_length && !memcmp(name, r->res_name, len))
365 goto found;
367 return -EBADR;
369 found:
370 if (r->res_nodeid && (flags & R_MASTER))
371 error = -ENOTBLK;
372 *r_ret = r;
373 return error;
376 static int _search_rsb(struct dlm_ls *ls, char *name, int len, int b,
377 unsigned int flags, struct dlm_rsb **r_ret)
379 struct dlm_rsb *r;
380 int error;
382 error = search_rsb_list(&ls->ls_rsbtbl[b].list, name, len, flags, &r);
383 if (!error) {
384 kref_get(&r->res_ref);
385 goto out;
387 error = search_rsb_list(&ls->ls_rsbtbl[b].toss, name, len, flags, &r);
388 if (error)
389 goto out;
391 list_move(&r->res_hashchain, &ls->ls_rsbtbl[b].list);
393 if (dlm_no_directory(ls))
394 goto out;
396 if (r->res_nodeid == -1) {
397 rsb_clear_flag(r, RSB_MASTER_UNCERTAIN);
398 r->res_first_lkid = 0;
399 } else if (r->res_nodeid > 0) {
400 rsb_set_flag(r, RSB_MASTER_UNCERTAIN);
401 r->res_first_lkid = 0;
402 } else {
403 DLM_ASSERT(r->res_nodeid == 0, dlm_print_rsb(r););
404 DLM_ASSERT(!rsb_flag(r, RSB_MASTER_UNCERTAIN),);
406 out:
407 *r_ret = r;
408 return error;
411 static int search_rsb(struct dlm_ls *ls, char *name, int len, int b,
412 unsigned int flags, struct dlm_rsb **r_ret)
414 int error;
415 write_lock(&ls->ls_rsbtbl[b].lock);
416 error = _search_rsb(ls, name, len, b, flags, r_ret);
417 write_unlock(&ls->ls_rsbtbl[b].lock);
418 return error;
422 * Find rsb in rsbtbl and potentially create/add one
424 * Delaying the release of rsb's has a similar benefit to applications keeping
425 * NL locks on an rsb, but without the guarantee that the cached master value
426 * will still be valid when the rsb is reused. Apps aren't always smart enough
427 * to keep NL locks on an rsb that they may lock again shortly; this can lead
428 * to excessive master lookups and removals if we don't delay the release.
430 * Searching for an rsb means looking through both the normal list and toss
431 * list. When found on the toss list the rsb is moved to the normal list with
432 * ref count of 1; when found on normal list the ref count is incremented.
435 static int find_rsb(struct dlm_ls *ls, char *name, int namelen,
436 unsigned int flags, struct dlm_rsb **r_ret)
438 struct dlm_rsb *r, *tmp;
439 uint32_t hash, bucket;
440 int error = 0;
442 if (dlm_no_directory(ls))
443 flags |= R_CREATE;
445 hash = jhash(name, namelen, 0);
446 bucket = hash & (ls->ls_rsbtbl_size - 1);
448 error = search_rsb(ls, name, namelen, bucket, flags, &r);
449 if (!error)
450 goto out;
452 if (error == -EBADR && !(flags & R_CREATE))
453 goto out;
455 /* the rsb was found but wasn't a master copy */
456 if (error == -ENOTBLK)
457 goto out;
459 error = -ENOMEM;
460 r = create_rsb(ls, name, namelen);
461 if (!r)
462 goto out;
464 r->res_hash = hash;
465 r->res_bucket = bucket;
466 r->res_nodeid = -1;
467 kref_init(&r->res_ref);
469 /* With no directory, the master can be set immediately */
470 if (dlm_no_directory(ls)) {
471 int nodeid = dlm_dir_nodeid(r);
472 if (nodeid == dlm_our_nodeid())
473 nodeid = 0;
474 r->res_nodeid = nodeid;
477 write_lock(&ls->ls_rsbtbl[bucket].lock);
478 error = _search_rsb(ls, name, namelen, bucket, 0, &tmp);
479 if (!error) {
480 write_unlock(&ls->ls_rsbtbl[bucket].lock);
481 free_rsb(r);
482 r = tmp;
483 goto out;
485 list_add(&r->res_hashchain, &ls->ls_rsbtbl[bucket].list);
486 write_unlock(&ls->ls_rsbtbl[bucket].lock);
487 error = 0;
488 out:
489 *r_ret = r;
490 return error;
493 int dlm_find_rsb(struct dlm_ls *ls, char *name, int namelen,
494 unsigned int flags, struct dlm_rsb **r_ret)
496 return find_rsb(ls, name, namelen, flags, r_ret);
499 /* This is only called to add a reference when the code already holds
500 a valid reference to the rsb, so there's no need for locking. */
502 static inline void hold_rsb(struct dlm_rsb *r)
504 kref_get(&r->res_ref);
507 void dlm_hold_rsb(struct dlm_rsb *r)
509 hold_rsb(r);
512 static void toss_rsb(struct kref *kref)
514 struct dlm_rsb *r = container_of(kref, struct dlm_rsb, res_ref);
515 struct dlm_ls *ls = r->res_ls;
517 DLM_ASSERT(list_empty(&r->res_root_list), dlm_print_rsb(r););
518 kref_init(&r->res_ref);
519 list_move(&r->res_hashchain, &ls->ls_rsbtbl[r->res_bucket].toss);
520 r->res_toss_time = jiffies;
521 if (r->res_lvbptr) {
522 free_lvb(r->res_lvbptr);
523 r->res_lvbptr = NULL;
527 /* When all references to the rsb are gone it's transfered to
528 the tossed list for later disposal. */
530 static void put_rsb(struct dlm_rsb *r)
532 struct dlm_ls *ls = r->res_ls;
533 uint32_t bucket = r->res_bucket;
535 write_lock(&ls->ls_rsbtbl[bucket].lock);
536 kref_put(&r->res_ref, toss_rsb);
537 write_unlock(&ls->ls_rsbtbl[bucket].lock);
540 void dlm_put_rsb(struct dlm_rsb *r)
542 put_rsb(r);
545 /* See comment for unhold_lkb */
547 static void unhold_rsb(struct dlm_rsb *r)
549 int rv;
550 rv = kref_put(&r->res_ref, toss_rsb);
551 DLM_ASSERT(!rv, dlm_dump_rsb(r););
554 static void kill_rsb(struct kref *kref)
556 struct dlm_rsb *r = container_of(kref, struct dlm_rsb, res_ref);
558 /* All work is done after the return from kref_put() so we
559 can release the write_lock before the remove and free. */
561 DLM_ASSERT(list_empty(&r->res_lookup), dlm_dump_rsb(r););
562 DLM_ASSERT(list_empty(&r->res_grantqueue), dlm_dump_rsb(r););
563 DLM_ASSERT(list_empty(&r->res_convertqueue), dlm_dump_rsb(r););
564 DLM_ASSERT(list_empty(&r->res_waitqueue), dlm_dump_rsb(r););
565 DLM_ASSERT(list_empty(&r->res_root_list), dlm_dump_rsb(r););
566 DLM_ASSERT(list_empty(&r->res_recover_list), dlm_dump_rsb(r););
569 /* Attaching/detaching lkb's from rsb's is for rsb reference counting.
570 The rsb must exist as long as any lkb's for it do. */
572 static void attach_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb)
574 hold_rsb(r);
575 lkb->lkb_resource = r;
578 static void detach_lkb(struct dlm_lkb *lkb)
580 if (lkb->lkb_resource) {
581 put_rsb(lkb->lkb_resource);
582 lkb->lkb_resource = NULL;
586 static int create_lkb(struct dlm_ls *ls, struct dlm_lkb **lkb_ret)
588 struct dlm_lkb *lkb, *tmp;
589 uint32_t lkid = 0;
590 uint16_t bucket;
592 lkb = allocate_lkb(ls);
593 if (!lkb)
594 return -ENOMEM;
596 lkb->lkb_nodeid = -1;
597 lkb->lkb_grmode = DLM_LOCK_IV;
598 kref_init(&lkb->lkb_ref);
599 INIT_LIST_HEAD(&lkb->lkb_ownqueue);
600 INIT_LIST_HEAD(&lkb->lkb_rsb_lookup);
601 INIT_LIST_HEAD(&lkb->lkb_time_list);
603 get_random_bytes(&bucket, sizeof(bucket));
604 bucket &= (ls->ls_lkbtbl_size - 1);
606 write_lock(&ls->ls_lkbtbl[bucket].lock);
608 /* counter can roll over so we must verify lkid is not in use */
610 while (lkid == 0) {
611 lkid = (bucket << 16) | ls->ls_lkbtbl[bucket].counter++;
613 list_for_each_entry(tmp, &ls->ls_lkbtbl[bucket].list,
614 lkb_idtbl_list) {
615 if (tmp->lkb_id != lkid)
616 continue;
617 lkid = 0;
618 break;
622 lkb->lkb_id = lkid;
623 list_add(&lkb->lkb_idtbl_list, &ls->ls_lkbtbl[bucket].list);
624 write_unlock(&ls->ls_lkbtbl[bucket].lock);
626 *lkb_ret = lkb;
627 return 0;
630 static struct dlm_lkb *__find_lkb(struct dlm_ls *ls, uint32_t lkid)
632 struct dlm_lkb *lkb;
633 uint16_t bucket = (lkid >> 16);
635 list_for_each_entry(lkb, &ls->ls_lkbtbl[bucket].list, lkb_idtbl_list) {
636 if (lkb->lkb_id == lkid)
637 return lkb;
639 return NULL;
642 static int find_lkb(struct dlm_ls *ls, uint32_t lkid, struct dlm_lkb **lkb_ret)
644 struct dlm_lkb *lkb;
645 uint16_t bucket = (lkid >> 16);
647 if (bucket >= ls->ls_lkbtbl_size)
648 return -EBADSLT;
650 read_lock(&ls->ls_lkbtbl[bucket].lock);
651 lkb = __find_lkb(ls, lkid);
652 if (lkb)
653 kref_get(&lkb->lkb_ref);
654 read_unlock(&ls->ls_lkbtbl[bucket].lock);
656 *lkb_ret = lkb;
657 return lkb ? 0 : -ENOENT;
660 static void kill_lkb(struct kref *kref)
662 struct dlm_lkb *lkb = container_of(kref, struct dlm_lkb, lkb_ref);
664 /* All work is done after the return from kref_put() so we
665 can release the write_lock before the detach_lkb */
667 DLM_ASSERT(!lkb->lkb_status, dlm_print_lkb(lkb););
670 /* __put_lkb() is used when an lkb may not have an rsb attached to
671 it so we need to provide the lockspace explicitly */
673 static int __put_lkb(struct dlm_ls *ls, struct dlm_lkb *lkb)
675 uint16_t bucket = (lkb->lkb_id >> 16);
677 write_lock(&ls->ls_lkbtbl[bucket].lock);
678 if (kref_put(&lkb->lkb_ref, kill_lkb)) {
679 list_del(&lkb->lkb_idtbl_list);
680 write_unlock(&ls->ls_lkbtbl[bucket].lock);
682 detach_lkb(lkb);
684 /* for local/process lkbs, lvbptr points to caller's lksb */
685 if (lkb->lkb_lvbptr && is_master_copy(lkb))
686 free_lvb(lkb->lkb_lvbptr);
687 free_lkb(lkb);
688 return 1;
689 } else {
690 write_unlock(&ls->ls_lkbtbl[bucket].lock);
691 return 0;
695 int dlm_put_lkb(struct dlm_lkb *lkb)
697 struct dlm_ls *ls;
699 DLM_ASSERT(lkb->lkb_resource, dlm_print_lkb(lkb););
700 DLM_ASSERT(lkb->lkb_resource->res_ls, dlm_print_lkb(lkb););
702 ls = lkb->lkb_resource->res_ls;
703 return __put_lkb(ls, lkb);
706 /* This is only called to add a reference when the code already holds
707 a valid reference to the lkb, so there's no need for locking. */
709 static inline void hold_lkb(struct dlm_lkb *lkb)
711 kref_get(&lkb->lkb_ref);
714 /* This is called when we need to remove a reference and are certain
715 it's not the last ref. e.g. del_lkb is always called between a
716 find_lkb/put_lkb and is always the inverse of a previous add_lkb.
717 put_lkb would work fine, but would involve unnecessary locking */
719 static inline void unhold_lkb(struct dlm_lkb *lkb)
721 int rv;
722 rv = kref_put(&lkb->lkb_ref, kill_lkb);
723 DLM_ASSERT(!rv, dlm_print_lkb(lkb););
726 static void lkb_add_ordered(struct list_head *new, struct list_head *head,
727 int mode)
729 struct dlm_lkb *lkb = NULL;
731 list_for_each_entry(lkb, head, lkb_statequeue)
732 if (lkb->lkb_rqmode < mode)
733 break;
735 if (!lkb)
736 list_add_tail(new, head);
737 else
738 __list_add(new, lkb->lkb_statequeue.prev, &lkb->lkb_statequeue);
741 /* add/remove lkb to rsb's grant/convert/wait queue */
743 static void add_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb, int status)
745 kref_get(&lkb->lkb_ref);
747 DLM_ASSERT(!lkb->lkb_status, dlm_print_lkb(lkb););
749 lkb->lkb_status = status;
751 switch (status) {
752 case DLM_LKSTS_WAITING:
753 if (lkb->lkb_exflags & DLM_LKF_HEADQUE)
754 list_add(&lkb->lkb_statequeue, &r->res_waitqueue);
755 else
756 list_add_tail(&lkb->lkb_statequeue, &r->res_waitqueue);
757 break;
758 case DLM_LKSTS_GRANTED:
759 /* convention says granted locks kept in order of grmode */
760 lkb_add_ordered(&lkb->lkb_statequeue, &r->res_grantqueue,
761 lkb->lkb_grmode);
762 break;
763 case DLM_LKSTS_CONVERT:
764 if (lkb->lkb_exflags & DLM_LKF_HEADQUE)
765 list_add(&lkb->lkb_statequeue, &r->res_convertqueue);
766 else
767 list_add_tail(&lkb->lkb_statequeue,
768 &r->res_convertqueue);
769 break;
770 default:
771 DLM_ASSERT(0, dlm_print_lkb(lkb); printk("sts=%d\n", status););
775 static void del_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb)
777 lkb->lkb_status = 0;
778 list_del(&lkb->lkb_statequeue);
779 unhold_lkb(lkb);
782 static void move_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb, int sts)
784 hold_lkb(lkb);
785 del_lkb(r, lkb);
786 add_lkb(r, lkb, sts);
787 unhold_lkb(lkb);
790 static int msg_reply_type(int mstype)
792 switch (mstype) {
793 case DLM_MSG_REQUEST:
794 return DLM_MSG_REQUEST_REPLY;
795 case DLM_MSG_CONVERT:
796 return DLM_MSG_CONVERT_REPLY;
797 case DLM_MSG_UNLOCK:
798 return DLM_MSG_UNLOCK_REPLY;
799 case DLM_MSG_CANCEL:
800 return DLM_MSG_CANCEL_REPLY;
801 case DLM_MSG_LOOKUP:
802 return DLM_MSG_LOOKUP_REPLY;
804 return -1;
807 /* add/remove lkb from global waiters list of lkb's waiting for
808 a reply from a remote node */
810 static int add_to_waiters(struct dlm_lkb *lkb, int mstype)
812 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
813 int error = 0;
815 mutex_lock(&ls->ls_waiters_mutex);
817 if (is_overlap_unlock(lkb) ||
818 (is_overlap_cancel(lkb) && (mstype == DLM_MSG_CANCEL))) {
819 error = -EINVAL;
820 goto out;
823 if (lkb->lkb_wait_type || is_overlap_cancel(lkb)) {
824 switch (mstype) {
825 case DLM_MSG_UNLOCK:
826 lkb->lkb_flags |= DLM_IFL_OVERLAP_UNLOCK;
827 break;
828 case DLM_MSG_CANCEL:
829 lkb->lkb_flags |= DLM_IFL_OVERLAP_CANCEL;
830 break;
831 default:
832 error = -EBUSY;
833 goto out;
835 lkb->lkb_wait_count++;
836 hold_lkb(lkb);
838 log_debug(ls, "add overlap %x cur %d new %d count %d flags %x",
839 lkb->lkb_id, lkb->lkb_wait_type, mstype,
840 lkb->lkb_wait_count, lkb->lkb_flags);
841 goto out;
844 DLM_ASSERT(!lkb->lkb_wait_count,
845 dlm_print_lkb(lkb);
846 printk("wait_count %d\n", lkb->lkb_wait_count););
848 lkb->lkb_wait_count++;
849 lkb->lkb_wait_type = mstype;
850 hold_lkb(lkb);
851 list_add(&lkb->lkb_wait_reply, &ls->ls_waiters);
852 out:
853 if (error)
854 log_error(ls, "add_to_waiters %x error %d flags %x %d %d %s",
855 lkb->lkb_id, error, lkb->lkb_flags, mstype,
856 lkb->lkb_wait_type, lkb->lkb_resource->res_name);
857 mutex_unlock(&ls->ls_waiters_mutex);
858 return error;
861 /* We clear the RESEND flag because we might be taking an lkb off the waiters
862 list as part of process_requestqueue (e.g. a lookup that has an optimized
863 request reply on the requestqueue) between dlm_recover_waiters_pre() which
864 set RESEND and dlm_recover_waiters_post() */
866 static int _remove_from_waiters(struct dlm_lkb *lkb, int mstype)
868 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
869 int overlap_done = 0;
871 if (is_overlap_unlock(lkb) && (mstype == DLM_MSG_UNLOCK_REPLY)) {
872 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
873 overlap_done = 1;
874 goto out_del;
877 if (is_overlap_cancel(lkb) && (mstype == DLM_MSG_CANCEL_REPLY)) {
878 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
879 overlap_done = 1;
880 goto out_del;
883 /* N.B. type of reply may not always correspond to type of original
884 msg due to lookup->request optimization, verify others? */
886 if (lkb->lkb_wait_type) {
887 lkb->lkb_wait_type = 0;
888 goto out_del;
891 log_error(ls, "remove_from_waiters lkid %x flags %x types %d %d",
892 lkb->lkb_id, lkb->lkb_flags, mstype, lkb->lkb_wait_type);
893 return -1;
895 out_del:
896 /* the force-unlock/cancel has completed and we haven't recvd a reply
897 to the op that was in progress prior to the unlock/cancel; we
898 give up on any reply to the earlier op. FIXME: not sure when/how
899 this would happen */
901 if (overlap_done && lkb->lkb_wait_type) {
902 log_error(ls, "remove_from_waiters %x reply %d give up on %d",
903 lkb->lkb_id, mstype, lkb->lkb_wait_type);
904 lkb->lkb_wait_count--;
905 lkb->lkb_wait_type = 0;
908 DLM_ASSERT(lkb->lkb_wait_count, dlm_print_lkb(lkb););
910 lkb->lkb_flags &= ~DLM_IFL_RESEND;
911 lkb->lkb_wait_count--;
912 if (!lkb->lkb_wait_count)
913 list_del_init(&lkb->lkb_wait_reply);
914 unhold_lkb(lkb);
915 return 0;
918 static int remove_from_waiters(struct dlm_lkb *lkb, int mstype)
920 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
921 int error;
923 mutex_lock(&ls->ls_waiters_mutex);
924 error = _remove_from_waiters(lkb, mstype);
925 mutex_unlock(&ls->ls_waiters_mutex);
926 return error;
929 /* Handles situations where we might be processing a "fake" or "stub" reply in
930 which we can't try to take waiters_mutex again. */
932 static int remove_from_waiters_ms(struct dlm_lkb *lkb, struct dlm_message *ms)
934 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
935 int error;
937 if (ms != &ls->ls_stub_ms)
938 mutex_lock(&ls->ls_waiters_mutex);
939 error = _remove_from_waiters(lkb, ms->m_type);
940 if (ms != &ls->ls_stub_ms)
941 mutex_unlock(&ls->ls_waiters_mutex);
942 return error;
945 static void dir_remove(struct dlm_rsb *r)
947 int to_nodeid;
949 if (dlm_no_directory(r->res_ls))
950 return;
952 to_nodeid = dlm_dir_nodeid(r);
953 if (to_nodeid != dlm_our_nodeid())
954 send_remove(r);
955 else
956 dlm_dir_remove_entry(r->res_ls, to_nodeid,
957 r->res_name, r->res_length);
960 /* FIXME: shouldn't this be able to exit as soon as one non-due rsb is
961 found since they are in order of newest to oldest? */
963 static int shrink_bucket(struct dlm_ls *ls, int b)
965 struct dlm_rsb *r;
966 int count = 0, found;
968 for (;;) {
969 found = 0;
970 write_lock(&ls->ls_rsbtbl[b].lock);
971 list_for_each_entry_reverse(r, &ls->ls_rsbtbl[b].toss,
972 res_hashchain) {
973 if (!time_after_eq(jiffies, r->res_toss_time +
974 dlm_config.ci_toss_secs * HZ))
975 continue;
976 found = 1;
977 break;
980 if (!found) {
981 write_unlock(&ls->ls_rsbtbl[b].lock);
982 break;
985 if (kref_put(&r->res_ref, kill_rsb)) {
986 list_del(&r->res_hashchain);
987 write_unlock(&ls->ls_rsbtbl[b].lock);
989 if (is_master(r))
990 dir_remove(r);
991 free_rsb(r);
992 count++;
993 } else {
994 write_unlock(&ls->ls_rsbtbl[b].lock);
995 log_error(ls, "tossed rsb in use %s", r->res_name);
999 return count;
1002 void dlm_scan_rsbs(struct dlm_ls *ls)
1004 int i;
1006 for (i = 0; i < ls->ls_rsbtbl_size; i++) {
1007 shrink_bucket(ls, i);
1008 if (dlm_locking_stopped(ls))
1009 break;
1010 cond_resched();
1014 static void add_timeout(struct dlm_lkb *lkb)
1016 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1018 if (is_master_copy(lkb)) {
1019 lkb->lkb_timestamp = jiffies;
1020 return;
1023 if (test_bit(LSFL_TIMEWARN, &ls->ls_flags) &&
1024 !(lkb->lkb_exflags & DLM_LKF_NODLCKWT)) {
1025 lkb->lkb_flags |= DLM_IFL_WATCH_TIMEWARN;
1026 goto add_it;
1028 if (lkb->lkb_exflags & DLM_LKF_TIMEOUT)
1029 goto add_it;
1030 return;
1032 add_it:
1033 DLM_ASSERT(list_empty(&lkb->lkb_time_list), dlm_print_lkb(lkb););
1034 mutex_lock(&ls->ls_timeout_mutex);
1035 hold_lkb(lkb);
1036 lkb->lkb_timestamp = jiffies;
1037 list_add_tail(&lkb->lkb_time_list, &ls->ls_timeout);
1038 mutex_unlock(&ls->ls_timeout_mutex);
1041 static void del_timeout(struct dlm_lkb *lkb)
1043 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1045 mutex_lock(&ls->ls_timeout_mutex);
1046 if (!list_empty(&lkb->lkb_time_list)) {
1047 list_del_init(&lkb->lkb_time_list);
1048 unhold_lkb(lkb);
1050 mutex_unlock(&ls->ls_timeout_mutex);
1053 /* FIXME: is it safe to look at lkb_exflags, lkb_flags, lkb_timestamp, and
1054 lkb_lksb_timeout without lock_rsb? Note: we can't lock timeout_mutex
1055 and then lock rsb because of lock ordering in add_timeout. We may need
1056 to specify some special timeout-related bits in the lkb that are just to
1057 be accessed under the timeout_mutex. */
1059 void dlm_scan_timeout(struct dlm_ls *ls)
1061 struct dlm_rsb *r;
1062 struct dlm_lkb *lkb;
1063 int do_cancel, do_warn;
1065 for (;;) {
1066 if (dlm_locking_stopped(ls))
1067 break;
1069 do_cancel = 0;
1070 do_warn = 0;
1071 mutex_lock(&ls->ls_timeout_mutex);
1072 list_for_each_entry(lkb, &ls->ls_timeout, lkb_time_list) {
1074 if ((lkb->lkb_exflags & DLM_LKF_TIMEOUT) &&
1075 time_after_eq(jiffies, lkb->lkb_timestamp +
1076 lkb->lkb_timeout_cs * HZ/100))
1077 do_cancel = 1;
1079 if ((lkb->lkb_flags & DLM_IFL_WATCH_TIMEWARN) &&
1080 time_after_eq(jiffies, lkb->lkb_timestamp +
1081 dlm_config.ci_timewarn_cs * HZ/100))
1082 do_warn = 1;
1084 if (!do_cancel && !do_warn)
1085 continue;
1086 hold_lkb(lkb);
1087 break;
1089 mutex_unlock(&ls->ls_timeout_mutex);
1091 if (!do_cancel && !do_warn)
1092 break;
1094 r = lkb->lkb_resource;
1095 hold_rsb(r);
1096 lock_rsb(r);
1098 if (do_warn) {
1099 /* clear flag so we only warn once */
1100 lkb->lkb_flags &= ~DLM_IFL_WATCH_TIMEWARN;
1101 if (!(lkb->lkb_exflags & DLM_LKF_TIMEOUT))
1102 del_timeout(lkb);
1103 dlm_timeout_warn(lkb);
1106 if (do_cancel) {
1107 log_debug(ls, "timeout cancel %x node %d %s",
1108 lkb->lkb_id, lkb->lkb_nodeid, r->res_name);
1109 lkb->lkb_flags &= ~DLM_IFL_WATCH_TIMEWARN;
1110 lkb->lkb_flags |= DLM_IFL_TIMEOUT_CANCEL;
1111 del_timeout(lkb);
1112 _cancel_lock(r, lkb);
1115 unlock_rsb(r);
1116 unhold_rsb(r);
1117 dlm_put_lkb(lkb);
1121 /* This is only called by dlm_recoverd, and we rely on dlm_ls_stop() stopping
1122 dlm_recoverd before checking/setting ls_recover_begin. */
1124 void dlm_adjust_timeouts(struct dlm_ls *ls)
1126 struct dlm_lkb *lkb;
1127 long adj = jiffies - ls->ls_recover_begin;
1129 ls->ls_recover_begin = 0;
1130 mutex_lock(&ls->ls_timeout_mutex);
1131 list_for_each_entry(lkb, &ls->ls_timeout, lkb_time_list)
1132 lkb->lkb_timestamp += adj;
1133 mutex_unlock(&ls->ls_timeout_mutex);
1136 /* lkb is master or local copy */
1138 static void set_lvb_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1140 int b, len = r->res_ls->ls_lvblen;
1142 /* b=1 lvb returned to caller
1143 b=0 lvb written to rsb or invalidated
1144 b=-1 do nothing */
1146 b = dlm_lvb_operations[lkb->lkb_grmode + 1][lkb->lkb_rqmode + 1];
1148 if (b == 1) {
1149 if (!lkb->lkb_lvbptr)
1150 return;
1152 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1153 return;
1155 if (!r->res_lvbptr)
1156 return;
1158 memcpy(lkb->lkb_lvbptr, r->res_lvbptr, len);
1159 lkb->lkb_lvbseq = r->res_lvbseq;
1161 } else if (b == 0) {
1162 if (lkb->lkb_exflags & DLM_LKF_IVVALBLK) {
1163 rsb_set_flag(r, RSB_VALNOTVALID);
1164 return;
1167 if (!lkb->lkb_lvbptr)
1168 return;
1170 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1171 return;
1173 if (!r->res_lvbptr)
1174 r->res_lvbptr = allocate_lvb(r->res_ls);
1176 if (!r->res_lvbptr)
1177 return;
1179 memcpy(r->res_lvbptr, lkb->lkb_lvbptr, len);
1180 r->res_lvbseq++;
1181 lkb->lkb_lvbseq = r->res_lvbseq;
1182 rsb_clear_flag(r, RSB_VALNOTVALID);
1185 if (rsb_flag(r, RSB_VALNOTVALID))
1186 lkb->lkb_sbflags |= DLM_SBF_VALNOTVALID;
1189 static void set_lvb_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1191 if (lkb->lkb_grmode < DLM_LOCK_PW)
1192 return;
1194 if (lkb->lkb_exflags & DLM_LKF_IVVALBLK) {
1195 rsb_set_flag(r, RSB_VALNOTVALID);
1196 return;
1199 if (!lkb->lkb_lvbptr)
1200 return;
1202 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1203 return;
1205 if (!r->res_lvbptr)
1206 r->res_lvbptr = allocate_lvb(r->res_ls);
1208 if (!r->res_lvbptr)
1209 return;
1211 memcpy(r->res_lvbptr, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
1212 r->res_lvbseq++;
1213 rsb_clear_flag(r, RSB_VALNOTVALID);
1216 /* lkb is process copy (pc) */
1218 static void set_lvb_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb,
1219 struct dlm_message *ms)
1221 int b;
1223 if (!lkb->lkb_lvbptr)
1224 return;
1226 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1227 return;
1229 b = dlm_lvb_operations[lkb->lkb_grmode + 1][lkb->lkb_rqmode + 1];
1230 if (b == 1) {
1231 int len = receive_extralen(ms);
1232 memcpy(lkb->lkb_lvbptr, ms->m_extra, len);
1233 lkb->lkb_lvbseq = ms->m_lvbseq;
1237 /* Manipulate lkb's on rsb's convert/granted/waiting queues
1238 remove_lock -- used for unlock, removes lkb from granted
1239 revert_lock -- used for cancel, moves lkb from convert to granted
1240 grant_lock -- used for request and convert, adds lkb to granted or
1241 moves lkb from convert or waiting to granted
1243 Each of these is used for master or local copy lkb's. There is
1244 also a _pc() variation used to make the corresponding change on
1245 a process copy (pc) lkb. */
1247 static void _remove_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1249 del_lkb(r, lkb);
1250 lkb->lkb_grmode = DLM_LOCK_IV;
1251 /* this unhold undoes the original ref from create_lkb()
1252 so this leads to the lkb being freed */
1253 unhold_lkb(lkb);
1256 static void remove_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1258 set_lvb_unlock(r, lkb);
1259 _remove_lock(r, lkb);
1262 static void remove_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb)
1264 _remove_lock(r, lkb);
1267 /* returns: 0 did nothing
1268 1 moved lock to granted
1269 -1 removed lock */
1271 static int revert_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1273 int rv = 0;
1275 lkb->lkb_rqmode = DLM_LOCK_IV;
1277 switch (lkb->lkb_status) {
1278 case DLM_LKSTS_GRANTED:
1279 break;
1280 case DLM_LKSTS_CONVERT:
1281 move_lkb(r, lkb, DLM_LKSTS_GRANTED);
1282 rv = 1;
1283 break;
1284 case DLM_LKSTS_WAITING:
1285 del_lkb(r, lkb);
1286 lkb->lkb_grmode = DLM_LOCK_IV;
1287 /* this unhold undoes the original ref from create_lkb()
1288 so this leads to the lkb being freed */
1289 unhold_lkb(lkb);
1290 rv = -1;
1291 break;
1292 default:
1293 log_print("invalid status for revert %d", lkb->lkb_status);
1295 return rv;
1298 static int revert_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb)
1300 return revert_lock(r, lkb);
1303 static void _grant_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1305 if (lkb->lkb_grmode != lkb->lkb_rqmode) {
1306 lkb->lkb_grmode = lkb->lkb_rqmode;
1307 if (lkb->lkb_status)
1308 move_lkb(r, lkb, DLM_LKSTS_GRANTED);
1309 else
1310 add_lkb(r, lkb, DLM_LKSTS_GRANTED);
1313 lkb->lkb_rqmode = DLM_LOCK_IV;
1316 static void grant_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1318 set_lvb_lock(r, lkb);
1319 _grant_lock(r, lkb);
1320 lkb->lkb_highbast = 0;
1323 static void grant_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb,
1324 struct dlm_message *ms)
1326 set_lvb_lock_pc(r, lkb, ms);
1327 _grant_lock(r, lkb);
1330 /* called by grant_pending_locks() which means an async grant message must
1331 be sent to the requesting node in addition to granting the lock if the
1332 lkb belongs to a remote node. */
1334 static void grant_lock_pending(struct dlm_rsb *r, struct dlm_lkb *lkb)
1336 grant_lock(r, lkb);
1337 if (is_master_copy(lkb))
1338 send_grant(r, lkb);
1339 else
1340 queue_cast(r, lkb, 0);
1343 /* The special CONVDEADLK, ALTPR and ALTCW flags allow the master to
1344 change the granted/requested modes. We're munging things accordingly in
1345 the process copy.
1346 CONVDEADLK: our grmode may have been forced down to NL to resolve a
1347 conversion deadlock
1348 ALTPR/ALTCW: our rqmode may have been changed to PR or CW to become
1349 compatible with other granted locks */
1351 static void munge_demoted(struct dlm_lkb *lkb, struct dlm_message *ms)
1353 if (ms->m_type != DLM_MSG_CONVERT_REPLY) {
1354 log_print("munge_demoted %x invalid reply type %d",
1355 lkb->lkb_id, ms->m_type);
1356 return;
1359 if (lkb->lkb_rqmode == DLM_LOCK_IV || lkb->lkb_grmode == DLM_LOCK_IV) {
1360 log_print("munge_demoted %x invalid modes gr %d rq %d",
1361 lkb->lkb_id, lkb->lkb_grmode, lkb->lkb_rqmode);
1362 return;
1365 lkb->lkb_grmode = DLM_LOCK_NL;
1368 static void munge_altmode(struct dlm_lkb *lkb, struct dlm_message *ms)
1370 if (ms->m_type != DLM_MSG_REQUEST_REPLY &&
1371 ms->m_type != DLM_MSG_GRANT) {
1372 log_print("munge_altmode %x invalid reply type %d",
1373 lkb->lkb_id, ms->m_type);
1374 return;
1377 if (lkb->lkb_exflags & DLM_LKF_ALTPR)
1378 lkb->lkb_rqmode = DLM_LOCK_PR;
1379 else if (lkb->lkb_exflags & DLM_LKF_ALTCW)
1380 lkb->lkb_rqmode = DLM_LOCK_CW;
1381 else {
1382 log_print("munge_altmode invalid exflags %x", lkb->lkb_exflags);
1383 dlm_print_lkb(lkb);
1387 static inline int first_in_list(struct dlm_lkb *lkb, struct list_head *head)
1389 struct dlm_lkb *first = list_entry(head->next, struct dlm_lkb,
1390 lkb_statequeue);
1391 if (lkb->lkb_id == first->lkb_id)
1392 return 1;
1394 return 0;
1397 /* Check if the given lkb conflicts with another lkb on the queue. */
1399 static int queue_conflict(struct list_head *head, struct dlm_lkb *lkb)
1401 struct dlm_lkb *this;
1403 list_for_each_entry(this, head, lkb_statequeue) {
1404 if (this == lkb)
1405 continue;
1406 if (!modes_compat(this, lkb))
1407 return 1;
1409 return 0;
1413 * "A conversion deadlock arises with a pair of lock requests in the converting
1414 * queue for one resource. The granted mode of each lock blocks the requested
1415 * mode of the other lock."
1417 * Part 2: if the granted mode of lkb is preventing an earlier lkb in the
1418 * convert queue from being granted, then deadlk/demote lkb.
1420 * Example:
1421 * Granted Queue: empty
1422 * Convert Queue: NL->EX (first lock)
1423 * PR->EX (second lock)
1425 * The first lock can't be granted because of the granted mode of the second
1426 * lock and the second lock can't be granted because it's not first in the
1427 * list. We either cancel lkb's conversion (PR->EX) and return EDEADLK, or we
1428 * demote the granted mode of lkb (from PR to NL) if it has the CONVDEADLK
1429 * flag set and return DEMOTED in the lksb flags.
1431 * Originally, this function detected conv-deadlk in a more limited scope:
1432 * - if !modes_compat(lkb1, lkb2) && !modes_compat(lkb2, lkb1), or
1433 * - if lkb1 was the first entry in the queue (not just earlier), and was
1434 * blocked by the granted mode of lkb2, and there was nothing on the
1435 * granted queue preventing lkb1 from being granted immediately, i.e.
1436 * lkb2 was the only thing preventing lkb1 from being granted.
1438 * That second condition meant we'd only say there was conv-deadlk if
1439 * resolving it (by demotion) would lead to the first lock on the convert
1440 * queue being granted right away. It allowed conversion deadlocks to exist
1441 * between locks on the convert queue while they couldn't be granted anyway.
1443 * Now, we detect and take action on conversion deadlocks immediately when
1444 * they're created, even if they may not be immediately consequential. If
1445 * lkb1 exists anywhere in the convert queue and lkb2 comes in with a granted
1446 * mode that would prevent lkb1's conversion from being granted, we do a
1447 * deadlk/demote on lkb2 right away and don't let it onto the convert queue.
1448 * I think this means that the lkb_is_ahead condition below should always
1449 * be zero, i.e. there will never be conv-deadlk between two locks that are
1450 * both already on the convert queue.
1453 static int conversion_deadlock_detect(struct dlm_rsb *r, struct dlm_lkb *lkb2)
1455 struct dlm_lkb *lkb1;
1456 int lkb_is_ahead = 0;
1458 list_for_each_entry(lkb1, &r->res_convertqueue, lkb_statequeue) {
1459 if (lkb1 == lkb2) {
1460 lkb_is_ahead = 1;
1461 continue;
1464 if (!lkb_is_ahead) {
1465 if (!modes_compat(lkb2, lkb1))
1466 return 1;
1467 } else {
1468 if (!modes_compat(lkb2, lkb1) &&
1469 !modes_compat(lkb1, lkb2))
1470 return 1;
1473 return 0;
1477 * Return 1 if the lock can be granted, 0 otherwise.
1478 * Also detect and resolve conversion deadlocks.
1480 * lkb is the lock to be granted
1482 * now is 1 if the function is being called in the context of the
1483 * immediate request, it is 0 if called later, after the lock has been
1484 * queued.
1486 * References are from chapter 6 of "VAXcluster Principles" by Roy Davis
1489 static int _can_be_granted(struct dlm_rsb *r, struct dlm_lkb *lkb, int now)
1491 int8_t conv = (lkb->lkb_grmode != DLM_LOCK_IV);
1494 * 6-10: Version 5.4 introduced an option to address the phenomenon of
1495 * a new request for a NL mode lock being blocked.
1497 * 6-11: If the optional EXPEDITE flag is used with the new NL mode
1498 * request, then it would be granted. In essence, the use of this flag
1499 * tells the Lock Manager to expedite theis request by not considering
1500 * what may be in the CONVERTING or WAITING queues... As of this
1501 * writing, the EXPEDITE flag can be used only with new requests for NL
1502 * mode locks. This flag is not valid for conversion requests.
1504 * A shortcut. Earlier checks return an error if EXPEDITE is used in a
1505 * conversion or used with a non-NL requested mode. We also know an
1506 * EXPEDITE request is always granted immediately, so now must always
1507 * be 1. The full condition to grant an expedite request: (now &&
1508 * !conv && lkb->rqmode == DLM_LOCK_NL && (flags & EXPEDITE)) can
1509 * therefore be shortened to just checking the flag.
1512 if (lkb->lkb_exflags & DLM_LKF_EXPEDITE)
1513 return 1;
1516 * A shortcut. Without this, !queue_conflict(grantqueue, lkb) would be
1517 * added to the remaining conditions.
1520 if (queue_conflict(&r->res_grantqueue, lkb))
1521 goto out;
1524 * 6-3: By default, a conversion request is immediately granted if the
1525 * requested mode is compatible with the modes of all other granted
1526 * locks
1529 if (queue_conflict(&r->res_convertqueue, lkb))
1530 goto out;
1533 * 6-5: But the default algorithm for deciding whether to grant or
1534 * queue conversion requests does not by itself guarantee that such
1535 * requests are serviced on a "first come first serve" basis. This, in
1536 * turn, can lead to a phenomenon known as "indefinate postponement".
1538 * 6-7: This issue is dealt with by using the optional QUECVT flag with
1539 * the system service employed to request a lock conversion. This flag
1540 * forces certain conversion requests to be queued, even if they are
1541 * compatible with the granted modes of other locks on the same
1542 * resource. Thus, the use of this flag results in conversion requests
1543 * being ordered on a "first come first servce" basis.
1545 * DCT: This condition is all about new conversions being able to occur
1546 * "in place" while the lock remains on the granted queue (assuming
1547 * nothing else conflicts.) IOW if QUECVT isn't set, a conversion
1548 * doesn't _have_ to go onto the convert queue where it's processed in
1549 * order. The "now" variable is necessary to distinguish converts
1550 * being received and processed for the first time now, because once a
1551 * convert is moved to the conversion queue the condition below applies
1552 * requiring fifo granting.
1555 if (now && conv && !(lkb->lkb_exflags & DLM_LKF_QUECVT))
1556 return 1;
1559 * The NOORDER flag is set to avoid the standard vms rules on grant
1560 * order.
1563 if (lkb->lkb_exflags & DLM_LKF_NOORDER)
1564 return 1;
1567 * 6-3: Once in that queue [CONVERTING], a conversion request cannot be
1568 * granted until all other conversion requests ahead of it are granted
1569 * and/or canceled.
1572 if (!now && conv && first_in_list(lkb, &r->res_convertqueue))
1573 return 1;
1576 * 6-4: By default, a new request is immediately granted only if all
1577 * three of the following conditions are satisfied when the request is
1578 * issued:
1579 * - The queue of ungranted conversion requests for the resource is
1580 * empty.
1581 * - The queue of ungranted new requests for the resource is empty.
1582 * - The mode of the new request is compatible with the most
1583 * restrictive mode of all granted locks on the resource.
1586 if (now && !conv && list_empty(&r->res_convertqueue) &&
1587 list_empty(&r->res_waitqueue))
1588 return 1;
1591 * 6-4: Once a lock request is in the queue of ungranted new requests,
1592 * it cannot be granted until the queue of ungranted conversion
1593 * requests is empty, all ungranted new requests ahead of it are
1594 * granted and/or canceled, and it is compatible with the granted mode
1595 * of the most restrictive lock granted on the resource.
1598 if (!now && !conv && list_empty(&r->res_convertqueue) &&
1599 first_in_list(lkb, &r->res_waitqueue))
1600 return 1;
1601 out:
1602 return 0;
1605 static int can_be_granted(struct dlm_rsb *r, struct dlm_lkb *lkb, int now,
1606 int *err)
1608 int rv;
1609 int8_t alt = 0, rqmode = lkb->lkb_rqmode;
1610 int8_t is_convert = (lkb->lkb_grmode != DLM_LOCK_IV);
1612 if (err)
1613 *err = 0;
1615 rv = _can_be_granted(r, lkb, now);
1616 if (rv)
1617 goto out;
1620 * The CONVDEADLK flag is non-standard and tells the dlm to resolve
1621 * conversion deadlocks by demoting grmode to NL, otherwise the dlm
1622 * cancels one of the locks.
1625 if (is_convert && can_be_queued(lkb) &&
1626 conversion_deadlock_detect(r, lkb)) {
1627 if (lkb->lkb_exflags & DLM_LKF_CONVDEADLK) {
1628 lkb->lkb_grmode = DLM_LOCK_NL;
1629 lkb->lkb_sbflags |= DLM_SBF_DEMOTED;
1630 } else if (!(lkb->lkb_exflags & DLM_LKF_NODLCKWT)) {
1631 if (err)
1632 *err = -EDEADLK;
1633 else {
1634 log_print("can_be_granted deadlock %x now %d",
1635 lkb->lkb_id, now);
1636 dlm_dump_rsb(r);
1639 goto out;
1643 * The ALTPR and ALTCW flags are non-standard and tell the dlm to try
1644 * to grant a request in a mode other than the normal rqmode. It's a
1645 * simple way to provide a big optimization to applications that can
1646 * use them.
1649 if (rqmode != DLM_LOCK_PR && (lkb->lkb_exflags & DLM_LKF_ALTPR))
1650 alt = DLM_LOCK_PR;
1651 else if (rqmode != DLM_LOCK_CW && (lkb->lkb_exflags & DLM_LKF_ALTCW))
1652 alt = DLM_LOCK_CW;
1654 if (alt) {
1655 lkb->lkb_rqmode = alt;
1656 rv = _can_be_granted(r, lkb, now);
1657 if (rv)
1658 lkb->lkb_sbflags |= DLM_SBF_ALTMODE;
1659 else
1660 lkb->lkb_rqmode = rqmode;
1662 out:
1663 return rv;
1666 /* FIXME: I don't think that can_be_granted() can/will demote or find deadlock
1667 for locks pending on the convert list. Once verified (watch for these
1668 log_prints), we should be able to just call _can_be_granted() and not
1669 bother with the demote/deadlk cases here (and there's no easy way to deal
1670 with a deadlk here, we'd have to generate something like grant_lock with
1671 the deadlk error.) */
1673 /* returns the highest requested mode of all blocked conversions */
1675 static int grant_pending_convert(struct dlm_rsb *r, int high)
1677 struct dlm_lkb *lkb, *s;
1678 int hi, demoted, quit, grant_restart, demote_restart;
1679 int deadlk;
1681 quit = 0;
1682 restart:
1683 grant_restart = 0;
1684 demote_restart = 0;
1685 hi = DLM_LOCK_IV;
1687 list_for_each_entry_safe(lkb, s, &r->res_convertqueue, lkb_statequeue) {
1688 demoted = is_demoted(lkb);
1689 deadlk = 0;
1691 if (can_be_granted(r, lkb, 0, &deadlk)) {
1692 grant_lock_pending(r, lkb);
1693 grant_restart = 1;
1694 continue;
1697 if (!demoted && is_demoted(lkb)) {
1698 log_print("WARN: pending demoted %x node %d %s",
1699 lkb->lkb_id, lkb->lkb_nodeid, r->res_name);
1700 demote_restart = 1;
1701 continue;
1704 if (deadlk) {
1705 log_print("WARN: pending deadlock %x node %d %s",
1706 lkb->lkb_id, lkb->lkb_nodeid, r->res_name);
1707 dlm_dump_rsb(r);
1708 continue;
1711 hi = max_t(int, lkb->lkb_rqmode, hi);
1714 if (grant_restart)
1715 goto restart;
1716 if (demote_restart && !quit) {
1717 quit = 1;
1718 goto restart;
1721 return max_t(int, high, hi);
1724 static int grant_pending_wait(struct dlm_rsb *r, int high)
1726 struct dlm_lkb *lkb, *s;
1728 list_for_each_entry_safe(lkb, s, &r->res_waitqueue, lkb_statequeue) {
1729 if (can_be_granted(r, lkb, 0, NULL))
1730 grant_lock_pending(r, lkb);
1731 else
1732 high = max_t(int, lkb->lkb_rqmode, high);
1735 return high;
1738 static void grant_pending_locks(struct dlm_rsb *r)
1740 struct dlm_lkb *lkb, *s;
1741 int high = DLM_LOCK_IV;
1743 DLM_ASSERT(is_master(r), dlm_dump_rsb(r););
1745 high = grant_pending_convert(r, high);
1746 high = grant_pending_wait(r, high);
1748 if (high == DLM_LOCK_IV)
1749 return;
1752 * If there are locks left on the wait/convert queue then send blocking
1753 * ASTs to granted locks based on the largest requested mode (high)
1754 * found above. FIXME: highbast < high comparison not valid for PR/CW.
1757 list_for_each_entry_safe(lkb, s, &r->res_grantqueue, lkb_statequeue) {
1758 if (lkb->lkb_bastaddr && (lkb->lkb_highbast < high) &&
1759 !__dlm_compat_matrix[lkb->lkb_grmode+1][high+1]) {
1760 queue_bast(r, lkb, high);
1761 lkb->lkb_highbast = high;
1766 static void send_bast_queue(struct dlm_rsb *r, struct list_head *head,
1767 struct dlm_lkb *lkb)
1769 struct dlm_lkb *gr;
1771 list_for_each_entry(gr, head, lkb_statequeue) {
1772 if (gr->lkb_bastaddr &&
1773 gr->lkb_highbast < lkb->lkb_rqmode &&
1774 !modes_compat(gr, lkb)) {
1775 queue_bast(r, gr, lkb->lkb_rqmode);
1776 gr->lkb_highbast = lkb->lkb_rqmode;
1781 static void send_blocking_asts(struct dlm_rsb *r, struct dlm_lkb *lkb)
1783 send_bast_queue(r, &r->res_grantqueue, lkb);
1786 static void send_blocking_asts_all(struct dlm_rsb *r, struct dlm_lkb *lkb)
1788 send_bast_queue(r, &r->res_grantqueue, lkb);
1789 send_bast_queue(r, &r->res_convertqueue, lkb);
1792 /* set_master(r, lkb) -- set the master nodeid of a resource
1794 The purpose of this function is to set the nodeid field in the given
1795 lkb using the nodeid field in the given rsb. If the rsb's nodeid is
1796 known, it can just be copied to the lkb and the function will return
1797 0. If the rsb's nodeid is _not_ known, it needs to be looked up
1798 before it can be copied to the lkb.
1800 When the rsb nodeid is being looked up remotely, the initial lkb
1801 causing the lookup is kept on the ls_waiters list waiting for the
1802 lookup reply. Other lkb's waiting for the same rsb lookup are kept
1803 on the rsb's res_lookup list until the master is verified.
1805 Return values:
1806 0: nodeid is set in rsb/lkb and the caller should go ahead and use it
1807 1: the rsb master is not available and the lkb has been placed on
1808 a wait queue
1811 static int set_master(struct dlm_rsb *r, struct dlm_lkb *lkb)
1813 struct dlm_ls *ls = r->res_ls;
1814 int error, dir_nodeid, ret_nodeid, our_nodeid = dlm_our_nodeid();
1816 if (rsb_flag(r, RSB_MASTER_UNCERTAIN)) {
1817 rsb_clear_flag(r, RSB_MASTER_UNCERTAIN);
1818 r->res_first_lkid = lkb->lkb_id;
1819 lkb->lkb_nodeid = r->res_nodeid;
1820 return 0;
1823 if (r->res_first_lkid && r->res_first_lkid != lkb->lkb_id) {
1824 list_add_tail(&lkb->lkb_rsb_lookup, &r->res_lookup);
1825 return 1;
1828 if (r->res_nodeid == 0) {
1829 lkb->lkb_nodeid = 0;
1830 return 0;
1833 if (r->res_nodeid > 0) {
1834 lkb->lkb_nodeid = r->res_nodeid;
1835 return 0;
1838 DLM_ASSERT(r->res_nodeid == -1, dlm_dump_rsb(r););
1840 dir_nodeid = dlm_dir_nodeid(r);
1842 if (dir_nodeid != our_nodeid) {
1843 r->res_first_lkid = lkb->lkb_id;
1844 send_lookup(r, lkb);
1845 return 1;
1848 for (;;) {
1849 /* It's possible for dlm_scand to remove an old rsb for
1850 this same resource from the toss list, us to create
1851 a new one, look up the master locally, and find it
1852 already exists just before dlm_scand does the
1853 dir_remove() on the previous rsb. */
1855 error = dlm_dir_lookup(ls, our_nodeid, r->res_name,
1856 r->res_length, &ret_nodeid);
1857 if (!error)
1858 break;
1859 log_debug(ls, "dir_lookup error %d %s", error, r->res_name);
1860 schedule();
1863 if (ret_nodeid == our_nodeid) {
1864 r->res_first_lkid = 0;
1865 r->res_nodeid = 0;
1866 lkb->lkb_nodeid = 0;
1867 } else {
1868 r->res_first_lkid = lkb->lkb_id;
1869 r->res_nodeid = ret_nodeid;
1870 lkb->lkb_nodeid = ret_nodeid;
1872 return 0;
1875 static void process_lookup_list(struct dlm_rsb *r)
1877 struct dlm_lkb *lkb, *safe;
1879 list_for_each_entry_safe(lkb, safe, &r->res_lookup, lkb_rsb_lookup) {
1880 list_del_init(&lkb->lkb_rsb_lookup);
1881 _request_lock(r, lkb);
1882 schedule();
1886 /* confirm_master -- confirm (or deny) an rsb's master nodeid */
1888 static void confirm_master(struct dlm_rsb *r, int error)
1890 struct dlm_lkb *lkb;
1892 if (!r->res_first_lkid)
1893 return;
1895 switch (error) {
1896 case 0:
1897 case -EINPROGRESS:
1898 r->res_first_lkid = 0;
1899 process_lookup_list(r);
1900 break;
1902 case -EAGAIN:
1903 /* the remote master didn't queue our NOQUEUE request;
1904 make a waiting lkb the first_lkid */
1906 r->res_first_lkid = 0;
1908 if (!list_empty(&r->res_lookup)) {
1909 lkb = list_entry(r->res_lookup.next, struct dlm_lkb,
1910 lkb_rsb_lookup);
1911 list_del_init(&lkb->lkb_rsb_lookup);
1912 r->res_first_lkid = lkb->lkb_id;
1913 _request_lock(r, lkb);
1914 } else
1915 r->res_nodeid = -1;
1916 break;
1918 default:
1919 log_error(r->res_ls, "confirm_master unknown error %d", error);
1923 static int set_lock_args(int mode, struct dlm_lksb *lksb, uint32_t flags,
1924 int namelen, unsigned long timeout_cs, void *ast,
1925 void *astarg, void *bast, struct dlm_args *args)
1927 int rv = -EINVAL;
1929 /* check for invalid arg usage */
1931 if (mode < 0 || mode > DLM_LOCK_EX)
1932 goto out;
1934 if (!(flags & DLM_LKF_CONVERT) && (namelen > DLM_RESNAME_MAXLEN))
1935 goto out;
1937 if (flags & DLM_LKF_CANCEL)
1938 goto out;
1940 if (flags & DLM_LKF_QUECVT && !(flags & DLM_LKF_CONVERT))
1941 goto out;
1943 if (flags & DLM_LKF_CONVDEADLK && !(flags & DLM_LKF_CONVERT))
1944 goto out;
1946 if (flags & DLM_LKF_CONVDEADLK && flags & DLM_LKF_NOQUEUE)
1947 goto out;
1949 if (flags & DLM_LKF_EXPEDITE && flags & DLM_LKF_CONVERT)
1950 goto out;
1952 if (flags & DLM_LKF_EXPEDITE && flags & DLM_LKF_QUECVT)
1953 goto out;
1955 if (flags & DLM_LKF_EXPEDITE && flags & DLM_LKF_NOQUEUE)
1956 goto out;
1958 if (flags & DLM_LKF_EXPEDITE && mode != DLM_LOCK_NL)
1959 goto out;
1961 if (!ast || !lksb)
1962 goto out;
1964 if (flags & DLM_LKF_VALBLK && !lksb->sb_lvbptr)
1965 goto out;
1967 if (flags & DLM_LKF_CONVERT && !lksb->sb_lkid)
1968 goto out;
1970 /* these args will be copied to the lkb in validate_lock_args,
1971 it cannot be done now because when converting locks, fields in
1972 an active lkb cannot be modified before locking the rsb */
1974 args->flags = flags;
1975 args->astaddr = ast;
1976 args->astparam = (long) astarg;
1977 args->bastaddr = bast;
1978 args->timeout = timeout_cs;
1979 args->mode = mode;
1980 args->lksb = lksb;
1981 rv = 0;
1982 out:
1983 return rv;
1986 static int set_unlock_args(uint32_t flags, void *astarg, struct dlm_args *args)
1988 if (flags & ~(DLM_LKF_CANCEL | DLM_LKF_VALBLK | DLM_LKF_IVVALBLK |
1989 DLM_LKF_FORCEUNLOCK))
1990 return -EINVAL;
1992 if (flags & DLM_LKF_CANCEL && flags & DLM_LKF_FORCEUNLOCK)
1993 return -EINVAL;
1995 args->flags = flags;
1996 args->astparam = (long) astarg;
1997 return 0;
2000 static int validate_lock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
2001 struct dlm_args *args)
2003 int rv = -EINVAL;
2005 if (args->flags & DLM_LKF_CONVERT) {
2006 if (lkb->lkb_flags & DLM_IFL_MSTCPY)
2007 goto out;
2009 if (args->flags & DLM_LKF_QUECVT &&
2010 !__quecvt_compat_matrix[lkb->lkb_grmode+1][args->mode+1])
2011 goto out;
2013 rv = -EBUSY;
2014 if (lkb->lkb_status != DLM_LKSTS_GRANTED)
2015 goto out;
2017 if (lkb->lkb_wait_type)
2018 goto out;
2020 if (is_overlap(lkb))
2021 goto out;
2024 lkb->lkb_exflags = args->flags;
2025 lkb->lkb_sbflags = 0;
2026 lkb->lkb_astaddr = args->astaddr;
2027 lkb->lkb_astparam = args->astparam;
2028 lkb->lkb_bastaddr = args->bastaddr;
2029 lkb->lkb_rqmode = args->mode;
2030 lkb->lkb_lksb = args->lksb;
2031 lkb->lkb_lvbptr = args->lksb->sb_lvbptr;
2032 lkb->lkb_ownpid = (int) current->pid;
2033 lkb->lkb_timeout_cs = args->timeout;
2034 rv = 0;
2035 out:
2036 return rv;
2039 /* when dlm_unlock() sees -EBUSY with CANCEL/FORCEUNLOCK it returns 0
2040 for success */
2042 /* note: it's valid for lkb_nodeid/res_nodeid to be -1 when we get here
2043 because there may be a lookup in progress and it's valid to do
2044 cancel/unlockf on it */
2046 static int validate_unlock_args(struct dlm_lkb *lkb, struct dlm_args *args)
2048 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
2049 int rv = -EINVAL;
2051 if (lkb->lkb_flags & DLM_IFL_MSTCPY) {
2052 log_error(ls, "unlock on MSTCPY %x", lkb->lkb_id);
2053 dlm_print_lkb(lkb);
2054 goto out;
2057 /* an lkb may still exist even though the lock is EOL'ed due to a
2058 cancel, unlock or failed noqueue request; an app can't use these
2059 locks; return same error as if the lkid had not been found at all */
2061 if (lkb->lkb_flags & DLM_IFL_ENDOFLIFE) {
2062 log_debug(ls, "unlock on ENDOFLIFE %x", lkb->lkb_id);
2063 rv = -ENOENT;
2064 goto out;
2067 /* an lkb may be waiting for an rsb lookup to complete where the
2068 lookup was initiated by another lock */
2070 if (args->flags & (DLM_LKF_CANCEL | DLM_LKF_FORCEUNLOCK)) {
2071 if (!list_empty(&lkb->lkb_rsb_lookup)) {
2072 log_debug(ls, "unlock on rsb_lookup %x", lkb->lkb_id);
2073 list_del_init(&lkb->lkb_rsb_lookup);
2074 queue_cast(lkb->lkb_resource, lkb,
2075 args->flags & DLM_LKF_CANCEL ?
2076 -DLM_ECANCEL : -DLM_EUNLOCK);
2077 unhold_lkb(lkb); /* undoes create_lkb() */
2078 rv = -EBUSY;
2079 goto out;
2083 /* cancel not allowed with another cancel/unlock in progress */
2085 if (args->flags & DLM_LKF_CANCEL) {
2086 if (lkb->lkb_exflags & DLM_LKF_CANCEL)
2087 goto out;
2089 if (is_overlap(lkb))
2090 goto out;
2092 /* don't let scand try to do a cancel */
2093 del_timeout(lkb);
2095 if (lkb->lkb_flags & DLM_IFL_RESEND) {
2096 lkb->lkb_flags |= DLM_IFL_OVERLAP_CANCEL;
2097 rv = -EBUSY;
2098 goto out;
2101 switch (lkb->lkb_wait_type) {
2102 case DLM_MSG_LOOKUP:
2103 case DLM_MSG_REQUEST:
2104 lkb->lkb_flags |= DLM_IFL_OVERLAP_CANCEL;
2105 rv = -EBUSY;
2106 goto out;
2107 case DLM_MSG_UNLOCK:
2108 case DLM_MSG_CANCEL:
2109 goto out;
2111 /* add_to_waiters() will set OVERLAP_CANCEL */
2112 goto out_ok;
2115 /* do we need to allow a force-unlock if there's a normal unlock
2116 already in progress? in what conditions could the normal unlock
2117 fail such that we'd want to send a force-unlock to be sure? */
2119 if (args->flags & DLM_LKF_FORCEUNLOCK) {
2120 if (lkb->lkb_exflags & DLM_LKF_FORCEUNLOCK)
2121 goto out;
2123 if (is_overlap_unlock(lkb))
2124 goto out;
2126 /* don't let scand try to do a cancel */
2127 del_timeout(lkb);
2129 if (lkb->lkb_flags & DLM_IFL_RESEND) {
2130 lkb->lkb_flags |= DLM_IFL_OVERLAP_UNLOCK;
2131 rv = -EBUSY;
2132 goto out;
2135 switch (lkb->lkb_wait_type) {
2136 case DLM_MSG_LOOKUP:
2137 case DLM_MSG_REQUEST:
2138 lkb->lkb_flags |= DLM_IFL_OVERLAP_UNLOCK;
2139 rv = -EBUSY;
2140 goto out;
2141 case DLM_MSG_UNLOCK:
2142 goto out;
2144 /* add_to_waiters() will set OVERLAP_UNLOCK */
2145 goto out_ok;
2148 /* normal unlock not allowed if there's any op in progress */
2149 rv = -EBUSY;
2150 if (lkb->lkb_wait_type || lkb->lkb_wait_count)
2151 goto out;
2153 out_ok:
2154 /* an overlapping op shouldn't blow away exflags from other op */
2155 lkb->lkb_exflags |= args->flags;
2156 lkb->lkb_sbflags = 0;
2157 lkb->lkb_astparam = args->astparam;
2158 rv = 0;
2159 out:
2160 if (rv)
2161 log_debug(ls, "validate_unlock_args %d %x %x %x %x %d %s", rv,
2162 lkb->lkb_id, lkb->lkb_flags, lkb->lkb_exflags,
2163 args->flags, lkb->lkb_wait_type,
2164 lkb->lkb_resource->res_name);
2165 return rv;
2169 * Four stage 4 varieties:
2170 * do_request(), do_convert(), do_unlock(), do_cancel()
2171 * These are called on the master node for the given lock and
2172 * from the central locking logic.
2175 static int do_request(struct dlm_rsb *r, struct dlm_lkb *lkb)
2177 int error = 0;
2179 if (can_be_granted(r, lkb, 1, NULL)) {
2180 grant_lock(r, lkb);
2181 queue_cast(r, lkb, 0);
2182 goto out;
2185 if (can_be_queued(lkb)) {
2186 error = -EINPROGRESS;
2187 add_lkb(r, lkb, DLM_LKSTS_WAITING);
2188 send_blocking_asts(r, lkb);
2189 add_timeout(lkb);
2190 goto out;
2193 error = -EAGAIN;
2194 if (force_blocking_asts(lkb))
2195 send_blocking_asts_all(r, lkb);
2196 queue_cast(r, lkb, -EAGAIN);
2198 out:
2199 return error;
2202 static int do_convert(struct dlm_rsb *r, struct dlm_lkb *lkb)
2204 int error = 0;
2205 int deadlk = 0;
2207 /* changing an existing lock may allow others to be granted */
2209 if (can_be_granted(r, lkb, 1, &deadlk)) {
2210 grant_lock(r, lkb);
2211 queue_cast(r, lkb, 0);
2212 grant_pending_locks(r);
2213 goto out;
2216 /* can_be_granted() detected that this lock would block in a conversion
2217 deadlock, so we leave it on the granted queue and return EDEADLK in
2218 the ast for the convert. */
2220 if (deadlk) {
2221 /* it's left on the granted queue */
2222 log_debug(r->res_ls, "deadlock %x node %d sts%d g%d r%d %s",
2223 lkb->lkb_id, lkb->lkb_nodeid, lkb->lkb_status,
2224 lkb->lkb_grmode, lkb->lkb_rqmode, r->res_name);
2225 revert_lock(r, lkb);
2226 queue_cast(r, lkb, -EDEADLK);
2227 error = -EDEADLK;
2228 goto out;
2231 /* is_demoted() means the can_be_granted() above set the grmode
2232 to NL, and left us on the granted queue. This auto-demotion
2233 (due to CONVDEADLK) might mean other locks, and/or this lock, are
2234 now grantable. We have to try to grant other converting locks
2235 before we try again to grant this one. */
2237 if (is_demoted(lkb)) {
2238 grant_pending_convert(r, DLM_LOCK_IV);
2239 if (_can_be_granted(r, lkb, 1)) {
2240 grant_lock(r, lkb);
2241 queue_cast(r, lkb, 0);
2242 grant_pending_locks(r);
2243 goto out;
2245 /* else fall through and move to convert queue */
2248 if (can_be_queued(lkb)) {
2249 error = -EINPROGRESS;
2250 del_lkb(r, lkb);
2251 add_lkb(r, lkb, DLM_LKSTS_CONVERT);
2252 send_blocking_asts(r, lkb);
2253 add_timeout(lkb);
2254 goto out;
2257 error = -EAGAIN;
2258 if (force_blocking_asts(lkb))
2259 send_blocking_asts_all(r, lkb);
2260 queue_cast(r, lkb, -EAGAIN);
2262 out:
2263 return error;
2266 static int do_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2268 remove_lock(r, lkb);
2269 queue_cast(r, lkb, -DLM_EUNLOCK);
2270 grant_pending_locks(r);
2271 return -DLM_EUNLOCK;
2274 /* returns: 0 did nothing, -DLM_ECANCEL canceled lock */
2276 static int do_cancel(struct dlm_rsb *r, struct dlm_lkb *lkb)
2278 int error;
2280 error = revert_lock(r, lkb);
2281 if (error) {
2282 queue_cast(r, lkb, -DLM_ECANCEL);
2283 grant_pending_locks(r);
2284 return -DLM_ECANCEL;
2286 return 0;
2290 * Four stage 3 varieties:
2291 * _request_lock(), _convert_lock(), _unlock_lock(), _cancel_lock()
2294 /* add a new lkb to a possibly new rsb, called by requesting process */
2296 static int _request_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2298 int error;
2300 /* set_master: sets lkb nodeid from r */
2302 error = set_master(r, lkb);
2303 if (error < 0)
2304 goto out;
2305 if (error) {
2306 error = 0;
2307 goto out;
2310 if (is_remote(r))
2311 /* receive_request() calls do_request() on remote node */
2312 error = send_request(r, lkb);
2313 else
2314 error = do_request(r, lkb);
2315 out:
2316 return error;
2319 /* change some property of an existing lkb, e.g. mode */
2321 static int _convert_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2323 int error;
2325 if (is_remote(r))
2326 /* receive_convert() calls do_convert() on remote node */
2327 error = send_convert(r, lkb);
2328 else
2329 error = do_convert(r, lkb);
2331 return error;
2334 /* remove an existing lkb from the granted queue */
2336 static int _unlock_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2338 int error;
2340 if (is_remote(r))
2341 /* receive_unlock() calls do_unlock() on remote node */
2342 error = send_unlock(r, lkb);
2343 else
2344 error = do_unlock(r, lkb);
2346 return error;
2349 /* remove an existing lkb from the convert or wait queue */
2351 static int _cancel_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2353 int error;
2355 if (is_remote(r))
2356 /* receive_cancel() calls do_cancel() on remote node */
2357 error = send_cancel(r, lkb);
2358 else
2359 error = do_cancel(r, lkb);
2361 return error;
2365 * Four stage 2 varieties:
2366 * request_lock(), convert_lock(), unlock_lock(), cancel_lock()
2369 static int request_lock(struct dlm_ls *ls, struct dlm_lkb *lkb, char *name,
2370 int len, struct dlm_args *args)
2372 struct dlm_rsb *r;
2373 int error;
2375 error = validate_lock_args(ls, lkb, args);
2376 if (error)
2377 goto out;
2379 error = find_rsb(ls, name, len, R_CREATE, &r);
2380 if (error)
2381 goto out;
2383 lock_rsb(r);
2385 attach_lkb(r, lkb);
2386 lkb->lkb_lksb->sb_lkid = lkb->lkb_id;
2388 error = _request_lock(r, lkb);
2390 unlock_rsb(r);
2391 put_rsb(r);
2393 out:
2394 return error;
2397 static int convert_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
2398 struct dlm_args *args)
2400 struct dlm_rsb *r;
2401 int error;
2403 r = lkb->lkb_resource;
2405 hold_rsb(r);
2406 lock_rsb(r);
2408 error = validate_lock_args(ls, lkb, args);
2409 if (error)
2410 goto out;
2412 error = _convert_lock(r, lkb);
2413 out:
2414 unlock_rsb(r);
2415 put_rsb(r);
2416 return error;
2419 static int unlock_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
2420 struct dlm_args *args)
2422 struct dlm_rsb *r;
2423 int error;
2425 r = lkb->lkb_resource;
2427 hold_rsb(r);
2428 lock_rsb(r);
2430 error = validate_unlock_args(lkb, args);
2431 if (error)
2432 goto out;
2434 error = _unlock_lock(r, lkb);
2435 out:
2436 unlock_rsb(r);
2437 put_rsb(r);
2438 return error;
2441 static int cancel_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
2442 struct dlm_args *args)
2444 struct dlm_rsb *r;
2445 int error;
2447 r = lkb->lkb_resource;
2449 hold_rsb(r);
2450 lock_rsb(r);
2452 error = validate_unlock_args(lkb, args);
2453 if (error)
2454 goto out;
2456 error = _cancel_lock(r, lkb);
2457 out:
2458 unlock_rsb(r);
2459 put_rsb(r);
2460 return error;
2464 * Two stage 1 varieties: dlm_lock() and dlm_unlock()
2467 int dlm_lock(dlm_lockspace_t *lockspace,
2468 int mode,
2469 struct dlm_lksb *lksb,
2470 uint32_t flags,
2471 void *name,
2472 unsigned int namelen,
2473 uint32_t parent_lkid,
2474 void (*ast) (void *astarg),
2475 void *astarg,
2476 void (*bast) (void *astarg, int mode))
2478 struct dlm_ls *ls;
2479 struct dlm_lkb *lkb;
2480 struct dlm_args args;
2481 int error, convert = flags & DLM_LKF_CONVERT;
2483 ls = dlm_find_lockspace_local(lockspace);
2484 if (!ls)
2485 return -EINVAL;
2487 dlm_lock_recovery(ls);
2489 if (convert)
2490 error = find_lkb(ls, lksb->sb_lkid, &lkb);
2491 else
2492 error = create_lkb(ls, &lkb);
2494 if (error)
2495 goto out;
2497 error = set_lock_args(mode, lksb, flags, namelen, 0, ast,
2498 astarg, bast, &args);
2499 if (error)
2500 goto out_put;
2502 if (convert)
2503 error = convert_lock(ls, lkb, &args);
2504 else
2505 error = request_lock(ls, lkb, name, namelen, &args);
2507 if (error == -EINPROGRESS)
2508 error = 0;
2509 out_put:
2510 if (convert || error)
2511 __put_lkb(ls, lkb);
2512 if (error == -EAGAIN || error == -EDEADLK)
2513 error = 0;
2514 out:
2515 dlm_unlock_recovery(ls);
2516 dlm_put_lockspace(ls);
2517 return error;
2520 int dlm_unlock(dlm_lockspace_t *lockspace,
2521 uint32_t lkid,
2522 uint32_t flags,
2523 struct dlm_lksb *lksb,
2524 void *astarg)
2526 struct dlm_ls *ls;
2527 struct dlm_lkb *lkb;
2528 struct dlm_args args;
2529 int error;
2531 ls = dlm_find_lockspace_local(lockspace);
2532 if (!ls)
2533 return -EINVAL;
2535 dlm_lock_recovery(ls);
2537 error = find_lkb(ls, lkid, &lkb);
2538 if (error)
2539 goto out;
2541 error = set_unlock_args(flags, astarg, &args);
2542 if (error)
2543 goto out_put;
2545 if (flags & DLM_LKF_CANCEL)
2546 error = cancel_lock(ls, lkb, &args);
2547 else
2548 error = unlock_lock(ls, lkb, &args);
2550 if (error == -DLM_EUNLOCK || error == -DLM_ECANCEL)
2551 error = 0;
2552 if (error == -EBUSY && (flags & (DLM_LKF_CANCEL | DLM_LKF_FORCEUNLOCK)))
2553 error = 0;
2554 out_put:
2555 dlm_put_lkb(lkb);
2556 out:
2557 dlm_unlock_recovery(ls);
2558 dlm_put_lockspace(ls);
2559 return error;
2563 * send/receive routines for remote operations and replies
2565 * send_args
2566 * send_common
2567 * send_request receive_request
2568 * send_convert receive_convert
2569 * send_unlock receive_unlock
2570 * send_cancel receive_cancel
2571 * send_grant receive_grant
2572 * send_bast receive_bast
2573 * send_lookup receive_lookup
2574 * send_remove receive_remove
2576 * send_common_reply
2577 * receive_request_reply send_request_reply
2578 * receive_convert_reply send_convert_reply
2579 * receive_unlock_reply send_unlock_reply
2580 * receive_cancel_reply send_cancel_reply
2581 * receive_lookup_reply send_lookup_reply
2584 static int _create_message(struct dlm_ls *ls, int mb_len,
2585 int to_nodeid, int mstype,
2586 struct dlm_message **ms_ret,
2587 struct dlm_mhandle **mh_ret)
2589 struct dlm_message *ms;
2590 struct dlm_mhandle *mh;
2591 char *mb;
2593 /* get_buffer gives us a message handle (mh) that we need to
2594 pass into lowcomms_commit and a message buffer (mb) that we
2595 write our data into */
2597 mh = dlm_lowcomms_get_buffer(to_nodeid, mb_len, ls->ls_allocation, &mb);
2598 if (!mh)
2599 return -ENOBUFS;
2601 memset(mb, 0, mb_len);
2603 ms = (struct dlm_message *) mb;
2605 ms->m_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
2606 ms->m_header.h_lockspace = ls->ls_global_id;
2607 ms->m_header.h_nodeid = dlm_our_nodeid();
2608 ms->m_header.h_length = mb_len;
2609 ms->m_header.h_cmd = DLM_MSG;
2611 ms->m_type = mstype;
2613 *mh_ret = mh;
2614 *ms_ret = ms;
2615 return 0;
2618 static int create_message(struct dlm_rsb *r, struct dlm_lkb *lkb,
2619 int to_nodeid, int mstype,
2620 struct dlm_message **ms_ret,
2621 struct dlm_mhandle **mh_ret)
2623 int mb_len = sizeof(struct dlm_message);
2625 switch (mstype) {
2626 case DLM_MSG_REQUEST:
2627 case DLM_MSG_LOOKUP:
2628 case DLM_MSG_REMOVE:
2629 mb_len += r->res_length;
2630 break;
2631 case DLM_MSG_CONVERT:
2632 case DLM_MSG_UNLOCK:
2633 case DLM_MSG_REQUEST_REPLY:
2634 case DLM_MSG_CONVERT_REPLY:
2635 case DLM_MSG_GRANT:
2636 if (lkb && lkb->lkb_lvbptr)
2637 mb_len += r->res_ls->ls_lvblen;
2638 break;
2641 return _create_message(r->res_ls, mb_len, to_nodeid, mstype,
2642 ms_ret, mh_ret);
2645 /* further lowcomms enhancements or alternate implementations may make
2646 the return value from this function useful at some point */
2648 static int send_message(struct dlm_mhandle *mh, struct dlm_message *ms)
2650 dlm_message_out(ms);
2651 dlm_lowcomms_commit_buffer(mh);
2652 return 0;
2655 static void send_args(struct dlm_rsb *r, struct dlm_lkb *lkb,
2656 struct dlm_message *ms)
2658 ms->m_nodeid = lkb->lkb_nodeid;
2659 ms->m_pid = lkb->lkb_ownpid;
2660 ms->m_lkid = lkb->lkb_id;
2661 ms->m_remid = lkb->lkb_remid;
2662 ms->m_exflags = lkb->lkb_exflags;
2663 ms->m_sbflags = lkb->lkb_sbflags;
2664 ms->m_flags = lkb->lkb_flags;
2665 ms->m_lvbseq = lkb->lkb_lvbseq;
2666 ms->m_status = lkb->lkb_status;
2667 ms->m_grmode = lkb->lkb_grmode;
2668 ms->m_rqmode = lkb->lkb_rqmode;
2669 ms->m_hash = r->res_hash;
2671 /* m_result and m_bastmode are set from function args,
2672 not from lkb fields */
2674 if (lkb->lkb_bastaddr)
2675 ms->m_asts |= AST_BAST;
2676 if (lkb->lkb_astaddr)
2677 ms->m_asts |= AST_COMP;
2679 /* compare with switch in create_message; send_remove() doesn't
2680 use send_args() */
2682 switch (ms->m_type) {
2683 case DLM_MSG_REQUEST:
2684 case DLM_MSG_LOOKUP:
2685 memcpy(ms->m_extra, r->res_name, r->res_length);
2686 break;
2687 case DLM_MSG_CONVERT:
2688 case DLM_MSG_UNLOCK:
2689 case DLM_MSG_REQUEST_REPLY:
2690 case DLM_MSG_CONVERT_REPLY:
2691 case DLM_MSG_GRANT:
2692 if (!lkb->lkb_lvbptr)
2693 break;
2694 memcpy(ms->m_extra, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
2695 break;
2699 static int send_common(struct dlm_rsb *r, struct dlm_lkb *lkb, int mstype)
2701 struct dlm_message *ms;
2702 struct dlm_mhandle *mh;
2703 int to_nodeid, error;
2705 error = add_to_waiters(lkb, mstype);
2706 if (error)
2707 return error;
2709 to_nodeid = r->res_nodeid;
2711 error = create_message(r, lkb, to_nodeid, mstype, &ms, &mh);
2712 if (error)
2713 goto fail;
2715 send_args(r, lkb, ms);
2717 error = send_message(mh, ms);
2718 if (error)
2719 goto fail;
2720 return 0;
2722 fail:
2723 remove_from_waiters(lkb, msg_reply_type(mstype));
2724 return error;
2727 static int send_request(struct dlm_rsb *r, struct dlm_lkb *lkb)
2729 return send_common(r, lkb, DLM_MSG_REQUEST);
2732 static int send_convert(struct dlm_rsb *r, struct dlm_lkb *lkb)
2734 int error;
2736 error = send_common(r, lkb, DLM_MSG_CONVERT);
2738 /* down conversions go without a reply from the master */
2739 if (!error && down_conversion(lkb)) {
2740 remove_from_waiters(lkb, DLM_MSG_CONVERT_REPLY);
2741 r->res_ls->ls_stub_ms.m_type = DLM_MSG_CONVERT_REPLY;
2742 r->res_ls->ls_stub_ms.m_result = 0;
2743 r->res_ls->ls_stub_ms.m_flags = lkb->lkb_flags;
2744 __receive_convert_reply(r, lkb, &r->res_ls->ls_stub_ms);
2747 return error;
2750 /* FIXME: if this lkb is the only lock we hold on the rsb, then set
2751 MASTER_UNCERTAIN to force the next request on the rsb to confirm
2752 that the master is still correct. */
2754 static int send_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2756 return send_common(r, lkb, DLM_MSG_UNLOCK);
2759 static int send_cancel(struct dlm_rsb *r, struct dlm_lkb *lkb)
2761 return send_common(r, lkb, DLM_MSG_CANCEL);
2764 static int send_grant(struct dlm_rsb *r, struct dlm_lkb *lkb)
2766 struct dlm_message *ms;
2767 struct dlm_mhandle *mh;
2768 int to_nodeid, error;
2770 to_nodeid = lkb->lkb_nodeid;
2772 error = create_message(r, lkb, to_nodeid, DLM_MSG_GRANT, &ms, &mh);
2773 if (error)
2774 goto out;
2776 send_args(r, lkb, ms);
2778 ms->m_result = 0;
2780 error = send_message(mh, ms);
2781 out:
2782 return error;
2785 static int send_bast(struct dlm_rsb *r, struct dlm_lkb *lkb, int mode)
2787 struct dlm_message *ms;
2788 struct dlm_mhandle *mh;
2789 int to_nodeid, error;
2791 to_nodeid = lkb->lkb_nodeid;
2793 error = create_message(r, NULL, to_nodeid, DLM_MSG_BAST, &ms, &mh);
2794 if (error)
2795 goto out;
2797 send_args(r, lkb, ms);
2799 ms->m_bastmode = mode;
2801 error = send_message(mh, ms);
2802 out:
2803 return error;
2806 static int send_lookup(struct dlm_rsb *r, struct dlm_lkb *lkb)
2808 struct dlm_message *ms;
2809 struct dlm_mhandle *mh;
2810 int to_nodeid, error;
2812 error = add_to_waiters(lkb, DLM_MSG_LOOKUP);
2813 if (error)
2814 return error;
2816 to_nodeid = dlm_dir_nodeid(r);
2818 error = create_message(r, NULL, to_nodeid, DLM_MSG_LOOKUP, &ms, &mh);
2819 if (error)
2820 goto fail;
2822 send_args(r, lkb, ms);
2824 error = send_message(mh, ms);
2825 if (error)
2826 goto fail;
2827 return 0;
2829 fail:
2830 remove_from_waiters(lkb, DLM_MSG_LOOKUP_REPLY);
2831 return error;
2834 static int send_remove(struct dlm_rsb *r)
2836 struct dlm_message *ms;
2837 struct dlm_mhandle *mh;
2838 int to_nodeid, error;
2840 to_nodeid = dlm_dir_nodeid(r);
2842 error = create_message(r, NULL, to_nodeid, DLM_MSG_REMOVE, &ms, &mh);
2843 if (error)
2844 goto out;
2846 memcpy(ms->m_extra, r->res_name, r->res_length);
2847 ms->m_hash = r->res_hash;
2849 error = send_message(mh, ms);
2850 out:
2851 return error;
2854 static int send_common_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
2855 int mstype, int rv)
2857 struct dlm_message *ms;
2858 struct dlm_mhandle *mh;
2859 int to_nodeid, error;
2861 to_nodeid = lkb->lkb_nodeid;
2863 error = create_message(r, lkb, to_nodeid, mstype, &ms, &mh);
2864 if (error)
2865 goto out;
2867 send_args(r, lkb, ms);
2869 ms->m_result = rv;
2871 error = send_message(mh, ms);
2872 out:
2873 return error;
2876 static int send_request_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
2878 return send_common_reply(r, lkb, DLM_MSG_REQUEST_REPLY, rv);
2881 static int send_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
2883 return send_common_reply(r, lkb, DLM_MSG_CONVERT_REPLY, rv);
2886 static int send_unlock_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
2888 return send_common_reply(r, lkb, DLM_MSG_UNLOCK_REPLY, rv);
2891 static int send_cancel_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
2893 return send_common_reply(r, lkb, DLM_MSG_CANCEL_REPLY, rv);
2896 static int send_lookup_reply(struct dlm_ls *ls, struct dlm_message *ms_in,
2897 int ret_nodeid, int rv)
2899 struct dlm_rsb *r = &ls->ls_stub_rsb;
2900 struct dlm_message *ms;
2901 struct dlm_mhandle *mh;
2902 int error, nodeid = ms_in->m_header.h_nodeid;
2904 error = create_message(r, NULL, nodeid, DLM_MSG_LOOKUP_REPLY, &ms, &mh);
2905 if (error)
2906 goto out;
2908 ms->m_lkid = ms_in->m_lkid;
2909 ms->m_result = rv;
2910 ms->m_nodeid = ret_nodeid;
2912 error = send_message(mh, ms);
2913 out:
2914 return error;
2917 /* which args we save from a received message depends heavily on the type
2918 of message, unlike the send side where we can safely send everything about
2919 the lkb for any type of message */
2921 static void receive_flags(struct dlm_lkb *lkb, struct dlm_message *ms)
2923 lkb->lkb_exflags = ms->m_exflags;
2924 lkb->lkb_sbflags = ms->m_sbflags;
2925 lkb->lkb_flags = (lkb->lkb_flags & 0xFFFF0000) |
2926 (ms->m_flags & 0x0000FFFF);
2929 static void receive_flags_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
2931 lkb->lkb_sbflags = ms->m_sbflags;
2932 lkb->lkb_flags = (lkb->lkb_flags & 0xFFFF0000) |
2933 (ms->m_flags & 0x0000FFFF);
2936 static int receive_extralen(struct dlm_message *ms)
2938 return (ms->m_header.h_length - sizeof(struct dlm_message));
2941 static int receive_lvb(struct dlm_ls *ls, struct dlm_lkb *lkb,
2942 struct dlm_message *ms)
2944 int len;
2946 if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
2947 if (!lkb->lkb_lvbptr)
2948 lkb->lkb_lvbptr = allocate_lvb(ls);
2949 if (!lkb->lkb_lvbptr)
2950 return -ENOMEM;
2951 len = receive_extralen(ms);
2952 memcpy(lkb->lkb_lvbptr, ms->m_extra, len);
2954 return 0;
2957 static int receive_request_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
2958 struct dlm_message *ms)
2960 lkb->lkb_nodeid = ms->m_header.h_nodeid;
2961 lkb->lkb_ownpid = ms->m_pid;
2962 lkb->lkb_remid = ms->m_lkid;
2963 lkb->lkb_grmode = DLM_LOCK_IV;
2964 lkb->lkb_rqmode = ms->m_rqmode;
2965 lkb->lkb_bastaddr = (void *) (long) (ms->m_asts & AST_BAST);
2966 lkb->lkb_astaddr = (void *) (long) (ms->m_asts & AST_COMP);
2968 DLM_ASSERT(is_master_copy(lkb), dlm_print_lkb(lkb););
2970 if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
2971 /* lkb was just created so there won't be an lvb yet */
2972 lkb->lkb_lvbptr = allocate_lvb(ls);
2973 if (!lkb->lkb_lvbptr)
2974 return -ENOMEM;
2977 return 0;
2980 static int receive_convert_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
2981 struct dlm_message *ms)
2983 if (lkb->lkb_nodeid != ms->m_header.h_nodeid) {
2984 log_error(ls, "convert_args nodeid %d %d lkid %x %x",
2985 lkb->lkb_nodeid, ms->m_header.h_nodeid,
2986 lkb->lkb_id, lkb->lkb_remid);
2987 return -EINVAL;
2990 if (!is_master_copy(lkb))
2991 return -EINVAL;
2993 if (lkb->lkb_status != DLM_LKSTS_GRANTED)
2994 return -EBUSY;
2996 if (receive_lvb(ls, lkb, ms))
2997 return -ENOMEM;
2999 lkb->lkb_rqmode = ms->m_rqmode;
3000 lkb->lkb_lvbseq = ms->m_lvbseq;
3002 return 0;
3005 static int receive_unlock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
3006 struct dlm_message *ms)
3008 if (!is_master_copy(lkb))
3009 return -EINVAL;
3010 if (receive_lvb(ls, lkb, ms))
3011 return -ENOMEM;
3012 return 0;
3015 /* We fill in the stub-lkb fields with the info that send_xxxx_reply()
3016 uses to send a reply and that the remote end uses to process the reply. */
3018 static void setup_stub_lkb(struct dlm_ls *ls, struct dlm_message *ms)
3020 struct dlm_lkb *lkb = &ls->ls_stub_lkb;
3021 lkb->lkb_nodeid = ms->m_header.h_nodeid;
3022 lkb->lkb_remid = ms->m_lkid;
3025 static void receive_request(struct dlm_ls *ls, struct dlm_message *ms)
3027 struct dlm_lkb *lkb;
3028 struct dlm_rsb *r;
3029 int error, namelen;
3031 error = create_lkb(ls, &lkb);
3032 if (error)
3033 goto fail;
3035 receive_flags(lkb, ms);
3036 lkb->lkb_flags |= DLM_IFL_MSTCPY;
3037 error = receive_request_args(ls, lkb, ms);
3038 if (error) {
3039 __put_lkb(ls, lkb);
3040 goto fail;
3043 namelen = receive_extralen(ms);
3045 error = find_rsb(ls, ms->m_extra, namelen, R_MASTER, &r);
3046 if (error) {
3047 __put_lkb(ls, lkb);
3048 goto fail;
3051 lock_rsb(r);
3053 attach_lkb(r, lkb);
3054 error = do_request(r, lkb);
3055 send_request_reply(r, lkb, error);
3057 unlock_rsb(r);
3058 put_rsb(r);
3060 if (error == -EINPROGRESS)
3061 error = 0;
3062 if (error)
3063 dlm_put_lkb(lkb);
3064 return;
3066 fail:
3067 setup_stub_lkb(ls, ms);
3068 send_request_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
3071 static void receive_convert(struct dlm_ls *ls, struct dlm_message *ms)
3073 struct dlm_lkb *lkb;
3074 struct dlm_rsb *r;
3075 int error, reply = 1;
3077 error = find_lkb(ls, ms->m_remid, &lkb);
3078 if (error)
3079 goto fail;
3081 r = lkb->lkb_resource;
3083 hold_rsb(r);
3084 lock_rsb(r);
3086 receive_flags(lkb, ms);
3087 error = receive_convert_args(ls, lkb, ms);
3088 if (error)
3089 goto out;
3090 reply = !down_conversion(lkb);
3092 error = do_convert(r, lkb);
3093 out:
3094 if (reply)
3095 send_convert_reply(r, lkb, error);
3097 unlock_rsb(r);
3098 put_rsb(r);
3099 dlm_put_lkb(lkb);
3100 return;
3102 fail:
3103 setup_stub_lkb(ls, ms);
3104 send_convert_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
3107 static void receive_unlock(struct dlm_ls *ls, struct dlm_message *ms)
3109 struct dlm_lkb *lkb;
3110 struct dlm_rsb *r;
3111 int error;
3113 error = find_lkb(ls, ms->m_remid, &lkb);
3114 if (error)
3115 goto fail;
3117 r = lkb->lkb_resource;
3119 hold_rsb(r);
3120 lock_rsb(r);
3122 receive_flags(lkb, ms);
3123 error = receive_unlock_args(ls, lkb, ms);
3124 if (error)
3125 goto out;
3127 error = do_unlock(r, lkb);
3128 out:
3129 send_unlock_reply(r, lkb, error);
3131 unlock_rsb(r);
3132 put_rsb(r);
3133 dlm_put_lkb(lkb);
3134 return;
3136 fail:
3137 setup_stub_lkb(ls, ms);
3138 send_unlock_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
3141 static void receive_cancel(struct dlm_ls *ls, struct dlm_message *ms)
3143 struct dlm_lkb *lkb;
3144 struct dlm_rsb *r;
3145 int error;
3147 error = find_lkb(ls, ms->m_remid, &lkb);
3148 if (error)
3149 goto fail;
3151 receive_flags(lkb, ms);
3153 r = lkb->lkb_resource;
3155 hold_rsb(r);
3156 lock_rsb(r);
3158 error = do_cancel(r, lkb);
3159 send_cancel_reply(r, lkb, error);
3161 unlock_rsb(r);
3162 put_rsb(r);
3163 dlm_put_lkb(lkb);
3164 return;
3166 fail:
3167 setup_stub_lkb(ls, ms);
3168 send_cancel_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
3171 static void receive_grant(struct dlm_ls *ls, struct dlm_message *ms)
3173 struct dlm_lkb *lkb;
3174 struct dlm_rsb *r;
3175 int error;
3177 error = find_lkb(ls, ms->m_remid, &lkb);
3178 if (error) {
3179 log_error(ls, "receive_grant no lkb");
3180 return;
3182 DLM_ASSERT(is_process_copy(lkb), dlm_print_lkb(lkb););
3184 r = lkb->lkb_resource;
3186 hold_rsb(r);
3187 lock_rsb(r);
3189 receive_flags_reply(lkb, ms);
3190 if (is_altmode(lkb))
3191 munge_altmode(lkb, ms);
3192 grant_lock_pc(r, lkb, ms);
3193 queue_cast(r, lkb, 0);
3195 unlock_rsb(r);
3196 put_rsb(r);
3197 dlm_put_lkb(lkb);
3200 static void receive_bast(struct dlm_ls *ls, struct dlm_message *ms)
3202 struct dlm_lkb *lkb;
3203 struct dlm_rsb *r;
3204 int error;
3206 error = find_lkb(ls, ms->m_remid, &lkb);
3207 if (error) {
3208 log_error(ls, "receive_bast no lkb");
3209 return;
3211 DLM_ASSERT(is_process_copy(lkb), dlm_print_lkb(lkb););
3213 r = lkb->lkb_resource;
3215 hold_rsb(r);
3216 lock_rsb(r);
3218 queue_bast(r, lkb, ms->m_bastmode);
3220 unlock_rsb(r);
3221 put_rsb(r);
3222 dlm_put_lkb(lkb);
3225 static void receive_lookup(struct dlm_ls *ls, struct dlm_message *ms)
3227 int len, error, ret_nodeid, dir_nodeid, from_nodeid, our_nodeid;
3229 from_nodeid = ms->m_header.h_nodeid;
3230 our_nodeid = dlm_our_nodeid();
3232 len = receive_extralen(ms);
3234 dir_nodeid = dlm_hash2nodeid(ls, ms->m_hash);
3235 if (dir_nodeid != our_nodeid) {
3236 log_error(ls, "lookup dir_nodeid %d from %d",
3237 dir_nodeid, from_nodeid);
3238 error = -EINVAL;
3239 ret_nodeid = -1;
3240 goto out;
3243 error = dlm_dir_lookup(ls, from_nodeid, ms->m_extra, len, &ret_nodeid);
3245 /* Optimization: we're master so treat lookup as a request */
3246 if (!error && ret_nodeid == our_nodeid) {
3247 receive_request(ls, ms);
3248 return;
3250 out:
3251 send_lookup_reply(ls, ms, ret_nodeid, error);
3254 static void receive_remove(struct dlm_ls *ls, struct dlm_message *ms)
3256 int len, dir_nodeid, from_nodeid;
3258 from_nodeid = ms->m_header.h_nodeid;
3260 len = receive_extralen(ms);
3262 dir_nodeid = dlm_hash2nodeid(ls, ms->m_hash);
3263 if (dir_nodeid != dlm_our_nodeid()) {
3264 log_error(ls, "remove dir entry dir_nodeid %d from %d",
3265 dir_nodeid, from_nodeid);
3266 return;
3269 dlm_dir_remove_entry(ls, from_nodeid, ms->m_extra, len);
3272 static void receive_purge(struct dlm_ls *ls, struct dlm_message *ms)
3274 do_purge(ls, ms->m_nodeid, ms->m_pid);
3277 static void receive_request_reply(struct dlm_ls *ls, struct dlm_message *ms)
3279 struct dlm_lkb *lkb;
3280 struct dlm_rsb *r;
3281 int error, mstype, result;
3283 error = find_lkb(ls, ms->m_remid, &lkb);
3284 if (error) {
3285 log_error(ls, "receive_request_reply no lkb");
3286 return;
3288 DLM_ASSERT(is_process_copy(lkb), dlm_print_lkb(lkb););
3290 r = lkb->lkb_resource;
3291 hold_rsb(r);
3292 lock_rsb(r);
3294 mstype = lkb->lkb_wait_type;
3295 error = remove_from_waiters(lkb, DLM_MSG_REQUEST_REPLY);
3296 if (error)
3297 goto out;
3299 /* Optimization: the dir node was also the master, so it took our
3300 lookup as a request and sent request reply instead of lookup reply */
3301 if (mstype == DLM_MSG_LOOKUP) {
3302 r->res_nodeid = ms->m_header.h_nodeid;
3303 lkb->lkb_nodeid = r->res_nodeid;
3306 /* this is the value returned from do_request() on the master */
3307 result = ms->m_result;
3309 switch (result) {
3310 case -EAGAIN:
3311 /* request would block (be queued) on remote master */
3312 queue_cast(r, lkb, -EAGAIN);
3313 confirm_master(r, -EAGAIN);
3314 unhold_lkb(lkb); /* undoes create_lkb() */
3315 break;
3317 case -EINPROGRESS:
3318 case 0:
3319 /* request was queued or granted on remote master */
3320 receive_flags_reply(lkb, ms);
3321 lkb->lkb_remid = ms->m_lkid;
3322 if (is_altmode(lkb))
3323 munge_altmode(lkb, ms);
3324 if (result) {
3325 add_lkb(r, lkb, DLM_LKSTS_WAITING);
3326 add_timeout(lkb);
3327 } else {
3328 grant_lock_pc(r, lkb, ms);
3329 queue_cast(r, lkb, 0);
3331 confirm_master(r, result);
3332 break;
3334 case -EBADR:
3335 case -ENOTBLK:
3336 /* find_rsb failed to find rsb or rsb wasn't master */
3337 log_debug(ls, "receive_request_reply %x %x master diff %d %d",
3338 lkb->lkb_id, lkb->lkb_flags, r->res_nodeid, result);
3339 r->res_nodeid = -1;
3340 lkb->lkb_nodeid = -1;
3342 if (is_overlap(lkb)) {
3343 /* we'll ignore error in cancel/unlock reply */
3344 queue_cast_overlap(r, lkb);
3345 unhold_lkb(lkb); /* undoes create_lkb() */
3346 } else
3347 _request_lock(r, lkb);
3348 break;
3350 default:
3351 log_error(ls, "receive_request_reply %x error %d",
3352 lkb->lkb_id, result);
3355 if (is_overlap_unlock(lkb) && (result == 0 || result == -EINPROGRESS)) {
3356 log_debug(ls, "receive_request_reply %x result %d unlock",
3357 lkb->lkb_id, result);
3358 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
3359 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
3360 send_unlock(r, lkb);
3361 } else if (is_overlap_cancel(lkb) && (result == -EINPROGRESS)) {
3362 log_debug(ls, "receive_request_reply %x cancel", lkb->lkb_id);
3363 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
3364 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
3365 send_cancel(r, lkb);
3366 } else {
3367 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
3368 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
3370 out:
3371 unlock_rsb(r);
3372 put_rsb(r);
3373 dlm_put_lkb(lkb);
3376 static void __receive_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
3377 struct dlm_message *ms)
3379 /* this is the value returned from do_convert() on the master */
3380 switch (ms->m_result) {
3381 case -EAGAIN:
3382 /* convert would block (be queued) on remote master */
3383 queue_cast(r, lkb, -EAGAIN);
3384 break;
3386 case -EDEADLK:
3387 receive_flags_reply(lkb, ms);
3388 revert_lock_pc(r, lkb);
3389 queue_cast(r, lkb, -EDEADLK);
3390 break;
3392 case -EINPROGRESS:
3393 /* convert was queued on remote master */
3394 receive_flags_reply(lkb, ms);
3395 if (is_demoted(lkb))
3396 munge_demoted(lkb, ms);
3397 del_lkb(r, lkb);
3398 add_lkb(r, lkb, DLM_LKSTS_CONVERT);
3399 add_timeout(lkb);
3400 break;
3402 case 0:
3403 /* convert was granted on remote master */
3404 receive_flags_reply(lkb, ms);
3405 if (is_demoted(lkb))
3406 munge_demoted(lkb, ms);
3407 grant_lock_pc(r, lkb, ms);
3408 queue_cast(r, lkb, 0);
3409 break;
3411 default:
3412 log_error(r->res_ls, "receive_convert_reply %x error %d",
3413 lkb->lkb_id, ms->m_result);
3417 static void _receive_convert_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
3419 struct dlm_rsb *r = lkb->lkb_resource;
3420 int error;
3422 hold_rsb(r);
3423 lock_rsb(r);
3425 /* stub reply can happen with waiters_mutex held */
3426 error = remove_from_waiters_ms(lkb, ms);
3427 if (error)
3428 goto out;
3430 __receive_convert_reply(r, lkb, ms);
3431 out:
3432 unlock_rsb(r);
3433 put_rsb(r);
3436 static void receive_convert_reply(struct dlm_ls *ls, struct dlm_message *ms)
3438 struct dlm_lkb *lkb;
3439 int error;
3441 error = find_lkb(ls, ms->m_remid, &lkb);
3442 if (error) {
3443 log_error(ls, "receive_convert_reply no lkb");
3444 return;
3446 DLM_ASSERT(is_process_copy(lkb), dlm_print_lkb(lkb););
3448 _receive_convert_reply(lkb, ms);
3449 dlm_put_lkb(lkb);
3452 static void _receive_unlock_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
3454 struct dlm_rsb *r = lkb->lkb_resource;
3455 int error;
3457 hold_rsb(r);
3458 lock_rsb(r);
3460 /* stub reply can happen with waiters_mutex held */
3461 error = remove_from_waiters_ms(lkb, ms);
3462 if (error)
3463 goto out;
3465 /* this is the value returned from do_unlock() on the master */
3467 switch (ms->m_result) {
3468 case -DLM_EUNLOCK:
3469 receive_flags_reply(lkb, ms);
3470 remove_lock_pc(r, lkb);
3471 queue_cast(r, lkb, -DLM_EUNLOCK);
3472 break;
3473 case -ENOENT:
3474 break;
3475 default:
3476 log_error(r->res_ls, "receive_unlock_reply %x error %d",
3477 lkb->lkb_id, ms->m_result);
3479 out:
3480 unlock_rsb(r);
3481 put_rsb(r);
3484 static void receive_unlock_reply(struct dlm_ls *ls, struct dlm_message *ms)
3486 struct dlm_lkb *lkb;
3487 int error;
3489 error = find_lkb(ls, ms->m_remid, &lkb);
3490 if (error) {
3491 log_error(ls, "receive_unlock_reply no lkb");
3492 return;
3494 DLM_ASSERT(is_process_copy(lkb), dlm_print_lkb(lkb););
3496 _receive_unlock_reply(lkb, ms);
3497 dlm_put_lkb(lkb);
3500 static void _receive_cancel_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
3502 struct dlm_rsb *r = lkb->lkb_resource;
3503 int error;
3505 hold_rsb(r);
3506 lock_rsb(r);
3508 /* stub reply can happen with waiters_mutex held */
3509 error = remove_from_waiters_ms(lkb, ms);
3510 if (error)
3511 goto out;
3513 /* this is the value returned from do_cancel() on the master */
3515 switch (ms->m_result) {
3516 case -DLM_ECANCEL:
3517 receive_flags_reply(lkb, ms);
3518 revert_lock_pc(r, lkb);
3519 queue_cast(r, lkb, -DLM_ECANCEL);
3520 break;
3521 case 0:
3522 break;
3523 default:
3524 log_error(r->res_ls, "receive_cancel_reply %x error %d",
3525 lkb->lkb_id, ms->m_result);
3527 out:
3528 unlock_rsb(r);
3529 put_rsb(r);
3532 static void receive_cancel_reply(struct dlm_ls *ls, struct dlm_message *ms)
3534 struct dlm_lkb *lkb;
3535 int error;
3537 error = find_lkb(ls, ms->m_remid, &lkb);
3538 if (error) {
3539 log_error(ls, "receive_cancel_reply no lkb");
3540 return;
3542 DLM_ASSERT(is_process_copy(lkb), dlm_print_lkb(lkb););
3544 _receive_cancel_reply(lkb, ms);
3545 dlm_put_lkb(lkb);
3548 static void receive_lookup_reply(struct dlm_ls *ls, struct dlm_message *ms)
3550 struct dlm_lkb *lkb;
3551 struct dlm_rsb *r;
3552 int error, ret_nodeid;
3554 error = find_lkb(ls, ms->m_lkid, &lkb);
3555 if (error) {
3556 log_error(ls, "receive_lookup_reply no lkb");
3557 return;
3560 /* ms->m_result is the value returned by dlm_dir_lookup on dir node
3561 FIXME: will a non-zero error ever be returned? */
3563 r = lkb->lkb_resource;
3564 hold_rsb(r);
3565 lock_rsb(r);
3567 error = remove_from_waiters(lkb, DLM_MSG_LOOKUP_REPLY);
3568 if (error)
3569 goto out;
3571 ret_nodeid = ms->m_nodeid;
3572 if (ret_nodeid == dlm_our_nodeid()) {
3573 r->res_nodeid = 0;
3574 ret_nodeid = 0;
3575 r->res_first_lkid = 0;
3576 } else {
3577 /* set_master() will copy res_nodeid to lkb_nodeid */
3578 r->res_nodeid = ret_nodeid;
3581 if (is_overlap(lkb)) {
3582 log_debug(ls, "receive_lookup_reply %x unlock %x",
3583 lkb->lkb_id, lkb->lkb_flags);
3584 queue_cast_overlap(r, lkb);
3585 unhold_lkb(lkb); /* undoes create_lkb() */
3586 goto out_list;
3589 _request_lock(r, lkb);
3591 out_list:
3592 if (!ret_nodeid)
3593 process_lookup_list(r);
3594 out:
3595 unlock_rsb(r);
3596 put_rsb(r);
3597 dlm_put_lkb(lkb);
3600 int dlm_receive_message(struct dlm_header *hd, int nodeid, int recovery)
3602 struct dlm_message *ms = (struct dlm_message *) hd;
3603 struct dlm_ls *ls;
3604 int error = 0;
3606 if (!recovery)
3607 dlm_message_in(ms);
3609 ls = dlm_find_lockspace_global(hd->h_lockspace);
3610 if (!ls) {
3611 log_print("drop message %d from %d for unknown lockspace %d",
3612 ms->m_type, nodeid, hd->h_lockspace);
3613 return -EINVAL;
3616 /* recovery may have just ended leaving a bunch of backed-up requests
3617 in the requestqueue; wait while dlm_recoverd clears them */
3619 if (!recovery)
3620 dlm_wait_requestqueue(ls);
3622 /* recovery may have just started while there were a bunch of
3623 in-flight requests -- save them in requestqueue to be processed
3624 after recovery. we can't let dlm_recvd block on the recovery
3625 lock. if dlm_recoverd is calling this function to clear the
3626 requestqueue, it needs to be interrupted (-EINTR) if another
3627 recovery operation is starting. */
3629 while (1) {
3630 if (dlm_locking_stopped(ls)) {
3631 if (recovery) {
3632 error = -EINTR;
3633 goto out;
3635 error = dlm_add_requestqueue(ls, nodeid, hd);
3636 if (error == -EAGAIN)
3637 continue;
3638 else {
3639 error = -EINTR;
3640 goto out;
3644 if (dlm_lock_recovery_try(ls))
3645 break;
3646 schedule();
3649 switch (ms->m_type) {
3651 /* messages sent to a master node */
3653 case DLM_MSG_REQUEST:
3654 receive_request(ls, ms);
3655 break;
3657 case DLM_MSG_CONVERT:
3658 receive_convert(ls, ms);
3659 break;
3661 case DLM_MSG_UNLOCK:
3662 receive_unlock(ls, ms);
3663 break;
3665 case DLM_MSG_CANCEL:
3666 receive_cancel(ls, ms);
3667 break;
3669 /* messages sent from a master node (replies to above) */
3671 case DLM_MSG_REQUEST_REPLY:
3672 receive_request_reply(ls, ms);
3673 break;
3675 case DLM_MSG_CONVERT_REPLY:
3676 receive_convert_reply(ls, ms);
3677 break;
3679 case DLM_MSG_UNLOCK_REPLY:
3680 receive_unlock_reply(ls, ms);
3681 break;
3683 case DLM_MSG_CANCEL_REPLY:
3684 receive_cancel_reply(ls, ms);
3685 break;
3687 /* messages sent from a master node (only two types of async msg) */
3689 case DLM_MSG_GRANT:
3690 receive_grant(ls, ms);
3691 break;
3693 case DLM_MSG_BAST:
3694 receive_bast(ls, ms);
3695 break;
3697 /* messages sent to a dir node */
3699 case DLM_MSG_LOOKUP:
3700 receive_lookup(ls, ms);
3701 break;
3703 case DLM_MSG_REMOVE:
3704 receive_remove(ls, ms);
3705 break;
3707 /* messages sent from a dir node (remove has no reply) */
3709 case DLM_MSG_LOOKUP_REPLY:
3710 receive_lookup_reply(ls, ms);
3711 break;
3713 /* other messages */
3715 case DLM_MSG_PURGE:
3716 receive_purge(ls, ms);
3717 break;
3719 default:
3720 log_error(ls, "unknown message type %d", ms->m_type);
3723 dlm_unlock_recovery(ls);
3724 out:
3725 dlm_put_lockspace(ls);
3726 dlm_astd_wake();
3727 return error;
3732 * Recovery related
3735 static void recover_convert_waiter(struct dlm_ls *ls, struct dlm_lkb *lkb)
3737 if (middle_conversion(lkb)) {
3738 hold_lkb(lkb);
3739 ls->ls_stub_ms.m_type = DLM_MSG_CONVERT_REPLY;
3740 ls->ls_stub_ms.m_result = -EINPROGRESS;
3741 ls->ls_stub_ms.m_flags = lkb->lkb_flags;
3742 _receive_convert_reply(lkb, &ls->ls_stub_ms);
3744 /* Same special case as in receive_rcom_lock_args() */
3745 lkb->lkb_grmode = DLM_LOCK_IV;
3746 rsb_set_flag(lkb->lkb_resource, RSB_RECOVER_CONVERT);
3747 unhold_lkb(lkb);
3749 } else if (lkb->lkb_rqmode >= lkb->lkb_grmode) {
3750 lkb->lkb_flags |= DLM_IFL_RESEND;
3753 /* lkb->lkb_rqmode < lkb->lkb_grmode shouldn't happen since down
3754 conversions are async; there's no reply from the remote master */
3757 /* A waiting lkb needs recovery if the master node has failed, or
3758 the master node is changing (only when no directory is used) */
3760 static int waiter_needs_recovery(struct dlm_ls *ls, struct dlm_lkb *lkb)
3762 if (dlm_is_removed(ls, lkb->lkb_nodeid))
3763 return 1;
3765 if (!dlm_no_directory(ls))
3766 return 0;
3768 if (dlm_dir_nodeid(lkb->lkb_resource) != lkb->lkb_nodeid)
3769 return 1;
3771 return 0;
3774 /* Recovery for locks that are waiting for replies from nodes that are now
3775 gone. We can just complete unlocks and cancels by faking a reply from the
3776 dead node. Requests and up-conversions we flag to be resent after
3777 recovery. Down-conversions can just be completed with a fake reply like
3778 unlocks. Conversions between PR and CW need special attention. */
3780 void dlm_recover_waiters_pre(struct dlm_ls *ls)
3782 struct dlm_lkb *lkb, *safe;
3784 mutex_lock(&ls->ls_waiters_mutex);
3786 list_for_each_entry_safe(lkb, safe, &ls->ls_waiters, lkb_wait_reply) {
3787 log_debug(ls, "pre recover waiter lkid %x type %d flags %x",
3788 lkb->lkb_id, lkb->lkb_wait_type, lkb->lkb_flags);
3790 /* all outstanding lookups, regardless of destination will be
3791 resent after recovery is done */
3793 if (lkb->lkb_wait_type == DLM_MSG_LOOKUP) {
3794 lkb->lkb_flags |= DLM_IFL_RESEND;
3795 continue;
3798 if (!waiter_needs_recovery(ls, lkb))
3799 continue;
3801 switch (lkb->lkb_wait_type) {
3803 case DLM_MSG_REQUEST:
3804 lkb->lkb_flags |= DLM_IFL_RESEND;
3805 break;
3807 case DLM_MSG_CONVERT:
3808 recover_convert_waiter(ls, lkb);
3809 break;
3811 case DLM_MSG_UNLOCK:
3812 hold_lkb(lkb);
3813 ls->ls_stub_ms.m_type = DLM_MSG_UNLOCK_REPLY;
3814 ls->ls_stub_ms.m_result = -DLM_EUNLOCK;
3815 ls->ls_stub_ms.m_flags = lkb->lkb_flags;
3816 _receive_unlock_reply(lkb, &ls->ls_stub_ms);
3817 dlm_put_lkb(lkb);
3818 break;
3820 case DLM_MSG_CANCEL:
3821 hold_lkb(lkb);
3822 ls->ls_stub_ms.m_type = DLM_MSG_CANCEL_REPLY;
3823 ls->ls_stub_ms.m_result = -DLM_ECANCEL;
3824 ls->ls_stub_ms.m_flags = lkb->lkb_flags;
3825 _receive_cancel_reply(lkb, &ls->ls_stub_ms);
3826 dlm_put_lkb(lkb);
3827 break;
3829 default:
3830 log_error(ls, "invalid lkb wait_type %d",
3831 lkb->lkb_wait_type);
3833 schedule();
3835 mutex_unlock(&ls->ls_waiters_mutex);
3838 static struct dlm_lkb *find_resend_waiter(struct dlm_ls *ls)
3840 struct dlm_lkb *lkb;
3841 int found = 0;
3843 mutex_lock(&ls->ls_waiters_mutex);
3844 list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
3845 if (lkb->lkb_flags & DLM_IFL_RESEND) {
3846 hold_lkb(lkb);
3847 found = 1;
3848 break;
3851 mutex_unlock(&ls->ls_waiters_mutex);
3853 if (!found)
3854 lkb = NULL;
3855 return lkb;
3858 /* Deal with lookups and lkb's marked RESEND from _pre. We may now be the
3859 master or dir-node for r. Processing the lkb may result in it being placed
3860 back on waiters. */
3862 /* We do this after normal locking has been enabled and any saved messages
3863 (in requestqueue) have been processed. We should be confident that at
3864 this point we won't get or process a reply to any of these waiting
3865 operations. But, new ops may be coming in on the rsbs/locks here from
3866 userspace or remotely. */
3868 /* there may have been an overlap unlock/cancel prior to recovery or after
3869 recovery. if before, the lkb may still have a pos wait_count; if after, the
3870 overlap flag would just have been set and nothing new sent. we can be
3871 confident here than any replies to either the initial op or overlap ops
3872 prior to recovery have been received. */
3874 int dlm_recover_waiters_post(struct dlm_ls *ls)
3876 struct dlm_lkb *lkb;
3877 struct dlm_rsb *r;
3878 int error = 0, mstype, err, oc, ou;
3880 while (1) {
3881 if (dlm_locking_stopped(ls)) {
3882 log_debug(ls, "recover_waiters_post aborted");
3883 error = -EINTR;
3884 break;
3887 lkb = find_resend_waiter(ls);
3888 if (!lkb)
3889 break;
3891 r = lkb->lkb_resource;
3892 hold_rsb(r);
3893 lock_rsb(r);
3895 mstype = lkb->lkb_wait_type;
3896 oc = is_overlap_cancel(lkb);
3897 ou = is_overlap_unlock(lkb);
3898 err = 0;
3900 log_debug(ls, "recover_waiters_post %x type %d flags %x %s",
3901 lkb->lkb_id, mstype, lkb->lkb_flags, r->res_name);
3903 /* At this point we assume that we won't get a reply to any
3904 previous op or overlap op on this lock. First, do a big
3905 remove_from_waiters() for all previous ops. */
3907 lkb->lkb_flags &= ~DLM_IFL_RESEND;
3908 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
3909 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
3910 lkb->lkb_wait_type = 0;
3911 lkb->lkb_wait_count = 0;
3912 mutex_lock(&ls->ls_waiters_mutex);
3913 list_del_init(&lkb->lkb_wait_reply);
3914 mutex_unlock(&ls->ls_waiters_mutex);
3915 unhold_lkb(lkb); /* for waiters list */
3917 if (oc || ou) {
3918 /* do an unlock or cancel instead of resending */
3919 switch (mstype) {
3920 case DLM_MSG_LOOKUP:
3921 case DLM_MSG_REQUEST:
3922 queue_cast(r, lkb, ou ? -DLM_EUNLOCK :
3923 -DLM_ECANCEL);
3924 unhold_lkb(lkb); /* undoes create_lkb() */
3925 break;
3926 case DLM_MSG_CONVERT:
3927 if (oc) {
3928 queue_cast(r, lkb, -DLM_ECANCEL);
3929 } else {
3930 lkb->lkb_exflags |= DLM_LKF_FORCEUNLOCK;
3931 _unlock_lock(r, lkb);
3933 break;
3934 default:
3935 err = 1;
3937 } else {
3938 switch (mstype) {
3939 case DLM_MSG_LOOKUP:
3940 case DLM_MSG_REQUEST:
3941 _request_lock(r, lkb);
3942 if (is_master(r))
3943 confirm_master(r, 0);
3944 break;
3945 case DLM_MSG_CONVERT:
3946 _convert_lock(r, lkb);
3947 break;
3948 default:
3949 err = 1;
3953 if (err)
3954 log_error(ls, "recover_waiters_post %x %d %x %d %d",
3955 lkb->lkb_id, mstype, lkb->lkb_flags, oc, ou);
3956 unlock_rsb(r);
3957 put_rsb(r);
3958 dlm_put_lkb(lkb);
3961 return error;
3964 static void purge_queue(struct dlm_rsb *r, struct list_head *queue,
3965 int (*test)(struct dlm_ls *ls, struct dlm_lkb *lkb))
3967 struct dlm_ls *ls = r->res_ls;
3968 struct dlm_lkb *lkb, *safe;
3970 list_for_each_entry_safe(lkb, safe, queue, lkb_statequeue) {
3971 if (test(ls, lkb)) {
3972 rsb_set_flag(r, RSB_LOCKS_PURGED);
3973 del_lkb(r, lkb);
3974 /* this put should free the lkb */
3975 if (!dlm_put_lkb(lkb))
3976 log_error(ls, "purged lkb not released");
3981 static int purge_dead_test(struct dlm_ls *ls, struct dlm_lkb *lkb)
3983 return (is_master_copy(lkb) && dlm_is_removed(ls, lkb->lkb_nodeid));
3986 static int purge_mstcpy_test(struct dlm_ls *ls, struct dlm_lkb *lkb)
3988 return is_master_copy(lkb);
3991 static void purge_dead_locks(struct dlm_rsb *r)
3993 purge_queue(r, &r->res_grantqueue, &purge_dead_test);
3994 purge_queue(r, &r->res_convertqueue, &purge_dead_test);
3995 purge_queue(r, &r->res_waitqueue, &purge_dead_test);
3998 void dlm_purge_mstcpy_locks(struct dlm_rsb *r)
4000 purge_queue(r, &r->res_grantqueue, &purge_mstcpy_test);
4001 purge_queue(r, &r->res_convertqueue, &purge_mstcpy_test);
4002 purge_queue(r, &r->res_waitqueue, &purge_mstcpy_test);
4005 /* Get rid of locks held by nodes that are gone. */
4007 int dlm_purge_locks(struct dlm_ls *ls)
4009 struct dlm_rsb *r;
4011 log_debug(ls, "dlm_purge_locks");
4013 down_write(&ls->ls_root_sem);
4014 list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
4015 hold_rsb(r);
4016 lock_rsb(r);
4017 if (is_master(r))
4018 purge_dead_locks(r);
4019 unlock_rsb(r);
4020 unhold_rsb(r);
4022 schedule();
4024 up_write(&ls->ls_root_sem);
4026 return 0;
4029 static struct dlm_rsb *find_purged_rsb(struct dlm_ls *ls, int bucket)
4031 struct dlm_rsb *r, *r_ret = NULL;
4033 read_lock(&ls->ls_rsbtbl[bucket].lock);
4034 list_for_each_entry(r, &ls->ls_rsbtbl[bucket].list, res_hashchain) {
4035 if (!rsb_flag(r, RSB_LOCKS_PURGED))
4036 continue;
4037 hold_rsb(r);
4038 rsb_clear_flag(r, RSB_LOCKS_PURGED);
4039 r_ret = r;
4040 break;
4042 read_unlock(&ls->ls_rsbtbl[bucket].lock);
4043 return r_ret;
4046 void dlm_grant_after_purge(struct dlm_ls *ls)
4048 struct dlm_rsb *r;
4049 int bucket = 0;
4051 while (1) {
4052 r = find_purged_rsb(ls, bucket);
4053 if (!r) {
4054 if (bucket == ls->ls_rsbtbl_size - 1)
4055 break;
4056 bucket++;
4057 continue;
4059 lock_rsb(r);
4060 if (is_master(r)) {
4061 grant_pending_locks(r);
4062 confirm_master(r, 0);
4064 unlock_rsb(r);
4065 put_rsb(r);
4066 schedule();
4070 static struct dlm_lkb *search_remid_list(struct list_head *head, int nodeid,
4071 uint32_t remid)
4073 struct dlm_lkb *lkb;
4075 list_for_each_entry(lkb, head, lkb_statequeue) {
4076 if (lkb->lkb_nodeid == nodeid && lkb->lkb_remid == remid)
4077 return lkb;
4079 return NULL;
4082 static struct dlm_lkb *search_remid(struct dlm_rsb *r, int nodeid,
4083 uint32_t remid)
4085 struct dlm_lkb *lkb;
4087 lkb = search_remid_list(&r->res_grantqueue, nodeid, remid);
4088 if (lkb)
4089 return lkb;
4090 lkb = search_remid_list(&r->res_convertqueue, nodeid, remid);
4091 if (lkb)
4092 return lkb;
4093 lkb = search_remid_list(&r->res_waitqueue, nodeid, remid);
4094 if (lkb)
4095 return lkb;
4096 return NULL;
4099 static int receive_rcom_lock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
4100 struct dlm_rsb *r, struct dlm_rcom *rc)
4102 struct rcom_lock *rl = (struct rcom_lock *) rc->rc_buf;
4103 int lvblen;
4105 lkb->lkb_nodeid = rc->rc_header.h_nodeid;
4106 lkb->lkb_ownpid = rl->rl_ownpid;
4107 lkb->lkb_remid = rl->rl_lkid;
4108 lkb->lkb_exflags = rl->rl_exflags;
4109 lkb->lkb_flags = rl->rl_flags & 0x0000FFFF;
4110 lkb->lkb_flags |= DLM_IFL_MSTCPY;
4111 lkb->lkb_lvbseq = rl->rl_lvbseq;
4112 lkb->lkb_rqmode = rl->rl_rqmode;
4113 lkb->lkb_grmode = rl->rl_grmode;
4114 /* don't set lkb_status because add_lkb wants to itself */
4116 lkb->lkb_bastaddr = (void *) (long) (rl->rl_asts & AST_BAST);
4117 lkb->lkb_astaddr = (void *) (long) (rl->rl_asts & AST_COMP);
4119 if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
4120 lkb->lkb_lvbptr = allocate_lvb(ls);
4121 if (!lkb->lkb_lvbptr)
4122 return -ENOMEM;
4123 lvblen = rc->rc_header.h_length - sizeof(struct dlm_rcom) -
4124 sizeof(struct rcom_lock);
4125 memcpy(lkb->lkb_lvbptr, rl->rl_lvb, lvblen);
4128 /* Conversions between PR and CW (middle modes) need special handling.
4129 The real granted mode of these converting locks cannot be determined
4130 until all locks have been rebuilt on the rsb (recover_conversion) */
4132 if (rl->rl_wait_type == DLM_MSG_CONVERT && middle_conversion(lkb)) {
4133 rl->rl_status = DLM_LKSTS_CONVERT;
4134 lkb->lkb_grmode = DLM_LOCK_IV;
4135 rsb_set_flag(r, RSB_RECOVER_CONVERT);
4138 return 0;
4141 /* This lkb may have been recovered in a previous aborted recovery so we need
4142 to check if the rsb already has an lkb with the given remote nodeid/lkid.
4143 If so we just send back a standard reply. If not, we create a new lkb with
4144 the given values and send back our lkid. We send back our lkid by sending
4145 back the rcom_lock struct we got but with the remid field filled in. */
4147 int dlm_recover_master_copy(struct dlm_ls *ls, struct dlm_rcom *rc)
4149 struct rcom_lock *rl = (struct rcom_lock *) rc->rc_buf;
4150 struct dlm_rsb *r;
4151 struct dlm_lkb *lkb;
4152 int error;
4154 if (rl->rl_parent_lkid) {
4155 error = -EOPNOTSUPP;
4156 goto out;
4159 error = find_rsb(ls, rl->rl_name, rl->rl_namelen, R_MASTER, &r);
4160 if (error)
4161 goto out;
4163 lock_rsb(r);
4165 lkb = search_remid(r, rc->rc_header.h_nodeid, rl->rl_lkid);
4166 if (lkb) {
4167 error = -EEXIST;
4168 goto out_remid;
4171 error = create_lkb(ls, &lkb);
4172 if (error)
4173 goto out_unlock;
4175 error = receive_rcom_lock_args(ls, lkb, r, rc);
4176 if (error) {
4177 __put_lkb(ls, lkb);
4178 goto out_unlock;
4181 attach_lkb(r, lkb);
4182 add_lkb(r, lkb, rl->rl_status);
4183 error = 0;
4185 out_remid:
4186 /* this is the new value returned to the lock holder for
4187 saving in its process-copy lkb */
4188 rl->rl_remid = lkb->lkb_id;
4190 out_unlock:
4191 unlock_rsb(r);
4192 put_rsb(r);
4193 out:
4194 if (error)
4195 log_print("recover_master_copy %d %x", error, rl->rl_lkid);
4196 rl->rl_result = error;
4197 return error;
4200 int dlm_recover_process_copy(struct dlm_ls *ls, struct dlm_rcom *rc)
4202 struct rcom_lock *rl = (struct rcom_lock *) rc->rc_buf;
4203 struct dlm_rsb *r;
4204 struct dlm_lkb *lkb;
4205 int error;
4207 error = find_lkb(ls, rl->rl_lkid, &lkb);
4208 if (error) {
4209 log_error(ls, "recover_process_copy no lkid %x", rl->rl_lkid);
4210 return error;
4213 DLM_ASSERT(is_process_copy(lkb), dlm_print_lkb(lkb););
4215 error = rl->rl_result;
4217 r = lkb->lkb_resource;
4218 hold_rsb(r);
4219 lock_rsb(r);
4221 switch (error) {
4222 case -EBADR:
4223 /* There's a chance the new master received our lock before
4224 dlm_recover_master_reply(), this wouldn't happen if we did
4225 a barrier between recover_masters and recover_locks. */
4226 log_debug(ls, "master copy not ready %x r %lx %s", lkb->lkb_id,
4227 (unsigned long)r, r->res_name);
4228 dlm_send_rcom_lock(r, lkb);
4229 goto out;
4230 case -EEXIST:
4231 log_debug(ls, "master copy exists %x", lkb->lkb_id);
4232 /* fall through */
4233 case 0:
4234 lkb->lkb_remid = rl->rl_remid;
4235 break;
4236 default:
4237 log_error(ls, "dlm_recover_process_copy unknown error %d %x",
4238 error, lkb->lkb_id);
4241 /* an ack for dlm_recover_locks() which waits for replies from
4242 all the locks it sends to new masters */
4243 dlm_recovered_lock(r);
4244 out:
4245 unlock_rsb(r);
4246 put_rsb(r);
4247 dlm_put_lkb(lkb);
4249 return 0;
4252 int dlm_user_request(struct dlm_ls *ls, struct dlm_user_args *ua,
4253 int mode, uint32_t flags, void *name, unsigned int namelen,
4254 unsigned long timeout_cs)
4256 struct dlm_lkb *lkb;
4257 struct dlm_args args;
4258 int error;
4260 dlm_lock_recovery(ls);
4262 error = create_lkb(ls, &lkb);
4263 if (error) {
4264 kfree(ua);
4265 goto out;
4268 if (flags & DLM_LKF_VALBLK) {
4269 ua->lksb.sb_lvbptr = kzalloc(DLM_USER_LVB_LEN, GFP_KERNEL);
4270 if (!ua->lksb.sb_lvbptr) {
4271 kfree(ua);
4272 __put_lkb(ls, lkb);
4273 error = -ENOMEM;
4274 goto out;
4278 /* After ua is attached to lkb it will be freed by free_lkb().
4279 When DLM_IFL_USER is set, the dlm knows that this is a userspace
4280 lock and that lkb_astparam is the dlm_user_args structure. */
4282 error = set_lock_args(mode, &ua->lksb, flags, namelen, timeout_cs,
4283 DLM_FAKE_USER_AST, ua, DLM_FAKE_USER_AST, &args);
4284 lkb->lkb_flags |= DLM_IFL_USER;
4285 ua->old_mode = DLM_LOCK_IV;
4287 if (error) {
4288 __put_lkb(ls, lkb);
4289 goto out;
4292 error = request_lock(ls, lkb, name, namelen, &args);
4294 switch (error) {
4295 case 0:
4296 break;
4297 case -EINPROGRESS:
4298 error = 0;
4299 break;
4300 case -EAGAIN:
4301 error = 0;
4302 /* fall through */
4303 default:
4304 __put_lkb(ls, lkb);
4305 goto out;
4308 /* add this new lkb to the per-process list of locks */
4309 spin_lock(&ua->proc->locks_spin);
4310 hold_lkb(lkb);
4311 list_add_tail(&lkb->lkb_ownqueue, &ua->proc->locks);
4312 spin_unlock(&ua->proc->locks_spin);
4313 out:
4314 dlm_unlock_recovery(ls);
4315 return error;
4318 int dlm_user_convert(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
4319 int mode, uint32_t flags, uint32_t lkid, char *lvb_in,
4320 unsigned long timeout_cs)
4322 struct dlm_lkb *lkb;
4323 struct dlm_args args;
4324 struct dlm_user_args *ua;
4325 int error;
4327 dlm_lock_recovery(ls);
4329 error = find_lkb(ls, lkid, &lkb);
4330 if (error)
4331 goto out;
4333 /* user can change the params on its lock when it converts it, or
4334 add an lvb that didn't exist before */
4336 ua = (struct dlm_user_args *)lkb->lkb_astparam;
4338 if (flags & DLM_LKF_VALBLK && !ua->lksb.sb_lvbptr) {
4339 ua->lksb.sb_lvbptr = kzalloc(DLM_USER_LVB_LEN, GFP_KERNEL);
4340 if (!ua->lksb.sb_lvbptr) {
4341 error = -ENOMEM;
4342 goto out_put;
4345 if (lvb_in && ua->lksb.sb_lvbptr)
4346 memcpy(ua->lksb.sb_lvbptr, lvb_in, DLM_USER_LVB_LEN);
4348 ua->xid = ua_tmp->xid;
4349 ua->castparam = ua_tmp->castparam;
4350 ua->castaddr = ua_tmp->castaddr;
4351 ua->bastparam = ua_tmp->bastparam;
4352 ua->bastaddr = ua_tmp->bastaddr;
4353 ua->user_lksb = ua_tmp->user_lksb;
4354 ua->old_mode = lkb->lkb_grmode;
4356 error = set_lock_args(mode, &ua->lksb, flags, 0, timeout_cs,
4357 DLM_FAKE_USER_AST, ua, DLM_FAKE_USER_AST, &args);
4358 if (error)
4359 goto out_put;
4361 error = convert_lock(ls, lkb, &args);
4363 if (error == -EINPROGRESS || error == -EAGAIN || error == -EDEADLK)
4364 error = 0;
4365 out_put:
4366 dlm_put_lkb(lkb);
4367 out:
4368 dlm_unlock_recovery(ls);
4369 kfree(ua_tmp);
4370 return error;
4373 int dlm_user_unlock(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
4374 uint32_t flags, uint32_t lkid, char *lvb_in)
4376 struct dlm_lkb *lkb;
4377 struct dlm_args args;
4378 struct dlm_user_args *ua;
4379 int error;
4381 dlm_lock_recovery(ls);
4383 error = find_lkb(ls, lkid, &lkb);
4384 if (error)
4385 goto out;
4387 ua = (struct dlm_user_args *)lkb->lkb_astparam;
4389 if (lvb_in && ua->lksb.sb_lvbptr)
4390 memcpy(ua->lksb.sb_lvbptr, lvb_in, DLM_USER_LVB_LEN);
4391 ua->castparam = ua_tmp->castparam;
4392 ua->user_lksb = ua_tmp->user_lksb;
4394 error = set_unlock_args(flags, ua, &args);
4395 if (error)
4396 goto out_put;
4398 error = unlock_lock(ls, lkb, &args);
4400 if (error == -DLM_EUNLOCK)
4401 error = 0;
4402 /* from validate_unlock_args() */
4403 if (error == -EBUSY && (flags & DLM_LKF_FORCEUNLOCK))
4404 error = 0;
4405 if (error)
4406 goto out_put;
4408 spin_lock(&ua->proc->locks_spin);
4409 /* dlm_user_add_ast() may have already taken lkb off the proc list */
4410 if (!list_empty(&lkb->lkb_ownqueue))
4411 list_move(&lkb->lkb_ownqueue, &ua->proc->unlocking);
4412 spin_unlock(&ua->proc->locks_spin);
4413 out_put:
4414 dlm_put_lkb(lkb);
4415 out:
4416 dlm_unlock_recovery(ls);
4417 kfree(ua_tmp);
4418 return error;
4421 int dlm_user_cancel(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
4422 uint32_t flags, uint32_t lkid)
4424 struct dlm_lkb *lkb;
4425 struct dlm_args args;
4426 struct dlm_user_args *ua;
4427 int error;
4429 dlm_lock_recovery(ls);
4431 error = find_lkb(ls, lkid, &lkb);
4432 if (error)
4433 goto out;
4435 ua = (struct dlm_user_args *)lkb->lkb_astparam;
4436 ua->castparam = ua_tmp->castparam;
4437 ua->user_lksb = ua_tmp->user_lksb;
4439 error = set_unlock_args(flags, ua, &args);
4440 if (error)
4441 goto out_put;
4443 error = cancel_lock(ls, lkb, &args);
4445 if (error == -DLM_ECANCEL)
4446 error = 0;
4447 /* from validate_unlock_args() */
4448 if (error == -EBUSY)
4449 error = 0;
4450 out_put:
4451 dlm_put_lkb(lkb);
4452 out:
4453 dlm_unlock_recovery(ls);
4454 kfree(ua_tmp);
4455 return error;
4458 int dlm_user_deadlock(struct dlm_ls *ls, uint32_t flags, uint32_t lkid)
4460 struct dlm_lkb *lkb;
4461 struct dlm_args args;
4462 struct dlm_user_args *ua;
4463 struct dlm_rsb *r;
4464 int error;
4466 dlm_lock_recovery(ls);
4468 error = find_lkb(ls, lkid, &lkb);
4469 if (error)
4470 goto out;
4472 ua = (struct dlm_user_args *)lkb->lkb_astparam;
4474 error = set_unlock_args(flags, ua, &args);
4475 if (error)
4476 goto out_put;
4478 /* same as cancel_lock(), but set DEADLOCK_CANCEL after lock_rsb */
4480 r = lkb->lkb_resource;
4481 hold_rsb(r);
4482 lock_rsb(r);
4484 error = validate_unlock_args(lkb, &args);
4485 if (error)
4486 goto out_r;
4487 lkb->lkb_flags |= DLM_IFL_DEADLOCK_CANCEL;
4489 error = _cancel_lock(r, lkb);
4490 out_r:
4491 unlock_rsb(r);
4492 put_rsb(r);
4494 if (error == -DLM_ECANCEL)
4495 error = 0;
4496 /* from validate_unlock_args() */
4497 if (error == -EBUSY)
4498 error = 0;
4499 out_put:
4500 dlm_put_lkb(lkb);
4501 out:
4502 dlm_unlock_recovery(ls);
4503 return error;
4506 /* lkb's that are removed from the waiters list by revert are just left on the
4507 orphans list with the granted orphan locks, to be freed by purge */
4509 static int orphan_proc_lock(struct dlm_ls *ls, struct dlm_lkb *lkb)
4511 struct dlm_user_args *ua = (struct dlm_user_args *)lkb->lkb_astparam;
4512 struct dlm_args args;
4513 int error;
4515 hold_lkb(lkb);
4516 mutex_lock(&ls->ls_orphans_mutex);
4517 list_add_tail(&lkb->lkb_ownqueue, &ls->ls_orphans);
4518 mutex_unlock(&ls->ls_orphans_mutex);
4520 set_unlock_args(0, ua, &args);
4522 error = cancel_lock(ls, lkb, &args);
4523 if (error == -DLM_ECANCEL)
4524 error = 0;
4525 return error;
4528 /* The force flag allows the unlock to go ahead even if the lkb isn't granted.
4529 Regardless of what rsb queue the lock is on, it's removed and freed. */
4531 static int unlock_proc_lock(struct dlm_ls *ls, struct dlm_lkb *lkb)
4533 struct dlm_user_args *ua = (struct dlm_user_args *)lkb->lkb_astparam;
4534 struct dlm_args args;
4535 int error;
4537 set_unlock_args(DLM_LKF_FORCEUNLOCK, ua, &args);
4539 error = unlock_lock(ls, lkb, &args);
4540 if (error == -DLM_EUNLOCK)
4541 error = 0;
4542 return error;
4545 /* We have to release clear_proc_locks mutex before calling unlock_proc_lock()
4546 (which does lock_rsb) due to deadlock with receiving a message that does
4547 lock_rsb followed by dlm_user_add_ast() */
4549 static struct dlm_lkb *del_proc_lock(struct dlm_ls *ls,
4550 struct dlm_user_proc *proc)
4552 struct dlm_lkb *lkb = NULL;
4554 mutex_lock(&ls->ls_clear_proc_locks);
4555 if (list_empty(&proc->locks))
4556 goto out;
4558 lkb = list_entry(proc->locks.next, struct dlm_lkb, lkb_ownqueue);
4559 list_del_init(&lkb->lkb_ownqueue);
4561 if (lkb->lkb_exflags & DLM_LKF_PERSISTENT)
4562 lkb->lkb_flags |= DLM_IFL_ORPHAN;
4563 else
4564 lkb->lkb_flags |= DLM_IFL_DEAD;
4565 out:
4566 mutex_unlock(&ls->ls_clear_proc_locks);
4567 return lkb;
4570 /* The ls_clear_proc_locks mutex protects against dlm_user_add_asts() which
4571 1) references lkb->ua which we free here and 2) adds lkbs to proc->asts,
4572 which we clear here. */
4574 /* proc CLOSING flag is set so no more device_reads should look at proc->asts
4575 list, and no more device_writes should add lkb's to proc->locks list; so we
4576 shouldn't need to take asts_spin or locks_spin here. this assumes that
4577 device reads/writes/closes are serialized -- FIXME: we may need to serialize
4578 them ourself. */
4580 void dlm_clear_proc_locks(struct dlm_ls *ls, struct dlm_user_proc *proc)
4582 struct dlm_lkb *lkb, *safe;
4584 dlm_lock_recovery(ls);
4586 while (1) {
4587 lkb = del_proc_lock(ls, proc);
4588 if (!lkb)
4589 break;
4590 del_timeout(lkb);
4591 if (lkb->lkb_exflags & DLM_LKF_PERSISTENT)
4592 orphan_proc_lock(ls, lkb);
4593 else
4594 unlock_proc_lock(ls, lkb);
4596 /* this removes the reference for the proc->locks list
4597 added by dlm_user_request, it may result in the lkb
4598 being freed */
4600 dlm_put_lkb(lkb);
4603 mutex_lock(&ls->ls_clear_proc_locks);
4605 /* in-progress unlocks */
4606 list_for_each_entry_safe(lkb, safe, &proc->unlocking, lkb_ownqueue) {
4607 list_del_init(&lkb->lkb_ownqueue);
4608 lkb->lkb_flags |= DLM_IFL_DEAD;
4609 dlm_put_lkb(lkb);
4612 list_for_each_entry_safe(lkb, safe, &proc->asts, lkb_astqueue) {
4613 list_del(&lkb->lkb_astqueue);
4614 dlm_put_lkb(lkb);
4617 mutex_unlock(&ls->ls_clear_proc_locks);
4618 dlm_unlock_recovery(ls);
4621 static void purge_proc_locks(struct dlm_ls *ls, struct dlm_user_proc *proc)
4623 struct dlm_lkb *lkb, *safe;
4625 while (1) {
4626 lkb = NULL;
4627 spin_lock(&proc->locks_spin);
4628 if (!list_empty(&proc->locks)) {
4629 lkb = list_entry(proc->locks.next, struct dlm_lkb,
4630 lkb_ownqueue);
4631 list_del_init(&lkb->lkb_ownqueue);
4633 spin_unlock(&proc->locks_spin);
4635 if (!lkb)
4636 break;
4638 lkb->lkb_flags |= DLM_IFL_DEAD;
4639 unlock_proc_lock(ls, lkb);
4640 dlm_put_lkb(lkb); /* ref from proc->locks list */
4643 spin_lock(&proc->locks_spin);
4644 list_for_each_entry_safe(lkb, safe, &proc->unlocking, lkb_ownqueue) {
4645 list_del_init(&lkb->lkb_ownqueue);
4646 lkb->lkb_flags |= DLM_IFL_DEAD;
4647 dlm_put_lkb(lkb);
4649 spin_unlock(&proc->locks_spin);
4651 spin_lock(&proc->asts_spin);
4652 list_for_each_entry_safe(lkb, safe, &proc->asts, lkb_astqueue) {
4653 list_del(&lkb->lkb_astqueue);
4654 dlm_put_lkb(lkb);
4656 spin_unlock(&proc->asts_spin);
4659 /* pid of 0 means purge all orphans */
4661 static void do_purge(struct dlm_ls *ls, int nodeid, int pid)
4663 struct dlm_lkb *lkb, *safe;
4665 mutex_lock(&ls->ls_orphans_mutex);
4666 list_for_each_entry_safe(lkb, safe, &ls->ls_orphans, lkb_ownqueue) {
4667 if (pid && lkb->lkb_ownpid != pid)
4668 continue;
4669 unlock_proc_lock(ls, lkb);
4670 list_del_init(&lkb->lkb_ownqueue);
4671 dlm_put_lkb(lkb);
4673 mutex_unlock(&ls->ls_orphans_mutex);
4676 static int send_purge(struct dlm_ls *ls, int nodeid, int pid)
4678 struct dlm_message *ms;
4679 struct dlm_mhandle *mh;
4680 int error;
4682 error = _create_message(ls, sizeof(struct dlm_message), nodeid,
4683 DLM_MSG_PURGE, &ms, &mh);
4684 if (error)
4685 return error;
4686 ms->m_nodeid = nodeid;
4687 ms->m_pid = pid;
4689 return send_message(mh, ms);
4692 int dlm_user_purge(struct dlm_ls *ls, struct dlm_user_proc *proc,
4693 int nodeid, int pid)
4695 int error = 0;
4697 if (nodeid != dlm_our_nodeid()) {
4698 error = send_purge(ls, nodeid, pid);
4699 } else {
4700 dlm_lock_recovery(ls);
4701 if (pid == current->pid)
4702 purge_proc_locks(ls, proc);
4703 else
4704 do_purge(ls, nodeid, pid);
4705 dlm_unlock_recovery(ls);
4707 return error;