blob: f3ba70301a4521b52761f8f0751ace52ff353857 [file] [log] [blame]
David Teiglande7fd4172006-01-18 09:30:29 +00001/******************************************************************************
2*******************************************************************************
3**
David Teigland7fe2b312010-02-24 11:08:18 -06004** Copyright (C) 2005-2010 Red Hat, Inc. All rights reserved.
David Teiglande7fd4172006-01-18 09:30:29 +00005**
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*/
David Teigland597d0ca2006-07-12 16:44:04 -050058#include <linux/types.h>
Bob Peterson9beb3bf2011-10-26 15:24:55 -050059#include <linux/rbtree.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090060#include <linux/slab.h>
David Teiglande7fd4172006-01-18 09:30:29 +000061#include "dlm_internal.h"
David Teigland597d0ca2006-07-12 16:44:04 -050062#include <linux/dlm_device.h>
David Teiglande7fd4172006-01-18 09:30:29 +000063#include "memory.h"
64#include "lowcomms.h"
65#include "requestqueue.h"
66#include "util.h"
67#include "dir.h"
68#include "member.h"
69#include "lockspace.h"
70#include "ast.h"
71#include "lock.h"
72#include "rcom.h"
73#include "recover.h"
74#include "lvb_table.h"
David Teigland597d0ca2006-07-12 16:44:04 -050075#include "user.h"
David Teiglande7fd4172006-01-18 09:30:29 +000076#include "config.h"
77
78static int send_request(struct dlm_rsb *r, struct dlm_lkb *lkb);
79static int send_convert(struct dlm_rsb *r, struct dlm_lkb *lkb);
80static int send_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb);
81static int send_cancel(struct dlm_rsb *r, struct dlm_lkb *lkb);
82static int send_grant(struct dlm_rsb *r, struct dlm_lkb *lkb);
83static int send_bast(struct dlm_rsb *r, struct dlm_lkb *lkb, int mode);
84static int send_lookup(struct dlm_rsb *r, struct dlm_lkb *lkb);
85static int send_remove(struct dlm_rsb *r);
86static int _request_lock(struct dlm_rsb *r, struct dlm_lkb *lkb);
David Teigland3ae1acf2007-05-18 08:59:31 -050087static int _cancel_lock(struct dlm_rsb *r, struct dlm_lkb *lkb);
David Teiglande7fd4172006-01-18 09:30:29 +000088static void __receive_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
89 struct dlm_message *ms);
90static int receive_extralen(struct dlm_message *ms);
David Teigland84991372007-03-30 15:02:40 -050091static void do_purge(struct dlm_ls *ls, int nodeid, int pid);
David Teigland3ae1acf2007-05-18 08:59:31 -050092static void del_timeout(struct dlm_lkb *lkb);
David Teiglande7fd4172006-01-18 09:30:29 +000093
94/*
95 * Lock compatibilty matrix - thanks Steve
96 * UN = Unlocked state. Not really a state, used as a flag
97 * PD = Padding. Used to make the matrix a nice power of two in size
98 * Other states are the same as the VMS DLM.
99 * Usage: matrix[grmode+1][rqmode+1] (although m[rq+1][gr+1] is the same)
100 */
101
102static const int __dlm_compat_matrix[8][8] = {
103 /* UN NL CR CW PR PW EX PD */
104 {1, 1, 1, 1, 1, 1, 1, 0}, /* UN */
105 {1, 1, 1, 1, 1, 1, 1, 0}, /* NL */
106 {1, 1, 1, 1, 1, 1, 0, 0}, /* CR */
107 {1, 1, 1, 1, 0, 0, 0, 0}, /* CW */
108 {1, 1, 1, 0, 1, 0, 0, 0}, /* PR */
109 {1, 1, 1, 0, 0, 0, 0, 0}, /* PW */
110 {1, 1, 0, 0, 0, 0, 0, 0}, /* EX */
111 {0, 0, 0, 0, 0, 0, 0, 0} /* PD */
112};
113
114/*
115 * This defines the direction of transfer of LVB data.
116 * Granted mode is the row; requested mode is the column.
117 * Usage: matrix[grmode+1][rqmode+1]
118 * 1 = LVB is returned to the caller
119 * 0 = LVB is written to the resource
120 * -1 = nothing happens to the LVB
121 */
122
123const int dlm_lvb_operations[8][8] = {
124 /* UN NL CR CW PR PW EX PD*/
125 { -1, 1, 1, 1, 1, 1, 1, -1 }, /* UN */
126 { -1, 1, 1, 1, 1, 1, 1, 0 }, /* NL */
127 { -1, -1, 1, 1, 1, 1, 1, 0 }, /* CR */
128 { -1, -1, -1, 1, 1, 1, 1, 0 }, /* CW */
129 { -1, -1, -1, -1, 1, 1, 1, 0 }, /* PR */
130 { -1, 0, 0, 0, 0, 0, 1, 0 }, /* PW */
131 { -1, 0, 0, 0, 0, 0, 0, 0 }, /* EX */
132 { -1, 0, 0, 0, 0, 0, 0, 0 } /* PD */
133};
David Teiglande7fd4172006-01-18 09:30:29 +0000134
135#define modes_compat(gr, rq) \
136 __dlm_compat_matrix[(gr)->lkb_grmode + 1][(rq)->lkb_rqmode + 1]
137
138int dlm_modes_compat(int mode1, int mode2)
139{
140 return __dlm_compat_matrix[mode1 + 1][mode2 + 1];
141}
142
143/*
144 * Compatibility matrix for conversions with QUECVT set.
145 * Granted mode is the row; requested mode is the column.
146 * Usage: matrix[grmode+1][rqmode+1]
147 */
148
149static const int __quecvt_compat_matrix[8][8] = {
150 /* UN NL CR CW PR PW EX PD */
151 {0, 0, 0, 0, 0, 0, 0, 0}, /* UN */
152 {0, 0, 1, 1, 1, 1, 1, 0}, /* NL */
153 {0, 0, 0, 1, 1, 1, 1, 0}, /* CR */
154 {0, 0, 0, 0, 1, 1, 1, 0}, /* CW */
155 {0, 0, 0, 1, 0, 1, 1, 0}, /* PR */
156 {0, 0, 0, 0, 0, 0, 1, 0}, /* PW */
157 {0, 0, 0, 0, 0, 0, 0, 0}, /* EX */
158 {0, 0, 0, 0, 0, 0, 0, 0} /* PD */
159};
160
David Teigland597d0ca2006-07-12 16:44:04 -0500161void dlm_print_lkb(struct dlm_lkb *lkb)
David Teiglande7fd4172006-01-18 09:30:29 +0000162{
David Teigland6d40c4a2012-04-23 16:36:01 -0500163 printk(KERN_ERR "lkb: nodeid %d id %x remid %x exflags %x flags %x "
164 "sts %d rq %d gr %d wait_type %d wait_nodeid %d\n",
David Teiglande7fd4172006-01-18 09:30:29 +0000165 lkb->lkb_nodeid, lkb->lkb_id, lkb->lkb_remid, lkb->lkb_exflags,
166 lkb->lkb_flags, lkb->lkb_status, lkb->lkb_rqmode,
David Teigland6d40c4a2012-04-23 16:36:01 -0500167 lkb->lkb_grmode, lkb->lkb_wait_type, lkb->lkb_wait_nodeid);
David Teiglande7fd4172006-01-18 09:30:29 +0000168}
169
Adrian Bunk170e19a2008-02-13 23:29:38 +0200170static void dlm_print_rsb(struct dlm_rsb *r)
David Teiglande7fd4172006-01-18 09:30:29 +0000171{
172 printk(KERN_ERR "rsb: nodeid %d flags %lx first %x rlc %d name %s\n",
173 r->res_nodeid, r->res_flags, r->res_first_lkid,
174 r->res_recover_locks_count, r->res_name);
175}
176
David Teiglanda345da32006-08-18 11:54:25 -0500177void dlm_dump_rsb(struct dlm_rsb *r)
178{
179 struct dlm_lkb *lkb;
180
181 dlm_print_rsb(r);
182
183 printk(KERN_ERR "rsb: root_list empty %d recover_list empty %d\n",
184 list_empty(&r->res_root_list), list_empty(&r->res_recover_list));
185 printk(KERN_ERR "rsb lookup list\n");
186 list_for_each_entry(lkb, &r->res_lookup, lkb_rsb_lookup)
187 dlm_print_lkb(lkb);
188 printk(KERN_ERR "rsb grant queue:\n");
189 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue)
190 dlm_print_lkb(lkb);
191 printk(KERN_ERR "rsb convert queue:\n");
192 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue)
193 dlm_print_lkb(lkb);
194 printk(KERN_ERR "rsb wait queue:\n");
195 list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue)
196 dlm_print_lkb(lkb);
197}
198
David Teiglande7fd4172006-01-18 09:30:29 +0000199/* Threads cannot use the lockspace while it's being recovered */
200
David Teigland85e86ed2007-05-18 08:58:15 -0500201static inline void dlm_lock_recovery(struct dlm_ls *ls)
David Teiglande7fd4172006-01-18 09:30:29 +0000202{
203 down_read(&ls->ls_in_recovery);
204}
205
David Teigland85e86ed2007-05-18 08:58:15 -0500206void dlm_unlock_recovery(struct dlm_ls *ls)
David Teiglande7fd4172006-01-18 09:30:29 +0000207{
208 up_read(&ls->ls_in_recovery);
209}
210
David Teigland85e86ed2007-05-18 08:58:15 -0500211int dlm_lock_recovery_try(struct dlm_ls *ls)
David Teiglande7fd4172006-01-18 09:30:29 +0000212{
213 return down_read_trylock(&ls->ls_in_recovery);
214}
215
216static inline int can_be_queued(struct dlm_lkb *lkb)
217{
218 return !(lkb->lkb_exflags & DLM_LKF_NOQUEUE);
219}
220
221static inline int force_blocking_asts(struct dlm_lkb *lkb)
222{
223 return (lkb->lkb_exflags & DLM_LKF_NOQUEUEBAST);
224}
225
226static inline int is_demoted(struct dlm_lkb *lkb)
227{
228 return (lkb->lkb_sbflags & DLM_SBF_DEMOTED);
229}
230
David Teigland7d3c1fe2007-04-19 10:30:41 -0500231static inline int is_altmode(struct dlm_lkb *lkb)
232{
233 return (lkb->lkb_sbflags & DLM_SBF_ALTMODE);
234}
235
236static inline int is_granted(struct dlm_lkb *lkb)
237{
238 return (lkb->lkb_status == DLM_LKSTS_GRANTED);
239}
240
David Teiglande7fd4172006-01-18 09:30:29 +0000241static inline int is_remote(struct dlm_rsb *r)
242{
243 DLM_ASSERT(r->res_nodeid >= 0, dlm_print_rsb(r););
244 return !!r->res_nodeid;
245}
246
247static inline int is_process_copy(struct dlm_lkb *lkb)
248{
249 return (lkb->lkb_nodeid && !(lkb->lkb_flags & DLM_IFL_MSTCPY));
250}
251
252static inline int is_master_copy(struct dlm_lkb *lkb)
253{
254 if (lkb->lkb_flags & DLM_IFL_MSTCPY)
255 DLM_ASSERT(lkb->lkb_nodeid, dlm_print_lkb(lkb););
David Teigland90135922006-01-20 08:47:07 +0000256 return (lkb->lkb_flags & DLM_IFL_MSTCPY) ? 1 : 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000257}
258
259static inline int middle_conversion(struct dlm_lkb *lkb)
260{
261 if ((lkb->lkb_grmode==DLM_LOCK_PR && lkb->lkb_rqmode==DLM_LOCK_CW) ||
262 (lkb->lkb_rqmode==DLM_LOCK_PR && lkb->lkb_grmode==DLM_LOCK_CW))
David Teigland90135922006-01-20 08:47:07 +0000263 return 1;
264 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000265}
266
267static inline int down_conversion(struct dlm_lkb *lkb)
268{
269 return (!middle_conversion(lkb) && lkb->lkb_rqmode < lkb->lkb_grmode);
270}
271
David Teiglandef0c2bb2007-03-28 09:56:46 -0500272static inline int is_overlap_unlock(struct dlm_lkb *lkb)
273{
274 return lkb->lkb_flags & DLM_IFL_OVERLAP_UNLOCK;
275}
276
277static inline int is_overlap_cancel(struct dlm_lkb *lkb)
278{
279 return lkb->lkb_flags & DLM_IFL_OVERLAP_CANCEL;
280}
281
282static inline int is_overlap(struct dlm_lkb *lkb)
283{
284 return (lkb->lkb_flags & (DLM_IFL_OVERLAP_UNLOCK |
285 DLM_IFL_OVERLAP_CANCEL));
286}
287
David Teiglande7fd4172006-01-18 09:30:29 +0000288static void queue_cast(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
289{
290 if (is_master_copy(lkb))
291 return;
292
David Teigland3ae1acf2007-05-18 08:59:31 -0500293 del_timeout(lkb);
294
David Teiglande7fd4172006-01-18 09:30:29 +0000295 DLM_ASSERT(lkb->lkb_lksb, dlm_print_lkb(lkb););
296
David Teigland3ae1acf2007-05-18 08:59:31 -0500297 /* if the operation was a cancel, then return -DLM_ECANCEL, if a
298 timeout caused the cancel then return -ETIMEDOUT */
299 if (rv == -DLM_ECANCEL && (lkb->lkb_flags & DLM_IFL_TIMEOUT_CANCEL)) {
300 lkb->lkb_flags &= ~DLM_IFL_TIMEOUT_CANCEL;
301 rv = -ETIMEDOUT;
302 }
303
David Teigland8b4021f2007-05-29 08:46:00 -0500304 if (rv == -DLM_ECANCEL && (lkb->lkb_flags & DLM_IFL_DEADLOCK_CANCEL)) {
305 lkb->lkb_flags &= ~DLM_IFL_DEADLOCK_CANCEL;
306 rv = -EDEADLK;
307 }
308
David Teigland23e8e1a2011-04-05 13:16:24 -0500309 dlm_add_cb(lkb, DLM_CB_CAST, lkb->lkb_grmode, rv, lkb->lkb_sbflags);
David Teiglande7fd4172006-01-18 09:30:29 +0000310}
311
David Teiglandef0c2bb2007-03-28 09:56:46 -0500312static inline void queue_cast_overlap(struct dlm_rsb *r, struct dlm_lkb *lkb)
313{
314 queue_cast(r, lkb,
315 is_overlap_unlock(lkb) ? -DLM_EUNLOCK : -DLM_ECANCEL);
316}
317
David Teiglande7fd4172006-01-18 09:30:29 +0000318static void queue_bast(struct dlm_rsb *r, struct dlm_lkb *lkb, int rqmode)
319{
David Teiglandb6fa8792010-02-25 12:20:57 -0600320 if (is_master_copy(lkb)) {
David Teiglande7fd4172006-01-18 09:30:29 +0000321 send_bast(r, lkb, rqmode);
David Teiglandb6fa8792010-02-25 12:20:57 -0600322 } else {
David Teigland23e8e1a2011-04-05 13:16:24 -0500323 dlm_add_cb(lkb, DLM_CB_BAST, rqmode, 0, 0);
David Teiglandb6fa8792010-02-25 12:20:57 -0600324 }
David Teiglande7fd4172006-01-18 09:30:29 +0000325}
326
327/*
328 * Basic operations on rsb's and lkb's
329 */
330
David Teigland3881ac02011-07-07 14:05:03 -0500331static int pre_rsb_struct(struct dlm_ls *ls)
332{
333 struct dlm_rsb *r1, *r2;
334 int count = 0;
335
336 spin_lock(&ls->ls_new_rsb_spin);
337 if (ls->ls_new_rsb_count > dlm_config.ci_new_rsb_count / 2) {
338 spin_unlock(&ls->ls_new_rsb_spin);
339 return 0;
340 }
341 spin_unlock(&ls->ls_new_rsb_spin);
342
343 r1 = dlm_allocate_rsb(ls);
344 r2 = dlm_allocate_rsb(ls);
345
346 spin_lock(&ls->ls_new_rsb_spin);
347 if (r1) {
348 list_add(&r1->res_hashchain, &ls->ls_new_rsb);
349 ls->ls_new_rsb_count++;
350 }
351 if (r2) {
352 list_add(&r2->res_hashchain, &ls->ls_new_rsb);
353 ls->ls_new_rsb_count++;
354 }
355 count = ls->ls_new_rsb_count;
356 spin_unlock(&ls->ls_new_rsb_spin);
357
358 if (!count)
359 return -ENOMEM;
360 return 0;
361}
362
363/* If ls->ls_new_rsb is empty, return -EAGAIN, so the caller can
364 unlock any spinlocks, go back and call pre_rsb_struct again.
365 Otherwise, take an rsb off the list and return it. */
366
367static int get_rsb_struct(struct dlm_ls *ls, char *name, int len,
368 struct dlm_rsb **r_ret)
David Teiglande7fd4172006-01-18 09:30:29 +0000369{
370 struct dlm_rsb *r;
David Teigland3881ac02011-07-07 14:05:03 -0500371 int count;
David Teiglande7fd4172006-01-18 09:30:29 +0000372
David Teigland3881ac02011-07-07 14:05:03 -0500373 spin_lock(&ls->ls_new_rsb_spin);
374 if (list_empty(&ls->ls_new_rsb)) {
375 count = ls->ls_new_rsb_count;
376 spin_unlock(&ls->ls_new_rsb_spin);
377 log_debug(ls, "find_rsb retry %d %d %s",
378 count, dlm_config.ci_new_rsb_count, name);
379 return -EAGAIN;
380 }
381
382 r = list_first_entry(&ls->ls_new_rsb, struct dlm_rsb, res_hashchain);
383 list_del(&r->res_hashchain);
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500384 /* Convert the empty list_head to a NULL rb_node for tree usage: */
385 memset(&r->res_hashnode, 0, sizeof(struct rb_node));
David Teigland3881ac02011-07-07 14:05:03 -0500386 ls->ls_new_rsb_count--;
387 spin_unlock(&ls->ls_new_rsb_spin);
David Teiglande7fd4172006-01-18 09:30:29 +0000388
389 r->res_ls = ls;
390 r->res_length = len;
391 memcpy(r->res_name, name, len);
David Teigland90135922006-01-20 08:47:07 +0000392 mutex_init(&r->res_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +0000393
394 INIT_LIST_HEAD(&r->res_lookup);
395 INIT_LIST_HEAD(&r->res_grantqueue);
396 INIT_LIST_HEAD(&r->res_convertqueue);
397 INIT_LIST_HEAD(&r->res_waitqueue);
398 INIT_LIST_HEAD(&r->res_root_list);
399 INIT_LIST_HEAD(&r->res_recover_list);
400
David Teigland3881ac02011-07-07 14:05:03 -0500401 *r_ret = r;
402 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000403}
404
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500405static int rsb_cmp(struct dlm_rsb *r, const char *name, int nlen)
406{
407 char maxname[DLM_RESNAME_MAXLEN];
408
409 memset(maxname, 0, DLM_RESNAME_MAXLEN);
410 memcpy(maxname, name, nlen);
411 return memcmp(r->res_name, maxname, DLM_RESNAME_MAXLEN);
412}
413
David Teigland7210cb72012-03-08 12:37:12 -0600414int dlm_search_rsb_tree(struct rb_root *tree, char *name, int len,
415 unsigned int flags, struct dlm_rsb **r_ret)
David Teiglande7fd4172006-01-18 09:30:29 +0000416{
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500417 struct rb_node *node = tree->rb_node;
David Teiglande7fd4172006-01-18 09:30:29 +0000418 struct dlm_rsb *r;
419 int error = 0;
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500420 int rc;
David Teiglande7fd4172006-01-18 09:30:29 +0000421
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500422 while (node) {
423 r = rb_entry(node, struct dlm_rsb, res_hashnode);
424 rc = rsb_cmp(r, name, len);
425 if (rc < 0)
426 node = node->rb_left;
427 else if (rc > 0)
428 node = node->rb_right;
429 else
David Teiglande7fd4172006-01-18 09:30:29 +0000430 goto found;
431 }
Benny Halevy18c60c02008-06-30 19:59:14 +0300432 *r_ret = NULL;
David Teigland597d0ca2006-07-12 16:44:04 -0500433 return -EBADR;
David Teiglande7fd4172006-01-18 09:30:29 +0000434
435 found:
436 if (r->res_nodeid && (flags & R_MASTER))
437 error = -ENOTBLK;
438 *r_ret = r;
439 return error;
440}
441
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500442static int rsb_insert(struct dlm_rsb *rsb, struct rb_root *tree)
443{
444 struct rb_node **newn = &tree->rb_node;
445 struct rb_node *parent = NULL;
446 int rc;
447
448 while (*newn) {
449 struct dlm_rsb *cur = rb_entry(*newn, struct dlm_rsb,
450 res_hashnode);
451
452 parent = *newn;
453 rc = rsb_cmp(cur, rsb->res_name, rsb->res_length);
454 if (rc < 0)
455 newn = &parent->rb_left;
456 else if (rc > 0)
457 newn = &parent->rb_right;
458 else {
459 log_print("rsb_insert match");
460 dlm_dump_rsb(rsb);
461 dlm_dump_rsb(cur);
462 return -EEXIST;
463 }
464 }
465
466 rb_link_node(&rsb->res_hashnode, parent, newn);
467 rb_insert_color(&rsb->res_hashnode, tree);
468 return 0;
469}
470
David Teiglande7fd4172006-01-18 09:30:29 +0000471static int _search_rsb(struct dlm_ls *ls, char *name, int len, int b,
472 unsigned int flags, struct dlm_rsb **r_ret)
473{
474 struct dlm_rsb *r;
475 int error;
476
David Teigland7210cb72012-03-08 12:37:12 -0600477 error = dlm_search_rsb_tree(&ls->ls_rsbtbl[b].keep, name, len, flags, &r);
David Teiglande7fd4172006-01-18 09:30:29 +0000478 if (!error) {
479 kref_get(&r->res_ref);
480 goto out;
481 }
David Teigland57638bf2012-04-23 14:08:52 -0500482 if (error == -ENOTBLK)
483 goto out;
484
David Teigland7210cb72012-03-08 12:37:12 -0600485 error = dlm_search_rsb_tree(&ls->ls_rsbtbl[b].toss, name, len, flags, &r);
David Teiglande7fd4172006-01-18 09:30:29 +0000486 if (error)
487 goto out;
488
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500489 rb_erase(&r->res_hashnode, &ls->ls_rsbtbl[b].toss);
490 error = rsb_insert(r, &ls->ls_rsbtbl[b].keep);
491 if (error)
492 return error;
David Teiglande7fd4172006-01-18 09:30:29 +0000493
494 if (dlm_no_directory(ls))
495 goto out;
496
497 if (r->res_nodeid == -1) {
498 rsb_clear_flag(r, RSB_MASTER_UNCERTAIN);
499 r->res_first_lkid = 0;
500 } else if (r->res_nodeid > 0) {
501 rsb_set_flag(r, RSB_MASTER_UNCERTAIN);
502 r->res_first_lkid = 0;
503 } else {
504 DLM_ASSERT(r->res_nodeid == 0, dlm_print_rsb(r););
505 DLM_ASSERT(!rsb_flag(r, RSB_MASTER_UNCERTAIN),);
506 }
507 out:
508 *r_ret = r;
509 return error;
510}
511
David Teiglande7fd4172006-01-18 09:30:29 +0000512/*
513 * Find rsb in rsbtbl and potentially create/add one
514 *
515 * Delaying the release of rsb's has a similar benefit to applications keeping
516 * NL locks on an rsb, but without the guarantee that the cached master value
517 * will still be valid when the rsb is reused. Apps aren't always smart enough
518 * to keep NL locks on an rsb that they may lock again shortly; this can lead
519 * to excessive master lookups and removals if we don't delay the release.
520 *
521 * Searching for an rsb means looking through both the normal list and toss
522 * list. When found on the toss list the rsb is moved to the normal list with
523 * ref count of 1; when found on normal list the ref count is incremented.
524 */
525
526static int find_rsb(struct dlm_ls *ls, char *name, int namelen,
527 unsigned int flags, struct dlm_rsb **r_ret)
528{
David Teigland3881ac02011-07-07 14:05:03 -0500529 struct dlm_rsb *r = NULL;
David Teiglande7fd4172006-01-18 09:30:29 +0000530 uint32_t hash, bucket;
David Teigland3881ac02011-07-07 14:05:03 -0500531 int error;
Al Viroef58bcc2008-01-25 23:22:26 -0500532
David Teigland3881ac02011-07-07 14:05:03 -0500533 if (namelen > DLM_RESNAME_MAXLEN) {
534 error = -EINVAL;
Al Viroef58bcc2008-01-25 23:22:26 -0500535 goto out;
David Teigland3881ac02011-07-07 14:05:03 -0500536 }
David Teiglande7fd4172006-01-18 09:30:29 +0000537
538 if (dlm_no_directory(ls))
539 flags |= R_CREATE;
540
541 hash = jhash(name, namelen, 0);
542 bucket = hash & (ls->ls_rsbtbl_size - 1);
543
David Teigland3881ac02011-07-07 14:05:03 -0500544 retry:
545 if (flags & R_CREATE) {
546 error = pre_rsb_struct(ls);
547 if (error < 0)
548 goto out;
549 }
550
551 spin_lock(&ls->ls_rsbtbl[bucket].lock);
552
553 error = _search_rsb(ls, name, namelen, bucket, flags, &r);
David Teiglande7fd4172006-01-18 09:30:29 +0000554 if (!error)
David Teigland3881ac02011-07-07 14:05:03 -0500555 goto out_unlock;
David Teiglande7fd4172006-01-18 09:30:29 +0000556
David Teigland597d0ca2006-07-12 16:44:04 -0500557 if (error == -EBADR && !(flags & R_CREATE))
David Teigland3881ac02011-07-07 14:05:03 -0500558 goto out_unlock;
David Teiglande7fd4172006-01-18 09:30:29 +0000559
560 /* the rsb was found but wasn't a master copy */
561 if (error == -ENOTBLK)
David Teigland3881ac02011-07-07 14:05:03 -0500562 goto out_unlock;
David Teiglande7fd4172006-01-18 09:30:29 +0000563
David Teigland3881ac02011-07-07 14:05:03 -0500564 error = get_rsb_struct(ls, name, namelen, &r);
565 if (error == -EAGAIN) {
566 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
567 goto retry;
568 }
569 if (error)
570 goto out_unlock;
David Teiglande7fd4172006-01-18 09:30:29 +0000571
572 r->res_hash = hash;
573 r->res_bucket = bucket;
574 r->res_nodeid = -1;
575 kref_init(&r->res_ref);
576
577 /* With no directory, the master can be set immediately */
578 if (dlm_no_directory(ls)) {
579 int nodeid = dlm_dir_nodeid(r);
580 if (nodeid == dlm_our_nodeid())
581 nodeid = 0;
582 r->res_nodeid = nodeid;
583 }
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500584 error = rsb_insert(r, &ls->ls_rsbtbl[bucket].keep);
David Teigland3881ac02011-07-07 14:05:03 -0500585 out_unlock:
586 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000587 out:
588 *r_ret = r;
589 return error;
590}
591
David Teigland6d40c4a2012-04-23 16:36:01 -0500592static void dlm_dump_rsb_hash(struct dlm_ls *ls, uint32_t hash)
593{
594 struct rb_node *n;
595 struct dlm_rsb *r;
596 int i;
597
598 for (i = 0; i < ls->ls_rsbtbl_size; i++) {
599 spin_lock(&ls->ls_rsbtbl[i].lock);
600 for (n = rb_first(&ls->ls_rsbtbl[i].keep); n; n = rb_next(n)) {
601 r = rb_entry(n, struct dlm_rsb, res_hashnode);
602 if (r->res_hash == hash)
603 dlm_dump_rsb(r);
604 }
605 spin_unlock(&ls->ls_rsbtbl[i].lock);
606 }
607}
608
David Teiglande7fd4172006-01-18 09:30:29 +0000609/* This is only called to add a reference when the code already holds
610 a valid reference to the rsb, so there's no need for locking. */
611
612static inline void hold_rsb(struct dlm_rsb *r)
613{
614 kref_get(&r->res_ref);
615}
616
617void dlm_hold_rsb(struct dlm_rsb *r)
618{
619 hold_rsb(r);
620}
621
622static void toss_rsb(struct kref *kref)
623{
624 struct dlm_rsb *r = container_of(kref, struct dlm_rsb, res_ref);
625 struct dlm_ls *ls = r->res_ls;
626
627 DLM_ASSERT(list_empty(&r->res_root_list), dlm_print_rsb(r););
628 kref_init(&r->res_ref);
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500629 rb_erase(&r->res_hashnode, &ls->ls_rsbtbl[r->res_bucket].keep);
630 rsb_insert(r, &ls->ls_rsbtbl[r->res_bucket].toss);
David Teiglande7fd4172006-01-18 09:30:29 +0000631 r->res_toss_time = jiffies;
632 if (r->res_lvbptr) {
David Teigland52bda2b2007-11-07 09:06:49 -0600633 dlm_free_lvb(r->res_lvbptr);
David Teiglande7fd4172006-01-18 09:30:29 +0000634 r->res_lvbptr = NULL;
635 }
636}
637
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300638/* When all references to the rsb are gone it's transferred to
David Teiglande7fd4172006-01-18 09:30:29 +0000639 the tossed list for later disposal. */
640
641static void put_rsb(struct dlm_rsb *r)
642{
643 struct dlm_ls *ls = r->res_ls;
644 uint32_t bucket = r->res_bucket;
645
David Teiglandc7be7612009-01-07 16:50:41 -0600646 spin_lock(&ls->ls_rsbtbl[bucket].lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000647 kref_put(&r->res_ref, toss_rsb);
David Teiglandc7be7612009-01-07 16:50:41 -0600648 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000649}
650
651void dlm_put_rsb(struct dlm_rsb *r)
652{
653 put_rsb(r);
654}
655
656/* See comment for unhold_lkb */
657
658static void unhold_rsb(struct dlm_rsb *r)
659{
660 int rv;
661 rv = kref_put(&r->res_ref, toss_rsb);
David Teiglanda345da32006-08-18 11:54:25 -0500662 DLM_ASSERT(!rv, dlm_dump_rsb(r););
David Teiglande7fd4172006-01-18 09:30:29 +0000663}
664
665static void kill_rsb(struct kref *kref)
666{
667 struct dlm_rsb *r = container_of(kref, struct dlm_rsb, res_ref);
668
669 /* All work is done after the return from kref_put() so we
670 can release the write_lock before the remove and free. */
671
David Teiglanda345da32006-08-18 11:54:25 -0500672 DLM_ASSERT(list_empty(&r->res_lookup), dlm_dump_rsb(r););
673 DLM_ASSERT(list_empty(&r->res_grantqueue), dlm_dump_rsb(r););
674 DLM_ASSERT(list_empty(&r->res_convertqueue), dlm_dump_rsb(r););
675 DLM_ASSERT(list_empty(&r->res_waitqueue), dlm_dump_rsb(r););
676 DLM_ASSERT(list_empty(&r->res_root_list), dlm_dump_rsb(r););
677 DLM_ASSERT(list_empty(&r->res_recover_list), dlm_dump_rsb(r););
David Teiglande7fd4172006-01-18 09:30:29 +0000678}
679
680/* Attaching/detaching lkb's from rsb's is for rsb reference counting.
681 The rsb must exist as long as any lkb's for it do. */
682
683static void attach_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb)
684{
685 hold_rsb(r);
686 lkb->lkb_resource = r;
687}
688
689static void detach_lkb(struct dlm_lkb *lkb)
690{
691 if (lkb->lkb_resource) {
692 put_rsb(lkb->lkb_resource);
693 lkb->lkb_resource = NULL;
694 }
695}
696
697static int create_lkb(struct dlm_ls *ls, struct dlm_lkb **lkb_ret)
698{
David Teigland3d6aa672011-07-06 17:00:54 -0500699 struct dlm_lkb *lkb;
700 int rv, id;
David Teiglande7fd4172006-01-18 09:30:29 +0000701
David Teigland52bda2b2007-11-07 09:06:49 -0600702 lkb = dlm_allocate_lkb(ls);
David Teiglande7fd4172006-01-18 09:30:29 +0000703 if (!lkb)
704 return -ENOMEM;
705
706 lkb->lkb_nodeid = -1;
707 lkb->lkb_grmode = DLM_LOCK_IV;
708 kref_init(&lkb->lkb_ref);
David Teigland34e22be2006-07-18 11:24:04 -0500709 INIT_LIST_HEAD(&lkb->lkb_ownqueue);
David Teiglandef0c2bb2007-03-28 09:56:46 -0500710 INIT_LIST_HEAD(&lkb->lkb_rsb_lookup);
David Teigland3ae1acf2007-05-18 08:59:31 -0500711 INIT_LIST_HEAD(&lkb->lkb_time_list);
David Teigland23e8e1a2011-04-05 13:16:24 -0500712 INIT_LIST_HEAD(&lkb->lkb_cb_list);
713 mutex_init(&lkb->lkb_cb_mutex);
714 INIT_WORK(&lkb->lkb_cb_work, dlm_callback_work);
David Teiglande7fd4172006-01-18 09:30:29 +0000715
David Teigland3d6aa672011-07-06 17:00:54 -0500716 retry:
717 rv = idr_pre_get(&ls->ls_lkbidr, GFP_NOFS);
718 if (!rv)
719 return -ENOMEM;
David Teiglande7fd4172006-01-18 09:30:29 +0000720
David Teigland3d6aa672011-07-06 17:00:54 -0500721 spin_lock(&ls->ls_lkbidr_spin);
722 rv = idr_get_new_above(&ls->ls_lkbidr, lkb, 1, &id);
723 if (!rv)
724 lkb->lkb_id = id;
725 spin_unlock(&ls->ls_lkbidr_spin);
David Teiglande7fd4172006-01-18 09:30:29 +0000726
David Teigland3d6aa672011-07-06 17:00:54 -0500727 if (rv == -EAGAIN)
728 goto retry;
David Teiglande7fd4172006-01-18 09:30:29 +0000729
David Teigland3d6aa672011-07-06 17:00:54 -0500730 if (rv < 0) {
731 log_error(ls, "create_lkb idr error %d", rv);
732 return rv;
David Teiglande7fd4172006-01-18 09:30:29 +0000733 }
734
David Teiglande7fd4172006-01-18 09:30:29 +0000735 *lkb_ret = lkb;
736 return 0;
737}
738
David Teiglande7fd4172006-01-18 09:30:29 +0000739static int find_lkb(struct dlm_ls *ls, uint32_t lkid, struct dlm_lkb **lkb_ret)
740{
741 struct dlm_lkb *lkb;
David Teiglande7fd4172006-01-18 09:30:29 +0000742
David Teigland3d6aa672011-07-06 17:00:54 -0500743 spin_lock(&ls->ls_lkbidr_spin);
744 lkb = idr_find(&ls->ls_lkbidr, lkid);
David Teiglande7fd4172006-01-18 09:30:29 +0000745 if (lkb)
746 kref_get(&lkb->lkb_ref);
David Teigland3d6aa672011-07-06 17:00:54 -0500747 spin_unlock(&ls->ls_lkbidr_spin);
David Teiglande7fd4172006-01-18 09:30:29 +0000748
749 *lkb_ret = lkb;
750 return lkb ? 0 : -ENOENT;
751}
752
753static void kill_lkb(struct kref *kref)
754{
755 struct dlm_lkb *lkb = container_of(kref, struct dlm_lkb, lkb_ref);
756
757 /* All work is done after the return from kref_put() so we
758 can release the write_lock before the detach_lkb */
759
760 DLM_ASSERT(!lkb->lkb_status, dlm_print_lkb(lkb););
761}
762
David Teiglandb3f58d82006-02-28 11:16:37 -0500763/* __put_lkb() is used when an lkb may not have an rsb attached to
764 it so we need to provide the lockspace explicitly */
765
766static int __put_lkb(struct dlm_ls *ls, struct dlm_lkb *lkb)
David Teiglande7fd4172006-01-18 09:30:29 +0000767{
David Teigland3d6aa672011-07-06 17:00:54 -0500768 uint32_t lkid = lkb->lkb_id;
David Teiglande7fd4172006-01-18 09:30:29 +0000769
David Teigland3d6aa672011-07-06 17:00:54 -0500770 spin_lock(&ls->ls_lkbidr_spin);
David Teiglande7fd4172006-01-18 09:30:29 +0000771 if (kref_put(&lkb->lkb_ref, kill_lkb)) {
David Teigland3d6aa672011-07-06 17:00:54 -0500772 idr_remove(&ls->ls_lkbidr, lkid);
773 spin_unlock(&ls->ls_lkbidr_spin);
David Teiglande7fd4172006-01-18 09:30:29 +0000774
775 detach_lkb(lkb);
776
777 /* for local/process lkbs, lvbptr points to caller's lksb */
778 if (lkb->lkb_lvbptr && is_master_copy(lkb))
David Teigland52bda2b2007-11-07 09:06:49 -0600779 dlm_free_lvb(lkb->lkb_lvbptr);
780 dlm_free_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +0000781 return 1;
782 } else {
David Teigland3d6aa672011-07-06 17:00:54 -0500783 spin_unlock(&ls->ls_lkbidr_spin);
David Teiglande7fd4172006-01-18 09:30:29 +0000784 return 0;
785 }
786}
787
788int dlm_put_lkb(struct dlm_lkb *lkb)
789{
David Teiglandb3f58d82006-02-28 11:16:37 -0500790 struct dlm_ls *ls;
791
792 DLM_ASSERT(lkb->lkb_resource, dlm_print_lkb(lkb););
793 DLM_ASSERT(lkb->lkb_resource->res_ls, dlm_print_lkb(lkb););
794
795 ls = lkb->lkb_resource->res_ls;
796 return __put_lkb(ls, lkb);
David Teiglande7fd4172006-01-18 09:30:29 +0000797}
798
799/* This is only called to add a reference when the code already holds
800 a valid reference to the lkb, so there's no need for locking. */
801
802static inline void hold_lkb(struct dlm_lkb *lkb)
803{
804 kref_get(&lkb->lkb_ref);
805}
806
807/* This is called when we need to remove a reference and are certain
808 it's not the last ref. e.g. del_lkb is always called between a
809 find_lkb/put_lkb and is always the inverse of a previous add_lkb.
810 put_lkb would work fine, but would involve unnecessary locking */
811
812static inline void unhold_lkb(struct dlm_lkb *lkb)
813{
814 int rv;
815 rv = kref_put(&lkb->lkb_ref, kill_lkb);
816 DLM_ASSERT(!rv, dlm_print_lkb(lkb););
817}
818
819static void lkb_add_ordered(struct list_head *new, struct list_head *head,
820 int mode)
821{
822 struct dlm_lkb *lkb = NULL;
823
824 list_for_each_entry(lkb, head, lkb_statequeue)
825 if (lkb->lkb_rqmode < mode)
826 break;
827
Dan Carpenter99fb19d2010-03-22 15:03:54 +0300828 __list_add(new, lkb->lkb_statequeue.prev, &lkb->lkb_statequeue);
David Teiglande7fd4172006-01-18 09:30:29 +0000829}
830
831/* add/remove lkb to rsb's grant/convert/wait queue */
832
833static void add_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb, int status)
834{
835 kref_get(&lkb->lkb_ref);
836
837 DLM_ASSERT(!lkb->lkb_status, dlm_print_lkb(lkb););
838
David Teiglandeeda4182008-12-09 14:12:21 -0600839 lkb->lkb_timestamp = ktime_get();
840
David Teiglande7fd4172006-01-18 09:30:29 +0000841 lkb->lkb_status = status;
842
843 switch (status) {
844 case DLM_LKSTS_WAITING:
845 if (lkb->lkb_exflags & DLM_LKF_HEADQUE)
846 list_add(&lkb->lkb_statequeue, &r->res_waitqueue);
847 else
848 list_add_tail(&lkb->lkb_statequeue, &r->res_waitqueue);
849 break;
850 case DLM_LKSTS_GRANTED:
851 /* convention says granted locks kept in order of grmode */
852 lkb_add_ordered(&lkb->lkb_statequeue, &r->res_grantqueue,
853 lkb->lkb_grmode);
854 break;
855 case DLM_LKSTS_CONVERT:
856 if (lkb->lkb_exflags & DLM_LKF_HEADQUE)
857 list_add(&lkb->lkb_statequeue, &r->res_convertqueue);
858 else
859 list_add_tail(&lkb->lkb_statequeue,
860 &r->res_convertqueue);
861 break;
862 default:
863 DLM_ASSERT(0, dlm_print_lkb(lkb); printk("sts=%d\n", status););
864 }
865}
866
867static void del_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb)
868{
869 lkb->lkb_status = 0;
870 list_del(&lkb->lkb_statequeue);
871 unhold_lkb(lkb);
872}
873
874static void move_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb, int sts)
875{
876 hold_lkb(lkb);
877 del_lkb(r, lkb);
878 add_lkb(r, lkb, sts);
879 unhold_lkb(lkb);
880}
881
David Teiglandef0c2bb2007-03-28 09:56:46 -0500882static int msg_reply_type(int mstype)
883{
884 switch (mstype) {
885 case DLM_MSG_REQUEST:
886 return DLM_MSG_REQUEST_REPLY;
887 case DLM_MSG_CONVERT:
888 return DLM_MSG_CONVERT_REPLY;
889 case DLM_MSG_UNLOCK:
890 return DLM_MSG_UNLOCK_REPLY;
891 case DLM_MSG_CANCEL:
892 return DLM_MSG_CANCEL_REPLY;
893 case DLM_MSG_LOOKUP:
894 return DLM_MSG_LOOKUP_REPLY;
895 }
896 return -1;
897}
898
David Teiglandc6ff6692011-03-28 14:17:26 -0500899static int nodeid_warned(int nodeid, int num_nodes, int *warned)
900{
901 int i;
902
903 for (i = 0; i < num_nodes; i++) {
904 if (!warned[i]) {
905 warned[i] = nodeid;
906 return 0;
907 }
908 if (warned[i] == nodeid)
909 return 1;
910 }
911 return 0;
912}
913
914void dlm_scan_waiters(struct dlm_ls *ls)
915{
916 struct dlm_lkb *lkb;
917 ktime_t zero = ktime_set(0, 0);
918 s64 us;
919 s64 debug_maxus = 0;
920 u32 debug_scanned = 0;
921 u32 debug_expired = 0;
922 int num_nodes = 0;
923 int *warned = NULL;
924
925 if (!dlm_config.ci_waitwarn_us)
926 return;
927
928 mutex_lock(&ls->ls_waiters_mutex);
929
930 list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
931 if (ktime_equal(lkb->lkb_wait_time, zero))
932 continue;
933
934 debug_scanned++;
935
936 us = ktime_to_us(ktime_sub(ktime_get(), lkb->lkb_wait_time));
937
938 if (us < dlm_config.ci_waitwarn_us)
939 continue;
940
941 lkb->lkb_wait_time = zero;
942
943 debug_expired++;
944 if (us > debug_maxus)
945 debug_maxus = us;
946
947 if (!num_nodes) {
948 num_nodes = ls->ls_num_nodes;
Jesper Juhl5d708282011-07-10 22:54:31 +0200949 warned = kzalloc(num_nodes * sizeof(int), GFP_KERNEL);
David Teiglandc6ff6692011-03-28 14:17:26 -0500950 }
951 if (!warned)
952 continue;
953 if (nodeid_warned(lkb->lkb_wait_nodeid, num_nodes, warned))
954 continue;
955
956 log_error(ls, "waitwarn %x %lld %d us check connection to "
957 "node %d", lkb->lkb_id, (long long)us,
958 dlm_config.ci_waitwarn_us, lkb->lkb_wait_nodeid);
959 }
960 mutex_unlock(&ls->ls_waiters_mutex);
Jesper Juhl5d708282011-07-10 22:54:31 +0200961 kfree(warned);
David Teiglandc6ff6692011-03-28 14:17:26 -0500962
963 if (debug_expired)
964 log_debug(ls, "scan_waiters %u warn %u over %d us max %lld us",
965 debug_scanned, debug_expired,
966 dlm_config.ci_waitwarn_us, (long long)debug_maxus);
967}
968
David Teiglande7fd4172006-01-18 09:30:29 +0000969/* add/remove lkb from global waiters list of lkb's waiting for
970 a reply from a remote node */
971
David Teiglandc6ff6692011-03-28 14:17:26 -0500972static int add_to_waiters(struct dlm_lkb *lkb, int mstype, int to_nodeid)
David Teiglande7fd4172006-01-18 09:30:29 +0000973{
974 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
David Teiglandef0c2bb2007-03-28 09:56:46 -0500975 int error = 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000976
David Teigland90135922006-01-20 08:47:07 +0000977 mutex_lock(&ls->ls_waiters_mutex);
David Teiglandef0c2bb2007-03-28 09:56:46 -0500978
979 if (is_overlap_unlock(lkb) ||
980 (is_overlap_cancel(lkb) && (mstype == DLM_MSG_CANCEL))) {
981 error = -EINVAL;
David Teiglande7fd4172006-01-18 09:30:29 +0000982 goto out;
983 }
David Teiglandef0c2bb2007-03-28 09:56:46 -0500984
985 if (lkb->lkb_wait_type || is_overlap_cancel(lkb)) {
986 switch (mstype) {
987 case DLM_MSG_UNLOCK:
988 lkb->lkb_flags |= DLM_IFL_OVERLAP_UNLOCK;
989 break;
990 case DLM_MSG_CANCEL:
991 lkb->lkb_flags |= DLM_IFL_OVERLAP_CANCEL;
992 break;
993 default:
994 error = -EBUSY;
995 goto out;
996 }
997 lkb->lkb_wait_count++;
998 hold_lkb(lkb);
999
David Teigland43279e52009-01-28 14:37:54 -06001000 log_debug(ls, "addwait %x cur %d overlap %d count %d f %x",
David Teiglandef0c2bb2007-03-28 09:56:46 -05001001 lkb->lkb_id, lkb->lkb_wait_type, mstype,
1002 lkb->lkb_wait_count, lkb->lkb_flags);
1003 goto out;
1004 }
1005
1006 DLM_ASSERT(!lkb->lkb_wait_count,
1007 dlm_print_lkb(lkb);
1008 printk("wait_count %d\n", lkb->lkb_wait_count););
1009
1010 lkb->lkb_wait_count++;
David Teiglande7fd4172006-01-18 09:30:29 +00001011 lkb->lkb_wait_type = mstype;
David Teiglandc6ff6692011-03-28 14:17:26 -05001012 lkb->lkb_wait_time = ktime_get();
1013 lkb->lkb_wait_nodeid = to_nodeid; /* for debugging */
David Teiglandef0c2bb2007-03-28 09:56:46 -05001014 hold_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00001015 list_add(&lkb->lkb_wait_reply, &ls->ls_waiters);
1016 out:
David Teiglandef0c2bb2007-03-28 09:56:46 -05001017 if (error)
David Teigland43279e52009-01-28 14:37:54 -06001018 log_error(ls, "addwait error %x %d flags %x %d %d %s",
David Teiglandef0c2bb2007-03-28 09:56:46 -05001019 lkb->lkb_id, error, lkb->lkb_flags, mstype,
1020 lkb->lkb_wait_type, lkb->lkb_resource->res_name);
David Teigland90135922006-01-20 08:47:07 +00001021 mutex_unlock(&ls->ls_waiters_mutex);
David Teiglandef0c2bb2007-03-28 09:56:46 -05001022 return error;
David Teiglande7fd4172006-01-18 09:30:29 +00001023}
1024
David Teiglandb790c3b2007-01-24 10:21:33 -06001025/* We clear the RESEND flag because we might be taking an lkb off the waiters
1026 list as part of process_requestqueue (e.g. a lookup that has an optimized
1027 request reply on the requestqueue) between dlm_recover_waiters_pre() which
1028 set RESEND and dlm_recover_waiters_post() */
1029
David Teigland43279e52009-01-28 14:37:54 -06001030static int _remove_from_waiters(struct dlm_lkb *lkb, int mstype,
1031 struct dlm_message *ms)
David Teiglande7fd4172006-01-18 09:30:29 +00001032{
David Teiglandef0c2bb2007-03-28 09:56:46 -05001033 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1034 int overlap_done = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001035
David Teiglandef0c2bb2007-03-28 09:56:46 -05001036 if (is_overlap_unlock(lkb) && (mstype == DLM_MSG_UNLOCK_REPLY)) {
David Teigland43279e52009-01-28 14:37:54 -06001037 log_debug(ls, "remwait %x unlock_reply overlap", lkb->lkb_id);
David Teiglandef0c2bb2007-03-28 09:56:46 -05001038 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
1039 overlap_done = 1;
1040 goto out_del;
David Teiglande7fd4172006-01-18 09:30:29 +00001041 }
David Teiglandef0c2bb2007-03-28 09:56:46 -05001042
1043 if (is_overlap_cancel(lkb) && (mstype == DLM_MSG_CANCEL_REPLY)) {
David Teigland43279e52009-01-28 14:37:54 -06001044 log_debug(ls, "remwait %x cancel_reply overlap", lkb->lkb_id);
David Teiglandef0c2bb2007-03-28 09:56:46 -05001045 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
1046 overlap_done = 1;
1047 goto out_del;
1048 }
1049
David Teigland43279e52009-01-28 14:37:54 -06001050 /* Cancel state was preemptively cleared by a successful convert,
1051 see next comment, nothing to do. */
1052
1053 if ((mstype == DLM_MSG_CANCEL_REPLY) &&
1054 (lkb->lkb_wait_type != DLM_MSG_CANCEL)) {
1055 log_debug(ls, "remwait %x cancel_reply wait_type %d",
1056 lkb->lkb_id, lkb->lkb_wait_type);
1057 return -1;
1058 }
1059
1060 /* Remove for the convert reply, and premptively remove for the
1061 cancel reply. A convert has been granted while there's still
1062 an outstanding cancel on it (the cancel is moot and the result
1063 in the cancel reply should be 0). We preempt the cancel reply
1064 because the app gets the convert result and then can follow up
1065 with another op, like convert. This subsequent op would see the
1066 lingering state of the cancel and fail with -EBUSY. */
1067
1068 if ((mstype == DLM_MSG_CONVERT_REPLY) &&
1069 (lkb->lkb_wait_type == DLM_MSG_CONVERT) &&
1070 is_overlap_cancel(lkb) && ms && !ms->m_result) {
1071 log_debug(ls, "remwait %x convert_reply zap overlap_cancel",
1072 lkb->lkb_id);
1073 lkb->lkb_wait_type = 0;
1074 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
1075 lkb->lkb_wait_count--;
1076 goto out_del;
1077 }
1078
David Teiglandef0c2bb2007-03-28 09:56:46 -05001079 /* N.B. type of reply may not always correspond to type of original
1080 msg due to lookup->request optimization, verify others? */
1081
1082 if (lkb->lkb_wait_type) {
1083 lkb->lkb_wait_type = 0;
1084 goto out_del;
1085 }
1086
David Teigland6d40c4a2012-04-23 16:36:01 -05001087 log_error(ls, "remwait error %x remote %d %x msg %d flags %x no wait",
1088 lkb->lkb_id, ms ? ms->m_header.h_nodeid : 0, lkb->lkb_remid,
1089 mstype, lkb->lkb_flags);
David Teiglandef0c2bb2007-03-28 09:56:46 -05001090 return -1;
1091
1092 out_del:
1093 /* the force-unlock/cancel has completed and we haven't recvd a reply
1094 to the op that was in progress prior to the unlock/cancel; we
1095 give up on any reply to the earlier op. FIXME: not sure when/how
1096 this would happen */
1097
1098 if (overlap_done && lkb->lkb_wait_type) {
David Teigland43279e52009-01-28 14:37:54 -06001099 log_error(ls, "remwait error %x reply %d wait_type %d overlap",
David Teiglandef0c2bb2007-03-28 09:56:46 -05001100 lkb->lkb_id, mstype, lkb->lkb_wait_type);
1101 lkb->lkb_wait_count--;
1102 lkb->lkb_wait_type = 0;
1103 }
1104
1105 DLM_ASSERT(lkb->lkb_wait_count, dlm_print_lkb(lkb););
1106
David Teiglandb790c3b2007-01-24 10:21:33 -06001107 lkb->lkb_flags &= ~DLM_IFL_RESEND;
David Teiglandef0c2bb2007-03-28 09:56:46 -05001108 lkb->lkb_wait_count--;
1109 if (!lkb->lkb_wait_count)
1110 list_del_init(&lkb->lkb_wait_reply);
David Teiglande7fd4172006-01-18 09:30:29 +00001111 unhold_lkb(lkb);
David Teiglandef0c2bb2007-03-28 09:56:46 -05001112 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001113}
1114
David Teiglandef0c2bb2007-03-28 09:56:46 -05001115static int remove_from_waiters(struct dlm_lkb *lkb, int mstype)
David Teiglande7fd4172006-01-18 09:30:29 +00001116{
1117 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1118 int error;
1119
David Teigland90135922006-01-20 08:47:07 +00001120 mutex_lock(&ls->ls_waiters_mutex);
David Teigland43279e52009-01-28 14:37:54 -06001121 error = _remove_from_waiters(lkb, mstype, NULL);
David Teigland90135922006-01-20 08:47:07 +00001122 mutex_unlock(&ls->ls_waiters_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +00001123 return error;
1124}
1125
David Teiglandef0c2bb2007-03-28 09:56:46 -05001126/* Handles situations where we might be processing a "fake" or "stub" reply in
1127 which we can't try to take waiters_mutex again. */
1128
1129static int remove_from_waiters_ms(struct dlm_lkb *lkb, struct dlm_message *ms)
1130{
1131 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1132 int error;
1133
David Teigland2a7ce0e2011-04-04 15:19:59 -05001134 if (ms->m_flags != DLM_IFL_STUB_MS)
David Teiglandef0c2bb2007-03-28 09:56:46 -05001135 mutex_lock(&ls->ls_waiters_mutex);
David Teigland43279e52009-01-28 14:37:54 -06001136 error = _remove_from_waiters(lkb, ms->m_type, ms);
David Teigland2a7ce0e2011-04-04 15:19:59 -05001137 if (ms->m_flags != DLM_IFL_STUB_MS)
David Teiglandef0c2bb2007-03-28 09:56:46 -05001138 mutex_unlock(&ls->ls_waiters_mutex);
1139 return error;
1140}
1141
David Teiglande7fd4172006-01-18 09:30:29 +00001142static void dir_remove(struct dlm_rsb *r)
1143{
1144 int to_nodeid;
1145
1146 if (dlm_no_directory(r->res_ls))
1147 return;
1148
1149 to_nodeid = dlm_dir_nodeid(r);
1150 if (to_nodeid != dlm_our_nodeid())
1151 send_remove(r);
1152 else
1153 dlm_dir_remove_entry(r->res_ls, to_nodeid,
1154 r->res_name, r->res_length);
1155}
1156
Bob Peterson9beb3bf2011-10-26 15:24:55 -05001157/* FIXME: make this more efficient */
David Teiglande7fd4172006-01-18 09:30:29 +00001158
1159static int shrink_bucket(struct dlm_ls *ls, int b)
1160{
Bob Peterson9beb3bf2011-10-26 15:24:55 -05001161 struct rb_node *n;
David Teiglande7fd4172006-01-18 09:30:29 +00001162 struct dlm_rsb *r;
1163 int count = 0, found;
1164
1165 for (;;) {
David Teigland90135922006-01-20 08:47:07 +00001166 found = 0;
David Teiglandc7be7612009-01-07 16:50:41 -06001167 spin_lock(&ls->ls_rsbtbl[b].lock);
Bob Peterson9beb3bf2011-10-26 15:24:55 -05001168 for (n = rb_first(&ls->ls_rsbtbl[b].toss); n; n = rb_next(n)) {
1169 r = rb_entry(n, struct dlm_rsb, res_hashnode);
David Teiglande7fd4172006-01-18 09:30:29 +00001170 if (!time_after_eq(jiffies, r->res_toss_time +
David Teigland68c817a2007-01-09 09:41:48 -06001171 dlm_config.ci_toss_secs * HZ))
David Teiglande7fd4172006-01-18 09:30:29 +00001172 continue;
David Teigland90135922006-01-20 08:47:07 +00001173 found = 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001174 break;
1175 }
1176
1177 if (!found) {
David Teiglandc7be7612009-01-07 16:50:41 -06001178 spin_unlock(&ls->ls_rsbtbl[b].lock);
David Teiglande7fd4172006-01-18 09:30:29 +00001179 break;
1180 }
1181
1182 if (kref_put(&r->res_ref, kill_rsb)) {
Bob Peterson9beb3bf2011-10-26 15:24:55 -05001183 rb_erase(&r->res_hashnode, &ls->ls_rsbtbl[b].toss);
David Teiglandc7be7612009-01-07 16:50:41 -06001184 spin_unlock(&ls->ls_rsbtbl[b].lock);
David Teiglande7fd4172006-01-18 09:30:29 +00001185
1186 if (is_master(r))
1187 dir_remove(r);
David Teigland52bda2b2007-11-07 09:06:49 -06001188 dlm_free_rsb(r);
David Teiglande7fd4172006-01-18 09:30:29 +00001189 count++;
1190 } else {
David Teiglandc7be7612009-01-07 16:50:41 -06001191 spin_unlock(&ls->ls_rsbtbl[b].lock);
David Teiglande7fd4172006-01-18 09:30:29 +00001192 log_error(ls, "tossed rsb in use %s", r->res_name);
1193 }
1194 }
1195
1196 return count;
1197}
1198
1199void dlm_scan_rsbs(struct dlm_ls *ls)
1200{
1201 int i;
1202
David Teiglande7fd4172006-01-18 09:30:29 +00001203 for (i = 0; i < ls->ls_rsbtbl_size; i++) {
1204 shrink_bucket(ls, i);
David Teigland85e86ed2007-05-18 08:58:15 -05001205 if (dlm_locking_stopped(ls))
1206 break;
David Teiglande7fd4172006-01-18 09:30:29 +00001207 cond_resched();
1208 }
1209}
1210
David Teigland3ae1acf2007-05-18 08:59:31 -05001211static void add_timeout(struct dlm_lkb *lkb)
1212{
1213 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1214
David Teiglandeeda4182008-12-09 14:12:21 -06001215 if (is_master_copy(lkb))
David Teigland3ae1acf2007-05-18 08:59:31 -05001216 return;
David Teigland3ae1acf2007-05-18 08:59:31 -05001217
1218 if (test_bit(LSFL_TIMEWARN, &ls->ls_flags) &&
1219 !(lkb->lkb_exflags & DLM_LKF_NODLCKWT)) {
1220 lkb->lkb_flags |= DLM_IFL_WATCH_TIMEWARN;
1221 goto add_it;
1222 }
David Teigland84d8cd62007-05-29 08:44:23 -05001223 if (lkb->lkb_exflags & DLM_LKF_TIMEOUT)
1224 goto add_it;
David Teigland3ae1acf2007-05-18 08:59:31 -05001225 return;
1226
1227 add_it:
1228 DLM_ASSERT(list_empty(&lkb->lkb_time_list), dlm_print_lkb(lkb););
1229 mutex_lock(&ls->ls_timeout_mutex);
1230 hold_lkb(lkb);
David Teigland3ae1acf2007-05-18 08:59:31 -05001231 list_add_tail(&lkb->lkb_time_list, &ls->ls_timeout);
1232 mutex_unlock(&ls->ls_timeout_mutex);
1233}
1234
1235static void del_timeout(struct dlm_lkb *lkb)
1236{
1237 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1238
1239 mutex_lock(&ls->ls_timeout_mutex);
1240 if (!list_empty(&lkb->lkb_time_list)) {
1241 list_del_init(&lkb->lkb_time_list);
1242 unhold_lkb(lkb);
1243 }
1244 mutex_unlock(&ls->ls_timeout_mutex);
1245}
1246
1247/* FIXME: is it safe to look at lkb_exflags, lkb_flags, lkb_timestamp, and
1248 lkb_lksb_timeout without lock_rsb? Note: we can't lock timeout_mutex
1249 and then lock rsb because of lock ordering in add_timeout. We may need
1250 to specify some special timeout-related bits in the lkb that are just to
1251 be accessed under the timeout_mutex. */
1252
1253void dlm_scan_timeout(struct dlm_ls *ls)
1254{
1255 struct dlm_rsb *r;
1256 struct dlm_lkb *lkb;
1257 int do_cancel, do_warn;
David Teiglandeeda4182008-12-09 14:12:21 -06001258 s64 wait_us;
David Teigland3ae1acf2007-05-18 08:59:31 -05001259
1260 for (;;) {
1261 if (dlm_locking_stopped(ls))
1262 break;
1263
1264 do_cancel = 0;
1265 do_warn = 0;
1266 mutex_lock(&ls->ls_timeout_mutex);
1267 list_for_each_entry(lkb, &ls->ls_timeout, lkb_time_list) {
1268
David Teiglandeeda4182008-12-09 14:12:21 -06001269 wait_us = ktime_to_us(ktime_sub(ktime_get(),
1270 lkb->lkb_timestamp));
1271
David Teigland3ae1acf2007-05-18 08:59:31 -05001272 if ((lkb->lkb_exflags & DLM_LKF_TIMEOUT) &&
David Teiglandeeda4182008-12-09 14:12:21 -06001273 wait_us >= (lkb->lkb_timeout_cs * 10000))
David Teigland3ae1acf2007-05-18 08:59:31 -05001274 do_cancel = 1;
1275
1276 if ((lkb->lkb_flags & DLM_IFL_WATCH_TIMEWARN) &&
David Teiglandeeda4182008-12-09 14:12:21 -06001277 wait_us >= dlm_config.ci_timewarn_cs * 10000)
David Teigland3ae1acf2007-05-18 08:59:31 -05001278 do_warn = 1;
1279
1280 if (!do_cancel && !do_warn)
1281 continue;
1282 hold_lkb(lkb);
1283 break;
1284 }
1285 mutex_unlock(&ls->ls_timeout_mutex);
1286
1287 if (!do_cancel && !do_warn)
1288 break;
1289
1290 r = lkb->lkb_resource;
1291 hold_rsb(r);
1292 lock_rsb(r);
1293
1294 if (do_warn) {
1295 /* clear flag so we only warn once */
1296 lkb->lkb_flags &= ~DLM_IFL_WATCH_TIMEWARN;
1297 if (!(lkb->lkb_exflags & DLM_LKF_TIMEOUT))
1298 del_timeout(lkb);
1299 dlm_timeout_warn(lkb);
1300 }
1301
1302 if (do_cancel) {
Steven Whitehouseb3cab7b2007-05-29 11:14:21 +01001303 log_debug(ls, "timeout cancel %x node %d %s",
David Teigland639aca42007-05-18 16:02:57 -05001304 lkb->lkb_id, lkb->lkb_nodeid, r->res_name);
David Teigland3ae1acf2007-05-18 08:59:31 -05001305 lkb->lkb_flags &= ~DLM_IFL_WATCH_TIMEWARN;
1306 lkb->lkb_flags |= DLM_IFL_TIMEOUT_CANCEL;
1307 del_timeout(lkb);
1308 _cancel_lock(r, lkb);
1309 }
1310
1311 unlock_rsb(r);
1312 unhold_rsb(r);
1313 dlm_put_lkb(lkb);
1314 }
1315}
1316
1317/* This is only called by dlm_recoverd, and we rely on dlm_ls_stop() stopping
1318 dlm_recoverd before checking/setting ls_recover_begin. */
1319
1320void dlm_adjust_timeouts(struct dlm_ls *ls)
1321{
1322 struct dlm_lkb *lkb;
David Teiglandeeda4182008-12-09 14:12:21 -06001323 u64 adj_us = jiffies_to_usecs(jiffies - ls->ls_recover_begin);
David Teigland3ae1acf2007-05-18 08:59:31 -05001324
1325 ls->ls_recover_begin = 0;
1326 mutex_lock(&ls->ls_timeout_mutex);
1327 list_for_each_entry(lkb, &ls->ls_timeout, lkb_time_list)
David Teiglandeeda4182008-12-09 14:12:21 -06001328 lkb->lkb_timestamp = ktime_add_us(lkb->lkb_timestamp, adj_us);
David Teigland3ae1acf2007-05-18 08:59:31 -05001329 mutex_unlock(&ls->ls_timeout_mutex);
David Teiglandc6ff6692011-03-28 14:17:26 -05001330
1331 if (!dlm_config.ci_waitwarn_us)
1332 return;
1333
1334 mutex_lock(&ls->ls_waiters_mutex);
1335 list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
1336 if (ktime_to_us(lkb->lkb_wait_time))
1337 lkb->lkb_wait_time = ktime_get();
1338 }
1339 mutex_unlock(&ls->ls_waiters_mutex);
David Teigland3ae1acf2007-05-18 08:59:31 -05001340}
1341
David Teiglande7fd4172006-01-18 09:30:29 +00001342/* lkb is master or local copy */
1343
1344static void set_lvb_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1345{
1346 int b, len = r->res_ls->ls_lvblen;
1347
1348 /* b=1 lvb returned to caller
1349 b=0 lvb written to rsb or invalidated
1350 b=-1 do nothing */
1351
1352 b = dlm_lvb_operations[lkb->lkb_grmode + 1][lkb->lkb_rqmode + 1];
1353
1354 if (b == 1) {
1355 if (!lkb->lkb_lvbptr)
1356 return;
1357
1358 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1359 return;
1360
1361 if (!r->res_lvbptr)
1362 return;
1363
1364 memcpy(lkb->lkb_lvbptr, r->res_lvbptr, len);
1365 lkb->lkb_lvbseq = r->res_lvbseq;
1366
1367 } else if (b == 0) {
1368 if (lkb->lkb_exflags & DLM_LKF_IVVALBLK) {
1369 rsb_set_flag(r, RSB_VALNOTVALID);
1370 return;
1371 }
1372
1373 if (!lkb->lkb_lvbptr)
1374 return;
1375
1376 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1377 return;
1378
1379 if (!r->res_lvbptr)
David Teigland52bda2b2007-11-07 09:06:49 -06001380 r->res_lvbptr = dlm_allocate_lvb(r->res_ls);
David Teiglande7fd4172006-01-18 09:30:29 +00001381
1382 if (!r->res_lvbptr)
1383 return;
1384
1385 memcpy(r->res_lvbptr, lkb->lkb_lvbptr, len);
1386 r->res_lvbseq++;
1387 lkb->lkb_lvbseq = r->res_lvbseq;
1388 rsb_clear_flag(r, RSB_VALNOTVALID);
1389 }
1390
1391 if (rsb_flag(r, RSB_VALNOTVALID))
1392 lkb->lkb_sbflags |= DLM_SBF_VALNOTVALID;
1393}
1394
1395static void set_lvb_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1396{
1397 if (lkb->lkb_grmode < DLM_LOCK_PW)
1398 return;
1399
1400 if (lkb->lkb_exflags & DLM_LKF_IVVALBLK) {
1401 rsb_set_flag(r, RSB_VALNOTVALID);
1402 return;
1403 }
1404
1405 if (!lkb->lkb_lvbptr)
1406 return;
1407
1408 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1409 return;
1410
1411 if (!r->res_lvbptr)
David Teigland52bda2b2007-11-07 09:06:49 -06001412 r->res_lvbptr = dlm_allocate_lvb(r->res_ls);
David Teiglande7fd4172006-01-18 09:30:29 +00001413
1414 if (!r->res_lvbptr)
1415 return;
1416
1417 memcpy(r->res_lvbptr, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
1418 r->res_lvbseq++;
1419 rsb_clear_flag(r, RSB_VALNOTVALID);
1420}
1421
1422/* lkb is process copy (pc) */
1423
1424static void set_lvb_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb,
1425 struct dlm_message *ms)
1426{
1427 int b;
1428
1429 if (!lkb->lkb_lvbptr)
1430 return;
1431
1432 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1433 return;
1434
David Teigland597d0ca2006-07-12 16:44:04 -05001435 b = dlm_lvb_operations[lkb->lkb_grmode + 1][lkb->lkb_rqmode + 1];
David Teiglande7fd4172006-01-18 09:30:29 +00001436 if (b == 1) {
1437 int len = receive_extralen(ms);
Al Viroa9cc9152008-01-26 00:02:29 -05001438 if (len > DLM_RESNAME_MAXLEN)
1439 len = DLM_RESNAME_MAXLEN;
David Teiglande7fd4172006-01-18 09:30:29 +00001440 memcpy(lkb->lkb_lvbptr, ms->m_extra, len);
1441 lkb->lkb_lvbseq = ms->m_lvbseq;
1442 }
1443}
1444
1445/* Manipulate lkb's on rsb's convert/granted/waiting queues
1446 remove_lock -- used for unlock, removes lkb from granted
1447 revert_lock -- used for cancel, moves lkb from convert to granted
1448 grant_lock -- used for request and convert, adds lkb to granted or
1449 moves lkb from convert or waiting to granted
1450
1451 Each of these is used for master or local copy lkb's. There is
1452 also a _pc() variation used to make the corresponding change on
1453 a process copy (pc) lkb. */
1454
1455static void _remove_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1456{
1457 del_lkb(r, lkb);
1458 lkb->lkb_grmode = DLM_LOCK_IV;
1459 /* this unhold undoes the original ref from create_lkb()
1460 so this leads to the lkb being freed */
1461 unhold_lkb(lkb);
1462}
1463
1464static void remove_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1465{
1466 set_lvb_unlock(r, lkb);
1467 _remove_lock(r, lkb);
1468}
1469
1470static void remove_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb)
1471{
1472 _remove_lock(r, lkb);
1473}
1474
David Teiglandef0c2bb2007-03-28 09:56:46 -05001475/* returns: 0 did nothing
1476 1 moved lock to granted
1477 -1 removed lock */
1478
1479static int revert_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
David Teiglande7fd4172006-01-18 09:30:29 +00001480{
David Teiglandef0c2bb2007-03-28 09:56:46 -05001481 int rv = 0;
1482
David Teiglande7fd4172006-01-18 09:30:29 +00001483 lkb->lkb_rqmode = DLM_LOCK_IV;
1484
1485 switch (lkb->lkb_status) {
David Teigland597d0ca2006-07-12 16:44:04 -05001486 case DLM_LKSTS_GRANTED:
1487 break;
David Teiglande7fd4172006-01-18 09:30:29 +00001488 case DLM_LKSTS_CONVERT:
1489 move_lkb(r, lkb, DLM_LKSTS_GRANTED);
David Teiglandef0c2bb2007-03-28 09:56:46 -05001490 rv = 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001491 break;
1492 case DLM_LKSTS_WAITING:
1493 del_lkb(r, lkb);
1494 lkb->lkb_grmode = DLM_LOCK_IV;
1495 /* this unhold undoes the original ref from create_lkb()
1496 so this leads to the lkb being freed */
1497 unhold_lkb(lkb);
David Teiglandef0c2bb2007-03-28 09:56:46 -05001498 rv = -1;
David Teiglande7fd4172006-01-18 09:30:29 +00001499 break;
1500 default:
1501 log_print("invalid status for revert %d", lkb->lkb_status);
1502 }
David Teiglandef0c2bb2007-03-28 09:56:46 -05001503 return rv;
David Teiglande7fd4172006-01-18 09:30:29 +00001504}
1505
David Teiglandef0c2bb2007-03-28 09:56:46 -05001506static int revert_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb)
David Teiglande7fd4172006-01-18 09:30:29 +00001507{
David Teiglandef0c2bb2007-03-28 09:56:46 -05001508 return revert_lock(r, lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00001509}
1510
1511static void _grant_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1512{
1513 if (lkb->lkb_grmode != lkb->lkb_rqmode) {
1514 lkb->lkb_grmode = lkb->lkb_rqmode;
1515 if (lkb->lkb_status)
1516 move_lkb(r, lkb, DLM_LKSTS_GRANTED);
1517 else
1518 add_lkb(r, lkb, DLM_LKSTS_GRANTED);
1519 }
1520
1521 lkb->lkb_rqmode = DLM_LOCK_IV;
David Teiglande7fd4172006-01-18 09:30:29 +00001522}
1523
1524static void grant_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1525{
1526 set_lvb_lock(r, lkb);
1527 _grant_lock(r, lkb);
1528 lkb->lkb_highbast = 0;
1529}
1530
1531static void grant_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb,
1532 struct dlm_message *ms)
1533{
1534 set_lvb_lock_pc(r, lkb, ms);
1535 _grant_lock(r, lkb);
1536}
1537
1538/* called by grant_pending_locks() which means an async grant message must
1539 be sent to the requesting node in addition to granting the lock if the
1540 lkb belongs to a remote node. */
1541
1542static void grant_lock_pending(struct dlm_rsb *r, struct dlm_lkb *lkb)
1543{
1544 grant_lock(r, lkb);
1545 if (is_master_copy(lkb))
1546 send_grant(r, lkb);
1547 else
1548 queue_cast(r, lkb, 0);
1549}
1550
David Teigland7d3c1fe2007-04-19 10:30:41 -05001551/* The special CONVDEADLK, ALTPR and ALTCW flags allow the master to
1552 change the granted/requested modes. We're munging things accordingly in
1553 the process copy.
1554 CONVDEADLK: our grmode may have been forced down to NL to resolve a
1555 conversion deadlock
1556 ALTPR/ALTCW: our rqmode may have been changed to PR or CW to become
1557 compatible with other granted locks */
1558
David Teigland2a7ce0e2011-04-04 15:19:59 -05001559static void munge_demoted(struct dlm_lkb *lkb)
David Teigland7d3c1fe2007-04-19 10:30:41 -05001560{
David Teigland7d3c1fe2007-04-19 10:30:41 -05001561 if (lkb->lkb_rqmode == DLM_LOCK_IV || lkb->lkb_grmode == DLM_LOCK_IV) {
1562 log_print("munge_demoted %x invalid modes gr %d rq %d",
1563 lkb->lkb_id, lkb->lkb_grmode, lkb->lkb_rqmode);
1564 return;
1565 }
1566
1567 lkb->lkb_grmode = DLM_LOCK_NL;
1568}
1569
1570static void munge_altmode(struct dlm_lkb *lkb, struct dlm_message *ms)
1571{
1572 if (ms->m_type != DLM_MSG_REQUEST_REPLY &&
1573 ms->m_type != DLM_MSG_GRANT) {
1574 log_print("munge_altmode %x invalid reply type %d",
1575 lkb->lkb_id, ms->m_type);
1576 return;
1577 }
1578
1579 if (lkb->lkb_exflags & DLM_LKF_ALTPR)
1580 lkb->lkb_rqmode = DLM_LOCK_PR;
1581 else if (lkb->lkb_exflags & DLM_LKF_ALTCW)
1582 lkb->lkb_rqmode = DLM_LOCK_CW;
1583 else {
1584 log_print("munge_altmode invalid exflags %x", lkb->lkb_exflags);
1585 dlm_print_lkb(lkb);
1586 }
1587}
1588
David Teiglande7fd4172006-01-18 09:30:29 +00001589static inline int first_in_list(struct dlm_lkb *lkb, struct list_head *head)
1590{
1591 struct dlm_lkb *first = list_entry(head->next, struct dlm_lkb,
1592 lkb_statequeue);
1593 if (lkb->lkb_id == first->lkb_id)
David Teigland90135922006-01-20 08:47:07 +00001594 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001595
David Teigland90135922006-01-20 08:47:07 +00001596 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001597}
1598
David Teiglande7fd4172006-01-18 09:30:29 +00001599/* Check if the given lkb conflicts with another lkb on the queue. */
1600
1601static int queue_conflict(struct list_head *head, struct dlm_lkb *lkb)
1602{
1603 struct dlm_lkb *this;
1604
1605 list_for_each_entry(this, head, lkb_statequeue) {
1606 if (this == lkb)
1607 continue;
David Teigland3bcd3682006-02-23 09:56:38 +00001608 if (!modes_compat(this, lkb))
David Teigland90135922006-01-20 08:47:07 +00001609 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001610 }
David Teigland90135922006-01-20 08:47:07 +00001611 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001612}
1613
1614/*
1615 * "A conversion deadlock arises with a pair of lock requests in the converting
1616 * queue for one resource. The granted mode of each lock blocks the requested
1617 * mode of the other lock."
1618 *
David Teiglandc85d65e2007-05-18 09:01:26 -05001619 * Part 2: if the granted mode of lkb is preventing an earlier lkb in the
1620 * convert queue from being granted, then deadlk/demote lkb.
David Teiglande7fd4172006-01-18 09:30:29 +00001621 *
1622 * Example:
1623 * Granted Queue: empty
1624 * Convert Queue: NL->EX (first lock)
1625 * PR->EX (second lock)
1626 *
1627 * The first lock can't be granted because of the granted mode of the second
1628 * lock and the second lock can't be granted because it's not first in the
David Teiglandc85d65e2007-05-18 09:01:26 -05001629 * list. We either cancel lkb's conversion (PR->EX) and return EDEADLK, or we
1630 * demote the granted mode of lkb (from PR to NL) if it has the CONVDEADLK
1631 * flag set and return DEMOTED in the lksb flags.
David Teiglande7fd4172006-01-18 09:30:29 +00001632 *
David Teiglandc85d65e2007-05-18 09:01:26 -05001633 * Originally, this function detected conv-deadlk in a more limited scope:
1634 * - if !modes_compat(lkb1, lkb2) && !modes_compat(lkb2, lkb1), or
1635 * - if lkb1 was the first entry in the queue (not just earlier), and was
1636 * blocked by the granted mode of lkb2, and there was nothing on the
1637 * granted queue preventing lkb1 from being granted immediately, i.e.
1638 * lkb2 was the only thing preventing lkb1 from being granted.
1639 *
1640 * That second condition meant we'd only say there was conv-deadlk if
1641 * resolving it (by demotion) would lead to the first lock on the convert
1642 * queue being granted right away. It allowed conversion deadlocks to exist
1643 * between locks on the convert queue while they couldn't be granted anyway.
1644 *
1645 * Now, we detect and take action on conversion deadlocks immediately when
1646 * they're created, even if they may not be immediately consequential. If
1647 * lkb1 exists anywhere in the convert queue and lkb2 comes in with a granted
1648 * mode that would prevent lkb1's conversion from being granted, we do a
1649 * deadlk/demote on lkb2 right away and don't let it onto the convert queue.
1650 * I think this means that the lkb_is_ahead condition below should always
1651 * be zero, i.e. there will never be conv-deadlk between two locks that are
1652 * both already on the convert queue.
David Teiglande7fd4172006-01-18 09:30:29 +00001653 */
1654
David Teiglandc85d65e2007-05-18 09:01:26 -05001655static int conversion_deadlock_detect(struct dlm_rsb *r, struct dlm_lkb *lkb2)
David Teiglande7fd4172006-01-18 09:30:29 +00001656{
David Teiglandc85d65e2007-05-18 09:01:26 -05001657 struct dlm_lkb *lkb1;
1658 int lkb_is_ahead = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001659
David Teiglandc85d65e2007-05-18 09:01:26 -05001660 list_for_each_entry(lkb1, &r->res_convertqueue, lkb_statequeue) {
1661 if (lkb1 == lkb2) {
1662 lkb_is_ahead = 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001663 continue;
1664 }
1665
David Teiglandc85d65e2007-05-18 09:01:26 -05001666 if (!lkb_is_ahead) {
1667 if (!modes_compat(lkb2, lkb1))
1668 return 1;
1669 } else {
1670 if (!modes_compat(lkb2, lkb1) &&
1671 !modes_compat(lkb1, lkb2))
1672 return 1;
1673 }
David Teiglande7fd4172006-01-18 09:30:29 +00001674 }
David Teigland90135922006-01-20 08:47:07 +00001675 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001676}
1677
1678/*
1679 * Return 1 if the lock can be granted, 0 otherwise.
1680 * Also detect and resolve conversion deadlocks.
1681 *
1682 * lkb is the lock to be granted
1683 *
1684 * now is 1 if the function is being called in the context of the
1685 * immediate request, it is 0 if called later, after the lock has been
1686 * queued.
1687 *
1688 * References are from chapter 6 of "VAXcluster Principles" by Roy Davis
1689 */
1690
1691static int _can_be_granted(struct dlm_rsb *r, struct dlm_lkb *lkb, int now)
1692{
1693 int8_t conv = (lkb->lkb_grmode != DLM_LOCK_IV);
1694
1695 /*
1696 * 6-10: Version 5.4 introduced an option to address the phenomenon of
1697 * a new request for a NL mode lock being blocked.
1698 *
1699 * 6-11: If the optional EXPEDITE flag is used with the new NL mode
1700 * request, then it would be granted. In essence, the use of this flag
1701 * tells the Lock Manager to expedite theis request by not considering
1702 * what may be in the CONVERTING or WAITING queues... As of this
1703 * writing, the EXPEDITE flag can be used only with new requests for NL
1704 * mode locks. This flag is not valid for conversion requests.
1705 *
1706 * A shortcut. Earlier checks return an error if EXPEDITE is used in a
1707 * conversion or used with a non-NL requested mode. We also know an
1708 * EXPEDITE request is always granted immediately, so now must always
1709 * be 1. The full condition to grant an expedite request: (now &&
1710 * !conv && lkb->rqmode == DLM_LOCK_NL && (flags & EXPEDITE)) can
1711 * therefore be shortened to just checking the flag.
1712 */
1713
1714 if (lkb->lkb_exflags & DLM_LKF_EXPEDITE)
David Teigland90135922006-01-20 08:47:07 +00001715 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001716
1717 /*
1718 * A shortcut. Without this, !queue_conflict(grantqueue, lkb) would be
1719 * added to the remaining conditions.
1720 */
1721
1722 if (queue_conflict(&r->res_grantqueue, lkb))
1723 goto out;
1724
1725 /*
1726 * 6-3: By default, a conversion request is immediately granted if the
1727 * requested mode is compatible with the modes of all other granted
1728 * locks
1729 */
1730
1731 if (queue_conflict(&r->res_convertqueue, lkb))
1732 goto out;
1733
1734 /*
1735 * 6-5: But the default algorithm for deciding whether to grant or
1736 * queue conversion requests does not by itself guarantee that such
1737 * requests are serviced on a "first come first serve" basis. This, in
1738 * turn, can lead to a phenomenon known as "indefinate postponement".
1739 *
1740 * 6-7: This issue is dealt with by using the optional QUECVT flag with
1741 * the system service employed to request a lock conversion. This flag
1742 * forces certain conversion requests to be queued, even if they are
1743 * compatible with the granted modes of other locks on the same
1744 * resource. Thus, the use of this flag results in conversion requests
1745 * being ordered on a "first come first servce" basis.
1746 *
1747 * DCT: This condition is all about new conversions being able to occur
1748 * "in place" while the lock remains on the granted queue (assuming
1749 * nothing else conflicts.) IOW if QUECVT isn't set, a conversion
1750 * doesn't _have_ to go onto the convert queue where it's processed in
1751 * order. The "now" variable is necessary to distinguish converts
1752 * being received and processed for the first time now, because once a
1753 * convert is moved to the conversion queue the condition below applies
1754 * requiring fifo granting.
1755 */
1756
1757 if (now && conv && !(lkb->lkb_exflags & DLM_LKF_QUECVT))
David Teigland90135922006-01-20 08:47:07 +00001758 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001759
1760 /*
David Teigland53ad1c92012-04-04 09:49:15 -05001761 * Even if the convert is compat with all granted locks,
1762 * QUECVT forces it behind other locks on the convert queue.
1763 */
1764
1765 if (now && conv && (lkb->lkb_exflags & DLM_LKF_QUECVT)) {
1766 if (list_empty(&r->res_convertqueue))
1767 return 1;
1768 else
1769 goto out;
1770 }
1771
1772 /*
David Teigland3bcd3682006-02-23 09:56:38 +00001773 * The NOORDER flag is set to avoid the standard vms rules on grant
1774 * order.
David Teiglande7fd4172006-01-18 09:30:29 +00001775 */
1776
1777 if (lkb->lkb_exflags & DLM_LKF_NOORDER)
David Teigland90135922006-01-20 08:47:07 +00001778 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001779
1780 /*
1781 * 6-3: Once in that queue [CONVERTING], a conversion request cannot be
1782 * granted until all other conversion requests ahead of it are granted
1783 * and/or canceled.
1784 */
1785
1786 if (!now && conv && first_in_list(lkb, &r->res_convertqueue))
David Teigland90135922006-01-20 08:47:07 +00001787 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001788
1789 /*
1790 * 6-4: By default, a new request is immediately granted only if all
1791 * three of the following conditions are satisfied when the request is
1792 * issued:
1793 * - The queue of ungranted conversion requests for the resource is
1794 * empty.
1795 * - The queue of ungranted new requests for the resource is empty.
1796 * - The mode of the new request is compatible with the most
1797 * restrictive mode of all granted locks on the resource.
1798 */
1799
1800 if (now && !conv && list_empty(&r->res_convertqueue) &&
1801 list_empty(&r->res_waitqueue))
David Teigland90135922006-01-20 08:47:07 +00001802 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001803
1804 /*
1805 * 6-4: Once a lock request is in the queue of ungranted new requests,
1806 * it cannot be granted until the queue of ungranted conversion
1807 * requests is empty, all ungranted new requests ahead of it are
1808 * granted and/or canceled, and it is compatible with the granted mode
1809 * of the most restrictive lock granted on the resource.
1810 */
1811
1812 if (!now && !conv && list_empty(&r->res_convertqueue) &&
1813 first_in_list(lkb, &r->res_waitqueue))
David Teigland90135922006-01-20 08:47:07 +00001814 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001815 out:
David Teigland90135922006-01-20 08:47:07 +00001816 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001817}
1818
David Teiglandc85d65e2007-05-18 09:01:26 -05001819static int can_be_granted(struct dlm_rsb *r, struct dlm_lkb *lkb, int now,
1820 int *err)
David Teiglande7fd4172006-01-18 09:30:29 +00001821{
David Teiglande7fd4172006-01-18 09:30:29 +00001822 int rv;
1823 int8_t alt = 0, rqmode = lkb->lkb_rqmode;
David Teiglandc85d65e2007-05-18 09:01:26 -05001824 int8_t is_convert = (lkb->lkb_grmode != DLM_LOCK_IV);
1825
1826 if (err)
1827 *err = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001828
1829 rv = _can_be_granted(r, lkb, now);
1830 if (rv)
1831 goto out;
1832
David Teiglandc85d65e2007-05-18 09:01:26 -05001833 /*
1834 * The CONVDEADLK flag is non-standard and tells the dlm to resolve
1835 * conversion deadlocks by demoting grmode to NL, otherwise the dlm
1836 * cancels one of the locks.
1837 */
David Teiglande7fd4172006-01-18 09:30:29 +00001838
David Teiglandc85d65e2007-05-18 09:01:26 -05001839 if (is_convert && can_be_queued(lkb) &&
1840 conversion_deadlock_detect(r, lkb)) {
1841 if (lkb->lkb_exflags & DLM_LKF_CONVDEADLK) {
1842 lkb->lkb_grmode = DLM_LOCK_NL;
1843 lkb->lkb_sbflags |= DLM_SBF_DEMOTED;
1844 } else if (!(lkb->lkb_exflags & DLM_LKF_NODLCKWT)) {
1845 if (err)
1846 *err = -EDEADLK;
1847 else {
1848 log_print("can_be_granted deadlock %x now %d",
1849 lkb->lkb_id, now);
1850 dlm_dump_rsb(r);
1851 }
1852 }
1853 goto out;
1854 }
1855
1856 /*
1857 * The ALTPR and ALTCW flags are non-standard and tell the dlm to try
1858 * to grant a request in a mode other than the normal rqmode. It's a
1859 * simple way to provide a big optimization to applications that can
1860 * use them.
1861 */
1862
1863 if (rqmode != DLM_LOCK_PR && (lkb->lkb_exflags & DLM_LKF_ALTPR))
David Teiglande7fd4172006-01-18 09:30:29 +00001864 alt = DLM_LOCK_PR;
David Teiglandc85d65e2007-05-18 09:01:26 -05001865 else if (rqmode != DLM_LOCK_CW && (lkb->lkb_exflags & DLM_LKF_ALTCW))
David Teiglande7fd4172006-01-18 09:30:29 +00001866 alt = DLM_LOCK_CW;
1867
1868 if (alt) {
1869 lkb->lkb_rqmode = alt;
1870 rv = _can_be_granted(r, lkb, now);
1871 if (rv)
1872 lkb->lkb_sbflags |= DLM_SBF_ALTMODE;
1873 else
1874 lkb->lkb_rqmode = rqmode;
1875 }
1876 out:
1877 return rv;
1878}
1879
David Teiglandc85d65e2007-05-18 09:01:26 -05001880/* FIXME: I don't think that can_be_granted() can/will demote or find deadlock
1881 for locks pending on the convert list. Once verified (watch for these
1882 log_prints), we should be able to just call _can_be_granted() and not
1883 bother with the demote/deadlk cases here (and there's no easy way to deal
1884 with a deadlk here, we'd have to generate something like grant_lock with
1885 the deadlk error.) */
1886
David Teigland36509252007-08-07 09:44:48 -05001887/* Returns the highest requested mode of all blocked conversions; sets
1888 cw if there's a blocked conversion to DLM_LOCK_CW. */
David Teiglandc85d65e2007-05-18 09:01:26 -05001889
David Teigland36509252007-08-07 09:44:48 -05001890static int grant_pending_convert(struct dlm_rsb *r, int high, int *cw)
David Teiglande7fd4172006-01-18 09:30:29 +00001891{
1892 struct dlm_lkb *lkb, *s;
1893 int hi, demoted, quit, grant_restart, demote_restart;
David Teiglandc85d65e2007-05-18 09:01:26 -05001894 int deadlk;
David Teiglande7fd4172006-01-18 09:30:29 +00001895
1896 quit = 0;
1897 restart:
1898 grant_restart = 0;
1899 demote_restart = 0;
1900 hi = DLM_LOCK_IV;
1901
1902 list_for_each_entry_safe(lkb, s, &r->res_convertqueue, lkb_statequeue) {
1903 demoted = is_demoted(lkb);
David Teiglandc85d65e2007-05-18 09:01:26 -05001904 deadlk = 0;
1905
1906 if (can_be_granted(r, lkb, 0, &deadlk)) {
David Teiglande7fd4172006-01-18 09:30:29 +00001907 grant_lock_pending(r, lkb);
1908 grant_restart = 1;
David Teiglandc85d65e2007-05-18 09:01:26 -05001909 continue;
David Teiglande7fd4172006-01-18 09:30:29 +00001910 }
David Teiglandc85d65e2007-05-18 09:01:26 -05001911
1912 if (!demoted && is_demoted(lkb)) {
1913 log_print("WARN: pending demoted %x node %d %s",
1914 lkb->lkb_id, lkb->lkb_nodeid, r->res_name);
1915 demote_restart = 1;
1916 continue;
1917 }
1918
1919 if (deadlk) {
1920 log_print("WARN: pending deadlock %x node %d %s",
1921 lkb->lkb_id, lkb->lkb_nodeid, r->res_name);
1922 dlm_dump_rsb(r);
1923 continue;
1924 }
1925
1926 hi = max_t(int, lkb->lkb_rqmode, hi);
David Teigland36509252007-08-07 09:44:48 -05001927
1928 if (cw && lkb->lkb_rqmode == DLM_LOCK_CW)
1929 *cw = 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001930 }
1931
1932 if (grant_restart)
1933 goto restart;
1934 if (demote_restart && !quit) {
1935 quit = 1;
1936 goto restart;
1937 }
1938
1939 return max_t(int, high, hi);
1940}
1941
David Teigland36509252007-08-07 09:44:48 -05001942static int grant_pending_wait(struct dlm_rsb *r, int high, int *cw)
David Teiglande7fd4172006-01-18 09:30:29 +00001943{
1944 struct dlm_lkb *lkb, *s;
1945
1946 list_for_each_entry_safe(lkb, s, &r->res_waitqueue, lkb_statequeue) {
David Teiglandc85d65e2007-05-18 09:01:26 -05001947 if (can_be_granted(r, lkb, 0, NULL))
David Teiglande7fd4172006-01-18 09:30:29 +00001948 grant_lock_pending(r, lkb);
David Teigland36509252007-08-07 09:44:48 -05001949 else {
David Teiglande7fd4172006-01-18 09:30:29 +00001950 high = max_t(int, lkb->lkb_rqmode, high);
David Teigland36509252007-08-07 09:44:48 -05001951 if (lkb->lkb_rqmode == DLM_LOCK_CW)
1952 *cw = 1;
1953 }
David Teiglande7fd4172006-01-18 09:30:29 +00001954 }
1955
1956 return high;
1957}
1958
David Teigland36509252007-08-07 09:44:48 -05001959/* cw of 1 means there's a lock with a rqmode of DLM_LOCK_CW that's blocked
1960 on either the convert or waiting queue.
1961 high is the largest rqmode of all locks blocked on the convert or
1962 waiting queue. */
1963
1964static int lock_requires_bast(struct dlm_lkb *gr, int high, int cw)
1965{
1966 if (gr->lkb_grmode == DLM_LOCK_PR && cw) {
1967 if (gr->lkb_highbast < DLM_LOCK_EX)
1968 return 1;
1969 return 0;
1970 }
1971
1972 if (gr->lkb_highbast < high &&
1973 !__dlm_compat_matrix[gr->lkb_grmode+1][high+1])
1974 return 1;
1975 return 0;
1976}
1977
David Teiglande7fd4172006-01-18 09:30:29 +00001978static void grant_pending_locks(struct dlm_rsb *r)
1979{
1980 struct dlm_lkb *lkb, *s;
1981 int high = DLM_LOCK_IV;
David Teigland36509252007-08-07 09:44:48 -05001982 int cw = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001983
David Teiglanda345da32006-08-18 11:54:25 -05001984 DLM_ASSERT(is_master(r), dlm_dump_rsb(r););
David Teiglande7fd4172006-01-18 09:30:29 +00001985
David Teigland36509252007-08-07 09:44:48 -05001986 high = grant_pending_convert(r, high, &cw);
1987 high = grant_pending_wait(r, high, &cw);
David Teiglande7fd4172006-01-18 09:30:29 +00001988
1989 if (high == DLM_LOCK_IV)
1990 return;
1991
1992 /*
1993 * If there are locks left on the wait/convert queue then send blocking
1994 * ASTs to granted locks based on the largest requested mode (high)
David Teigland36509252007-08-07 09:44:48 -05001995 * found above.
David Teiglande7fd4172006-01-18 09:30:29 +00001996 */
1997
1998 list_for_each_entry_safe(lkb, s, &r->res_grantqueue, lkb_statequeue) {
David Teiglande5dae542008-02-06 00:35:45 -06001999 if (lkb->lkb_bastfn && lock_requires_bast(lkb, high, cw)) {
David Teigland329fc4c2008-05-20 12:18:10 -05002000 if (cw && high == DLM_LOCK_PR &&
2001 lkb->lkb_grmode == DLM_LOCK_PR)
David Teigland36509252007-08-07 09:44:48 -05002002 queue_bast(r, lkb, DLM_LOCK_CW);
2003 else
2004 queue_bast(r, lkb, high);
David Teiglande7fd4172006-01-18 09:30:29 +00002005 lkb->lkb_highbast = high;
2006 }
2007 }
2008}
2009
David Teigland36509252007-08-07 09:44:48 -05002010static int modes_require_bast(struct dlm_lkb *gr, struct dlm_lkb *rq)
2011{
2012 if ((gr->lkb_grmode == DLM_LOCK_PR && rq->lkb_rqmode == DLM_LOCK_CW) ||
2013 (gr->lkb_grmode == DLM_LOCK_CW && rq->lkb_rqmode == DLM_LOCK_PR)) {
2014 if (gr->lkb_highbast < DLM_LOCK_EX)
2015 return 1;
2016 return 0;
2017 }
2018
2019 if (gr->lkb_highbast < rq->lkb_rqmode && !modes_compat(gr, rq))
2020 return 1;
2021 return 0;
2022}
2023
David Teiglande7fd4172006-01-18 09:30:29 +00002024static void send_bast_queue(struct dlm_rsb *r, struct list_head *head,
2025 struct dlm_lkb *lkb)
2026{
2027 struct dlm_lkb *gr;
2028
2029 list_for_each_entry(gr, head, lkb_statequeue) {
Steven Whitehouse314dd2a2010-09-03 10:07:48 -05002030 /* skip self when sending basts to convertqueue */
2031 if (gr == lkb)
2032 continue;
David Teiglande5dae542008-02-06 00:35:45 -06002033 if (gr->lkb_bastfn && modes_require_bast(gr, lkb)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002034 queue_bast(r, gr, lkb->lkb_rqmode);
2035 gr->lkb_highbast = lkb->lkb_rqmode;
2036 }
2037 }
2038}
2039
2040static void send_blocking_asts(struct dlm_rsb *r, struct dlm_lkb *lkb)
2041{
2042 send_bast_queue(r, &r->res_grantqueue, lkb);
2043}
2044
2045static void send_blocking_asts_all(struct dlm_rsb *r, struct dlm_lkb *lkb)
2046{
2047 send_bast_queue(r, &r->res_grantqueue, lkb);
2048 send_bast_queue(r, &r->res_convertqueue, lkb);
2049}
2050
2051/* set_master(r, lkb) -- set the master nodeid of a resource
2052
2053 The purpose of this function is to set the nodeid field in the given
2054 lkb using the nodeid field in the given rsb. If the rsb's nodeid is
2055 known, it can just be copied to the lkb and the function will return
2056 0. If the rsb's nodeid is _not_ known, it needs to be looked up
2057 before it can be copied to the lkb.
2058
2059 When the rsb nodeid is being looked up remotely, the initial lkb
2060 causing the lookup is kept on the ls_waiters list waiting for the
2061 lookup reply. Other lkb's waiting for the same rsb lookup are kept
2062 on the rsb's res_lookup list until the master is verified.
2063
2064 Return values:
2065 0: nodeid is set in rsb/lkb and the caller should go ahead and use it
2066 1: the rsb master is not available and the lkb has been placed on
2067 a wait queue
2068*/
2069
2070static int set_master(struct dlm_rsb *r, struct dlm_lkb *lkb)
2071{
2072 struct dlm_ls *ls = r->res_ls;
David Teigland755b5eb2008-01-09 10:37:39 -06002073 int i, error, dir_nodeid, ret_nodeid, our_nodeid = dlm_our_nodeid();
David Teiglande7fd4172006-01-18 09:30:29 +00002074
2075 if (rsb_flag(r, RSB_MASTER_UNCERTAIN)) {
2076 rsb_clear_flag(r, RSB_MASTER_UNCERTAIN);
2077 r->res_first_lkid = lkb->lkb_id;
2078 lkb->lkb_nodeid = r->res_nodeid;
2079 return 0;
2080 }
2081
2082 if (r->res_first_lkid && r->res_first_lkid != lkb->lkb_id) {
2083 list_add_tail(&lkb->lkb_rsb_lookup, &r->res_lookup);
2084 return 1;
2085 }
2086
2087 if (r->res_nodeid == 0) {
2088 lkb->lkb_nodeid = 0;
2089 return 0;
2090 }
2091
2092 if (r->res_nodeid > 0) {
2093 lkb->lkb_nodeid = r->res_nodeid;
2094 return 0;
2095 }
2096
David Teiglanda345da32006-08-18 11:54:25 -05002097 DLM_ASSERT(r->res_nodeid == -1, dlm_dump_rsb(r););
David Teiglande7fd4172006-01-18 09:30:29 +00002098
2099 dir_nodeid = dlm_dir_nodeid(r);
2100
2101 if (dir_nodeid != our_nodeid) {
2102 r->res_first_lkid = lkb->lkb_id;
2103 send_lookup(r, lkb);
2104 return 1;
2105 }
2106
David Teigland755b5eb2008-01-09 10:37:39 -06002107 for (i = 0; i < 2; i++) {
David Teiglande7fd4172006-01-18 09:30:29 +00002108 /* It's possible for dlm_scand to remove an old rsb for
2109 this same resource from the toss list, us to create
2110 a new one, look up the master locally, and find it
2111 already exists just before dlm_scand does the
2112 dir_remove() on the previous rsb. */
2113
2114 error = dlm_dir_lookup(ls, our_nodeid, r->res_name,
2115 r->res_length, &ret_nodeid);
2116 if (!error)
2117 break;
2118 log_debug(ls, "dir_lookup error %d %s", error, r->res_name);
2119 schedule();
2120 }
David Teigland755b5eb2008-01-09 10:37:39 -06002121 if (error && error != -EEXIST)
2122 return error;
David Teiglande7fd4172006-01-18 09:30:29 +00002123
2124 if (ret_nodeid == our_nodeid) {
2125 r->res_first_lkid = 0;
2126 r->res_nodeid = 0;
2127 lkb->lkb_nodeid = 0;
2128 } else {
2129 r->res_first_lkid = lkb->lkb_id;
2130 r->res_nodeid = ret_nodeid;
2131 lkb->lkb_nodeid = ret_nodeid;
2132 }
2133 return 0;
2134}
2135
2136static void process_lookup_list(struct dlm_rsb *r)
2137{
2138 struct dlm_lkb *lkb, *safe;
2139
2140 list_for_each_entry_safe(lkb, safe, &r->res_lookup, lkb_rsb_lookup) {
David Teiglandef0c2bb2007-03-28 09:56:46 -05002141 list_del_init(&lkb->lkb_rsb_lookup);
David Teiglande7fd4172006-01-18 09:30:29 +00002142 _request_lock(r, lkb);
2143 schedule();
2144 }
2145}
2146
2147/* confirm_master -- confirm (or deny) an rsb's master nodeid */
2148
2149static void confirm_master(struct dlm_rsb *r, int error)
2150{
2151 struct dlm_lkb *lkb;
2152
2153 if (!r->res_first_lkid)
2154 return;
2155
2156 switch (error) {
2157 case 0:
2158 case -EINPROGRESS:
2159 r->res_first_lkid = 0;
2160 process_lookup_list(r);
2161 break;
2162
2163 case -EAGAIN:
David Teiglandaec64e12008-01-08 15:37:47 -06002164 case -EBADR:
2165 case -ENOTBLK:
2166 /* the remote request failed and won't be retried (it was
2167 a NOQUEUE, or has been canceled/unlocked); make a waiting
2168 lkb the first_lkid */
David Teiglande7fd4172006-01-18 09:30:29 +00002169
2170 r->res_first_lkid = 0;
2171
2172 if (!list_empty(&r->res_lookup)) {
2173 lkb = list_entry(r->res_lookup.next, struct dlm_lkb,
2174 lkb_rsb_lookup);
David Teiglandef0c2bb2007-03-28 09:56:46 -05002175 list_del_init(&lkb->lkb_rsb_lookup);
David Teiglande7fd4172006-01-18 09:30:29 +00002176 r->res_first_lkid = lkb->lkb_id;
2177 _request_lock(r, lkb);
David Teigland761b9d32008-02-21 11:25:42 -06002178 }
David Teiglande7fd4172006-01-18 09:30:29 +00002179 break;
2180
2181 default:
2182 log_error(r->res_ls, "confirm_master unknown error %d", error);
2183 }
2184}
2185
2186static int set_lock_args(int mode, struct dlm_lksb *lksb, uint32_t flags,
David Teiglande5dae542008-02-06 00:35:45 -06002187 int namelen, unsigned long timeout_cs,
2188 void (*ast) (void *astparam),
2189 void *astparam,
2190 void (*bast) (void *astparam, int mode),
2191 struct dlm_args *args)
David Teiglande7fd4172006-01-18 09:30:29 +00002192{
2193 int rv = -EINVAL;
2194
2195 /* check for invalid arg usage */
2196
2197 if (mode < 0 || mode > DLM_LOCK_EX)
2198 goto out;
2199
2200 if (!(flags & DLM_LKF_CONVERT) && (namelen > DLM_RESNAME_MAXLEN))
2201 goto out;
2202
2203 if (flags & DLM_LKF_CANCEL)
2204 goto out;
2205
2206 if (flags & DLM_LKF_QUECVT && !(flags & DLM_LKF_CONVERT))
2207 goto out;
2208
2209 if (flags & DLM_LKF_CONVDEADLK && !(flags & DLM_LKF_CONVERT))
2210 goto out;
2211
2212 if (flags & DLM_LKF_CONVDEADLK && flags & DLM_LKF_NOQUEUE)
2213 goto out;
2214
2215 if (flags & DLM_LKF_EXPEDITE && flags & DLM_LKF_CONVERT)
2216 goto out;
2217
2218 if (flags & DLM_LKF_EXPEDITE && flags & DLM_LKF_QUECVT)
2219 goto out;
2220
2221 if (flags & DLM_LKF_EXPEDITE && flags & DLM_LKF_NOQUEUE)
2222 goto out;
2223
2224 if (flags & DLM_LKF_EXPEDITE && mode != DLM_LOCK_NL)
2225 goto out;
2226
2227 if (!ast || !lksb)
2228 goto out;
2229
2230 if (flags & DLM_LKF_VALBLK && !lksb->sb_lvbptr)
2231 goto out;
2232
David Teiglande7fd4172006-01-18 09:30:29 +00002233 if (flags & DLM_LKF_CONVERT && !lksb->sb_lkid)
2234 goto out;
2235
2236 /* these args will be copied to the lkb in validate_lock_args,
2237 it cannot be done now because when converting locks, fields in
2238 an active lkb cannot be modified before locking the rsb */
2239
2240 args->flags = flags;
David Teiglande5dae542008-02-06 00:35:45 -06002241 args->astfn = ast;
2242 args->astparam = astparam;
2243 args->bastfn = bast;
David Teiglandd7db9232007-05-18 09:00:32 -05002244 args->timeout = timeout_cs;
David Teiglande7fd4172006-01-18 09:30:29 +00002245 args->mode = mode;
2246 args->lksb = lksb;
David Teiglande7fd4172006-01-18 09:30:29 +00002247 rv = 0;
2248 out:
2249 return rv;
2250}
2251
2252static int set_unlock_args(uint32_t flags, void *astarg, struct dlm_args *args)
2253{
2254 if (flags & ~(DLM_LKF_CANCEL | DLM_LKF_VALBLK | DLM_LKF_IVVALBLK |
2255 DLM_LKF_FORCEUNLOCK))
2256 return -EINVAL;
2257
David Teiglandef0c2bb2007-03-28 09:56:46 -05002258 if (flags & DLM_LKF_CANCEL && flags & DLM_LKF_FORCEUNLOCK)
2259 return -EINVAL;
2260
David Teiglande7fd4172006-01-18 09:30:29 +00002261 args->flags = flags;
David Teiglande5dae542008-02-06 00:35:45 -06002262 args->astparam = astarg;
David Teiglande7fd4172006-01-18 09:30:29 +00002263 return 0;
2264}
2265
2266static int validate_lock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
2267 struct dlm_args *args)
2268{
2269 int rv = -EINVAL;
2270
2271 if (args->flags & DLM_LKF_CONVERT) {
2272 if (lkb->lkb_flags & DLM_IFL_MSTCPY)
2273 goto out;
2274
2275 if (args->flags & DLM_LKF_QUECVT &&
2276 !__quecvt_compat_matrix[lkb->lkb_grmode+1][args->mode+1])
2277 goto out;
2278
2279 rv = -EBUSY;
2280 if (lkb->lkb_status != DLM_LKSTS_GRANTED)
2281 goto out;
2282
2283 if (lkb->lkb_wait_type)
2284 goto out;
David Teiglandef0c2bb2007-03-28 09:56:46 -05002285
2286 if (is_overlap(lkb))
2287 goto out;
David Teiglande7fd4172006-01-18 09:30:29 +00002288 }
2289
2290 lkb->lkb_exflags = args->flags;
2291 lkb->lkb_sbflags = 0;
David Teiglande5dae542008-02-06 00:35:45 -06002292 lkb->lkb_astfn = args->astfn;
David Teiglande7fd4172006-01-18 09:30:29 +00002293 lkb->lkb_astparam = args->astparam;
David Teiglande5dae542008-02-06 00:35:45 -06002294 lkb->lkb_bastfn = args->bastfn;
David Teiglande7fd4172006-01-18 09:30:29 +00002295 lkb->lkb_rqmode = args->mode;
2296 lkb->lkb_lksb = args->lksb;
2297 lkb->lkb_lvbptr = args->lksb->sb_lvbptr;
2298 lkb->lkb_ownpid = (int) current->pid;
David Teiglandd7db9232007-05-18 09:00:32 -05002299 lkb->lkb_timeout_cs = args->timeout;
David Teiglande7fd4172006-01-18 09:30:29 +00002300 rv = 0;
2301 out:
David Teigland43279e52009-01-28 14:37:54 -06002302 if (rv)
2303 log_debug(ls, "validate_lock_args %d %x %x %x %d %d %s",
2304 rv, lkb->lkb_id, lkb->lkb_flags, args->flags,
2305 lkb->lkb_status, lkb->lkb_wait_type,
2306 lkb->lkb_resource->res_name);
David Teiglande7fd4172006-01-18 09:30:29 +00002307 return rv;
2308}
2309
David Teiglandef0c2bb2007-03-28 09:56:46 -05002310/* when dlm_unlock() sees -EBUSY with CANCEL/FORCEUNLOCK it returns 0
2311 for success */
2312
2313/* note: it's valid for lkb_nodeid/res_nodeid to be -1 when we get here
2314 because there may be a lookup in progress and it's valid to do
2315 cancel/unlockf on it */
2316
David Teiglande7fd4172006-01-18 09:30:29 +00002317static int validate_unlock_args(struct dlm_lkb *lkb, struct dlm_args *args)
2318{
David Teiglandef0c2bb2007-03-28 09:56:46 -05002319 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
David Teiglande7fd4172006-01-18 09:30:29 +00002320 int rv = -EINVAL;
2321
David Teiglandef0c2bb2007-03-28 09:56:46 -05002322 if (lkb->lkb_flags & DLM_IFL_MSTCPY) {
2323 log_error(ls, "unlock on MSTCPY %x", lkb->lkb_id);
2324 dlm_print_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00002325 goto out;
David Teiglandef0c2bb2007-03-28 09:56:46 -05002326 }
David Teiglande7fd4172006-01-18 09:30:29 +00002327
David Teiglandef0c2bb2007-03-28 09:56:46 -05002328 /* an lkb may still exist even though the lock is EOL'ed due to a
2329 cancel, unlock or failed noqueue request; an app can't use these
2330 locks; return same error as if the lkid had not been found at all */
2331
2332 if (lkb->lkb_flags & DLM_IFL_ENDOFLIFE) {
2333 log_debug(ls, "unlock on ENDOFLIFE %x", lkb->lkb_id);
2334 rv = -ENOENT;
2335 goto out;
2336 }
2337
2338 /* an lkb may be waiting for an rsb lookup to complete where the
2339 lookup was initiated by another lock */
2340
David Teigland42dc1602008-01-09 10:30:45 -06002341 if (!list_empty(&lkb->lkb_rsb_lookup)) {
2342 if (args->flags & (DLM_LKF_CANCEL | DLM_LKF_FORCEUNLOCK)) {
David Teiglandef0c2bb2007-03-28 09:56:46 -05002343 log_debug(ls, "unlock on rsb_lookup %x", lkb->lkb_id);
2344 list_del_init(&lkb->lkb_rsb_lookup);
2345 queue_cast(lkb->lkb_resource, lkb,
2346 args->flags & DLM_LKF_CANCEL ?
2347 -DLM_ECANCEL : -DLM_EUNLOCK);
2348 unhold_lkb(lkb); /* undoes create_lkb() */
David Teiglandef0c2bb2007-03-28 09:56:46 -05002349 }
David Teigland42dc1602008-01-09 10:30:45 -06002350 /* caller changes -EBUSY to 0 for CANCEL and FORCEUNLOCK */
2351 rv = -EBUSY;
2352 goto out;
David Teiglandef0c2bb2007-03-28 09:56:46 -05002353 }
2354
2355 /* cancel not allowed with another cancel/unlock in progress */
2356
2357 if (args->flags & DLM_LKF_CANCEL) {
2358 if (lkb->lkb_exflags & DLM_LKF_CANCEL)
2359 goto out;
2360
2361 if (is_overlap(lkb))
2362 goto out;
2363
David Teigland3ae1acf2007-05-18 08:59:31 -05002364 /* don't let scand try to do a cancel */
2365 del_timeout(lkb);
2366
David Teiglandef0c2bb2007-03-28 09:56:46 -05002367 if (lkb->lkb_flags & DLM_IFL_RESEND) {
2368 lkb->lkb_flags |= DLM_IFL_OVERLAP_CANCEL;
2369 rv = -EBUSY;
2370 goto out;
2371 }
2372
David Teiglanda536e382009-02-27 15:23:28 -06002373 /* there's nothing to cancel */
2374 if (lkb->lkb_status == DLM_LKSTS_GRANTED &&
2375 !lkb->lkb_wait_type) {
2376 rv = -EBUSY;
2377 goto out;
2378 }
2379
David Teiglandef0c2bb2007-03-28 09:56:46 -05002380 switch (lkb->lkb_wait_type) {
2381 case DLM_MSG_LOOKUP:
2382 case DLM_MSG_REQUEST:
2383 lkb->lkb_flags |= DLM_IFL_OVERLAP_CANCEL;
2384 rv = -EBUSY;
2385 goto out;
2386 case DLM_MSG_UNLOCK:
2387 case DLM_MSG_CANCEL:
2388 goto out;
2389 }
2390 /* add_to_waiters() will set OVERLAP_CANCEL */
David Teiglande7fd4172006-01-18 09:30:29 +00002391 goto out_ok;
David Teiglandef0c2bb2007-03-28 09:56:46 -05002392 }
David Teiglande7fd4172006-01-18 09:30:29 +00002393
David Teiglandef0c2bb2007-03-28 09:56:46 -05002394 /* do we need to allow a force-unlock if there's a normal unlock
2395 already in progress? in what conditions could the normal unlock
2396 fail such that we'd want to send a force-unlock to be sure? */
David Teiglande7fd4172006-01-18 09:30:29 +00002397
David Teiglandef0c2bb2007-03-28 09:56:46 -05002398 if (args->flags & DLM_LKF_FORCEUNLOCK) {
2399 if (lkb->lkb_exflags & DLM_LKF_FORCEUNLOCK)
2400 goto out;
David Teiglande7fd4172006-01-18 09:30:29 +00002401
David Teiglandef0c2bb2007-03-28 09:56:46 -05002402 if (is_overlap_unlock(lkb))
2403 goto out;
2404
David Teigland3ae1acf2007-05-18 08:59:31 -05002405 /* don't let scand try to do a cancel */
2406 del_timeout(lkb);
2407
David Teiglandef0c2bb2007-03-28 09:56:46 -05002408 if (lkb->lkb_flags & DLM_IFL_RESEND) {
2409 lkb->lkb_flags |= DLM_IFL_OVERLAP_UNLOCK;
2410 rv = -EBUSY;
2411 goto out;
2412 }
2413
2414 switch (lkb->lkb_wait_type) {
2415 case DLM_MSG_LOOKUP:
2416 case DLM_MSG_REQUEST:
2417 lkb->lkb_flags |= DLM_IFL_OVERLAP_UNLOCK;
2418 rv = -EBUSY;
2419 goto out;
2420 case DLM_MSG_UNLOCK:
2421 goto out;
2422 }
2423 /* add_to_waiters() will set OVERLAP_UNLOCK */
2424 goto out_ok;
2425 }
2426
2427 /* normal unlock not allowed if there's any op in progress */
David Teiglande7fd4172006-01-18 09:30:29 +00002428 rv = -EBUSY;
David Teiglandef0c2bb2007-03-28 09:56:46 -05002429 if (lkb->lkb_wait_type || lkb->lkb_wait_count)
David Teiglande7fd4172006-01-18 09:30:29 +00002430 goto out;
2431
2432 out_ok:
David Teiglandef0c2bb2007-03-28 09:56:46 -05002433 /* an overlapping op shouldn't blow away exflags from other op */
2434 lkb->lkb_exflags |= args->flags;
David Teiglande7fd4172006-01-18 09:30:29 +00002435 lkb->lkb_sbflags = 0;
2436 lkb->lkb_astparam = args->astparam;
David Teiglande7fd4172006-01-18 09:30:29 +00002437 rv = 0;
2438 out:
David Teiglandef0c2bb2007-03-28 09:56:46 -05002439 if (rv)
2440 log_debug(ls, "validate_unlock_args %d %x %x %x %x %d %s", rv,
2441 lkb->lkb_id, lkb->lkb_flags, lkb->lkb_exflags,
2442 args->flags, lkb->lkb_wait_type,
2443 lkb->lkb_resource->res_name);
David Teiglande7fd4172006-01-18 09:30:29 +00002444 return rv;
2445}
2446
2447/*
2448 * Four stage 4 varieties:
2449 * do_request(), do_convert(), do_unlock(), do_cancel()
2450 * These are called on the master node for the given lock and
2451 * from the central locking logic.
2452 */
2453
2454static int do_request(struct dlm_rsb *r, struct dlm_lkb *lkb)
2455{
2456 int error = 0;
2457
David Teiglandc85d65e2007-05-18 09:01:26 -05002458 if (can_be_granted(r, lkb, 1, NULL)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002459 grant_lock(r, lkb);
2460 queue_cast(r, lkb, 0);
2461 goto out;
2462 }
2463
2464 if (can_be_queued(lkb)) {
2465 error = -EINPROGRESS;
2466 add_lkb(r, lkb, DLM_LKSTS_WAITING);
David Teigland3ae1acf2007-05-18 08:59:31 -05002467 add_timeout(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00002468 goto out;
2469 }
2470
2471 error = -EAGAIN;
David Teiglande7fd4172006-01-18 09:30:29 +00002472 queue_cast(r, lkb, -EAGAIN);
David Teiglande7fd4172006-01-18 09:30:29 +00002473 out:
2474 return error;
2475}
2476
David Teiglandcf6620a2010-02-24 11:59:23 -06002477static void do_request_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
2478 int error)
2479{
2480 switch (error) {
2481 case -EAGAIN:
2482 if (force_blocking_asts(lkb))
2483 send_blocking_asts_all(r, lkb);
2484 break;
2485 case -EINPROGRESS:
2486 send_blocking_asts(r, lkb);
2487 break;
2488 }
2489}
2490
David Teiglande7fd4172006-01-18 09:30:29 +00002491static int do_convert(struct dlm_rsb *r, struct dlm_lkb *lkb)
2492{
2493 int error = 0;
David Teiglandc85d65e2007-05-18 09:01:26 -05002494 int deadlk = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00002495
2496 /* changing an existing lock may allow others to be granted */
2497
David Teiglandc85d65e2007-05-18 09:01:26 -05002498 if (can_be_granted(r, lkb, 1, &deadlk)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002499 grant_lock(r, lkb);
2500 queue_cast(r, lkb, 0);
David Teiglande7fd4172006-01-18 09:30:29 +00002501 goto out;
2502 }
2503
David Teiglandc85d65e2007-05-18 09:01:26 -05002504 /* can_be_granted() detected that this lock would block in a conversion
2505 deadlock, so we leave it on the granted queue and return EDEADLK in
2506 the ast for the convert. */
2507
2508 if (deadlk) {
2509 /* it's left on the granted queue */
David Teiglandc85d65e2007-05-18 09:01:26 -05002510 revert_lock(r, lkb);
2511 queue_cast(r, lkb, -EDEADLK);
2512 error = -EDEADLK;
2513 goto out;
2514 }
2515
David Teigland7d3c1fe2007-04-19 10:30:41 -05002516 /* is_demoted() means the can_be_granted() above set the grmode
2517 to NL, and left us on the granted queue. This auto-demotion
2518 (due to CONVDEADLK) might mean other locks, and/or this lock, are
2519 now grantable. We have to try to grant other converting locks
2520 before we try again to grant this one. */
2521
2522 if (is_demoted(lkb)) {
David Teigland36509252007-08-07 09:44:48 -05002523 grant_pending_convert(r, DLM_LOCK_IV, NULL);
David Teigland7d3c1fe2007-04-19 10:30:41 -05002524 if (_can_be_granted(r, lkb, 1)) {
2525 grant_lock(r, lkb);
2526 queue_cast(r, lkb, 0);
David Teigland7d3c1fe2007-04-19 10:30:41 -05002527 goto out;
2528 }
2529 /* else fall through and move to convert queue */
2530 }
2531
2532 if (can_be_queued(lkb)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002533 error = -EINPROGRESS;
2534 del_lkb(r, lkb);
2535 add_lkb(r, lkb, DLM_LKSTS_CONVERT);
David Teigland3ae1acf2007-05-18 08:59:31 -05002536 add_timeout(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00002537 goto out;
2538 }
2539
2540 error = -EAGAIN;
David Teiglande7fd4172006-01-18 09:30:29 +00002541 queue_cast(r, lkb, -EAGAIN);
David Teiglande7fd4172006-01-18 09:30:29 +00002542 out:
2543 return error;
2544}
2545
David Teiglandcf6620a2010-02-24 11:59:23 -06002546static void do_convert_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
2547 int error)
2548{
2549 switch (error) {
2550 case 0:
2551 grant_pending_locks(r);
2552 /* grant_pending_locks also sends basts */
2553 break;
2554 case -EAGAIN:
2555 if (force_blocking_asts(lkb))
2556 send_blocking_asts_all(r, lkb);
2557 break;
2558 case -EINPROGRESS:
2559 send_blocking_asts(r, lkb);
2560 break;
2561 }
2562}
2563
David Teiglande7fd4172006-01-18 09:30:29 +00002564static int do_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2565{
2566 remove_lock(r, lkb);
2567 queue_cast(r, lkb, -DLM_EUNLOCK);
David Teiglande7fd4172006-01-18 09:30:29 +00002568 return -DLM_EUNLOCK;
2569}
2570
David Teiglandcf6620a2010-02-24 11:59:23 -06002571static void do_unlock_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
2572 int error)
2573{
2574 grant_pending_locks(r);
2575}
2576
David Teiglandef0c2bb2007-03-28 09:56:46 -05002577/* returns: 0 did nothing, -DLM_ECANCEL canceled lock */
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04002578
David Teiglande7fd4172006-01-18 09:30:29 +00002579static int do_cancel(struct dlm_rsb *r, struct dlm_lkb *lkb)
2580{
David Teiglandef0c2bb2007-03-28 09:56:46 -05002581 int error;
2582
2583 error = revert_lock(r, lkb);
2584 if (error) {
2585 queue_cast(r, lkb, -DLM_ECANCEL);
David Teiglandef0c2bb2007-03-28 09:56:46 -05002586 return -DLM_ECANCEL;
2587 }
2588 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00002589}
2590
David Teiglandcf6620a2010-02-24 11:59:23 -06002591static void do_cancel_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
2592 int error)
2593{
2594 if (error)
2595 grant_pending_locks(r);
2596}
2597
David Teiglande7fd4172006-01-18 09:30:29 +00002598/*
2599 * Four stage 3 varieties:
2600 * _request_lock(), _convert_lock(), _unlock_lock(), _cancel_lock()
2601 */
2602
2603/* add a new lkb to a possibly new rsb, called by requesting process */
2604
2605static int _request_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2606{
2607 int error;
2608
2609 /* set_master: sets lkb nodeid from r */
2610
2611 error = set_master(r, lkb);
2612 if (error < 0)
2613 goto out;
2614 if (error) {
2615 error = 0;
2616 goto out;
2617 }
2618
David Teiglandcf6620a2010-02-24 11:59:23 -06002619 if (is_remote(r)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002620 /* receive_request() calls do_request() on remote node */
2621 error = send_request(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002622 } else {
David Teiglande7fd4172006-01-18 09:30:29 +00002623 error = do_request(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002624 /* for remote locks the request_reply is sent
2625 between do_request and do_request_effects */
2626 do_request_effects(r, lkb, error);
2627 }
David Teiglande7fd4172006-01-18 09:30:29 +00002628 out:
2629 return error;
2630}
2631
David Teigland3bcd3682006-02-23 09:56:38 +00002632/* change some property of an existing lkb, e.g. mode */
David Teiglande7fd4172006-01-18 09:30:29 +00002633
2634static int _convert_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2635{
2636 int error;
2637
David Teiglandcf6620a2010-02-24 11:59:23 -06002638 if (is_remote(r)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002639 /* receive_convert() calls do_convert() on remote node */
2640 error = send_convert(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002641 } else {
David Teiglande7fd4172006-01-18 09:30:29 +00002642 error = do_convert(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002643 /* for remote locks the convert_reply is sent
2644 between do_convert and do_convert_effects */
2645 do_convert_effects(r, lkb, error);
2646 }
David Teiglande7fd4172006-01-18 09:30:29 +00002647
2648 return error;
2649}
2650
2651/* remove an existing lkb from the granted queue */
2652
2653static int _unlock_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2654{
2655 int error;
2656
David Teiglandcf6620a2010-02-24 11:59:23 -06002657 if (is_remote(r)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002658 /* receive_unlock() calls do_unlock() on remote node */
2659 error = send_unlock(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002660 } else {
David Teiglande7fd4172006-01-18 09:30:29 +00002661 error = do_unlock(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002662 /* for remote locks the unlock_reply is sent
2663 between do_unlock and do_unlock_effects */
2664 do_unlock_effects(r, lkb, error);
2665 }
David Teiglande7fd4172006-01-18 09:30:29 +00002666
2667 return error;
2668}
2669
2670/* remove an existing lkb from the convert or wait queue */
2671
2672static int _cancel_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2673{
2674 int error;
2675
David Teiglandcf6620a2010-02-24 11:59:23 -06002676 if (is_remote(r)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002677 /* receive_cancel() calls do_cancel() on remote node */
2678 error = send_cancel(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002679 } else {
David Teiglande7fd4172006-01-18 09:30:29 +00002680 error = do_cancel(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002681 /* for remote locks the cancel_reply is sent
2682 between do_cancel and do_cancel_effects */
2683 do_cancel_effects(r, lkb, error);
2684 }
David Teiglande7fd4172006-01-18 09:30:29 +00002685
2686 return error;
2687}
2688
2689/*
2690 * Four stage 2 varieties:
2691 * request_lock(), convert_lock(), unlock_lock(), cancel_lock()
2692 */
2693
2694static int request_lock(struct dlm_ls *ls, struct dlm_lkb *lkb, char *name,
2695 int len, struct dlm_args *args)
2696{
2697 struct dlm_rsb *r;
2698 int error;
2699
2700 error = validate_lock_args(ls, lkb, args);
2701 if (error)
2702 goto out;
2703
2704 error = find_rsb(ls, name, len, R_CREATE, &r);
2705 if (error)
2706 goto out;
2707
2708 lock_rsb(r);
2709
2710 attach_lkb(r, lkb);
2711 lkb->lkb_lksb->sb_lkid = lkb->lkb_id;
2712
2713 error = _request_lock(r, lkb);
2714
2715 unlock_rsb(r);
2716 put_rsb(r);
2717
2718 out:
2719 return error;
2720}
2721
2722static int convert_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
2723 struct dlm_args *args)
2724{
2725 struct dlm_rsb *r;
2726 int error;
2727
2728 r = lkb->lkb_resource;
2729
2730 hold_rsb(r);
2731 lock_rsb(r);
2732
2733 error = validate_lock_args(ls, lkb, args);
2734 if (error)
2735 goto out;
2736
2737 error = _convert_lock(r, lkb);
2738 out:
2739 unlock_rsb(r);
2740 put_rsb(r);
2741 return error;
2742}
2743
2744static int unlock_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
2745 struct dlm_args *args)
2746{
2747 struct dlm_rsb *r;
2748 int error;
2749
2750 r = lkb->lkb_resource;
2751
2752 hold_rsb(r);
2753 lock_rsb(r);
2754
2755 error = validate_unlock_args(lkb, args);
2756 if (error)
2757 goto out;
2758
2759 error = _unlock_lock(r, lkb);
2760 out:
2761 unlock_rsb(r);
2762 put_rsb(r);
2763 return error;
2764}
2765
2766static int cancel_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
2767 struct dlm_args *args)
2768{
2769 struct dlm_rsb *r;
2770 int error;
2771
2772 r = lkb->lkb_resource;
2773
2774 hold_rsb(r);
2775 lock_rsb(r);
2776
2777 error = validate_unlock_args(lkb, args);
2778 if (error)
2779 goto out;
2780
2781 error = _cancel_lock(r, lkb);
2782 out:
2783 unlock_rsb(r);
2784 put_rsb(r);
2785 return error;
2786}
2787
2788/*
2789 * Two stage 1 varieties: dlm_lock() and dlm_unlock()
2790 */
2791
2792int dlm_lock(dlm_lockspace_t *lockspace,
2793 int mode,
2794 struct dlm_lksb *lksb,
2795 uint32_t flags,
2796 void *name,
2797 unsigned int namelen,
2798 uint32_t parent_lkid,
2799 void (*ast) (void *astarg),
2800 void *astarg,
David Teigland3bcd3682006-02-23 09:56:38 +00002801 void (*bast) (void *astarg, int mode))
David Teiglande7fd4172006-01-18 09:30:29 +00002802{
2803 struct dlm_ls *ls;
2804 struct dlm_lkb *lkb;
2805 struct dlm_args args;
2806 int error, convert = flags & DLM_LKF_CONVERT;
2807
2808 ls = dlm_find_lockspace_local(lockspace);
2809 if (!ls)
2810 return -EINVAL;
2811
David Teigland85e86ed2007-05-18 08:58:15 -05002812 dlm_lock_recovery(ls);
David Teiglande7fd4172006-01-18 09:30:29 +00002813
2814 if (convert)
2815 error = find_lkb(ls, lksb->sb_lkid, &lkb);
2816 else
2817 error = create_lkb(ls, &lkb);
2818
2819 if (error)
2820 goto out;
2821
David Teiglandd7db9232007-05-18 09:00:32 -05002822 error = set_lock_args(mode, lksb, flags, namelen, 0, ast,
David Teigland3bcd3682006-02-23 09:56:38 +00002823 astarg, bast, &args);
David Teiglande7fd4172006-01-18 09:30:29 +00002824 if (error)
2825 goto out_put;
2826
2827 if (convert)
2828 error = convert_lock(ls, lkb, &args);
2829 else
2830 error = request_lock(ls, lkb, name, namelen, &args);
2831
2832 if (error == -EINPROGRESS)
2833 error = 0;
2834 out_put:
2835 if (convert || error)
David Teiglandb3f58d82006-02-28 11:16:37 -05002836 __put_lkb(ls, lkb);
David Teiglandc85d65e2007-05-18 09:01:26 -05002837 if (error == -EAGAIN || error == -EDEADLK)
David Teiglande7fd4172006-01-18 09:30:29 +00002838 error = 0;
2839 out:
David Teigland85e86ed2007-05-18 08:58:15 -05002840 dlm_unlock_recovery(ls);
David Teiglande7fd4172006-01-18 09:30:29 +00002841 dlm_put_lockspace(ls);
2842 return error;
2843}
2844
2845int dlm_unlock(dlm_lockspace_t *lockspace,
2846 uint32_t lkid,
2847 uint32_t flags,
2848 struct dlm_lksb *lksb,
2849 void *astarg)
2850{
2851 struct dlm_ls *ls;
2852 struct dlm_lkb *lkb;
2853 struct dlm_args args;
2854 int error;
2855
2856 ls = dlm_find_lockspace_local(lockspace);
2857 if (!ls)
2858 return -EINVAL;
2859
David Teigland85e86ed2007-05-18 08:58:15 -05002860 dlm_lock_recovery(ls);
David Teiglande7fd4172006-01-18 09:30:29 +00002861
2862 error = find_lkb(ls, lkid, &lkb);
2863 if (error)
2864 goto out;
2865
2866 error = set_unlock_args(flags, astarg, &args);
2867 if (error)
2868 goto out_put;
2869
2870 if (flags & DLM_LKF_CANCEL)
2871 error = cancel_lock(ls, lkb, &args);
2872 else
2873 error = unlock_lock(ls, lkb, &args);
2874
2875 if (error == -DLM_EUNLOCK || error == -DLM_ECANCEL)
2876 error = 0;
David Teiglandef0c2bb2007-03-28 09:56:46 -05002877 if (error == -EBUSY && (flags & (DLM_LKF_CANCEL | DLM_LKF_FORCEUNLOCK)))
2878 error = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00002879 out_put:
David Teiglandb3f58d82006-02-28 11:16:37 -05002880 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00002881 out:
David Teigland85e86ed2007-05-18 08:58:15 -05002882 dlm_unlock_recovery(ls);
David Teiglande7fd4172006-01-18 09:30:29 +00002883 dlm_put_lockspace(ls);
2884 return error;
2885}
2886
2887/*
2888 * send/receive routines for remote operations and replies
2889 *
2890 * send_args
2891 * send_common
2892 * send_request receive_request
2893 * send_convert receive_convert
2894 * send_unlock receive_unlock
2895 * send_cancel receive_cancel
2896 * send_grant receive_grant
2897 * send_bast receive_bast
2898 * send_lookup receive_lookup
2899 * send_remove receive_remove
2900 *
2901 * send_common_reply
2902 * receive_request_reply send_request_reply
2903 * receive_convert_reply send_convert_reply
2904 * receive_unlock_reply send_unlock_reply
2905 * receive_cancel_reply send_cancel_reply
2906 * receive_lookup_reply send_lookup_reply
2907 */
2908
David Teigland7e4dac32007-04-02 09:06:41 -05002909static int _create_message(struct dlm_ls *ls, int mb_len,
2910 int to_nodeid, int mstype,
2911 struct dlm_message **ms_ret,
2912 struct dlm_mhandle **mh_ret)
2913{
2914 struct dlm_message *ms;
2915 struct dlm_mhandle *mh;
2916 char *mb;
2917
2918 /* get_buffer gives us a message handle (mh) that we need to
2919 pass into lowcomms_commit and a message buffer (mb) that we
2920 write our data into */
2921
David Teigland573c24c2009-11-30 16:34:43 -06002922 mh = dlm_lowcomms_get_buffer(to_nodeid, mb_len, GFP_NOFS, &mb);
David Teigland7e4dac32007-04-02 09:06:41 -05002923 if (!mh)
2924 return -ENOBUFS;
2925
2926 memset(mb, 0, mb_len);
2927
2928 ms = (struct dlm_message *) mb;
2929
2930 ms->m_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
2931 ms->m_header.h_lockspace = ls->ls_global_id;
2932 ms->m_header.h_nodeid = dlm_our_nodeid();
2933 ms->m_header.h_length = mb_len;
2934 ms->m_header.h_cmd = DLM_MSG;
2935
2936 ms->m_type = mstype;
2937
2938 *mh_ret = mh;
2939 *ms_ret = ms;
2940 return 0;
2941}
2942
David Teiglande7fd4172006-01-18 09:30:29 +00002943static int create_message(struct dlm_rsb *r, struct dlm_lkb *lkb,
2944 int to_nodeid, int mstype,
2945 struct dlm_message **ms_ret,
2946 struct dlm_mhandle **mh_ret)
2947{
David Teiglande7fd4172006-01-18 09:30:29 +00002948 int mb_len = sizeof(struct dlm_message);
2949
2950 switch (mstype) {
2951 case DLM_MSG_REQUEST:
2952 case DLM_MSG_LOOKUP:
2953 case DLM_MSG_REMOVE:
2954 mb_len += r->res_length;
2955 break;
2956 case DLM_MSG_CONVERT:
2957 case DLM_MSG_UNLOCK:
2958 case DLM_MSG_REQUEST_REPLY:
2959 case DLM_MSG_CONVERT_REPLY:
2960 case DLM_MSG_GRANT:
2961 if (lkb && lkb->lkb_lvbptr)
2962 mb_len += r->res_ls->ls_lvblen;
2963 break;
2964 }
2965
David Teigland7e4dac32007-04-02 09:06:41 -05002966 return _create_message(r->res_ls, mb_len, to_nodeid, mstype,
2967 ms_ret, mh_ret);
David Teiglande7fd4172006-01-18 09:30:29 +00002968}
2969
2970/* further lowcomms enhancements or alternate implementations may make
2971 the return value from this function useful at some point */
2972
2973static int send_message(struct dlm_mhandle *mh, struct dlm_message *ms)
2974{
2975 dlm_message_out(ms);
2976 dlm_lowcomms_commit_buffer(mh);
2977 return 0;
2978}
2979
2980static void send_args(struct dlm_rsb *r, struct dlm_lkb *lkb,
2981 struct dlm_message *ms)
2982{
2983 ms->m_nodeid = lkb->lkb_nodeid;
2984 ms->m_pid = lkb->lkb_ownpid;
2985 ms->m_lkid = lkb->lkb_id;
2986 ms->m_remid = lkb->lkb_remid;
2987 ms->m_exflags = lkb->lkb_exflags;
2988 ms->m_sbflags = lkb->lkb_sbflags;
2989 ms->m_flags = lkb->lkb_flags;
2990 ms->m_lvbseq = lkb->lkb_lvbseq;
2991 ms->m_status = lkb->lkb_status;
2992 ms->m_grmode = lkb->lkb_grmode;
2993 ms->m_rqmode = lkb->lkb_rqmode;
2994 ms->m_hash = r->res_hash;
2995
2996 /* m_result and m_bastmode are set from function args,
2997 not from lkb fields */
2998
David Teiglande5dae542008-02-06 00:35:45 -06002999 if (lkb->lkb_bastfn)
David Teigland8304d6f2011-02-21 14:58:21 -06003000 ms->m_asts |= DLM_CB_BAST;
David Teiglande5dae542008-02-06 00:35:45 -06003001 if (lkb->lkb_astfn)
David Teigland8304d6f2011-02-21 14:58:21 -06003002 ms->m_asts |= DLM_CB_CAST;
David Teiglande7fd4172006-01-18 09:30:29 +00003003
David Teiglandda49f362006-12-13 10:38:45 -06003004 /* compare with switch in create_message; send_remove() doesn't
3005 use send_args() */
3006
3007 switch (ms->m_type) {
3008 case DLM_MSG_REQUEST:
3009 case DLM_MSG_LOOKUP:
David Teiglande7fd4172006-01-18 09:30:29 +00003010 memcpy(ms->m_extra, r->res_name, r->res_length);
David Teiglandda49f362006-12-13 10:38:45 -06003011 break;
3012 case DLM_MSG_CONVERT:
3013 case DLM_MSG_UNLOCK:
3014 case DLM_MSG_REQUEST_REPLY:
3015 case DLM_MSG_CONVERT_REPLY:
3016 case DLM_MSG_GRANT:
3017 if (!lkb->lkb_lvbptr)
3018 break;
David Teiglande7fd4172006-01-18 09:30:29 +00003019 memcpy(ms->m_extra, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
David Teiglandda49f362006-12-13 10:38:45 -06003020 break;
3021 }
David Teiglande7fd4172006-01-18 09:30:29 +00003022}
3023
3024static int send_common(struct dlm_rsb *r, struct dlm_lkb *lkb, int mstype)
3025{
3026 struct dlm_message *ms;
3027 struct dlm_mhandle *mh;
3028 int to_nodeid, error;
3029
David Teiglandc6ff6692011-03-28 14:17:26 -05003030 to_nodeid = r->res_nodeid;
3031
3032 error = add_to_waiters(lkb, mstype, to_nodeid);
David Teiglandef0c2bb2007-03-28 09:56:46 -05003033 if (error)
3034 return error;
David Teiglande7fd4172006-01-18 09:30:29 +00003035
David Teiglande7fd4172006-01-18 09:30:29 +00003036 error = create_message(r, lkb, to_nodeid, mstype, &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:
David Teiglandef0c2bb2007-03-28 09:56:46 -05003048 remove_from_waiters(lkb, msg_reply_type(mstype));
David Teiglande7fd4172006-01-18 09:30:29 +00003049 return error;
3050}
3051
3052static int send_request(struct dlm_rsb *r, struct dlm_lkb *lkb)
3053{
3054 return send_common(r, lkb, DLM_MSG_REQUEST);
3055}
3056
3057static int send_convert(struct dlm_rsb *r, struct dlm_lkb *lkb)
3058{
3059 int error;
3060
3061 error = send_common(r, lkb, DLM_MSG_CONVERT);
3062
3063 /* down conversions go without a reply from the master */
3064 if (!error && down_conversion(lkb)) {
David Teiglandef0c2bb2007-03-28 09:56:46 -05003065 remove_from_waiters(lkb, DLM_MSG_CONVERT_REPLY);
David Teigland2a7ce0e2011-04-04 15:19:59 -05003066 r->res_ls->ls_stub_ms.m_flags = DLM_IFL_STUB_MS;
David Teiglandef0c2bb2007-03-28 09:56:46 -05003067 r->res_ls->ls_stub_ms.m_type = DLM_MSG_CONVERT_REPLY;
David Teiglande7fd4172006-01-18 09:30:29 +00003068 r->res_ls->ls_stub_ms.m_result = 0;
3069 __receive_convert_reply(r, lkb, &r->res_ls->ls_stub_ms);
3070 }
3071
3072 return error;
3073}
3074
3075/* FIXME: if this lkb is the only lock we hold on the rsb, then set
3076 MASTER_UNCERTAIN to force the next request on the rsb to confirm
3077 that the master is still correct. */
3078
3079static int send_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
3080{
3081 return send_common(r, lkb, DLM_MSG_UNLOCK);
3082}
3083
3084static int send_cancel(struct dlm_rsb *r, struct dlm_lkb *lkb)
3085{
3086 return send_common(r, lkb, DLM_MSG_CANCEL);
3087}
3088
3089static int send_grant(struct dlm_rsb *r, struct dlm_lkb *lkb)
3090{
3091 struct dlm_message *ms;
3092 struct dlm_mhandle *mh;
3093 int to_nodeid, error;
3094
3095 to_nodeid = lkb->lkb_nodeid;
3096
3097 error = create_message(r, lkb, to_nodeid, DLM_MSG_GRANT, &ms, &mh);
3098 if (error)
3099 goto out;
3100
3101 send_args(r, lkb, ms);
3102
3103 ms->m_result = 0;
3104
3105 error = send_message(mh, ms);
3106 out:
3107 return error;
3108}
3109
3110static int send_bast(struct dlm_rsb *r, struct dlm_lkb *lkb, int mode)
3111{
3112 struct dlm_message *ms;
3113 struct dlm_mhandle *mh;
3114 int to_nodeid, error;
3115
3116 to_nodeid = lkb->lkb_nodeid;
3117
3118 error = create_message(r, NULL, to_nodeid, DLM_MSG_BAST, &ms, &mh);
3119 if (error)
3120 goto out;
3121
3122 send_args(r, lkb, ms);
3123
3124 ms->m_bastmode = mode;
3125
3126 error = send_message(mh, ms);
3127 out:
3128 return error;
3129}
3130
3131static int send_lookup(struct dlm_rsb *r, struct dlm_lkb *lkb)
3132{
3133 struct dlm_message *ms;
3134 struct dlm_mhandle *mh;
3135 int to_nodeid, error;
3136
David Teiglandc6ff6692011-03-28 14:17:26 -05003137 to_nodeid = dlm_dir_nodeid(r);
3138
3139 error = add_to_waiters(lkb, DLM_MSG_LOOKUP, to_nodeid);
David Teiglandef0c2bb2007-03-28 09:56:46 -05003140 if (error)
3141 return error;
David Teiglande7fd4172006-01-18 09:30:29 +00003142
David Teiglande7fd4172006-01-18 09:30:29 +00003143 error = create_message(r, NULL, to_nodeid, DLM_MSG_LOOKUP, &ms, &mh);
3144 if (error)
3145 goto fail;
3146
3147 send_args(r, lkb, ms);
3148
3149 error = send_message(mh, ms);
3150 if (error)
3151 goto fail;
3152 return 0;
3153
3154 fail:
David Teiglandef0c2bb2007-03-28 09:56:46 -05003155 remove_from_waiters(lkb, DLM_MSG_LOOKUP_REPLY);
David Teiglande7fd4172006-01-18 09:30:29 +00003156 return error;
3157}
3158
3159static int send_remove(struct dlm_rsb *r)
3160{
3161 struct dlm_message *ms;
3162 struct dlm_mhandle *mh;
3163 int to_nodeid, error;
3164
3165 to_nodeid = dlm_dir_nodeid(r);
3166
3167 error = create_message(r, NULL, to_nodeid, DLM_MSG_REMOVE, &ms, &mh);
3168 if (error)
3169 goto out;
3170
3171 memcpy(ms->m_extra, r->res_name, r->res_length);
3172 ms->m_hash = r->res_hash;
3173
3174 error = send_message(mh, ms);
3175 out:
3176 return error;
3177}
3178
3179static int send_common_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
3180 int mstype, int rv)
3181{
3182 struct dlm_message *ms;
3183 struct dlm_mhandle *mh;
3184 int to_nodeid, error;
3185
3186 to_nodeid = lkb->lkb_nodeid;
3187
3188 error = create_message(r, lkb, to_nodeid, mstype, &ms, &mh);
3189 if (error)
3190 goto out;
3191
3192 send_args(r, lkb, ms);
3193
3194 ms->m_result = rv;
3195
3196 error = send_message(mh, ms);
3197 out:
3198 return error;
3199}
3200
3201static int send_request_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
3202{
3203 return send_common_reply(r, lkb, DLM_MSG_REQUEST_REPLY, rv);
3204}
3205
3206static int send_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
3207{
3208 return send_common_reply(r, lkb, DLM_MSG_CONVERT_REPLY, rv);
3209}
3210
3211static int send_unlock_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
3212{
3213 return send_common_reply(r, lkb, DLM_MSG_UNLOCK_REPLY, rv);
3214}
3215
3216static int send_cancel_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
3217{
3218 return send_common_reply(r, lkb, DLM_MSG_CANCEL_REPLY, rv);
3219}
3220
3221static int send_lookup_reply(struct dlm_ls *ls, struct dlm_message *ms_in,
3222 int ret_nodeid, int rv)
3223{
3224 struct dlm_rsb *r = &ls->ls_stub_rsb;
3225 struct dlm_message *ms;
3226 struct dlm_mhandle *mh;
3227 int error, nodeid = ms_in->m_header.h_nodeid;
3228
3229 error = create_message(r, NULL, nodeid, DLM_MSG_LOOKUP_REPLY, &ms, &mh);
3230 if (error)
3231 goto out;
3232
3233 ms->m_lkid = ms_in->m_lkid;
3234 ms->m_result = rv;
3235 ms->m_nodeid = ret_nodeid;
3236
3237 error = send_message(mh, ms);
3238 out:
3239 return error;
3240}
3241
3242/* which args we save from a received message depends heavily on the type
3243 of message, unlike the send side where we can safely send everything about
3244 the lkb for any type of message */
3245
3246static void receive_flags(struct dlm_lkb *lkb, struct dlm_message *ms)
3247{
3248 lkb->lkb_exflags = ms->m_exflags;
David Teigland6f90a8b12006-11-10 14:16:27 -06003249 lkb->lkb_sbflags = ms->m_sbflags;
David Teiglande7fd4172006-01-18 09:30:29 +00003250 lkb->lkb_flags = (lkb->lkb_flags & 0xFFFF0000) |
3251 (ms->m_flags & 0x0000FFFF);
3252}
3253
3254static void receive_flags_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
3255{
David Teigland2a7ce0e2011-04-04 15:19:59 -05003256 if (ms->m_flags == DLM_IFL_STUB_MS)
3257 return;
3258
David Teiglande7fd4172006-01-18 09:30:29 +00003259 lkb->lkb_sbflags = ms->m_sbflags;
3260 lkb->lkb_flags = (lkb->lkb_flags & 0xFFFF0000) |
3261 (ms->m_flags & 0x0000FFFF);
3262}
3263
3264static int receive_extralen(struct dlm_message *ms)
3265{
3266 return (ms->m_header.h_length - sizeof(struct dlm_message));
3267}
3268
David Teiglande7fd4172006-01-18 09:30:29 +00003269static int receive_lvb(struct dlm_ls *ls, struct dlm_lkb *lkb,
3270 struct dlm_message *ms)
3271{
3272 int len;
3273
3274 if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
3275 if (!lkb->lkb_lvbptr)
David Teigland52bda2b2007-11-07 09:06:49 -06003276 lkb->lkb_lvbptr = dlm_allocate_lvb(ls);
David Teiglande7fd4172006-01-18 09:30:29 +00003277 if (!lkb->lkb_lvbptr)
3278 return -ENOMEM;
3279 len = receive_extralen(ms);
Al Viroa9cc9152008-01-26 00:02:29 -05003280 if (len > DLM_RESNAME_MAXLEN)
3281 len = DLM_RESNAME_MAXLEN;
David Teiglande7fd4172006-01-18 09:30:29 +00003282 memcpy(lkb->lkb_lvbptr, ms->m_extra, len);
3283 }
3284 return 0;
3285}
3286
David Teiglande5dae542008-02-06 00:35:45 -06003287static void fake_bastfn(void *astparam, int mode)
3288{
3289 log_print("fake_bastfn should not be called");
3290}
3291
3292static void fake_astfn(void *astparam)
3293{
3294 log_print("fake_astfn should not be called");
3295}
3296
David Teiglande7fd4172006-01-18 09:30:29 +00003297static int receive_request_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
3298 struct dlm_message *ms)
3299{
3300 lkb->lkb_nodeid = ms->m_header.h_nodeid;
3301 lkb->lkb_ownpid = ms->m_pid;
3302 lkb->lkb_remid = ms->m_lkid;
3303 lkb->lkb_grmode = DLM_LOCK_IV;
3304 lkb->lkb_rqmode = ms->m_rqmode;
David Teiglande5dae542008-02-06 00:35:45 -06003305
David Teigland8304d6f2011-02-21 14:58:21 -06003306 lkb->lkb_bastfn = (ms->m_asts & DLM_CB_BAST) ? &fake_bastfn : NULL;
3307 lkb->lkb_astfn = (ms->m_asts & DLM_CB_CAST) ? &fake_astfn : NULL;
David Teiglande7fd4172006-01-18 09:30:29 +00003308
David Teigland8d07fd52006-12-13 10:39:20 -06003309 if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
3310 /* lkb was just created so there won't be an lvb yet */
David Teigland52bda2b2007-11-07 09:06:49 -06003311 lkb->lkb_lvbptr = dlm_allocate_lvb(ls);
David Teigland8d07fd52006-12-13 10:39:20 -06003312 if (!lkb->lkb_lvbptr)
3313 return -ENOMEM;
3314 }
David Teiglande7fd4172006-01-18 09:30:29 +00003315
3316 return 0;
3317}
3318
3319static int receive_convert_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
3320 struct dlm_message *ms)
3321{
David Teiglande7fd4172006-01-18 09:30:29 +00003322 if (lkb->lkb_status != DLM_LKSTS_GRANTED)
3323 return -EBUSY;
3324
David Teiglande7fd4172006-01-18 09:30:29 +00003325 if (receive_lvb(ls, lkb, ms))
3326 return -ENOMEM;
3327
3328 lkb->lkb_rqmode = ms->m_rqmode;
3329 lkb->lkb_lvbseq = ms->m_lvbseq;
3330
3331 return 0;
3332}
3333
3334static int receive_unlock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
3335 struct dlm_message *ms)
3336{
David Teiglande7fd4172006-01-18 09:30:29 +00003337 if (receive_lvb(ls, lkb, ms))
3338 return -ENOMEM;
3339 return 0;
3340}
3341
3342/* We fill in the stub-lkb fields with the info that send_xxxx_reply()
3343 uses to send a reply and that the remote end uses to process the reply. */
3344
3345static void setup_stub_lkb(struct dlm_ls *ls, struct dlm_message *ms)
3346{
3347 struct dlm_lkb *lkb = &ls->ls_stub_lkb;
3348 lkb->lkb_nodeid = ms->m_header.h_nodeid;
3349 lkb->lkb_remid = ms->m_lkid;
3350}
3351
David Teiglandc54e04b2008-01-09 09:59:41 -06003352/* This is called after the rsb is locked so that we can safely inspect
3353 fields in the lkb. */
3354
3355static int validate_message(struct dlm_lkb *lkb, struct dlm_message *ms)
3356{
3357 int from = ms->m_header.h_nodeid;
3358 int error = 0;
3359
3360 switch (ms->m_type) {
3361 case DLM_MSG_CONVERT:
3362 case DLM_MSG_UNLOCK:
3363 case DLM_MSG_CANCEL:
3364 if (!is_master_copy(lkb) || lkb->lkb_nodeid != from)
3365 error = -EINVAL;
3366 break;
3367
3368 case DLM_MSG_CONVERT_REPLY:
3369 case DLM_MSG_UNLOCK_REPLY:
3370 case DLM_MSG_CANCEL_REPLY:
3371 case DLM_MSG_GRANT:
3372 case DLM_MSG_BAST:
3373 if (!is_process_copy(lkb) || lkb->lkb_nodeid != from)
3374 error = -EINVAL;
3375 break;
3376
3377 case DLM_MSG_REQUEST_REPLY:
3378 if (!is_process_copy(lkb))
3379 error = -EINVAL;
3380 else if (lkb->lkb_nodeid != -1 && lkb->lkb_nodeid != from)
3381 error = -EINVAL;
3382 break;
3383
3384 default:
3385 error = -EINVAL;
3386 }
3387
3388 if (error)
3389 log_error(lkb->lkb_resource->res_ls,
3390 "ignore invalid message %d from %d %x %x %x %d",
3391 ms->m_type, from, lkb->lkb_id, lkb->lkb_remid,
3392 lkb->lkb_flags, lkb->lkb_nodeid);
3393 return error;
3394}
3395
David Teigland6d40c4a2012-04-23 16:36:01 -05003396static int receive_request(struct dlm_ls *ls, struct dlm_message *ms)
David Teiglande7fd4172006-01-18 09:30:29 +00003397{
3398 struct dlm_lkb *lkb;
3399 struct dlm_rsb *r;
3400 int error, namelen;
3401
3402 error = create_lkb(ls, &lkb);
3403 if (error)
3404 goto fail;
3405
3406 receive_flags(lkb, ms);
3407 lkb->lkb_flags |= DLM_IFL_MSTCPY;
3408 error = receive_request_args(ls, lkb, ms);
3409 if (error) {
David Teiglandb3f58d82006-02-28 11:16:37 -05003410 __put_lkb(ls, lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003411 goto fail;
3412 }
3413
3414 namelen = receive_extralen(ms);
3415
3416 error = find_rsb(ls, ms->m_extra, namelen, R_MASTER, &r);
3417 if (error) {
David Teiglandb3f58d82006-02-28 11:16:37 -05003418 __put_lkb(ls, lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003419 goto fail;
3420 }
3421
3422 lock_rsb(r);
3423
3424 attach_lkb(r, lkb);
3425 error = do_request(r, lkb);
3426 send_request_reply(r, lkb, error);
David Teiglandcf6620a2010-02-24 11:59:23 -06003427 do_request_effects(r, lkb, error);
David Teiglande7fd4172006-01-18 09:30:29 +00003428
3429 unlock_rsb(r);
3430 put_rsb(r);
3431
3432 if (error == -EINPROGRESS)
3433 error = 0;
3434 if (error)
David Teiglandb3f58d82006-02-28 11:16:37 -05003435 dlm_put_lkb(lkb);
David Teigland6d40c4a2012-04-23 16:36:01 -05003436 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00003437
3438 fail:
3439 setup_stub_lkb(ls, ms);
3440 send_request_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
David Teigland6d40c4a2012-04-23 16:36:01 -05003441 return error;
David Teiglande7fd4172006-01-18 09:30:29 +00003442}
3443
David Teigland6d40c4a2012-04-23 16:36:01 -05003444static int receive_convert(struct dlm_ls *ls, struct dlm_message *ms)
David Teiglande7fd4172006-01-18 09:30:29 +00003445{
3446 struct dlm_lkb *lkb;
3447 struct dlm_rsb *r;
David Teigland90135922006-01-20 08:47:07 +00003448 int error, reply = 1;
David Teiglande7fd4172006-01-18 09:30:29 +00003449
3450 error = find_lkb(ls, ms->m_remid, &lkb);
3451 if (error)
3452 goto fail;
3453
David Teigland6d40c4a2012-04-23 16:36:01 -05003454 if (lkb->lkb_remid != ms->m_lkid) {
3455 log_error(ls, "receive_convert %x remid %x remote %d %x",
3456 lkb->lkb_id, lkb->lkb_remid,
3457 ms->m_header.h_nodeid, ms->m_lkid);
3458 error = -ENOENT;
3459 goto fail;
3460 }
3461
David Teiglande7fd4172006-01-18 09:30:29 +00003462 r = lkb->lkb_resource;
3463
3464 hold_rsb(r);
3465 lock_rsb(r);
3466
David Teiglandc54e04b2008-01-09 09:59:41 -06003467 error = validate_message(lkb, ms);
3468 if (error)
3469 goto out;
3470
David Teiglande7fd4172006-01-18 09:30:29 +00003471 receive_flags(lkb, ms);
David Teiglandcf6620a2010-02-24 11:59:23 -06003472
David Teiglande7fd4172006-01-18 09:30:29 +00003473 error = receive_convert_args(ls, lkb, ms);
David Teiglandcf6620a2010-02-24 11:59:23 -06003474 if (error) {
3475 send_convert_reply(r, lkb, error);
3476 goto out;
3477 }
3478
David Teiglande7fd4172006-01-18 09:30:29 +00003479 reply = !down_conversion(lkb);
3480
3481 error = do_convert(r, lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003482 if (reply)
3483 send_convert_reply(r, lkb, error);
David Teiglandcf6620a2010-02-24 11:59:23 -06003484 do_convert_effects(r, lkb, error);
David Teiglandc54e04b2008-01-09 09:59:41 -06003485 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003486 unlock_rsb(r);
3487 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05003488 dlm_put_lkb(lkb);
David Teigland6d40c4a2012-04-23 16:36:01 -05003489 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00003490
3491 fail:
3492 setup_stub_lkb(ls, ms);
3493 send_convert_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
David Teigland6d40c4a2012-04-23 16:36:01 -05003494 return error;
David Teiglande7fd4172006-01-18 09:30:29 +00003495}
3496
David Teigland6d40c4a2012-04-23 16:36:01 -05003497static int receive_unlock(struct dlm_ls *ls, struct dlm_message *ms)
David Teiglande7fd4172006-01-18 09:30:29 +00003498{
3499 struct dlm_lkb *lkb;
3500 struct dlm_rsb *r;
3501 int error;
3502
3503 error = find_lkb(ls, ms->m_remid, &lkb);
3504 if (error)
3505 goto fail;
3506
David Teigland6d40c4a2012-04-23 16:36:01 -05003507 if (lkb->lkb_remid != ms->m_lkid) {
3508 log_error(ls, "receive_unlock %x remid %x remote %d %x",
3509 lkb->lkb_id, lkb->lkb_remid,
3510 ms->m_header.h_nodeid, ms->m_lkid);
3511 error = -ENOENT;
3512 goto fail;
3513 }
3514
David Teiglande7fd4172006-01-18 09:30:29 +00003515 r = lkb->lkb_resource;
3516
3517 hold_rsb(r);
3518 lock_rsb(r);
3519
David Teiglandc54e04b2008-01-09 09:59:41 -06003520 error = validate_message(lkb, ms);
David Teiglande7fd4172006-01-18 09:30:29 +00003521 if (error)
3522 goto out;
3523
David Teiglandc54e04b2008-01-09 09:59:41 -06003524 receive_flags(lkb, ms);
David Teiglandcf6620a2010-02-24 11:59:23 -06003525
David Teiglandc54e04b2008-01-09 09:59:41 -06003526 error = receive_unlock_args(ls, lkb, ms);
David Teiglandcf6620a2010-02-24 11:59:23 -06003527 if (error) {
3528 send_unlock_reply(r, lkb, error);
3529 goto out;
3530 }
David Teiglande7fd4172006-01-18 09:30:29 +00003531
David Teiglandc54e04b2008-01-09 09:59:41 -06003532 error = do_unlock(r, lkb);
David Teiglandc54e04b2008-01-09 09:59:41 -06003533 send_unlock_reply(r, lkb, error);
David Teiglandcf6620a2010-02-24 11:59:23 -06003534 do_unlock_effects(r, lkb, error);
David Teiglandc54e04b2008-01-09 09:59:41 -06003535 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003536 unlock_rsb(r);
3537 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05003538 dlm_put_lkb(lkb);
David Teigland6d40c4a2012-04-23 16:36:01 -05003539 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00003540
3541 fail:
3542 setup_stub_lkb(ls, ms);
3543 send_unlock_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
David Teigland6d40c4a2012-04-23 16:36:01 -05003544 return error;
David Teiglande7fd4172006-01-18 09:30:29 +00003545}
3546
David Teigland6d40c4a2012-04-23 16:36:01 -05003547static int receive_cancel(struct dlm_ls *ls, struct dlm_message *ms)
David Teiglande7fd4172006-01-18 09:30:29 +00003548{
3549 struct dlm_lkb *lkb;
3550 struct dlm_rsb *r;
3551 int error;
3552
3553 error = find_lkb(ls, ms->m_remid, &lkb);
3554 if (error)
3555 goto fail;
3556
3557 receive_flags(lkb, ms);
3558
3559 r = lkb->lkb_resource;
3560
3561 hold_rsb(r);
3562 lock_rsb(r);
3563
David Teiglandc54e04b2008-01-09 09:59:41 -06003564 error = validate_message(lkb, ms);
3565 if (error)
3566 goto out;
3567
David Teiglande7fd4172006-01-18 09:30:29 +00003568 error = do_cancel(r, lkb);
3569 send_cancel_reply(r, lkb, error);
David Teiglandcf6620a2010-02-24 11:59:23 -06003570 do_cancel_effects(r, lkb, error);
David Teiglandc54e04b2008-01-09 09:59:41 -06003571 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003572 unlock_rsb(r);
3573 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05003574 dlm_put_lkb(lkb);
David Teigland6d40c4a2012-04-23 16:36:01 -05003575 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00003576
3577 fail:
3578 setup_stub_lkb(ls, ms);
3579 send_cancel_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
David Teigland6d40c4a2012-04-23 16:36:01 -05003580 return error;
David Teiglande7fd4172006-01-18 09:30:29 +00003581}
3582
David Teigland6d40c4a2012-04-23 16:36:01 -05003583static int receive_grant(struct dlm_ls *ls, struct dlm_message *ms)
David Teiglande7fd4172006-01-18 09:30:29 +00003584{
3585 struct dlm_lkb *lkb;
3586 struct dlm_rsb *r;
3587 int error;
3588
3589 error = find_lkb(ls, ms->m_remid, &lkb);
David Teigland6d40c4a2012-04-23 16:36:01 -05003590 if (error)
3591 return error;
David Teiglande7fd4172006-01-18 09:30:29 +00003592
3593 r = lkb->lkb_resource;
3594
3595 hold_rsb(r);
3596 lock_rsb(r);
3597
David Teiglandc54e04b2008-01-09 09:59:41 -06003598 error = validate_message(lkb, ms);
3599 if (error)
3600 goto out;
3601
David Teiglande7fd4172006-01-18 09:30:29 +00003602 receive_flags_reply(lkb, ms);
David Teigland7d3c1fe2007-04-19 10:30:41 -05003603 if (is_altmode(lkb))
3604 munge_altmode(lkb, ms);
David Teiglande7fd4172006-01-18 09:30:29 +00003605 grant_lock_pc(r, lkb, ms);
3606 queue_cast(r, lkb, 0);
David Teiglandc54e04b2008-01-09 09:59:41 -06003607 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003608 unlock_rsb(r);
3609 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05003610 dlm_put_lkb(lkb);
David Teigland6d40c4a2012-04-23 16:36:01 -05003611 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00003612}
3613
David Teigland6d40c4a2012-04-23 16:36:01 -05003614static int receive_bast(struct dlm_ls *ls, struct dlm_message *ms)
David Teiglande7fd4172006-01-18 09:30:29 +00003615{
3616 struct dlm_lkb *lkb;
3617 struct dlm_rsb *r;
3618 int error;
3619
3620 error = find_lkb(ls, ms->m_remid, &lkb);
David Teigland6d40c4a2012-04-23 16:36:01 -05003621 if (error)
3622 return error;
David Teiglande7fd4172006-01-18 09:30:29 +00003623
3624 r = lkb->lkb_resource;
3625
3626 hold_rsb(r);
3627 lock_rsb(r);
3628
David Teiglandc54e04b2008-01-09 09:59:41 -06003629 error = validate_message(lkb, ms);
3630 if (error)
3631 goto out;
David Teiglande7fd4172006-01-18 09:30:29 +00003632
David Teiglandc54e04b2008-01-09 09:59:41 -06003633 queue_bast(r, lkb, ms->m_bastmode);
3634 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003635 unlock_rsb(r);
3636 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05003637 dlm_put_lkb(lkb);
David Teigland6d40c4a2012-04-23 16:36:01 -05003638 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00003639}
3640
3641static void receive_lookup(struct dlm_ls *ls, struct dlm_message *ms)
3642{
3643 int len, error, ret_nodeid, dir_nodeid, from_nodeid, our_nodeid;
3644
3645 from_nodeid = ms->m_header.h_nodeid;
3646 our_nodeid = dlm_our_nodeid();
3647
3648 len = receive_extralen(ms);
3649
3650 dir_nodeid = dlm_hash2nodeid(ls, ms->m_hash);
3651 if (dir_nodeid != our_nodeid) {
3652 log_error(ls, "lookup dir_nodeid %d from %d",
3653 dir_nodeid, from_nodeid);
3654 error = -EINVAL;
3655 ret_nodeid = -1;
3656 goto out;
3657 }
3658
3659 error = dlm_dir_lookup(ls, from_nodeid, ms->m_extra, len, &ret_nodeid);
3660
3661 /* Optimization: we're master so treat lookup as a request */
3662 if (!error && ret_nodeid == our_nodeid) {
3663 receive_request(ls, ms);
3664 return;
3665 }
3666 out:
3667 send_lookup_reply(ls, ms, ret_nodeid, error);
3668}
3669
3670static void receive_remove(struct dlm_ls *ls, struct dlm_message *ms)
3671{
3672 int len, dir_nodeid, from_nodeid;
3673
3674 from_nodeid = ms->m_header.h_nodeid;
3675
3676 len = receive_extralen(ms);
3677
3678 dir_nodeid = dlm_hash2nodeid(ls, ms->m_hash);
3679 if (dir_nodeid != dlm_our_nodeid()) {
3680 log_error(ls, "remove dir entry dir_nodeid %d from %d",
3681 dir_nodeid, from_nodeid);
3682 return;
3683 }
3684
3685 dlm_dir_remove_entry(ls, from_nodeid, ms->m_extra, len);
3686}
3687
David Teigland84991372007-03-30 15:02:40 -05003688static void receive_purge(struct dlm_ls *ls, struct dlm_message *ms)
3689{
3690 do_purge(ls, ms->m_nodeid, ms->m_pid);
3691}
3692
David Teigland6d40c4a2012-04-23 16:36:01 -05003693static int receive_request_reply(struct dlm_ls *ls, struct dlm_message *ms)
David Teiglande7fd4172006-01-18 09:30:29 +00003694{
3695 struct dlm_lkb *lkb;
3696 struct dlm_rsb *r;
David Teiglandef0c2bb2007-03-28 09:56:46 -05003697 int error, mstype, result;
David Teiglande7fd4172006-01-18 09:30:29 +00003698
3699 error = find_lkb(ls, ms->m_remid, &lkb);
David Teigland6d40c4a2012-04-23 16:36:01 -05003700 if (error)
3701 return error;
David Teiglande7fd4172006-01-18 09:30:29 +00003702
David Teiglande7fd4172006-01-18 09:30:29 +00003703 r = lkb->lkb_resource;
3704 hold_rsb(r);
3705 lock_rsb(r);
3706
David Teiglandc54e04b2008-01-09 09:59:41 -06003707 error = validate_message(lkb, ms);
3708 if (error)
3709 goto out;
3710
David Teiglandef0c2bb2007-03-28 09:56:46 -05003711 mstype = lkb->lkb_wait_type;
3712 error = remove_from_waiters(lkb, DLM_MSG_REQUEST_REPLY);
3713 if (error)
3714 goto out;
3715
David Teiglande7fd4172006-01-18 09:30:29 +00003716 /* Optimization: the dir node was also the master, so it took our
3717 lookup as a request and sent request reply instead of lookup reply */
3718 if (mstype == DLM_MSG_LOOKUP) {
3719 r->res_nodeid = ms->m_header.h_nodeid;
3720 lkb->lkb_nodeid = r->res_nodeid;
3721 }
3722
David Teiglandef0c2bb2007-03-28 09:56:46 -05003723 /* this is the value returned from do_request() on the master */
3724 result = ms->m_result;
3725
3726 switch (result) {
David Teiglande7fd4172006-01-18 09:30:29 +00003727 case -EAGAIN:
David Teiglandef0c2bb2007-03-28 09:56:46 -05003728 /* request would block (be queued) on remote master */
David Teiglande7fd4172006-01-18 09:30:29 +00003729 queue_cast(r, lkb, -EAGAIN);
3730 confirm_master(r, -EAGAIN);
David Teiglandef0c2bb2007-03-28 09:56:46 -05003731 unhold_lkb(lkb); /* undoes create_lkb() */
David Teiglande7fd4172006-01-18 09:30:29 +00003732 break;
3733
3734 case -EINPROGRESS:
3735 case 0:
3736 /* request was queued or granted on remote master */
3737 receive_flags_reply(lkb, ms);
3738 lkb->lkb_remid = ms->m_lkid;
David Teigland7d3c1fe2007-04-19 10:30:41 -05003739 if (is_altmode(lkb))
3740 munge_altmode(lkb, ms);
David Teigland3ae1acf2007-05-18 08:59:31 -05003741 if (result) {
David Teiglande7fd4172006-01-18 09:30:29 +00003742 add_lkb(r, lkb, DLM_LKSTS_WAITING);
David Teigland3ae1acf2007-05-18 08:59:31 -05003743 add_timeout(lkb);
3744 } else {
David Teiglande7fd4172006-01-18 09:30:29 +00003745 grant_lock_pc(r, lkb, ms);
3746 queue_cast(r, lkb, 0);
3747 }
David Teiglandef0c2bb2007-03-28 09:56:46 -05003748 confirm_master(r, result);
David Teiglande7fd4172006-01-18 09:30:29 +00003749 break;
3750
David Teigland597d0ca2006-07-12 16:44:04 -05003751 case -EBADR:
David Teiglande7fd4172006-01-18 09:30:29 +00003752 case -ENOTBLK:
3753 /* find_rsb failed to find rsb or rsb wasn't master */
David Teiglandef0c2bb2007-03-28 09:56:46 -05003754 log_debug(ls, "receive_request_reply %x %x master diff %d %d",
3755 lkb->lkb_id, lkb->lkb_flags, r->res_nodeid, result);
David Teiglande7fd4172006-01-18 09:30:29 +00003756 r->res_nodeid = -1;
3757 lkb->lkb_nodeid = -1;
David Teiglandef0c2bb2007-03-28 09:56:46 -05003758
3759 if (is_overlap(lkb)) {
3760 /* we'll ignore error in cancel/unlock reply */
3761 queue_cast_overlap(r, lkb);
David Teiglandaec64e12008-01-08 15:37:47 -06003762 confirm_master(r, result);
David Teiglandef0c2bb2007-03-28 09:56:46 -05003763 unhold_lkb(lkb); /* undoes create_lkb() */
3764 } else
3765 _request_lock(r, lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003766 break;
3767
3768 default:
David Teiglandef0c2bb2007-03-28 09:56:46 -05003769 log_error(ls, "receive_request_reply %x error %d",
3770 lkb->lkb_id, result);
David Teiglande7fd4172006-01-18 09:30:29 +00003771 }
3772
David Teiglandef0c2bb2007-03-28 09:56:46 -05003773 if (is_overlap_unlock(lkb) && (result == 0 || result == -EINPROGRESS)) {
3774 log_debug(ls, "receive_request_reply %x result %d unlock",
3775 lkb->lkb_id, result);
3776 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
3777 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
3778 send_unlock(r, lkb);
3779 } else if (is_overlap_cancel(lkb) && (result == -EINPROGRESS)) {
3780 log_debug(ls, "receive_request_reply %x cancel", lkb->lkb_id);
3781 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
3782 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
3783 send_cancel(r, lkb);
3784 } else {
3785 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
3786 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
3787 }
3788 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003789 unlock_rsb(r);
3790 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05003791 dlm_put_lkb(lkb);
David Teigland6d40c4a2012-04-23 16:36:01 -05003792 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00003793}
3794
3795static void __receive_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
3796 struct dlm_message *ms)
3797{
David Teiglande7fd4172006-01-18 09:30:29 +00003798 /* this is the value returned from do_convert() on the master */
David Teiglandef0c2bb2007-03-28 09:56:46 -05003799 switch (ms->m_result) {
David Teiglande7fd4172006-01-18 09:30:29 +00003800 case -EAGAIN:
3801 /* convert would block (be queued) on remote master */
3802 queue_cast(r, lkb, -EAGAIN);
3803 break;
3804
David Teiglandc85d65e2007-05-18 09:01:26 -05003805 case -EDEADLK:
3806 receive_flags_reply(lkb, ms);
3807 revert_lock_pc(r, lkb);
3808 queue_cast(r, lkb, -EDEADLK);
3809 break;
3810
David Teiglande7fd4172006-01-18 09:30:29 +00003811 case -EINPROGRESS:
3812 /* convert was queued on remote master */
David Teigland7d3c1fe2007-04-19 10:30:41 -05003813 receive_flags_reply(lkb, ms);
3814 if (is_demoted(lkb))
David Teigland2a7ce0e2011-04-04 15:19:59 -05003815 munge_demoted(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003816 del_lkb(r, lkb);
3817 add_lkb(r, lkb, DLM_LKSTS_CONVERT);
David Teigland3ae1acf2007-05-18 08:59:31 -05003818 add_timeout(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003819 break;
3820
3821 case 0:
3822 /* convert was granted on remote master */
3823 receive_flags_reply(lkb, ms);
David Teigland7d3c1fe2007-04-19 10:30:41 -05003824 if (is_demoted(lkb))
David Teigland2a7ce0e2011-04-04 15:19:59 -05003825 munge_demoted(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003826 grant_lock_pc(r, lkb, ms);
3827 queue_cast(r, lkb, 0);
3828 break;
3829
3830 default:
David Teigland6d40c4a2012-04-23 16:36:01 -05003831 log_error(r->res_ls, "receive_convert_reply %x remote %d %x %d",
3832 lkb->lkb_id, ms->m_header.h_nodeid, ms->m_lkid,
3833 ms->m_result);
3834 dlm_print_rsb(r);
3835 dlm_print_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003836 }
3837}
3838
3839static void _receive_convert_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
3840{
3841 struct dlm_rsb *r = lkb->lkb_resource;
David Teiglandef0c2bb2007-03-28 09:56:46 -05003842 int error;
David Teiglande7fd4172006-01-18 09:30:29 +00003843
3844 hold_rsb(r);
3845 lock_rsb(r);
3846
David Teiglandc54e04b2008-01-09 09:59:41 -06003847 error = validate_message(lkb, ms);
3848 if (error)
3849 goto out;
3850
David Teiglandef0c2bb2007-03-28 09:56:46 -05003851 /* stub reply can happen with waiters_mutex held */
3852 error = remove_from_waiters_ms(lkb, ms);
3853 if (error)
3854 goto out;
David Teiglande7fd4172006-01-18 09:30:29 +00003855
David Teiglandef0c2bb2007-03-28 09:56:46 -05003856 __receive_convert_reply(r, lkb, ms);
3857 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003858 unlock_rsb(r);
3859 put_rsb(r);
3860}
3861
David Teigland6d40c4a2012-04-23 16:36:01 -05003862static int receive_convert_reply(struct dlm_ls *ls, struct dlm_message *ms)
David Teiglande7fd4172006-01-18 09:30:29 +00003863{
3864 struct dlm_lkb *lkb;
3865 int error;
3866
3867 error = find_lkb(ls, ms->m_remid, &lkb);
David Teigland6d40c4a2012-04-23 16:36:01 -05003868 if (error)
3869 return error;
David Teiglande7fd4172006-01-18 09:30:29 +00003870
David Teiglande7fd4172006-01-18 09:30:29 +00003871 _receive_convert_reply(lkb, ms);
David Teiglandb3f58d82006-02-28 11:16:37 -05003872 dlm_put_lkb(lkb);
David Teigland6d40c4a2012-04-23 16:36:01 -05003873 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00003874}
3875
3876static void _receive_unlock_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
3877{
3878 struct dlm_rsb *r = lkb->lkb_resource;
David Teiglandef0c2bb2007-03-28 09:56:46 -05003879 int error;
David Teiglande7fd4172006-01-18 09:30:29 +00003880
3881 hold_rsb(r);
3882 lock_rsb(r);
3883
David Teiglandc54e04b2008-01-09 09:59:41 -06003884 error = validate_message(lkb, ms);
3885 if (error)
3886 goto out;
3887
David Teiglandef0c2bb2007-03-28 09:56:46 -05003888 /* stub reply can happen with waiters_mutex held */
3889 error = remove_from_waiters_ms(lkb, ms);
3890 if (error)
3891 goto out;
3892
David Teiglande7fd4172006-01-18 09:30:29 +00003893 /* this is the value returned from do_unlock() on the master */
3894
David Teiglandef0c2bb2007-03-28 09:56:46 -05003895 switch (ms->m_result) {
David Teiglande7fd4172006-01-18 09:30:29 +00003896 case -DLM_EUNLOCK:
3897 receive_flags_reply(lkb, ms);
3898 remove_lock_pc(r, lkb);
3899 queue_cast(r, lkb, -DLM_EUNLOCK);
3900 break;
David Teiglandef0c2bb2007-03-28 09:56:46 -05003901 case -ENOENT:
3902 break;
David Teiglande7fd4172006-01-18 09:30:29 +00003903 default:
David Teiglandef0c2bb2007-03-28 09:56:46 -05003904 log_error(r->res_ls, "receive_unlock_reply %x error %d",
3905 lkb->lkb_id, ms->m_result);
David Teiglande7fd4172006-01-18 09:30:29 +00003906 }
David Teiglandef0c2bb2007-03-28 09:56:46 -05003907 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003908 unlock_rsb(r);
3909 put_rsb(r);
3910}
3911
David Teigland6d40c4a2012-04-23 16:36:01 -05003912static int receive_unlock_reply(struct dlm_ls *ls, struct dlm_message *ms)
David Teiglande7fd4172006-01-18 09:30:29 +00003913{
3914 struct dlm_lkb *lkb;
3915 int error;
3916
3917 error = find_lkb(ls, ms->m_remid, &lkb);
David Teigland6d40c4a2012-04-23 16:36:01 -05003918 if (error)
3919 return error;
David Teiglande7fd4172006-01-18 09:30:29 +00003920
David Teiglande7fd4172006-01-18 09:30:29 +00003921 _receive_unlock_reply(lkb, ms);
David Teiglandb3f58d82006-02-28 11:16:37 -05003922 dlm_put_lkb(lkb);
David Teigland6d40c4a2012-04-23 16:36:01 -05003923 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00003924}
3925
3926static void _receive_cancel_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
3927{
3928 struct dlm_rsb *r = lkb->lkb_resource;
David Teiglandef0c2bb2007-03-28 09:56:46 -05003929 int error;
David Teiglande7fd4172006-01-18 09:30:29 +00003930
3931 hold_rsb(r);
3932 lock_rsb(r);
3933
David Teiglandc54e04b2008-01-09 09:59:41 -06003934 error = validate_message(lkb, ms);
3935 if (error)
3936 goto out;
3937
David Teiglandef0c2bb2007-03-28 09:56:46 -05003938 /* stub reply can happen with waiters_mutex held */
3939 error = remove_from_waiters_ms(lkb, ms);
3940 if (error)
3941 goto out;
3942
David Teiglande7fd4172006-01-18 09:30:29 +00003943 /* this is the value returned from do_cancel() on the master */
3944
David Teiglandef0c2bb2007-03-28 09:56:46 -05003945 switch (ms->m_result) {
David Teiglande7fd4172006-01-18 09:30:29 +00003946 case -DLM_ECANCEL:
3947 receive_flags_reply(lkb, ms);
3948 revert_lock_pc(r, lkb);
David Teigland84d8cd62007-05-29 08:44:23 -05003949 queue_cast(r, lkb, -DLM_ECANCEL);
David Teiglandef0c2bb2007-03-28 09:56:46 -05003950 break;
3951 case 0:
David Teiglande7fd4172006-01-18 09:30:29 +00003952 break;
3953 default:
David Teiglandef0c2bb2007-03-28 09:56:46 -05003954 log_error(r->res_ls, "receive_cancel_reply %x error %d",
3955 lkb->lkb_id, ms->m_result);
David Teiglande7fd4172006-01-18 09:30:29 +00003956 }
David Teiglandef0c2bb2007-03-28 09:56:46 -05003957 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003958 unlock_rsb(r);
3959 put_rsb(r);
3960}
3961
David Teigland6d40c4a2012-04-23 16:36:01 -05003962static int receive_cancel_reply(struct dlm_ls *ls, struct dlm_message *ms)
David Teiglande7fd4172006-01-18 09:30:29 +00003963{
3964 struct dlm_lkb *lkb;
3965 int error;
3966
3967 error = find_lkb(ls, ms->m_remid, &lkb);
David Teigland6d40c4a2012-04-23 16:36:01 -05003968 if (error)
3969 return error;
David Teiglande7fd4172006-01-18 09:30:29 +00003970
David Teiglande7fd4172006-01-18 09:30:29 +00003971 _receive_cancel_reply(lkb, ms);
David Teiglandb3f58d82006-02-28 11:16:37 -05003972 dlm_put_lkb(lkb);
David Teigland6d40c4a2012-04-23 16:36:01 -05003973 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00003974}
3975
3976static void receive_lookup_reply(struct dlm_ls *ls, struct dlm_message *ms)
3977{
3978 struct dlm_lkb *lkb;
3979 struct dlm_rsb *r;
3980 int error, ret_nodeid;
3981
3982 error = find_lkb(ls, ms->m_lkid, &lkb);
3983 if (error) {
David Teigland6d40c4a2012-04-23 16:36:01 -05003984 log_error(ls, "receive_lookup_reply no lkid %x", ms->m_lkid);
David Teiglande7fd4172006-01-18 09:30:29 +00003985 return;
3986 }
3987
David Teiglandef0c2bb2007-03-28 09:56:46 -05003988 /* ms->m_result is the value returned by dlm_dir_lookup on dir node
David Teiglande7fd4172006-01-18 09:30:29 +00003989 FIXME: will a non-zero error ever be returned? */
David Teiglande7fd4172006-01-18 09:30:29 +00003990
3991 r = lkb->lkb_resource;
3992 hold_rsb(r);
3993 lock_rsb(r);
3994
David Teiglandef0c2bb2007-03-28 09:56:46 -05003995 error = remove_from_waiters(lkb, DLM_MSG_LOOKUP_REPLY);
3996 if (error)
3997 goto out;
3998
David Teiglande7fd4172006-01-18 09:30:29 +00003999 ret_nodeid = ms->m_nodeid;
4000 if (ret_nodeid == dlm_our_nodeid()) {
4001 r->res_nodeid = 0;
4002 ret_nodeid = 0;
4003 r->res_first_lkid = 0;
4004 } else {
4005 /* set_master() will copy res_nodeid to lkb_nodeid */
4006 r->res_nodeid = ret_nodeid;
4007 }
4008
David Teiglandef0c2bb2007-03-28 09:56:46 -05004009 if (is_overlap(lkb)) {
4010 log_debug(ls, "receive_lookup_reply %x unlock %x",
4011 lkb->lkb_id, lkb->lkb_flags);
4012 queue_cast_overlap(r, lkb);
4013 unhold_lkb(lkb); /* undoes create_lkb() */
4014 goto out_list;
4015 }
4016
David Teiglande7fd4172006-01-18 09:30:29 +00004017 _request_lock(r, lkb);
4018
David Teiglandef0c2bb2007-03-28 09:56:46 -05004019 out_list:
David Teiglande7fd4172006-01-18 09:30:29 +00004020 if (!ret_nodeid)
4021 process_lookup_list(r);
David Teiglandef0c2bb2007-03-28 09:56:46 -05004022 out:
David Teiglande7fd4172006-01-18 09:30:29 +00004023 unlock_rsb(r);
4024 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05004025 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00004026}
4027
David Teigland6d40c4a2012-04-23 16:36:01 -05004028static void _receive_message(struct dlm_ls *ls, struct dlm_message *ms,
4029 uint32_t saved_seq)
David Teiglande7fd4172006-01-18 09:30:29 +00004030{
David Teigland6d40c4a2012-04-23 16:36:01 -05004031 int error = 0, noent = 0;
4032
David Teigland46b43ee2008-01-08 16:24:00 -06004033 if (!dlm_is_member(ls, ms->m_header.h_nodeid)) {
4034 log_debug(ls, "ignore non-member message %d from %d %x %x %d",
4035 ms->m_type, ms->m_header.h_nodeid, ms->m_lkid,
4036 ms->m_remid, ms->m_result);
4037 return;
4038 }
4039
David Teiglande7fd4172006-01-18 09:30:29 +00004040 switch (ms->m_type) {
4041
4042 /* messages sent to a master node */
4043
4044 case DLM_MSG_REQUEST:
David Teigland6d40c4a2012-04-23 16:36:01 -05004045 error = receive_request(ls, ms);
David Teiglande7fd4172006-01-18 09:30:29 +00004046 break;
4047
4048 case DLM_MSG_CONVERT:
David Teigland6d40c4a2012-04-23 16:36:01 -05004049 error = receive_convert(ls, ms);
David Teiglande7fd4172006-01-18 09:30:29 +00004050 break;
4051
4052 case DLM_MSG_UNLOCK:
David Teigland6d40c4a2012-04-23 16:36:01 -05004053 error = receive_unlock(ls, ms);
David Teiglande7fd4172006-01-18 09:30:29 +00004054 break;
4055
4056 case DLM_MSG_CANCEL:
David Teigland6d40c4a2012-04-23 16:36:01 -05004057 noent = 1;
4058 error = receive_cancel(ls, ms);
David Teiglande7fd4172006-01-18 09:30:29 +00004059 break;
4060
4061 /* messages sent from a master node (replies to above) */
4062
4063 case DLM_MSG_REQUEST_REPLY:
David Teigland6d40c4a2012-04-23 16:36:01 -05004064 error = receive_request_reply(ls, ms);
David Teiglande7fd4172006-01-18 09:30:29 +00004065 break;
4066
4067 case DLM_MSG_CONVERT_REPLY:
David Teigland6d40c4a2012-04-23 16:36:01 -05004068 error = receive_convert_reply(ls, ms);
David Teiglande7fd4172006-01-18 09:30:29 +00004069 break;
4070
4071 case DLM_MSG_UNLOCK_REPLY:
David Teigland6d40c4a2012-04-23 16:36:01 -05004072 error = receive_unlock_reply(ls, ms);
David Teiglande7fd4172006-01-18 09:30:29 +00004073 break;
4074
4075 case DLM_MSG_CANCEL_REPLY:
David Teigland6d40c4a2012-04-23 16:36:01 -05004076 error = receive_cancel_reply(ls, ms);
David Teiglande7fd4172006-01-18 09:30:29 +00004077 break;
4078
4079 /* messages sent from a master node (only two types of async msg) */
4080
4081 case DLM_MSG_GRANT:
David Teigland6d40c4a2012-04-23 16:36:01 -05004082 noent = 1;
4083 error = receive_grant(ls, ms);
David Teiglande7fd4172006-01-18 09:30:29 +00004084 break;
4085
4086 case DLM_MSG_BAST:
David Teigland6d40c4a2012-04-23 16:36:01 -05004087 noent = 1;
4088 error = receive_bast(ls, ms);
David Teiglande7fd4172006-01-18 09:30:29 +00004089 break;
4090
4091 /* messages sent to a dir node */
4092
4093 case DLM_MSG_LOOKUP:
4094 receive_lookup(ls, ms);
4095 break;
4096
4097 case DLM_MSG_REMOVE:
4098 receive_remove(ls, ms);
4099 break;
4100
4101 /* messages sent from a dir node (remove has no reply) */
4102
4103 case DLM_MSG_LOOKUP_REPLY:
4104 receive_lookup_reply(ls, ms);
4105 break;
4106
David Teigland84991372007-03-30 15:02:40 -05004107 /* other messages */
4108
4109 case DLM_MSG_PURGE:
4110 receive_purge(ls, ms);
4111 break;
4112
David Teiglande7fd4172006-01-18 09:30:29 +00004113 default:
4114 log_error(ls, "unknown message type %d", ms->m_type);
4115 }
David Teigland6d40c4a2012-04-23 16:36:01 -05004116
4117 /*
4118 * When checking for ENOENT, we're checking the result of
4119 * find_lkb(m_remid):
4120 *
4121 * The lock id referenced in the message wasn't found. This may
4122 * happen in normal usage for the async messages and cancel, so
4123 * only use log_debug for them.
4124 *
4125 * Other errors are expected and normal.
4126 */
4127
4128 if (error == -ENOENT && noent) {
4129 log_debug(ls, "receive %d no %x remote %d %x seq %u",
4130 ms->m_type, ms->m_remid, ms->m_header.h_nodeid,
4131 ms->m_lkid, saved_seq);
4132 } else if (error == -ENOENT) {
4133 log_error(ls, "receive %d no %x remote %d %x seq %u",
4134 ms->m_type, ms->m_remid, ms->m_header.h_nodeid,
4135 ms->m_lkid, saved_seq);
4136
4137 if (ms->m_type == DLM_MSG_CONVERT)
4138 dlm_dump_rsb_hash(ls, ms->m_hash);
4139 }
David Teiglande7fd4172006-01-18 09:30:29 +00004140}
4141
David Teiglandc36258b2007-09-27 15:53:38 -05004142/* If the lockspace is in recovery mode (locking stopped), then normal
4143 messages are saved on the requestqueue for processing after recovery is
4144 done. When not in recovery mode, we wait for dlm_recoverd to drain saved
4145 messages off the requestqueue before we process new ones. This occurs right
4146 after recovery completes when we transition from saving all messages on
4147 requestqueue, to processing all the saved messages, to processing new
4148 messages as they arrive. */
David Teiglande7fd4172006-01-18 09:30:29 +00004149
David Teiglandc36258b2007-09-27 15:53:38 -05004150static void dlm_receive_message(struct dlm_ls *ls, struct dlm_message *ms,
4151 int nodeid)
4152{
4153 if (dlm_locking_stopped(ls)) {
Al Viro8b0d8e02008-01-25 00:28:28 -05004154 dlm_add_requestqueue(ls, nodeid, ms);
David Teiglandc36258b2007-09-27 15:53:38 -05004155 } else {
4156 dlm_wait_requestqueue(ls);
David Teigland6d40c4a2012-04-23 16:36:01 -05004157 _receive_message(ls, ms, 0);
David Teiglandc36258b2007-09-27 15:53:38 -05004158 }
4159}
4160
4161/* This is called by dlm_recoverd to process messages that were saved on
4162 the requestqueue. */
4163
David Teigland6d40c4a2012-04-23 16:36:01 -05004164void dlm_receive_message_saved(struct dlm_ls *ls, struct dlm_message *ms,
4165 uint32_t saved_seq)
David Teiglandc36258b2007-09-27 15:53:38 -05004166{
David Teigland6d40c4a2012-04-23 16:36:01 -05004167 _receive_message(ls, ms, saved_seq);
David Teiglandc36258b2007-09-27 15:53:38 -05004168}
4169
4170/* This is called by the midcomms layer when something is received for
4171 the lockspace. It could be either a MSG (normal message sent as part of
4172 standard locking activity) or an RCOM (recovery message sent as part of
4173 lockspace recovery). */
4174
Al Viroeef7d732008-01-25 00:58:46 -05004175void dlm_receive_buffer(union dlm_packet *p, int nodeid)
David Teiglandc36258b2007-09-27 15:53:38 -05004176{
Al Viroeef7d732008-01-25 00:58:46 -05004177 struct dlm_header *hd = &p->header;
David Teiglandc36258b2007-09-27 15:53:38 -05004178 struct dlm_ls *ls;
4179 int type = 0;
4180
4181 switch (hd->h_cmd) {
4182 case DLM_MSG:
Al Viroeef7d732008-01-25 00:58:46 -05004183 dlm_message_in(&p->message);
4184 type = p->message.m_type;
David Teiglandc36258b2007-09-27 15:53:38 -05004185 break;
4186 case DLM_RCOM:
Al Viroeef7d732008-01-25 00:58:46 -05004187 dlm_rcom_in(&p->rcom);
4188 type = p->rcom.rc_type;
David Teiglandc36258b2007-09-27 15:53:38 -05004189 break;
4190 default:
4191 log_print("invalid h_cmd %d from %u", hd->h_cmd, nodeid);
4192 return;
4193 }
4194
4195 if (hd->h_nodeid != nodeid) {
4196 log_print("invalid h_nodeid %d from %d lockspace %x",
4197 hd->h_nodeid, nodeid, hd->h_lockspace);
4198 return;
4199 }
4200
4201 ls = dlm_find_lockspace_global(hd->h_lockspace);
4202 if (!ls) {
David Teigland594199e2008-01-16 11:03:41 -06004203 if (dlm_config.ci_log_debug)
4204 log_print("invalid lockspace %x from %d cmd %d type %d",
4205 hd->h_lockspace, nodeid, hd->h_cmd, type);
David Teiglandc36258b2007-09-27 15:53:38 -05004206
4207 if (hd->h_cmd == DLM_RCOM && type == DLM_RCOM_STATUS)
Al Viroeef7d732008-01-25 00:58:46 -05004208 dlm_send_ls_not_ready(nodeid, &p->rcom);
David Teiglandc36258b2007-09-27 15:53:38 -05004209 return;
4210 }
4211
4212 /* this rwsem allows dlm_ls_stop() to wait for all dlm_recv threads to
4213 be inactive (in this ls) before transitioning to recovery mode */
4214
4215 down_read(&ls->ls_recv_active);
4216 if (hd->h_cmd == DLM_MSG)
Al Viroeef7d732008-01-25 00:58:46 -05004217 dlm_receive_message(ls, &p->message, nodeid);
David Teiglandc36258b2007-09-27 15:53:38 -05004218 else
Al Viroeef7d732008-01-25 00:58:46 -05004219 dlm_receive_rcom(ls, &p->rcom, nodeid);
David Teiglandc36258b2007-09-27 15:53:38 -05004220 up_read(&ls->ls_recv_active);
4221
4222 dlm_put_lockspace(ls);
4223}
David Teiglande7fd4172006-01-18 09:30:29 +00004224
David Teigland2a7ce0e2011-04-04 15:19:59 -05004225static void recover_convert_waiter(struct dlm_ls *ls, struct dlm_lkb *lkb,
4226 struct dlm_message *ms_stub)
David Teiglande7fd4172006-01-18 09:30:29 +00004227{
4228 if (middle_conversion(lkb)) {
4229 hold_lkb(lkb);
David Teigland2a7ce0e2011-04-04 15:19:59 -05004230 memset(ms_stub, 0, sizeof(struct dlm_message));
4231 ms_stub->m_flags = DLM_IFL_STUB_MS;
4232 ms_stub->m_type = DLM_MSG_CONVERT_REPLY;
4233 ms_stub->m_result = -EINPROGRESS;
4234 ms_stub->m_header.h_nodeid = lkb->lkb_nodeid;
4235 _receive_convert_reply(lkb, ms_stub);
David Teiglande7fd4172006-01-18 09:30:29 +00004236
4237 /* Same special case as in receive_rcom_lock_args() */
4238 lkb->lkb_grmode = DLM_LOCK_IV;
4239 rsb_set_flag(lkb->lkb_resource, RSB_RECOVER_CONVERT);
4240 unhold_lkb(lkb);
4241
4242 } else if (lkb->lkb_rqmode >= lkb->lkb_grmode) {
4243 lkb->lkb_flags |= DLM_IFL_RESEND;
4244 }
4245
4246 /* lkb->lkb_rqmode < lkb->lkb_grmode shouldn't happen since down
4247 conversions are async; there's no reply from the remote master */
4248}
4249
4250/* A waiting lkb needs recovery if the master node has failed, or
4251 the master node is changing (only when no directory is used) */
4252
David Teigland13ef1112012-04-23 12:18:18 -05004253static int waiter_needs_recovery(struct dlm_ls *ls, struct dlm_lkb *lkb,
4254 int dir_nodeid)
David Teiglande7fd4172006-01-18 09:30:29 +00004255{
David Teigland13ef1112012-04-23 12:18:18 -05004256 if (dlm_is_removed(ls, lkb->lkb_wait_nodeid))
David Teiglande7fd4172006-01-18 09:30:29 +00004257 return 1;
4258
4259 if (!dlm_no_directory(ls))
4260 return 0;
4261
David Teigland13ef1112012-04-23 12:18:18 -05004262 if (dir_nodeid == dlm_our_nodeid())
4263 return 1;
4264
4265 if (dir_nodeid != lkb->lkb_wait_nodeid)
David Teiglande7fd4172006-01-18 09:30:29 +00004266 return 1;
4267
4268 return 0;
4269}
4270
4271/* Recovery for locks that are waiting for replies from nodes that are now
4272 gone. We can just complete unlocks and cancels by faking a reply from the
4273 dead node. Requests and up-conversions we flag to be resent after
4274 recovery. Down-conversions can just be completed with a fake reply like
4275 unlocks. Conversions between PR and CW need special attention. */
4276
4277void dlm_recover_waiters_pre(struct dlm_ls *ls)
4278{
4279 struct dlm_lkb *lkb, *safe;
David Teigland2a7ce0e2011-04-04 15:19:59 -05004280 struct dlm_message *ms_stub;
David Teigland601342c2008-01-07 16:15:05 -06004281 int wait_type, stub_unlock_result, stub_cancel_result;
David Teigland13ef1112012-04-23 12:18:18 -05004282 int dir_nodeid;
David Teiglande7fd4172006-01-18 09:30:29 +00004283
David Teiglanda22ca482011-07-11 08:40:53 -05004284 ms_stub = kmalloc(sizeof(struct dlm_message), GFP_KERNEL);
David Teigland2a7ce0e2011-04-04 15:19:59 -05004285 if (!ms_stub) {
4286 log_error(ls, "dlm_recover_waiters_pre no mem");
4287 return;
4288 }
4289
David Teigland90135922006-01-20 08:47:07 +00004290 mutex_lock(&ls->ls_waiters_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +00004291
4292 list_for_each_entry_safe(lkb, safe, &ls->ls_waiters, lkb_wait_reply) {
David Teigland2a7ce0e2011-04-04 15:19:59 -05004293
David Teigland13ef1112012-04-23 12:18:18 -05004294 dir_nodeid = dlm_dir_nodeid(lkb->lkb_resource);
4295
David Teigland2a7ce0e2011-04-04 15:19:59 -05004296 /* exclude debug messages about unlocks because there can be so
4297 many and they aren't very interesting */
4298
4299 if (lkb->lkb_wait_type != DLM_MSG_UNLOCK) {
David Teigland13ef1112012-04-23 12:18:18 -05004300 log_debug(ls, "waiter %x remote %x msg %d r_nodeid %d "
4301 "lkb_nodeid %d wait_nodeid %d dir_nodeid %d",
4302 lkb->lkb_id,
4303 lkb->lkb_remid,
4304 lkb->lkb_wait_type,
4305 lkb->lkb_resource->res_nodeid,
4306 lkb->lkb_nodeid,
4307 lkb->lkb_wait_nodeid,
4308 dir_nodeid);
David Teigland2a7ce0e2011-04-04 15:19:59 -05004309 }
David Teiglande7fd4172006-01-18 09:30:29 +00004310
4311 /* all outstanding lookups, regardless of destination will be
4312 resent after recovery is done */
4313
4314 if (lkb->lkb_wait_type == DLM_MSG_LOOKUP) {
4315 lkb->lkb_flags |= DLM_IFL_RESEND;
4316 continue;
4317 }
4318
David Teigland13ef1112012-04-23 12:18:18 -05004319 if (!waiter_needs_recovery(ls, lkb, dir_nodeid))
David Teiglande7fd4172006-01-18 09:30:29 +00004320 continue;
4321
David Teigland601342c2008-01-07 16:15:05 -06004322 wait_type = lkb->lkb_wait_type;
4323 stub_unlock_result = -DLM_EUNLOCK;
4324 stub_cancel_result = -DLM_ECANCEL;
4325
4326 /* Main reply may have been received leaving a zero wait_type,
4327 but a reply for the overlapping op may not have been
4328 received. In that case we need to fake the appropriate
4329 reply for the overlap op. */
4330
4331 if (!wait_type) {
4332 if (is_overlap_cancel(lkb)) {
4333 wait_type = DLM_MSG_CANCEL;
4334 if (lkb->lkb_grmode == DLM_LOCK_IV)
4335 stub_cancel_result = 0;
4336 }
4337 if (is_overlap_unlock(lkb)) {
4338 wait_type = DLM_MSG_UNLOCK;
4339 if (lkb->lkb_grmode == DLM_LOCK_IV)
4340 stub_unlock_result = -ENOENT;
4341 }
4342
4343 log_debug(ls, "rwpre overlap %x %x %d %d %d",
4344 lkb->lkb_id, lkb->lkb_flags, wait_type,
4345 stub_cancel_result, stub_unlock_result);
4346 }
4347
4348 switch (wait_type) {
David Teiglande7fd4172006-01-18 09:30:29 +00004349
4350 case DLM_MSG_REQUEST:
4351 lkb->lkb_flags |= DLM_IFL_RESEND;
4352 break;
4353
4354 case DLM_MSG_CONVERT:
David Teigland2a7ce0e2011-04-04 15:19:59 -05004355 recover_convert_waiter(ls, lkb, ms_stub);
David Teiglande7fd4172006-01-18 09:30:29 +00004356 break;
4357
4358 case DLM_MSG_UNLOCK:
4359 hold_lkb(lkb);
David Teigland2a7ce0e2011-04-04 15:19:59 -05004360 memset(ms_stub, 0, sizeof(struct dlm_message));
4361 ms_stub->m_flags = DLM_IFL_STUB_MS;
4362 ms_stub->m_type = DLM_MSG_UNLOCK_REPLY;
4363 ms_stub->m_result = stub_unlock_result;
4364 ms_stub->m_header.h_nodeid = lkb->lkb_nodeid;
4365 _receive_unlock_reply(lkb, ms_stub);
David Teiglandb3f58d82006-02-28 11:16:37 -05004366 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00004367 break;
4368
4369 case DLM_MSG_CANCEL:
4370 hold_lkb(lkb);
David Teigland2a7ce0e2011-04-04 15:19:59 -05004371 memset(ms_stub, 0, sizeof(struct dlm_message));
4372 ms_stub->m_flags = DLM_IFL_STUB_MS;
4373 ms_stub->m_type = DLM_MSG_CANCEL_REPLY;
4374 ms_stub->m_result = stub_cancel_result;
4375 ms_stub->m_header.h_nodeid = lkb->lkb_nodeid;
4376 _receive_cancel_reply(lkb, ms_stub);
David Teiglandb3f58d82006-02-28 11:16:37 -05004377 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00004378 break;
4379
4380 default:
David Teigland601342c2008-01-07 16:15:05 -06004381 log_error(ls, "invalid lkb wait_type %d %d",
4382 lkb->lkb_wait_type, wait_type);
David Teiglande7fd4172006-01-18 09:30:29 +00004383 }
David Teigland81456802006-07-25 14:05:09 -05004384 schedule();
David Teiglande7fd4172006-01-18 09:30:29 +00004385 }
David Teigland90135922006-01-20 08:47:07 +00004386 mutex_unlock(&ls->ls_waiters_mutex);
David Teigland2a7ce0e2011-04-04 15:19:59 -05004387 kfree(ms_stub);
David Teiglande7fd4172006-01-18 09:30:29 +00004388}
4389
David Teiglandef0c2bb2007-03-28 09:56:46 -05004390static struct dlm_lkb *find_resend_waiter(struct dlm_ls *ls)
David Teiglande7fd4172006-01-18 09:30:29 +00004391{
4392 struct dlm_lkb *lkb;
David Teiglandef0c2bb2007-03-28 09:56:46 -05004393 int found = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00004394
David Teigland90135922006-01-20 08:47:07 +00004395 mutex_lock(&ls->ls_waiters_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +00004396 list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
4397 if (lkb->lkb_flags & DLM_IFL_RESEND) {
David Teiglandef0c2bb2007-03-28 09:56:46 -05004398 hold_lkb(lkb);
4399 found = 1;
David Teiglande7fd4172006-01-18 09:30:29 +00004400 break;
4401 }
4402 }
David Teigland90135922006-01-20 08:47:07 +00004403 mutex_unlock(&ls->ls_waiters_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +00004404
David Teiglandef0c2bb2007-03-28 09:56:46 -05004405 if (!found)
David Teiglande7fd4172006-01-18 09:30:29 +00004406 lkb = NULL;
David Teiglandef0c2bb2007-03-28 09:56:46 -05004407 return lkb;
David Teiglande7fd4172006-01-18 09:30:29 +00004408}
4409
4410/* Deal with lookups and lkb's marked RESEND from _pre. We may now be the
4411 master or dir-node for r. Processing the lkb may result in it being placed
4412 back on waiters. */
4413
David Teiglandef0c2bb2007-03-28 09:56:46 -05004414/* We do this after normal locking has been enabled and any saved messages
4415 (in requestqueue) have been processed. We should be confident that at
4416 this point we won't get or process a reply to any of these waiting
4417 operations. But, new ops may be coming in on the rsbs/locks here from
4418 userspace or remotely. */
4419
4420/* there may have been an overlap unlock/cancel prior to recovery or after
4421 recovery. if before, the lkb may still have a pos wait_count; if after, the
4422 overlap flag would just have been set and nothing new sent. we can be
4423 confident here than any replies to either the initial op or overlap ops
4424 prior to recovery have been received. */
4425
David Teiglande7fd4172006-01-18 09:30:29 +00004426int dlm_recover_waiters_post(struct dlm_ls *ls)
4427{
4428 struct dlm_lkb *lkb;
4429 struct dlm_rsb *r;
David Teiglandef0c2bb2007-03-28 09:56:46 -05004430 int error = 0, mstype, err, oc, ou;
David Teiglande7fd4172006-01-18 09:30:29 +00004431
4432 while (1) {
4433 if (dlm_locking_stopped(ls)) {
4434 log_debug(ls, "recover_waiters_post aborted");
4435 error = -EINTR;
4436 break;
4437 }
4438
David Teiglandef0c2bb2007-03-28 09:56:46 -05004439 lkb = find_resend_waiter(ls);
4440 if (!lkb)
David Teiglande7fd4172006-01-18 09:30:29 +00004441 break;
4442
4443 r = lkb->lkb_resource;
David Teiglandef0c2bb2007-03-28 09:56:46 -05004444 hold_rsb(r);
4445 lock_rsb(r);
4446
4447 mstype = lkb->lkb_wait_type;
4448 oc = is_overlap_cancel(lkb);
4449 ou = is_overlap_unlock(lkb);
4450 err = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00004451
David Teigland13ef1112012-04-23 12:18:18 -05004452 log_debug(ls, "waiter %x remote %x msg %d r_nodeid %d "
4453 "lkb_nodeid %d wait_nodeid %d dir_nodeid %d "
4454 "overlap %d %d", lkb->lkb_id, lkb->lkb_remid, mstype,
4455 r->res_nodeid, lkb->lkb_nodeid, lkb->lkb_wait_nodeid,
4456 dlm_dir_nodeid(r), oc, ou);
David Teiglande7fd4172006-01-18 09:30:29 +00004457
David Teiglandef0c2bb2007-03-28 09:56:46 -05004458 /* At this point we assume that we won't get a reply to any
4459 previous op or overlap op on this lock. First, do a big
4460 remove_from_waiters() for all previous ops. */
David Teiglande7fd4172006-01-18 09:30:29 +00004461
David Teiglandef0c2bb2007-03-28 09:56:46 -05004462 lkb->lkb_flags &= ~DLM_IFL_RESEND;
4463 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
4464 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
4465 lkb->lkb_wait_type = 0;
4466 lkb->lkb_wait_count = 0;
4467 mutex_lock(&ls->ls_waiters_mutex);
4468 list_del_init(&lkb->lkb_wait_reply);
4469 mutex_unlock(&ls->ls_waiters_mutex);
4470 unhold_lkb(lkb); /* for waiters list */
David Teiglande7fd4172006-01-18 09:30:29 +00004471
David Teiglandef0c2bb2007-03-28 09:56:46 -05004472 if (oc || ou) {
4473 /* do an unlock or cancel instead of resending */
4474 switch (mstype) {
4475 case DLM_MSG_LOOKUP:
4476 case DLM_MSG_REQUEST:
4477 queue_cast(r, lkb, ou ? -DLM_EUNLOCK :
4478 -DLM_ECANCEL);
4479 unhold_lkb(lkb); /* undoes create_lkb() */
4480 break;
4481 case DLM_MSG_CONVERT:
4482 if (oc) {
4483 queue_cast(r, lkb, -DLM_ECANCEL);
4484 } else {
4485 lkb->lkb_exflags |= DLM_LKF_FORCEUNLOCK;
4486 _unlock_lock(r, lkb);
4487 }
4488 break;
4489 default:
4490 err = 1;
4491 }
4492 } else {
4493 switch (mstype) {
4494 case DLM_MSG_LOOKUP:
4495 case DLM_MSG_REQUEST:
4496 _request_lock(r, lkb);
4497 if (is_master(r))
4498 confirm_master(r, 0);
4499 break;
4500 case DLM_MSG_CONVERT:
4501 _convert_lock(r, lkb);
4502 break;
4503 default:
4504 err = 1;
4505 }
David Teiglande7fd4172006-01-18 09:30:29 +00004506 }
David Teiglandef0c2bb2007-03-28 09:56:46 -05004507
David Teigland13ef1112012-04-23 12:18:18 -05004508 if (err) {
4509 log_error(ls, "waiter %x msg %d r_nodeid %d "
4510 "dir_nodeid %d overlap %d %d",
4511 lkb->lkb_id, mstype, r->res_nodeid,
4512 dlm_dir_nodeid(r), oc, ou);
4513 }
David Teiglandef0c2bb2007-03-28 09:56:46 -05004514 unlock_rsb(r);
4515 put_rsb(r);
4516 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00004517 }
4518
4519 return error;
4520}
4521
4522static void purge_queue(struct dlm_rsb *r, struct list_head *queue,
4523 int (*test)(struct dlm_ls *ls, struct dlm_lkb *lkb))
4524{
4525 struct dlm_ls *ls = r->res_ls;
4526 struct dlm_lkb *lkb, *safe;
4527
4528 list_for_each_entry_safe(lkb, safe, queue, lkb_statequeue) {
4529 if (test(ls, lkb)) {
David Teigland97a35d12006-05-02 13:34:03 -04004530 rsb_set_flag(r, RSB_LOCKS_PURGED);
David Teiglande7fd4172006-01-18 09:30:29 +00004531 del_lkb(r, lkb);
4532 /* this put should free the lkb */
David Teiglandb3f58d82006-02-28 11:16:37 -05004533 if (!dlm_put_lkb(lkb))
David Teiglande7fd4172006-01-18 09:30:29 +00004534 log_error(ls, "purged lkb not released");
4535 }
4536 }
4537}
4538
4539static int purge_dead_test(struct dlm_ls *ls, struct dlm_lkb *lkb)
4540{
4541 return (is_master_copy(lkb) && dlm_is_removed(ls, lkb->lkb_nodeid));
4542}
4543
4544static int purge_mstcpy_test(struct dlm_ls *ls, struct dlm_lkb *lkb)
4545{
4546 return is_master_copy(lkb);
4547}
4548
4549static void purge_dead_locks(struct dlm_rsb *r)
4550{
4551 purge_queue(r, &r->res_grantqueue, &purge_dead_test);
4552 purge_queue(r, &r->res_convertqueue, &purge_dead_test);
4553 purge_queue(r, &r->res_waitqueue, &purge_dead_test);
4554}
4555
4556void dlm_purge_mstcpy_locks(struct dlm_rsb *r)
4557{
4558 purge_queue(r, &r->res_grantqueue, &purge_mstcpy_test);
4559 purge_queue(r, &r->res_convertqueue, &purge_mstcpy_test);
4560 purge_queue(r, &r->res_waitqueue, &purge_mstcpy_test);
4561}
4562
4563/* Get rid of locks held by nodes that are gone. */
4564
4565int dlm_purge_locks(struct dlm_ls *ls)
4566{
4567 struct dlm_rsb *r;
4568
4569 log_debug(ls, "dlm_purge_locks");
4570
4571 down_write(&ls->ls_root_sem);
4572 list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
4573 hold_rsb(r);
4574 lock_rsb(r);
4575 if (is_master(r))
4576 purge_dead_locks(r);
4577 unlock_rsb(r);
4578 unhold_rsb(r);
4579
4580 schedule();
4581 }
4582 up_write(&ls->ls_root_sem);
4583
4584 return 0;
4585}
4586
David Teigland97a35d12006-05-02 13:34:03 -04004587static struct dlm_rsb *find_purged_rsb(struct dlm_ls *ls, int bucket)
4588{
Bob Peterson9beb3bf2011-10-26 15:24:55 -05004589 struct rb_node *n;
David Teigland97a35d12006-05-02 13:34:03 -04004590 struct dlm_rsb *r, *r_ret = NULL;
4591
David Teiglandc7be7612009-01-07 16:50:41 -06004592 spin_lock(&ls->ls_rsbtbl[bucket].lock);
Bob Peterson9beb3bf2011-10-26 15:24:55 -05004593 for (n = rb_first(&ls->ls_rsbtbl[bucket].keep); n; n = rb_next(n)) {
4594 r = rb_entry(n, struct dlm_rsb, res_hashnode);
David Teigland97a35d12006-05-02 13:34:03 -04004595 if (!rsb_flag(r, RSB_LOCKS_PURGED))
4596 continue;
4597 hold_rsb(r);
4598 rsb_clear_flag(r, RSB_LOCKS_PURGED);
4599 r_ret = r;
4600 break;
4601 }
David Teiglandc7be7612009-01-07 16:50:41 -06004602 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teigland97a35d12006-05-02 13:34:03 -04004603 return r_ret;
4604}
4605
4606void dlm_grant_after_purge(struct dlm_ls *ls)
David Teiglande7fd4172006-01-18 09:30:29 +00004607{
4608 struct dlm_rsb *r;
David Teigland2b4e9262006-07-25 13:59:48 -05004609 int bucket = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00004610
David Teigland2b4e9262006-07-25 13:59:48 -05004611 while (1) {
4612 r = find_purged_rsb(ls, bucket);
4613 if (!r) {
4614 if (bucket == ls->ls_rsbtbl_size - 1)
4615 break;
4616 bucket++;
David Teigland97a35d12006-05-02 13:34:03 -04004617 continue;
David Teigland2b4e9262006-07-25 13:59:48 -05004618 }
David Teigland97a35d12006-05-02 13:34:03 -04004619 lock_rsb(r);
4620 if (is_master(r)) {
4621 grant_pending_locks(r);
4622 confirm_master(r, 0);
David Teiglande7fd4172006-01-18 09:30:29 +00004623 }
David Teigland97a35d12006-05-02 13:34:03 -04004624 unlock_rsb(r);
4625 put_rsb(r);
David Teigland2b4e9262006-07-25 13:59:48 -05004626 schedule();
David Teiglande7fd4172006-01-18 09:30:29 +00004627 }
David Teiglande7fd4172006-01-18 09:30:29 +00004628}
4629
4630static struct dlm_lkb *search_remid_list(struct list_head *head, int nodeid,
4631 uint32_t remid)
4632{
4633 struct dlm_lkb *lkb;
4634
4635 list_for_each_entry(lkb, head, lkb_statequeue) {
4636 if (lkb->lkb_nodeid == nodeid && lkb->lkb_remid == remid)
4637 return lkb;
4638 }
4639 return NULL;
4640}
4641
4642static struct dlm_lkb *search_remid(struct dlm_rsb *r, int nodeid,
4643 uint32_t remid)
4644{
4645 struct dlm_lkb *lkb;
4646
4647 lkb = search_remid_list(&r->res_grantqueue, nodeid, remid);
4648 if (lkb)
4649 return lkb;
4650 lkb = search_remid_list(&r->res_convertqueue, nodeid, remid);
4651 if (lkb)
4652 return lkb;
4653 lkb = search_remid_list(&r->res_waitqueue, nodeid, remid);
4654 if (lkb)
4655 return lkb;
4656 return NULL;
4657}
4658
Al Viroae773d02008-01-25 19:55:09 -05004659/* needs at least dlm_rcom + rcom_lock */
David Teiglande7fd4172006-01-18 09:30:29 +00004660static int receive_rcom_lock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
4661 struct dlm_rsb *r, struct dlm_rcom *rc)
4662{
4663 struct rcom_lock *rl = (struct rcom_lock *) rc->rc_buf;
David Teiglande7fd4172006-01-18 09:30:29 +00004664
4665 lkb->lkb_nodeid = rc->rc_header.h_nodeid;
Al Viro163a1852008-01-25 02:08:26 -05004666 lkb->lkb_ownpid = le32_to_cpu(rl->rl_ownpid);
4667 lkb->lkb_remid = le32_to_cpu(rl->rl_lkid);
4668 lkb->lkb_exflags = le32_to_cpu(rl->rl_exflags);
4669 lkb->lkb_flags = le32_to_cpu(rl->rl_flags) & 0x0000FFFF;
David Teiglande7fd4172006-01-18 09:30:29 +00004670 lkb->lkb_flags |= DLM_IFL_MSTCPY;
Al Viro163a1852008-01-25 02:08:26 -05004671 lkb->lkb_lvbseq = le32_to_cpu(rl->rl_lvbseq);
David Teiglande7fd4172006-01-18 09:30:29 +00004672 lkb->lkb_rqmode = rl->rl_rqmode;
4673 lkb->lkb_grmode = rl->rl_grmode;
4674 /* don't set lkb_status because add_lkb wants to itself */
4675
David Teigland8304d6f2011-02-21 14:58:21 -06004676 lkb->lkb_bastfn = (rl->rl_asts & DLM_CB_BAST) ? &fake_bastfn : NULL;
4677 lkb->lkb_astfn = (rl->rl_asts & DLM_CB_CAST) ? &fake_astfn : NULL;
David Teiglande7fd4172006-01-18 09:30:29 +00004678
David Teiglande7fd4172006-01-18 09:30:29 +00004679 if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
Al Viroa5dd0632008-01-25 20:22:22 -05004680 int lvblen = rc->rc_header.h_length - sizeof(struct dlm_rcom) -
4681 sizeof(struct rcom_lock);
4682 if (lvblen > ls->ls_lvblen)
4683 return -EINVAL;
David Teigland52bda2b2007-11-07 09:06:49 -06004684 lkb->lkb_lvbptr = dlm_allocate_lvb(ls);
David Teiglande7fd4172006-01-18 09:30:29 +00004685 if (!lkb->lkb_lvbptr)
4686 return -ENOMEM;
David Teiglande7fd4172006-01-18 09:30:29 +00004687 memcpy(lkb->lkb_lvbptr, rl->rl_lvb, lvblen);
4688 }
4689
4690 /* Conversions between PR and CW (middle modes) need special handling.
4691 The real granted mode of these converting locks cannot be determined
4692 until all locks have been rebuilt on the rsb (recover_conversion) */
4693
Al Viro163a1852008-01-25 02:08:26 -05004694 if (rl->rl_wait_type == cpu_to_le16(DLM_MSG_CONVERT) &&
4695 middle_conversion(lkb)) {
David Teiglande7fd4172006-01-18 09:30:29 +00004696 rl->rl_status = DLM_LKSTS_CONVERT;
4697 lkb->lkb_grmode = DLM_LOCK_IV;
4698 rsb_set_flag(r, RSB_RECOVER_CONVERT);
4699 }
4700
4701 return 0;
4702}
4703
4704/* This lkb may have been recovered in a previous aborted recovery so we need
4705 to check if the rsb already has an lkb with the given remote nodeid/lkid.
4706 If so we just send back a standard reply. If not, we create a new lkb with
4707 the given values and send back our lkid. We send back our lkid by sending
4708 back the rcom_lock struct we got but with the remid field filled in. */
4709
Al Viroae773d02008-01-25 19:55:09 -05004710/* needs at least dlm_rcom + rcom_lock */
David Teiglande7fd4172006-01-18 09:30:29 +00004711int dlm_recover_master_copy(struct dlm_ls *ls, struct dlm_rcom *rc)
4712{
4713 struct rcom_lock *rl = (struct rcom_lock *) rc->rc_buf;
4714 struct dlm_rsb *r;
4715 struct dlm_lkb *lkb;
David Teigland6d40c4a2012-04-23 16:36:01 -05004716 uint32_t remid = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00004717 int error;
4718
4719 if (rl->rl_parent_lkid) {
4720 error = -EOPNOTSUPP;
4721 goto out;
4722 }
4723
David Teigland6d40c4a2012-04-23 16:36:01 -05004724 remid = le32_to_cpu(rl->rl_lkid);
4725
Al Viro163a1852008-01-25 02:08:26 -05004726 error = find_rsb(ls, rl->rl_name, le16_to_cpu(rl->rl_namelen),
4727 R_MASTER, &r);
David Teiglande7fd4172006-01-18 09:30:29 +00004728 if (error)
4729 goto out;
4730
4731 lock_rsb(r);
4732
David Teigland6d40c4a2012-04-23 16:36:01 -05004733 lkb = search_remid(r, rc->rc_header.h_nodeid, remid);
David Teiglande7fd4172006-01-18 09:30:29 +00004734 if (lkb) {
4735 error = -EEXIST;
4736 goto out_remid;
4737 }
4738
4739 error = create_lkb(ls, &lkb);
4740 if (error)
4741 goto out_unlock;
4742
4743 error = receive_rcom_lock_args(ls, lkb, r, rc);
4744 if (error) {
David Teiglandb3f58d82006-02-28 11:16:37 -05004745 __put_lkb(ls, lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00004746 goto out_unlock;
4747 }
4748
4749 attach_lkb(r, lkb);
4750 add_lkb(r, lkb, rl->rl_status);
4751 error = 0;
4752
4753 out_remid:
4754 /* this is the new value returned to the lock holder for
4755 saving in its process-copy lkb */
Al Viro163a1852008-01-25 02:08:26 -05004756 rl->rl_remid = cpu_to_le32(lkb->lkb_id);
David Teiglande7fd4172006-01-18 09:30:29 +00004757
4758 out_unlock:
4759 unlock_rsb(r);
4760 put_rsb(r);
4761 out:
David Teigland6d40c4a2012-04-23 16:36:01 -05004762 if (error && error != -EEXIST)
4763 log_debug(ls, "dlm_recover_master_copy remote %d %x error %d",
4764 rc->rc_header.h_nodeid, remid, error);
Al Viro163a1852008-01-25 02:08:26 -05004765 rl->rl_result = cpu_to_le32(error);
David Teiglande7fd4172006-01-18 09:30:29 +00004766 return error;
4767}
4768
Al Viroae773d02008-01-25 19:55:09 -05004769/* needs at least dlm_rcom + rcom_lock */
David Teiglande7fd4172006-01-18 09:30:29 +00004770int dlm_recover_process_copy(struct dlm_ls *ls, struct dlm_rcom *rc)
4771{
4772 struct rcom_lock *rl = (struct rcom_lock *) rc->rc_buf;
4773 struct dlm_rsb *r;
4774 struct dlm_lkb *lkb;
David Teigland6d40c4a2012-04-23 16:36:01 -05004775 uint32_t lkid, remid;
4776 int error, result;
David Teiglande7fd4172006-01-18 09:30:29 +00004777
David Teigland6d40c4a2012-04-23 16:36:01 -05004778 lkid = le32_to_cpu(rl->rl_lkid);
4779 remid = le32_to_cpu(rl->rl_remid);
4780 result = le32_to_cpu(rl->rl_result);
4781
4782 error = find_lkb(ls, lkid, &lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00004783 if (error) {
David Teigland6d40c4a2012-04-23 16:36:01 -05004784 log_error(ls, "dlm_recover_process_copy no %x remote %d %x %d",
4785 lkid, rc->rc_header.h_nodeid, remid, result);
David Teiglande7fd4172006-01-18 09:30:29 +00004786 return error;
4787 }
4788
David Teigland6d40c4a2012-04-23 16:36:01 -05004789 if (!is_process_copy(lkb)) {
4790 log_error(ls, "dlm_recover_process_copy bad %x remote %d %x %d",
4791 lkid, rc->rc_header.h_nodeid, remid, result);
4792 dlm_print_lkb(lkb);
4793 return -EINVAL;
4794 }
David Teiglande7fd4172006-01-18 09:30:29 +00004795
4796 r = lkb->lkb_resource;
4797 hold_rsb(r);
4798 lock_rsb(r);
4799
David Teigland6d40c4a2012-04-23 16:36:01 -05004800 switch (result) {
David Teiglanddc200a82006-12-13 10:36:37 -06004801 case -EBADR:
4802 /* There's a chance the new master received our lock before
4803 dlm_recover_master_reply(), this wouldn't happen if we did
4804 a barrier between recover_masters and recover_locks. */
David Teigland6d40c4a2012-04-23 16:36:01 -05004805
4806 log_debug(ls, "dlm_recover_process_copy %x remote %d %x %d",
4807 lkid, rc->rc_header.h_nodeid, remid, result);
4808
David Teiglanddc200a82006-12-13 10:36:37 -06004809 dlm_send_rcom_lock(r, lkb);
4810 goto out;
David Teiglande7fd4172006-01-18 09:30:29 +00004811 case -EEXIST:
David Teiglande7fd4172006-01-18 09:30:29 +00004812 case 0:
David Teigland6d40c4a2012-04-23 16:36:01 -05004813 lkb->lkb_remid = remid;
David Teiglande7fd4172006-01-18 09:30:29 +00004814 break;
4815 default:
David Teigland6d40c4a2012-04-23 16:36:01 -05004816 log_error(ls, "dlm_recover_process_copy %x remote %d %x %d unk",
4817 lkid, rc->rc_header.h_nodeid, remid, result);
David Teiglande7fd4172006-01-18 09:30:29 +00004818 }
4819
4820 /* an ack for dlm_recover_locks() which waits for replies from
4821 all the locks it sends to new masters */
4822 dlm_recovered_lock(r);
David Teiglanddc200a82006-12-13 10:36:37 -06004823 out:
David Teiglande7fd4172006-01-18 09:30:29 +00004824 unlock_rsb(r);
4825 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05004826 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00004827
4828 return 0;
4829}
4830
David Teigland597d0ca2006-07-12 16:44:04 -05004831int dlm_user_request(struct dlm_ls *ls, struct dlm_user_args *ua,
4832 int mode, uint32_t flags, void *name, unsigned int namelen,
David Teiglandd7db9232007-05-18 09:00:32 -05004833 unsigned long timeout_cs)
David Teigland597d0ca2006-07-12 16:44:04 -05004834{
4835 struct dlm_lkb *lkb;
4836 struct dlm_args args;
4837 int error;
4838
David Teigland85e86ed2007-05-18 08:58:15 -05004839 dlm_lock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05004840
4841 error = create_lkb(ls, &lkb);
4842 if (error) {
4843 kfree(ua);
4844 goto out;
4845 }
4846
4847 if (flags & DLM_LKF_VALBLK) {
David Teigland573c24c2009-11-30 16:34:43 -06004848 ua->lksb.sb_lvbptr = kzalloc(DLM_USER_LVB_LEN, GFP_NOFS);
David Teigland597d0ca2006-07-12 16:44:04 -05004849 if (!ua->lksb.sb_lvbptr) {
4850 kfree(ua);
4851 __put_lkb(ls, lkb);
4852 error = -ENOMEM;
4853 goto out;
4854 }
4855 }
4856
David Teigland52bda2b2007-11-07 09:06:49 -06004857 /* After ua is attached to lkb it will be freed by dlm_free_lkb().
David Teigland597d0ca2006-07-12 16:44:04 -05004858 When DLM_IFL_USER is set, the dlm knows that this is a userspace
4859 lock and that lkb_astparam is the dlm_user_args structure. */
4860
David Teiglandd7db9232007-05-18 09:00:32 -05004861 error = set_lock_args(mode, &ua->lksb, flags, namelen, timeout_cs,
David Teiglande5dae542008-02-06 00:35:45 -06004862 fake_astfn, ua, fake_bastfn, &args);
David Teigland597d0ca2006-07-12 16:44:04 -05004863 lkb->lkb_flags |= DLM_IFL_USER;
David Teigland597d0ca2006-07-12 16:44:04 -05004864
4865 if (error) {
4866 __put_lkb(ls, lkb);
4867 goto out;
4868 }
4869
4870 error = request_lock(ls, lkb, name, namelen, &args);
4871
4872 switch (error) {
4873 case 0:
4874 break;
4875 case -EINPROGRESS:
4876 error = 0;
4877 break;
4878 case -EAGAIN:
4879 error = 0;
4880 /* fall through */
4881 default:
4882 __put_lkb(ls, lkb);
4883 goto out;
4884 }
4885
4886 /* add this new lkb to the per-process list of locks */
4887 spin_lock(&ua->proc->locks_spin);
David Teiglandef0c2bb2007-03-28 09:56:46 -05004888 hold_lkb(lkb);
David Teigland597d0ca2006-07-12 16:44:04 -05004889 list_add_tail(&lkb->lkb_ownqueue, &ua->proc->locks);
4890 spin_unlock(&ua->proc->locks_spin);
4891 out:
David Teigland85e86ed2007-05-18 08:58:15 -05004892 dlm_unlock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05004893 return error;
4894}
4895
4896int dlm_user_convert(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
David Teiglandd7db9232007-05-18 09:00:32 -05004897 int mode, uint32_t flags, uint32_t lkid, char *lvb_in,
4898 unsigned long timeout_cs)
David Teigland597d0ca2006-07-12 16:44:04 -05004899{
4900 struct dlm_lkb *lkb;
4901 struct dlm_args args;
4902 struct dlm_user_args *ua;
4903 int error;
4904
David Teigland85e86ed2007-05-18 08:58:15 -05004905 dlm_lock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05004906
4907 error = find_lkb(ls, lkid, &lkb);
4908 if (error)
4909 goto out;
4910
4911 /* user can change the params on its lock when it converts it, or
4912 add an lvb that didn't exist before */
4913
David Teiglandd292c0c2008-02-06 23:27:04 -06004914 ua = lkb->lkb_ua;
David Teigland597d0ca2006-07-12 16:44:04 -05004915
4916 if (flags & DLM_LKF_VALBLK && !ua->lksb.sb_lvbptr) {
David Teigland573c24c2009-11-30 16:34:43 -06004917 ua->lksb.sb_lvbptr = kzalloc(DLM_USER_LVB_LEN, GFP_NOFS);
David Teigland597d0ca2006-07-12 16:44:04 -05004918 if (!ua->lksb.sb_lvbptr) {
4919 error = -ENOMEM;
4920 goto out_put;
4921 }
4922 }
4923 if (lvb_in && ua->lksb.sb_lvbptr)
4924 memcpy(ua->lksb.sb_lvbptr, lvb_in, DLM_USER_LVB_LEN);
4925
David Teiglandd7db9232007-05-18 09:00:32 -05004926 ua->xid = ua_tmp->xid;
David Teigland597d0ca2006-07-12 16:44:04 -05004927 ua->castparam = ua_tmp->castparam;
4928 ua->castaddr = ua_tmp->castaddr;
4929 ua->bastparam = ua_tmp->bastparam;
4930 ua->bastaddr = ua_tmp->bastaddr;
Patrick Caulfield10948eb2006-08-23 09:49:31 +01004931 ua->user_lksb = ua_tmp->user_lksb;
David Teigland597d0ca2006-07-12 16:44:04 -05004932
David Teiglandd7db9232007-05-18 09:00:32 -05004933 error = set_lock_args(mode, &ua->lksb, flags, 0, timeout_cs,
David Teiglande5dae542008-02-06 00:35:45 -06004934 fake_astfn, ua, fake_bastfn, &args);
David Teigland597d0ca2006-07-12 16:44:04 -05004935 if (error)
4936 goto out_put;
4937
4938 error = convert_lock(ls, lkb, &args);
4939
David Teiglandc85d65e2007-05-18 09:01:26 -05004940 if (error == -EINPROGRESS || error == -EAGAIN || error == -EDEADLK)
David Teigland597d0ca2006-07-12 16:44:04 -05004941 error = 0;
4942 out_put:
4943 dlm_put_lkb(lkb);
4944 out:
David Teigland85e86ed2007-05-18 08:58:15 -05004945 dlm_unlock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05004946 kfree(ua_tmp);
4947 return error;
4948}
4949
4950int dlm_user_unlock(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
4951 uint32_t flags, uint32_t lkid, char *lvb_in)
4952{
4953 struct dlm_lkb *lkb;
4954 struct dlm_args args;
4955 struct dlm_user_args *ua;
4956 int error;
4957
David Teigland85e86ed2007-05-18 08:58:15 -05004958 dlm_lock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05004959
4960 error = find_lkb(ls, lkid, &lkb);
4961 if (error)
4962 goto out;
4963
David Teiglandd292c0c2008-02-06 23:27:04 -06004964 ua = lkb->lkb_ua;
David Teigland597d0ca2006-07-12 16:44:04 -05004965
4966 if (lvb_in && ua->lksb.sb_lvbptr)
4967 memcpy(ua->lksb.sb_lvbptr, lvb_in, DLM_USER_LVB_LEN);
Patrick Caulfieldb434eda2007-10-01 15:28:42 +01004968 if (ua_tmp->castparam)
4969 ua->castparam = ua_tmp->castparam;
Patrick Caulfieldcc346d52006-08-08 10:34:40 -04004970 ua->user_lksb = ua_tmp->user_lksb;
David Teigland597d0ca2006-07-12 16:44:04 -05004971
4972 error = set_unlock_args(flags, ua, &args);
4973 if (error)
4974 goto out_put;
4975
4976 error = unlock_lock(ls, lkb, &args);
4977
4978 if (error == -DLM_EUNLOCK)
4979 error = 0;
David Teiglandef0c2bb2007-03-28 09:56:46 -05004980 /* from validate_unlock_args() */
4981 if (error == -EBUSY && (flags & DLM_LKF_FORCEUNLOCK))
4982 error = 0;
David Teigland597d0ca2006-07-12 16:44:04 -05004983 if (error)
4984 goto out_put;
4985
4986 spin_lock(&ua->proc->locks_spin);
David Teigland23e8e1a2011-04-05 13:16:24 -05004987 /* dlm_user_add_cb() may have already taken lkb off the proc list */
David Teiglanda1bc86e2007-01-15 10:34:52 -06004988 if (!list_empty(&lkb->lkb_ownqueue))
4989 list_move(&lkb->lkb_ownqueue, &ua->proc->unlocking);
David Teigland597d0ca2006-07-12 16:44:04 -05004990 spin_unlock(&ua->proc->locks_spin);
David Teigland597d0ca2006-07-12 16:44:04 -05004991 out_put:
4992 dlm_put_lkb(lkb);
4993 out:
David Teigland85e86ed2007-05-18 08:58:15 -05004994 dlm_unlock_recovery(ls);
David Teiglandef0c2bb2007-03-28 09:56:46 -05004995 kfree(ua_tmp);
David Teigland597d0ca2006-07-12 16:44:04 -05004996 return error;
4997}
4998
4999int dlm_user_cancel(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
5000 uint32_t flags, uint32_t lkid)
5001{
5002 struct dlm_lkb *lkb;
5003 struct dlm_args args;
5004 struct dlm_user_args *ua;
5005 int error;
5006
David Teigland85e86ed2007-05-18 08:58:15 -05005007 dlm_lock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05005008
5009 error = find_lkb(ls, lkid, &lkb);
5010 if (error)
5011 goto out;
5012
David Teiglandd292c0c2008-02-06 23:27:04 -06005013 ua = lkb->lkb_ua;
Patrick Caulfieldb434eda2007-10-01 15:28:42 +01005014 if (ua_tmp->castparam)
5015 ua->castparam = ua_tmp->castparam;
Patrick Caulfieldc059f702006-08-23 10:24:03 +01005016 ua->user_lksb = ua_tmp->user_lksb;
David Teigland597d0ca2006-07-12 16:44:04 -05005017
5018 error = set_unlock_args(flags, ua, &args);
5019 if (error)
5020 goto out_put;
5021
5022 error = cancel_lock(ls, lkb, &args);
5023
5024 if (error == -DLM_ECANCEL)
5025 error = 0;
David Teiglandef0c2bb2007-03-28 09:56:46 -05005026 /* from validate_unlock_args() */
5027 if (error == -EBUSY)
5028 error = 0;
David Teigland597d0ca2006-07-12 16:44:04 -05005029 out_put:
5030 dlm_put_lkb(lkb);
5031 out:
David Teigland85e86ed2007-05-18 08:58:15 -05005032 dlm_unlock_recovery(ls);
David Teiglandef0c2bb2007-03-28 09:56:46 -05005033 kfree(ua_tmp);
David Teigland597d0ca2006-07-12 16:44:04 -05005034 return error;
5035}
5036
David Teigland8b4021f2007-05-29 08:46:00 -05005037int dlm_user_deadlock(struct dlm_ls *ls, uint32_t flags, uint32_t lkid)
5038{
5039 struct dlm_lkb *lkb;
5040 struct dlm_args args;
5041 struct dlm_user_args *ua;
5042 struct dlm_rsb *r;
5043 int error;
5044
5045 dlm_lock_recovery(ls);
5046
5047 error = find_lkb(ls, lkid, &lkb);
5048 if (error)
5049 goto out;
5050
David Teiglandd292c0c2008-02-06 23:27:04 -06005051 ua = lkb->lkb_ua;
David Teigland8b4021f2007-05-29 08:46:00 -05005052
5053 error = set_unlock_args(flags, ua, &args);
5054 if (error)
5055 goto out_put;
5056
5057 /* same as cancel_lock(), but set DEADLOCK_CANCEL after lock_rsb */
5058
5059 r = lkb->lkb_resource;
5060 hold_rsb(r);
5061 lock_rsb(r);
5062
5063 error = validate_unlock_args(lkb, &args);
5064 if (error)
5065 goto out_r;
5066 lkb->lkb_flags |= DLM_IFL_DEADLOCK_CANCEL;
5067
5068 error = _cancel_lock(r, lkb);
5069 out_r:
5070 unlock_rsb(r);
5071 put_rsb(r);
5072
5073 if (error == -DLM_ECANCEL)
5074 error = 0;
5075 /* from validate_unlock_args() */
5076 if (error == -EBUSY)
5077 error = 0;
5078 out_put:
5079 dlm_put_lkb(lkb);
5080 out:
5081 dlm_unlock_recovery(ls);
5082 return error;
5083}
5084
David Teiglandef0c2bb2007-03-28 09:56:46 -05005085/* lkb's that are removed from the waiters list by revert are just left on the
5086 orphans list with the granted orphan locks, to be freed by purge */
5087
David Teigland597d0ca2006-07-12 16:44:04 -05005088static int orphan_proc_lock(struct dlm_ls *ls, struct dlm_lkb *lkb)
5089{
David Teiglandef0c2bb2007-03-28 09:56:46 -05005090 struct dlm_args args;
5091 int error;
David Teigland597d0ca2006-07-12 16:44:04 -05005092
David Teiglandef0c2bb2007-03-28 09:56:46 -05005093 hold_lkb(lkb);
5094 mutex_lock(&ls->ls_orphans_mutex);
5095 list_add_tail(&lkb->lkb_ownqueue, &ls->ls_orphans);
5096 mutex_unlock(&ls->ls_orphans_mutex);
David Teigland597d0ca2006-07-12 16:44:04 -05005097
David Teiglandd292c0c2008-02-06 23:27:04 -06005098 set_unlock_args(0, lkb->lkb_ua, &args);
David Teiglandef0c2bb2007-03-28 09:56:46 -05005099
5100 error = cancel_lock(ls, lkb, &args);
5101 if (error == -DLM_ECANCEL)
5102 error = 0;
5103 return error;
David Teigland597d0ca2006-07-12 16:44:04 -05005104}
5105
5106/* The force flag allows the unlock to go ahead even if the lkb isn't granted.
5107 Regardless of what rsb queue the lock is on, it's removed and freed. */
5108
5109static int unlock_proc_lock(struct dlm_ls *ls, struct dlm_lkb *lkb)
5110{
David Teigland597d0ca2006-07-12 16:44:04 -05005111 struct dlm_args args;
5112 int error;
5113
David Teiglandd292c0c2008-02-06 23:27:04 -06005114 set_unlock_args(DLM_LKF_FORCEUNLOCK, lkb->lkb_ua, &args);
David Teigland597d0ca2006-07-12 16:44:04 -05005115
5116 error = unlock_lock(ls, lkb, &args);
5117 if (error == -DLM_EUNLOCK)
5118 error = 0;
5119 return error;
5120}
5121
David Teiglandef0c2bb2007-03-28 09:56:46 -05005122/* We have to release clear_proc_locks mutex before calling unlock_proc_lock()
5123 (which does lock_rsb) due to deadlock with receiving a message that does
David Teigland23e8e1a2011-04-05 13:16:24 -05005124 lock_rsb followed by dlm_user_add_cb() */
David Teiglandef0c2bb2007-03-28 09:56:46 -05005125
5126static struct dlm_lkb *del_proc_lock(struct dlm_ls *ls,
5127 struct dlm_user_proc *proc)
5128{
5129 struct dlm_lkb *lkb = NULL;
5130
5131 mutex_lock(&ls->ls_clear_proc_locks);
5132 if (list_empty(&proc->locks))
5133 goto out;
5134
5135 lkb = list_entry(proc->locks.next, struct dlm_lkb, lkb_ownqueue);
5136 list_del_init(&lkb->lkb_ownqueue);
5137
5138 if (lkb->lkb_exflags & DLM_LKF_PERSISTENT)
5139 lkb->lkb_flags |= DLM_IFL_ORPHAN;
5140 else
5141 lkb->lkb_flags |= DLM_IFL_DEAD;
5142 out:
5143 mutex_unlock(&ls->ls_clear_proc_locks);
5144 return lkb;
5145}
5146
David Teigland23e8e1a2011-04-05 13:16:24 -05005147/* The ls_clear_proc_locks mutex protects against dlm_user_add_cb() which
David Teigland597d0ca2006-07-12 16:44:04 -05005148 1) references lkb->ua which we free here and 2) adds lkbs to proc->asts,
5149 which we clear here. */
5150
5151/* proc CLOSING flag is set so no more device_reads should look at proc->asts
5152 list, and no more device_writes should add lkb's to proc->locks list; so we
5153 shouldn't need to take asts_spin or locks_spin here. this assumes that
5154 device reads/writes/closes are serialized -- FIXME: we may need to serialize
5155 them ourself. */
5156
5157void dlm_clear_proc_locks(struct dlm_ls *ls, struct dlm_user_proc *proc)
5158{
5159 struct dlm_lkb *lkb, *safe;
5160
David Teigland85e86ed2007-05-18 08:58:15 -05005161 dlm_lock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05005162
David Teiglandef0c2bb2007-03-28 09:56:46 -05005163 while (1) {
5164 lkb = del_proc_lock(ls, proc);
5165 if (!lkb)
5166 break;
David Teigland84d8cd62007-05-29 08:44:23 -05005167 del_timeout(lkb);
David Teiglandef0c2bb2007-03-28 09:56:46 -05005168 if (lkb->lkb_exflags & DLM_LKF_PERSISTENT)
David Teigland597d0ca2006-07-12 16:44:04 -05005169 orphan_proc_lock(ls, lkb);
David Teiglandef0c2bb2007-03-28 09:56:46 -05005170 else
David Teigland597d0ca2006-07-12 16:44:04 -05005171 unlock_proc_lock(ls, lkb);
David Teigland597d0ca2006-07-12 16:44:04 -05005172
5173 /* this removes the reference for the proc->locks list
5174 added by dlm_user_request, it may result in the lkb
5175 being freed */
5176
5177 dlm_put_lkb(lkb);
5178 }
David Teiglanda1bc86e2007-01-15 10:34:52 -06005179
David Teiglandef0c2bb2007-03-28 09:56:46 -05005180 mutex_lock(&ls->ls_clear_proc_locks);
5181
David Teiglanda1bc86e2007-01-15 10:34:52 -06005182 /* in-progress unlocks */
5183 list_for_each_entry_safe(lkb, safe, &proc->unlocking, lkb_ownqueue) {
5184 list_del_init(&lkb->lkb_ownqueue);
5185 lkb->lkb_flags |= DLM_IFL_DEAD;
5186 dlm_put_lkb(lkb);
5187 }
5188
David Teigland23e8e1a2011-04-05 13:16:24 -05005189 list_for_each_entry_safe(lkb, safe, &proc->asts, lkb_cb_list) {
David Teigland8304d6f2011-02-21 14:58:21 -06005190 memset(&lkb->lkb_callbacks, 0,
5191 sizeof(struct dlm_callback) * DLM_CALLBACKS_SIZE);
David Teigland23e8e1a2011-04-05 13:16:24 -05005192 list_del_init(&lkb->lkb_cb_list);
David Teiglanda1bc86e2007-01-15 10:34:52 -06005193 dlm_put_lkb(lkb);
5194 }
5195
David Teigland597d0ca2006-07-12 16:44:04 -05005196 mutex_unlock(&ls->ls_clear_proc_locks);
David Teigland85e86ed2007-05-18 08:58:15 -05005197 dlm_unlock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05005198}
David Teiglanda1bc86e2007-01-15 10:34:52 -06005199
David Teigland84991372007-03-30 15:02:40 -05005200static void purge_proc_locks(struct dlm_ls *ls, struct dlm_user_proc *proc)
5201{
5202 struct dlm_lkb *lkb, *safe;
5203
5204 while (1) {
5205 lkb = NULL;
5206 spin_lock(&proc->locks_spin);
5207 if (!list_empty(&proc->locks)) {
5208 lkb = list_entry(proc->locks.next, struct dlm_lkb,
5209 lkb_ownqueue);
5210 list_del_init(&lkb->lkb_ownqueue);
5211 }
5212 spin_unlock(&proc->locks_spin);
5213
5214 if (!lkb)
5215 break;
5216
5217 lkb->lkb_flags |= DLM_IFL_DEAD;
5218 unlock_proc_lock(ls, lkb);
5219 dlm_put_lkb(lkb); /* ref from proc->locks list */
5220 }
5221
5222 spin_lock(&proc->locks_spin);
5223 list_for_each_entry_safe(lkb, safe, &proc->unlocking, lkb_ownqueue) {
5224 list_del_init(&lkb->lkb_ownqueue);
5225 lkb->lkb_flags |= DLM_IFL_DEAD;
5226 dlm_put_lkb(lkb);
5227 }
5228 spin_unlock(&proc->locks_spin);
5229
5230 spin_lock(&proc->asts_spin);
David Teigland23e8e1a2011-04-05 13:16:24 -05005231 list_for_each_entry_safe(lkb, safe, &proc->asts, lkb_cb_list) {
David Teigland8304d6f2011-02-21 14:58:21 -06005232 memset(&lkb->lkb_callbacks, 0,
5233 sizeof(struct dlm_callback) * DLM_CALLBACKS_SIZE);
David Teigland23e8e1a2011-04-05 13:16:24 -05005234 list_del_init(&lkb->lkb_cb_list);
David Teigland84991372007-03-30 15:02:40 -05005235 dlm_put_lkb(lkb);
5236 }
5237 spin_unlock(&proc->asts_spin);
5238}
5239
5240/* pid of 0 means purge all orphans */
5241
5242static void do_purge(struct dlm_ls *ls, int nodeid, int pid)
5243{
5244 struct dlm_lkb *lkb, *safe;
5245
5246 mutex_lock(&ls->ls_orphans_mutex);
5247 list_for_each_entry_safe(lkb, safe, &ls->ls_orphans, lkb_ownqueue) {
5248 if (pid && lkb->lkb_ownpid != pid)
5249 continue;
5250 unlock_proc_lock(ls, lkb);
5251 list_del_init(&lkb->lkb_ownqueue);
5252 dlm_put_lkb(lkb);
5253 }
5254 mutex_unlock(&ls->ls_orphans_mutex);
5255}
5256
5257static int send_purge(struct dlm_ls *ls, int nodeid, int pid)
5258{
5259 struct dlm_message *ms;
5260 struct dlm_mhandle *mh;
5261 int error;
5262
5263 error = _create_message(ls, sizeof(struct dlm_message), nodeid,
5264 DLM_MSG_PURGE, &ms, &mh);
5265 if (error)
5266 return error;
5267 ms->m_nodeid = nodeid;
5268 ms->m_pid = pid;
5269
5270 return send_message(mh, ms);
5271}
5272
5273int dlm_user_purge(struct dlm_ls *ls, struct dlm_user_proc *proc,
5274 int nodeid, int pid)
5275{
5276 int error = 0;
5277
5278 if (nodeid != dlm_our_nodeid()) {
5279 error = send_purge(ls, nodeid, pid);
5280 } else {
David Teigland85e86ed2007-05-18 08:58:15 -05005281 dlm_lock_recovery(ls);
David Teigland84991372007-03-30 15:02:40 -05005282 if (pid == current->pid)
5283 purge_proc_locks(ls, proc);
5284 else
5285 do_purge(ls, nodeid, pid);
David Teigland85e86ed2007-05-18 08:58:15 -05005286 dlm_unlock_recovery(ls);
David Teigland84991372007-03-30 15:02:40 -05005287 }
5288 return error;
5289}
5290