dlm: don't do pointless NULL check, use kzalloc and fix order of arguments
[deliverable/linux.git] / fs / dlm / lock.c
CommitLineData
e7fd4179
DT
1/******************************************************************************
2*******************************************************************************
3**
7fe2b319 4** Copyright (C) 2005-2010 Red Hat, Inc. All rights reserved.
e7fd4179
DT
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******************************************************************************/
12
13/* Central locking logic has four stages:
14
15 dlm_lock()
16 dlm_unlock()
17
18 request_lock(ls, lkb)
19 convert_lock(ls, lkb)
20 unlock_lock(ls, lkb)
21 cancel_lock(ls, lkb)
22
23 _request_lock(r, lkb)
24 _convert_lock(r, lkb)
25 _unlock_lock(r, lkb)
26 _cancel_lock(r, lkb)
27
28 do_request(r, lkb)
29 do_convert(r, lkb)
30 do_unlock(r, lkb)
31 do_cancel(r, lkb)
32
33 Stage 1 (lock, unlock) is mainly about checking input args and
34 splitting into one of the four main operations:
35
36 dlm_lock = request_lock
37 dlm_lock+CONVERT = convert_lock
38 dlm_unlock = unlock_lock
39 dlm_unlock+CANCEL = cancel_lock
40
41 Stage 2, xxxx_lock(), just finds and locks the relevant rsb which is
42 provided to the next stage.
43
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().
46
47 Stage 4, do_xxxx(), is the guts of the operation. It manipulates the
48 given rsb and lkb and queues callbacks.
49
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:
53
54 L: send_xxxx() -> R: receive_xxxx()
55 R: do_xxxx()
56 L: receive_xxxx_reply() <- R: send_xxxx_reply()
57*/
597d0cae 58#include <linux/types.h>
5a0e3ad6 59#include <linux/slab.h>
e7fd4179 60#include "dlm_internal.h"
597d0cae 61#include <linux/dlm_device.h>
e7fd4179
DT
62#include "memory.h"
63#include "lowcomms.h"
64#include "requestqueue.h"
65#include "util.h"
66#include "dir.h"
67#include "member.h"
68#include "lockspace.h"
69#include "ast.h"
70#include "lock.h"
71#include "rcom.h"
72#include "recover.h"
73#include "lvb_table.h"
597d0cae 74#include "user.h"
e7fd4179
DT
75#include "config.h"
76
77static int send_request(struct dlm_rsb *r, struct dlm_lkb *lkb);
78static int send_convert(struct dlm_rsb *r, struct dlm_lkb *lkb);
79static int send_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb);
80static int send_cancel(struct dlm_rsb *r, struct dlm_lkb *lkb);
81static int send_grant(struct dlm_rsb *r, struct dlm_lkb *lkb);
82static int send_bast(struct dlm_rsb *r, struct dlm_lkb *lkb, int mode);
83static int send_lookup(struct dlm_rsb *r, struct dlm_lkb *lkb);
84static int send_remove(struct dlm_rsb *r);
85static int _request_lock(struct dlm_rsb *r, struct dlm_lkb *lkb);
3ae1acf9 86static int _cancel_lock(struct dlm_rsb *r, struct dlm_lkb *lkb);
e7fd4179
DT
87static void __receive_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
88 struct dlm_message *ms);
89static int receive_extralen(struct dlm_message *ms);
8499137d 90static void do_purge(struct dlm_ls *ls, int nodeid, int pid);
3ae1acf9 91static void del_timeout(struct dlm_lkb *lkb);
e7fd4179
DT
92
93/*
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)
99 */
100
101static 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 */
111};
112
113/*
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
120 */
121
122const 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 */
132};
e7fd4179
DT
133
134#define modes_compat(gr, rq) \
135 __dlm_compat_matrix[(gr)->lkb_grmode + 1][(rq)->lkb_rqmode + 1]
136
137int dlm_modes_compat(int mode1, int mode2)
138{
139 return __dlm_compat_matrix[mode1 + 1][mode2 + 1];
140}
141
142/*
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]
146 */
147
148static 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 */
158};
159
597d0cae 160void dlm_print_lkb(struct dlm_lkb *lkb)
e7fd4179
DT
161{
162 printk(KERN_ERR "lkb: nodeid %d id %x remid %x exflags %x flags %x\n"
8304d6f2 163 " status %d rqmode %d grmode %d wait_type %d\n",
e7fd4179
DT
164 lkb->lkb_nodeid, lkb->lkb_id, lkb->lkb_remid, lkb->lkb_exflags,
165 lkb->lkb_flags, lkb->lkb_status, lkb->lkb_rqmode,
8304d6f2 166 lkb->lkb_grmode, lkb->lkb_wait_type);
e7fd4179
DT
167}
168
170e19ab 169static void dlm_print_rsb(struct dlm_rsb *r)
e7fd4179
DT
170{
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);
174}
175
a345da3e
DT
176void dlm_dump_rsb(struct dlm_rsb *r)
177{
178 struct dlm_lkb *lkb;
179
180 dlm_print_rsb(r);
181
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);
196}
197
e7fd4179
DT
198/* Threads cannot use the lockspace while it's being recovered */
199
85e86edf 200static inline void dlm_lock_recovery(struct dlm_ls *ls)
e7fd4179
DT
201{
202 down_read(&ls->ls_in_recovery);
203}
204
85e86edf 205void dlm_unlock_recovery(struct dlm_ls *ls)
e7fd4179
DT
206{
207 up_read(&ls->ls_in_recovery);
208}
209
85e86edf 210int dlm_lock_recovery_try(struct dlm_ls *ls)
e7fd4179
DT
211{
212 return down_read_trylock(&ls->ls_in_recovery);
213}
214
215static inline int can_be_queued(struct dlm_lkb *lkb)
216{
217 return !(lkb->lkb_exflags & DLM_LKF_NOQUEUE);
218}
219
220static inline int force_blocking_asts(struct dlm_lkb *lkb)
221{
222 return (lkb->lkb_exflags & DLM_LKF_NOQUEUEBAST);
223}
224
225static inline int is_demoted(struct dlm_lkb *lkb)
226{
227 return (lkb->lkb_sbflags & DLM_SBF_DEMOTED);
228}
229
7d3c1feb
DT
230static inline int is_altmode(struct dlm_lkb *lkb)
231{
232 return (lkb->lkb_sbflags & DLM_SBF_ALTMODE);
233}
234
235static inline int is_granted(struct dlm_lkb *lkb)
236{
237 return (lkb->lkb_status == DLM_LKSTS_GRANTED);
238}
239
e7fd4179
DT
240static inline int is_remote(struct dlm_rsb *r)
241{
242 DLM_ASSERT(r->res_nodeid >= 0, dlm_print_rsb(r););
243 return !!r->res_nodeid;
244}
245
246static inline int is_process_copy(struct dlm_lkb *lkb)
247{
248 return (lkb->lkb_nodeid && !(lkb->lkb_flags & DLM_IFL_MSTCPY));
249}
250
251static inline int is_master_copy(struct dlm_lkb *lkb)
252{
253 if (lkb->lkb_flags & DLM_IFL_MSTCPY)
254 DLM_ASSERT(lkb->lkb_nodeid, dlm_print_lkb(lkb););
90135925 255 return (lkb->lkb_flags & DLM_IFL_MSTCPY) ? 1 : 0;
e7fd4179
DT
256}
257
258static inline int middle_conversion(struct dlm_lkb *lkb)
259{
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))
90135925
DT
262 return 1;
263 return 0;
e7fd4179
DT
264}
265
266static inline int down_conversion(struct dlm_lkb *lkb)
267{
268 return (!middle_conversion(lkb) && lkb->lkb_rqmode < lkb->lkb_grmode);
269}
270
ef0c2bb0
DT
271static inline int is_overlap_unlock(struct dlm_lkb *lkb)
272{
273 return lkb->lkb_flags & DLM_IFL_OVERLAP_UNLOCK;
274}
275
276static inline int is_overlap_cancel(struct dlm_lkb *lkb)
277{
278 return lkb->lkb_flags & DLM_IFL_OVERLAP_CANCEL;
279}
280
281static inline int is_overlap(struct dlm_lkb *lkb)
282{
283 return (lkb->lkb_flags & (DLM_IFL_OVERLAP_UNLOCK |
284 DLM_IFL_OVERLAP_CANCEL));
285}
286
e7fd4179
DT
287static void queue_cast(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
288{
289 if (is_master_copy(lkb))
290 return;
291
3ae1acf9
DT
292 del_timeout(lkb);
293
e7fd4179
DT
294 DLM_ASSERT(lkb->lkb_lksb, dlm_print_lkb(lkb););
295
3ae1acf9
DT
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;
301 }
302
8b4021fa
DT
303 if (rv == -DLM_ECANCEL && (lkb->lkb_flags & DLM_IFL_DEADLOCK_CANCEL)) {
304 lkb->lkb_flags &= ~DLM_IFL_DEADLOCK_CANCEL;
305 rv = -EDEADLK;
306 }
307
8304d6f2 308 dlm_add_ast(lkb, DLM_CB_CAST, lkb->lkb_grmode, rv, lkb->lkb_sbflags);
e7fd4179
DT
309}
310
ef0c2bb0
DT
311static inline void queue_cast_overlap(struct dlm_rsb *r, struct dlm_lkb *lkb)
312{
313 queue_cast(r, lkb,
314 is_overlap_unlock(lkb) ? -DLM_EUNLOCK : -DLM_ECANCEL);
315}
316
e7fd4179
DT
317static void queue_bast(struct dlm_rsb *r, struct dlm_lkb *lkb, int rqmode)
318{
b6fa8796 319 if (is_master_copy(lkb)) {
e7fd4179 320 send_bast(r, lkb, rqmode);
b6fa8796 321 } else {
8304d6f2 322 dlm_add_ast(lkb, DLM_CB_BAST, rqmode, 0, 0);
b6fa8796 323 }
e7fd4179
DT
324}
325
326/*
327 * Basic operations on rsb's and lkb's
328 */
329
330static struct dlm_rsb *create_rsb(struct dlm_ls *ls, char *name, int len)
331{
332 struct dlm_rsb *r;
333
52bda2b5 334 r = dlm_allocate_rsb(ls, len);
e7fd4179
DT
335 if (!r)
336 return NULL;
337
338 r->res_ls = ls;
339 r->res_length = len;
340 memcpy(r->res_name, name, len);
90135925 341 mutex_init(&r->res_mutex);
e7fd4179
DT
342
343 INIT_LIST_HEAD(&r->res_lookup);
344 INIT_LIST_HEAD(&r->res_grantqueue);
345 INIT_LIST_HEAD(&r->res_convertqueue);
346 INIT_LIST_HEAD(&r->res_waitqueue);
347 INIT_LIST_HEAD(&r->res_root_list);
348 INIT_LIST_HEAD(&r->res_recover_list);
349
350 return r;
351}
352
353static int search_rsb_list(struct list_head *head, char *name, int len,
354 unsigned int flags, struct dlm_rsb **r_ret)
355{
356 struct dlm_rsb *r;
357 int error = 0;
358
359 list_for_each_entry(r, head, res_hashchain) {
360 if (len == r->res_length && !memcmp(name, r->res_name, len))
361 goto found;
362 }
18c60c0a 363 *r_ret = NULL;
597d0cae 364 return -EBADR;
e7fd4179
DT
365
366 found:
367 if (r->res_nodeid && (flags & R_MASTER))
368 error = -ENOTBLK;
369 *r_ret = r;
370 return error;
371}
372
373static int _search_rsb(struct dlm_ls *ls, char *name, int len, int b,
374 unsigned int flags, struct dlm_rsb **r_ret)
375{
376 struct dlm_rsb *r;
377 int error;
378
379 error = search_rsb_list(&ls->ls_rsbtbl[b].list, name, len, flags, &r);
380 if (!error) {
381 kref_get(&r->res_ref);
382 goto out;
383 }
384 error = search_rsb_list(&ls->ls_rsbtbl[b].toss, name, len, flags, &r);
385 if (error)
386 goto out;
387
388 list_move(&r->res_hashchain, &ls->ls_rsbtbl[b].list);
389
390 if (dlm_no_directory(ls))
391 goto out;
392
393 if (r->res_nodeid == -1) {
394 rsb_clear_flag(r, RSB_MASTER_UNCERTAIN);
395 r->res_first_lkid = 0;
396 } else if (r->res_nodeid > 0) {
397 rsb_set_flag(r, RSB_MASTER_UNCERTAIN);
398 r->res_first_lkid = 0;
399 } else {
400 DLM_ASSERT(r->res_nodeid == 0, dlm_print_rsb(r););
401 DLM_ASSERT(!rsb_flag(r, RSB_MASTER_UNCERTAIN),);
402 }
403 out:
404 *r_ret = r;
405 return error;
406}
407
408static int search_rsb(struct dlm_ls *ls, char *name, int len, int b,
409 unsigned int flags, struct dlm_rsb **r_ret)
410{
411 int error;
c7be761a 412 spin_lock(&ls->ls_rsbtbl[b].lock);
e7fd4179 413 error = _search_rsb(ls, name, len, b, flags, r_ret);
c7be761a 414 spin_unlock(&ls->ls_rsbtbl[b].lock);
e7fd4179
DT
415 return error;
416}
417
418/*
419 * Find rsb in rsbtbl and potentially create/add one
420 *
421 * Delaying the release of rsb's has a similar benefit to applications keeping
422 * NL locks on an rsb, but without the guarantee that the cached master value
423 * will still be valid when the rsb is reused. Apps aren't always smart enough
424 * to keep NL locks on an rsb that they may lock again shortly; this can lead
425 * to excessive master lookups and removals if we don't delay the release.
426 *
427 * Searching for an rsb means looking through both the normal list and toss
428 * list. When found on the toss list the rsb is moved to the normal list with
429 * ref count of 1; when found on normal list the ref count is incremented.
430 */
431
432static int find_rsb(struct dlm_ls *ls, char *name, int namelen,
433 unsigned int flags, struct dlm_rsb **r_ret)
434{
a566a6b1 435 struct dlm_rsb *r = NULL, *tmp;
e7fd4179 436 uint32_t hash, bucket;
ef58bcca
AV
437 int error = -EINVAL;
438
439 if (namelen > DLM_RESNAME_MAXLEN)
440 goto out;
e7fd4179
DT
441
442 if (dlm_no_directory(ls))
443 flags |= R_CREATE;
444
ef58bcca 445 error = 0;
e7fd4179
DT
446 hash = jhash(name, namelen, 0);
447 bucket = hash & (ls->ls_rsbtbl_size - 1);
448
449 error = search_rsb(ls, name, namelen, bucket, flags, &r);
450 if (!error)
451 goto out;
452
597d0cae 453 if (error == -EBADR && !(flags & R_CREATE))
e7fd4179
DT
454 goto out;
455
456 /* the rsb was found but wasn't a master copy */
457 if (error == -ENOTBLK)
458 goto out;
459
460 error = -ENOMEM;
461 r = create_rsb(ls, name, namelen);
462 if (!r)
463 goto out;
464
465 r->res_hash = hash;
466 r->res_bucket = bucket;
467 r->res_nodeid = -1;
468 kref_init(&r->res_ref);
469
470 /* With no directory, the master can be set immediately */
471 if (dlm_no_directory(ls)) {
472 int nodeid = dlm_dir_nodeid(r);
473 if (nodeid == dlm_our_nodeid())
474 nodeid = 0;
475 r->res_nodeid = nodeid;
476 }
477
c7be761a 478 spin_lock(&ls->ls_rsbtbl[bucket].lock);
e7fd4179
DT
479 error = _search_rsb(ls, name, namelen, bucket, 0, &tmp);
480 if (!error) {
c7be761a 481 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
52bda2b5 482 dlm_free_rsb(r);
e7fd4179
DT
483 r = tmp;
484 goto out;
485 }
486 list_add(&r->res_hashchain, &ls->ls_rsbtbl[bucket].list);
c7be761a 487 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
e7fd4179
DT
488 error = 0;
489 out:
490 *r_ret = r;
491 return error;
492}
493
e7fd4179
DT
494/* This is only called to add a reference when the code already holds
495 a valid reference to the rsb, so there's no need for locking. */
496
497static inline void hold_rsb(struct dlm_rsb *r)
498{
499 kref_get(&r->res_ref);
500}
501
502void dlm_hold_rsb(struct dlm_rsb *r)
503{
504 hold_rsb(r);
505}
506
507static void toss_rsb(struct kref *kref)
508{
509 struct dlm_rsb *r = container_of(kref, struct dlm_rsb, res_ref);
510 struct dlm_ls *ls = r->res_ls;
511
512 DLM_ASSERT(list_empty(&r->res_root_list), dlm_print_rsb(r););
513 kref_init(&r->res_ref);
514 list_move(&r->res_hashchain, &ls->ls_rsbtbl[r->res_bucket].toss);
515 r->res_toss_time = jiffies;
516 if (r->res_lvbptr) {
52bda2b5 517 dlm_free_lvb(r->res_lvbptr);
e7fd4179
DT
518 r->res_lvbptr = NULL;
519 }
520}
521
25985edc 522/* When all references to the rsb are gone it's transferred to
e7fd4179
DT
523 the tossed list for later disposal. */
524
525static void put_rsb(struct dlm_rsb *r)
526{
527 struct dlm_ls *ls = r->res_ls;
528 uint32_t bucket = r->res_bucket;
529
c7be761a 530 spin_lock(&ls->ls_rsbtbl[bucket].lock);
e7fd4179 531 kref_put(&r->res_ref, toss_rsb);
c7be761a 532 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
e7fd4179
DT
533}
534
535void dlm_put_rsb(struct dlm_rsb *r)
536{
537 put_rsb(r);
538}
539
540/* See comment for unhold_lkb */
541
542static void unhold_rsb(struct dlm_rsb *r)
543{
544 int rv;
545 rv = kref_put(&r->res_ref, toss_rsb);
a345da3e 546 DLM_ASSERT(!rv, dlm_dump_rsb(r););
e7fd4179
DT
547}
548
549static void kill_rsb(struct kref *kref)
550{
551 struct dlm_rsb *r = container_of(kref, struct dlm_rsb, res_ref);
552
553 /* All work is done after the return from kref_put() so we
554 can release the write_lock before the remove and free. */
555
a345da3e
DT
556 DLM_ASSERT(list_empty(&r->res_lookup), dlm_dump_rsb(r););
557 DLM_ASSERT(list_empty(&r->res_grantqueue), dlm_dump_rsb(r););
558 DLM_ASSERT(list_empty(&r->res_convertqueue), dlm_dump_rsb(r););
559 DLM_ASSERT(list_empty(&r->res_waitqueue), dlm_dump_rsb(r););
560 DLM_ASSERT(list_empty(&r->res_root_list), dlm_dump_rsb(r););
561 DLM_ASSERT(list_empty(&r->res_recover_list), dlm_dump_rsb(r););
e7fd4179
DT
562}
563
564/* Attaching/detaching lkb's from rsb's is for rsb reference counting.
565 The rsb must exist as long as any lkb's for it do. */
566
567static void attach_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb)
568{
569 hold_rsb(r);
570 lkb->lkb_resource = r;
571}
572
573static void detach_lkb(struct dlm_lkb *lkb)
574{
575 if (lkb->lkb_resource) {
576 put_rsb(lkb->lkb_resource);
577 lkb->lkb_resource = NULL;
578 }
579}
580
581static int create_lkb(struct dlm_ls *ls, struct dlm_lkb **lkb_ret)
582{
583 struct dlm_lkb *lkb, *tmp;
584 uint32_t lkid = 0;
585 uint16_t bucket;
586
52bda2b5 587 lkb = dlm_allocate_lkb(ls);
e7fd4179
DT
588 if (!lkb)
589 return -ENOMEM;
590
591 lkb->lkb_nodeid = -1;
592 lkb->lkb_grmode = DLM_LOCK_IV;
593 kref_init(&lkb->lkb_ref);
34e22bed 594 INIT_LIST_HEAD(&lkb->lkb_ownqueue);
ef0c2bb0 595 INIT_LIST_HEAD(&lkb->lkb_rsb_lookup);
3ae1acf9 596 INIT_LIST_HEAD(&lkb->lkb_time_list);
8304d6f2 597 INIT_LIST_HEAD(&lkb->lkb_astqueue);
e7fd4179
DT
598
599 get_random_bytes(&bucket, sizeof(bucket));
600 bucket &= (ls->ls_lkbtbl_size - 1);
601
602 write_lock(&ls->ls_lkbtbl[bucket].lock);
603
604 /* counter can roll over so we must verify lkid is not in use */
605
606 while (lkid == 0) {
ce03f12b 607 lkid = (bucket << 16) | ls->ls_lkbtbl[bucket].counter++;
e7fd4179
DT
608
609 list_for_each_entry(tmp, &ls->ls_lkbtbl[bucket].list,
610 lkb_idtbl_list) {
611 if (tmp->lkb_id != lkid)
612 continue;
613 lkid = 0;
614 break;
615 }
616 }
617
618 lkb->lkb_id = lkid;
619 list_add(&lkb->lkb_idtbl_list, &ls->ls_lkbtbl[bucket].list);
620 write_unlock(&ls->ls_lkbtbl[bucket].lock);
621
622 *lkb_ret = lkb;
623 return 0;
624}
625
626static struct dlm_lkb *__find_lkb(struct dlm_ls *ls, uint32_t lkid)
627{
e7fd4179 628 struct dlm_lkb *lkb;
ce03f12b 629 uint16_t bucket = (lkid >> 16);
e7fd4179
DT
630
631 list_for_each_entry(lkb, &ls->ls_lkbtbl[bucket].list, lkb_idtbl_list) {
632 if (lkb->lkb_id == lkid)
633 return lkb;
634 }
635 return NULL;
636}
637
638static int find_lkb(struct dlm_ls *ls, uint32_t lkid, struct dlm_lkb **lkb_ret)
639{
640 struct dlm_lkb *lkb;
ce03f12b 641 uint16_t bucket = (lkid >> 16);
e7fd4179
DT
642
643 if (bucket >= ls->ls_lkbtbl_size)
644 return -EBADSLT;
645
646 read_lock(&ls->ls_lkbtbl[bucket].lock);
647 lkb = __find_lkb(ls, lkid);
648 if (lkb)
649 kref_get(&lkb->lkb_ref);
650 read_unlock(&ls->ls_lkbtbl[bucket].lock);
651
652 *lkb_ret = lkb;
653 return lkb ? 0 : -ENOENT;
654}
655
656static void kill_lkb(struct kref *kref)
657{
658 struct dlm_lkb *lkb = container_of(kref, struct dlm_lkb, lkb_ref);
659
660 /* All work is done after the return from kref_put() so we
661 can release the write_lock before the detach_lkb */
662
663 DLM_ASSERT(!lkb->lkb_status, dlm_print_lkb(lkb););
664}
665
b3f58d8f
DT
666/* __put_lkb() is used when an lkb may not have an rsb attached to
667 it so we need to provide the lockspace explicitly */
668
669static int __put_lkb(struct dlm_ls *ls, struct dlm_lkb *lkb)
e7fd4179 670{
ce03f12b 671 uint16_t bucket = (lkb->lkb_id >> 16);
e7fd4179
DT
672
673 write_lock(&ls->ls_lkbtbl[bucket].lock);
674 if (kref_put(&lkb->lkb_ref, kill_lkb)) {
675 list_del(&lkb->lkb_idtbl_list);
676 write_unlock(&ls->ls_lkbtbl[bucket].lock);
677
678 detach_lkb(lkb);
679
680 /* for local/process lkbs, lvbptr points to caller's lksb */
681 if (lkb->lkb_lvbptr && is_master_copy(lkb))
52bda2b5
DT
682 dlm_free_lvb(lkb->lkb_lvbptr);
683 dlm_free_lkb(lkb);
e7fd4179
DT
684 return 1;
685 } else {
686 write_unlock(&ls->ls_lkbtbl[bucket].lock);
687 return 0;
688 }
689}
690
691int dlm_put_lkb(struct dlm_lkb *lkb)
692{
b3f58d8f
DT
693 struct dlm_ls *ls;
694
695 DLM_ASSERT(lkb->lkb_resource, dlm_print_lkb(lkb););
696 DLM_ASSERT(lkb->lkb_resource->res_ls, dlm_print_lkb(lkb););
697
698 ls = lkb->lkb_resource->res_ls;
699 return __put_lkb(ls, lkb);
e7fd4179
DT
700}
701
702/* This is only called to add a reference when the code already holds
703 a valid reference to the lkb, so there's no need for locking. */
704
705static inline void hold_lkb(struct dlm_lkb *lkb)
706{
707 kref_get(&lkb->lkb_ref);
708}
709
710/* This is called when we need to remove a reference and are certain
711 it's not the last ref. e.g. del_lkb is always called between a
712 find_lkb/put_lkb and is always the inverse of a previous add_lkb.
713 put_lkb would work fine, but would involve unnecessary locking */
714
715static inline void unhold_lkb(struct dlm_lkb *lkb)
716{
717 int rv;
718 rv = kref_put(&lkb->lkb_ref, kill_lkb);
719 DLM_ASSERT(!rv, dlm_print_lkb(lkb););
720}
721
722static void lkb_add_ordered(struct list_head *new, struct list_head *head,
723 int mode)
724{
725 struct dlm_lkb *lkb = NULL;
726
727 list_for_each_entry(lkb, head, lkb_statequeue)
728 if (lkb->lkb_rqmode < mode)
729 break;
730
99fb19d4 731 __list_add(new, lkb->lkb_statequeue.prev, &lkb->lkb_statequeue);
e7fd4179
DT
732}
733
734/* add/remove lkb to rsb's grant/convert/wait queue */
735
736static void add_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb, int status)
737{
738 kref_get(&lkb->lkb_ref);
739
740 DLM_ASSERT(!lkb->lkb_status, dlm_print_lkb(lkb););
741
eeda418d
DT
742 lkb->lkb_timestamp = ktime_get();
743
e7fd4179
DT
744 lkb->lkb_status = status;
745
746 switch (status) {
747 case DLM_LKSTS_WAITING:
748 if (lkb->lkb_exflags & DLM_LKF_HEADQUE)
749 list_add(&lkb->lkb_statequeue, &r->res_waitqueue);
750 else
751 list_add_tail(&lkb->lkb_statequeue, &r->res_waitqueue);
752 break;
753 case DLM_LKSTS_GRANTED:
754 /* convention says granted locks kept in order of grmode */
755 lkb_add_ordered(&lkb->lkb_statequeue, &r->res_grantqueue,
756 lkb->lkb_grmode);
757 break;
758 case DLM_LKSTS_CONVERT:
759 if (lkb->lkb_exflags & DLM_LKF_HEADQUE)
760 list_add(&lkb->lkb_statequeue, &r->res_convertqueue);
761 else
762 list_add_tail(&lkb->lkb_statequeue,
763 &r->res_convertqueue);
764 break;
765 default:
766 DLM_ASSERT(0, dlm_print_lkb(lkb); printk("sts=%d\n", status););
767 }
768}
769
770static void del_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb)
771{
772 lkb->lkb_status = 0;
773 list_del(&lkb->lkb_statequeue);
774 unhold_lkb(lkb);
775}
776
777static void move_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb, int sts)
778{
779 hold_lkb(lkb);
780 del_lkb(r, lkb);
781 add_lkb(r, lkb, sts);
782 unhold_lkb(lkb);
783}
784
ef0c2bb0
DT
785static int msg_reply_type(int mstype)
786{
787 switch (mstype) {
788 case DLM_MSG_REQUEST:
789 return DLM_MSG_REQUEST_REPLY;
790 case DLM_MSG_CONVERT:
791 return DLM_MSG_CONVERT_REPLY;
792 case DLM_MSG_UNLOCK:
793 return DLM_MSG_UNLOCK_REPLY;
794 case DLM_MSG_CANCEL:
795 return DLM_MSG_CANCEL_REPLY;
796 case DLM_MSG_LOOKUP:
797 return DLM_MSG_LOOKUP_REPLY;
798 }
799 return -1;
800}
801
c6ff669b
DT
802static int nodeid_warned(int nodeid, int num_nodes, int *warned)
803{
804 int i;
805
806 for (i = 0; i < num_nodes; i++) {
807 if (!warned[i]) {
808 warned[i] = nodeid;
809 return 0;
810 }
811 if (warned[i] == nodeid)
812 return 1;
813 }
814 return 0;
815}
816
817void dlm_scan_waiters(struct dlm_ls *ls)
818{
819 struct dlm_lkb *lkb;
820 ktime_t zero = ktime_set(0, 0);
821 s64 us;
822 s64 debug_maxus = 0;
823 u32 debug_scanned = 0;
824 u32 debug_expired = 0;
825 int num_nodes = 0;
826 int *warned = NULL;
827
828 if (!dlm_config.ci_waitwarn_us)
829 return;
830
831 mutex_lock(&ls->ls_waiters_mutex);
832
833 list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
834 if (ktime_equal(lkb->lkb_wait_time, zero))
835 continue;
836
837 debug_scanned++;
838
839 us = ktime_to_us(ktime_sub(ktime_get(), lkb->lkb_wait_time));
840
841 if (us < dlm_config.ci_waitwarn_us)
842 continue;
843
844 lkb->lkb_wait_time = zero;
845
846 debug_expired++;
847 if (us > debug_maxus)
848 debug_maxus = us;
849
850 if (!num_nodes) {
851 num_nodes = ls->ls_num_nodes;
5d70828a 852 warned = kzalloc(num_nodes * sizeof(int), GFP_KERNEL);
c6ff669b
DT
853 }
854 if (!warned)
855 continue;
856 if (nodeid_warned(lkb->lkb_wait_nodeid, num_nodes, warned))
857 continue;
858
859 log_error(ls, "waitwarn %x %lld %d us check connection to "
860 "node %d", lkb->lkb_id, (long long)us,
861 dlm_config.ci_waitwarn_us, lkb->lkb_wait_nodeid);
862 }
863 mutex_unlock(&ls->ls_waiters_mutex);
5d70828a 864 kfree(warned);
c6ff669b
DT
865
866 if (debug_expired)
867 log_debug(ls, "scan_waiters %u warn %u over %d us max %lld us",
868 debug_scanned, debug_expired,
869 dlm_config.ci_waitwarn_us, (long long)debug_maxus);
870}
871
e7fd4179
DT
872/* add/remove lkb from global waiters list of lkb's waiting for
873 a reply from a remote node */
874
c6ff669b 875static int add_to_waiters(struct dlm_lkb *lkb, int mstype, int to_nodeid)
e7fd4179
DT
876{
877 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
ef0c2bb0 878 int error = 0;
e7fd4179 879
90135925 880 mutex_lock(&ls->ls_waiters_mutex);
ef0c2bb0
DT
881
882 if (is_overlap_unlock(lkb) ||
883 (is_overlap_cancel(lkb) && (mstype == DLM_MSG_CANCEL))) {
884 error = -EINVAL;
885 goto out;
886 }
887
888 if (lkb->lkb_wait_type || is_overlap_cancel(lkb)) {
889 switch (mstype) {
890 case DLM_MSG_UNLOCK:
891 lkb->lkb_flags |= DLM_IFL_OVERLAP_UNLOCK;
892 break;
893 case DLM_MSG_CANCEL:
894 lkb->lkb_flags |= DLM_IFL_OVERLAP_CANCEL;
895 break;
896 default:
897 error = -EBUSY;
898 goto out;
899 }
900 lkb->lkb_wait_count++;
901 hold_lkb(lkb);
902
43279e53 903 log_debug(ls, "addwait %x cur %d overlap %d count %d f %x",
ef0c2bb0
DT
904 lkb->lkb_id, lkb->lkb_wait_type, mstype,
905 lkb->lkb_wait_count, lkb->lkb_flags);
e7fd4179
DT
906 goto out;
907 }
ef0c2bb0
DT
908
909 DLM_ASSERT(!lkb->lkb_wait_count,
910 dlm_print_lkb(lkb);
911 printk("wait_count %d\n", lkb->lkb_wait_count););
912
913 lkb->lkb_wait_count++;
e7fd4179 914 lkb->lkb_wait_type = mstype;
c6ff669b
DT
915 lkb->lkb_wait_time = ktime_get();
916 lkb->lkb_wait_nodeid = to_nodeid; /* for debugging */
ef0c2bb0 917 hold_lkb(lkb);
e7fd4179
DT
918 list_add(&lkb->lkb_wait_reply, &ls->ls_waiters);
919 out:
ef0c2bb0 920 if (error)
43279e53 921 log_error(ls, "addwait error %x %d flags %x %d %d %s",
ef0c2bb0
DT
922 lkb->lkb_id, error, lkb->lkb_flags, mstype,
923 lkb->lkb_wait_type, lkb->lkb_resource->res_name);
90135925 924 mutex_unlock(&ls->ls_waiters_mutex);
ef0c2bb0 925 return error;
e7fd4179
DT
926}
927
b790c3b7
DT
928/* We clear the RESEND flag because we might be taking an lkb off the waiters
929 list as part of process_requestqueue (e.g. a lookup that has an optimized
930 request reply on the requestqueue) between dlm_recover_waiters_pre() which
931 set RESEND and dlm_recover_waiters_post() */
932
43279e53
DT
933static int _remove_from_waiters(struct dlm_lkb *lkb, int mstype,
934 struct dlm_message *ms)
e7fd4179 935{
ef0c2bb0
DT
936 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
937 int overlap_done = 0;
e7fd4179 938
ef0c2bb0 939 if (is_overlap_unlock(lkb) && (mstype == DLM_MSG_UNLOCK_REPLY)) {
43279e53 940 log_debug(ls, "remwait %x unlock_reply overlap", lkb->lkb_id);
ef0c2bb0
DT
941 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
942 overlap_done = 1;
943 goto out_del;
e7fd4179 944 }
ef0c2bb0
DT
945
946 if (is_overlap_cancel(lkb) && (mstype == DLM_MSG_CANCEL_REPLY)) {
43279e53 947 log_debug(ls, "remwait %x cancel_reply overlap", lkb->lkb_id);
ef0c2bb0
DT
948 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
949 overlap_done = 1;
950 goto out_del;
951 }
952
43279e53
DT
953 /* Cancel state was preemptively cleared by a successful convert,
954 see next comment, nothing to do. */
955
956 if ((mstype == DLM_MSG_CANCEL_REPLY) &&
957 (lkb->lkb_wait_type != DLM_MSG_CANCEL)) {
958 log_debug(ls, "remwait %x cancel_reply wait_type %d",
959 lkb->lkb_id, lkb->lkb_wait_type);
960 return -1;
961 }
962
963 /* Remove for the convert reply, and premptively remove for the
964 cancel reply. A convert has been granted while there's still
965 an outstanding cancel on it (the cancel is moot and the result
966 in the cancel reply should be 0). We preempt the cancel reply
967 because the app gets the convert result and then can follow up
968 with another op, like convert. This subsequent op would see the
969 lingering state of the cancel and fail with -EBUSY. */
970
971 if ((mstype == DLM_MSG_CONVERT_REPLY) &&
972 (lkb->lkb_wait_type == DLM_MSG_CONVERT) &&
973 is_overlap_cancel(lkb) && ms && !ms->m_result) {
974 log_debug(ls, "remwait %x convert_reply zap overlap_cancel",
975 lkb->lkb_id);
976 lkb->lkb_wait_type = 0;
977 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
978 lkb->lkb_wait_count--;
979 goto out_del;
980 }
981
ef0c2bb0
DT
982 /* N.B. type of reply may not always correspond to type of original
983 msg due to lookup->request optimization, verify others? */
984
985 if (lkb->lkb_wait_type) {
986 lkb->lkb_wait_type = 0;
987 goto out_del;
988 }
989
43279e53
DT
990 log_error(ls, "remwait error %x reply %d flags %x no wait_type",
991 lkb->lkb_id, mstype, lkb->lkb_flags);
ef0c2bb0
DT
992 return -1;
993
994 out_del:
995 /* the force-unlock/cancel has completed and we haven't recvd a reply
996 to the op that was in progress prior to the unlock/cancel; we
997 give up on any reply to the earlier op. FIXME: not sure when/how
998 this would happen */
999
1000 if (overlap_done && lkb->lkb_wait_type) {
43279e53 1001 log_error(ls, "remwait error %x reply %d wait_type %d overlap",
ef0c2bb0
DT
1002 lkb->lkb_id, mstype, lkb->lkb_wait_type);
1003 lkb->lkb_wait_count--;
1004 lkb->lkb_wait_type = 0;
1005 }
1006
1007 DLM_ASSERT(lkb->lkb_wait_count, dlm_print_lkb(lkb););
1008
b790c3b7 1009 lkb->lkb_flags &= ~DLM_IFL_RESEND;
ef0c2bb0
DT
1010 lkb->lkb_wait_count--;
1011 if (!lkb->lkb_wait_count)
1012 list_del_init(&lkb->lkb_wait_reply);
e7fd4179 1013 unhold_lkb(lkb);
ef0c2bb0 1014 return 0;
e7fd4179
DT
1015}
1016
ef0c2bb0 1017static int remove_from_waiters(struct dlm_lkb *lkb, int mstype)
e7fd4179
DT
1018{
1019 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1020 int error;
1021
90135925 1022 mutex_lock(&ls->ls_waiters_mutex);
43279e53 1023 error = _remove_from_waiters(lkb, mstype, NULL);
90135925 1024 mutex_unlock(&ls->ls_waiters_mutex);
e7fd4179
DT
1025 return error;
1026}
1027
ef0c2bb0
DT
1028/* Handles situations where we might be processing a "fake" or "stub" reply in
1029 which we can't try to take waiters_mutex again. */
1030
1031static int remove_from_waiters_ms(struct dlm_lkb *lkb, struct dlm_message *ms)
1032{
1033 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1034 int error;
1035
2a7ce0ed 1036 if (ms->m_flags != DLM_IFL_STUB_MS)
ef0c2bb0 1037 mutex_lock(&ls->ls_waiters_mutex);
43279e53 1038 error = _remove_from_waiters(lkb, ms->m_type, ms);
2a7ce0ed 1039 if (ms->m_flags != DLM_IFL_STUB_MS)
ef0c2bb0
DT
1040 mutex_unlock(&ls->ls_waiters_mutex);
1041 return error;
1042}
1043
e7fd4179
DT
1044static void dir_remove(struct dlm_rsb *r)
1045{
1046 int to_nodeid;
1047
1048 if (dlm_no_directory(r->res_ls))
1049 return;
1050
1051 to_nodeid = dlm_dir_nodeid(r);
1052 if (to_nodeid != dlm_our_nodeid())
1053 send_remove(r);
1054 else
1055 dlm_dir_remove_entry(r->res_ls, to_nodeid,
1056 r->res_name, r->res_length);
1057}
1058
1059/* FIXME: shouldn't this be able to exit as soon as one non-due rsb is
1060 found since they are in order of newest to oldest? */
1061
1062static int shrink_bucket(struct dlm_ls *ls, int b)
1063{
1064 struct dlm_rsb *r;
1065 int count = 0, found;
1066
1067 for (;;) {
90135925 1068 found = 0;
c7be761a 1069 spin_lock(&ls->ls_rsbtbl[b].lock);
e7fd4179
DT
1070 list_for_each_entry_reverse(r, &ls->ls_rsbtbl[b].toss,
1071 res_hashchain) {
1072 if (!time_after_eq(jiffies, r->res_toss_time +
68c817a1 1073 dlm_config.ci_toss_secs * HZ))
e7fd4179 1074 continue;
90135925 1075 found = 1;
e7fd4179
DT
1076 break;
1077 }
1078
1079 if (!found) {
c7be761a 1080 spin_unlock(&ls->ls_rsbtbl[b].lock);
e7fd4179
DT
1081 break;
1082 }
1083
1084 if (kref_put(&r->res_ref, kill_rsb)) {
1085 list_del(&r->res_hashchain);
c7be761a 1086 spin_unlock(&ls->ls_rsbtbl[b].lock);
e7fd4179
DT
1087
1088 if (is_master(r))
1089 dir_remove(r);
52bda2b5 1090 dlm_free_rsb(r);
e7fd4179
DT
1091 count++;
1092 } else {
c7be761a 1093 spin_unlock(&ls->ls_rsbtbl[b].lock);
e7fd4179
DT
1094 log_error(ls, "tossed rsb in use %s", r->res_name);
1095 }
1096 }
1097
1098 return count;
1099}
1100
1101void dlm_scan_rsbs(struct dlm_ls *ls)
1102{
1103 int i;
1104
e7fd4179
DT
1105 for (i = 0; i < ls->ls_rsbtbl_size; i++) {
1106 shrink_bucket(ls, i);
85e86edf
DT
1107 if (dlm_locking_stopped(ls))
1108 break;
e7fd4179
DT
1109 cond_resched();
1110 }
1111}
1112
3ae1acf9
DT
1113static void add_timeout(struct dlm_lkb *lkb)
1114{
1115 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1116
eeda418d 1117 if (is_master_copy(lkb))
3ae1acf9 1118 return;
3ae1acf9
DT
1119
1120 if (test_bit(LSFL_TIMEWARN, &ls->ls_flags) &&
1121 !(lkb->lkb_exflags & DLM_LKF_NODLCKWT)) {
1122 lkb->lkb_flags |= DLM_IFL_WATCH_TIMEWARN;
1123 goto add_it;
1124 }
84d8cd69
DT
1125 if (lkb->lkb_exflags & DLM_LKF_TIMEOUT)
1126 goto add_it;
3ae1acf9
DT
1127 return;
1128
1129 add_it:
1130 DLM_ASSERT(list_empty(&lkb->lkb_time_list), dlm_print_lkb(lkb););
1131 mutex_lock(&ls->ls_timeout_mutex);
1132 hold_lkb(lkb);
3ae1acf9
DT
1133 list_add_tail(&lkb->lkb_time_list, &ls->ls_timeout);
1134 mutex_unlock(&ls->ls_timeout_mutex);
1135}
1136
1137static void del_timeout(struct dlm_lkb *lkb)
1138{
1139 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1140
1141 mutex_lock(&ls->ls_timeout_mutex);
1142 if (!list_empty(&lkb->lkb_time_list)) {
1143 list_del_init(&lkb->lkb_time_list);
1144 unhold_lkb(lkb);
1145 }
1146 mutex_unlock(&ls->ls_timeout_mutex);
1147}
1148
1149/* FIXME: is it safe to look at lkb_exflags, lkb_flags, lkb_timestamp, and
1150 lkb_lksb_timeout without lock_rsb? Note: we can't lock timeout_mutex
1151 and then lock rsb because of lock ordering in add_timeout. We may need
1152 to specify some special timeout-related bits in the lkb that are just to
1153 be accessed under the timeout_mutex. */
1154
1155void dlm_scan_timeout(struct dlm_ls *ls)
1156{
1157 struct dlm_rsb *r;
1158 struct dlm_lkb *lkb;
1159 int do_cancel, do_warn;
eeda418d 1160 s64 wait_us;
3ae1acf9
DT
1161
1162 for (;;) {
1163 if (dlm_locking_stopped(ls))
1164 break;
1165
1166 do_cancel = 0;
1167 do_warn = 0;
1168 mutex_lock(&ls->ls_timeout_mutex);
1169 list_for_each_entry(lkb, &ls->ls_timeout, lkb_time_list) {
1170
eeda418d
DT
1171 wait_us = ktime_to_us(ktime_sub(ktime_get(),
1172 lkb->lkb_timestamp));
1173
3ae1acf9 1174 if ((lkb->lkb_exflags & DLM_LKF_TIMEOUT) &&
eeda418d 1175 wait_us >= (lkb->lkb_timeout_cs * 10000))
3ae1acf9
DT
1176 do_cancel = 1;
1177
1178 if ((lkb->lkb_flags & DLM_IFL_WATCH_TIMEWARN) &&
eeda418d 1179 wait_us >= dlm_config.ci_timewarn_cs * 10000)
3ae1acf9
DT
1180 do_warn = 1;
1181
1182 if (!do_cancel && !do_warn)
1183 continue;
1184 hold_lkb(lkb);
1185 break;
1186 }
1187 mutex_unlock(&ls->ls_timeout_mutex);
1188
1189 if (!do_cancel && !do_warn)
1190 break;
1191
1192 r = lkb->lkb_resource;
1193 hold_rsb(r);
1194 lock_rsb(r);
1195
1196 if (do_warn) {
1197 /* clear flag so we only warn once */
1198 lkb->lkb_flags &= ~DLM_IFL_WATCH_TIMEWARN;
1199 if (!(lkb->lkb_exflags & DLM_LKF_TIMEOUT))
1200 del_timeout(lkb);
1201 dlm_timeout_warn(lkb);
1202 }
1203
1204 if (do_cancel) {
b3cab7b9 1205 log_debug(ls, "timeout cancel %x node %d %s",
639aca41 1206 lkb->lkb_id, lkb->lkb_nodeid, r->res_name);
3ae1acf9
DT
1207 lkb->lkb_flags &= ~DLM_IFL_WATCH_TIMEWARN;
1208 lkb->lkb_flags |= DLM_IFL_TIMEOUT_CANCEL;
1209 del_timeout(lkb);
1210 _cancel_lock(r, lkb);
1211 }
1212
1213 unlock_rsb(r);
1214 unhold_rsb(r);
1215 dlm_put_lkb(lkb);
1216 }
1217}
1218
1219/* This is only called by dlm_recoverd, and we rely on dlm_ls_stop() stopping
1220 dlm_recoverd before checking/setting ls_recover_begin. */
1221
1222void dlm_adjust_timeouts(struct dlm_ls *ls)
1223{
1224 struct dlm_lkb *lkb;
eeda418d 1225 u64 adj_us = jiffies_to_usecs(jiffies - ls->ls_recover_begin);
3ae1acf9
DT
1226
1227 ls->ls_recover_begin = 0;
1228 mutex_lock(&ls->ls_timeout_mutex);
1229 list_for_each_entry(lkb, &ls->ls_timeout, lkb_time_list)
eeda418d 1230 lkb->lkb_timestamp = ktime_add_us(lkb->lkb_timestamp, adj_us);
3ae1acf9 1231 mutex_unlock(&ls->ls_timeout_mutex);
c6ff669b
DT
1232
1233 if (!dlm_config.ci_waitwarn_us)
1234 return;
1235
1236 mutex_lock(&ls->ls_waiters_mutex);
1237 list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
1238 if (ktime_to_us(lkb->lkb_wait_time))
1239 lkb->lkb_wait_time = ktime_get();
1240 }
1241 mutex_unlock(&ls->ls_waiters_mutex);
3ae1acf9
DT
1242}
1243
e7fd4179
DT
1244/* lkb is master or local copy */
1245
1246static void set_lvb_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1247{
1248 int b, len = r->res_ls->ls_lvblen;
1249
1250 /* b=1 lvb returned to caller
1251 b=0 lvb written to rsb or invalidated
1252 b=-1 do nothing */
1253
1254 b = dlm_lvb_operations[lkb->lkb_grmode + 1][lkb->lkb_rqmode + 1];
1255
1256 if (b == 1) {
1257 if (!lkb->lkb_lvbptr)
1258 return;
1259
1260 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1261 return;
1262
1263 if (!r->res_lvbptr)
1264 return;
1265
1266 memcpy(lkb->lkb_lvbptr, r->res_lvbptr, len);
1267 lkb->lkb_lvbseq = r->res_lvbseq;
1268
1269 } else if (b == 0) {
1270 if (lkb->lkb_exflags & DLM_LKF_IVVALBLK) {
1271 rsb_set_flag(r, RSB_VALNOTVALID);
1272 return;
1273 }
1274
1275 if (!lkb->lkb_lvbptr)
1276 return;
1277
1278 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1279 return;
1280
1281 if (!r->res_lvbptr)
52bda2b5 1282 r->res_lvbptr = dlm_allocate_lvb(r->res_ls);
e7fd4179
DT
1283
1284 if (!r->res_lvbptr)
1285 return;
1286
1287 memcpy(r->res_lvbptr, lkb->lkb_lvbptr, len);
1288 r->res_lvbseq++;
1289 lkb->lkb_lvbseq = r->res_lvbseq;
1290 rsb_clear_flag(r, RSB_VALNOTVALID);
1291 }
1292
1293 if (rsb_flag(r, RSB_VALNOTVALID))
1294 lkb->lkb_sbflags |= DLM_SBF_VALNOTVALID;
1295}
1296
1297static void set_lvb_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1298{
1299 if (lkb->lkb_grmode < DLM_LOCK_PW)
1300 return;
1301
1302 if (lkb->lkb_exflags & DLM_LKF_IVVALBLK) {
1303 rsb_set_flag(r, RSB_VALNOTVALID);
1304 return;
1305 }
1306
1307 if (!lkb->lkb_lvbptr)
1308 return;
1309
1310 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1311 return;
1312
1313 if (!r->res_lvbptr)
52bda2b5 1314 r->res_lvbptr = dlm_allocate_lvb(r->res_ls);
e7fd4179
DT
1315
1316 if (!r->res_lvbptr)
1317 return;
1318
1319 memcpy(r->res_lvbptr, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
1320 r->res_lvbseq++;
1321 rsb_clear_flag(r, RSB_VALNOTVALID);
1322}
1323
1324/* lkb is process copy (pc) */
1325
1326static void set_lvb_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb,
1327 struct dlm_message *ms)
1328{
1329 int b;
1330
1331 if (!lkb->lkb_lvbptr)
1332 return;
1333
1334 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1335 return;
1336
597d0cae 1337 b = dlm_lvb_operations[lkb->lkb_grmode + 1][lkb->lkb_rqmode + 1];
e7fd4179
DT
1338 if (b == 1) {
1339 int len = receive_extralen(ms);
a9cc9159
AV
1340 if (len > DLM_RESNAME_MAXLEN)
1341 len = DLM_RESNAME_MAXLEN;
e7fd4179
DT
1342 memcpy(lkb->lkb_lvbptr, ms->m_extra, len);
1343 lkb->lkb_lvbseq = ms->m_lvbseq;
1344 }
1345}
1346
1347/* Manipulate lkb's on rsb's convert/granted/waiting queues
1348 remove_lock -- used for unlock, removes lkb from granted
1349 revert_lock -- used for cancel, moves lkb from convert to granted
1350 grant_lock -- used for request and convert, adds lkb to granted or
1351 moves lkb from convert or waiting to granted
1352
1353 Each of these is used for master or local copy lkb's. There is
1354 also a _pc() variation used to make the corresponding change on
1355 a process copy (pc) lkb. */
1356
1357static void _remove_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1358{
1359 del_lkb(r, lkb);
1360 lkb->lkb_grmode = DLM_LOCK_IV;
1361 /* this unhold undoes the original ref from create_lkb()
1362 so this leads to the lkb being freed */
1363 unhold_lkb(lkb);
1364}
1365
1366static void remove_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1367{
1368 set_lvb_unlock(r, lkb);
1369 _remove_lock(r, lkb);
1370}
1371
1372static void remove_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb)
1373{
1374 _remove_lock(r, lkb);
1375}
1376
ef0c2bb0
DT
1377/* returns: 0 did nothing
1378 1 moved lock to granted
1379 -1 removed lock */
1380
1381static int revert_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
e7fd4179 1382{
ef0c2bb0
DT
1383 int rv = 0;
1384
e7fd4179
DT
1385 lkb->lkb_rqmode = DLM_LOCK_IV;
1386
1387 switch (lkb->lkb_status) {
597d0cae
DT
1388 case DLM_LKSTS_GRANTED:
1389 break;
e7fd4179
DT
1390 case DLM_LKSTS_CONVERT:
1391 move_lkb(r, lkb, DLM_LKSTS_GRANTED);
ef0c2bb0 1392 rv = 1;
e7fd4179
DT
1393 break;
1394 case DLM_LKSTS_WAITING:
1395 del_lkb(r, lkb);
1396 lkb->lkb_grmode = DLM_LOCK_IV;
1397 /* this unhold undoes the original ref from create_lkb()
1398 so this leads to the lkb being freed */
1399 unhold_lkb(lkb);
ef0c2bb0 1400 rv = -1;
e7fd4179
DT
1401 break;
1402 default:
1403 log_print("invalid status for revert %d", lkb->lkb_status);
1404 }
ef0c2bb0 1405 return rv;
e7fd4179
DT
1406}
1407
ef0c2bb0 1408static int revert_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb)
e7fd4179 1409{
ef0c2bb0 1410 return revert_lock(r, lkb);
e7fd4179
DT
1411}
1412
1413static void _grant_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1414{
1415 if (lkb->lkb_grmode != lkb->lkb_rqmode) {
1416 lkb->lkb_grmode = lkb->lkb_rqmode;
1417 if (lkb->lkb_status)
1418 move_lkb(r, lkb, DLM_LKSTS_GRANTED);
1419 else
1420 add_lkb(r, lkb, DLM_LKSTS_GRANTED);
1421 }
1422
1423 lkb->lkb_rqmode = DLM_LOCK_IV;
e7fd4179
DT
1424}
1425
1426static void grant_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1427{
1428 set_lvb_lock(r, lkb);
1429 _grant_lock(r, lkb);
1430 lkb->lkb_highbast = 0;
1431}
1432
1433static void grant_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb,
1434 struct dlm_message *ms)
1435{
1436 set_lvb_lock_pc(r, lkb, ms);
1437 _grant_lock(r, lkb);
1438}
1439
1440/* called by grant_pending_locks() which means an async grant message must
1441 be sent to the requesting node in addition to granting the lock if the
1442 lkb belongs to a remote node. */
1443
1444static void grant_lock_pending(struct dlm_rsb *r, struct dlm_lkb *lkb)
1445{
1446 grant_lock(r, lkb);
1447 if (is_master_copy(lkb))
1448 send_grant(r, lkb);
1449 else
1450 queue_cast(r, lkb, 0);
1451}
1452
7d3c1feb
DT
1453/* The special CONVDEADLK, ALTPR and ALTCW flags allow the master to
1454 change the granted/requested modes. We're munging things accordingly in
1455 the process copy.
1456 CONVDEADLK: our grmode may have been forced down to NL to resolve a
1457 conversion deadlock
1458 ALTPR/ALTCW: our rqmode may have been changed to PR or CW to become
1459 compatible with other granted locks */
1460
2a7ce0ed 1461static void munge_demoted(struct dlm_lkb *lkb)
7d3c1feb 1462{
7d3c1feb
DT
1463 if (lkb->lkb_rqmode == DLM_LOCK_IV || lkb->lkb_grmode == DLM_LOCK_IV) {
1464 log_print("munge_demoted %x invalid modes gr %d rq %d",
1465 lkb->lkb_id, lkb->lkb_grmode, lkb->lkb_rqmode);
1466 return;
1467 }
1468
1469 lkb->lkb_grmode = DLM_LOCK_NL;
1470}
1471
1472static void munge_altmode(struct dlm_lkb *lkb, struct dlm_message *ms)
1473{
1474 if (ms->m_type != DLM_MSG_REQUEST_REPLY &&
1475 ms->m_type != DLM_MSG_GRANT) {
1476 log_print("munge_altmode %x invalid reply type %d",
1477 lkb->lkb_id, ms->m_type);
1478 return;
1479 }
1480
1481 if (lkb->lkb_exflags & DLM_LKF_ALTPR)
1482 lkb->lkb_rqmode = DLM_LOCK_PR;
1483 else if (lkb->lkb_exflags & DLM_LKF_ALTCW)
1484 lkb->lkb_rqmode = DLM_LOCK_CW;
1485 else {
1486 log_print("munge_altmode invalid exflags %x", lkb->lkb_exflags);
1487 dlm_print_lkb(lkb);
1488 }
1489}
1490
e7fd4179
DT
1491static inline int first_in_list(struct dlm_lkb *lkb, struct list_head *head)
1492{
1493 struct dlm_lkb *first = list_entry(head->next, struct dlm_lkb,
1494 lkb_statequeue);
1495 if (lkb->lkb_id == first->lkb_id)
90135925 1496 return 1;
e7fd4179 1497
90135925 1498 return 0;
e7fd4179
DT
1499}
1500
e7fd4179
DT
1501/* Check if the given lkb conflicts with another lkb on the queue. */
1502
1503static int queue_conflict(struct list_head *head, struct dlm_lkb *lkb)
1504{
1505 struct dlm_lkb *this;
1506
1507 list_for_each_entry(this, head, lkb_statequeue) {
1508 if (this == lkb)
1509 continue;
3bcd3687 1510 if (!modes_compat(this, lkb))
90135925 1511 return 1;
e7fd4179 1512 }
90135925 1513 return 0;
e7fd4179
DT
1514}
1515
1516/*
1517 * "A conversion deadlock arises with a pair of lock requests in the converting
1518 * queue for one resource. The granted mode of each lock blocks the requested
1519 * mode of the other lock."
1520 *
c85d65e9
DT
1521 * Part 2: if the granted mode of lkb is preventing an earlier lkb in the
1522 * convert queue from being granted, then deadlk/demote lkb.
e7fd4179
DT
1523 *
1524 * Example:
1525 * Granted Queue: empty
1526 * Convert Queue: NL->EX (first lock)
1527 * PR->EX (second lock)
1528 *
1529 * The first lock can't be granted because of the granted mode of the second
1530 * lock and the second lock can't be granted because it's not first in the
c85d65e9
DT
1531 * list. We either cancel lkb's conversion (PR->EX) and return EDEADLK, or we
1532 * demote the granted mode of lkb (from PR to NL) if it has the CONVDEADLK
1533 * flag set and return DEMOTED in the lksb flags.
e7fd4179 1534 *
c85d65e9
DT
1535 * Originally, this function detected conv-deadlk in a more limited scope:
1536 * - if !modes_compat(lkb1, lkb2) && !modes_compat(lkb2, lkb1), or
1537 * - if lkb1 was the first entry in the queue (not just earlier), and was
1538 * blocked by the granted mode of lkb2, and there was nothing on the
1539 * granted queue preventing lkb1 from being granted immediately, i.e.
1540 * lkb2 was the only thing preventing lkb1 from being granted.
1541 *
1542 * That second condition meant we'd only say there was conv-deadlk if
1543 * resolving it (by demotion) would lead to the first lock on the convert
1544 * queue being granted right away. It allowed conversion deadlocks to exist
1545 * between locks on the convert queue while they couldn't be granted anyway.
1546 *
1547 * Now, we detect and take action on conversion deadlocks immediately when
1548 * they're created, even if they may not be immediately consequential. If
1549 * lkb1 exists anywhere in the convert queue and lkb2 comes in with a granted
1550 * mode that would prevent lkb1's conversion from being granted, we do a
1551 * deadlk/demote on lkb2 right away and don't let it onto the convert queue.
1552 * I think this means that the lkb_is_ahead condition below should always
1553 * be zero, i.e. there will never be conv-deadlk between two locks that are
1554 * both already on the convert queue.
e7fd4179
DT
1555 */
1556
c85d65e9 1557static int conversion_deadlock_detect(struct dlm_rsb *r, struct dlm_lkb *lkb2)
e7fd4179 1558{
c85d65e9
DT
1559 struct dlm_lkb *lkb1;
1560 int lkb_is_ahead = 0;
e7fd4179 1561
c85d65e9
DT
1562 list_for_each_entry(lkb1, &r->res_convertqueue, lkb_statequeue) {
1563 if (lkb1 == lkb2) {
1564 lkb_is_ahead = 1;
e7fd4179
DT
1565 continue;
1566 }
1567
c85d65e9
DT
1568 if (!lkb_is_ahead) {
1569 if (!modes_compat(lkb2, lkb1))
1570 return 1;
1571 } else {
1572 if (!modes_compat(lkb2, lkb1) &&
1573 !modes_compat(lkb1, lkb2))
1574 return 1;
1575 }
e7fd4179 1576 }
90135925 1577 return 0;
e7fd4179
DT
1578}
1579
1580/*
1581 * Return 1 if the lock can be granted, 0 otherwise.
1582 * Also detect and resolve conversion deadlocks.
1583 *
1584 * lkb is the lock to be granted
1585 *
1586 * now is 1 if the function is being called in the context of the
1587 * immediate request, it is 0 if called later, after the lock has been
1588 * queued.
1589 *
1590 * References are from chapter 6 of "VAXcluster Principles" by Roy Davis
1591 */
1592
1593static int _can_be_granted(struct dlm_rsb *r, struct dlm_lkb *lkb, int now)
1594{
1595 int8_t conv = (lkb->lkb_grmode != DLM_LOCK_IV);
1596
1597 /*
1598 * 6-10: Version 5.4 introduced an option to address the phenomenon of
1599 * a new request for a NL mode lock being blocked.
1600 *
1601 * 6-11: If the optional EXPEDITE flag is used with the new NL mode
1602 * request, then it would be granted. In essence, the use of this flag
1603 * tells the Lock Manager to expedite theis request by not considering
1604 * what may be in the CONVERTING or WAITING queues... As of this
1605 * writing, the EXPEDITE flag can be used only with new requests for NL
1606 * mode locks. This flag is not valid for conversion requests.
1607 *
1608 * A shortcut. Earlier checks return an error if EXPEDITE is used in a
1609 * conversion or used with a non-NL requested mode. We also know an
1610 * EXPEDITE request is always granted immediately, so now must always
1611 * be 1. The full condition to grant an expedite request: (now &&
1612 * !conv && lkb->rqmode == DLM_LOCK_NL && (flags & EXPEDITE)) can
1613 * therefore be shortened to just checking the flag.
1614 */
1615
1616 if (lkb->lkb_exflags & DLM_LKF_EXPEDITE)
90135925 1617 return 1;
e7fd4179
DT
1618
1619 /*
1620 * A shortcut. Without this, !queue_conflict(grantqueue, lkb) would be
1621 * added to the remaining conditions.
1622 */
1623
1624 if (queue_conflict(&r->res_grantqueue, lkb))
1625 goto out;
1626
1627 /*
1628 * 6-3: By default, a conversion request is immediately granted if the
1629 * requested mode is compatible with the modes of all other granted
1630 * locks
1631 */
1632
1633 if (queue_conflict(&r->res_convertqueue, lkb))
1634 goto out;
1635
1636 /*
1637 * 6-5: But the default algorithm for deciding whether to grant or
1638 * queue conversion requests does not by itself guarantee that such
1639 * requests are serviced on a "first come first serve" basis. This, in
1640 * turn, can lead to a phenomenon known as "indefinate postponement".
1641 *
1642 * 6-7: This issue is dealt with by using the optional QUECVT flag with
1643 * the system service employed to request a lock conversion. This flag
1644 * forces certain conversion requests to be queued, even if they are
1645 * compatible with the granted modes of other locks on the same
1646 * resource. Thus, the use of this flag results in conversion requests
1647 * being ordered on a "first come first servce" basis.
1648 *
1649 * DCT: This condition is all about new conversions being able to occur
1650 * "in place" while the lock remains on the granted queue (assuming
1651 * nothing else conflicts.) IOW if QUECVT isn't set, a conversion
1652 * doesn't _have_ to go onto the convert queue where it's processed in
1653 * order. The "now" variable is necessary to distinguish converts
1654 * being received and processed for the first time now, because once a
1655 * convert is moved to the conversion queue the condition below applies
1656 * requiring fifo granting.
1657 */
1658
1659 if (now && conv && !(lkb->lkb_exflags & DLM_LKF_QUECVT))
90135925 1660 return 1;
e7fd4179
DT
1661
1662 /*
3bcd3687
DT
1663 * The NOORDER flag is set to avoid the standard vms rules on grant
1664 * order.
e7fd4179
DT
1665 */
1666
1667 if (lkb->lkb_exflags & DLM_LKF_NOORDER)
90135925 1668 return 1;
e7fd4179
DT
1669
1670 /*
1671 * 6-3: Once in that queue [CONVERTING], a conversion request cannot be
1672 * granted until all other conversion requests ahead of it are granted
1673 * and/or canceled.
1674 */
1675
1676 if (!now && conv && first_in_list(lkb, &r->res_convertqueue))
90135925 1677 return 1;
e7fd4179
DT
1678
1679 /*
1680 * 6-4: By default, a new request is immediately granted only if all
1681 * three of the following conditions are satisfied when the request is
1682 * issued:
1683 * - The queue of ungranted conversion requests for the resource is
1684 * empty.
1685 * - The queue of ungranted new requests for the resource is empty.
1686 * - The mode of the new request is compatible with the most
1687 * restrictive mode of all granted locks on the resource.
1688 */
1689
1690 if (now && !conv && list_empty(&r->res_convertqueue) &&
1691 list_empty(&r->res_waitqueue))
90135925 1692 return 1;
e7fd4179
DT
1693
1694 /*
1695 * 6-4: Once a lock request is in the queue of ungranted new requests,
1696 * it cannot be granted until the queue of ungranted conversion
1697 * requests is empty, all ungranted new requests ahead of it are
1698 * granted and/or canceled, and it is compatible with the granted mode
1699 * of the most restrictive lock granted on the resource.
1700 */
1701
1702 if (!now && !conv && list_empty(&r->res_convertqueue) &&
1703 first_in_list(lkb, &r->res_waitqueue))
90135925 1704 return 1;
e7fd4179 1705 out:
90135925 1706 return 0;
e7fd4179
DT
1707}
1708
c85d65e9
DT
1709static int can_be_granted(struct dlm_rsb *r, struct dlm_lkb *lkb, int now,
1710 int *err)
e7fd4179 1711{
e7fd4179
DT
1712 int rv;
1713 int8_t alt = 0, rqmode = lkb->lkb_rqmode;
c85d65e9
DT
1714 int8_t is_convert = (lkb->lkb_grmode != DLM_LOCK_IV);
1715
1716 if (err)
1717 *err = 0;
e7fd4179
DT
1718
1719 rv = _can_be_granted(r, lkb, now);
1720 if (rv)
1721 goto out;
1722
c85d65e9
DT
1723 /*
1724 * The CONVDEADLK flag is non-standard and tells the dlm to resolve
1725 * conversion deadlocks by demoting grmode to NL, otherwise the dlm
1726 * cancels one of the locks.
1727 */
1728
1729 if (is_convert && can_be_queued(lkb) &&
1730 conversion_deadlock_detect(r, lkb)) {
1731 if (lkb->lkb_exflags & DLM_LKF_CONVDEADLK) {
1732 lkb->lkb_grmode = DLM_LOCK_NL;
1733 lkb->lkb_sbflags |= DLM_SBF_DEMOTED;
1734 } else if (!(lkb->lkb_exflags & DLM_LKF_NODLCKWT)) {
1735 if (err)
1736 *err = -EDEADLK;
1737 else {
1738 log_print("can_be_granted deadlock %x now %d",
1739 lkb->lkb_id, now);
1740 dlm_dump_rsb(r);
1741 }
1742 }
e7fd4179 1743 goto out;
c85d65e9 1744 }
e7fd4179 1745
c85d65e9
DT
1746 /*
1747 * The ALTPR and ALTCW flags are non-standard and tell the dlm to try
1748 * to grant a request in a mode other than the normal rqmode. It's a
1749 * simple way to provide a big optimization to applications that can
1750 * use them.
1751 */
1752
1753 if (rqmode != DLM_LOCK_PR && (lkb->lkb_exflags & DLM_LKF_ALTPR))
e7fd4179 1754 alt = DLM_LOCK_PR;
c85d65e9 1755 else if (rqmode != DLM_LOCK_CW && (lkb->lkb_exflags & DLM_LKF_ALTCW))
e7fd4179
DT
1756 alt = DLM_LOCK_CW;
1757
1758 if (alt) {
1759 lkb->lkb_rqmode = alt;
1760 rv = _can_be_granted(r, lkb, now);
1761 if (rv)
1762 lkb->lkb_sbflags |= DLM_SBF_ALTMODE;
1763 else
1764 lkb->lkb_rqmode = rqmode;
1765 }
1766 out:
1767 return rv;
1768}
1769
c85d65e9
DT
1770/* FIXME: I don't think that can_be_granted() can/will demote or find deadlock
1771 for locks pending on the convert list. Once verified (watch for these
1772 log_prints), we should be able to just call _can_be_granted() and not
1773 bother with the demote/deadlk cases here (and there's no easy way to deal
1774 with a deadlk here, we'd have to generate something like grant_lock with
1775 the deadlk error.) */
1776
36509258
DT
1777/* Returns the highest requested mode of all blocked conversions; sets
1778 cw if there's a blocked conversion to DLM_LOCK_CW. */
c85d65e9 1779
36509258 1780static int grant_pending_convert(struct dlm_rsb *r, int high, int *cw)
e7fd4179
DT
1781{
1782 struct dlm_lkb *lkb, *s;
1783 int hi, demoted, quit, grant_restart, demote_restart;
c85d65e9 1784 int deadlk;
e7fd4179
DT
1785
1786 quit = 0;
1787 restart:
1788 grant_restart = 0;
1789 demote_restart = 0;
1790 hi = DLM_LOCK_IV;
1791
1792 list_for_each_entry_safe(lkb, s, &r->res_convertqueue, lkb_statequeue) {
1793 demoted = is_demoted(lkb);
c85d65e9
DT
1794 deadlk = 0;
1795
1796 if (can_be_granted(r, lkb, 0, &deadlk)) {
e7fd4179
DT
1797 grant_lock_pending(r, lkb);
1798 grant_restart = 1;
c85d65e9 1799 continue;
e7fd4179 1800 }
c85d65e9
DT
1801
1802 if (!demoted && is_demoted(lkb)) {
1803 log_print("WARN: pending demoted %x node %d %s",
1804 lkb->lkb_id, lkb->lkb_nodeid, r->res_name);
1805 demote_restart = 1;
1806 continue;
1807 }
1808
1809 if (deadlk) {
1810 log_print("WARN: pending deadlock %x node %d %s",
1811 lkb->lkb_id, lkb->lkb_nodeid, r->res_name);
1812 dlm_dump_rsb(r);
1813 continue;
1814 }
1815
1816 hi = max_t(int, lkb->lkb_rqmode, hi);
36509258
DT
1817
1818 if (cw && lkb->lkb_rqmode == DLM_LOCK_CW)
1819 *cw = 1;
e7fd4179
DT
1820 }
1821
1822 if (grant_restart)
1823 goto restart;
1824 if (demote_restart && !quit) {
1825 quit = 1;
1826 goto restart;
1827 }
1828
1829 return max_t(int, high, hi);
1830}
1831
36509258 1832static int grant_pending_wait(struct dlm_rsb *r, int high, int *cw)
e7fd4179
DT
1833{
1834 struct dlm_lkb *lkb, *s;
1835
1836 list_for_each_entry_safe(lkb, s, &r->res_waitqueue, lkb_statequeue) {
c85d65e9 1837 if (can_be_granted(r, lkb, 0, NULL))
e7fd4179 1838 grant_lock_pending(r, lkb);
36509258 1839 else {
e7fd4179 1840 high = max_t(int, lkb->lkb_rqmode, high);
36509258
DT
1841 if (lkb->lkb_rqmode == DLM_LOCK_CW)
1842 *cw = 1;
1843 }
e7fd4179
DT
1844 }
1845
1846 return high;
1847}
1848
36509258
DT
1849/* cw of 1 means there's a lock with a rqmode of DLM_LOCK_CW that's blocked
1850 on either the convert or waiting queue.
1851 high is the largest rqmode of all locks blocked on the convert or
1852 waiting queue. */
1853
1854static int lock_requires_bast(struct dlm_lkb *gr, int high, int cw)
1855{
1856 if (gr->lkb_grmode == DLM_LOCK_PR && cw) {
1857 if (gr->lkb_highbast < DLM_LOCK_EX)
1858 return 1;
1859 return 0;
1860 }
1861
1862 if (gr->lkb_highbast < high &&
1863 !__dlm_compat_matrix[gr->lkb_grmode+1][high+1])
1864 return 1;
1865 return 0;
1866}
1867
e7fd4179
DT
1868static void grant_pending_locks(struct dlm_rsb *r)
1869{
1870 struct dlm_lkb *lkb, *s;
1871 int high = DLM_LOCK_IV;
36509258 1872 int cw = 0;
e7fd4179 1873
a345da3e 1874 DLM_ASSERT(is_master(r), dlm_dump_rsb(r););
e7fd4179 1875
36509258
DT
1876 high = grant_pending_convert(r, high, &cw);
1877 high = grant_pending_wait(r, high, &cw);
e7fd4179
DT
1878
1879 if (high == DLM_LOCK_IV)
1880 return;
1881
1882 /*
1883 * If there are locks left on the wait/convert queue then send blocking
1884 * ASTs to granted locks based on the largest requested mode (high)
36509258 1885 * found above.
e7fd4179
DT
1886 */
1887
1888 list_for_each_entry_safe(lkb, s, &r->res_grantqueue, lkb_statequeue) {
e5dae548 1889 if (lkb->lkb_bastfn && lock_requires_bast(lkb, high, cw)) {
329fc4c3
DT
1890 if (cw && high == DLM_LOCK_PR &&
1891 lkb->lkb_grmode == DLM_LOCK_PR)
36509258
DT
1892 queue_bast(r, lkb, DLM_LOCK_CW);
1893 else
1894 queue_bast(r, lkb, high);
e7fd4179
DT
1895 lkb->lkb_highbast = high;
1896 }
1897 }
1898}
1899
36509258
DT
1900static int modes_require_bast(struct dlm_lkb *gr, struct dlm_lkb *rq)
1901{
1902 if ((gr->lkb_grmode == DLM_LOCK_PR && rq->lkb_rqmode == DLM_LOCK_CW) ||
1903 (gr->lkb_grmode == DLM_LOCK_CW && rq->lkb_rqmode == DLM_LOCK_PR)) {
1904 if (gr->lkb_highbast < DLM_LOCK_EX)
1905 return 1;
1906 return 0;
1907 }
1908
1909 if (gr->lkb_highbast < rq->lkb_rqmode && !modes_compat(gr, rq))
1910 return 1;
1911 return 0;
1912}
1913
e7fd4179
DT
1914static void send_bast_queue(struct dlm_rsb *r, struct list_head *head,
1915 struct dlm_lkb *lkb)
1916{
1917 struct dlm_lkb *gr;
1918
1919 list_for_each_entry(gr, head, lkb_statequeue) {
314dd2a0
SW
1920 /* skip self when sending basts to convertqueue */
1921 if (gr == lkb)
1922 continue;
e5dae548 1923 if (gr->lkb_bastfn && modes_require_bast(gr, lkb)) {
e7fd4179
DT
1924 queue_bast(r, gr, lkb->lkb_rqmode);
1925 gr->lkb_highbast = lkb->lkb_rqmode;
1926 }
1927 }
1928}
1929
1930static void send_blocking_asts(struct dlm_rsb *r, struct dlm_lkb *lkb)
1931{
1932 send_bast_queue(r, &r->res_grantqueue, lkb);
1933}
1934
1935static void send_blocking_asts_all(struct dlm_rsb *r, struct dlm_lkb *lkb)
1936{
1937 send_bast_queue(r, &r->res_grantqueue, lkb);
1938 send_bast_queue(r, &r->res_convertqueue, lkb);
1939}
1940
1941/* set_master(r, lkb) -- set the master nodeid of a resource
1942
1943 The purpose of this function is to set the nodeid field in the given
1944 lkb using the nodeid field in the given rsb. If the rsb's nodeid is
1945 known, it can just be copied to the lkb and the function will return
1946 0. If the rsb's nodeid is _not_ known, it needs to be looked up
1947 before it can be copied to the lkb.
1948
1949 When the rsb nodeid is being looked up remotely, the initial lkb
1950 causing the lookup is kept on the ls_waiters list waiting for the
1951 lookup reply. Other lkb's waiting for the same rsb lookup are kept
1952 on the rsb's res_lookup list until the master is verified.
1953
1954 Return values:
1955 0: nodeid is set in rsb/lkb and the caller should go ahead and use it
1956 1: the rsb master is not available and the lkb has been placed on
1957 a wait queue
1958*/
1959
1960static int set_master(struct dlm_rsb *r, struct dlm_lkb *lkb)
1961{
1962 struct dlm_ls *ls = r->res_ls;
755b5eb8 1963 int i, error, dir_nodeid, ret_nodeid, our_nodeid = dlm_our_nodeid();
e7fd4179
DT
1964
1965 if (rsb_flag(r, RSB_MASTER_UNCERTAIN)) {
1966 rsb_clear_flag(r, RSB_MASTER_UNCERTAIN);
1967 r->res_first_lkid = lkb->lkb_id;
1968 lkb->lkb_nodeid = r->res_nodeid;
1969 return 0;
1970 }
1971
1972 if (r->res_first_lkid && r->res_first_lkid != lkb->lkb_id) {
1973 list_add_tail(&lkb->lkb_rsb_lookup, &r->res_lookup);
1974 return 1;
1975 }
1976
1977 if (r->res_nodeid == 0) {
1978 lkb->lkb_nodeid = 0;
1979 return 0;
1980 }
1981
1982 if (r->res_nodeid > 0) {
1983 lkb->lkb_nodeid = r->res_nodeid;
1984 return 0;
1985 }
1986
a345da3e 1987 DLM_ASSERT(r->res_nodeid == -1, dlm_dump_rsb(r););
e7fd4179
DT
1988
1989 dir_nodeid = dlm_dir_nodeid(r);
1990
1991 if (dir_nodeid != our_nodeid) {
1992 r->res_first_lkid = lkb->lkb_id;
1993 send_lookup(r, lkb);
1994 return 1;
1995 }
1996
755b5eb8 1997 for (i = 0; i < 2; i++) {
e7fd4179
DT
1998 /* It's possible for dlm_scand to remove an old rsb for
1999 this same resource from the toss list, us to create
2000 a new one, look up the master locally, and find it
2001 already exists just before dlm_scand does the
2002 dir_remove() on the previous rsb. */
2003
2004 error = dlm_dir_lookup(ls, our_nodeid, r->res_name,
2005 r->res_length, &ret_nodeid);
2006 if (!error)
2007 break;
2008 log_debug(ls, "dir_lookup error %d %s", error, r->res_name);
2009 schedule();
2010 }
755b5eb8
DT
2011 if (error && error != -EEXIST)
2012 return error;
e7fd4179
DT
2013
2014 if (ret_nodeid == our_nodeid) {
2015 r->res_first_lkid = 0;
2016 r->res_nodeid = 0;
2017 lkb->lkb_nodeid = 0;
2018 } else {
2019 r->res_first_lkid = lkb->lkb_id;
2020 r->res_nodeid = ret_nodeid;
2021 lkb->lkb_nodeid = ret_nodeid;
2022 }
2023 return 0;
2024}
2025
2026static void process_lookup_list(struct dlm_rsb *r)
2027{
2028 struct dlm_lkb *lkb, *safe;
2029
2030 list_for_each_entry_safe(lkb, safe, &r->res_lookup, lkb_rsb_lookup) {
ef0c2bb0 2031 list_del_init(&lkb->lkb_rsb_lookup);
e7fd4179
DT
2032 _request_lock(r, lkb);
2033 schedule();
2034 }
2035}
2036
2037/* confirm_master -- confirm (or deny) an rsb's master nodeid */
2038
2039static void confirm_master(struct dlm_rsb *r, int error)
2040{
2041 struct dlm_lkb *lkb;
2042
2043 if (!r->res_first_lkid)
2044 return;
2045
2046 switch (error) {
2047 case 0:
2048 case -EINPROGRESS:
2049 r->res_first_lkid = 0;
2050 process_lookup_list(r);
2051 break;
2052
2053 case -EAGAIN:
aec64e1b
DT
2054 case -EBADR:
2055 case -ENOTBLK:
2056 /* the remote request failed and won't be retried (it was
2057 a NOQUEUE, or has been canceled/unlocked); make a waiting
2058 lkb the first_lkid */
e7fd4179
DT
2059
2060 r->res_first_lkid = 0;
2061
2062 if (!list_empty(&r->res_lookup)) {
2063 lkb = list_entry(r->res_lookup.next, struct dlm_lkb,
2064 lkb_rsb_lookup);
ef0c2bb0 2065 list_del_init(&lkb->lkb_rsb_lookup);
e7fd4179
DT
2066 r->res_first_lkid = lkb->lkb_id;
2067 _request_lock(r, lkb);
761b9d3f 2068 }
e7fd4179
DT
2069 break;
2070
2071 default:
2072 log_error(r->res_ls, "confirm_master unknown error %d", error);
2073 }
2074}
2075
2076static int set_lock_args(int mode, struct dlm_lksb *lksb, uint32_t flags,
e5dae548
DT
2077 int namelen, unsigned long timeout_cs,
2078 void (*ast) (void *astparam),
2079 void *astparam,
2080 void (*bast) (void *astparam, int mode),
2081 struct dlm_args *args)
e7fd4179
DT
2082{
2083 int rv = -EINVAL;
2084
2085 /* check for invalid arg usage */
2086
2087 if (mode < 0 || mode > DLM_LOCK_EX)
2088 goto out;
2089
2090 if (!(flags & DLM_LKF_CONVERT) && (namelen > DLM_RESNAME_MAXLEN))
2091 goto out;
2092
2093 if (flags & DLM_LKF_CANCEL)
2094 goto out;
2095
2096 if (flags & DLM_LKF_QUECVT && !(flags & DLM_LKF_CONVERT))
2097 goto out;
2098
2099 if (flags & DLM_LKF_CONVDEADLK && !(flags & DLM_LKF_CONVERT))
2100 goto out;
2101
2102 if (flags & DLM_LKF_CONVDEADLK && flags & DLM_LKF_NOQUEUE)
2103 goto out;
2104
2105 if (flags & DLM_LKF_EXPEDITE && flags & DLM_LKF_CONVERT)
2106 goto out;
2107
2108 if (flags & DLM_LKF_EXPEDITE && flags & DLM_LKF_QUECVT)
2109 goto out;
2110
2111 if (flags & DLM_LKF_EXPEDITE && flags & DLM_LKF_NOQUEUE)
2112 goto out;
2113
2114 if (flags & DLM_LKF_EXPEDITE && mode != DLM_LOCK_NL)
2115 goto out;
2116
2117 if (!ast || !lksb)
2118 goto out;
2119
2120 if (flags & DLM_LKF_VALBLK && !lksb->sb_lvbptr)
2121 goto out;
2122
e7fd4179
DT
2123 if (flags & DLM_LKF_CONVERT && !lksb->sb_lkid)
2124 goto out;
2125
2126 /* these args will be copied to the lkb in validate_lock_args,
2127 it cannot be done now because when converting locks, fields in
2128 an active lkb cannot be modified before locking the rsb */
2129
2130 args->flags = flags;
e5dae548
DT
2131 args->astfn = ast;
2132 args->astparam = astparam;
2133 args->bastfn = bast;
d7db923e 2134 args->timeout = timeout_cs;
e7fd4179
DT
2135 args->mode = mode;
2136 args->lksb = lksb;
e7fd4179
DT
2137 rv = 0;
2138 out:
2139 return rv;
2140}
2141
2142static int set_unlock_args(uint32_t flags, void *astarg, struct dlm_args *args)
2143{
2144 if (flags & ~(DLM_LKF_CANCEL | DLM_LKF_VALBLK | DLM_LKF_IVVALBLK |
2145 DLM_LKF_FORCEUNLOCK))
2146 return -EINVAL;
2147
ef0c2bb0
DT
2148 if (flags & DLM_LKF_CANCEL && flags & DLM_LKF_FORCEUNLOCK)
2149 return -EINVAL;
2150
e7fd4179 2151 args->flags = flags;
e5dae548 2152 args->astparam = astarg;
e7fd4179
DT
2153 return 0;
2154}
2155
2156static int validate_lock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
2157 struct dlm_args *args)
2158{
2159 int rv = -EINVAL;
2160
2161 if (args->flags & DLM_LKF_CONVERT) {
2162 if (lkb->lkb_flags & DLM_IFL_MSTCPY)
2163 goto out;
2164
2165 if (args->flags & DLM_LKF_QUECVT &&
2166 !__quecvt_compat_matrix[lkb->lkb_grmode+1][args->mode+1])
2167 goto out;
2168
2169 rv = -EBUSY;
2170 if (lkb->lkb_status != DLM_LKSTS_GRANTED)
2171 goto out;
2172
2173 if (lkb->lkb_wait_type)
2174 goto out;
ef0c2bb0
DT
2175
2176 if (is_overlap(lkb))
2177 goto out;
e7fd4179
DT
2178 }
2179
2180 lkb->lkb_exflags = args->flags;
2181 lkb->lkb_sbflags = 0;
e5dae548 2182 lkb->lkb_astfn = args->astfn;
e7fd4179 2183 lkb->lkb_astparam = args->astparam;
e5dae548 2184 lkb->lkb_bastfn = args->bastfn;
e7fd4179
DT
2185 lkb->lkb_rqmode = args->mode;
2186 lkb->lkb_lksb = args->lksb;
2187 lkb->lkb_lvbptr = args->lksb->sb_lvbptr;
2188 lkb->lkb_ownpid = (int) current->pid;
d7db923e 2189 lkb->lkb_timeout_cs = args->timeout;
e7fd4179
DT
2190 rv = 0;
2191 out:
43279e53
DT
2192 if (rv)
2193 log_debug(ls, "validate_lock_args %d %x %x %x %d %d %s",
2194 rv, lkb->lkb_id, lkb->lkb_flags, args->flags,
2195 lkb->lkb_status, lkb->lkb_wait_type,
2196 lkb->lkb_resource->res_name);
e7fd4179
DT
2197 return rv;
2198}
2199
ef0c2bb0
DT
2200/* when dlm_unlock() sees -EBUSY with CANCEL/FORCEUNLOCK it returns 0
2201 for success */
2202
2203/* note: it's valid for lkb_nodeid/res_nodeid to be -1 when we get here
2204 because there may be a lookup in progress and it's valid to do
2205 cancel/unlockf on it */
2206
e7fd4179
DT
2207static int validate_unlock_args(struct dlm_lkb *lkb, struct dlm_args *args)
2208{
ef0c2bb0 2209 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
e7fd4179
DT
2210 int rv = -EINVAL;
2211
ef0c2bb0
DT
2212 if (lkb->lkb_flags & DLM_IFL_MSTCPY) {
2213 log_error(ls, "unlock on MSTCPY %x", lkb->lkb_id);
2214 dlm_print_lkb(lkb);
e7fd4179 2215 goto out;
ef0c2bb0 2216 }
e7fd4179 2217
ef0c2bb0
DT
2218 /* an lkb may still exist even though the lock is EOL'ed due to a
2219 cancel, unlock or failed noqueue request; an app can't use these
2220 locks; return same error as if the lkid had not been found at all */
e7fd4179 2221
ef0c2bb0
DT
2222 if (lkb->lkb_flags & DLM_IFL_ENDOFLIFE) {
2223 log_debug(ls, "unlock on ENDOFLIFE %x", lkb->lkb_id);
2224 rv = -ENOENT;
e7fd4179 2225 goto out;
ef0c2bb0 2226 }
e7fd4179 2227
ef0c2bb0
DT
2228 /* an lkb may be waiting for an rsb lookup to complete where the
2229 lookup was initiated by another lock */
2230
42dc1601
DT
2231 if (!list_empty(&lkb->lkb_rsb_lookup)) {
2232 if (args->flags & (DLM_LKF_CANCEL | DLM_LKF_FORCEUNLOCK)) {
ef0c2bb0
DT
2233 log_debug(ls, "unlock on rsb_lookup %x", lkb->lkb_id);
2234 list_del_init(&lkb->lkb_rsb_lookup);
2235 queue_cast(lkb->lkb_resource, lkb,
2236 args->flags & DLM_LKF_CANCEL ?
2237 -DLM_ECANCEL : -DLM_EUNLOCK);
2238 unhold_lkb(lkb); /* undoes create_lkb() */
ef0c2bb0 2239 }
42dc1601
DT
2240 /* caller changes -EBUSY to 0 for CANCEL and FORCEUNLOCK */
2241 rv = -EBUSY;
2242 goto out;
ef0c2bb0
DT
2243 }
2244
2245 /* cancel not allowed with another cancel/unlock in progress */
2246
2247 if (args->flags & DLM_LKF_CANCEL) {
2248 if (lkb->lkb_exflags & DLM_LKF_CANCEL)
2249 goto out;
2250
2251 if (is_overlap(lkb))
2252 goto out;
2253
3ae1acf9
DT
2254 /* don't let scand try to do a cancel */
2255 del_timeout(lkb);
2256
ef0c2bb0
DT
2257 if (lkb->lkb_flags & DLM_IFL_RESEND) {
2258 lkb->lkb_flags |= DLM_IFL_OVERLAP_CANCEL;
2259 rv = -EBUSY;
2260 goto out;
2261 }
2262
a536e381
DT
2263 /* there's nothing to cancel */
2264 if (lkb->lkb_status == DLM_LKSTS_GRANTED &&
2265 !lkb->lkb_wait_type) {
2266 rv = -EBUSY;
2267 goto out;
2268 }
2269
ef0c2bb0
DT
2270 switch (lkb->lkb_wait_type) {
2271 case DLM_MSG_LOOKUP:
2272 case DLM_MSG_REQUEST:
2273 lkb->lkb_flags |= DLM_IFL_OVERLAP_CANCEL;
2274 rv = -EBUSY;
2275 goto out;
2276 case DLM_MSG_UNLOCK:
2277 case DLM_MSG_CANCEL:
2278 goto out;
2279 }
2280 /* add_to_waiters() will set OVERLAP_CANCEL */
2281 goto out_ok;
2282 }
2283
2284 /* do we need to allow a force-unlock if there's a normal unlock
2285 already in progress? in what conditions could the normal unlock
2286 fail such that we'd want to send a force-unlock to be sure? */
2287
2288 if (args->flags & DLM_LKF_FORCEUNLOCK) {
2289 if (lkb->lkb_exflags & DLM_LKF_FORCEUNLOCK)
2290 goto out;
2291
2292 if (is_overlap_unlock(lkb))
2293 goto out;
e7fd4179 2294
3ae1acf9
DT
2295 /* don't let scand try to do a cancel */
2296 del_timeout(lkb);
2297
ef0c2bb0
DT
2298 if (lkb->lkb_flags & DLM_IFL_RESEND) {
2299 lkb->lkb_flags |= DLM_IFL_OVERLAP_UNLOCK;
2300 rv = -EBUSY;
2301 goto out;
2302 }
2303
2304 switch (lkb->lkb_wait_type) {
2305 case DLM_MSG_LOOKUP:
2306 case DLM_MSG_REQUEST:
2307 lkb->lkb_flags |= DLM_IFL_OVERLAP_UNLOCK;
2308 rv = -EBUSY;
2309 goto out;
2310 case DLM_MSG_UNLOCK:
2311 goto out;
2312 }
2313 /* add_to_waiters() will set OVERLAP_UNLOCK */
2314 goto out_ok;
2315 }
2316
2317 /* normal unlock not allowed if there's any op in progress */
e7fd4179 2318 rv = -EBUSY;
ef0c2bb0 2319 if (lkb->lkb_wait_type || lkb->lkb_wait_count)
e7fd4179
DT
2320 goto out;
2321
2322 out_ok:
ef0c2bb0
DT
2323 /* an overlapping op shouldn't blow away exflags from other op */
2324 lkb->lkb_exflags |= args->flags;
e7fd4179
DT
2325 lkb->lkb_sbflags = 0;
2326 lkb->lkb_astparam = args->astparam;
e7fd4179
DT
2327 rv = 0;
2328 out:
ef0c2bb0
DT
2329 if (rv)
2330 log_debug(ls, "validate_unlock_args %d %x %x %x %x %d %s", rv,
2331 lkb->lkb_id, lkb->lkb_flags, lkb->lkb_exflags,
2332 args->flags, lkb->lkb_wait_type,
2333 lkb->lkb_resource->res_name);
e7fd4179
DT
2334 return rv;
2335}
2336
2337/*
2338 * Four stage 4 varieties:
2339 * do_request(), do_convert(), do_unlock(), do_cancel()
2340 * These are called on the master node for the given lock and
2341 * from the central locking logic.
2342 */
2343
2344static int do_request(struct dlm_rsb *r, struct dlm_lkb *lkb)
2345{
2346 int error = 0;
2347
c85d65e9 2348 if (can_be_granted(r, lkb, 1, NULL)) {
e7fd4179
DT
2349 grant_lock(r, lkb);
2350 queue_cast(r, lkb, 0);
2351 goto out;
2352 }
2353
2354 if (can_be_queued(lkb)) {
2355 error = -EINPROGRESS;
2356 add_lkb(r, lkb, DLM_LKSTS_WAITING);
3ae1acf9 2357 add_timeout(lkb);
e7fd4179
DT
2358 goto out;
2359 }
2360
2361 error = -EAGAIN;
e7fd4179 2362 queue_cast(r, lkb, -EAGAIN);
e7fd4179
DT
2363 out:
2364 return error;
2365}
2366
cf6620ac
DT
2367static void do_request_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
2368 int error)
2369{
2370 switch (error) {
2371 case -EAGAIN:
2372 if (force_blocking_asts(lkb))
2373 send_blocking_asts_all(r, lkb);
2374 break;
2375 case -EINPROGRESS:
2376 send_blocking_asts(r, lkb);
2377 break;
2378 }
2379}
2380
e7fd4179
DT
2381static int do_convert(struct dlm_rsb *r, struct dlm_lkb *lkb)
2382{
2383 int error = 0;
c85d65e9 2384 int deadlk = 0;
e7fd4179
DT
2385
2386 /* changing an existing lock may allow others to be granted */
2387
c85d65e9 2388 if (can_be_granted(r, lkb, 1, &deadlk)) {
e7fd4179
DT
2389 grant_lock(r, lkb);
2390 queue_cast(r, lkb, 0);
e7fd4179
DT
2391 goto out;
2392 }
2393
c85d65e9
DT
2394 /* can_be_granted() detected that this lock would block in a conversion
2395 deadlock, so we leave it on the granted queue and return EDEADLK in
2396 the ast for the convert. */
2397
2398 if (deadlk) {
2399 /* it's left on the granted queue */
2400 log_debug(r->res_ls, "deadlock %x node %d sts%d g%d r%d %s",
2401 lkb->lkb_id, lkb->lkb_nodeid, lkb->lkb_status,
2402 lkb->lkb_grmode, lkb->lkb_rqmode, r->res_name);
2403 revert_lock(r, lkb);
2404 queue_cast(r, lkb, -EDEADLK);
2405 error = -EDEADLK;
2406 goto out;
2407 }
2408
7d3c1feb
DT
2409 /* is_demoted() means the can_be_granted() above set the grmode
2410 to NL, and left us on the granted queue. This auto-demotion
2411 (due to CONVDEADLK) might mean other locks, and/or this lock, are
2412 now grantable. We have to try to grant other converting locks
2413 before we try again to grant this one. */
2414
2415 if (is_demoted(lkb)) {
36509258 2416 grant_pending_convert(r, DLM_LOCK_IV, NULL);
7d3c1feb
DT
2417 if (_can_be_granted(r, lkb, 1)) {
2418 grant_lock(r, lkb);
2419 queue_cast(r, lkb, 0);
7d3c1feb
DT
2420 goto out;
2421 }
2422 /* else fall through and move to convert queue */
2423 }
2424
2425 if (can_be_queued(lkb)) {
e7fd4179
DT
2426 error = -EINPROGRESS;
2427 del_lkb(r, lkb);
2428 add_lkb(r, lkb, DLM_LKSTS_CONVERT);
3ae1acf9 2429 add_timeout(lkb);
e7fd4179
DT
2430 goto out;
2431 }
2432
2433 error = -EAGAIN;
e7fd4179 2434 queue_cast(r, lkb, -EAGAIN);
e7fd4179
DT
2435 out:
2436 return error;
2437}
2438
cf6620ac
DT
2439static void do_convert_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
2440 int error)
2441{
2442 switch (error) {
2443 case 0:
2444 grant_pending_locks(r);
2445 /* grant_pending_locks also sends basts */
2446 break;
2447 case -EAGAIN:
2448 if (force_blocking_asts(lkb))
2449 send_blocking_asts_all(r, lkb);
2450 break;
2451 case -EINPROGRESS:
2452 send_blocking_asts(r, lkb);
2453 break;
2454 }
2455}
2456
e7fd4179
DT
2457static int do_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2458{
2459 remove_lock(r, lkb);
2460 queue_cast(r, lkb, -DLM_EUNLOCK);
e7fd4179
DT
2461 return -DLM_EUNLOCK;
2462}
2463
cf6620ac
DT
2464static void do_unlock_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
2465 int error)
2466{
2467 grant_pending_locks(r);
2468}
2469
ef0c2bb0 2470/* returns: 0 did nothing, -DLM_ECANCEL canceled lock */
907b9bce 2471
e7fd4179
DT
2472static int do_cancel(struct dlm_rsb *r, struct dlm_lkb *lkb)
2473{
ef0c2bb0
DT
2474 int error;
2475
2476 error = revert_lock(r, lkb);
2477 if (error) {
2478 queue_cast(r, lkb, -DLM_ECANCEL);
ef0c2bb0
DT
2479 return -DLM_ECANCEL;
2480 }
2481 return 0;
e7fd4179
DT
2482}
2483
cf6620ac
DT
2484static void do_cancel_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
2485 int error)
2486{
2487 if (error)
2488 grant_pending_locks(r);
2489}
2490
e7fd4179
DT
2491/*
2492 * Four stage 3 varieties:
2493 * _request_lock(), _convert_lock(), _unlock_lock(), _cancel_lock()
2494 */
2495
2496/* add a new lkb to a possibly new rsb, called by requesting process */
2497
2498static int _request_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2499{
2500 int error;
2501
2502 /* set_master: sets lkb nodeid from r */
2503
2504 error = set_master(r, lkb);
2505 if (error < 0)
2506 goto out;
2507 if (error) {
2508 error = 0;
2509 goto out;
2510 }
2511
cf6620ac 2512 if (is_remote(r)) {
e7fd4179
DT
2513 /* receive_request() calls do_request() on remote node */
2514 error = send_request(r, lkb);
cf6620ac 2515 } else {
e7fd4179 2516 error = do_request(r, lkb);
cf6620ac
DT
2517 /* for remote locks the request_reply is sent
2518 between do_request and do_request_effects */
2519 do_request_effects(r, lkb, error);
2520 }
e7fd4179
DT
2521 out:
2522 return error;
2523}
2524
3bcd3687 2525/* change some property of an existing lkb, e.g. mode */
e7fd4179
DT
2526
2527static int _convert_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2528{
2529 int error;
2530
cf6620ac 2531 if (is_remote(r)) {
e7fd4179
DT
2532 /* receive_convert() calls do_convert() on remote node */
2533 error = send_convert(r, lkb);
cf6620ac 2534 } else {
e7fd4179 2535 error = do_convert(r, lkb);
cf6620ac
DT
2536 /* for remote locks the convert_reply is sent
2537 between do_convert and do_convert_effects */
2538 do_convert_effects(r, lkb, error);
2539 }
e7fd4179
DT
2540
2541 return error;
2542}
2543
2544/* remove an existing lkb from the granted queue */
2545
2546static int _unlock_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2547{
2548 int error;
2549
cf6620ac 2550 if (is_remote(r)) {
e7fd4179
DT
2551 /* receive_unlock() calls do_unlock() on remote node */
2552 error = send_unlock(r, lkb);
cf6620ac 2553 } else {
e7fd4179 2554 error = do_unlock(r, lkb);
cf6620ac
DT
2555 /* for remote locks the unlock_reply is sent
2556 between do_unlock and do_unlock_effects */
2557 do_unlock_effects(r, lkb, error);
2558 }
e7fd4179
DT
2559
2560 return error;
2561}
2562
2563/* remove an existing lkb from the convert or wait queue */
2564
2565static int _cancel_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2566{
2567 int error;
2568
cf6620ac 2569 if (is_remote(r)) {
e7fd4179
DT
2570 /* receive_cancel() calls do_cancel() on remote node */
2571 error = send_cancel(r, lkb);
cf6620ac 2572 } else {
e7fd4179 2573 error = do_cancel(r, lkb);
cf6620ac
DT
2574 /* for remote locks the cancel_reply is sent
2575 between do_cancel and do_cancel_effects */
2576 do_cancel_effects(r, lkb, error);
2577 }
e7fd4179
DT
2578
2579 return error;
2580}
2581
2582/*
2583 * Four stage 2 varieties:
2584 * request_lock(), convert_lock(), unlock_lock(), cancel_lock()
2585 */
2586
2587static int request_lock(struct dlm_ls *ls, struct dlm_lkb *lkb, char *name,
2588 int len, struct dlm_args *args)
2589{
2590 struct dlm_rsb *r;
2591 int error;
2592
2593 error = validate_lock_args(ls, lkb, args);
2594 if (error)
2595 goto out;
2596
2597 error = find_rsb(ls, name, len, R_CREATE, &r);
2598 if (error)
2599 goto out;
2600
2601 lock_rsb(r);
2602
2603 attach_lkb(r, lkb);
2604 lkb->lkb_lksb->sb_lkid = lkb->lkb_id;
2605
2606 error = _request_lock(r, lkb);
2607
2608 unlock_rsb(r);
2609 put_rsb(r);
2610
2611 out:
2612 return error;
2613}
2614
2615static int convert_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
2616 struct dlm_args *args)
2617{
2618 struct dlm_rsb *r;
2619 int error;
2620
2621 r = lkb->lkb_resource;
2622
2623 hold_rsb(r);
2624 lock_rsb(r);
2625
2626 error = validate_lock_args(ls, lkb, args);
2627 if (error)
2628 goto out;
2629
2630 error = _convert_lock(r, lkb);
2631 out:
2632 unlock_rsb(r);
2633 put_rsb(r);
2634 return error;
2635}
2636
2637static int unlock_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
2638 struct dlm_args *args)
2639{
2640 struct dlm_rsb *r;
2641 int error;
2642
2643 r = lkb->lkb_resource;
2644
2645 hold_rsb(r);
2646 lock_rsb(r);
2647
2648 error = validate_unlock_args(lkb, args);
2649 if (error)
2650 goto out;
2651
2652 error = _unlock_lock(r, lkb);
2653 out:
2654 unlock_rsb(r);
2655 put_rsb(r);
2656 return error;
2657}
2658
2659static int cancel_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
2660 struct dlm_args *args)
2661{
2662 struct dlm_rsb *r;
2663 int error;
2664
2665 r = lkb->lkb_resource;
2666
2667 hold_rsb(r);
2668 lock_rsb(r);
2669
2670 error = validate_unlock_args(lkb, args);
2671 if (error)
2672 goto out;
2673
2674 error = _cancel_lock(r, lkb);
2675 out:
2676 unlock_rsb(r);
2677 put_rsb(r);
2678 return error;
2679}
2680
2681/*
2682 * Two stage 1 varieties: dlm_lock() and dlm_unlock()
2683 */
2684
2685int dlm_lock(dlm_lockspace_t *lockspace,
2686 int mode,
2687 struct dlm_lksb *lksb,
2688 uint32_t flags,
2689 void *name,
2690 unsigned int namelen,
2691 uint32_t parent_lkid,
2692 void (*ast) (void *astarg),
2693 void *astarg,
3bcd3687 2694 void (*bast) (void *astarg, int mode))
e7fd4179
DT
2695{
2696 struct dlm_ls *ls;
2697 struct dlm_lkb *lkb;
2698 struct dlm_args args;
2699 int error, convert = flags & DLM_LKF_CONVERT;
2700
2701 ls = dlm_find_lockspace_local(lockspace);
2702 if (!ls)
2703 return -EINVAL;
2704
85e86edf 2705 dlm_lock_recovery(ls);
e7fd4179
DT
2706
2707 if (convert)
2708 error = find_lkb(ls, lksb->sb_lkid, &lkb);
2709 else
2710 error = create_lkb(ls, &lkb);
2711
2712 if (error)
2713 goto out;
2714
d7db923e 2715 error = set_lock_args(mode, lksb, flags, namelen, 0, ast,
3bcd3687 2716 astarg, bast, &args);
e7fd4179
DT
2717 if (error)
2718 goto out_put;
2719
2720 if (convert)
2721 error = convert_lock(ls, lkb, &args);
2722 else
2723 error = request_lock(ls, lkb, name, namelen, &args);
2724
2725 if (error == -EINPROGRESS)
2726 error = 0;
2727 out_put:
2728 if (convert || error)
b3f58d8f 2729 __put_lkb(ls, lkb);
c85d65e9 2730 if (error == -EAGAIN || error == -EDEADLK)
e7fd4179
DT
2731 error = 0;
2732 out:
85e86edf 2733 dlm_unlock_recovery(ls);
e7fd4179
DT
2734 dlm_put_lockspace(ls);
2735 return error;
2736}
2737
2738int dlm_unlock(dlm_lockspace_t *lockspace,
2739 uint32_t lkid,
2740 uint32_t flags,
2741 struct dlm_lksb *lksb,
2742 void *astarg)
2743{
2744 struct dlm_ls *ls;
2745 struct dlm_lkb *lkb;
2746 struct dlm_args args;
2747 int error;
2748
2749 ls = dlm_find_lockspace_local(lockspace);
2750 if (!ls)
2751 return -EINVAL;
2752
85e86edf 2753 dlm_lock_recovery(ls);
e7fd4179
DT
2754
2755 error = find_lkb(ls, lkid, &lkb);
2756 if (error)
2757 goto out;
2758
2759 error = set_unlock_args(flags, astarg, &args);
2760 if (error)
2761 goto out_put;
2762
2763 if (flags & DLM_LKF_CANCEL)
2764 error = cancel_lock(ls, lkb, &args);
2765 else
2766 error = unlock_lock(ls, lkb, &args);
2767
2768 if (error == -DLM_EUNLOCK || error == -DLM_ECANCEL)
2769 error = 0;
ef0c2bb0
DT
2770 if (error == -EBUSY && (flags & (DLM_LKF_CANCEL | DLM_LKF_FORCEUNLOCK)))
2771 error = 0;
e7fd4179 2772 out_put:
b3f58d8f 2773 dlm_put_lkb(lkb);
e7fd4179 2774 out:
85e86edf 2775 dlm_unlock_recovery(ls);
e7fd4179
DT
2776 dlm_put_lockspace(ls);
2777 return error;
2778}
2779
2780/*
2781 * send/receive routines for remote operations and replies
2782 *
2783 * send_args
2784 * send_common
2785 * send_request receive_request
2786 * send_convert receive_convert
2787 * send_unlock receive_unlock
2788 * send_cancel receive_cancel
2789 * send_grant receive_grant
2790 * send_bast receive_bast
2791 * send_lookup receive_lookup
2792 * send_remove receive_remove
2793 *
2794 * send_common_reply
2795 * receive_request_reply send_request_reply
2796 * receive_convert_reply send_convert_reply
2797 * receive_unlock_reply send_unlock_reply
2798 * receive_cancel_reply send_cancel_reply
2799 * receive_lookup_reply send_lookup_reply
2800 */
2801
7e4dac33
DT
2802static int _create_message(struct dlm_ls *ls, int mb_len,
2803 int to_nodeid, int mstype,
2804 struct dlm_message **ms_ret,
2805 struct dlm_mhandle **mh_ret)
e7fd4179
DT
2806{
2807 struct dlm_message *ms;
2808 struct dlm_mhandle *mh;
2809 char *mb;
e7fd4179
DT
2810
2811 /* get_buffer gives us a message handle (mh) that we need to
2812 pass into lowcomms_commit and a message buffer (mb) that we
2813 write our data into */
2814
573c24c4 2815 mh = dlm_lowcomms_get_buffer(to_nodeid, mb_len, GFP_NOFS, &mb);
e7fd4179
DT
2816 if (!mh)
2817 return -ENOBUFS;
2818
2819 memset(mb, 0, mb_len);
2820
2821 ms = (struct dlm_message *) mb;
2822
2823 ms->m_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
7e4dac33 2824 ms->m_header.h_lockspace = ls->ls_global_id;
e7fd4179
DT
2825 ms->m_header.h_nodeid = dlm_our_nodeid();
2826 ms->m_header.h_length = mb_len;
2827 ms->m_header.h_cmd = DLM_MSG;
2828
2829 ms->m_type = mstype;
2830
2831 *mh_ret = mh;
2832 *ms_ret = ms;
2833 return 0;
2834}
2835
7e4dac33
DT
2836static int create_message(struct dlm_rsb *r, struct dlm_lkb *lkb,
2837 int to_nodeid, int mstype,
2838 struct dlm_message **ms_ret,
2839 struct dlm_mhandle **mh_ret)
2840{
2841 int mb_len = sizeof(struct dlm_message);
2842
2843 switch (mstype) {
2844 case DLM_MSG_REQUEST:
2845 case DLM_MSG_LOOKUP:
2846 case DLM_MSG_REMOVE:
2847 mb_len += r->res_length;
2848 break;
2849 case DLM_MSG_CONVERT:
2850 case DLM_MSG_UNLOCK:
2851 case DLM_MSG_REQUEST_REPLY:
2852 case DLM_MSG_CONVERT_REPLY:
2853 case DLM_MSG_GRANT:
2854 if (lkb && lkb->lkb_lvbptr)
2855 mb_len += r->res_ls->ls_lvblen;
2856 break;
2857 }
2858
2859 return _create_message(r->res_ls, mb_len, to_nodeid, mstype,
2860 ms_ret, mh_ret);
2861}
2862
e7fd4179
DT
2863/* further lowcomms enhancements or alternate implementations may make
2864 the return value from this function useful at some point */
2865
2866static int send_message(struct dlm_mhandle *mh, struct dlm_message *ms)
2867{
2868 dlm_message_out(ms);
2869 dlm_lowcomms_commit_buffer(mh);
2870 return 0;
2871}
2872
2873static void send_args(struct dlm_rsb *r, struct dlm_lkb *lkb,
2874 struct dlm_message *ms)
2875{
2876 ms->m_nodeid = lkb->lkb_nodeid;
2877 ms->m_pid = lkb->lkb_ownpid;
2878 ms->m_lkid = lkb->lkb_id;
2879 ms->m_remid = lkb->lkb_remid;
2880 ms->m_exflags = lkb->lkb_exflags;
2881 ms->m_sbflags = lkb->lkb_sbflags;
2882 ms->m_flags = lkb->lkb_flags;
2883 ms->m_lvbseq = lkb->lkb_lvbseq;
2884 ms->m_status = lkb->lkb_status;
2885 ms->m_grmode = lkb->lkb_grmode;
2886 ms->m_rqmode = lkb->lkb_rqmode;
2887 ms->m_hash = r->res_hash;
2888
2889 /* m_result and m_bastmode are set from function args,
2890 not from lkb fields */
2891
e5dae548 2892 if (lkb->lkb_bastfn)
8304d6f2 2893 ms->m_asts |= DLM_CB_BAST;
e5dae548 2894 if (lkb->lkb_astfn)
8304d6f2 2895 ms->m_asts |= DLM_CB_CAST;
e7fd4179 2896
da49f36f
DT
2897 /* compare with switch in create_message; send_remove() doesn't
2898 use send_args() */
e7fd4179 2899
da49f36f
DT
2900 switch (ms->m_type) {
2901 case DLM_MSG_REQUEST:
2902 case DLM_MSG_LOOKUP:
2903 memcpy(ms->m_extra, r->res_name, r->res_length);
2904 break;
2905 case DLM_MSG_CONVERT:
2906 case DLM_MSG_UNLOCK:
2907 case DLM_MSG_REQUEST_REPLY:
2908 case DLM_MSG_CONVERT_REPLY:
2909 case DLM_MSG_GRANT:
2910 if (!lkb->lkb_lvbptr)
2911 break;
e7fd4179 2912 memcpy(ms->m_extra, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
da49f36f
DT
2913 break;
2914 }
e7fd4179
DT
2915}
2916
2917static int send_common(struct dlm_rsb *r, struct dlm_lkb *lkb, int mstype)
2918{
2919 struct dlm_message *ms;
2920 struct dlm_mhandle *mh;
2921 int to_nodeid, error;
2922
c6ff669b
DT
2923 to_nodeid = r->res_nodeid;
2924
2925 error = add_to_waiters(lkb, mstype, to_nodeid);
ef0c2bb0
DT
2926 if (error)
2927 return error;
e7fd4179 2928
e7fd4179
DT
2929 error = create_message(r, lkb, to_nodeid, mstype, &ms, &mh);
2930 if (error)
2931 goto fail;
2932
2933 send_args(r, lkb, ms);
2934
2935 error = send_message(mh, ms);
2936 if (error)
2937 goto fail;
2938 return 0;
2939
2940 fail:
ef0c2bb0 2941 remove_from_waiters(lkb, msg_reply_type(mstype));
e7fd4179
DT
2942 return error;
2943}
2944
2945static int send_request(struct dlm_rsb *r, struct dlm_lkb *lkb)
2946{
2947 return send_common(r, lkb, DLM_MSG_REQUEST);
2948}
2949
2950static int send_convert(struct dlm_rsb *r, struct dlm_lkb *lkb)
2951{
2952 int error;
2953
2954 error = send_common(r, lkb, DLM_MSG_CONVERT);
2955
2956 /* down conversions go without a reply from the master */
2957 if (!error && down_conversion(lkb)) {
ef0c2bb0 2958 remove_from_waiters(lkb, DLM_MSG_CONVERT_REPLY);
2a7ce0ed 2959 r->res_ls->ls_stub_ms.m_flags = DLM_IFL_STUB_MS;
ef0c2bb0 2960 r->res_ls->ls_stub_ms.m_type = DLM_MSG_CONVERT_REPLY;
e7fd4179
DT
2961 r->res_ls->ls_stub_ms.m_result = 0;
2962 __receive_convert_reply(r, lkb, &r->res_ls->ls_stub_ms);
2963 }
2964
2965 return error;
2966}
2967
2968/* FIXME: if this lkb is the only lock we hold on the rsb, then set
2969 MASTER_UNCERTAIN to force the next request on the rsb to confirm
2970 that the master is still correct. */
2971
2972static int send_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2973{
2974 return send_common(r, lkb, DLM_MSG_UNLOCK);
2975}
2976
2977static int send_cancel(struct dlm_rsb *r, struct dlm_lkb *lkb)
2978{
2979 return send_common(r, lkb, DLM_MSG_CANCEL);
2980}
2981
2982static int send_grant(struct dlm_rsb *r, struct dlm_lkb *lkb)
2983{
2984 struct dlm_message *ms;
2985 struct dlm_mhandle *mh;
2986 int to_nodeid, error;
2987
2988 to_nodeid = lkb->lkb_nodeid;
2989
2990 error = create_message(r, lkb, to_nodeid, DLM_MSG_GRANT, &ms, &mh);
2991 if (error)
2992 goto out;
2993
2994 send_args(r, lkb, ms);
2995
2996 ms->m_result = 0;
2997
2998 error = send_message(mh, ms);
2999 out:
3000 return error;
3001}
3002
3003static int send_bast(struct dlm_rsb *r, struct dlm_lkb *lkb, int mode)
3004{
3005 struct dlm_message *ms;
3006 struct dlm_mhandle *mh;
3007 int to_nodeid, error;
3008
3009 to_nodeid = lkb->lkb_nodeid;
3010
3011 error = create_message(r, NULL, to_nodeid, DLM_MSG_BAST, &ms, &mh);
3012 if (error)
3013 goto out;
3014
3015 send_args(r, lkb, ms);
3016
3017 ms->m_bastmode = mode;
3018
3019 error = send_message(mh, ms);
3020 out:
3021 return error;
3022}
3023
3024static int send_lookup(struct dlm_rsb *r, struct dlm_lkb *lkb)
3025{
3026 struct dlm_message *ms;
3027 struct dlm_mhandle *mh;
3028 int to_nodeid, error;
3029
c6ff669b
DT
3030 to_nodeid = dlm_dir_nodeid(r);
3031
3032 error = add_to_waiters(lkb, DLM_MSG_LOOKUP, to_nodeid);
ef0c2bb0
DT
3033 if (error)
3034 return error;
e7fd4179 3035
e7fd4179
DT
3036 error = create_message(r, NULL, to_nodeid, DLM_MSG_LOOKUP, &ms, &mh);
3037 if (error)
3038 goto fail;
3039
3040 send_args(r, lkb, ms);
3041
3042 error = send_message(mh, ms);
3043 if (error)
3044 goto fail;
3045 return 0;
3046
3047 fail:
ef0c2bb0 3048 remove_from_waiters(lkb, DLM_MSG_LOOKUP_REPLY);
e7fd4179
DT
3049 return error;
3050}
3051
3052static int send_remove(struct dlm_rsb *r)
3053{
3054 struct dlm_message *ms;
3055 struct dlm_mhandle *mh;
3056 int to_nodeid, error;
3057
3058 to_nodeid = dlm_dir_nodeid(r);
3059
3060 error = create_message(r, NULL, to_nodeid, DLM_MSG_REMOVE, &ms, &mh);
3061 if (error)
3062 goto out;
3063
3064 memcpy(ms->m_extra, r->res_name, r->res_length);
3065 ms->m_hash = r->res_hash;
3066
3067 error = send_message(mh, ms);
3068 out:
3069 return error;
3070}
3071
3072static int send_common_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
3073 int mstype, int rv)
3074{
3075 struct dlm_message *ms;
3076 struct dlm_mhandle *mh;
3077 int to_nodeid, error;
3078
3079 to_nodeid = lkb->lkb_nodeid;
3080
3081 error = create_message(r, lkb, to_nodeid, mstype, &ms, &mh);
3082 if (error)
3083 goto out;
3084
3085 send_args(r, lkb, ms);
3086
3087 ms->m_result = rv;
3088
3089 error = send_message(mh, ms);
3090 out:
3091 return error;
3092}
3093
3094static int send_request_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
3095{
3096 return send_common_reply(r, lkb, DLM_MSG_REQUEST_REPLY, rv);
3097}
3098
3099static int send_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
3100{
3101 return send_common_reply(r, lkb, DLM_MSG_CONVERT_REPLY, rv);
3102}
3103
3104static int send_unlock_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
3105{
3106 return send_common_reply(r, lkb, DLM_MSG_UNLOCK_REPLY, rv);
3107}
3108
3109static int send_cancel_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
3110{
3111 return send_common_reply(r, lkb, DLM_MSG_CANCEL_REPLY, rv);
3112}
3113
3114static int send_lookup_reply(struct dlm_ls *ls, struct dlm_message *ms_in,
3115 int ret_nodeid, int rv)
3116{
3117 struct dlm_rsb *r = &ls->ls_stub_rsb;
3118 struct dlm_message *ms;
3119 struct dlm_mhandle *mh;
3120 int error, nodeid = ms_in->m_header.h_nodeid;
3121
3122 error = create_message(r, NULL, nodeid, DLM_MSG_LOOKUP_REPLY, &ms, &mh);
3123 if (error)
3124 goto out;
3125
3126 ms->m_lkid = ms_in->m_lkid;
3127 ms->m_result = rv;
3128 ms->m_nodeid = ret_nodeid;
3129
3130 error = send_message(mh, ms);
3131 out:
3132 return error;
3133}
3134
3135/* which args we save from a received message depends heavily on the type
3136 of message, unlike the send side where we can safely send everything about
3137 the lkb for any type of message */
3138
3139static void receive_flags(struct dlm_lkb *lkb, struct dlm_message *ms)
3140{
3141 lkb->lkb_exflags = ms->m_exflags;
6f90a8b1 3142 lkb->lkb_sbflags = ms->m_sbflags;
e7fd4179
DT
3143 lkb->lkb_flags = (lkb->lkb_flags & 0xFFFF0000) |
3144 (ms->m_flags & 0x0000FFFF);
3145}
3146
3147static void receive_flags_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
3148{
2a7ce0ed
DT
3149 if (ms->m_flags == DLM_IFL_STUB_MS)
3150 return;
3151
e7fd4179
DT
3152 lkb->lkb_sbflags = ms->m_sbflags;
3153 lkb->lkb_flags = (lkb->lkb_flags & 0xFFFF0000) |
3154 (ms->m_flags & 0x0000FFFF);
3155}
3156
3157static int receive_extralen(struct dlm_message *ms)
3158{
3159 return (ms->m_header.h_length - sizeof(struct dlm_message));
3160}
3161
e7fd4179
DT
3162static int receive_lvb(struct dlm_ls *ls, struct dlm_lkb *lkb,
3163 struct dlm_message *ms)
3164{
3165 int len;
3166
3167 if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
3168 if (!lkb->lkb_lvbptr)
52bda2b5 3169 lkb->lkb_lvbptr = dlm_allocate_lvb(ls);
e7fd4179
DT
3170 if (!lkb->lkb_lvbptr)
3171 return -ENOMEM;
3172 len = receive_extralen(ms);
a9cc9159
AV
3173 if (len > DLM_RESNAME_MAXLEN)
3174 len = DLM_RESNAME_MAXLEN;
e7fd4179
DT
3175 memcpy(lkb->lkb_lvbptr, ms->m_extra, len);
3176 }
3177 return 0;
3178}
3179
e5dae548
DT
3180static void fake_bastfn(void *astparam, int mode)
3181{
3182 log_print("fake_bastfn should not be called");
3183}
3184
3185static void fake_astfn(void *astparam)
3186{
3187 log_print("fake_astfn should not be called");
3188}
3189
e7fd4179
DT
3190static int receive_request_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
3191 struct dlm_message *ms)
3192{
3193 lkb->lkb_nodeid = ms->m_header.h_nodeid;
3194 lkb->lkb_ownpid = ms->m_pid;
3195 lkb->lkb_remid = ms->m_lkid;
3196 lkb->lkb_grmode = DLM_LOCK_IV;
3197 lkb->lkb_rqmode = ms->m_rqmode;
e5dae548 3198
8304d6f2
DT
3199 lkb->lkb_bastfn = (ms->m_asts & DLM_CB_BAST) ? &fake_bastfn : NULL;
3200 lkb->lkb_astfn = (ms->m_asts & DLM_CB_CAST) ? &fake_astfn : NULL;
e7fd4179 3201
8d07fd50
DT
3202 if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
3203 /* lkb was just created so there won't be an lvb yet */
52bda2b5 3204 lkb->lkb_lvbptr = dlm_allocate_lvb(ls);
8d07fd50
DT
3205 if (!lkb->lkb_lvbptr)
3206 return -ENOMEM;
3207 }
e7fd4179
DT
3208
3209 return 0;
3210}
3211
3212static int receive_convert_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
3213 struct dlm_message *ms)
3214{
e7fd4179
DT
3215 if (lkb->lkb_status != DLM_LKSTS_GRANTED)
3216 return -EBUSY;
3217
e7fd4179
DT
3218 if (receive_lvb(ls, lkb, ms))
3219 return -ENOMEM;
3220
3221 lkb->lkb_rqmode = ms->m_rqmode;
3222 lkb->lkb_lvbseq = ms->m_lvbseq;
3223
3224 return 0;
3225}
3226
3227static int receive_unlock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
3228 struct dlm_message *ms)
3229{
e7fd4179
DT
3230 if (receive_lvb(ls, lkb, ms))
3231 return -ENOMEM;
3232 return 0;
3233}
3234
3235/* We fill in the stub-lkb fields with the info that send_xxxx_reply()
3236 uses to send a reply and that the remote end uses to process the reply. */
3237
3238static void setup_stub_lkb(struct dlm_ls *ls, struct dlm_message *ms)
3239{
3240 struct dlm_lkb *lkb = &ls->ls_stub_lkb;
3241 lkb->lkb_nodeid = ms->m_header.h_nodeid;
3242 lkb->lkb_remid = ms->m_lkid;
3243}
3244
c54e04b0
DT
3245/* This is called after the rsb is locked so that we can safely inspect
3246 fields in the lkb. */
3247
3248static int validate_message(struct dlm_lkb *lkb, struct dlm_message *ms)
3249{
3250 int from = ms->m_header.h_nodeid;
3251 int error = 0;
3252
3253 switch (ms->m_type) {
3254 case DLM_MSG_CONVERT:
3255 case DLM_MSG_UNLOCK:
3256 case DLM_MSG_CANCEL:
3257 if (!is_master_copy(lkb) || lkb->lkb_nodeid != from)
3258 error = -EINVAL;
3259 break;
3260
3261 case DLM_MSG_CONVERT_REPLY:
3262 case DLM_MSG_UNLOCK_REPLY:
3263 case DLM_MSG_CANCEL_REPLY:
3264 case DLM_MSG_GRANT:
3265 case DLM_MSG_BAST:
3266 if (!is_process_copy(lkb) || lkb->lkb_nodeid != from)
3267 error = -EINVAL;
3268 break;
3269
3270 case DLM_MSG_REQUEST_REPLY:
3271 if (!is_process_copy(lkb))
3272 error = -EINVAL;
3273 else if (lkb->lkb_nodeid != -1 && lkb->lkb_nodeid != from)
3274 error = -EINVAL;
3275 break;
3276
3277 default:
3278 error = -EINVAL;
3279 }
3280
3281 if (error)
3282 log_error(lkb->lkb_resource->res_ls,
3283 "ignore invalid message %d from %d %x %x %x %d",
3284 ms->m_type, from, lkb->lkb_id, lkb->lkb_remid,
3285 lkb->lkb_flags, lkb->lkb_nodeid);
3286 return error;
3287}
3288
e7fd4179
DT
3289static void receive_request(struct dlm_ls *ls, struct dlm_message *ms)
3290{
3291 struct dlm_lkb *lkb;
3292 struct dlm_rsb *r;
3293 int error, namelen;
3294
3295 error = create_lkb(ls, &lkb);
3296 if (error)
3297 goto fail;
3298
3299 receive_flags(lkb, ms);
3300 lkb->lkb_flags |= DLM_IFL_MSTCPY;
3301 error = receive_request_args(ls, lkb, ms);
3302 if (error) {
b3f58d8f 3303 __put_lkb(ls, lkb);
e7fd4179
DT
3304 goto fail;
3305 }
3306
3307 namelen = receive_extralen(ms);
3308
3309 error = find_rsb(ls, ms->m_extra, namelen, R_MASTER, &r);
3310 if (error) {
b3f58d8f 3311 __put_lkb(ls, lkb);
e7fd4179
DT
3312 goto fail;
3313 }
3314
3315 lock_rsb(r);
3316
3317 attach_lkb(r, lkb);
3318 error = do_request(r, lkb);
3319 send_request_reply(r, lkb, error);
cf6620ac 3320 do_request_effects(r, lkb, error);
e7fd4179
DT
3321
3322 unlock_rsb(r);
3323 put_rsb(r);
3324
3325 if (error == -EINPROGRESS)
3326 error = 0;
3327 if (error)
b3f58d8f 3328 dlm_put_lkb(lkb);
e7fd4179
DT
3329 return;
3330
3331 fail:
3332 setup_stub_lkb(ls, ms);
3333 send_request_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
3334}
3335
3336static void receive_convert(struct dlm_ls *ls, struct dlm_message *ms)
3337{
3338 struct dlm_lkb *lkb;
3339 struct dlm_rsb *r;
90135925 3340 int error, reply = 1;
e7fd4179
DT
3341
3342 error = find_lkb(ls, ms->m_remid, &lkb);
3343 if (error)
3344 goto fail;
3345
3346 r = lkb->lkb_resource;
3347
3348 hold_rsb(r);
3349 lock_rsb(r);
3350
c54e04b0
DT
3351 error = validate_message(lkb, ms);
3352 if (error)
3353 goto out;
3354
e7fd4179 3355 receive_flags(lkb, ms);
cf6620ac 3356
e7fd4179 3357 error = receive_convert_args(ls, lkb, ms);
cf6620ac
DT
3358 if (error) {
3359 send_convert_reply(r, lkb, error);
3360 goto out;
3361 }
3362
e7fd4179
DT
3363 reply = !down_conversion(lkb);
3364
3365 error = do_convert(r, lkb);
e7fd4179
DT
3366 if (reply)
3367 send_convert_reply(r, lkb, error);
cf6620ac 3368 do_convert_effects(r, lkb, error);
c54e04b0 3369 out:
e7fd4179
DT
3370 unlock_rsb(r);
3371 put_rsb(r);
b3f58d8f 3372 dlm_put_lkb(lkb);
e7fd4179
DT
3373 return;
3374
3375 fail:
3376 setup_stub_lkb(ls, ms);
3377 send_convert_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
3378}
3379
3380static void receive_unlock(struct dlm_ls *ls, struct dlm_message *ms)
3381{
3382 struct dlm_lkb *lkb;
3383 struct dlm_rsb *r;
3384 int error;
3385
3386 error = find_lkb(ls, ms->m_remid, &lkb);
3387 if (error)
3388 goto fail;
3389
3390 r = lkb->lkb_resource;
3391
3392 hold_rsb(r);
3393 lock_rsb(r);
3394
c54e04b0
DT
3395 error = validate_message(lkb, ms);
3396 if (error)
3397 goto out;
3398
e7fd4179 3399 receive_flags(lkb, ms);
cf6620ac 3400
e7fd4179 3401 error = receive_unlock_args(ls, lkb, ms);
cf6620ac
DT
3402 if (error) {
3403 send_unlock_reply(r, lkb, error);
3404 goto out;
3405 }
e7fd4179
DT
3406
3407 error = do_unlock(r, lkb);
e7fd4179 3408 send_unlock_reply(r, lkb, error);
cf6620ac 3409 do_unlock_effects(r, lkb, error);
c54e04b0 3410 out:
e7fd4179
DT
3411 unlock_rsb(r);
3412 put_rsb(r);
b3f58d8f 3413 dlm_put_lkb(lkb);
e7fd4179
DT
3414 return;
3415
3416 fail:
3417 setup_stub_lkb(ls, ms);
3418 send_unlock_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
3419}
3420
3421static void receive_cancel(struct dlm_ls *ls, struct dlm_message *ms)
3422{
3423 struct dlm_lkb *lkb;
3424 struct dlm_rsb *r;
3425 int error;
3426
3427 error = find_lkb(ls, ms->m_remid, &lkb);
3428 if (error)
3429 goto fail;
3430
3431 receive_flags(lkb, ms);
3432
3433 r = lkb->lkb_resource;
3434
3435 hold_rsb(r);
3436 lock_rsb(r);
3437
c54e04b0
DT
3438 error = validate_message(lkb, ms);
3439 if (error)
3440 goto out;
3441
e7fd4179
DT
3442 error = do_cancel(r, lkb);
3443 send_cancel_reply(r, lkb, error);
cf6620ac 3444 do_cancel_effects(r, lkb, error);
c54e04b0 3445 out:
e7fd4179
DT
3446 unlock_rsb(r);
3447 put_rsb(r);
b3f58d8f 3448 dlm_put_lkb(lkb);
e7fd4179
DT
3449 return;
3450
3451 fail:
3452 setup_stub_lkb(ls, ms);
3453 send_cancel_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
3454}
3455
3456static void receive_grant(struct dlm_ls *ls, struct dlm_message *ms)
3457{
3458 struct dlm_lkb *lkb;
3459 struct dlm_rsb *r;
3460 int error;
3461
3462 error = find_lkb(ls, ms->m_remid, &lkb);
3463 if (error) {
c54e04b0
DT
3464 log_debug(ls, "receive_grant from %d no lkb %x",
3465 ms->m_header.h_nodeid, ms->m_remid);
e7fd4179
DT
3466 return;
3467 }
e7fd4179
DT
3468
3469 r = lkb->lkb_resource;
3470
3471 hold_rsb(r);
3472 lock_rsb(r);
3473
c54e04b0
DT
3474 error = validate_message(lkb, ms);
3475 if (error)
3476 goto out;
3477
e7fd4179 3478 receive_flags_reply(lkb, ms);
7d3c1feb
DT
3479 if (is_altmode(lkb))
3480 munge_altmode(lkb, ms);
e7fd4179
DT
3481 grant_lock_pc(r, lkb, ms);
3482 queue_cast(r, lkb, 0);
c54e04b0 3483 out:
e7fd4179
DT
3484 unlock_rsb(r);
3485 put_rsb(r);
b3f58d8f 3486 dlm_put_lkb(lkb);
e7fd4179
DT
3487}
3488
3489static void receive_bast(struct dlm_ls *ls, struct dlm_message *ms)
3490{
3491 struct dlm_lkb *lkb;
3492 struct dlm_rsb *r;
3493 int error;
3494
3495 error = find_lkb(ls, ms->m_remid, &lkb);
3496 if (error) {
c54e04b0
DT
3497 log_debug(ls, "receive_bast from %d no lkb %x",
3498 ms->m_header.h_nodeid, ms->m_remid);
e7fd4179
DT
3499 return;
3500 }
e7fd4179
DT
3501
3502 r = lkb->lkb_resource;
3503
3504 hold_rsb(r);
3505 lock_rsb(r);
3506
c54e04b0
DT
3507 error = validate_message(lkb, ms);
3508 if (error)
3509 goto out;
e7fd4179 3510
c54e04b0
DT
3511 queue_bast(r, lkb, ms->m_bastmode);
3512 out:
e7fd4179
DT
3513 unlock_rsb(r);
3514 put_rsb(r);
b3f58d8f 3515 dlm_put_lkb(lkb);
e7fd4179
DT
3516}
3517
3518static void receive_lookup(struct dlm_ls *ls, struct dlm_message *ms)
3519{
3520 int len, error, ret_nodeid, dir_nodeid, from_nodeid, our_nodeid;
3521
3522 from_nodeid = ms->m_header.h_nodeid;
3523 our_nodeid = dlm_our_nodeid();
3524
3525 len = receive_extralen(ms);
3526
3527 dir_nodeid = dlm_hash2nodeid(ls, ms->m_hash);
3528 if (dir_nodeid != our_nodeid) {
3529 log_error(ls, "lookup dir_nodeid %d from %d",
3530 dir_nodeid, from_nodeid);
3531 error = -EINVAL;
3532 ret_nodeid = -1;
3533 goto out;
3534 }
3535
3536 error = dlm_dir_lookup(ls, from_nodeid, ms->m_extra, len, &ret_nodeid);
3537
3538 /* Optimization: we're master so treat lookup as a request */
3539 if (!error && ret_nodeid == our_nodeid) {
3540 receive_request(ls, ms);
3541 return;
3542 }
3543 out:
3544 send_lookup_reply(ls, ms, ret_nodeid, error);
3545}
3546
3547static void receive_remove(struct dlm_ls *ls, struct dlm_message *ms)
3548{
3549 int len, dir_nodeid, from_nodeid;
3550
3551 from_nodeid = ms->m_header.h_nodeid;
3552
3553 len = receive_extralen(ms);
3554
3555 dir_nodeid = dlm_hash2nodeid(ls, ms->m_hash);
3556 if (dir_nodeid != dlm_our_nodeid()) {
3557 log_error(ls, "remove dir entry dir_nodeid %d from %d",
3558 dir_nodeid, from_nodeid);
3559 return;
3560 }
3561
3562 dlm_dir_remove_entry(ls, from_nodeid, ms->m_extra, len);
3563}
3564
8499137d
DT
3565static void receive_purge(struct dlm_ls *ls, struct dlm_message *ms)
3566{
3567 do_purge(ls, ms->m_nodeid, ms->m_pid);
3568}
3569
e7fd4179
DT
3570static void receive_request_reply(struct dlm_ls *ls, struct dlm_message *ms)
3571{
3572 struct dlm_lkb *lkb;
3573 struct dlm_rsb *r;
ef0c2bb0 3574 int error, mstype, result;
e7fd4179
DT
3575
3576 error = find_lkb(ls, ms->m_remid, &lkb);
3577 if (error) {
c54e04b0
DT
3578 log_debug(ls, "receive_request_reply from %d no lkb %x",
3579 ms->m_header.h_nodeid, ms->m_remid);
e7fd4179
DT
3580 return;
3581 }
e7fd4179 3582
e7fd4179
DT
3583 r = lkb->lkb_resource;
3584 hold_rsb(r);
3585 lock_rsb(r);
3586
c54e04b0
DT
3587 error = validate_message(lkb, ms);
3588 if (error)
3589 goto out;
3590
ef0c2bb0
DT
3591 mstype = lkb->lkb_wait_type;
3592 error = remove_from_waiters(lkb, DLM_MSG_REQUEST_REPLY);
3593 if (error)
3594 goto out;
3595
e7fd4179
DT
3596 /* Optimization: the dir node was also the master, so it took our
3597 lookup as a request and sent request reply instead of lookup reply */
3598 if (mstype == DLM_MSG_LOOKUP) {
3599 r->res_nodeid = ms->m_header.h_nodeid;
3600 lkb->lkb_nodeid = r->res_nodeid;
3601 }
3602
ef0c2bb0
DT
3603 /* this is the value returned from do_request() on the master */
3604 result = ms->m_result;
3605
3606 switch (result) {
e7fd4179 3607 case -EAGAIN:
ef0c2bb0 3608 /* request would block (be queued) on remote master */
e7fd4179
DT
3609 queue_cast(r, lkb, -EAGAIN);
3610 confirm_master(r, -EAGAIN);
ef0c2bb0 3611 unhold_lkb(lkb); /* undoes create_lkb() */
e7fd4179
DT
3612 break;
3613
3614 case -EINPROGRESS:
3615 case 0:
3616 /* request was queued or granted on remote master */
3617 receive_flags_reply(lkb, ms);
3618 lkb->lkb_remid = ms->m_lkid;
7d3c1feb
DT
3619 if (is_altmode(lkb))
3620 munge_altmode(lkb, ms);
3ae1acf9 3621 if (result) {
e7fd4179 3622 add_lkb(r, lkb, DLM_LKSTS_WAITING);
3ae1acf9
DT
3623 add_timeout(lkb);
3624 } else {
e7fd4179
DT
3625 grant_lock_pc(r, lkb, ms);
3626 queue_cast(r, lkb, 0);
3627 }
ef0c2bb0 3628 confirm_master(r, result);
e7fd4179
DT
3629 break;
3630
597d0cae 3631 case -EBADR:
e7fd4179
DT
3632 case -ENOTBLK:
3633 /* find_rsb failed to find rsb or rsb wasn't master */
ef0c2bb0
DT
3634 log_debug(ls, "receive_request_reply %x %x master diff %d %d",
3635 lkb->lkb_id, lkb->lkb_flags, r->res_nodeid, result);
e7fd4179
DT
3636 r->res_nodeid = -1;
3637 lkb->lkb_nodeid = -1;
ef0c2bb0
DT
3638
3639 if (is_overlap(lkb)) {
3640 /* we'll ignore error in cancel/unlock reply */
3641 queue_cast_overlap(r, lkb);
aec64e1b 3642 confirm_master(r, result);
ef0c2bb0
DT
3643 unhold_lkb(lkb); /* undoes create_lkb() */
3644 } else
3645 _request_lock(r, lkb);
e7fd4179
DT
3646 break;
3647
3648 default:
ef0c2bb0
DT
3649 log_error(ls, "receive_request_reply %x error %d",
3650 lkb->lkb_id, result);
e7fd4179
DT
3651 }
3652
ef0c2bb0
DT
3653 if (is_overlap_unlock(lkb) && (result == 0 || result == -EINPROGRESS)) {
3654 log_debug(ls, "receive_request_reply %x result %d unlock",
3655 lkb->lkb_id, result);
3656 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
3657 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
3658 send_unlock(r, lkb);
3659 } else if (is_overlap_cancel(lkb) && (result == -EINPROGRESS)) {
3660 log_debug(ls, "receive_request_reply %x cancel", lkb->lkb_id);
3661 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
3662 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
3663 send_cancel(r, lkb);
3664 } else {
3665 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
3666 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
3667 }
3668 out:
e7fd4179
DT
3669 unlock_rsb(r);
3670 put_rsb(r);
b3f58d8f 3671 dlm_put_lkb(lkb);
e7fd4179
DT
3672}
3673
3674static void __receive_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
3675 struct dlm_message *ms)
3676{
e7fd4179 3677 /* this is the value returned from do_convert() on the master */
ef0c2bb0 3678 switch (ms->m_result) {
e7fd4179
DT
3679 case -EAGAIN:
3680 /* convert would block (be queued) on remote master */
3681 queue_cast(r, lkb, -EAGAIN);
3682 break;
3683
c85d65e9
DT
3684 case -EDEADLK:
3685 receive_flags_reply(lkb, ms);
3686 revert_lock_pc(r, lkb);
3687 queue_cast(r, lkb, -EDEADLK);
3688 break;
3689
e7fd4179
DT
3690 case -EINPROGRESS:
3691 /* convert was queued on remote master */
7d3c1feb
DT
3692 receive_flags_reply(lkb, ms);
3693 if (is_demoted(lkb))
2a7ce0ed 3694 munge_demoted(lkb);
e7fd4179
DT
3695 del_lkb(r, lkb);
3696 add_lkb(r, lkb, DLM_LKSTS_CONVERT);
3ae1acf9 3697 add_timeout(lkb);
e7fd4179
DT
3698 break;
3699
3700 case 0:
3701 /* convert was granted on remote master */
3702 receive_flags_reply(lkb, ms);
7d3c1feb 3703 if (is_demoted(lkb))
2a7ce0ed 3704 munge_demoted(lkb);
e7fd4179
DT
3705 grant_lock_pc(r, lkb, ms);
3706 queue_cast(r, lkb, 0);
3707 break;
3708
3709 default:
ef0c2bb0
DT
3710 log_error(r->res_ls, "receive_convert_reply %x error %d",
3711 lkb->lkb_id, ms->m_result);
e7fd4179
DT
3712 }
3713}
3714
3715static void _receive_convert_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
3716{
3717 struct dlm_rsb *r = lkb->lkb_resource;
ef0c2bb0 3718 int error;
e7fd4179
DT
3719
3720 hold_rsb(r);
3721 lock_rsb(r);
3722
c54e04b0
DT
3723 error = validate_message(lkb, ms);
3724 if (error)
3725 goto out;
3726
ef0c2bb0
DT
3727 /* stub reply can happen with waiters_mutex held */
3728 error = remove_from_waiters_ms(lkb, ms);
3729 if (error)
3730 goto out;
e7fd4179 3731
ef0c2bb0
DT
3732 __receive_convert_reply(r, lkb, ms);
3733 out:
e7fd4179
DT
3734 unlock_rsb(r);
3735 put_rsb(r);
3736}
3737
3738static void receive_convert_reply(struct dlm_ls *ls, struct dlm_message *ms)
3739{
3740 struct dlm_lkb *lkb;
3741 int error;
3742
3743 error = find_lkb(ls, ms->m_remid, &lkb);
3744 if (error) {
c54e04b0
DT
3745 log_debug(ls, "receive_convert_reply from %d no lkb %x",
3746 ms->m_header.h_nodeid, ms->m_remid);
e7fd4179
DT
3747 return;
3748 }
e7fd4179 3749
e7fd4179 3750 _receive_convert_reply(lkb, ms);
b3f58d8f 3751 dlm_put_lkb(lkb);
e7fd4179
DT
3752}
3753
3754static void _receive_unlock_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
3755{
3756 struct dlm_rsb *r = lkb->lkb_resource;
ef0c2bb0 3757 int error;
e7fd4179
DT
3758
3759 hold_rsb(r);
3760 lock_rsb(r);
3761
c54e04b0
DT
3762 error = validate_message(lkb, ms);
3763 if (error)
3764 goto out;
3765
ef0c2bb0
DT
3766 /* stub reply can happen with waiters_mutex held */
3767 error = remove_from_waiters_ms(lkb, ms);
3768 if (error)
3769 goto out;
3770
e7fd4179
DT
3771 /* this is the value returned from do_unlock() on the master */
3772
ef0c2bb0 3773 switch (ms->m_result) {
e7fd4179
DT
3774 case -DLM_EUNLOCK:
3775 receive_flags_reply(lkb, ms);
3776 remove_lock_pc(r, lkb);
3777 queue_cast(r, lkb, -DLM_EUNLOCK);
3778 break;
ef0c2bb0
DT
3779 case -ENOENT:
3780 break;
e7fd4179 3781 default:
ef0c2bb0
DT
3782 log_error(r->res_ls, "receive_unlock_reply %x error %d",
3783 lkb->lkb_id, ms->m_result);
e7fd4179 3784 }
ef0c2bb0 3785 out:
e7fd4179
DT
3786 unlock_rsb(r);
3787 put_rsb(r);
3788}
3789
3790static void receive_unlock_reply(struct dlm_ls *ls, struct dlm_message *ms)
3791{
3792 struct dlm_lkb *lkb;
3793 int error;
3794
3795 error = find_lkb(ls, ms->m_remid, &lkb);
3796 if (error) {
c54e04b0
DT
3797 log_debug(ls, "receive_unlock_reply from %d no lkb %x",
3798 ms->m_header.h_nodeid, ms->m_remid);
e7fd4179
DT
3799 return;
3800 }
e7fd4179 3801
e7fd4179 3802 _receive_unlock_reply(lkb, ms);
b3f58d8f 3803 dlm_put_lkb(lkb);
e7fd4179
DT
3804}
3805
3806static void _receive_cancel_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
3807{
3808 struct dlm_rsb *r = lkb->lkb_resource;
ef0c2bb0 3809 int error;
e7fd4179
DT
3810
3811 hold_rsb(r);
3812 lock_rsb(r);
3813
c54e04b0
DT
3814 error = validate_message(lkb, ms);
3815 if (error)
3816 goto out;
3817
ef0c2bb0
DT
3818 /* stub reply can happen with waiters_mutex held */
3819 error = remove_from_waiters_ms(lkb, ms);
3820 if (error)
3821 goto out;
3822
e7fd4179
DT
3823 /* this is the value returned from do_cancel() on the master */
3824
ef0c2bb0 3825 switch (ms->m_result) {
e7fd4179
DT
3826 case -DLM_ECANCEL:
3827 receive_flags_reply(lkb, ms);
3828 revert_lock_pc(r, lkb);
84d8cd69 3829 queue_cast(r, lkb, -DLM_ECANCEL);
ef0c2bb0
DT
3830 break;
3831 case 0:
e7fd4179
DT
3832 break;
3833 default:
ef0c2bb0
DT
3834 log_error(r->res_ls, "receive_cancel_reply %x error %d",
3835 lkb->lkb_id, ms->m_result);
e7fd4179 3836 }
ef0c2bb0 3837 out:
e7fd4179
DT
3838 unlock_rsb(r);
3839 put_rsb(r);
3840}
3841
3842static void receive_cancel_reply(struct dlm_ls *ls, struct dlm_message *ms)
3843{
3844 struct dlm_lkb *lkb;
3845 int error;
3846
3847 error = find_lkb(ls, ms->m_remid, &lkb);
3848 if (error) {
c54e04b0
DT
3849 log_debug(ls, "receive_cancel_reply from %d no lkb %x",
3850 ms->m_header.h_nodeid, ms->m_remid);
e7fd4179
DT
3851 return;
3852 }
e7fd4179 3853
e7fd4179 3854 _receive_cancel_reply(lkb, ms);
b3f58d8f 3855 dlm_put_lkb(lkb);
e7fd4179
DT
3856}
3857
3858static void receive_lookup_reply(struct dlm_ls *ls, struct dlm_message *ms)
3859{
3860 struct dlm_lkb *lkb;
3861 struct dlm_rsb *r;
3862 int error, ret_nodeid;
3863
3864 error = find_lkb(ls, ms->m_lkid, &lkb);
3865 if (error) {
3866 log_error(ls, "receive_lookup_reply no lkb");
3867 return;
3868 }
3869
ef0c2bb0 3870 /* ms->m_result is the value returned by dlm_dir_lookup on dir node
e7fd4179 3871 FIXME: will a non-zero error ever be returned? */
e7fd4179
DT
3872
3873 r = lkb->lkb_resource;
3874 hold_rsb(r);
3875 lock_rsb(r);
3876
ef0c2bb0
DT
3877 error = remove_from_waiters(lkb, DLM_MSG_LOOKUP_REPLY);
3878 if (error)
3879 goto out;
3880
e7fd4179
DT
3881 ret_nodeid = ms->m_nodeid;
3882 if (ret_nodeid == dlm_our_nodeid()) {
3883 r->res_nodeid = 0;
3884 ret_nodeid = 0;
3885 r->res_first_lkid = 0;
3886 } else {
3887 /* set_master() will copy res_nodeid to lkb_nodeid */
3888 r->res_nodeid = ret_nodeid;
3889 }
3890
ef0c2bb0
DT
3891 if (is_overlap(lkb)) {
3892 log_debug(ls, "receive_lookup_reply %x unlock %x",
3893 lkb->lkb_id, lkb->lkb_flags);
3894 queue_cast_overlap(r, lkb);
3895 unhold_lkb(lkb); /* undoes create_lkb() */
3896 goto out_list;
3897 }
3898
e7fd4179
DT
3899 _request_lock(r, lkb);
3900
ef0c2bb0 3901 out_list:
e7fd4179
DT
3902 if (!ret_nodeid)
3903 process_lookup_list(r);
ef0c2bb0 3904 out:
e7fd4179
DT
3905 unlock_rsb(r);
3906 put_rsb(r);
b3f58d8f 3907 dlm_put_lkb(lkb);
e7fd4179
DT
3908}
3909
c36258b5 3910static void _receive_message(struct dlm_ls *ls, struct dlm_message *ms)
e7fd4179 3911{
46b43eed
DT
3912 if (!dlm_is_member(ls, ms->m_header.h_nodeid)) {
3913 log_debug(ls, "ignore non-member message %d from %d %x %x %d",
3914 ms->m_type, ms->m_header.h_nodeid, ms->m_lkid,
3915 ms->m_remid, ms->m_result);
3916 return;
3917 }
3918
e7fd4179
DT
3919 switch (ms->m_type) {
3920
3921 /* messages sent to a master node */
3922
3923 case DLM_MSG_REQUEST:
3924 receive_request(ls, ms);
3925 break;
3926
3927 case DLM_MSG_CONVERT:
3928 receive_convert(ls, ms);
3929 break;
3930
3931 case DLM_MSG_UNLOCK:
3932 receive_unlock(ls, ms);
3933 break;
3934
3935 case DLM_MSG_CANCEL:
3936 receive_cancel(ls, ms);
3937 break;
3938
3939 /* messages sent from a master node (replies to above) */
3940
3941 case DLM_MSG_REQUEST_REPLY:
3942 receive_request_reply(ls, ms);
3943 break;
3944
3945 case DLM_MSG_CONVERT_REPLY:
3946 receive_convert_reply(ls, ms);
3947 break;
3948
3949 case DLM_MSG_UNLOCK_REPLY:
3950 receive_unlock_reply(ls, ms);
3951 break;
3952
3953 case DLM_MSG_CANCEL_REPLY:
3954 receive_cancel_reply(ls, ms);
3955 break;
3956
3957 /* messages sent from a master node (only two types of async msg) */
3958
3959 case DLM_MSG_GRANT:
3960 receive_grant(ls, ms);
3961 break;
3962
3963 case DLM_MSG_BAST:
3964 receive_bast(ls, ms);
3965 break;
3966
3967 /* messages sent to a dir node */
3968
3969 case DLM_MSG_LOOKUP:
3970 receive_lookup(ls, ms);
3971 break;
3972
3973 case DLM_MSG_REMOVE:
3974 receive_remove(ls, ms);
3975 break;
3976
3977 /* messages sent from a dir node (remove has no reply) */
3978
3979 case DLM_MSG_LOOKUP_REPLY:
3980 receive_lookup_reply(ls, ms);
3981 break;
3982
8499137d
DT
3983 /* other messages */
3984
3985 case DLM_MSG_PURGE:
3986 receive_purge(ls, ms);
3987 break;
3988
e7fd4179
DT
3989 default:
3990 log_error(ls, "unknown message type %d", ms->m_type);
3991 }
3992
e7fd4179 3993 dlm_astd_wake();
e7fd4179
DT
3994}
3995
c36258b5
DT
3996/* If the lockspace is in recovery mode (locking stopped), then normal
3997 messages are saved on the requestqueue for processing after recovery is
3998 done. When not in recovery mode, we wait for dlm_recoverd to drain saved
3999 messages off the requestqueue before we process new ones. This occurs right
4000 after recovery completes when we transition from saving all messages on
4001 requestqueue, to processing all the saved messages, to processing new
4002 messages as they arrive. */
e7fd4179 4003
c36258b5
DT
4004static void dlm_receive_message(struct dlm_ls *ls, struct dlm_message *ms,
4005 int nodeid)
4006{
4007 if (dlm_locking_stopped(ls)) {
8b0d8e03 4008 dlm_add_requestqueue(ls, nodeid, ms);
c36258b5
DT
4009 } else {
4010 dlm_wait_requestqueue(ls);
4011 _receive_message(ls, ms);
4012 }
4013}
4014
4015/* This is called by dlm_recoverd to process messages that were saved on
4016 the requestqueue. */
4017
4018void dlm_receive_message_saved(struct dlm_ls *ls, struct dlm_message *ms)
4019{
4020 _receive_message(ls, ms);
4021}
4022
4023/* This is called by the midcomms layer when something is received for
4024 the lockspace. It could be either a MSG (normal message sent as part of
4025 standard locking activity) or an RCOM (recovery message sent as part of
4026 lockspace recovery). */
4027
eef7d739 4028void dlm_receive_buffer(union dlm_packet *p, int nodeid)
c36258b5 4029{
eef7d739 4030 struct dlm_header *hd = &p->header;
c36258b5
DT
4031 struct dlm_ls *ls;
4032 int type = 0;
4033
4034 switch (hd->h_cmd) {
4035 case DLM_MSG:
eef7d739
AV
4036 dlm_message_in(&p->message);
4037 type = p->message.m_type;
c36258b5
DT
4038 break;
4039 case DLM_RCOM:
eef7d739
AV
4040 dlm_rcom_in(&p->rcom);
4041 type = p->rcom.rc_type;
c36258b5
DT
4042 break;
4043 default:
4044 log_print("invalid h_cmd %d from %u", hd->h_cmd, nodeid);
4045 return;
4046 }
4047
4048 if (hd->h_nodeid != nodeid) {
4049 log_print("invalid h_nodeid %d from %d lockspace %x",
4050 hd->h_nodeid, nodeid, hd->h_lockspace);
4051 return;
4052 }
4053
4054 ls = dlm_find_lockspace_global(hd->h_lockspace);
4055 if (!ls) {
594199eb
DT
4056 if (dlm_config.ci_log_debug)
4057 log_print("invalid lockspace %x from %d cmd %d type %d",
4058 hd->h_lockspace, nodeid, hd->h_cmd, type);
c36258b5
DT
4059
4060 if (hd->h_cmd == DLM_RCOM && type == DLM_RCOM_STATUS)
eef7d739 4061 dlm_send_ls_not_ready(nodeid, &p->rcom);
c36258b5
DT
4062 return;
4063 }
4064
4065 /* this rwsem allows dlm_ls_stop() to wait for all dlm_recv threads to
4066 be inactive (in this ls) before transitioning to recovery mode */
4067
4068 down_read(&ls->ls_recv_active);
4069 if (hd->h_cmd == DLM_MSG)
eef7d739 4070 dlm_receive_message(ls, &p->message, nodeid);
c36258b5 4071 else
eef7d739 4072 dlm_receive_rcom(ls, &p->rcom, nodeid);
c36258b5
DT
4073 up_read(&ls->ls_recv_active);
4074
4075 dlm_put_lockspace(ls);
4076}
e7fd4179 4077
2a7ce0ed
DT
4078static void recover_convert_waiter(struct dlm_ls *ls, struct dlm_lkb *lkb,
4079 struct dlm_message *ms_stub)
e7fd4179
DT
4080{
4081 if (middle_conversion(lkb)) {
4082 hold_lkb(lkb);
2a7ce0ed
DT
4083 memset(ms_stub, 0, sizeof(struct dlm_message));
4084 ms_stub->m_flags = DLM_IFL_STUB_MS;
4085 ms_stub->m_type = DLM_MSG_CONVERT_REPLY;
4086 ms_stub->m_result = -EINPROGRESS;
4087 ms_stub->m_header.h_nodeid = lkb->lkb_nodeid;
4088 _receive_convert_reply(lkb, ms_stub);
e7fd4179
DT
4089
4090 /* Same special case as in receive_rcom_lock_args() */
4091 lkb->lkb_grmode = DLM_LOCK_IV;
4092 rsb_set_flag(lkb->lkb_resource, RSB_RECOVER_CONVERT);
4093 unhold_lkb(lkb);
4094
4095 } else if (lkb->lkb_rqmode >= lkb->lkb_grmode) {
4096 lkb->lkb_flags |= DLM_IFL_RESEND;
4097 }
4098
4099 /* lkb->lkb_rqmode < lkb->lkb_grmode shouldn't happen since down
4100 conversions are async; there's no reply from the remote master */
4101}
4102
4103/* A waiting lkb needs recovery if the master node has failed, or
4104 the master node is changing (only when no directory is used) */
4105
4106static int waiter_needs_recovery(struct dlm_ls *ls, struct dlm_lkb *lkb)
4107{
4108 if (dlm_is_removed(ls, lkb->lkb_nodeid))
4109 return 1;
4110
4111 if (!dlm_no_directory(ls))
4112 return 0;
4113
4114 if (dlm_dir_nodeid(lkb->lkb_resource) != lkb->lkb_nodeid)
4115 return 1;
4116
4117 return 0;
4118}
4119
4120/* Recovery for locks that are waiting for replies from nodes that are now
4121 gone. We can just complete unlocks and cancels by faking a reply from the
4122 dead node. Requests and up-conversions we flag to be resent after
4123 recovery. Down-conversions can just be completed with a fake reply like
4124 unlocks. Conversions between PR and CW need special attention. */
4125
4126void dlm_recover_waiters_pre(struct dlm_ls *ls)
4127{
4128 struct dlm_lkb *lkb, *safe;
2a7ce0ed 4129 struct dlm_message *ms_stub;
601342ce 4130 int wait_type, stub_unlock_result, stub_cancel_result;
e7fd4179 4131
2a7ce0ed
DT
4132 ms_stub = kmalloc(GFP_KERNEL, sizeof(struct dlm_message));
4133 if (!ms_stub) {
4134 log_error(ls, "dlm_recover_waiters_pre no mem");
4135 return;
4136 }
4137
90135925 4138 mutex_lock(&ls->ls_waiters_mutex);
e7fd4179
DT
4139
4140 list_for_each_entry_safe(lkb, safe, &ls->ls_waiters, lkb_wait_reply) {
2a7ce0ed
DT
4141
4142 /* exclude debug messages about unlocks because there can be so
4143 many and they aren't very interesting */
4144
4145 if (lkb->lkb_wait_type != DLM_MSG_UNLOCK) {
4146 log_debug(ls, "recover_waiter %x nodeid %d "
4147 "msg %d to %d", lkb->lkb_id, lkb->lkb_nodeid,
4148 lkb->lkb_wait_type, lkb->lkb_wait_nodeid);
4149 }
e7fd4179
DT
4150
4151 /* all outstanding lookups, regardless of destination will be
4152 resent after recovery is done */
4153
4154 if (lkb->lkb_wait_type == DLM_MSG_LOOKUP) {
4155 lkb->lkb_flags |= DLM_IFL_RESEND;
4156 continue;
4157 }
4158
4159 if (!waiter_needs_recovery(ls, lkb))
4160 continue;
4161
601342ce
DT
4162 wait_type = lkb->lkb_wait_type;
4163 stub_unlock_result = -DLM_EUNLOCK;
4164 stub_cancel_result = -DLM_ECANCEL;
4165
4166 /* Main reply may have been received leaving a zero wait_type,
4167 but a reply for the overlapping op may not have been
4168 received. In that case we need to fake the appropriate
4169 reply for the overlap op. */
4170
4171 if (!wait_type) {
4172 if (is_overlap_cancel(lkb)) {
4173 wait_type = DLM_MSG_CANCEL;
4174 if (lkb->lkb_grmode == DLM_LOCK_IV)
4175 stub_cancel_result = 0;
4176 }
4177 if (is_overlap_unlock(lkb)) {
4178 wait_type = DLM_MSG_UNLOCK;
4179 if (lkb->lkb_grmode == DLM_LOCK_IV)
4180 stub_unlock_result = -ENOENT;
4181 }
4182
4183 log_debug(ls, "rwpre overlap %x %x %d %d %d",
4184 lkb->lkb_id, lkb->lkb_flags, wait_type,
4185 stub_cancel_result, stub_unlock_result);
4186 }
4187
4188 switch (wait_type) {
e7fd4179
DT
4189
4190 case DLM_MSG_REQUEST:
4191 lkb->lkb_flags |= DLM_IFL_RESEND;
4192 break;
4193
4194 case DLM_MSG_CONVERT:
2a7ce0ed 4195 recover_convert_waiter(ls, lkb, ms_stub);
e7fd4179
DT
4196 break;
4197
4198 case DLM_MSG_UNLOCK:
4199 hold_lkb(lkb);
2a7ce0ed
DT
4200 memset(ms_stub, 0, sizeof(struct dlm_message));
4201 ms_stub->m_flags = DLM_IFL_STUB_MS;
4202 ms_stub->m_type = DLM_MSG_UNLOCK_REPLY;
4203 ms_stub->m_result = stub_unlock_result;
4204 ms_stub->m_header.h_nodeid = lkb->lkb_nodeid;
4205 _receive_unlock_reply(lkb, ms_stub);
b3f58d8f 4206 dlm_put_lkb(lkb);
e7fd4179
DT
4207 break;
4208
4209 case DLM_MSG_CANCEL:
4210 hold_lkb(lkb);
2a7ce0ed
DT
4211 memset(ms_stub, 0, sizeof(struct dlm_message));
4212 ms_stub->m_flags = DLM_IFL_STUB_MS;
4213 ms_stub->m_type = DLM_MSG_CANCEL_REPLY;
4214 ms_stub->m_result = stub_cancel_result;
4215 ms_stub->m_header.h_nodeid = lkb->lkb_nodeid;
4216 _receive_cancel_reply(lkb, ms_stub);
b3f58d8f 4217 dlm_put_lkb(lkb);
e7fd4179
DT
4218 break;
4219
4220 default:
601342ce
DT
4221 log_error(ls, "invalid lkb wait_type %d %d",
4222 lkb->lkb_wait_type, wait_type);
e7fd4179 4223 }
81456807 4224 schedule();
e7fd4179 4225 }
90135925 4226 mutex_unlock(&ls->ls_waiters_mutex);
2a7ce0ed 4227 kfree(ms_stub);
e7fd4179
DT
4228}
4229
ef0c2bb0 4230static struct dlm_lkb *find_resend_waiter(struct dlm_ls *ls)
e7fd4179
DT
4231{
4232 struct dlm_lkb *lkb;
ef0c2bb0 4233 int found = 0;
e7fd4179 4234
90135925 4235 mutex_lock(&ls->ls_waiters_mutex);
e7fd4179
DT
4236 list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
4237 if (lkb->lkb_flags & DLM_IFL_RESEND) {
ef0c2bb0
DT
4238 hold_lkb(lkb);
4239 found = 1;
e7fd4179
DT
4240 break;
4241 }
4242 }
90135925 4243 mutex_unlock(&ls->ls_waiters_mutex);
e7fd4179 4244
ef0c2bb0 4245 if (!found)
e7fd4179 4246 lkb = NULL;
ef0c2bb0 4247 return lkb;
e7fd4179
DT
4248}
4249
4250/* Deal with lookups and lkb's marked RESEND from _pre. We may now be the
4251 master or dir-node for r. Processing the lkb may result in it being placed
4252 back on waiters. */
4253
ef0c2bb0
DT
4254/* We do this after normal locking has been enabled and any saved messages
4255 (in requestqueue) have been processed. We should be confident that at
4256 this point we won't get or process a reply to any of these waiting
4257 operations. But, new ops may be coming in on the rsbs/locks here from
4258 userspace or remotely. */
4259
4260/* there may have been an overlap unlock/cancel prior to recovery or after
4261 recovery. if before, the lkb may still have a pos wait_count; if after, the
4262 overlap flag would just have been set and nothing new sent. we can be
4263 confident here than any replies to either the initial op or overlap ops
4264 prior to recovery have been received. */
4265
e7fd4179
DT
4266int dlm_recover_waiters_post(struct dlm_ls *ls)
4267{
4268 struct dlm_lkb *lkb;
4269 struct dlm_rsb *r;
ef0c2bb0 4270 int error = 0, mstype, err, oc, ou;
e7fd4179
DT
4271
4272 while (1) {
4273 if (dlm_locking_stopped(ls)) {
4274 log_debug(ls, "recover_waiters_post aborted");
4275 error = -EINTR;
4276 break;
4277 }
4278
ef0c2bb0
DT
4279 lkb = find_resend_waiter(ls);
4280 if (!lkb)
e7fd4179
DT
4281 break;
4282
4283 r = lkb->lkb_resource;
ef0c2bb0
DT
4284 hold_rsb(r);
4285 lock_rsb(r);
4286
4287 mstype = lkb->lkb_wait_type;
4288 oc = is_overlap_cancel(lkb);
4289 ou = is_overlap_unlock(lkb);
4290 err = 0;
e7fd4179 4291
2a7ce0ed
DT
4292 log_debug(ls, "recover_waiter %x nodeid %d msg %d r_nodeid %d",
4293 lkb->lkb_id, lkb->lkb_nodeid, mstype, r->res_nodeid);
e7fd4179 4294
ef0c2bb0
DT
4295 /* At this point we assume that we won't get a reply to any
4296 previous op or overlap op on this lock. First, do a big
4297 remove_from_waiters() for all previous ops. */
4298
4299 lkb->lkb_flags &= ~DLM_IFL_RESEND;
4300 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
4301 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
4302 lkb->lkb_wait_type = 0;
4303 lkb->lkb_wait_count = 0;
4304 mutex_lock(&ls->ls_waiters_mutex);
4305 list_del_init(&lkb->lkb_wait_reply);
4306 mutex_unlock(&ls->ls_waiters_mutex);
4307 unhold_lkb(lkb); /* for waiters list */
4308
4309 if (oc || ou) {
4310 /* do an unlock or cancel instead of resending */
4311 switch (mstype) {
4312 case DLM_MSG_LOOKUP:
4313 case DLM_MSG_REQUEST:
4314 queue_cast(r, lkb, ou ? -DLM_EUNLOCK :
4315 -DLM_ECANCEL);
4316 unhold_lkb(lkb); /* undoes create_lkb() */
4317 break;
4318 case DLM_MSG_CONVERT:
4319 if (oc) {
4320 queue_cast(r, lkb, -DLM_ECANCEL);
4321 } else {
4322 lkb->lkb_exflags |= DLM_LKF_FORCEUNLOCK;
4323 _unlock_lock(r, lkb);
4324 }
4325 break;
4326 default:
4327 err = 1;
4328 }
4329 } else {
4330 switch (mstype) {
4331 case DLM_MSG_LOOKUP:
4332 case DLM_MSG_REQUEST:
4333 _request_lock(r, lkb);
4334 if (is_master(r))
4335 confirm_master(r, 0);
4336 break;
4337 case DLM_MSG_CONVERT:
4338 _convert_lock(r, lkb);
4339 break;
4340 default:
4341 err = 1;
4342 }
e7fd4179 4343 }
ef0c2bb0
DT
4344
4345 if (err)
4346 log_error(ls, "recover_waiters_post %x %d %x %d %d",
4347 lkb->lkb_id, mstype, lkb->lkb_flags, oc, ou);
4348 unlock_rsb(r);
4349 put_rsb(r);
4350 dlm_put_lkb(lkb);
e7fd4179
DT
4351 }
4352
4353 return error;
4354}
4355
4356static void purge_queue(struct dlm_rsb *r, struct list_head *queue,
4357 int (*test)(struct dlm_ls *ls, struct dlm_lkb *lkb))
4358{
4359 struct dlm_ls *ls = r->res_ls;
4360 struct dlm_lkb *lkb, *safe;
4361
4362 list_for_each_entry_safe(lkb, safe, queue, lkb_statequeue) {
4363 if (test(ls, lkb)) {
97a35d1e 4364 rsb_set_flag(r, RSB_LOCKS_PURGED);
e7fd4179
DT
4365 del_lkb(r, lkb);
4366 /* this put should free the lkb */
b3f58d8f 4367 if (!dlm_put_lkb(lkb))
e7fd4179
DT
4368 log_error(ls, "purged lkb not released");
4369 }
4370 }
4371}
4372
4373static int purge_dead_test(struct dlm_ls *ls, struct dlm_lkb *lkb)
4374{
4375 return (is_master_copy(lkb) && dlm_is_removed(ls, lkb->lkb_nodeid));
4376}
4377
4378static int purge_mstcpy_test(struct dlm_ls *ls, struct dlm_lkb *lkb)
4379{
4380 return is_master_copy(lkb);
4381}
4382
4383static void purge_dead_locks(struct dlm_rsb *r)
4384{
4385 purge_queue(r, &r->res_grantqueue, &purge_dead_test);
4386 purge_queue(r, &r->res_convertqueue, &purge_dead_test);
4387 purge_queue(r, &r->res_waitqueue, &purge_dead_test);
4388}
4389
4390void dlm_purge_mstcpy_locks(struct dlm_rsb *r)
4391{
4392 purge_queue(r, &r->res_grantqueue, &purge_mstcpy_test);
4393 purge_queue(r, &r->res_convertqueue, &purge_mstcpy_test);
4394 purge_queue(r, &r->res_waitqueue, &purge_mstcpy_test);
4395}
4396
4397/* Get rid of locks held by nodes that are gone. */
4398
4399int dlm_purge_locks(struct dlm_ls *ls)
4400{
4401 struct dlm_rsb *r;
4402
4403 log_debug(ls, "dlm_purge_locks");
4404
4405 down_write(&ls->ls_root_sem);
4406 list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
4407 hold_rsb(r);
4408 lock_rsb(r);
4409 if (is_master(r))
4410 purge_dead_locks(r);
4411 unlock_rsb(r);
4412 unhold_rsb(r);
4413
4414 schedule();
4415 }
4416 up_write(&ls->ls_root_sem);
4417
4418 return 0;
4419}
4420
97a35d1e
DT
4421static struct dlm_rsb *find_purged_rsb(struct dlm_ls *ls, int bucket)
4422{
4423 struct dlm_rsb *r, *r_ret = NULL;
4424
c7be761a 4425 spin_lock(&ls->ls_rsbtbl[bucket].lock);
97a35d1e
DT
4426 list_for_each_entry(r, &ls->ls_rsbtbl[bucket].list, res_hashchain) {
4427 if (!rsb_flag(r, RSB_LOCKS_PURGED))
4428 continue;
4429 hold_rsb(r);
4430 rsb_clear_flag(r, RSB_LOCKS_PURGED);
4431 r_ret = r;
4432 break;
4433 }
c7be761a 4434 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
97a35d1e
DT
4435 return r_ret;
4436}
4437
4438void dlm_grant_after_purge(struct dlm_ls *ls)
e7fd4179
DT
4439{
4440 struct dlm_rsb *r;
2b4e926a 4441 int bucket = 0;
e7fd4179 4442
2b4e926a
DT
4443 while (1) {
4444 r = find_purged_rsb(ls, bucket);
4445 if (!r) {
4446 if (bucket == ls->ls_rsbtbl_size - 1)
4447 break;
4448 bucket++;
97a35d1e 4449 continue;
2b4e926a 4450 }
97a35d1e
DT
4451 lock_rsb(r);
4452 if (is_master(r)) {
4453 grant_pending_locks(r);
4454 confirm_master(r, 0);
e7fd4179 4455 }
97a35d1e
DT
4456 unlock_rsb(r);
4457 put_rsb(r);
2b4e926a 4458 schedule();
e7fd4179 4459 }
e7fd4179
DT
4460}
4461
4462static struct dlm_lkb *search_remid_list(struct list_head *head, int nodeid,
4463 uint32_t remid)
4464{
4465 struct dlm_lkb *lkb;
4466
4467 list_for_each_entry(lkb, head, lkb_statequeue) {
4468 if (lkb->lkb_nodeid == nodeid && lkb->lkb_remid == remid)
4469 return lkb;
4470 }
4471 return NULL;
4472}
4473
4474static struct dlm_lkb *search_remid(struct dlm_rsb *r, int nodeid,
4475 uint32_t remid)
4476{
4477 struct dlm_lkb *lkb;
4478
4479 lkb = search_remid_list(&r->res_grantqueue, nodeid, remid);
4480 if (lkb)
4481 return lkb;
4482 lkb = search_remid_list(&r->res_convertqueue, nodeid, remid);
4483 if (lkb)
4484 return lkb;
4485 lkb = search_remid_list(&r->res_waitqueue, nodeid, remid);
4486 if (lkb)
4487 return lkb;
4488 return NULL;
4489}
4490
ae773d0b 4491/* needs at least dlm_rcom + rcom_lock */
e7fd4179
DT
4492static int receive_rcom_lock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
4493 struct dlm_rsb *r, struct dlm_rcom *rc)
4494{
4495 struct rcom_lock *rl = (struct rcom_lock *) rc->rc_buf;
e7fd4179
DT
4496
4497 lkb->lkb_nodeid = rc->rc_header.h_nodeid;
163a1859
AV
4498 lkb->lkb_ownpid = le32_to_cpu(rl->rl_ownpid);
4499 lkb->lkb_remid = le32_to_cpu(rl->rl_lkid);
4500 lkb->lkb_exflags = le32_to_cpu(rl->rl_exflags);
4501 lkb->lkb_flags = le32_to_cpu(rl->rl_flags) & 0x0000FFFF;
e7fd4179 4502 lkb->lkb_flags |= DLM_IFL_MSTCPY;
163a1859 4503 lkb->lkb_lvbseq = le32_to_cpu(rl->rl_lvbseq);
e7fd4179
DT
4504 lkb->lkb_rqmode = rl->rl_rqmode;
4505 lkb->lkb_grmode = rl->rl_grmode;
4506 /* don't set lkb_status because add_lkb wants to itself */
4507
8304d6f2
DT
4508 lkb->lkb_bastfn = (rl->rl_asts & DLM_CB_BAST) ? &fake_bastfn : NULL;
4509 lkb->lkb_astfn = (rl->rl_asts & DLM_CB_CAST) ? &fake_astfn : NULL;
e7fd4179 4510
e7fd4179 4511 if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
a5dd0631
AV
4512 int lvblen = rc->rc_header.h_length - sizeof(struct dlm_rcom) -
4513 sizeof(struct rcom_lock);
4514 if (lvblen > ls->ls_lvblen)
4515 return -EINVAL;
52bda2b5 4516 lkb->lkb_lvbptr = dlm_allocate_lvb(ls);
e7fd4179
DT
4517 if (!lkb->lkb_lvbptr)
4518 return -ENOMEM;
e7fd4179
DT
4519 memcpy(lkb->lkb_lvbptr, rl->rl_lvb, lvblen);
4520 }
4521
4522 /* Conversions between PR and CW (middle modes) need special handling.
4523 The real granted mode of these converting locks cannot be determined
4524 until all locks have been rebuilt on the rsb (recover_conversion) */
4525
163a1859
AV
4526 if (rl->rl_wait_type == cpu_to_le16(DLM_MSG_CONVERT) &&
4527 middle_conversion(lkb)) {
e7fd4179
DT
4528 rl->rl_status = DLM_LKSTS_CONVERT;
4529 lkb->lkb_grmode = DLM_LOCK_IV;
4530 rsb_set_flag(r, RSB_RECOVER_CONVERT);
4531 }
4532
4533 return 0;
4534}
4535
4536/* This lkb may have been recovered in a previous aborted recovery so we need
4537 to check if the rsb already has an lkb with the given remote nodeid/lkid.
4538 If so we just send back a standard reply. If not, we create a new lkb with
4539 the given values and send back our lkid. We send back our lkid by sending
4540 back the rcom_lock struct we got but with the remid field filled in. */
4541
ae773d0b 4542/* needs at least dlm_rcom + rcom_lock */
e7fd4179
DT
4543int dlm_recover_master_copy(struct dlm_ls *ls, struct dlm_rcom *rc)
4544{
4545 struct rcom_lock *rl = (struct rcom_lock *) rc->rc_buf;
4546 struct dlm_rsb *r;
4547 struct dlm_lkb *lkb;
4548 int error;
4549
4550 if (rl->rl_parent_lkid) {
4551 error = -EOPNOTSUPP;
4552 goto out;
4553 }
4554
163a1859
AV
4555 error = find_rsb(ls, rl->rl_name, le16_to_cpu(rl->rl_namelen),
4556 R_MASTER, &r);
e7fd4179
DT
4557 if (error)
4558 goto out;
4559
4560 lock_rsb(r);
4561
163a1859 4562 lkb = search_remid(r, rc->rc_header.h_nodeid, le32_to_cpu(rl->rl_lkid));
e7fd4179
DT
4563 if (lkb) {
4564 error = -EEXIST;
4565 goto out_remid;
4566 }
4567
4568 error = create_lkb(ls, &lkb);
4569 if (error)
4570 goto out_unlock;
4571
4572 error = receive_rcom_lock_args(ls, lkb, r, rc);
4573 if (error) {
b3f58d8f 4574 __put_lkb(ls, lkb);
e7fd4179
DT
4575 goto out_unlock;
4576 }
4577
4578 attach_lkb(r, lkb);
4579 add_lkb(r, lkb, rl->rl_status);
4580 error = 0;
4581
4582 out_remid:
4583 /* this is the new value returned to the lock holder for
4584 saving in its process-copy lkb */
163a1859 4585 rl->rl_remid = cpu_to_le32(lkb->lkb_id);
e7fd4179
DT
4586
4587 out_unlock:
4588 unlock_rsb(r);
4589 put_rsb(r);
4590 out:
4591 if (error)
163a1859
AV
4592 log_debug(ls, "recover_master_copy %d %x", error,
4593 le32_to_cpu(rl->rl_lkid));
4594 rl->rl_result = cpu_to_le32(error);
e7fd4179
DT
4595 return error;
4596}
4597
ae773d0b 4598/* needs at least dlm_rcom + rcom_lock */
e7fd4179
DT
4599int dlm_recover_process_copy(struct dlm_ls *ls, struct dlm_rcom *rc)
4600{
4601 struct rcom_lock *rl = (struct rcom_lock *) rc->rc_buf;
4602 struct dlm_rsb *r;
4603 struct dlm_lkb *lkb;
4604 int error;
4605
163a1859 4606 error = find_lkb(ls, le32_to_cpu(rl->rl_lkid), &lkb);
e7fd4179 4607 if (error) {
163a1859
AV
4608 log_error(ls, "recover_process_copy no lkid %x",
4609 le32_to_cpu(rl->rl_lkid));
e7fd4179
DT
4610 return error;
4611 }
4612
4613 DLM_ASSERT(is_process_copy(lkb), dlm_print_lkb(lkb););
4614
163a1859 4615 error = le32_to_cpu(rl->rl_result);
e7fd4179
DT
4616
4617 r = lkb->lkb_resource;
4618 hold_rsb(r);
4619 lock_rsb(r);
4620
4621 switch (error) {
dc200a88
DT
4622 case -EBADR:
4623 /* There's a chance the new master received our lock before
4624 dlm_recover_master_reply(), this wouldn't happen if we did
4625 a barrier between recover_masters and recover_locks. */
4626 log_debug(ls, "master copy not ready %x r %lx %s", lkb->lkb_id,
4627 (unsigned long)r, r->res_name);
4628 dlm_send_rcom_lock(r, lkb);
4629 goto out;
e7fd4179
DT
4630 case -EEXIST:
4631 log_debug(ls, "master copy exists %x", lkb->lkb_id);
4632 /* fall through */
4633 case 0:
163a1859 4634 lkb->lkb_remid = le32_to_cpu(rl->rl_remid);
e7fd4179
DT
4635 break;
4636 default:
4637 log_error(ls, "dlm_recover_process_copy unknown error %d %x",
4638 error, lkb->lkb_id);
4639 }
4640
4641 /* an ack for dlm_recover_locks() which waits for replies from
4642 all the locks it sends to new masters */
4643 dlm_recovered_lock(r);
dc200a88 4644 out:
e7fd4179
DT
4645 unlock_rsb(r);
4646 put_rsb(r);
b3f58d8f 4647 dlm_put_lkb(lkb);
e7fd4179
DT
4648
4649 return 0;
4650}
4651
597d0cae
DT
4652int dlm_user_request(struct dlm_ls *ls, struct dlm_user_args *ua,
4653 int mode, uint32_t flags, void *name, unsigned int namelen,
d7db923e 4654 unsigned long timeout_cs)
597d0cae
DT
4655{
4656 struct dlm_lkb *lkb;
4657 struct dlm_args args;
4658 int error;
4659
85e86edf 4660 dlm_lock_recovery(ls);
597d0cae
DT
4661
4662 error = create_lkb(ls, &lkb);
4663 if (error) {
4664 kfree(ua);
4665 goto out;
4666 }
4667
4668 if (flags & DLM_LKF_VALBLK) {
573c24c4 4669 ua->lksb.sb_lvbptr = kzalloc(DLM_USER_LVB_LEN, GFP_NOFS);
597d0cae
DT
4670 if (!ua->lksb.sb_lvbptr) {
4671 kfree(ua);
4672 __put_lkb(ls, lkb);
4673 error = -ENOMEM;
4674 goto out;
4675 }
4676 }
4677
52bda2b5 4678 /* After ua is attached to lkb it will be freed by dlm_free_lkb().
597d0cae
DT
4679 When DLM_IFL_USER is set, the dlm knows that this is a userspace
4680 lock and that lkb_astparam is the dlm_user_args structure. */
4681
d7db923e 4682 error = set_lock_args(mode, &ua->lksb, flags, namelen, timeout_cs,
e5dae548 4683 fake_astfn, ua, fake_bastfn, &args);
597d0cae 4684 lkb->lkb_flags |= DLM_IFL_USER;
597d0cae
DT
4685
4686 if (error) {
4687 __put_lkb(ls, lkb);
4688 goto out;
4689 }
4690
4691 error = request_lock(ls, lkb, name, namelen, &args);
4692
4693 switch (error) {
4694 case 0:
4695 break;
4696 case -EINPROGRESS:
4697 error = 0;
4698 break;
4699 case -EAGAIN:
4700 error = 0;
4701 /* fall through */
4702 default:
4703 __put_lkb(ls, lkb);
4704 goto out;
4705 }
4706
4707 /* add this new lkb to the per-process list of locks */
4708 spin_lock(&ua->proc->locks_spin);
ef0c2bb0 4709 hold_lkb(lkb);
597d0cae
DT
4710 list_add_tail(&lkb->lkb_ownqueue, &ua->proc->locks);
4711 spin_unlock(&ua->proc->locks_spin);
4712 out:
85e86edf 4713 dlm_unlock_recovery(ls);
597d0cae
DT
4714 return error;
4715}
4716
4717int dlm_user_convert(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
d7db923e
DT
4718 int mode, uint32_t flags, uint32_t lkid, char *lvb_in,
4719 unsigned long timeout_cs)
597d0cae
DT
4720{
4721 struct dlm_lkb *lkb;
4722 struct dlm_args args;
4723 struct dlm_user_args *ua;
4724 int error;
4725
85e86edf 4726 dlm_lock_recovery(ls);
597d0cae
DT
4727
4728 error = find_lkb(ls, lkid, &lkb);
4729 if (error)
4730 goto out;
4731
4732 /* user can change the params on its lock when it converts it, or
4733 add an lvb that didn't exist before */
4734
d292c0cc 4735 ua = lkb->lkb_ua;
597d0cae
DT
4736
4737 if (flags & DLM_LKF_VALBLK && !ua->lksb.sb_lvbptr) {
573c24c4 4738 ua->lksb.sb_lvbptr = kzalloc(DLM_USER_LVB_LEN, GFP_NOFS);
597d0cae
DT
4739 if (!ua->lksb.sb_lvbptr) {
4740 error = -ENOMEM;
4741 goto out_put;
4742 }
4743 }
4744 if (lvb_in && ua->lksb.sb_lvbptr)
4745 memcpy(ua->lksb.sb_lvbptr, lvb_in, DLM_USER_LVB_LEN);
4746
d7db923e 4747 ua->xid = ua_tmp->xid;
597d0cae
DT
4748 ua->castparam = ua_tmp->castparam;
4749 ua->castaddr = ua_tmp->castaddr;
4750 ua->bastparam = ua_tmp->bastparam;
4751 ua->bastaddr = ua_tmp->bastaddr;
10948eb4 4752 ua->user_lksb = ua_tmp->user_lksb;
597d0cae 4753
d7db923e 4754 error = set_lock_args(mode, &ua->lksb, flags, 0, timeout_cs,
e5dae548 4755 fake_astfn, ua, fake_bastfn, &args);
597d0cae
DT
4756 if (error)
4757 goto out_put;
4758
4759 error = convert_lock(ls, lkb, &args);
4760
c85d65e9 4761 if (error == -EINPROGRESS || error == -EAGAIN || error == -EDEADLK)
597d0cae
DT
4762 error = 0;
4763 out_put:
4764 dlm_put_lkb(lkb);
4765 out:
85e86edf 4766 dlm_unlock_recovery(ls);
597d0cae
DT
4767 kfree(ua_tmp);
4768 return error;
4769}
4770
4771int dlm_user_unlock(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
4772 uint32_t flags, uint32_t lkid, char *lvb_in)
4773{
4774 struct dlm_lkb *lkb;
4775 struct dlm_args args;
4776 struct dlm_user_args *ua;
4777 int error;
4778
85e86edf 4779 dlm_lock_recovery(ls);
597d0cae
DT
4780
4781 error = find_lkb(ls, lkid, &lkb);
4782 if (error)
4783 goto out;
4784
d292c0cc 4785 ua = lkb->lkb_ua;
597d0cae
DT
4786
4787 if (lvb_in && ua->lksb.sb_lvbptr)
4788 memcpy(ua->lksb.sb_lvbptr, lvb_in, DLM_USER_LVB_LEN);
b434eda6
PC
4789 if (ua_tmp->castparam)
4790 ua->castparam = ua_tmp->castparam;
cc346d55 4791 ua->user_lksb = ua_tmp->user_lksb;
597d0cae
DT
4792
4793 error = set_unlock_args(flags, ua, &args);
4794 if (error)
4795 goto out_put;
4796
4797 error = unlock_lock(ls, lkb, &args);
4798
4799 if (error == -DLM_EUNLOCK)
4800 error = 0;
ef0c2bb0
DT
4801 /* from validate_unlock_args() */
4802 if (error == -EBUSY && (flags & DLM_LKF_FORCEUNLOCK))
4803 error = 0;
597d0cae
DT
4804 if (error)
4805 goto out_put;
4806
4807 spin_lock(&ua->proc->locks_spin);
a1bc86e6
DT
4808 /* dlm_user_add_ast() may have already taken lkb off the proc list */
4809 if (!list_empty(&lkb->lkb_ownqueue))
4810 list_move(&lkb->lkb_ownqueue, &ua->proc->unlocking);
597d0cae 4811 spin_unlock(&ua->proc->locks_spin);
597d0cae
DT
4812 out_put:
4813 dlm_put_lkb(lkb);
4814 out:
85e86edf 4815 dlm_unlock_recovery(ls);
ef0c2bb0 4816 kfree(ua_tmp);
597d0cae
DT
4817 return error;
4818}
4819
4820int dlm_user_cancel(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
4821 uint32_t flags, uint32_t lkid)
4822{
4823 struct dlm_lkb *lkb;
4824 struct dlm_args args;
4825 struct dlm_user_args *ua;
4826 int error;
4827
85e86edf 4828 dlm_lock_recovery(ls);
597d0cae
DT
4829
4830 error = find_lkb(ls, lkid, &lkb);
4831 if (error)
4832 goto out;
4833
d292c0cc 4834 ua = lkb->lkb_ua;
b434eda6
PC
4835 if (ua_tmp->castparam)
4836 ua->castparam = ua_tmp->castparam;
c059f70e 4837 ua->user_lksb = ua_tmp->user_lksb;
597d0cae
DT
4838
4839 error = set_unlock_args(flags, ua, &args);
4840 if (error)
4841 goto out_put;
4842
4843 error = cancel_lock(ls, lkb, &args);
4844
4845 if (error == -DLM_ECANCEL)
4846 error = 0;
ef0c2bb0
DT
4847 /* from validate_unlock_args() */
4848 if (error == -EBUSY)
4849 error = 0;
597d0cae
DT
4850 out_put:
4851 dlm_put_lkb(lkb);
4852 out:
85e86edf 4853 dlm_unlock_recovery(ls);
ef0c2bb0 4854 kfree(ua_tmp);
597d0cae
DT
4855 return error;
4856}
4857
8b4021fa
DT
4858int dlm_user_deadlock(struct dlm_ls *ls, uint32_t flags, uint32_t lkid)
4859{
4860 struct dlm_lkb *lkb;
4861 struct dlm_args args;
4862 struct dlm_user_args *ua;
4863 struct dlm_rsb *r;
4864 int error;
4865
4866 dlm_lock_recovery(ls);
4867
4868 error = find_lkb(ls, lkid, &lkb);
4869 if (error)
4870 goto out;
4871
d292c0cc 4872 ua = lkb->lkb_ua;
8b4021fa
DT
4873
4874 error = set_unlock_args(flags, ua, &args);
4875 if (error)
4876 goto out_put;
4877
4878 /* same as cancel_lock(), but set DEADLOCK_CANCEL after lock_rsb */
4879
4880 r = lkb->lkb_resource;
4881 hold_rsb(r);
4882 lock_rsb(r);
4883
4884 error = validate_unlock_args(lkb, &args);
4885 if (error)
4886 goto out_r;
4887 lkb->lkb_flags |= DLM_IFL_DEADLOCK_CANCEL;
4888
4889 error = _cancel_lock(r, lkb);
4890 out_r:
4891 unlock_rsb(r);
4892 put_rsb(r);
4893
4894 if (error == -DLM_ECANCEL)
4895 error = 0;
4896 /* from validate_unlock_args() */
4897 if (error == -EBUSY)
4898 error = 0;
4899 out_put:
4900 dlm_put_lkb(lkb);
4901 out:
4902 dlm_unlock_recovery(ls);
4903 return error;
4904}
4905
ef0c2bb0
DT
4906/* lkb's that are removed from the waiters list by revert are just left on the
4907 orphans list with the granted orphan locks, to be freed by purge */
4908
597d0cae
DT
4909static int orphan_proc_lock(struct dlm_ls *ls, struct dlm_lkb *lkb)
4910{
ef0c2bb0
DT
4911 struct dlm_args args;
4912 int error;
597d0cae 4913
ef0c2bb0
DT
4914 hold_lkb(lkb);
4915 mutex_lock(&ls->ls_orphans_mutex);
4916 list_add_tail(&lkb->lkb_ownqueue, &ls->ls_orphans);
4917 mutex_unlock(&ls->ls_orphans_mutex);
597d0cae 4918
d292c0cc 4919 set_unlock_args(0, lkb->lkb_ua, &args);
ef0c2bb0
DT
4920
4921 error = cancel_lock(ls, lkb, &args);
4922 if (error == -DLM_ECANCEL)
4923 error = 0;
4924 return error;
597d0cae
DT
4925}
4926
4927/* The force flag allows the unlock to go ahead even if the lkb isn't granted.
4928 Regardless of what rsb queue the lock is on, it's removed and freed. */
4929
4930static int unlock_proc_lock(struct dlm_ls *ls, struct dlm_lkb *lkb)
4931{
597d0cae
DT
4932 struct dlm_args args;
4933 int error;
4934
d292c0cc 4935 set_unlock_args(DLM_LKF_FORCEUNLOCK, lkb->lkb_ua, &args);
597d0cae
DT
4936
4937 error = unlock_lock(ls, lkb, &args);
4938 if (error == -DLM_EUNLOCK)
4939 error = 0;
4940 return error;
4941}
4942
ef0c2bb0
DT
4943/* We have to release clear_proc_locks mutex before calling unlock_proc_lock()
4944 (which does lock_rsb) due to deadlock with receiving a message that does
4945 lock_rsb followed by dlm_user_add_ast() */
4946
4947static struct dlm_lkb *del_proc_lock(struct dlm_ls *ls,
4948 struct dlm_user_proc *proc)
4949{
4950 struct dlm_lkb *lkb = NULL;
4951
4952 mutex_lock(&ls->ls_clear_proc_locks);
4953 if (list_empty(&proc->locks))
4954 goto out;
4955
4956 lkb = list_entry(proc->locks.next, struct dlm_lkb, lkb_ownqueue);
4957 list_del_init(&lkb->lkb_ownqueue);
4958
4959 if (lkb->lkb_exflags & DLM_LKF_PERSISTENT)
4960 lkb->lkb_flags |= DLM_IFL_ORPHAN;
4961 else
4962 lkb->lkb_flags |= DLM_IFL_DEAD;
4963 out:
4964 mutex_unlock(&ls->ls_clear_proc_locks);
4965 return lkb;
4966}
4967
597d0cae
DT
4968/* The ls_clear_proc_locks mutex protects against dlm_user_add_asts() which
4969 1) references lkb->ua which we free here and 2) adds lkbs to proc->asts,
4970 which we clear here. */
4971
4972/* proc CLOSING flag is set so no more device_reads should look at proc->asts
4973 list, and no more device_writes should add lkb's to proc->locks list; so we
4974 shouldn't need to take asts_spin or locks_spin here. this assumes that
4975 device reads/writes/closes are serialized -- FIXME: we may need to serialize
4976 them ourself. */
4977
4978void dlm_clear_proc_locks(struct dlm_ls *ls, struct dlm_user_proc *proc)
4979{
4980 struct dlm_lkb *lkb, *safe;
4981
85e86edf 4982 dlm_lock_recovery(ls);
597d0cae 4983
ef0c2bb0
DT
4984 while (1) {
4985 lkb = del_proc_lock(ls, proc);
4986 if (!lkb)
4987 break;
84d8cd69 4988 del_timeout(lkb);
ef0c2bb0 4989 if (lkb->lkb_exflags & DLM_LKF_PERSISTENT)
597d0cae 4990 orphan_proc_lock(ls, lkb);
ef0c2bb0 4991 else
597d0cae 4992 unlock_proc_lock(ls, lkb);
597d0cae
DT
4993
4994 /* this removes the reference for the proc->locks list
4995 added by dlm_user_request, it may result in the lkb
4996 being freed */
4997
4998 dlm_put_lkb(lkb);
4999 }
a1bc86e6 5000
ef0c2bb0
DT
5001 mutex_lock(&ls->ls_clear_proc_locks);
5002
a1bc86e6
DT
5003 /* in-progress unlocks */
5004 list_for_each_entry_safe(lkb, safe, &proc->unlocking, lkb_ownqueue) {
5005 list_del_init(&lkb->lkb_ownqueue);
5006 lkb->lkb_flags |= DLM_IFL_DEAD;
5007 dlm_put_lkb(lkb);
5008 }
5009
5010 list_for_each_entry_safe(lkb, safe, &proc->asts, lkb_astqueue) {
8304d6f2
DT
5011 memset(&lkb->lkb_callbacks, 0,
5012 sizeof(struct dlm_callback) * DLM_CALLBACKS_SIZE);
5013 list_del_init(&lkb->lkb_astqueue);
a1bc86e6
DT
5014 dlm_put_lkb(lkb);
5015 }
5016
597d0cae 5017 mutex_unlock(&ls->ls_clear_proc_locks);
85e86edf 5018 dlm_unlock_recovery(ls);
597d0cae 5019}
a1bc86e6 5020
8499137d
DT
5021static void purge_proc_locks(struct dlm_ls *ls, struct dlm_user_proc *proc)
5022{
5023 struct dlm_lkb *lkb, *safe;
5024
5025 while (1) {
5026 lkb = NULL;
5027 spin_lock(&proc->locks_spin);
5028 if (!list_empty(&proc->locks)) {
5029 lkb = list_entry(proc->locks.next, struct dlm_lkb,
5030 lkb_ownqueue);
5031 list_del_init(&lkb->lkb_ownqueue);
5032 }
5033 spin_unlock(&proc->locks_spin);
5034
5035 if (!lkb)
5036 break;
5037
5038 lkb->lkb_flags |= DLM_IFL_DEAD;
5039 unlock_proc_lock(ls, lkb);
5040 dlm_put_lkb(lkb); /* ref from proc->locks list */
5041 }
5042
5043 spin_lock(&proc->locks_spin);
5044 list_for_each_entry_safe(lkb, safe, &proc->unlocking, lkb_ownqueue) {
5045 list_del_init(&lkb->lkb_ownqueue);
5046 lkb->lkb_flags |= DLM_IFL_DEAD;
5047 dlm_put_lkb(lkb);
5048 }
5049 spin_unlock(&proc->locks_spin);
5050
5051 spin_lock(&proc->asts_spin);
5052 list_for_each_entry_safe(lkb, safe, &proc->asts, lkb_astqueue) {
8304d6f2
DT
5053 memset(&lkb->lkb_callbacks, 0,
5054 sizeof(struct dlm_callback) * DLM_CALLBACKS_SIZE);
5055 list_del_init(&lkb->lkb_astqueue);
8499137d
DT
5056 dlm_put_lkb(lkb);
5057 }
5058 spin_unlock(&proc->asts_spin);
5059}
5060
5061/* pid of 0 means purge all orphans */
5062
5063static void do_purge(struct dlm_ls *ls, int nodeid, int pid)
5064{
5065 struct dlm_lkb *lkb, *safe;
5066
5067 mutex_lock(&ls->ls_orphans_mutex);
5068 list_for_each_entry_safe(lkb, safe, &ls->ls_orphans, lkb_ownqueue) {
5069 if (pid && lkb->lkb_ownpid != pid)
5070 continue;
5071 unlock_proc_lock(ls, lkb);
5072 list_del_init(&lkb->lkb_ownqueue);
5073 dlm_put_lkb(lkb);
5074 }
5075 mutex_unlock(&ls->ls_orphans_mutex);
5076}
5077
5078static int send_purge(struct dlm_ls *ls, int nodeid, int pid)
5079{
5080 struct dlm_message *ms;
5081 struct dlm_mhandle *mh;
5082 int error;
5083
5084 error = _create_message(ls, sizeof(struct dlm_message), nodeid,
5085 DLM_MSG_PURGE, &ms, &mh);
5086 if (error)
5087 return error;
5088 ms->m_nodeid = nodeid;
5089 ms->m_pid = pid;
5090
5091 return send_message(mh, ms);
5092}
5093
5094int dlm_user_purge(struct dlm_ls *ls, struct dlm_user_proc *proc,
5095 int nodeid, int pid)
5096{
5097 int error = 0;
5098
5099 if (nodeid != dlm_our_nodeid()) {
5100 error = send_purge(ls, nodeid, pid);
5101 } else {
85e86edf 5102 dlm_lock_recovery(ls);
8499137d
DT
5103 if (pid == current->pid)
5104 purge_proc_locks(ls, proc);
5105 else
5106 do_purge(ls, nodeid, pid);
85e86edf 5107 dlm_unlock_recovery(ls);
8499137d
DT
5108 }
5109 return error;
5110}
5111
This page took 0.60755 seconds and 5 git commands to generate.