blob: fa5c07d51dccf2cb1b66d4b2d04a4eec8a92e7da [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{
163 printk(KERN_ERR "lkb: nodeid %d id %x remid %x exflags %x flags %x\n"
David Teigland8304d6f2011-02-21 14:58:21 -0600164 " status %d rqmode %d grmode %d wait_type %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 Teigland8304d6f2011-02-21 14:58:21 -0600167 lkb->lkb_grmode, lkb->lkb_wait_type);
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 Teigland7210cb72012-03-08 12:37:12 -0600482 error = dlm_search_rsb_tree(&ls->ls_rsbtbl[b].toss, name, len, flags, &r);
David Teiglande7fd4172006-01-18 09:30:29 +0000483 if (error)
484 goto out;
485
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500486 rb_erase(&r->res_hashnode, &ls->ls_rsbtbl[b].toss);
487 error = rsb_insert(r, &ls->ls_rsbtbl[b].keep);
488 if (error)
489 return error;
David Teiglande7fd4172006-01-18 09:30:29 +0000490
491 if (dlm_no_directory(ls))
492 goto out;
493
494 if (r->res_nodeid == -1) {
495 rsb_clear_flag(r, RSB_MASTER_UNCERTAIN);
496 r->res_first_lkid = 0;
497 } else if (r->res_nodeid > 0) {
498 rsb_set_flag(r, RSB_MASTER_UNCERTAIN);
499 r->res_first_lkid = 0;
500 } else {
501 DLM_ASSERT(r->res_nodeid == 0, dlm_print_rsb(r););
502 DLM_ASSERT(!rsb_flag(r, RSB_MASTER_UNCERTAIN),);
503 }
504 out:
505 *r_ret = r;
506 return error;
507}
508
David Teiglande7fd4172006-01-18 09:30:29 +0000509/*
510 * Find rsb in rsbtbl and potentially create/add one
511 *
512 * Delaying the release of rsb's has a similar benefit to applications keeping
513 * NL locks on an rsb, but without the guarantee that the cached master value
514 * will still be valid when the rsb is reused. Apps aren't always smart enough
515 * to keep NL locks on an rsb that they may lock again shortly; this can lead
516 * to excessive master lookups and removals if we don't delay the release.
517 *
518 * Searching for an rsb means looking through both the normal list and toss
519 * list. When found on the toss list the rsb is moved to the normal list with
520 * ref count of 1; when found on normal list the ref count is incremented.
521 */
522
523static int find_rsb(struct dlm_ls *ls, char *name, int namelen,
524 unsigned int flags, struct dlm_rsb **r_ret)
525{
David Teigland3881ac02011-07-07 14:05:03 -0500526 struct dlm_rsb *r = NULL;
David Teiglande7fd4172006-01-18 09:30:29 +0000527 uint32_t hash, bucket;
David Teigland3881ac02011-07-07 14:05:03 -0500528 int error;
Al Viroef58bcc2008-01-25 23:22:26 -0500529
David Teigland3881ac02011-07-07 14:05:03 -0500530 if (namelen > DLM_RESNAME_MAXLEN) {
531 error = -EINVAL;
Al Viroef58bcc2008-01-25 23:22:26 -0500532 goto out;
David Teigland3881ac02011-07-07 14:05:03 -0500533 }
David Teiglande7fd4172006-01-18 09:30:29 +0000534
535 if (dlm_no_directory(ls))
536 flags |= R_CREATE;
537
538 hash = jhash(name, namelen, 0);
539 bucket = hash & (ls->ls_rsbtbl_size - 1);
540
David Teigland3881ac02011-07-07 14:05:03 -0500541 retry:
542 if (flags & R_CREATE) {
543 error = pre_rsb_struct(ls);
544 if (error < 0)
545 goto out;
546 }
547
548 spin_lock(&ls->ls_rsbtbl[bucket].lock);
549
550 error = _search_rsb(ls, name, namelen, bucket, flags, &r);
David Teiglande7fd4172006-01-18 09:30:29 +0000551 if (!error)
David Teigland3881ac02011-07-07 14:05:03 -0500552 goto out_unlock;
David Teiglande7fd4172006-01-18 09:30:29 +0000553
David Teigland597d0ca2006-07-12 16:44:04 -0500554 if (error == -EBADR && !(flags & R_CREATE))
David Teigland3881ac02011-07-07 14:05:03 -0500555 goto out_unlock;
David Teiglande7fd4172006-01-18 09:30:29 +0000556
557 /* the rsb was found but wasn't a master copy */
558 if (error == -ENOTBLK)
David Teigland3881ac02011-07-07 14:05:03 -0500559 goto out_unlock;
David Teiglande7fd4172006-01-18 09:30:29 +0000560
David Teigland3881ac02011-07-07 14:05:03 -0500561 error = get_rsb_struct(ls, name, namelen, &r);
562 if (error == -EAGAIN) {
563 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
564 goto retry;
565 }
566 if (error)
567 goto out_unlock;
David Teiglande7fd4172006-01-18 09:30:29 +0000568
569 r->res_hash = hash;
570 r->res_bucket = bucket;
571 r->res_nodeid = -1;
572 kref_init(&r->res_ref);
573
574 /* With no directory, the master can be set immediately */
575 if (dlm_no_directory(ls)) {
576 int nodeid = dlm_dir_nodeid(r);
577 if (nodeid == dlm_our_nodeid())
578 nodeid = 0;
579 r->res_nodeid = nodeid;
580 }
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500581 error = rsb_insert(r, &ls->ls_rsbtbl[bucket].keep);
David Teigland3881ac02011-07-07 14:05:03 -0500582 out_unlock:
583 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000584 out:
585 *r_ret = r;
586 return error;
587}
588
David Teiglande7fd4172006-01-18 09:30:29 +0000589/* This is only called to add a reference when the code already holds
590 a valid reference to the rsb, so there's no need for locking. */
591
592static inline void hold_rsb(struct dlm_rsb *r)
593{
594 kref_get(&r->res_ref);
595}
596
597void dlm_hold_rsb(struct dlm_rsb *r)
598{
599 hold_rsb(r);
600}
601
602static void toss_rsb(struct kref *kref)
603{
604 struct dlm_rsb *r = container_of(kref, struct dlm_rsb, res_ref);
605 struct dlm_ls *ls = r->res_ls;
606
607 DLM_ASSERT(list_empty(&r->res_root_list), dlm_print_rsb(r););
608 kref_init(&r->res_ref);
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500609 rb_erase(&r->res_hashnode, &ls->ls_rsbtbl[r->res_bucket].keep);
610 rsb_insert(r, &ls->ls_rsbtbl[r->res_bucket].toss);
David Teiglande7fd4172006-01-18 09:30:29 +0000611 r->res_toss_time = jiffies;
612 if (r->res_lvbptr) {
David Teigland52bda2b2007-11-07 09:06:49 -0600613 dlm_free_lvb(r->res_lvbptr);
David Teiglande7fd4172006-01-18 09:30:29 +0000614 r->res_lvbptr = NULL;
615 }
616}
617
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300618/* When all references to the rsb are gone it's transferred to
David Teiglande7fd4172006-01-18 09:30:29 +0000619 the tossed list for later disposal. */
620
621static void put_rsb(struct dlm_rsb *r)
622{
623 struct dlm_ls *ls = r->res_ls;
624 uint32_t bucket = r->res_bucket;
625
David Teiglandc7be7612009-01-07 16:50:41 -0600626 spin_lock(&ls->ls_rsbtbl[bucket].lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000627 kref_put(&r->res_ref, toss_rsb);
David Teiglandc7be7612009-01-07 16:50:41 -0600628 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000629}
630
631void dlm_put_rsb(struct dlm_rsb *r)
632{
633 put_rsb(r);
634}
635
636/* See comment for unhold_lkb */
637
638static void unhold_rsb(struct dlm_rsb *r)
639{
640 int rv;
641 rv = kref_put(&r->res_ref, toss_rsb);
David Teiglanda345da32006-08-18 11:54:25 -0500642 DLM_ASSERT(!rv, dlm_dump_rsb(r););
David Teiglande7fd4172006-01-18 09:30:29 +0000643}
644
645static void kill_rsb(struct kref *kref)
646{
647 struct dlm_rsb *r = container_of(kref, struct dlm_rsb, res_ref);
648
649 /* All work is done after the return from kref_put() so we
650 can release the write_lock before the remove and free. */
651
David Teiglanda345da32006-08-18 11:54:25 -0500652 DLM_ASSERT(list_empty(&r->res_lookup), dlm_dump_rsb(r););
653 DLM_ASSERT(list_empty(&r->res_grantqueue), dlm_dump_rsb(r););
654 DLM_ASSERT(list_empty(&r->res_convertqueue), dlm_dump_rsb(r););
655 DLM_ASSERT(list_empty(&r->res_waitqueue), dlm_dump_rsb(r););
656 DLM_ASSERT(list_empty(&r->res_root_list), dlm_dump_rsb(r););
657 DLM_ASSERT(list_empty(&r->res_recover_list), dlm_dump_rsb(r););
David Teiglande7fd4172006-01-18 09:30:29 +0000658}
659
660/* Attaching/detaching lkb's from rsb's is for rsb reference counting.
661 The rsb must exist as long as any lkb's for it do. */
662
663static void attach_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb)
664{
665 hold_rsb(r);
666 lkb->lkb_resource = r;
667}
668
669static void detach_lkb(struct dlm_lkb *lkb)
670{
671 if (lkb->lkb_resource) {
672 put_rsb(lkb->lkb_resource);
673 lkb->lkb_resource = NULL;
674 }
675}
676
677static int create_lkb(struct dlm_ls *ls, struct dlm_lkb **lkb_ret)
678{
David Teigland3d6aa672011-07-06 17:00:54 -0500679 struct dlm_lkb *lkb;
680 int rv, id;
David Teiglande7fd4172006-01-18 09:30:29 +0000681
David Teigland52bda2b2007-11-07 09:06:49 -0600682 lkb = dlm_allocate_lkb(ls);
David Teiglande7fd4172006-01-18 09:30:29 +0000683 if (!lkb)
684 return -ENOMEM;
685
686 lkb->lkb_nodeid = -1;
687 lkb->lkb_grmode = DLM_LOCK_IV;
688 kref_init(&lkb->lkb_ref);
David Teigland34e22be2006-07-18 11:24:04 -0500689 INIT_LIST_HEAD(&lkb->lkb_ownqueue);
David Teiglandef0c2bb2007-03-28 09:56:46 -0500690 INIT_LIST_HEAD(&lkb->lkb_rsb_lookup);
David Teigland3ae1acf2007-05-18 08:59:31 -0500691 INIT_LIST_HEAD(&lkb->lkb_time_list);
David Teigland23e8e1a2011-04-05 13:16:24 -0500692 INIT_LIST_HEAD(&lkb->lkb_cb_list);
693 mutex_init(&lkb->lkb_cb_mutex);
694 INIT_WORK(&lkb->lkb_cb_work, dlm_callback_work);
David Teiglande7fd4172006-01-18 09:30:29 +0000695
David Teigland3d6aa672011-07-06 17:00:54 -0500696 retry:
697 rv = idr_pre_get(&ls->ls_lkbidr, GFP_NOFS);
698 if (!rv)
699 return -ENOMEM;
David Teiglande7fd4172006-01-18 09:30:29 +0000700
David Teigland3d6aa672011-07-06 17:00:54 -0500701 spin_lock(&ls->ls_lkbidr_spin);
702 rv = idr_get_new_above(&ls->ls_lkbidr, lkb, 1, &id);
703 if (!rv)
704 lkb->lkb_id = id;
705 spin_unlock(&ls->ls_lkbidr_spin);
David Teiglande7fd4172006-01-18 09:30:29 +0000706
David Teigland3d6aa672011-07-06 17:00:54 -0500707 if (rv == -EAGAIN)
708 goto retry;
David Teiglande7fd4172006-01-18 09:30:29 +0000709
David Teigland3d6aa672011-07-06 17:00:54 -0500710 if (rv < 0) {
711 log_error(ls, "create_lkb idr error %d", rv);
712 return rv;
David Teiglande7fd4172006-01-18 09:30:29 +0000713 }
714
David Teiglande7fd4172006-01-18 09:30:29 +0000715 *lkb_ret = lkb;
716 return 0;
717}
718
David Teiglande7fd4172006-01-18 09:30:29 +0000719static int find_lkb(struct dlm_ls *ls, uint32_t lkid, struct dlm_lkb **lkb_ret)
720{
721 struct dlm_lkb *lkb;
David Teiglande7fd4172006-01-18 09:30:29 +0000722
David Teigland3d6aa672011-07-06 17:00:54 -0500723 spin_lock(&ls->ls_lkbidr_spin);
724 lkb = idr_find(&ls->ls_lkbidr, lkid);
David Teiglande7fd4172006-01-18 09:30:29 +0000725 if (lkb)
726 kref_get(&lkb->lkb_ref);
David Teigland3d6aa672011-07-06 17:00:54 -0500727 spin_unlock(&ls->ls_lkbidr_spin);
David Teiglande7fd4172006-01-18 09:30:29 +0000728
729 *lkb_ret = lkb;
730 return lkb ? 0 : -ENOENT;
731}
732
733static void kill_lkb(struct kref *kref)
734{
735 struct dlm_lkb *lkb = container_of(kref, struct dlm_lkb, lkb_ref);
736
737 /* All work is done after the return from kref_put() so we
738 can release the write_lock before the detach_lkb */
739
740 DLM_ASSERT(!lkb->lkb_status, dlm_print_lkb(lkb););
741}
742
David Teiglandb3f58d82006-02-28 11:16:37 -0500743/* __put_lkb() is used when an lkb may not have an rsb attached to
744 it so we need to provide the lockspace explicitly */
745
746static int __put_lkb(struct dlm_ls *ls, struct dlm_lkb *lkb)
David Teiglande7fd4172006-01-18 09:30:29 +0000747{
David Teigland3d6aa672011-07-06 17:00:54 -0500748 uint32_t lkid = lkb->lkb_id;
David Teiglande7fd4172006-01-18 09:30:29 +0000749
David Teigland3d6aa672011-07-06 17:00:54 -0500750 spin_lock(&ls->ls_lkbidr_spin);
David Teiglande7fd4172006-01-18 09:30:29 +0000751 if (kref_put(&lkb->lkb_ref, kill_lkb)) {
David Teigland3d6aa672011-07-06 17:00:54 -0500752 idr_remove(&ls->ls_lkbidr, lkid);
753 spin_unlock(&ls->ls_lkbidr_spin);
David Teiglande7fd4172006-01-18 09:30:29 +0000754
755 detach_lkb(lkb);
756
757 /* for local/process lkbs, lvbptr points to caller's lksb */
758 if (lkb->lkb_lvbptr && is_master_copy(lkb))
David Teigland52bda2b2007-11-07 09:06:49 -0600759 dlm_free_lvb(lkb->lkb_lvbptr);
760 dlm_free_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +0000761 return 1;
762 } else {
David Teigland3d6aa672011-07-06 17:00:54 -0500763 spin_unlock(&ls->ls_lkbidr_spin);
David Teiglande7fd4172006-01-18 09:30:29 +0000764 return 0;
765 }
766}
767
768int dlm_put_lkb(struct dlm_lkb *lkb)
769{
David Teiglandb3f58d82006-02-28 11:16:37 -0500770 struct dlm_ls *ls;
771
772 DLM_ASSERT(lkb->lkb_resource, dlm_print_lkb(lkb););
773 DLM_ASSERT(lkb->lkb_resource->res_ls, dlm_print_lkb(lkb););
774
775 ls = lkb->lkb_resource->res_ls;
776 return __put_lkb(ls, lkb);
David Teiglande7fd4172006-01-18 09:30:29 +0000777}
778
779/* This is only called to add a reference when the code already holds
780 a valid reference to the lkb, so there's no need for locking. */
781
782static inline void hold_lkb(struct dlm_lkb *lkb)
783{
784 kref_get(&lkb->lkb_ref);
785}
786
787/* This is called when we need to remove a reference and are certain
788 it's not the last ref. e.g. del_lkb is always called between a
789 find_lkb/put_lkb and is always the inverse of a previous add_lkb.
790 put_lkb would work fine, but would involve unnecessary locking */
791
792static inline void unhold_lkb(struct dlm_lkb *lkb)
793{
794 int rv;
795 rv = kref_put(&lkb->lkb_ref, kill_lkb);
796 DLM_ASSERT(!rv, dlm_print_lkb(lkb););
797}
798
799static void lkb_add_ordered(struct list_head *new, struct list_head *head,
800 int mode)
801{
802 struct dlm_lkb *lkb = NULL;
803
804 list_for_each_entry(lkb, head, lkb_statequeue)
805 if (lkb->lkb_rqmode < mode)
806 break;
807
Dan Carpenter99fb19d2010-03-22 15:03:54 +0300808 __list_add(new, lkb->lkb_statequeue.prev, &lkb->lkb_statequeue);
David Teiglande7fd4172006-01-18 09:30:29 +0000809}
810
811/* add/remove lkb to rsb's grant/convert/wait queue */
812
813static void add_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb, int status)
814{
815 kref_get(&lkb->lkb_ref);
816
817 DLM_ASSERT(!lkb->lkb_status, dlm_print_lkb(lkb););
818
David Teiglandeeda4182008-12-09 14:12:21 -0600819 lkb->lkb_timestamp = ktime_get();
820
David Teiglande7fd4172006-01-18 09:30:29 +0000821 lkb->lkb_status = status;
822
823 switch (status) {
824 case DLM_LKSTS_WAITING:
825 if (lkb->lkb_exflags & DLM_LKF_HEADQUE)
826 list_add(&lkb->lkb_statequeue, &r->res_waitqueue);
827 else
828 list_add_tail(&lkb->lkb_statequeue, &r->res_waitqueue);
829 break;
830 case DLM_LKSTS_GRANTED:
831 /* convention says granted locks kept in order of grmode */
832 lkb_add_ordered(&lkb->lkb_statequeue, &r->res_grantqueue,
833 lkb->lkb_grmode);
834 break;
835 case DLM_LKSTS_CONVERT:
836 if (lkb->lkb_exflags & DLM_LKF_HEADQUE)
837 list_add(&lkb->lkb_statequeue, &r->res_convertqueue);
838 else
839 list_add_tail(&lkb->lkb_statequeue,
840 &r->res_convertqueue);
841 break;
842 default:
843 DLM_ASSERT(0, dlm_print_lkb(lkb); printk("sts=%d\n", status););
844 }
845}
846
847static void del_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb)
848{
849 lkb->lkb_status = 0;
850 list_del(&lkb->lkb_statequeue);
851 unhold_lkb(lkb);
852}
853
854static void move_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb, int sts)
855{
856 hold_lkb(lkb);
857 del_lkb(r, lkb);
858 add_lkb(r, lkb, sts);
859 unhold_lkb(lkb);
860}
861
David Teiglandef0c2bb2007-03-28 09:56:46 -0500862static int msg_reply_type(int mstype)
863{
864 switch (mstype) {
865 case DLM_MSG_REQUEST:
866 return DLM_MSG_REQUEST_REPLY;
867 case DLM_MSG_CONVERT:
868 return DLM_MSG_CONVERT_REPLY;
869 case DLM_MSG_UNLOCK:
870 return DLM_MSG_UNLOCK_REPLY;
871 case DLM_MSG_CANCEL:
872 return DLM_MSG_CANCEL_REPLY;
873 case DLM_MSG_LOOKUP:
874 return DLM_MSG_LOOKUP_REPLY;
875 }
876 return -1;
877}
878
David Teiglandc6ff6692011-03-28 14:17:26 -0500879static int nodeid_warned(int nodeid, int num_nodes, int *warned)
880{
881 int i;
882
883 for (i = 0; i < num_nodes; i++) {
884 if (!warned[i]) {
885 warned[i] = nodeid;
886 return 0;
887 }
888 if (warned[i] == nodeid)
889 return 1;
890 }
891 return 0;
892}
893
894void dlm_scan_waiters(struct dlm_ls *ls)
895{
896 struct dlm_lkb *lkb;
897 ktime_t zero = ktime_set(0, 0);
898 s64 us;
899 s64 debug_maxus = 0;
900 u32 debug_scanned = 0;
901 u32 debug_expired = 0;
902 int num_nodes = 0;
903 int *warned = NULL;
904
905 if (!dlm_config.ci_waitwarn_us)
906 return;
907
908 mutex_lock(&ls->ls_waiters_mutex);
909
910 list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
911 if (ktime_equal(lkb->lkb_wait_time, zero))
912 continue;
913
914 debug_scanned++;
915
916 us = ktime_to_us(ktime_sub(ktime_get(), lkb->lkb_wait_time));
917
918 if (us < dlm_config.ci_waitwarn_us)
919 continue;
920
921 lkb->lkb_wait_time = zero;
922
923 debug_expired++;
924 if (us > debug_maxus)
925 debug_maxus = us;
926
927 if (!num_nodes) {
928 num_nodes = ls->ls_num_nodes;
Jesper Juhl5d708282011-07-10 22:54:31 +0200929 warned = kzalloc(num_nodes * sizeof(int), GFP_KERNEL);
David Teiglandc6ff6692011-03-28 14:17:26 -0500930 }
931 if (!warned)
932 continue;
933 if (nodeid_warned(lkb->lkb_wait_nodeid, num_nodes, warned))
934 continue;
935
936 log_error(ls, "waitwarn %x %lld %d us check connection to "
937 "node %d", lkb->lkb_id, (long long)us,
938 dlm_config.ci_waitwarn_us, lkb->lkb_wait_nodeid);
939 }
940 mutex_unlock(&ls->ls_waiters_mutex);
Jesper Juhl5d708282011-07-10 22:54:31 +0200941 kfree(warned);
David Teiglandc6ff6692011-03-28 14:17:26 -0500942
943 if (debug_expired)
944 log_debug(ls, "scan_waiters %u warn %u over %d us max %lld us",
945 debug_scanned, debug_expired,
946 dlm_config.ci_waitwarn_us, (long long)debug_maxus);
947}
948
David Teiglande7fd4172006-01-18 09:30:29 +0000949/* add/remove lkb from global waiters list of lkb's waiting for
950 a reply from a remote node */
951
David Teiglandc6ff6692011-03-28 14:17:26 -0500952static int add_to_waiters(struct dlm_lkb *lkb, int mstype, int to_nodeid)
David Teiglande7fd4172006-01-18 09:30:29 +0000953{
954 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
David Teiglandef0c2bb2007-03-28 09:56:46 -0500955 int error = 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000956
David Teigland90135922006-01-20 08:47:07 +0000957 mutex_lock(&ls->ls_waiters_mutex);
David Teiglandef0c2bb2007-03-28 09:56:46 -0500958
959 if (is_overlap_unlock(lkb) ||
960 (is_overlap_cancel(lkb) && (mstype == DLM_MSG_CANCEL))) {
961 error = -EINVAL;
David Teiglande7fd4172006-01-18 09:30:29 +0000962 goto out;
963 }
David Teiglandef0c2bb2007-03-28 09:56:46 -0500964
965 if (lkb->lkb_wait_type || is_overlap_cancel(lkb)) {
966 switch (mstype) {
967 case DLM_MSG_UNLOCK:
968 lkb->lkb_flags |= DLM_IFL_OVERLAP_UNLOCK;
969 break;
970 case DLM_MSG_CANCEL:
971 lkb->lkb_flags |= DLM_IFL_OVERLAP_CANCEL;
972 break;
973 default:
974 error = -EBUSY;
975 goto out;
976 }
977 lkb->lkb_wait_count++;
978 hold_lkb(lkb);
979
David Teigland43279e52009-01-28 14:37:54 -0600980 log_debug(ls, "addwait %x cur %d overlap %d count %d f %x",
David Teiglandef0c2bb2007-03-28 09:56:46 -0500981 lkb->lkb_id, lkb->lkb_wait_type, mstype,
982 lkb->lkb_wait_count, lkb->lkb_flags);
983 goto out;
984 }
985
986 DLM_ASSERT(!lkb->lkb_wait_count,
987 dlm_print_lkb(lkb);
988 printk("wait_count %d\n", lkb->lkb_wait_count););
989
990 lkb->lkb_wait_count++;
David Teiglande7fd4172006-01-18 09:30:29 +0000991 lkb->lkb_wait_type = mstype;
David Teiglandc6ff6692011-03-28 14:17:26 -0500992 lkb->lkb_wait_time = ktime_get();
993 lkb->lkb_wait_nodeid = to_nodeid; /* for debugging */
David Teiglandef0c2bb2007-03-28 09:56:46 -0500994 hold_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +0000995 list_add(&lkb->lkb_wait_reply, &ls->ls_waiters);
996 out:
David Teiglandef0c2bb2007-03-28 09:56:46 -0500997 if (error)
David Teigland43279e52009-01-28 14:37:54 -0600998 log_error(ls, "addwait error %x %d flags %x %d %d %s",
David Teiglandef0c2bb2007-03-28 09:56:46 -0500999 lkb->lkb_id, error, lkb->lkb_flags, mstype,
1000 lkb->lkb_wait_type, lkb->lkb_resource->res_name);
David Teigland90135922006-01-20 08:47:07 +00001001 mutex_unlock(&ls->ls_waiters_mutex);
David Teiglandef0c2bb2007-03-28 09:56:46 -05001002 return error;
David Teiglande7fd4172006-01-18 09:30:29 +00001003}
1004
David Teiglandb790c3b2007-01-24 10:21:33 -06001005/* We clear the RESEND flag because we might be taking an lkb off the waiters
1006 list as part of process_requestqueue (e.g. a lookup that has an optimized
1007 request reply on the requestqueue) between dlm_recover_waiters_pre() which
1008 set RESEND and dlm_recover_waiters_post() */
1009
David Teigland43279e52009-01-28 14:37:54 -06001010static int _remove_from_waiters(struct dlm_lkb *lkb, int mstype,
1011 struct dlm_message *ms)
David Teiglande7fd4172006-01-18 09:30:29 +00001012{
David Teiglandef0c2bb2007-03-28 09:56:46 -05001013 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1014 int overlap_done = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001015
David Teiglandef0c2bb2007-03-28 09:56:46 -05001016 if (is_overlap_unlock(lkb) && (mstype == DLM_MSG_UNLOCK_REPLY)) {
David Teigland43279e52009-01-28 14:37:54 -06001017 log_debug(ls, "remwait %x unlock_reply overlap", lkb->lkb_id);
David Teiglandef0c2bb2007-03-28 09:56:46 -05001018 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
1019 overlap_done = 1;
1020 goto out_del;
David Teiglande7fd4172006-01-18 09:30:29 +00001021 }
David Teiglandef0c2bb2007-03-28 09:56:46 -05001022
1023 if (is_overlap_cancel(lkb) && (mstype == DLM_MSG_CANCEL_REPLY)) {
David Teigland43279e52009-01-28 14:37:54 -06001024 log_debug(ls, "remwait %x cancel_reply overlap", lkb->lkb_id);
David Teiglandef0c2bb2007-03-28 09:56:46 -05001025 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
1026 overlap_done = 1;
1027 goto out_del;
1028 }
1029
David Teigland43279e52009-01-28 14:37:54 -06001030 /* Cancel state was preemptively cleared by a successful convert,
1031 see next comment, nothing to do. */
1032
1033 if ((mstype == DLM_MSG_CANCEL_REPLY) &&
1034 (lkb->lkb_wait_type != DLM_MSG_CANCEL)) {
1035 log_debug(ls, "remwait %x cancel_reply wait_type %d",
1036 lkb->lkb_id, lkb->lkb_wait_type);
1037 return -1;
1038 }
1039
1040 /* Remove for the convert reply, and premptively remove for the
1041 cancel reply. A convert has been granted while there's still
1042 an outstanding cancel on it (the cancel is moot and the result
1043 in the cancel reply should be 0). We preempt the cancel reply
1044 because the app gets the convert result and then can follow up
1045 with another op, like convert. This subsequent op would see the
1046 lingering state of the cancel and fail with -EBUSY. */
1047
1048 if ((mstype == DLM_MSG_CONVERT_REPLY) &&
1049 (lkb->lkb_wait_type == DLM_MSG_CONVERT) &&
1050 is_overlap_cancel(lkb) && ms && !ms->m_result) {
1051 log_debug(ls, "remwait %x convert_reply zap overlap_cancel",
1052 lkb->lkb_id);
1053 lkb->lkb_wait_type = 0;
1054 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
1055 lkb->lkb_wait_count--;
1056 goto out_del;
1057 }
1058
David Teiglandef0c2bb2007-03-28 09:56:46 -05001059 /* N.B. type of reply may not always correspond to type of original
1060 msg due to lookup->request optimization, verify others? */
1061
1062 if (lkb->lkb_wait_type) {
1063 lkb->lkb_wait_type = 0;
1064 goto out_del;
1065 }
1066
David Teigland43279e52009-01-28 14:37:54 -06001067 log_error(ls, "remwait error %x reply %d flags %x no wait_type",
1068 lkb->lkb_id, mstype, lkb->lkb_flags);
David Teiglandef0c2bb2007-03-28 09:56:46 -05001069 return -1;
1070
1071 out_del:
1072 /* the force-unlock/cancel has completed and we haven't recvd a reply
1073 to the op that was in progress prior to the unlock/cancel; we
1074 give up on any reply to the earlier op. FIXME: not sure when/how
1075 this would happen */
1076
1077 if (overlap_done && lkb->lkb_wait_type) {
David Teigland43279e52009-01-28 14:37:54 -06001078 log_error(ls, "remwait error %x reply %d wait_type %d overlap",
David Teiglandef0c2bb2007-03-28 09:56:46 -05001079 lkb->lkb_id, mstype, lkb->lkb_wait_type);
1080 lkb->lkb_wait_count--;
1081 lkb->lkb_wait_type = 0;
1082 }
1083
1084 DLM_ASSERT(lkb->lkb_wait_count, dlm_print_lkb(lkb););
1085
David Teiglandb790c3b2007-01-24 10:21:33 -06001086 lkb->lkb_flags &= ~DLM_IFL_RESEND;
David Teiglandef0c2bb2007-03-28 09:56:46 -05001087 lkb->lkb_wait_count--;
1088 if (!lkb->lkb_wait_count)
1089 list_del_init(&lkb->lkb_wait_reply);
David Teiglande7fd4172006-01-18 09:30:29 +00001090 unhold_lkb(lkb);
David Teiglandef0c2bb2007-03-28 09:56:46 -05001091 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001092}
1093
David Teiglandef0c2bb2007-03-28 09:56:46 -05001094static int remove_from_waiters(struct dlm_lkb *lkb, int mstype)
David Teiglande7fd4172006-01-18 09:30:29 +00001095{
1096 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1097 int error;
1098
David Teigland90135922006-01-20 08:47:07 +00001099 mutex_lock(&ls->ls_waiters_mutex);
David Teigland43279e52009-01-28 14:37:54 -06001100 error = _remove_from_waiters(lkb, mstype, NULL);
David Teigland90135922006-01-20 08:47:07 +00001101 mutex_unlock(&ls->ls_waiters_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +00001102 return error;
1103}
1104
David Teiglandef0c2bb2007-03-28 09:56:46 -05001105/* Handles situations where we might be processing a "fake" or "stub" reply in
1106 which we can't try to take waiters_mutex again. */
1107
1108static int remove_from_waiters_ms(struct dlm_lkb *lkb, struct dlm_message *ms)
1109{
1110 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1111 int error;
1112
David Teigland2a7ce0e2011-04-04 15:19:59 -05001113 if (ms->m_flags != DLM_IFL_STUB_MS)
David Teiglandef0c2bb2007-03-28 09:56:46 -05001114 mutex_lock(&ls->ls_waiters_mutex);
David Teigland43279e52009-01-28 14:37:54 -06001115 error = _remove_from_waiters(lkb, ms->m_type, ms);
David Teigland2a7ce0e2011-04-04 15:19:59 -05001116 if (ms->m_flags != DLM_IFL_STUB_MS)
David Teiglandef0c2bb2007-03-28 09:56:46 -05001117 mutex_unlock(&ls->ls_waiters_mutex);
1118 return error;
1119}
1120
David Teiglande7fd4172006-01-18 09:30:29 +00001121static void dir_remove(struct dlm_rsb *r)
1122{
1123 int to_nodeid;
1124
1125 if (dlm_no_directory(r->res_ls))
1126 return;
1127
1128 to_nodeid = dlm_dir_nodeid(r);
1129 if (to_nodeid != dlm_our_nodeid())
1130 send_remove(r);
1131 else
1132 dlm_dir_remove_entry(r->res_ls, to_nodeid,
1133 r->res_name, r->res_length);
1134}
1135
Bob Peterson9beb3bf2011-10-26 15:24:55 -05001136/* FIXME: make this more efficient */
David Teiglande7fd4172006-01-18 09:30:29 +00001137
1138static int shrink_bucket(struct dlm_ls *ls, int b)
1139{
Bob Peterson9beb3bf2011-10-26 15:24:55 -05001140 struct rb_node *n;
David Teiglande7fd4172006-01-18 09:30:29 +00001141 struct dlm_rsb *r;
1142 int count = 0, found;
1143
1144 for (;;) {
David Teigland90135922006-01-20 08:47:07 +00001145 found = 0;
David Teiglandc7be7612009-01-07 16:50:41 -06001146 spin_lock(&ls->ls_rsbtbl[b].lock);
Bob Peterson9beb3bf2011-10-26 15:24:55 -05001147 for (n = rb_first(&ls->ls_rsbtbl[b].toss); n; n = rb_next(n)) {
1148 r = rb_entry(n, struct dlm_rsb, res_hashnode);
David Teiglande7fd4172006-01-18 09:30:29 +00001149 if (!time_after_eq(jiffies, r->res_toss_time +
David Teigland68c817a2007-01-09 09:41:48 -06001150 dlm_config.ci_toss_secs * HZ))
David Teiglande7fd4172006-01-18 09:30:29 +00001151 continue;
David Teigland90135922006-01-20 08:47:07 +00001152 found = 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001153 break;
1154 }
1155
1156 if (!found) {
David Teiglandc7be7612009-01-07 16:50:41 -06001157 spin_unlock(&ls->ls_rsbtbl[b].lock);
David Teiglande7fd4172006-01-18 09:30:29 +00001158 break;
1159 }
1160
1161 if (kref_put(&r->res_ref, kill_rsb)) {
Bob Peterson9beb3bf2011-10-26 15:24:55 -05001162 rb_erase(&r->res_hashnode, &ls->ls_rsbtbl[b].toss);
David Teiglandc7be7612009-01-07 16:50:41 -06001163 spin_unlock(&ls->ls_rsbtbl[b].lock);
David Teiglande7fd4172006-01-18 09:30:29 +00001164
1165 if (is_master(r))
1166 dir_remove(r);
David Teigland52bda2b2007-11-07 09:06:49 -06001167 dlm_free_rsb(r);
David Teiglande7fd4172006-01-18 09:30:29 +00001168 count++;
1169 } else {
David Teiglandc7be7612009-01-07 16:50:41 -06001170 spin_unlock(&ls->ls_rsbtbl[b].lock);
David Teiglande7fd4172006-01-18 09:30:29 +00001171 log_error(ls, "tossed rsb in use %s", r->res_name);
1172 }
1173 }
1174
1175 return count;
1176}
1177
1178void dlm_scan_rsbs(struct dlm_ls *ls)
1179{
1180 int i;
1181
David Teiglande7fd4172006-01-18 09:30:29 +00001182 for (i = 0; i < ls->ls_rsbtbl_size; i++) {
1183 shrink_bucket(ls, i);
David Teigland85e86ed2007-05-18 08:58:15 -05001184 if (dlm_locking_stopped(ls))
1185 break;
David Teiglande7fd4172006-01-18 09:30:29 +00001186 cond_resched();
1187 }
1188}
1189
David Teigland3ae1acf2007-05-18 08:59:31 -05001190static void add_timeout(struct dlm_lkb *lkb)
1191{
1192 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1193
David Teiglandeeda4182008-12-09 14:12:21 -06001194 if (is_master_copy(lkb))
David Teigland3ae1acf2007-05-18 08:59:31 -05001195 return;
David Teigland3ae1acf2007-05-18 08:59:31 -05001196
1197 if (test_bit(LSFL_TIMEWARN, &ls->ls_flags) &&
1198 !(lkb->lkb_exflags & DLM_LKF_NODLCKWT)) {
1199 lkb->lkb_flags |= DLM_IFL_WATCH_TIMEWARN;
1200 goto add_it;
1201 }
David Teigland84d8cd62007-05-29 08:44:23 -05001202 if (lkb->lkb_exflags & DLM_LKF_TIMEOUT)
1203 goto add_it;
David Teigland3ae1acf2007-05-18 08:59:31 -05001204 return;
1205
1206 add_it:
1207 DLM_ASSERT(list_empty(&lkb->lkb_time_list), dlm_print_lkb(lkb););
1208 mutex_lock(&ls->ls_timeout_mutex);
1209 hold_lkb(lkb);
David Teigland3ae1acf2007-05-18 08:59:31 -05001210 list_add_tail(&lkb->lkb_time_list, &ls->ls_timeout);
1211 mutex_unlock(&ls->ls_timeout_mutex);
1212}
1213
1214static void del_timeout(struct dlm_lkb *lkb)
1215{
1216 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1217
1218 mutex_lock(&ls->ls_timeout_mutex);
1219 if (!list_empty(&lkb->lkb_time_list)) {
1220 list_del_init(&lkb->lkb_time_list);
1221 unhold_lkb(lkb);
1222 }
1223 mutex_unlock(&ls->ls_timeout_mutex);
1224}
1225
1226/* FIXME: is it safe to look at lkb_exflags, lkb_flags, lkb_timestamp, and
1227 lkb_lksb_timeout without lock_rsb? Note: we can't lock timeout_mutex
1228 and then lock rsb because of lock ordering in add_timeout. We may need
1229 to specify some special timeout-related bits in the lkb that are just to
1230 be accessed under the timeout_mutex. */
1231
1232void dlm_scan_timeout(struct dlm_ls *ls)
1233{
1234 struct dlm_rsb *r;
1235 struct dlm_lkb *lkb;
1236 int do_cancel, do_warn;
David Teiglandeeda4182008-12-09 14:12:21 -06001237 s64 wait_us;
David Teigland3ae1acf2007-05-18 08:59:31 -05001238
1239 for (;;) {
1240 if (dlm_locking_stopped(ls))
1241 break;
1242
1243 do_cancel = 0;
1244 do_warn = 0;
1245 mutex_lock(&ls->ls_timeout_mutex);
1246 list_for_each_entry(lkb, &ls->ls_timeout, lkb_time_list) {
1247
David Teiglandeeda4182008-12-09 14:12:21 -06001248 wait_us = ktime_to_us(ktime_sub(ktime_get(),
1249 lkb->lkb_timestamp));
1250
David Teigland3ae1acf2007-05-18 08:59:31 -05001251 if ((lkb->lkb_exflags & DLM_LKF_TIMEOUT) &&
David Teiglandeeda4182008-12-09 14:12:21 -06001252 wait_us >= (lkb->lkb_timeout_cs * 10000))
David Teigland3ae1acf2007-05-18 08:59:31 -05001253 do_cancel = 1;
1254
1255 if ((lkb->lkb_flags & DLM_IFL_WATCH_TIMEWARN) &&
David Teiglandeeda4182008-12-09 14:12:21 -06001256 wait_us >= dlm_config.ci_timewarn_cs * 10000)
David Teigland3ae1acf2007-05-18 08:59:31 -05001257 do_warn = 1;
1258
1259 if (!do_cancel && !do_warn)
1260 continue;
1261 hold_lkb(lkb);
1262 break;
1263 }
1264 mutex_unlock(&ls->ls_timeout_mutex);
1265
1266 if (!do_cancel && !do_warn)
1267 break;
1268
1269 r = lkb->lkb_resource;
1270 hold_rsb(r);
1271 lock_rsb(r);
1272
1273 if (do_warn) {
1274 /* clear flag so we only warn once */
1275 lkb->lkb_flags &= ~DLM_IFL_WATCH_TIMEWARN;
1276 if (!(lkb->lkb_exflags & DLM_LKF_TIMEOUT))
1277 del_timeout(lkb);
1278 dlm_timeout_warn(lkb);
1279 }
1280
1281 if (do_cancel) {
Steven Whitehouseb3cab7b2007-05-29 11:14:21 +01001282 log_debug(ls, "timeout cancel %x node %d %s",
David Teigland639aca42007-05-18 16:02:57 -05001283 lkb->lkb_id, lkb->lkb_nodeid, r->res_name);
David Teigland3ae1acf2007-05-18 08:59:31 -05001284 lkb->lkb_flags &= ~DLM_IFL_WATCH_TIMEWARN;
1285 lkb->lkb_flags |= DLM_IFL_TIMEOUT_CANCEL;
1286 del_timeout(lkb);
1287 _cancel_lock(r, lkb);
1288 }
1289
1290 unlock_rsb(r);
1291 unhold_rsb(r);
1292 dlm_put_lkb(lkb);
1293 }
1294}
1295
1296/* This is only called by dlm_recoverd, and we rely on dlm_ls_stop() stopping
1297 dlm_recoverd before checking/setting ls_recover_begin. */
1298
1299void dlm_adjust_timeouts(struct dlm_ls *ls)
1300{
1301 struct dlm_lkb *lkb;
David Teiglandeeda4182008-12-09 14:12:21 -06001302 u64 adj_us = jiffies_to_usecs(jiffies - ls->ls_recover_begin);
David Teigland3ae1acf2007-05-18 08:59:31 -05001303
1304 ls->ls_recover_begin = 0;
1305 mutex_lock(&ls->ls_timeout_mutex);
1306 list_for_each_entry(lkb, &ls->ls_timeout, lkb_time_list)
David Teiglandeeda4182008-12-09 14:12:21 -06001307 lkb->lkb_timestamp = ktime_add_us(lkb->lkb_timestamp, adj_us);
David Teigland3ae1acf2007-05-18 08:59:31 -05001308 mutex_unlock(&ls->ls_timeout_mutex);
David Teiglandc6ff6692011-03-28 14:17:26 -05001309
1310 if (!dlm_config.ci_waitwarn_us)
1311 return;
1312
1313 mutex_lock(&ls->ls_waiters_mutex);
1314 list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
1315 if (ktime_to_us(lkb->lkb_wait_time))
1316 lkb->lkb_wait_time = ktime_get();
1317 }
1318 mutex_unlock(&ls->ls_waiters_mutex);
David Teigland3ae1acf2007-05-18 08:59:31 -05001319}
1320
David Teiglande7fd4172006-01-18 09:30:29 +00001321/* lkb is master or local copy */
1322
1323static void set_lvb_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1324{
1325 int b, len = r->res_ls->ls_lvblen;
1326
1327 /* b=1 lvb returned to caller
1328 b=0 lvb written to rsb or invalidated
1329 b=-1 do nothing */
1330
1331 b = dlm_lvb_operations[lkb->lkb_grmode + 1][lkb->lkb_rqmode + 1];
1332
1333 if (b == 1) {
1334 if (!lkb->lkb_lvbptr)
1335 return;
1336
1337 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1338 return;
1339
1340 if (!r->res_lvbptr)
1341 return;
1342
1343 memcpy(lkb->lkb_lvbptr, r->res_lvbptr, len);
1344 lkb->lkb_lvbseq = r->res_lvbseq;
1345
1346 } else if (b == 0) {
1347 if (lkb->lkb_exflags & DLM_LKF_IVVALBLK) {
1348 rsb_set_flag(r, RSB_VALNOTVALID);
1349 return;
1350 }
1351
1352 if (!lkb->lkb_lvbptr)
1353 return;
1354
1355 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1356 return;
1357
1358 if (!r->res_lvbptr)
David Teigland52bda2b2007-11-07 09:06:49 -06001359 r->res_lvbptr = dlm_allocate_lvb(r->res_ls);
David Teiglande7fd4172006-01-18 09:30:29 +00001360
1361 if (!r->res_lvbptr)
1362 return;
1363
1364 memcpy(r->res_lvbptr, lkb->lkb_lvbptr, len);
1365 r->res_lvbseq++;
1366 lkb->lkb_lvbseq = r->res_lvbseq;
1367 rsb_clear_flag(r, RSB_VALNOTVALID);
1368 }
1369
1370 if (rsb_flag(r, RSB_VALNOTVALID))
1371 lkb->lkb_sbflags |= DLM_SBF_VALNOTVALID;
1372}
1373
1374static void set_lvb_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1375{
1376 if (lkb->lkb_grmode < DLM_LOCK_PW)
1377 return;
1378
1379 if (lkb->lkb_exflags & DLM_LKF_IVVALBLK) {
1380 rsb_set_flag(r, RSB_VALNOTVALID);
1381 return;
1382 }
1383
1384 if (!lkb->lkb_lvbptr)
1385 return;
1386
1387 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1388 return;
1389
1390 if (!r->res_lvbptr)
David Teigland52bda2b2007-11-07 09:06:49 -06001391 r->res_lvbptr = dlm_allocate_lvb(r->res_ls);
David Teiglande7fd4172006-01-18 09:30:29 +00001392
1393 if (!r->res_lvbptr)
1394 return;
1395
1396 memcpy(r->res_lvbptr, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
1397 r->res_lvbseq++;
1398 rsb_clear_flag(r, RSB_VALNOTVALID);
1399}
1400
1401/* lkb is process copy (pc) */
1402
1403static void set_lvb_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb,
1404 struct dlm_message *ms)
1405{
1406 int b;
1407
1408 if (!lkb->lkb_lvbptr)
1409 return;
1410
1411 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1412 return;
1413
David Teigland597d0ca2006-07-12 16:44:04 -05001414 b = dlm_lvb_operations[lkb->lkb_grmode + 1][lkb->lkb_rqmode + 1];
David Teiglande7fd4172006-01-18 09:30:29 +00001415 if (b == 1) {
1416 int len = receive_extralen(ms);
Al Viroa9cc9152008-01-26 00:02:29 -05001417 if (len > DLM_RESNAME_MAXLEN)
1418 len = DLM_RESNAME_MAXLEN;
David Teiglande7fd4172006-01-18 09:30:29 +00001419 memcpy(lkb->lkb_lvbptr, ms->m_extra, len);
1420 lkb->lkb_lvbseq = ms->m_lvbseq;
1421 }
1422}
1423
1424/* Manipulate lkb's on rsb's convert/granted/waiting queues
1425 remove_lock -- used for unlock, removes lkb from granted
1426 revert_lock -- used for cancel, moves lkb from convert to granted
1427 grant_lock -- used for request and convert, adds lkb to granted or
1428 moves lkb from convert or waiting to granted
1429
1430 Each of these is used for master or local copy lkb's. There is
1431 also a _pc() variation used to make the corresponding change on
1432 a process copy (pc) lkb. */
1433
1434static void _remove_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1435{
1436 del_lkb(r, lkb);
1437 lkb->lkb_grmode = DLM_LOCK_IV;
1438 /* this unhold undoes the original ref from create_lkb()
1439 so this leads to the lkb being freed */
1440 unhold_lkb(lkb);
1441}
1442
1443static void remove_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1444{
1445 set_lvb_unlock(r, lkb);
1446 _remove_lock(r, lkb);
1447}
1448
1449static void remove_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb)
1450{
1451 _remove_lock(r, lkb);
1452}
1453
David Teiglandef0c2bb2007-03-28 09:56:46 -05001454/* returns: 0 did nothing
1455 1 moved lock to granted
1456 -1 removed lock */
1457
1458static int revert_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
David Teiglande7fd4172006-01-18 09:30:29 +00001459{
David Teiglandef0c2bb2007-03-28 09:56:46 -05001460 int rv = 0;
1461
David Teiglande7fd4172006-01-18 09:30:29 +00001462 lkb->lkb_rqmode = DLM_LOCK_IV;
1463
1464 switch (lkb->lkb_status) {
David Teigland597d0ca2006-07-12 16:44:04 -05001465 case DLM_LKSTS_GRANTED:
1466 break;
David Teiglande7fd4172006-01-18 09:30:29 +00001467 case DLM_LKSTS_CONVERT:
1468 move_lkb(r, lkb, DLM_LKSTS_GRANTED);
David Teiglandef0c2bb2007-03-28 09:56:46 -05001469 rv = 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001470 break;
1471 case DLM_LKSTS_WAITING:
1472 del_lkb(r, lkb);
1473 lkb->lkb_grmode = DLM_LOCK_IV;
1474 /* this unhold undoes the original ref from create_lkb()
1475 so this leads to the lkb being freed */
1476 unhold_lkb(lkb);
David Teiglandef0c2bb2007-03-28 09:56:46 -05001477 rv = -1;
David Teiglande7fd4172006-01-18 09:30:29 +00001478 break;
1479 default:
1480 log_print("invalid status for revert %d", lkb->lkb_status);
1481 }
David Teiglandef0c2bb2007-03-28 09:56:46 -05001482 return rv;
David Teiglande7fd4172006-01-18 09:30:29 +00001483}
1484
David Teiglandef0c2bb2007-03-28 09:56:46 -05001485static int revert_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb)
David Teiglande7fd4172006-01-18 09:30:29 +00001486{
David Teiglandef0c2bb2007-03-28 09:56:46 -05001487 return revert_lock(r, lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00001488}
1489
1490static void _grant_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1491{
1492 if (lkb->lkb_grmode != lkb->lkb_rqmode) {
1493 lkb->lkb_grmode = lkb->lkb_rqmode;
1494 if (lkb->lkb_status)
1495 move_lkb(r, lkb, DLM_LKSTS_GRANTED);
1496 else
1497 add_lkb(r, lkb, DLM_LKSTS_GRANTED);
1498 }
1499
1500 lkb->lkb_rqmode = DLM_LOCK_IV;
David Teiglande7fd4172006-01-18 09:30:29 +00001501}
1502
1503static void grant_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1504{
1505 set_lvb_lock(r, lkb);
1506 _grant_lock(r, lkb);
1507 lkb->lkb_highbast = 0;
1508}
1509
1510static void grant_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb,
1511 struct dlm_message *ms)
1512{
1513 set_lvb_lock_pc(r, lkb, ms);
1514 _grant_lock(r, lkb);
1515}
1516
1517/* called by grant_pending_locks() which means an async grant message must
1518 be sent to the requesting node in addition to granting the lock if the
1519 lkb belongs to a remote node. */
1520
1521static void grant_lock_pending(struct dlm_rsb *r, struct dlm_lkb *lkb)
1522{
1523 grant_lock(r, lkb);
1524 if (is_master_copy(lkb))
1525 send_grant(r, lkb);
1526 else
1527 queue_cast(r, lkb, 0);
1528}
1529
David Teigland7d3c1fe2007-04-19 10:30:41 -05001530/* The special CONVDEADLK, ALTPR and ALTCW flags allow the master to
1531 change the granted/requested modes. We're munging things accordingly in
1532 the process copy.
1533 CONVDEADLK: our grmode may have been forced down to NL to resolve a
1534 conversion deadlock
1535 ALTPR/ALTCW: our rqmode may have been changed to PR or CW to become
1536 compatible with other granted locks */
1537
David Teigland2a7ce0e2011-04-04 15:19:59 -05001538static void munge_demoted(struct dlm_lkb *lkb)
David Teigland7d3c1fe2007-04-19 10:30:41 -05001539{
David Teigland7d3c1fe2007-04-19 10:30:41 -05001540 if (lkb->lkb_rqmode == DLM_LOCK_IV || lkb->lkb_grmode == DLM_LOCK_IV) {
1541 log_print("munge_demoted %x invalid modes gr %d rq %d",
1542 lkb->lkb_id, lkb->lkb_grmode, lkb->lkb_rqmode);
1543 return;
1544 }
1545
1546 lkb->lkb_grmode = DLM_LOCK_NL;
1547}
1548
1549static void munge_altmode(struct dlm_lkb *lkb, struct dlm_message *ms)
1550{
1551 if (ms->m_type != DLM_MSG_REQUEST_REPLY &&
1552 ms->m_type != DLM_MSG_GRANT) {
1553 log_print("munge_altmode %x invalid reply type %d",
1554 lkb->lkb_id, ms->m_type);
1555 return;
1556 }
1557
1558 if (lkb->lkb_exflags & DLM_LKF_ALTPR)
1559 lkb->lkb_rqmode = DLM_LOCK_PR;
1560 else if (lkb->lkb_exflags & DLM_LKF_ALTCW)
1561 lkb->lkb_rqmode = DLM_LOCK_CW;
1562 else {
1563 log_print("munge_altmode invalid exflags %x", lkb->lkb_exflags);
1564 dlm_print_lkb(lkb);
1565 }
1566}
1567
David Teiglande7fd4172006-01-18 09:30:29 +00001568static inline int first_in_list(struct dlm_lkb *lkb, struct list_head *head)
1569{
1570 struct dlm_lkb *first = list_entry(head->next, struct dlm_lkb,
1571 lkb_statequeue);
1572 if (lkb->lkb_id == first->lkb_id)
David Teigland90135922006-01-20 08:47:07 +00001573 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001574
David Teigland90135922006-01-20 08:47:07 +00001575 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001576}
1577
David Teiglande7fd4172006-01-18 09:30:29 +00001578/* Check if the given lkb conflicts with another lkb on the queue. */
1579
1580static int queue_conflict(struct list_head *head, struct dlm_lkb *lkb)
1581{
1582 struct dlm_lkb *this;
1583
1584 list_for_each_entry(this, head, lkb_statequeue) {
1585 if (this == lkb)
1586 continue;
David Teigland3bcd3682006-02-23 09:56:38 +00001587 if (!modes_compat(this, lkb))
David Teigland90135922006-01-20 08:47:07 +00001588 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001589 }
David Teigland90135922006-01-20 08:47:07 +00001590 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001591}
1592
1593/*
1594 * "A conversion deadlock arises with a pair of lock requests in the converting
1595 * queue for one resource. The granted mode of each lock blocks the requested
1596 * mode of the other lock."
1597 *
David Teiglandc85d65e2007-05-18 09:01:26 -05001598 * Part 2: if the granted mode of lkb is preventing an earlier lkb in the
1599 * convert queue from being granted, then deadlk/demote lkb.
David Teiglande7fd4172006-01-18 09:30:29 +00001600 *
1601 * Example:
1602 * Granted Queue: empty
1603 * Convert Queue: NL->EX (first lock)
1604 * PR->EX (second lock)
1605 *
1606 * The first lock can't be granted because of the granted mode of the second
1607 * lock and the second lock can't be granted because it's not first in the
David Teiglandc85d65e2007-05-18 09:01:26 -05001608 * list. We either cancel lkb's conversion (PR->EX) and return EDEADLK, or we
1609 * demote the granted mode of lkb (from PR to NL) if it has the CONVDEADLK
1610 * flag set and return DEMOTED in the lksb flags.
David Teiglande7fd4172006-01-18 09:30:29 +00001611 *
David Teiglandc85d65e2007-05-18 09:01:26 -05001612 * Originally, this function detected conv-deadlk in a more limited scope:
1613 * - if !modes_compat(lkb1, lkb2) && !modes_compat(lkb2, lkb1), or
1614 * - if lkb1 was the first entry in the queue (not just earlier), and was
1615 * blocked by the granted mode of lkb2, and there was nothing on the
1616 * granted queue preventing lkb1 from being granted immediately, i.e.
1617 * lkb2 was the only thing preventing lkb1 from being granted.
1618 *
1619 * That second condition meant we'd only say there was conv-deadlk if
1620 * resolving it (by demotion) would lead to the first lock on the convert
1621 * queue being granted right away. It allowed conversion deadlocks to exist
1622 * between locks on the convert queue while they couldn't be granted anyway.
1623 *
1624 * Now, we detect and take action on conversion deadlocks immediately when
1625 * they're created, even if they may not be immediately consequential. If
1626 * lkb1 exists anywhere in the convert queue and lkb2 comes in with a granted
1627 * mode that would prevent lkb1's conversion from being granted, we do a
1628 * deadlk/demote on lkb2 right away and don't let it onto the convert queue.
1629 * I think this means that the lkb_is_ahead condition below should always
1630 * be zero, i.e. there will never be conv-deadlk between two locks that are
1631 * both already on the convert queue.
David Teiglande7fd4172006-01-18 09:30:29 +00001632 */
1633
David Teiglandc85d65e2007-05-18 09:01:26 -05001634static int conversion_deadlock_detect(struct dlm_rsb *r, struct dlm_lkb *lkb2)
David Teiglande7fd4172006-01-18 09:30:29 +00001635{
David Teiglandc85d65e2007-05-18 09:01:26 -05001636 struct dlm_lkb *lkb1;
1637 int lkb_is_ahead = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001638
David Teiglandc85d65e2007-05-18 09:01:26 -05001639 list_for_each_entry(lkb1, &r->res_convertqueue, lkb_statequeue) {
1640 if (lkb1 == lkb2) {
1641 lkb_is_ahead = 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001642 continue;
1643 }
1644
David Teiglandc85d65e2007-05-18 09:01:26 -05001645 if (!lkb_is_ahead) {
1646 if (!modes_compat(lkb2, lkb1))
1647 return 1;
1648 } else {
1649 if (!modes_compat(lkb2, lkb1) &&
1650 !modes_compat(lkb1, lkb2))
1651 return 1;
1652 }
David Teiglande7fd4172006-01-18 09:30:29 +00001653 }
David Teigland90135922006-01-20 08:47:07 +00001654 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001655}
1656
1657/*
1658 * Return 1 if the lock can be granted, 0 otherwise.
1659 * Also detect and resolve conversion deadlocks.
1660 *
1661 * lkb is the lock to be granted
1662 *
1663 * now is 1 if the function is being called in the context of the
1664 * immediate request, it is 0 if called later, after the lock has been
1665 * queued.
1666 *
1667 * References are from chapter 6 of "VAXcluster Principles" by Roy Davis
1668 */
1669
1670static int _can_be_granted(struct dlm_rsb *r, struct dlm_lkb *lkb, int now)
1671{
1672 int8_t conv = (lkb->lkb_grmode != DLM_LOCK_IV);
1673
1674 /*
1675 * 6-10: Version 5.4 introduced an option to address the phenomenon of
1676 * a new request for a NL mode lock being blocked.
1677 *
1678 * 6-11: If the optional EXPEDITE flag is used with the new NL mode
1679 * request, then it would be granted. In essence, the use of this flag
1680 * tells the Lock Manager to expedite theis request by not considering
1681 * what may be in the CONVERTING or WAITING queues... As of this
1682 * writing, the EXPEDITE flag can be used only with new requests for NL
1683 * mode locks. This flag is not valid for conversion requests.
1684 *
1685 * A shortcut. Earlier checks return an error if EXPEDITE is used in a
1686 * conversion or used with a non-NL requested mode. We also know an
1687 * EXPEDITE request is always granted immediately, so now must always
1688 * be 1. The full condition to grant an expedite request: (now &&
1689 * !conv && lkb->rqmode == DLM_LOCK_NL && (flags & EXPEDITE)) can
1690 * therefore be shortened to just checking the flag.
1691 */
1692
1693 if (lkb->lkb_exflags & DLM_LKF_EXPEDITE)
David Teigland90135922006-01-20 08:47:07 +00001694 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001695
1696 /*
1697 * A shortcut. Without this, !queue_conflict(grantqueue, lkb) would be
1698 * added to the remaining conditions.
1699 */
1700
1701 if (queue_conflict(&r->res_grantqueue, lkb))
1702 goto out;
1703
1704 /*
1705 * 6-3: By default, a conversion request is immediately granted if the
1706 * requested mode is compatible with the modes of all other granted
1707 * locks
1708 */
1709
1710 if (queue_conflict(&r->res_convertqueue, lkb))
1711 goto out;
1712
1713 /*
1714 * 6-5: But the default algorithm for deciding whether to grant or
1715 * queue conversion requests does not by itself guarantee that such
1716 * requests are serviced on a "first come first serve" basis. This, in
1717 * turn, can lead to a phenomenon known as "indefinate postponement".
1718 *
1719 * 6-7: This issue is dealt with by using the optional QUECVT flag with
1720 * the system service employed to request a lock conversion. This flag
1721 * forces certain conversion requests to be queued, even if they are
1722 * compatible with the granted modes of other locks on the same
1723 * resource. Thus, the use of this flag results in conversion requests
1724 * being ordered on a "first come first servce" basis.
1725 *
1726 * DCT: This condition is all about new conversions being able to occur
1727 * "in place" while the lock remains on the granted queue (assuming
1728 * nothing else conflicts.) IOW if QUECVT isn't set, a conversion
1729 * doesn't _have_ to go onto the convert queue where it's processed in
1730 * order. The "now" variable is necessary to distinguish converts
1731 * being received and processed for the first time now, because once a
1732 * convert is moved to the conversion queue the condition below applies
1733 * requiring fifo granting.
1734 */
1735
1736 if (now && conv && !(lkb->lkb_exflags & DLM_LKF_QUECVT))
David Teigland90135922006-01-20 08:47:07 +00001737 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001738
1739 /*
David Teigland3bcd3682006-02-23 09:56:38 +00001740 * The NOORDER flag is set to avoid the standard vms rules on grant
1741 * order.
David Teiglande7fd4172006-01-18 09:30:29 +00001742 */
1743
1744 if (lkb->lkb_exflags & DLM_LKF_NOORDER)
David Teigland90135922006-01-20 08:47:07 +00001745 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001746
1747 /*
1748 * 6-3: Once in that queue [CONVERTING], a conversion request cannot be
1749 * granted until all other conversion requests ahead of it are granted
1750 * and/or canceled.
1751 */
1752
1753 if (!now && conv && first_in_list(lkb, &r->res_convertqueue))
David Teigland90135922006-01-20 08:47:07 +00001754 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001755
1756 /*
1757 * 6-4: By default, a new request is immediately granted only if all
1758 * three of the following conditions are satisfied when the request is
1759 * issued:
1760 * - The queue of ungranted conversion requests for the resource is
1761 * empty.
1762 * - The queue of ungranted new requests for the resource is empty.
1763 * - The mode of the new request is compatible with the most
1764 * restrictive mode of all granted locks on the resource.
1765 */
1766
1767 if (now && !conv && list_empty(&r->res_convertqueue) &&
1768 list_empty(&r->res_waitqueue))
David Teigland90135922006-01-20 08:47:07 +00001769 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001770
1771 /*
1772 * 6-4: Once a lock request is in the queue of ungranted new requests,
1773 * it cannot be granted until the queue of ungranted conversion
1774 * requests is empty, all ungranted new requests ahead of it are
1775 * granted and/or canceled, and it is compatible with the granted mode
1776 * of the most restrictive lock granted on the resource.
1777 */
1778
1779 if (!now && !conv && list_empty(&r->res_convertqueue) &&
1780 first_in_list(lkb, &r->res_waitqueue))
David Teigland90135922006-01-20 08:47:07 +00001781 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001782 out:
David Teigland90135922006-01-20 08:47:07 +00001783 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001784}
1785
David Teiglandc85d65e2007-05-18 09:01:26 -05001786static int can_be_granted(struct dlm_rsb *r, struct dlm_lkb *lkb, int now,
1787 int *err)
David Teiglande7fd4172006-01-18 09:30:29 +00001788{
David Teiglande7fd4172006-01-18 09:30:29 +00001789 int rv;
1790 int8_t alt = 0, rqmode = lkb->lkb_rqmode;
David Teiglandc85d65e2007-05-18 09:01:26 -05001791 int8_t is_convert = (lkb->lkb_grmode != DLM_LOCK_IV);
1792
1793 if (err)
1794 *err = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001795
1796 rv = _can_be_granted(r, lkb, now);
1797 if (rv)
1798 goto out;
1799
David Teiglandc85d65e2007-05-18 09:01:26 -05001800 /*
1801 * The CONVDEADLK flag is non-standard and tells the dlm to resolve
1802 * conversion deadlocks by demoting grmode to NL, otherwise the dlm
1803 * cancels one of the locks.
1804 */
David Teiglande7fd4172006-01-18 09:30:29 +00001805
David Teiglandc85d65e2007-05-18 09:01:26 -05001806 if (is_convert && can_be_queued(lkb) &&
1807 conversion_deadlock_detect(r, lkb)) {
1808 if (lkb->lkb_exflags & DLM_LKF_CONVDEADLK) {
1809 lkb->lkb_grmode = DLM_LOCK_NL;
1810 lkb->lkb_sbflags |= DLM_SBF_DEMOTED;
1811 } else if (!(lkb->lkb_exflags & DLM_LKF_NODLCKWT)) {
1812 if (err)
1813 *err = -EDEADLK;
1814 else {
1815 log_print("can_be_granted deadlock %x now %d",
1816 lkb->lkb_id, now);
1817 dlm_dump_rsb(r);
1818 }
1819 }
1820 goto out;
1821 }
1822
1823 /*
1824 * The ALTPR and ALTCW flags are non-standard and tell the dlm to try
1825 * to grant a request in a mode other than the normal rqmode. It's a
1826 * simple way to provide a big optimization to applications that can
1827 * use them.
1828 */
1829
1830 if (rqmode != DLM_LOCK_PR && (lkb->lkb_exflags & DLM_LKF_ALTPR))
David Teiglande7fd4172006-01-18 09:30:29 +00001831 alt = DLM_LOCK_PR;
David Teiglandc85d65e2007-05-18 09:01:26 -05001832 else if (rqmode != DLM_LOCK_CW && (lkb->lkb_exflags & DLM_LKF_ALTCW))
David Teiglande7fd4172006-01-18 09:30:29 +00001833 alt = DLM_LOCK_CW;
1834
1835 if (alt) {
1836 lkb->lkb_rqmode = alt;
1837 rv = _can_be_granted(r, lkb, now);
1838 if (rv)
1839 lkb->lkb_sbflags |= DLM_SBF_ALTMODE;
1840 else
1841 lkb->lkb_rqmode = rqmode;
1842 }
1843 out:
1844 return rv;
1845}
1846
David Teiglandc85d65e2007-05-18 09:01:26 -05001847/* FIXME: I don't think that can_be_granted() can/will demote or find deadlock
1848 for locks pending on the convert list. Once verified (watch for these
1849 log_prints), we should be able to just call _can_be_granted() and not
1850 bother with the demote/deadlk cases here (and there's no easy way to deal
1851 with a deadlk here, we'd have to generate something like grant_lock with
1852 the deadlk error.) */
1853
David Teigland36509252007-08-07 09:44:48 -05001854/* Returns the highest requested mode of all blocked conversions; sets
1855 cw if there's a blocked conversion to DLM_LOCK_CW. */
David Teiglandc85d65e2007-05-18 09:01:26 -05001856
David Teigland36509252007-08-07 09:44:48 -05001857static int grant_pending_convert(struct dlm_rsb *r, int high, int *cw)
David Teiglande7fd4172006-01-18 09:30:29 +00001858{
1859 struct dlm_lkb *lkb, *s;
1860 int hi, demoted, quit, grant_restart, demote_restart;
David Teiglandc85d65e2007-05-18 09:01:26 -05001861 int deadlk;
David Teiglande7fd4172006-01-18 09:30:29 +00001862
1863 quit = 0;
1864 restart:
1865 grant_restart = 0;
1866 demote_restart = 0;
1867 hi = DLM_LOCK_IV;
1868
1869 list_for_each_entry_safe(lkb, s, &r->res_convertqueue, lkb_statequeue) {
1870 demoted = is_demoted(lkb);
David Teiglandc85d65e2007-05-18 09:01:26 -05001871 deadlk = 0;
1872
1873 if (can_be_granted(r, lkb, 0, &deadlk)) {
David Teiglande7fd4172006-01-18 09:30:29 +00001874 grant_lock_pending(r, lkb);
1875 grant_restart = 1;
David Teiglandc85d65e2007-05-18 09:01:26 -05001876 continue;
David Teiglande7fd4172006-01-18 09:30:29 +00001877 }
David Teiglandc85d65e2007-05-18 09:01:26 -05001878
1879 if (!demoted && is_demoted(lkb)) {
1880 log_print("WARN: pending demoted %x node %d %s",
1881 lkb->lkb_id, lkb->lkb_nodeid, r->res_name);
1882 demote_restart = 1;
1883 continue;
1884 }
1885
1886 if (deadlk) {
1887 log_print("WARN: pending deadlock %x node %d %s",
1888 lkb->lkb_id, lkb->lkb_nodeid, r->res_name);
1889 dlm_dump_rsb(r);
1890 continue;
1891 }
1892
1893 hi = max_t(int, lkb->lkb_rqmode, hi);
David Teigland36509252007-08-07 09:44:48 -05001894
1895 if (cw && lkb->lkb_rqmode == DLM_LOCK_CW)
1896 *cw = 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001897 }
1898
1899 if (grant_restart)
1900 goto restart;
1901 if (demote_restart && !quit) {
1902 quit = 1;
1903 goto restart;
1904 }
1905
1906 return max_t(int, high, hi);
1907}
1908
David Teigland36509252007-08-07 09:44:48 -05001909static int grant_pending_wait(struct dlm_rsb *r, int high, int *cw)
David Teiglande7fd4172006-01-18 09:30:29 +00001910{
1911 struct dlm_lkb *lkb, *s;
1912
1913 list_for_each_entry_safe(lkb, s, &r->res_waitqueue, lkb_statequeue) {
David Teiglandc85d65e2007-05-18 09:01:26 -05001914 if (can_be_granted(r, lkb, 0, NULL))
David Teiglande7fd4172006-01-18 09:30:29 +00001915 grant_lock_pending(r, lkb);
David Teigland36509252007-08-07 09:44:48 -05001916 else {
David Teiglande7fd4172006-01-18 09:30:29 +00001917 high = max_t(int, lkb->lkb_rqmode, high);
David Teigland36509252007-08-07 09:44:48 -05001918 if (lkb->lkb_rqmode == DLM_LOCK_CW)
1919 *cw = 1;
1920 }
David Teiglande7fd4172006-01-18 09:30:29 +00001921 }
1922
1923 return high;
1924}
1925
David Teigland36509252007-08-07 09:44:48 -05001926/* cw of 1 means there's a lock with a rqmode of DLM_LOCK_CW that's blocked
1927 on either the convert or waiting queue.
1928 high is the largest rqmode of all locks blocked on the convert or
1929 waiting queue. */
1930
1931static int lock_requires_bast(struct dlm_lkb *gr, int high, int cw)
1932{
1933 if (gr->lkb_grmode == DLM_LOCK_PR && cw) {
1934 if (gr->lkb_highbast < DLM_LOCK_EX)
1935 return 1;
1936 return 0;
1937 }
1938
1939 if (gr->lkb_highbast < high &&
1940 !__dlm_compat_matrix[gr->lkb_grmode+1][high+1])
1941 return 1;
1942 return 0;
1943}
1944
David Teiglande7fd4172006-01-18 09:30:29 +00001945static void grant_pending_locks(struct dlm_rsb *r)
1946{
1947 struct dlm_lkb *lkb, *s;
1948 int high = DLM_LOCK_IV;
David Teigland36509252007-08-07 09:44:48 -05001949 int cw = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001950
David Teiglanda345da32006-08-18 11:54:25 -05001951 DLM_ASSERT(is_master(r), dlm_dump_rsb(r););
David Teiglande7fd4172006-01-18 09:30:29 +00001952
David Teigland36509252007-08-07 09:44:48 -05001953 high = grant_pending_convert(r, high, &cw);
1954 high = grant_pending_wait(r, high, &cw);
David Teiglande7fd4172006-01-18 09:30:29 +00001955
1956 if (high == DLM_LOCK_IV)
1957 return;
1958
1959 /*
1960 * If there are locks left on the wait/convert queue then send blocking
1961 * ASTs to granted locks based on the largest requested mode (high)
David Teigland36509252007-08-07 09:44:48 -05001962 * found above.
David Teiglande7fd4172006-01-18 09:30:29 +00001963 */
1964
1965 list_for_each_entry_safe(lkb, s, &r->res_grantqueue, lkb_statequeue) {
David Teiglande5dae542008-02-06 00:35:45 -06001966 if (lkb->lkb_bastfn && lock_requires_bast(lkb, high, cw)) {
David Teigland329fc4c2008-05-20 12:18:10 -05001967 if (cw && high == DLM_LOCK_PR &&
1968 lkb->lkb_grmode == DLM_LOCK_PR)
David Teigland36509252007-08-07 09:44:48 -05001969 queue_bast(r, lkb, DLM_LOCK_CW);
1970 else
1971 queue_bast(r, lkb, high);
David Teiglande7fd4172006-01-18 09:30:29 +00001972 lkb->lkb_highbast = high;
1973 }
1974 }
1975}
1976
David Teigland36509252007-08-07 09:44:48 -05001977static int modes_require_bast(struct dlm_lkb *gr, struct dlm_lkb *rq)
1978{
1979 if ((gr->lkb_grmode == DLM_LOCK_PR && rq->lkb_rqmode == DLM_LOCK_CW) ||
1980 (gr->lkb_grmode == DLM_LOCK_CW && rq->lkb_rqmode == DLM_LOCK_PR)) {
1981 if (gr->lkb_highbast < DLM_LOCK_EX)
1982 return 1;
1983 return 0;
1984 }
1985
1986 if (gr->lkb_highbast < rq->lkb_rqmode && !modes_compat(gr, rq))
1987 return 1;
1988 return 0;
1989}
1990
David Teiglande7fd4172006-01-18 09:30:29 +00001991static void send_bast_queue(struct dlm_rsb *r, struct list_head *head,
1992 struct dlm_lkb *lkb)
1993{
1994 struct dlm_lkb *gr;
1995
1996 list_for_each_entry(gr, head, lkb_statequeue) {
Steven Whitehouse314dd2a2010-09-03 10:07:48 -05001997 /* skip self when sending basts to convertqueue */
1998 if (gr == lkb)
1999 continue;
David Teiglande5dae542008-02-06 00:35:45 -06002000 if (gr->lkb_bastfn && modes_require_bast(gr, lkb)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002001 queue_bast(r, gr, lkb->lkb_rqmode);
2002 gr->lkb_highbast = lkb->lkb_rqmode;
2003 }
2004 }
2005}
2006
2007static void send_blocking_asts(struct dlm_rsb *r, struct dlm_lkb *lkb)
2008{
2009 send_bast_queue(r, &r->res_grantqueue, lkb);
2010}
2011
2012static void send_blocking_asts_all(struct dlm_rsb *r, struct dlm_lkb *lkb)
2013{
2014 send_bast_queue(r, &r->res_grantqueue, lkb);
2015 send_bast_queue(r, &r->res_convertqueue, lkb);
2016}
2017
2018/* set_master(r, lkb) -- set the master nodeid of a resource
2019
2020 The purpose of this function is to set the nodeid field in the given
2021 lkb using the nodeid field in the given rsb. If the rsb's nodeid is
2022 known, it can just be copied to the lkb and the function will return
2023 0. If the rsb's nodeid is _not_ known, it needs to be looked up
2024 before it can be copied to the lkb.
2025
2026 When the rsb nodeid is being looked up remotely, the initial lkb
2027 causing the lookup is kept on the ls_waiters list waiting for the
2028 lookup reply. Other lkb's waiting for the same rsb lookup are kept
2029 on the rsb's res_lookup list until the master is verified.
2030
2031 Return values:
2032 0: nodeid is set in rsb/lkb and the caller should go ahead and use it
2033 1: the rsb master is not available and the lkb has been placed on
2034 a wait queue
2035*/
2036
2037static int set_master(struct dlm_rsb *r, struct dlm_lkb *lkb)
2038{
2039 struct dlm_ls *ls = r->res_ls;
David Teigland755b5eb2008-01-09 10:37:39 -06002040 int i, error, dir_nodeid, ret_nodeid, our_nodeid = dlm_our_nodeid();
David Teiglande7fd4172006-01-18 09:30:29 +00002041
2042 if (rsb_flag(r, RSB_MASTER_UNCERTAIN)) {
2043 rsb_clear_flag(r, RSB_MASTER_UNCERTAIN);
2044 r->res_first_lkid = lkb->lkb_id;
2045 lkb->lkb_nodeid = r->res_nodeid;
2046 return 0;
2047 }
2048
2049 if (r->res_first_lkid && r->res_first_lkid != lkb->lkb_id) {
2050 list_add_tail(&lkb->lkb_rsb_lookup, &r->res_lookup);
2051 return 1;
2052 }
2053
2054 if (r->res_nodeid == 0) {
2055 lkb->lkb_nodeid = 0;
2056 return 0;
2057 }
2058
2059 if (r->res_nodeid > 0) {
2060 lkb->lkb_nodeid = r->res_nodeid;
2061 return 0;
2062 }
2063
David Teiglanda345da32006-08-18 11:54:25 -05002064 DLM_ASSERT(r->res_nodeid == -1, dlm_dump_rsb(r););
David Teiglande7fd4172006-01-18 09:30:29 +00002065
2066 dir_nodeid = dlm_dir_nodeid(r);
2067
2068 if (dir_nodeid != our_nodeid) {
2069 r->res_first_lkid = lkb->lkb_id;
2070 send_lookup(r, lkb);
2071 return 1;
2072 }
2073
David Teigland755b5eb2008-01-09 10:37:39 -06002074 for (i = 0; i < 2; i++) {
David Teiglande7fd4172006-01-18 09:30:29 +00002075 /* It's possible for dlm_scand to remove an old rsb for
2076 this same resource from the toss list, us to create
2077 a new one, look up the master locally, and find it
2078 already exists just before dlm_scand does the
2079 dir_remove() on the previous rsb. */
2080
2081 error = dlm_dir_lookup(ls, our_nodeid, r->res_name,
2082 r->res_length, &ret_nodeid);
2083 if (!error)
2084 break;
2085 log_debug(ls, "dir_lookup error %d %s", error, r->res_name);
2086 schedule();
2087 }
David Teigland755b5eb2008-01-09 10:37:39 -06002088 if (error && error != -EEXIST)
2089 return error;
David Teiglande7fd4172006-01-18 09:30:29 +00002090
2091 if (ret_nodeid == our_nodeid) {
2092 r->res_first_lkid = 0;
2093 r->res_nodeid = 0;
2094 lkb->lkb_nodeid = 0;
2095 } else {
2096 r->res_first_lkid = lkb->lkb_id;
2097 r->res_nodeid = ret_nodeid;
2098 lkb->lkb_nodeid = ret_nodeid;
2099 }
2100 return 0;
2101}
2102
2103static void process_lookup_list(struct dlm_rsb *r)
2104{
2105 struct dlm_lkb *lkb, *safe;
2106
2107 list_for_each_entry_safe(lkb, safe, &r->res_lookup, lkb_rsb_lookup) {
David Teiglandef0c2bb2007-03-28 09:56:46 -05002108 list_del_init(&lkb->lkb_rsb_lookup);
David Teiglande7fd4172006-01-18 09:30:29 +00002109 _request_lock(r, lkb);
2110 schedule();
2111 }
2112}
2113
2114/* confirm_master -- confirm (or deny) an rsb's master nodeid */
2115
2116static void confirm_master(struct dlm_rsb *r, int error)
2117{
2118 struct dlm_lkb *lkb;
2119
2120 if (!r->res_first_lkid)
2121 return;
2122
2123 switch (error) {
2124 case 0:
2125 case -EINPROGRESS:
2126 r->res_first_lkid = 0;
2127 process_lookup_list(r);
2128 break;
2129
2130 case -EAGAIN:
David Teiglandaec64e12008-01-08 15:37:47 -06002131 case -EBADR:
2132 case -ENOTBLK:
2133 /* the remote request failed and won't be retried (it was
2134 a NOQUEUE, or has been canceled/unlocked); make a waiting
2135 lkb the first_lkid */
David Teiglande7fd4172006-01-18 09:30:29 +00002136
2137 r->res_first_lkid = 0;
2138
2139 if (!list_empty(&r->res_lookup)) {
2140 lkb = list_entry(r->res_lookup.next, struct dlm_lkb,
2141 lkb_rsb_lookup);
David Teiglandef0c2bb2007-03-28 09:56:46 -05002142 list_del_init(&lkb->lkb_rsb_lookup);
David Teiglande7fd4172006-01-18 09:30:29 +00002143 r->res_first_lkid = lkb->lkb_id;
2144 _request_lock(r, lkb);
David Teigland761b9d32008-02-21 11:25:42 -06002145 }
David Teiglande7fd4172006-01-18 09:30:29 +00002146 break;
2147
2148 default:
2149 log_error(r->res_ls, "confirm_master unknown error %d", error);
2150 }
2151}
2152
2153static int set_lock_args(int mode, struct dlm_lksb *lksb, uint32_t flags,
David Teiglande5dae542008-02-06 00:35:45 -06002154 int namelen, unsigned long timeout_cs,
2155 void (*ast) (void *astparam),
2156 void *astparam,
2157 void (*bast) (void *astparam, int mode),
2158 struct dlm_args *args)
David Teiglande7fd4172006-01-18 09:30:29 +00002159{
2160 int rv = -EINVAL;
2161
2162 /* check for invalid arg usage */
2163
2164 if (mode < 0 || mode > DLM_LOCK_EX)
2165 goto out;
2166
2167 if (!(flags & DLM_LKF_CONVERT) && (namelen > DLM_RESNAME_MAXLEN))
2168 goto out;
2169
2170 if (flags & DLM_LKF_CANCEL)
2171 goto out;
2172
2173 if (flags & DLM_LKF_QUECVT && !(flags & DLM_LKF_CONVERT))
2174 goto out;
2175
2176 if (flags & DLM_LKF_CONVDEADLK && !(flags & DLM_LKF_CONVERT))
2177 goto out;
2178
2179 if (flags & DLM_LKF_CONVDEADLK && flags & DLM_LKF_NOQUEUE)
2180 goto out;
2181
2182 if (flags & DLM_LKF_EXPEDITE && flags & DLM_LKF_CONVERT)
2183 goto out;
2184
2185 if (flags & DLM_LKF_EXPEDITE && flags & DLM_LKF_QUECVT)
2186 goto out;
2187
2188 if (flags & DLM_LKF_EXPEDITE && flags & DLM_LKF_NOQUEUE)
2189 goto out;
2190
2191 if (flags & DLM_LKF_EXPEDITE && mode != DLM_LOCK_NL)
2192 goto out;
2193
2194 if (!ast || !lksb)
2195 goto out;
2196
2197 if (flags & DLM_LKF_VALBLK && !lksb->sb_lvbptr)
2198 goto out;
2199
David Teiglande7fd4172006-01-18 09:30:29 +00002200 if (flags & DLM_LKF_CONVERT && !lksb->sb_lkid)
2201 goto out;
2202
2203 /* these args will be copied to the lkb in validate_lock_args,
2204 it cannot be done now because when converting locks, fields in
2205 an active lkb cannot be modified before locking the rsb */
2206
2207 args->flags = flags;
David Teiglande5dae542008-02-06 00:35:45 -06002208 args->astfn = ast;
2209 args->astparam = astparam;
2210 args->bastfn = bast;
David Teiglandd7db9232007-05-18 09:00:32 -05002211 args->timeout = timeout_cs;
David Teiglande7fd4172006-01-18 09:30:29 +00002212 args->mode = mode;
2213 args->lksb = lksb;
David Teiglande7fd4172006-01-18 09:30:29 +00002214 rv = 0;
2215 out:
2216 return rv;
2217}
2218
2219static int set_unlock_args(uint32_t flags, void *astarg, struct dlm_args *args)
2220{
2221 if (flags & ~(DLM_LKF_CANCEL | DLM_LKF_VALBLK | DLM_LKF_IVVALBLK |
2222 DLM_LKF_FORCEUNLOCK))
2223 return -EINVAL;
2224
David Teiglandef0c2bb2007-03-28 09:56:46 -05002225 if (flags & DLM_LKF_CANCEL && flags & DLM_LKF_FORCEUNLOCK)
2226 return -EINVAL;
2227
David Teiglande7fd4172006-01-18 09:30:29 +00002228 args->flags = flags;
David Teiglande5dae542008-02-06 00:35:45 -06002229 args->astparam = astarg;
David Teiglande7fd4172006-01-18 09:30:29 +00002230 return 0;
2231}
2232
2233static int validate_lock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
2234 struct dlm_args *args)
2235{
2236 int rv = -EINVAL;
2237
2238 if (args->flags & DLM_LKF_CONVERT) {
2239 if (lkb->lkb_flags & DLM_IFL_MSTCPY)
2240 goto out;
2241
2242 if (args->flags & DLM_LKF_QUECVT &&
2243 !__quecvt_compat_matrix[lkb->lkb_grmode+1][args->mode+1])
2244 goto out;
2245
2246 rv = -EBUSY;
2247 if (lkb->lkb_status != DLM_LKSTS_GRANTED)
2248 goto out;
2249
2250 if (lkb->lkb_wait_type)
2251 goto out;
David Teiglandef0c2bb2007-03-28 09:56:46 -05002252
2253 if (is_overlap(lkb))
2254 goto out;
David Teiglande7fd4172006-01-18 09:30:29 +00002255 }
2256
2257 lkb->lkb_exflags = args->flags;
2258 lkb->lkb_sbflags = 0;
David Teiglande5dae542008-02-06 00:35:45 -06002259 lkb->lkb_astfn = args->astfn;
David Teiglande7fd4172006-01-18 09:30:29 +00002260 lkb->lkb_astparam = args->astparam;
David Teiglande5dae542008-02-06 00:35:45 -06002261 lkb->lkb_bastfn = args->bastfn;
David Teiglande7fd4172006-01-18 09:30:29 +00002262 lkb->lkb_rqmode = args->mode;
2263 lkb->lkb_lksb = args->lksb;
2264 lkb->lkb_lvbptr = args->lksb->sb_lvbptr;
2265 lkb->lkb_ownpid = (int) current->pid;
David Teiglandd7db9232007-05-18 09:00:32 -05002266 lkb->lkb_timeout_cs = args->timeout;
David Teiglande7fd4172006-01-18 09:30:29 +00002267 rv = 0;
2268 out:
David Teigland43279e52009-01-28 14:37:54 -06002269 if (rv)
2270 log_debug(ls, "validate_lock_args %d %x %x %x %d %d %s",
2271 rv, lkb->lkb_id, lkb->lkb_flags, args->flags,
2272 lkb->lkb_status, lkb->lkb_wait_type,
2273 lkb->lkb_resource->res_name);
David Teiglande7fd4172006-01-18 09:30:29 +00002274 return rv;
2275}
2276
David Teiglandef0c2bb2007-03-28 09:56:46 -05002277/* when dlm_unlock() sees -EBUSY with CANCEL/FORCEUNLOCK it returns 0
2278 for success */
2279
2280/* note: it's valid for lkb_nodeid/res_nodeid to be -1 when we get here
2281 because there may be a lookup in progress and it's valid to do
2282 cancel/unlockf on it */
2283
David Teiglande7fd4172006-01-18 09:30:29 +00002284static int validate_unlock_args(struct dlm_lkb *lkb, struct dlm_args *args)
2285{
David Teiglandef0c2bb2007-03-28 09:56:46 -05002286 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
David Teiglande7fd4172006-01-18 09:30:29 +00002287 int rv = -EINVAL;
2288
David Teiglandef0c2bb2007-03-28 09:56:46 -05002289 if (lkb->lkb_flags & DLM_IFL_MSTCPY) {
2290 log_error(ls, "unlock on MSTCPY %x", lkb->lkb_id);
2291 dlm_print_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00002292 goto out;
David Teiglandef0c2bb2007-03-28 09:56:46 -05002293 }
David Teiglande7fd4172006-01-18 09:30:29 +00002294
David Teiglandef0c2bb2007-03-28 09:56:46 -05002295 /* an lkb may still exist even though the lock is EOL'ed due to a
2296 cancel, unlock or failed noqueue request; an app can't use these
2297 locks; return same error as if the lkid had not been found at all */
2298
2299 if (lkb->lkb_flags & DLM_IFL_ENDOFLIFE) {
2300 log_debug(ls, "unlock on ENDOFLIFE %x", lkb->lkb_id);
2301 rv = -ENOENT;
2302 goto out;
2303 }
2304
2305 /* an lkb may be waiting for an rsb lookup to complete where the
2306 lookup was initiated by another lock */
2307
David Teigland42dc1602008-01-09 10:30:45 -06002308 if (!list_empty(&lkb->lkb_rsb_lookup)) {
2309 if (args->flags & (DLM_LKF_CANCEL | DLM_LKF_FORCEUNLOCK)) {
David Teiglandef0c2bb2007-03-28 09:56:46 -05002310 log_debug(ls, "unlock on rsb_lookup %x", lkb->lkb_id);
2311 list_del_init(&lkb->lkb_rsb_lookup);
2312 queue_cast(lkb->lkb_resource, lkb,
2313 args->flags & DLM_LKF_CANCEL ?
2314 -DLM_ECANCEL : -DLM_EUNLOCK);
2315 unhold_lkb(lkb); /* undoes create_lkb() */
David Teiglandef0c2bb2007-03-28 09:56:46 -05002316 }
David Teigland42dc1602008-01-09 10:30:45 -06002317 /* caller changes -EBUSY to 0 for CANCEL and FORCEUNLOCK */
2318 rv = -EBUSY;
2319 goto out;
David Teiglandef0c2bb2007-03-28 09:56:46 -05002320 }
2321
2322 /* cancel not allowed with another cancel/unlock in progress */
2323
2324 if (args->flags & DLM_LKF_CANCEL) {
2325 if (lkb->lkb_exflags & DLM_LKF_CANCEL)
2326 goto out;
2327
2328 if (is_overlap(lkb))
2329 goto out;
2330
David Teigland3ae1acf2007-05-18 08:59:31 -05002331 /* don't let scand try to do a cancel */
2332 del_timeout(lkb);
2333
David Teiglandef0c2bb2007-03-28 09:56:46 -05002334 if (lkb->lkb_flags & DLM_IFL_RESEND) {
2335 lkb->lkb_flags |= DLM_IFL_OVERLAP_CANCEL;
2336 rv = -EBUSY;
2337 goto out;
2338 }
2339
David Teiglanda536e382009-02-27 15:23:28 -06002340 /* there's nothing to cancel */
2341 if (lkb->lkb_status == DLM_LKSTS_GRANTED &&
2342 !lkb->lkb_wait_type) {
2343 rv = -EBUSY;
2344 goto out;
2345 }
2346
David Teiglandef0c2bb2007-03-28 09:56:46 -05002347 switch (lkb->lkb_wait_type) {
2348 case DLM_MSG_LOOKUP:
2349 case DLM_MSG_REQUEST:
2350 lkb->lkb_flags |= DLM_IFL_OVERLAP_CANCEL;
2351 rv = -EBUSY;
2352 goto out;
2353 case DLM_MSG_UNLOCK:
2354 case DLM_MSG_CANCEL:
2355 goto out;
2356 }
2357 /* add_to_waiters() will set OVERLAP_CANCEL */
David Teiglande7fd4172006-01-18 09:30:29 +00002358 goto out_ok;
David Teiglandef0c2bb2007-03-28 09:56:46 -05002359 }
David Teiglande7fd4172006-01-18 09:30:29 +00002360
David Teiglandef0c2bb2007-03-28 09:56:46 -05002361 /* do we need to allow a force-unlock if there's a normal unlock
2362 already in progress? in what conditions could the normal unlock
2363 fail such that we'd want to send a force-unlock to be sure? */
David Teiglande7fd4172006-01-18 09:30:29 +00002364
David Teiglandef0c2bb2007-03-28 09:56:46 -05002365 if (args->flags & DLM_LKF_FORCEUNLOCK) {
2366 if (lkb->lkb_exflags & DLM_LKF_FORCEUNLOCK)
2367 goto out;
David Teiglande7fd4172006-01-18 09:30:29 +00002368
David Teiglandef0c2bb2007-03-28 09:56:46 -05002369 if (is_overlap_unlock(lkb))
2370 goto out;
2371
David Teigland3ae1acf2007-05-18 08:59:31 -05002372 /* don't let scand try to do a cancel */
2373 del_timeout(lkb);
2374
David Teiglandef0c2bb2007-03-28 09:56:46 -05002375 if (lkb->lkb_flags & DLM_IFL_RESEND) {
2376 lkb->lkb_flags |= DLM_IFL_OVERLAP_UNLOCK;
2377 rv = -EBUSY;
2378 goto out;
2379 }
2380
2381 switch (lkb->lkb_wait_type) {
2382 case DLM_MSG_LOOKUP:
2383 case DLM_MSG_REQUEST:
2384 lkb->lkb_flags |= DLM_IFL_OVERLAP_UNLOCK;
2385 rv = -EBUSY;
2386 goto out;
2387 case DLM_MSG_UNLOCK:
2388 goto out;
2389 }
2390 /* add_to_waiters() will set OVERLAP_UNLOCK */
2391 goto out_ok;
2392 }
2393
2394 /* normal unlock not allowed if there's any op in progress */
David Teiglande7fd4172006-01-18 09:30:29 +00002395 rv = -EBUSY;
David Teiglandef0c2bb2007-03-28 09:56:46 -05002396 if (lkb->lkb_wait_type || lkb->lkb_wait_count)
David Teiglande7fd4172006-01-18 09:30:29 +00002397 goto out;
2398
2399 out_ok:
David Teiglandef0c2bb2007-03-28 09:56:46 -05002400 /* an overlapping op shouldn't blow away exflags from other op */
2401 lkb->lkb_exflags |= args->flags;
David Teiglande7fd4172006-01-18 09:30:29 +00002402 lkb->lkb_sbflags = 0;
2403 lkb->lkb_astparam = args->astparam;
David Teiglande7fd4172006-01-18 09:30:29 +00002404 rv = 0;
2405 out:
David Teiglandef0c2bb2007-03-28 09:56:46 -05002406 if (rv)
2407 log_debug(ls, "validate_unlock_args %d %x %x %x %x %d %s", rv,
2408 lkb->lkb_id, lkb->lkb_flags, lkb->lkb_exflags,
2409 args->flags, lkb->lkb_wait_type,
2410 lkb->lkb_resource->res_name);
David Teiglande7fd4172006-01-18 09:30:29 +00002411 return rv;
2412}
2413
2414/*
2415 * Four stage 4 varieties:
2416 * do_request(), do_convert(), do_unlock(), do_cancel()
2417 * These are called on the master node for the given lock and
2418 * from the central locking logic.
2419 */
2420
2421static int do_request(struct dlm_rsb *r, struct dlm_lkb *lkb)
2422{
2423 int error = 0;
2424
David Teiglandc85d65e2007-05-18 09:01:26 -05002425 if (can_be_granted(r, lkb, 1, NULL)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002426 grant_lock(r, lkb);
2427 queue_cast(r, lkb, 0);
2428 goto out;
2429 }
2430
2431 if (can_be_queued(lkb)) {
2432 error = -EINPROGRESS;
2433 add_lkb(r, lkb, DLM_LKSTS_WAITING);
David Teigland3ae1acf2007-05-18 08:59:31 -05002434 add_timeout(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00002435 goto out;
2436 }
2437
2438 error = -EAGAIN;
David Teiglande7fd4172006-01-18 09:30:29 +00002439 queue_cast(r, lkb, -EAGAIN);
David Teiglande7fd4172006-01-18 09:30:29 +00002440 out:
2441 return error;
2442}
2443
David Teiglandcf6620a2010-02-24 11:59:23 -06002444static void do_request_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
2445 int error)
2446{
2447 switch (error) {
2448 case -EAGAIN:
2449 if (force_blocking_asts(lkb))
2450 send_blocking_asts_all(r, lkb);
2451 break;
2452 case -EINPROGRESS:
2453 send_blocking_asts(r, lkb);
2454 break;
2455 }
2456}
2457
David Teiglande7fd4172006-01-18 09:30:29 +00002458static int do_convert(struct dlm_rsb *r, struct dlm_lkb *lkb)
2459{
2460 int error = 0;
David Teiglandc85d65e2007-05-18 09:01:26 -05002461 int deadlk = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00002462
2463 /* changing an existing lock may allow others to be granted */
2464
David Teiglandc85d65e2007-05-18 09:01:26 -05002465 if (can_be_granted(r, lkb, 1, &deadlk)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002466 grant_lock(r, lkb);
2467 queue_cast(r, lkb, 0);
David Teiglande7fd4172006-01-18 09:30:29 +00002468 goto out;
2469 }
2470
David Teiglandc85d65e2007-05-18 09:01:26 -05002471 /* can_be_granted() detected that this lock would block in a conversion
2472 deadlock, so we leave it on the granted queue and return EDEADLK in
2473 the ast for the convert. */
2474
2475 if (deadlk) {
2476 /* it's left on the granted queue */
David Teiglandc85d65e2007-05-18 09:01:26 -05002477 revert_lock(r, lkb);
2478 queue_cast(r, lkb, -EDEADLK);
2479 error = -EDEADLK;
2480 goto out;
2481 }
2482
David Teigland7d3c1fe2007-04-19 10:30:41 -05002483 /* is_demoted() means the can_be_granted() above set the grmode
2484 to NL, and left us on the granted queue. This auto-demotion
2485 (due to CONVDEADLK) might mean other locks, and/or this lock, are
2486 now grantable. We have to try to grant other converting locks
2487 before we try again to grant this one. */
2488
2489 if (is_demoted(lkb)) {
David Teigland36509252007-08-07 09:44:48 -05002490 grant_pending_convert(r, DLM_LOCK_IV, NULL);
David Teigland7d3c1fe2007-04-19 10:30:41 -05002491 if (_can_be_granted(r, lkb, 1)) {
2492 grant_lock(r, lkb);
2493 queue_cast(r, lkb, 0);
David Teigland7d3c1fe2007-04-19 10:30:41 -05002494 goto out;
2495 }
2496 /* else fall through and move to convert queue */
2497 }
2498
2499 if (can_be_queued(lkb)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002500 error = -EINPROGRESS;
2501 del_lkb(r, lkb);
2502 add_lkb(r, lkb, DLM_LKSTS_CONVERT);
David Teigland3ae1acf2007-05-18 08:59:31 -05002503 add_timeout(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00002504 goto out;
2505 }
2506
2507 error = -EAGAIN;
David Teiglande7fd4172006-01-18 09:30:29 +00002508 queue_cast(r, lkb, -EAGAIN);
David Teiglande7fd4172006-01-18 09:30:29 +00002509 out:
2510 return error;
2511}
2512
David Teiglandcf6620a2010-02-24 11:59:23 -06002513static void do_convert_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
2514 int error)
2515{
2516 switch (error) {
2517 case 0:
2518 grant_pending_locks(r);
2519 /* grant_pending_locks also sends basts */
2520 break;
2521 case -EAGAIN:
2522 if (force_blocking_asts(lkb))
2523 send_blocking_asts_all(r, lkb);
2524 break;
2525 case -EINPROGRESS:
2526 send_blocking_asts(r, lkb);
2527 break;
2528 }
2529}
2530
David Teiglande7fd4172006-01-18 09:30:29 +00002531static int do_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2532{
2533 remove_lock(r, lkb);
2534 queue_cast(r, lkb, -DLM_EUNLOCK);
David Teiglande7fd4172006-01-18 09:30:29 +00002535 return -DLM_EUNLOCK;
2536}
2537
David Teiglandcf6620a2010-02-24 11:59:23 -06002538static void do_unlock_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
2539 int error)
2540{
2541 grant_pending_locks(r);
2542}
2543
David Teiglandef0c2bb2007-03-28 09:56:46 -05002544/* returns: 0 did nothing, -DLM_ECANCEL canceled lock */
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04002545
David Teiglande7fd4172006-01-18 09:30:29 +00002546static int do_cancel(struct dlm_rsb *r, struct dlm_lkb *lkb)
2547{
David Teiglandef0c2bb2007-03-28 09:56:46 -05002548 int error;
2549
2550 error = revert_lock(r, lkb);
2551 if (error) {
2552 queue_cast(r, lkb, -DLM_ECANCEL);
David Teiglandef0c2bb2007-03-28 09:56:46 -05002553 return -DLM_ECANCEL;
2554 }
2555 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00002556}
2557
David Teiglandcf6620a2010-02-24 11:59:23 -06002558static void do_cancel_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
2559 int error)
2560{
2561 if (error)
2562 grant_pending_locks(r);
2563}
2564
David Teiglande7fd4172006-01-18 09:30:29 +00002565/*
2566 * Four stage 3 varieties:
2567 * _request_lock(), _convert_lock(), _unlock_lock(), _cancel_lock()
2568 */
2569
2570/* add a new lkb to a possibly new rsb, called by requesting process */
2571
2572static int _request_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2573{
2574 int error;
2575
2576 /* set_master: sets lkb nodeid from r */
2577
2578 error = set_master(r, lkb);
2579 if (error < 0)
2580 goto out;
2581 if (error) {
2582 error = 0;
2583 goto out;
2584 }
2585
David Teiglandcf6620a2010-02-24 11:59:23 -06002586 if (is_remote(r)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002587 /* receive_request() calls do_request() on remote node */
2588 error = send_request(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002589 } else {
David Teiglande7fd4172006-01-18 09:30:29 +00002590 error = do_request(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002591 /* for remote locks the request_reply is sent
2592 between do_request and do_request_effects */
2593 do_request_effects(r, lkb, error);
2594 }
David Teiglande7fd4172006-01-18 09:30:29 +00002595 out:
2596 return error;
2597}
2598
David Teigland3bcd3682006-02-23 09:56:38 +00002599/* change some property of an existing lkb, e.g. mode */
David Teiglande7fd4172006-01-18 09:30:29 +00002600
2601static int _convert_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2602{
2603 int error;
2604
David Teiglandcf6620a2010-02-24 11:59:23 -06002605 if (is_remote(r)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002606 /* receive_convert() calls do_convert() on remote node */
2607 error = send_convert(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002608 } else {
David Teiglande7fd4172006-01-18 09:30:29 +00002609 error = do_convert(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002610 /* for remote locks the convert_reply is sent
2611 between do_convert and do_convert_effects */
2612 do_convert_effects(r, lkb, error);
2613 }
David Teiglande7fd4172006-01-18 09:30:29 +00002614
2615 return error;
2616}
2617
2618/* remove an existing lkb from the granted queue */
2619
2620static int _unlock_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2621{
2622 int error;
2623
David Teiglandcf6620a2010-02-24 11:59:23 -06002624 if (is_remote(r)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002625 /* receive_unlock() calls do_unlock() on remote node */
2626 error = send_unlock(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002627 } else {
David Teiglande7fd4172006-01-18 09:30:29 +00002628 error = do_unlock(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002629 /* for remote locks the unlock_reply is sent
2630 between do_unlock and do_unlock_effects */
2631 do_unlock_effects(r, lkb, error);
2632 }
David Teiglande7fd4172006-01-18 09:30:29 +00002633
2634 return error;
2635}
2636
2637/* remove an existing lkb from the convert or wait queue */
2638
2639static int _cancel_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2640{
2641 int error;
2642
David Teiglandcf6620a2010-02-24 11:59:23 -06002643 if (is_remote(r)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002644 /* receive_cancel() calls do_cancel() on remote node */
2645 error = send_cancel(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002646 } else {
David Teiglande7fd4172006-01-18 09:30:29 +00002647 error = do_cancel(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002648 /* for remote locks the cancel_reply is sent
2649 between do_cancel and do_cancel_effects */
2650 do_cancel_effects(r, lkb, error);
2651 }
David Teiglande7fd4172006-01-18 09:30:29 +00002652
2653 return error;
2654}
2655
2656/*
2657 * Four stage 2 varieties:
2658 * request_lock(), convert_lock(), unlock_lock(), cancel_lock()
2659 */
2660
2661static int request_lock(struct dlm_ls *ls, struct dlm_lkb *lkb, char *name,
2662 int len, struct dlm_args *args)
2663{
2664 struct dlm_rsb *r;
2665 int error;
2666
2667 error = validate_lock_args(ls, lkb, args);
2668 if (error)
2669 goto out;
2670
2671 error = find_rsb(ls, name, len, R_CREATE, &r);
2672 if (error)
2673 goto out;
2674
2675 lock_rsb(r);
2676
2677 attach_lkb(r, lkb);
2678 lkb->lkb_lksb->sb_lkid = lkb->lkb_id;
2679
2680 error = _request_lock(r, lkb);
2681
2682 unlock_rsb(r);
2683 put_rsb(r);
2684
2685 out:
2686 return error;
2687}
2688
2689static int convert_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
2690 struct dlm_args *args)
2691{
2692 struct dlm_rsb *r;
2693 int error;
2694
2695 r = lkb->lkb_resource;
2696
2697 hold_rsb(r);
2698 lock_rsb(r);
2699
2700 error = validate_lock_args(ls, lkb, args);
2701 if (error)
2702 goto out;
2703
2704 error = _convert_lock(r, lkb);
2705 out:
2706 unlock_rsb(r);
2707 put_rsb(r);
2708 return error;
2709}
2710
2711static int unlock_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
2712 struct dlm_args *args)
2713{
2714 struct dlm_rsb *r;
2715 int error;
2716
2717 r = lkb->lkb_resource;
2718
2719 hold_rsb(r);
2720 lock_rsb(r);
2721
2722 error = validate_unlock_args(lkb, args);
2723 if (error)
2724 goto out;
2725
2726 error = _unlock_lock(r, lkb);
2727 out:
2728 unlock_rsb(r);
2729 put_rsb(r);
2730 return error;
2731}
2732
2733static int cancel_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
2734 struct dlm_args *args)
2735{
2736 struct dlm_rsb *r;
2737 int error;
2738
2739 r = lkb->lkb_resource;
2740
2741 hold_rsb(r);
2742 lock_rsb(r);
2743
2744 error = validate_unlock_args(lkb, args);
2745 if (error)
2746 goto out;
2747
2748 error = _cancel_lock(r, lkb);
2749 out:
2750 unlock_rsb(r);
2751 put_rsb(r);
2752 return error;
2753}
2754
2755/*
2756 * Two stage 1 varieties: dlm_lock() and dlm_unlock()
2757 */
2758
2759int dlm_lock(dlm_lockspace_t *lockspace,
2760 int mode,
2761 struct dlm_lksb *lksb,
2762 uint32_t flags,
2763 void *name,
2764 unsigned int namelen,
2765 uint32_t parent_lkid,
2766 void (*ast) (void *astarg),
2767 void *astarg,
David Teigland3bcd3682006-02-23 09:56:38 +00002768 void (*bast) (void *astarg, int mode))
David Teiglande7fd4172006-01-18 09:30:29 +00002769{
2770 struct dlm_ls *ls;
2771 struct dlm_lkb *lkb;
2772 struct dlm_args args;
2773 int error, convert = flags & DLM_LKF_CONVERT;
2774
2775 ls = dlm_find_lockspace_local(lockspace);
2776 if (!ls)
2777 return -EINVAL;
2778
David Teigland85e86ed2007-05-18 08:58:15 -05002779 dlm_lock_recovery(ls);
David Teiglande7fd4172006-01-18 09:30:29 +00002780
2781 if (convert)
2782 error = find_lkb(ls, lksb->sb_lkid, &lkb);
2783 else
2784 error = create_lkb(ls, &lkb);
2785
2786 if (error)
2787 goto out;
2788
David Teiglandd7db9232007-05-18 09:00:32 -05002789 error = set_lock_args(mode, lksb, flags, namelen, 0, ast,
David Teigland3bcd3682006-02-23 09:56:38 +00002790 astarg, bast, &args);
David Teiglande7fd4172006-01-18 09:30:29 +00002791 if (error)
2792 goto out_put;
2793
2794 if (convert)
2795 error = convert_lock(ls, lkb, &args);
2796 else
2797 error = request_lock(ls, lkb, name, namelen, &args);
2798
2799 if (error == -EINPROGRESS)
2800 error = 0;
2801 out_put:
2802 if (convert || error)
David Teiglandb3f58d82006-02-28 11:16:37 -05002803 __put_lkb(ls, lkb);
David Teiglandc85d65e2007-05-18 09:01:26 -05002804 if (error == -EAGAIN || error == -EDEADLK)
David Teiglande7fd4172006-01-18 09:30:29 +00002805 error = 0;
2806 out:
David Teigland85e86ed2007-05-18 08:58:15 -05002807 dlm_unlock_recovery(ls);
David Teiglande7fd4172006-01-18 09:30:29 +00002808 dlm_put_lockspace(ls);
2809 return error;
2810}
2811
2812int dlm_unlock(dlm_lockspace_t *lockspace,
2813 uint32_t lkid,
2814 uint32_t flags,
2815 struct dlm_lksb *lksb,
2816 void *astarg)
2817{
2818 struct dlm_ls *ls;
2819 struct dlm_lkb *lkb;
2820 struct dlm_args args;
2821 int error;
2822
2823 ls = dlm_find_lockspace_local(lockspace);
2824 if (!ls)
2825 return -EINVAL;
2826
David Teigland85e86ed2007-05-18 08:58:15 -05002827 dlm_lock_recovery(ls);
David Teiglande7fd4172006-01-18 09:30:29 +00002828
2829 error = find_lkb(ls, lkid, &lkb);
2830 if (error)
2831 goto out;
2832
2833 error = set_unlock_args(flags, astarg, &args);
2834 if (error)
2835 goto out_put;
2836
2837 if (flags & DLM_LKF_CANCEL)
2838 error = cancel_lock(ls, lkb, &args);
2839 else
2840 error = unlock_lock(ls, lkb, &args);
2841
2842 if (error == -DLM_EUNLOCK || error == -DLM_ECANCEL)
2843 error = 0;
David Teiglandef0c2bb2007-03-28 09:56:46 -05002844 if (error == -EBUSY && (flags & (DLM_LKF_CANCEL | DLM_LKF_FORCEUNLOCK)))
2845 error = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00002846 out_put:
David Teiglandb3f58d82006-02-28 11:16:37 -05002847 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00002848 out:
David Teigland85e86ed2007-05-18 08:58:15 -05002849 dlm_unlock_recovery(ls);
David Teiglande7fd4172006-01-18 09:30:29 +00002850 dlm_put_lockspace(ls);
2851 return error;
2852}
2853
2854/*
2855 * send/receive routines for remote operations and replies
2856 *
2857 * send_args
2858 * send_common
2859 * send_request receive_request
2860 * send_convert receive_convert
2861 * send_unlock receive_unlock
2862 * send_cancel receive_cancel
2863 * send_grant receive_grant
2864 * send_bast receive_bast
2865 * send_lookup receive_lookup
2866 * send_remove receive_remove
2867 *
2868 * send_common_reply
2869 * receive_request_reply send_request_reply
2870 * receive_convert_reply send_convert_reply
2871 * receive_unlock_reply send_unlock_reply
2872 * receive_cancel_reply send_cancel_reply
2873 * receive_lookup_reply send_lookup_reply
2874 */
2875
David Teigland7e4dac32007-04-02 09:06:41 -05002876static int _create_message(struct dlm_ls *ls, int mb_len,
2877 int to_nodeid, int mstype,
2878 struct dlm_message **ms_ret,
2879 struct dlm_mhandle **mh_ret)
2880{
2881 struct dlm_message *ms;
2882 struct dlm_mhandle *mh;
2883 char *mb;
2884
2885 /* get_buffer gives us a message handle (mh) that we need to
2886 pass into lowcomms_commit and a message buffer (mb) that we
2887 write our data into */
2888
David Teigland573c24c2009-11-30 16:34:43 -06002889 mh = dlm_lowcomms_get_buffer(to_nodeid, mb_len, GFP_NOFS, &mb);
David Teigland7e4dac32007-04-02 09:06:41 -05002890 if (!mh)
2891 return -ENOBUFS;
2892
2893 memset(mb, 0, mb_len);
2894
2895 ms = (struct dlm_message *) mb;
2896
2897 ms->m_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
2898 ms->m_header.h_lockspace = ls->ls_global_id;
2899 ms->m_header.h_nodeid = dlm_our_nodeid();
2900 ms->m_header.h_length = mb_len;
2901 ms->m_header.h_cmd = DLM_MSG;
2902
2903 ms->m_type = mstype;
2904
2905 *mh_ret = mh;
2906 *ms_ret = ms;
2907 return 0;
2908}
2909
David Teiglande7fd4172006-01-18 09:30:29 +00002910static int create_message(struct dlm_rsb *r, struct dlm_lkb *lkb,
2911 int to_nodeid, int mstype,
2912 struct dlm_message **ms_ret,
2913 struct dlm_mhandle **mh_ret)
2914{
David Teiglande7fd4172006-01-18 09:30:29 +00002915 int mb_len = sizeof(struct dlm_message);
2916
2917 switch (mstype) {
2918 case DLM_MSG_REQUEST:
2919 case DLM_MSG_LOOKUP:
2920 case DLM_MSG_REMOVE:
2921 mb_len += r->res_length;
2922 break;
2923 case DLM_MSG_CONVERT:
2924 case DLM_MSG_UNLOCK:
2925 case DLM_MSG_REQUEST_REPLY:
2926 case DLM_MSG_CONVERT_REPLY:
2927 case DLM_MSG_GRANT:
2928 if (lkb && lkb->lkb_lvbptr)
2929 mb_len += r->res_ls->ls_lvblen;
2930 break;
2931 }
2932
David Teigland7e4dac32007-04-02 09:06:41 -05002933 return _create_message(r->res_ls, mb_len, to_nodeid, mstype,
2934 ms_ret, mh_ret);
David Teiglande7fd4172006-01-18 09:30:29 +00002935}
2936
2937/* further lowcomms enhancements or alternate implementations may make
2938 the return value from this function useful at some point */
2939
2940static int send_message(struct dlm_mhandle *mh, struct dlm_message *ms)
2941{
2942 dlm_message_out(ms);
2943 dlm_lowcomms_commit_buffer(mh);
2944 return 0;
2945}
2946
2947static void send_args(struct dlm_rsb *r, struct dlm_lkb *lkb,
2948 struct dlm_message *ms)
2949{
2950 ms->m_nodeid = lkb->lkb_nodeid;
2951 ms->m_pid = lkb->lkb_ownpid;
2952 ms->m_lkid = lkb->lkb_id;
2953 ms->m_remid = lkb->lkb_remid;
2954 ms->m_exflags = lkb->lkb_exflags;
2955 ms->m_sbflags = lkb->lkb_sbflags;
2956 ms->m_flags = lkb->lkb_flags;
2957 ms->m_lvbseq = lkb->lkb_lvbseq;
2958 ms->m_status = lkb->lkb_status;
2959 ms->m_grmode = lkb->lkb_grmode;
2960 ms->m_rqmode = lkb->lkb_rqmode;
2961 ms->m_hash = r->res_hash;
2962
2963 /* m_result and m_bastmode are set from function args,
2964 not from lkb fields */
2965
David Teiglande5dae542008-02-06 00:35:45 -06002966 if (lkb->lkb_bastfn)
David Teigland8304d6f2011-02-21 14:58:21 -06002967 ms->m_asts |= DLM_CB_BAST;
David Teiglande5dae542008-02-06 00:35:45 -06002968 if (lkb->lkb_astfn)
David Teigland8304d6f2011-02-21 14:58:21 -06002969 ms->m_asts |= DLM_CB_CAST;
David Teiglande7fd4172006-01-18 09:30:29 +00002970
David Teiglandda49f362006-12-13 10:38:45 -06002971 /* compare with switch in create_message; send_remove() doesn't
2972 use send_args() */
2973
2974 switch (ms->m_type) {
2975 case DLM_MSG_REQUEST:
2976 case DLM_MSG_LOOKUP:
David Teiglande7fd4172006-01-18 09:30:29 +00002977 memcpy(ms->m_extra, r->res_name, r->res_length);
David Teiglandda49f362006-12-13 10:38:45 -06002978 break;
2979 case DLM_MSG_CONVERT:
2980 case DLM_MSG_UNLOCK:
2981 case DLM_MSG_REQUEST_REPLY:
2982 case DLM_MSG_CONVERT_REPLY:
2983 case DLM_MSG_GRANT:
2984 if (!lkb->lkb_lvbptr)
2985 break;
David Teiglande7fd4172006-01-18 09:30:29 +00002986 memcpy(ms->m_extra, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
David Teiglandda49f362006-12-13 10:38:45 -06002987 break;
2988 }
David Teiglande7fd4172006-01-18 09:30:29 +00002989}
2990
2991static int send_common(struct dlm_rsb *r, struct dlm_lkb *lkb, int mstype)
2992{
2993 struct dlm_message *ms;
2994 struct dlm_mhandle *mh;
2995 int to_nodeid, error;
2996
David Teiglandc6ff6692011-03-28 14:17:26 -05002997 to_nodeid = r->res_nodeid;
2998
2999 error = add_to_waiters(lkb, mstype, to_nodeid);
David Teiglandef0c2bb2007-03-28 09:56:46 -05003000 if (error)
3001 return error;
David Teiglande7fd4172006-01-18 09:30:29 +00003002
David Teiglande7fd4172006-01-18 09:30:29 +00003003 error = create_message(r, lkb, to_nodeid, mstype, &ms, &mh);
3004 if (error)
3005 goto fail;
3006
3007 send_args(r, lkb, ms);
3008
3009 error = send_message(mh, ms);
3010 if (error)
3011 goto fail;
3012 return 0;
3013
3014 fail:
David Teiglandef0c2bb2007-03-28 09:56:46 -05003015 remove_from_waiters(lkb, msg_reply_type(mstype));
David Teiglande7fd4172006-01-18 09:30:29 +00003016 return error;
3017}
3018
3019static int send_request(struct dlm_rsb *r, struct dlm_lkb *lkb)
3020{
3021 return send_common(r, lkb, DLM_MSG_REQUEST);
3022}
3023
3024static int send_convert(struct dlm_rsb *r, struct dlm_lkb *lkb)
3025{
3026 int error;
3027
3028 error = send_common(r, lkb, DLM_MSG_CONVERT);
3029
3030 /* down conversions go without a reply from the master */
3031 if (!error && down_conversion(lkb)) {
David Teiglandef0c2bb2007-03-28 09:56:46 -05003032 remove_from_waiters(lkb, DLM_MSG_CONVERT_REPLY);
David Teigland2a7ce0e2011-04-04 15:19:59 -05003033 r->res_ls->ls_stub_ms.m_flags = DLM_IFL_STUB_MS;
David Teiglandef0c2bb2007-03-28 09:56:46 -05003034 r->res_ls->ls_stub_ms.m_type = DLM_MSG_CONVERT_REPLY;
David Teiglande7fd4172006-01-18 09:30:29 +00003035 r->res_ls->ls_stub_ms.m_result = 0;
3036 __receive_convert_reply(r, lkb, &r->res_ls->ls_stub_ms);
3037 }
3038
3039 return error;
3040}
3041
3042/* FIXME: if this lkb is the only lock we hold on the rsb, then set
3043 MASTER_UNCERTAIN to force the next request on the rsb to confirm
3044 that the master is still correct. */
3045
3046static int send_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
3047{
3048 return send_common(r, lkb, DLM_MSG_UNLOCK);
3049}
3050
3051static int send_cancel(struct dlm_rsb *r, struct dlm_lkb *lkb)
3052{
3053 return send_common(r, lkb, DLM_MSG_CANCEL);
3054}
3055
3056static int send_grant(struct dlm_rsb *r, struct dlm_lkb *lkb)
3057{
3058 struct dlm_message *ms;
3059 struct dlm_mhandle *mh;
3060 int to_nodeid, error;
3061
3062 to_nodeid = lkb->lkb_nodeid;
3063
3064 error = create_message(r, lkb, to_nodeid, DLM_MSG_GRANT, &ms, &mh);
3065 if (error)
3066 goto out;
3067
3068 send_args(r, lkb, ms);
3069
3070 ms->m_result = 0;
3071
3072 error = send_message(mh, ms);
3073 out:
3074 return error;
3075}
3076
3077static int send_bast(struct dlm_rsb *r, struct dlm_lkb *lkb, int mode)
3078{
3079 struct dlm_message *ms;
3080 struct dlm_mhandle *mh;
3081 int to_nodeid, error;
3082
3083 to_nodeid = lkb->lkb_nodeid;
3084
3085 error = create_message(r, NULL, to_nodeid, DLM_MSG_BAST, &ms, &mh);
3086 if (error)
3087 goto out;
3088
3089 send_args(r, lkb, ms);
3090
3091 ms->m_bastmode = mode;
3092
3093 error = send_message(mh, ms);
3094 out:
3095 return error;
3096}
3097
3098static int send_lookup(struct dlm_rsb *r, struct dlm_lkb *lkb)
3099{
3100 struct dlm_message *ms;
3101 struct dlm_mhandle *mh;
3102 int to_nodeid, error;
3103
David Teiglandc6ff6692011-03-28 14:17:26 -05003104 to_nodeid = dlm_dir_nodeid(r);
3105
3106 error = add_to_waiters(lkb, DLM_MSG_LOOKUP, to_nodeid);
David Teiglandef0c2bb2007-03-28 09:56:46 -05003107 if (error)
3108 return error;
David Teiglande7fd4172006-01-18 09:30:29 +00003109
David Teiglande7fd4172006-01-18 09:30:29 +00003110 error = create_message(r, NULL, to_nodeid, DLM_MSG_LOOKUP, &ms, &mh);
3111 if (error)
3112 goto fail;
3113
3114 send_args(r, lkb, ms);
3115
3116 error = send_message(mh, ms);
3117 if (error)
3118 goto fail;
3119 return 0;
3120
3121 fail:
David Teiglandef0c2bb2007-03-28 09:56:46 -05003122 remove_from_waiters(lkb, DLM_MSG_LOOKUP_REPLY);
David Teiglande7fd4172006-01-18 09:30:29 +00003123 return error;
3124}
3125
3126static int send_remove(struct dlm_rsb *r)
3127{
3128 struct dlm_message *ms;
3129 struct dlm_mhandle *mh;
3130 int to_nodeid, error;
3131
3132 to_nodeid = dlm_dir_nodeid(r);
3133
3134 error = create_message(r, NULL, to_nodeid, DLM_MSG_REMOVE, &ms, &mh);
3135 if (error)
3136 goto out;
3137
3138 memcpy(ms->m_extra, r->res_name, r->res_length);
3139 ms->m_hash = r->res_hash;
3140
3141 error = send_message(mh, ms);
3142 out:
3143 return error;
3144}
3145
3146static int send_common_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
3147 int mstype, int rv)
3148{
3149 struct dlm_message *ms;
3150 struct dlm_mhandle *mh;
3151 int to_nodeid, error;
3152
3153 to_nodeid = lkb->lkb_nodeid;
3154
3155 error = create_message(r, lkb, to_nodeid, mstype, &ms, &mh);
3156 if (error)
3157 goto out;
3158
3159 send_args(r, lkb, ms);
3160
3161 ms->m_result = rv;
3162
3163 error = send_message(mh, ms);
3164 out:
3165 return error;
3166}
3167
3168static int send_request_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
3169{
3170 return send_common_reply(r, lkb, DLM_MSG_REQUEST_REPLY, rv);
3171}
3172
3173static int send_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
3174{
3175 return send_common_reply(r, lkb, DLM_MSG_CONVERT_REPLY, rv);
3176}
3177
3178static int send_unlock_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
3179{
3180 return send_common_reply(r, lkb, DLM_MSG_UNLOCK_REPLY, rv);
3181}
3182
3183static int send_cancel_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
3184{
3185 return send_common_reply(r, lkb, DLM_MSG_CANCEL_REPLY, rv);
3186}
3187
3188static int send_lookup_reply(struct dlm_ls *ls, struct dlm_message *ms_in,
3189 int ret_nodeid, int rv)
3190{
3191 struct dlm_rsb *r = &ls->ls_stub_rsb;
3192 struct dlm_message *ms;
3193 struct dlm_mhandle *mh;
3194 int error, nodeid = ms_in->m_header.h_nodeid;
3195
3196 error = create_message(r, NULL, nodeid, DLM_MSG_LOOKUP_REPLY, &ms, &mh);
3197 if (error)
3198 goto out;
3199
3200 ms->m_lkid = ms_in->m_lkid;
3201 ms->m_result = rv;
3202 ms->m_nodeid = ret_nodeid;
3203
3204 error = send_message(mh, ms);
3205 out:
3206 return error;
3207}
3208
3209/* which args we save from a received message depends heavily on the type
3210 of message, unlike the send side where we can safely send everything about
3211 the lkb for any type of message */
3212
3213static void receive_flags(struct dlm_lkb *lkb, struct dlm_message *ms)
3214{
3215 lkb->lkb_exflags = ms->m_exflags;
David Teigland6f90a8b12006-11-10 14:16:27 -06003216 lkb->lkb_sbflags = ms->m_sbflags;
David Teiglande7fd4172006-01-18 09:30:29 +00003217 lkb->lkb_flags = (lkb->lkb_flags & 0xFFFF0000) |
3218 (ms->m_flags & 0x0000FFFF);
3219}
3220
3221static void receive_flags_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
3222{
David Teigland2a7ce0e2011-04-04 15:19:59 -05003223 if (ms->m_flags == DLM_IFL_STUB_MS)
3224 return;
3225
David Teiglande7fd4172006-01-18 09:30:29 +00003226 lkb->lkb_sbflags = ms->m_sbflags;
3227 lkb->lkb_flags = (lkb->lkb_flags & 0xFFFF0000) |
3228 (ms->m_flags & 0x0000FFFF);
3229}
3230
3231static int receive_extralen(struct dlm_message *ms)
3232{
3233 return (ms->m_header.h_length - sizeof(struct dlm_message));
3234}
3235
David Teiglande7fd4172006-01-18 09:30:29 +00003236static int receive_lvb(struct dlm_ls *ls, struct dlm_lkb *lkb,
3237 struct dlm_message *ms)
3238{
3239 int len;
3240
3241 if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
3242 if (!lkb->lkb_lvbptr)
David Teigland52bda2b2007-11-07 09:06:49 -06003243 lkb->lkb_lvbptr = dlm_allocate_lvb(ls);
David Teiglande7fd4172006-01-18 09:30:29 +00003244 if (!lkb->lkb_lvbptr)
3245 return -ENOMEM;
3246 len = receive_extralen(ms);
Al Viroa9cc9152008-01-26 00:02:29 -05003247 if (len > DLM_RESNAME_MAXLEN)
3248 len = DLM_RESNAME_MAXLEN;
David Teiglande7fd4172006-01-18 09:30:29 +00003249 memcpy(lkb->lkb_lvbptr, ms->m_extra, len);
3250 }
3251 return 0;
3252}
3253
David Teiglande5dae542008-02-06 00:35:45 -06003254static void fake_bastfn(void *astparam, int mode)
3255{
3256 log_print("fake_bastfn should not be called");
3257}
3258
3259static void fake_astfn(void *astparam)
3260{
3261 log_print("fake_astfn should not be called");
3262}
3263
David Teiglande7fd4172006-01-18 09:30:29 +00003264static int receive_request_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
3265 struct dlm_message *ms)
3266{
3267 lkb->lkb_nodeid = ms->m_header.h_nodeid;
3268 lkb->lkb_ownpid = ms->m_pid;
3269 lkb->lkb_remid = ms->m_lkid;
3270 lkb->lkb_grmode = DLM_LOCK_IV;
3271 lkb->lkb_rqmode = ms->m_rqmode;
David Teiglande5dae542008-02-06 00:35:45 -06003272
David Teigland8304d6f2011-02-21 14:58:21 -06003273 lkb->lkb_bastfn = (ms->m_asts & DLM_CB_BAST) ? &fake_bastfn : NULL;
3274 lkb->lkb_astfn = (ms->m_asts & DLM_CB_CAST) ? &fake_astfn : NULL;
David Teiglande7fd4172006-01-18 09:30:29 +00003275
David Teigland8d07fd52006-12-13 10:39:20 -06003276 if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
3277 /* lkb was just created so there won't be an lvb yet */
David Teigland52bda2b2007-11-07 09:06:49 -06003278 lkb->lkb_lvbptr = dlm_allocate_lvb(ls);
David Teigland8d07fd52006-12-13 10:39:20 -06003279 if (!lkb->lkb_lvbptr)
3280 return -ENOMEM;
3281 }
David Teiglande7fd4172006-01-18 09:30:29 +00003282
3283 return 0;
3284}
3285
3286static int receive_convert_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
3287 struct dlm_message *ms)
3288{
David Teiglande7fd4172006-01-18 09:30:29 +00003289 if (lkb->lkb_status != DLM_LKSTS_GRANTED)
3290 return -EBUSY;
3291
David Teiglande7fd4172006-01-18 09:30:29 +00003292 if (receive_lvb(ls, lkb, ms))
3293 return -ENOMEM;
3294
3295 lkb->lkb_rqmode = ms->m_rqmode;
3296 lkb->lkb_lvbseq = ms->m_lvbseq;
3297
3298 return 0;
3299}
3300
3301static int receive_unlock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
3302 struct dlm_message *ms)
3303{
David Teiglande7fd4172006-01-18 09:30:29 +00003304 if (receive_lvb(ls, lkb, ms))
3305 return -ENOMEM;
3306 return 0;
3307}
3308
3309/* We fill in the stub-lkb fields with the info that send_xxxx_reply()
3310 uses to send a reply and that the remote end uses to process the reply. */
3311
3312static void setup_stub_lkb(struct dlm_ls *ls, struct dlm_message *ms)
3313{
3314 struct dlm_lkb *lkb = &ls->ls_stub_lkb;
3315 lkb->lkb_nodeid = ms->m_header.h_nodeid;
3316 lkb->lkb_remid = ms->m_lkid;
3317}
3318
David Teiglandc54e04b2008-01-09 09:59:41 -06003319/* This is called after the rsb is locked so that we can safely inspect
3320 fields in the lkb. */
3321
3322static int validate_message(struct dlm_lkb *lkb, struct dlm_message *ms)
3323{
3324 int from = ms->m_header.h_nodeid;
3325 int error = 0;
3326
3327 switch (ms->m_type) {
3328 case DLM_MSG_CONVERT:
3329 case DLM_MSG_UNLOCK:
3330 case DLM_MSG_CANCEL:
3331 if (!is_master_copy(lkb) || lkb->lkb_nodeid != from)
3332 error = -EINVAL;
3333 break;
3334
3335 case DLM_MSG_CONVERT_REPLY:
3336 case DLM_MSG_UNLOCK_REPLY:
3337 case DLM_MSG_CANCEL_REPLY:
3338 case DLM_MSG_GRANT:
3339 case DLM_MSG_BAST:
3340 if (!is_process_copy(lkb) || lkb->lkb_nodeid != from)
3341 error = -EINVAL;
3342 break;
3343
3344 case DLM_MSG_REQUEST_REPLY:
3345 if (!is_process_copy(lkb))
3346 error = -EINVAL;
3347 else if (lkb->lkb_nodeid != -1 && lkb->lkb_nodeid != from)
3348 error = -EINVAL;
3349 break;
3350
3351 default:
3352 error = -EINVAL;
3353 }
3354
3355 if (error)
3356 log_error(lkb->lkb_resource->res_ls,
3357 "ignore invalid message %d from %d %x %x %x %d",
3358 ms->m_type, from, lkb->lkb_id, lkb->lkb_remid,
3359 lkb->lkb_flags, lkb->lkb_nodeid);
3360 return error;
3361}
3362
David Teiglande7fd4172006-01-18 09:30:29 +00003363static void receive_request(struct dlm_ls *ls, struct dlm_message *ms)
3364{
3365 struct dlm_lkb *lkb;
3366 struct dlm_rsb *r;
3367 int error, namelen;
3368
3369 error = create_lkb(ls, &lkb);
3370 if (error)
3371 goto fail;
3372
3373 receive_flags(lkb, ms);
3374 lkb->lkb_flags |= DLM_IFL_MSTCPY;
3375 error = receive_request_args(ls, lkb, ms);
3376 if (error) {
David Teiglandb3f58d82006-02-28 11:16:37 -05003377 __put_lkb(ls, lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003378 goto fail;
3379 }
3380
3381 namelen = receive_extralen(ms);
3382
3383 error = find_rsb(ls, ms->m_extra, namelen, R_MASTER, &r);
3384 if (error) {
David Teiglandb3f58d82006-02-28 11:16:37 -05003385 __put_lkb(ls, lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003386 goto fail;
3387 }
3388
3389 lock_rsb(r);
3390
3391 attach_lkb(r, lkb);
3392 error = do_request(r, lkb);
3393 send_request_reply(r, lkb, error);
David Teiglandcf6620a2010-02-24 11:59:23 -06003394 do_request_effects(r, lkb, error);
David Teiglande7fd4172006-01-18 09:30:29 +00003395
3396 unlock_rsb(r);
3397 put_rsb(r);
3398
3399 if (error == -EINPROGRESS)
3400 error = 0;
3401 if (error)
David Teiglandb3f58d82006-02-28 11:16:37 -05003402 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003403 return;
3404
3405 fail:
3406 setup_stub_lkb(ls, ms);
3407 send_request_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
3408}
3409
3410static void receive_convert(struct dlm_ls *ls, struct dlm_message *ms)
3411{
3412 struct dlm_lkb *lkb;
3413 struct dlm_rsb *r;
David Teigland90135922006-01-20 08:47:07 +00003414 int error, reply = 1;
David Teiglande7fd4172006-01-18 09:30:29 +00003415
3416 error = find_lkb(ls, ms->m_remid, &lkb);
3417 if (error)
3418 goto fail;
3419
3420 r = lkb->lkb_resource;
3421
3422 hold_rsb(r);
3423 lock_rsb(r);
3424
David Teiglandc54e04b2008-01-09 09:59:41 -06003425 error = validate_message(lkb, ms);
3426 if (error)
3427 goto out;
3428
David Teiglande7fd4172006-01-18 09:30:29 +00003429 receive_flags(lkb, ms);
David Teiglandcf6620a2010-02-24 11:59:23 -06003430
David Teiglande7fd4172006-01-18 09:30:29 +00003431 error = receive_convert_args(ls, lkb, ms);
David Teiglandcf6620a2010-02-24 11:59:23 -06003432 if (error) {
3433 send_convert_reply(r, lkb, error);
3434 goto out;
3435 }
3436
David Teiglande7fd4172006-01-18 09:30:29 +00003437 reply = !down_conversion(lkb);
3438
3439 error = do_convert(r, lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003440 if (reply)
3441 send_convert_reply(r, lkb, error);
David Teiglandcf6620a2010-02-24 11:59:23 -06003442 do_convert_effects(r, lkb, error);
David Teiglandc54e04b2008-01-09 09:59:41 -06003443 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003444 unlock_rsb(r);
3445 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05003446 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003447 return;
3448
3449 fail:
3450 setup_stub_lkb(ls, ms);
3451 send_convert_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
3452}
3453
3454static void receive_unlock(struct dlm_ls *ls, struct dlm_message *ms)
3455{
3456 struct dlm_lkb *lkb;
3457 struct dlm_rsb *r;
3458 int error;
3459
3460 error = find_lkb(ls, ms->m_remid, &lkb);
3461 if (error)
3462 goto fail;
3463
3464 r = lkb->lkb_resource;
3465
3466 hold_rsb(r);
3467 lock_rsb(r);
3468
David Teiglandc54e04b2008-01-09 09:59:41 -06003469 error = validate_message(lkb, ms);
David Teiglande7fd4172006-01-18 09:30:29 +00003470 if (error)
3471 goto out;
3472
David Teiglandc54e04b2008-01-09 09:59:41 -06003473 receive_flags(lkb, ms);
David Teiglandcf6620a2010-02-24 11:59:23 -06003474
David Teiglandc54e04b2008-01-09 09:59:41 -06003475 error = receive_unlock_args(ls, lkb, ms);
David Teiglandcf6620a2010-02-24 11:59:23 -06003476 if (error) {
3477 send_unlock_reply(r, lkb, error);
3478 goto out;
3479 }
David Teiglande7fd4172006-01-18 09:30:29 +00003480
David Teiglandc54e04b2008-01-09 09:59:41 -06003481 error = do_unlock(r, lkb);
David Teiglandc54e04b2008-01-09 09:59:41 -06003482 send_unlock_reply(r, lkb, error);
David Teiglandcf6620a2010-02-24 11:59:23 -06003483 do_unlock_effects(r, lkb, error);
David Teiglandc54e04b2008-01-09 09:59:41 -06003484 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003485 unlock_rsb(r);
3486 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05003487 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003488 return;
3489
3490 fail:
3491 setup_stub_lkb(ls, ms);
3492 send_unlock_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
3493}
3494
3495static void receive_cancel(struct dlm_ls *ls, struct dlm_message *ms)
3496{
3497 struct dlm_lkb *lkb;
3498 struct dlm_rsb *r;
3499 int error;
3500
3501 error = find_lkb(ls, ms->m_remid, &lkb);
3502 if (error)
3503 goto fail;
3504
3505 receive_flags(lkb, ms);
3506
3507 r = lkb->lkb_resource;
3508
3509 hold_rsb(r);
3510 lock_rsb(r);
3511
David Teiglandc54e04b2008-01-09 09:59:41 -06003512 error = validate_message(lkb, ms);
3513 if (error)
3514 goto out;
3515
David Teiglande7fd4172006-01-18 09:30:29 +00003516 error = do_cancel(r, lkb);
3517 send_cancel_reply(r, lkb, error);
David Teiglandcf6620a2010-02-24 11:59:23 -06003518 do_cancel_effects(r, lkb, error);
David Teiglandc54e04b2008-01-09 09:59:41 -06003519 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003520 unlock_rsb(r);
3521 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05003522 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003523 return;
3524
3525 fail:
3526 setup_stub_lkb(ls, ms);
3527 send_cancel_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
3528}
3529
3530static void receive_grant(struct dlm_ls *ls, struct dlm_message *ms)
3531{
3532 struct dlm_lkb *lkb;
3533 struct dlm_rsb *r;
3534 int error;
3535
3536 error = find_lkb(ls, ms->m_remid, &lkb);
3537 if (error) {
David Teiglandc54e04b2008-01-09 09:59:41 -06003538 log_debug(ls, "receive_grant from %d no lkb %x",
3539 ms->m_header.h_nodeid, ms->m_remid);
David Teiglande7fd4172006-01-18 09:30:29 +00003540 return;
3541 }
David Teiglande7fd4172006-01-18 09:30:29 +00003542
3543 r = lkb->lkb_resource;
3544
3545 hold_rsb(r);
3546 lock_rsb(r);
3547
David Teiglandc54e04b2008-01-09 09:59:41 -06003548 error = validate_message(lkb, ms);
3549 if (error)
3550 goto out;
3551
David Teiglande7fd4172006-01-18 09:30:29 +00003552 receive_flags_reply(lkb, ms);
David Teigland7d3c1fe2007-04-19 10:30:41 -05003553 if (is_altmode(lkb))
3554 munge_altmode(lkb, ms);
David Teiglande7fd4172006-01-18 09:30:29 +00003555 grant_lock_pc(r, lkb, ms);
3556 queue_cast(r, lkb, 0);
David Teiglandc54e04b2008-01-09 09:59:41 -06003557 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003558 unlock_rsb(r);
3559 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05003560 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003561}
3562
3563static void receive_bast(struct dlm_ls *ls, struct dlm_message *ms)
3564{
3565 struct dlm_lkb *lkb;
3566 struct dlm_rsb *r;
3567 int error;
3568
3569 error = find_lkb(ls, ms->m_remid, &lkb);
3570 if (error) {
David Teiglandc54e04b2008-01-09 09:59:41 -06003571 log_debug(ls, "receive_bast from %d no lkb %x",
3572 ms->m_header.h_nodeid, ms->m_remid);
David Teiglande7fd4172006-01-18 09:30:29 +00003573 return;
3574 }
David Teiglande7fd4172006-01-18 09:30:29 +00003575
3576 r = lkb->lkb_resource;
3577
3578 hold_rsb(r);
3579 lock_rsb(r);
3580
David Teiglandc54e04b2008-01-09 09:59:41 -06003581 error = validate_message(lkb, ms);
3582 if (error)
3583 goto out;
David Teiglande7fd4172006-01-18 09:30:29 +00003584
David Teiglandc54e04b2008-01-09 09:59:41 -06003585 queue_bast(r, lkb, ms->m_bastmode);
3586 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003587 unlock_rsb(r);
3588 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05003589 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003590}
3591
3592static void receive_lookup(struct dlm_ls *ls, struct dlm_message *ms)
3593{
3594 int len, error, ret_nodeid, dir_nodeid, from_nodeid, our_nodeid;
3595
3596 from_nodeid = ms->m_header.h_nodeid;
3597 our_nodeid = dlm_our_nodeid();
3598
3599 len = receive_extralen(ms);
3600
3601 dir_nodeid = dlm_hash2nodeid(ls, ms->m_hash);
3602 if (dir_nodeid != our_nodeid) {
3603 log_error(ls, "lookup dir_nodeid %d from %d",
3604 dir_nodeid, from_nodeid);
3605 error = -EINVAL;
3606 ret_nodeid = -1;
3607 goto out;
3608 }
3609
3610 error = dlm_dir_lookup(ls, from_nodeid, ms->m_extra, len, &ret_nodeid);
3611
3612 /* Optimization: we're master so treat lookup as a request */
3613 if (!error && ret_nodeid == our_nodeid) {
3614 receive_request(ls, ms);
3615 return;
3616 }
3617 out:
3618 send_lookup_reply(ls, ms, ret_nodeid, error);
3619}
3620
3621static void receive_remove(struct dlm_ls *ls, struct dlm_message *ms)
3622{
3623 int len, dir_nodeid, from_nodeid;
3624
3625 from_nodeid = ms->m_header.h_nodeid;
3626
3627 len = receive_extralen(ms);
3628
3629 dir_nodeid = dlm_hash2nodeid(ls, ms->m_hash);
3630 if (dir_nodeid != dlm_our_nodeid()) {
3631 log_error(ls, "remove dir entry dir_nodeid %d from %d",
3632 dir_nodeid, from_nodeid);
3633 return;
3634 }
3635
3636 dlm_dir_remove_entry(ls, from_nodeid, ms->m_extra, len);
3637}
3638
David Teigland84991372007-03-30 15:02:40 -05003639static void receive_purge(struct dlm_ls *ls, struct dlm_message *ms)
3640{
3641 do_purge(ls, ms->m_nodeid, ms->m_pid);
3642}
3643
David Teiglande7fd4172006-01-18 09:30:29 +00003644static void receive_request_reply(struct dlm_ls *ls, struct dlm_message *ms)
3645{
3646 struct dlm_lkb *lkb;
3647 struct dlm_rsb *r;
David Teiglandef0c2bb2007-03-28 09:56:46 -05003648 int error, mstype, result;
David Teiglande7fd4172006-01-18 09:30:29 +00003649
3650 error = find_lkb(ls, ms->m_remid, &lkb);
3651 if (error) {
David Teiglandc54e04b2008-01-09 09:59:41 -06003652 log_debug(ls, "receive_request_reply from %d no lkb %x",
3653 ms->m_header.h_nodeid, ms->m_remid);
David Teiglande7fd4172006-01-18 09:30:29 +00003654 return;
3655 }
David Teiglande7fd4172006-01-18 09:30:29 +00003656
David Teiglande7fd4172006-01-18 09:30:29 +00003657 r = lkb->lkb_resource;
3658 hold_rsb(r);
3659 lock_rsb(r);
3660
David Teiglandc54e04b2008-01-09 09:59:41 -06003661 error = validate_message(lkb, ms);
3662 if (error)
3663 goto out;
3664
David Teiglandef0c2bb2007-03-28 09:56:46 -05003665 mstype = lkb->lkb_wait_type;
3666 error = remove_from_waiters(lkb, DLM_MSG_REQUEST_REPLY);
3667 if (error)
3668 goto out;
3669
David Teiglande7fd4172006-01-18 09:30:29 +00003670 /* Optimization: the dir node was also the master, so it took our
3671 lookup as a request and sent request reply instead of lookup reply */
3672 if (mstype == DLM_MSG_LOOKUP) {
3673 r->res_nodeid = ms->m_header.h_nodeid;
3674 lkb->lkb_nodeid = r->res_nodeid;
3675 }
3676
David Teiglandef0c2bb2007-03-28 09:56:46 -05003677 /* this is the value returned from do_request() on the master */
3678 result = ms->m_result;
3679
3680 switch (result) {
David Teiglande7fd4172006-01-18 09:30:29 +00003681 case -EAGAIN:
David Teiglandef0c2bb2007-03-28 09:56:46 -05003682 /* request would block (be queued) on remote master */
David Teiglande7fd4172006-01-18 09:30:29 +00003683 queue_cast(r, lkb, -EAGAIN);
3684 confirm_master(r, -EAGAIN);
David Teiglandef0c2bb2007-03-28 09:56:46 -05003685 unhold_lkb(lkb); /* undoes create_lkb() */
David Teiglande7fd4172006-01-18 09:30:29 +00003686 break;
3687
3688 case -EINPROGRESS:
3689 case 0:
3690 /* request was queued or granted on remote master */
3691 receive_flags_reply(lkb, ms);
3692 lkb->lkb_remid = ms->m_lkid;
David Teigland7d3c1fe2007-04-19 10:30:41 -05003693 if (is_altmode(lkb))
3694 munge_altmode(lkb, ms);
David Teigland3ae1acf2007-05-18 08:59:31 -05003695 if (result) {
David Teiglande7fd4172006-01-18 09:30:29 +00003696 add_lkb(r, lkb, DLM_LKSTS_WAITING);
David Teigland3ae1acf2007-05-18 08:59:31 -05003697 add_timeout(lkb);
3698 } else {
David Teiglande7fd4172006-01-18 09:30:29 +00003699 grant_lock_pc(r, lkb, ms);
3700 queue_cast(r, lkb, 0);
3701 }
David Teiglandef0c2bb2007-03-28 09:56:46 -05003702 confirm_master(r, result);
David Teiglande7fd4172006-01-18 09:30:29 +00003703 break;
3704
David Teigland597d0ca2006-07-12 16:44:04 -05003705 case -EBADR:
David Teiglande7fd4172006-01-18 09:30:29 +00003706 case -ENOTBLK:
3707 /* find_rsb failed to find rsb or rsb wasn't master */
David Teiglandef0c2bb2007-03-28 09:56:46 -05003708 log_debug(ls, "receive_request_reply %x %x master diff %d %d",
3709 lkb->lkb_id, lkb->lkb_flags, r->res_nodeid, result);
David Teiglande7fd4172006-01-18 09:30:29 +00003710 r->res_nodeid = -1;
3711 lkb->lkb_nodeid = -1;
David Teiglandef0c2bb2007-03-28 09:56:46 -05003712
3713 if (is_overlap(lkb)) {
3714 /* we'll ignore error in cancel/unlock reply */
3715 queue_cast_overlap(r, lkb);
David Teiglandaec64e12008-01-08 15:37:47 -06003716 confirm_master(r, result);
David Teiglandef0c2bb2007-03-28 09:56:46 -05003717 unhold_lkb(lkb); /* undoes create_lkb() */
3718 } else
3719 _request_lock(r, lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003720 break;
3721
3722 default:
David Teiglandef0c2bb2007-03-28 09:56:46 -05003723 log_error(ls, "receive_request_reply %x error %d",
3724 lkb->lkb_id, result);
David Teiglande7fd4172006-01-18 09:30:29 +00003725 }
3726
David Teiglandef0c2bb2007-03-28 09:56:46 -05003727 if (is_overlap_unlock(lkb) && (result == 0 || result == -EINPROGRESS)) {
3728 log_debug(ls, "receive_request_reply %x result %d unlock",
3729 lkb->lkb_id, result);
3730 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
3731 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
3732 send_unlock(r, lkb);
3733 } else if (is_overlap_cancel(lkb) && (result == -EINPROGRESS)) {
3734 log_debug(ls, "receive_request_reply %x cancel", lkb->lkb_id);
3735 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
3736 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
3737 send_cancel(r, lkb);
3738 } else {
3739 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
3740 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
3741 }
3742 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003743 unlock_rsb(r);
3744 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05003745 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003746}
3747
3748static void __receive_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
3749 struct dlm_message *ms)
3750{
David Teiglande7fd4172006-01-18 09:30:29 +00003751 /* this is the value returned from do_convert() on the master */
David Teiglandef0c2bb2007-03-28 09:56:46 -05003752 switch (ms->m_result) {
David Teiglande7fd4172006-01-18 09:30:29 +00003753 case -EAGAIN:
3754 /* convert would block (be queued) on remote master */
3755 queue_cast(r, lkb, -EAGAIN);
3756 break;
3757
David Teiglandc85d65e2007-05-18 09:01:26 -05003758 case -EDEADLK:
3759 receive_flags_reply(lkb, ms);
3760 revert_lock_pc(r, lkb);
3761 queue_cast(r, lkb, -EDEADLK);
3762 break;
3763
David Teiglande7fd4172006-01-18 09:30:29 +00003764 case -EINPROGRESS:
3765 /* convert was queued on remote master */
David Teigland7d3c1fe2007-04-19 10:30:41 -05003766 receive_flags_reply(lkb, ms);
3767 if (is_demoted(lkb))
David Teigland2a7ce0e2011-04-04 15:19:59 -05003768 munge_demoted(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003769 del_lkb(r, lkb);
3770 add_lkb(r, lkb, DLM_LKSTS_CONVERT);
David Teigland3ae1acf2007-05-18 08:59:31 -05003771 add_timeout(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003772 break;
3773
3774 case 0:
3775 /* convert was granted on remote master */
3776 receive_flags_reply(lkb, ms);
David Teigland7d3c1fe2007-04-19 10:30:41 -05003777 if (is_demoted(lkb))
David Teigland2a7ce0e2011-04-04 15:19:59 -05003778 munge_demoted(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003779 grant_lock_pc(r, lkb, ms);
3780 queue_cast(r, lkb, 0);
3781 break;
3782
3783 default:
David Teiglandef0c2bb2007-03-28 09:56:46 -05003784 log_error(r->res_ls, "receive_convert_reply %x error %d",
3785 lkb->lkb_id, ms->m_result);
David Teiglande7fd4172006-01-18 09:30:29 +00003786 }
3787}
3788
3789static void _receive_convert_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
3790{
3791 struct dlm_rsb *r = lkb->lkb_resource;
David Teiglandef0c2bb2007-03-28 09:56:46 -05003792 int error;
David Teiglande7fd4172006-01-18 09:30:29 +00003793
3794 hold_rsb(r);
3795 lock_rsb(r);
3796
David Teiglandc54e04b2008-01-09 09:59:41 -06003797 error = validate_message(lkb, ms);
3798 if (error)
3799 goto out;
3800
David Teiglandef0c2bb2007-03-28 09:56:46 -05003801 /* stub reply can happen with waiters_mutex held */
3802 error = remove_from_waiters_ms(lkb, ms);
3803 if (error)
3804 goto out;
David Teiglande7fd4172006-01-18 09:30:29 +00003805
David Teiglandef0c2bb2007-03-28 09:56:46 -05003806 __receive_convert_reply(r, lkb, ms);
3807 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003808 unlock_rsb(r);
3809 put_rsb(r);
3810}
3811
3812static void receive_convert_reply(struct dlm_ls *ls, struct dlm_message *ms)
3813{
3814 struct dlm_lkb *lkb;
3815 int error;
3816
3817 error = find_lkb(ls, ms->m_remid, &lkb);
3818 if (error) {
David Teiglandc54e04b2008-01-09 09:59:41 -06003819 log_debug(ls, "receive_convert_reply from %d no lkb %x",
3820 ms->m_header.h_nodeid, ms->m_remid);
David Teiglande7fd4172006-01-18 09:30:29 +00003821 return;
3822 }
David Teiglande7fd4172006-01-18 09:30:29 +00003823
David Teiglande7fd4172006-01-18 09:30:29 +00003824 _receive_convert_reply(lkb, ms);
David Teiglandb3f58d82006-02-28 11:16:37 -05003825 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003826}
3827
3828static void _receive_unlock_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
3829{
3830 struct dlm_rsb *r = lkb->lkb_resource;
David Teiglandef0c2bb2007-03-28 09:56:46 -05003831 int error;
David Teiglande7fd4172006-01-18 09:30:29 +00003832
3833 hold_rsb(r);
3834 lock_rsb(r);
3835
David Teiglandc54e04b2008-01-09 09:59:41 -06003836 error = validate_message(lkb, ms);
3837 if (error)
3838 goto out;
3839
David Teiglandef0c2bb2007-03-28 09:56:46 -05003840 /* stub reply can happen with waiters_mutex held */
3841 error = remove_from_waiters_ms(lkb, ms);
3842 if (error)
3843 goto out;
3844
David Teiglande7fd4172006-01-18 09:30:29 +00003845 /* this is the value returned from do_unlock() on the master */
3846
David Teiglandef0c2bb2007-03-28 09:56:46 -05003847 switch (ms->m_result) {
David Teiglande7fd4172006-01-18 09:30:29 +00003848 case -DLM_EUNLOCK:
3849 receive_flags_reply(lkb, ms);
3850 remove_lock_pc(r, lkb);
3851 queue_cast(r, lkb, -DLM_EUNLOCK);
3852 break;
David Teiglandef0c2bb2007-03-28 09:56:46 -05003853 case -ENOENT:
3854 break;
David Teiglande7fd4172006-01-18 09:30:29 +00003855 default:
David Teiglandef0c2bb2007-03-28 09:56:46 -05003856 log_error(r->res_ls, "receive_unlock_reply %x error %d",
3857 lkb->lkb_id, ms->m_result);
David Teiglande7fd4172006-01-18 09:30:29 +00003858 }
David Teiglandef0c2bb2007-03-28 09:56:46 -05003859 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003860 unlock_rsb(r);
3861 put_rsb(r);
3862}
3863
3864static void receive_unlock_reply(struct dlm_ls *ls, struct dlm_message *ms)
3865{
3866 struct dlm_lkb *lkb;
3867 int error;
3868
3869 error = find_lkb(ls, ms->m_remid, &lkb);
3870 if (error) {
David Teiglandc54e04b2008-01-09 09:59:41 -06003871 log_debug(ls, "receive_unlock_reply from %d no lkb %x",
3872 ms->m_header.h_nodeid, ms->m_remid);
David Teiglande7fd4172006-01-18 09:30:29 +00003873 return;
3874 }
David Teiglande7fd4172006-01-18 09:30:29 +00003875
David Teiglande7fd4172006-01-18 09:30:29 +00003876 _receive_unlock_reply(lkb, ms);
David Teiglandb3f58d82006-02-28 11:16:37 -05003877 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003878}
3879
3880static void _receive_cancel_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
3881{
3882 struct dlm_rsb *r = lkb->lkb_resource;
David Teiglandef0c2bb2007-03-28 09:56:46 -05003883 int error;
David Teiglande7fd4172006-01-18 09:30:29 +00003884
3885 hold_rsb(r);
3886 lock_rsb(r);
3887
David Teiglandc54e04b2008-01-09 09:59:41 -06003888 error = validate_message(lkb, ms);
3889 if (error)
3890 goto out;
3891
David Teiglandef0c2bb2007-03-28 09:56:46 -05003892 /* stub reply can happen with waiters_mutex held */
3893 error = remove_from_waiters_ms(lkb, ms);
3894 if (error)
3895 goto out;
3896
David Teiglande7fd4172006-01-18 09:30:29 +00003897 /* this is the value returned from do_cancel() on the master */
3898
David Teiglandef0c2bb2007-03-28 09:56:46 -05003899 switch (ms->m_result) {
David Teiglande7fd4172006-01-18 09:30:29 +00003900 case -DLM_ECANCEL:
3901 receive_flags_reply(lkb, ms);
3902 revert_lock_pc(r, lkb);
David Teigland84d8cd62007-05-29 08:44:23 -05003903 queue_cast(r, lkb, -DLM_ECANCEL);
David Teiglandef0c2bb2007-03-28 09:56:46 -05003904 break;
3905 case 0:
David Teiglande7fd4172006-01-18 09:30:29 +00003906 break;
3907 default:
David Teiglandef0c2bb2007-03-28 09:56:46 -05003908 log_error(r->res_ls, "receive_cancel_reply %x error %d",
3909 lkb->lkb_id, ms->m_result);
David Teiglande7fd4172006-01-18 09:30:29 +00003910 }
David Teiglandef0c2bb2007-03-28 09:56:46 -05003911 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003912 unlock_rsb(r);
3913 put_rsb(r);
3914}
3915
3916static void receive_cancel_reply(struct dlm_ls *ls, struct dlm_message *ms)
3917{
3918 struct dlm_lkb *lkb;
3919 int error;
3920
3921 error = find_lkb(ls, ms->m_remid, &lkb);
3922 if (error) {
David Teiglandc54e04b2008-01-09 09:59:41 -06003923 log_debug(ls, "receive_cancel_reply from %d no lkb %x",
3924 ms->m_header.h_nodeid, ms->m_remid);
David Teiglande7fd4172006-01-18 09:30:29 +00003925 return;
3926 }
David Teiglande7fd4172006-01-18 09:30:29 +00003927
David Teiglande7fd4172006-01-18 09:30:29 +00003928 _receive_cancel_reply(lkb, ms);
David Teiglandb3f58d82006-02-28 11:16:37 -05003929 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003930}
3931
3932static void receive_lookup_reply(struct dlm_ls *ls, struct dlm_message *ms)
3933{
3934 struct dlm_lkb *lkb;
3935 struct dlm_rsb *r;
3936 int error, ret_nodeid;
3937
3938 error = find_lkb(ls, ms->m_lkid, &lkb);
3939 if (error) {
3940 log_error(ls, "receive_lookup_reply no lkb");
3941 return;
3942 }
3943
David Teiglandef0c2bb2007-03-28 09:56:46 -05003944 /* ms->m_result is the value returned by dlm_dir_lookup on dir node
David Teiglande7fd4172006-01-18 09:30:29 +00003945 FIXME: will a non-zero error ever be returned? */
David Teiglande7fd4172006-01-18 09:30:29 +00003946
3947 r = lkb->lkb_resource;
3948 hold_rsb(r);
3949 lock_rsb(r);
3950
David Teiglandef0c2bb2007-03-28 09:56:46 -05003951 error = remove_from_waiters(lkb, DLM_MSG_LOOKUP_REPLY);
3952 if (error)
3953 goto out;
3954
David Teiglande7fd4172006-01-18 09:30:29 +00003955 ret_nodeid = ms->m_nodeid;
3956 if (ret_nodeid == dlm_our_nodeid()) {
3957 r->res_nodeid = 0;
3958 ret_nodeid = 0;
3959 r->res_first_lkid = 0;
3960 } else {
3961 /* set_master() will copy res_nodeid to lkb_nodeid */
3962 r->res_nodeid = ret_nodeid;
3963 }
3964
David Teiglandef0c2bb2007-03-28 09:56:46 -05003965 if (is_overlap(lkb)) {
3966 log_debug(ls, "receive_lookup_reply %x unlock %x",
3967 lkb->lkb_id, lkb->lkb_flags);
3968 queue_cast_overlap(r, lkb);
3969 unhold_lkb(lkb); /* undoes create_lkb() */
3970 goto out_list;
3971 }
3972
David Teiglande7fd4172006-01-18 09:30:29 +00003973 _request_lock(r, lkb);
3974
David Teiglandef0c2bb2007-03-28 09:56:46 -05003975 out_list:
David Teiglande7fd4172006-01-18 09:30:29 +00003976 if (!ret_nodeid)
3977 process_lookup_list(r);
David Teiglandef0c2bb2007-03-28 09:56:46 -05003978 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003979 unlock_rsb(r);
3980 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05003981 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003982}
3983
David Teiglandc36258b2007-09-27 15:53:38 -05003984static void _receive_message(struct dlm_ls *ls, struct dlm_message *ms)
David Teiglande7fd4172006-01-18 09:30:29 +00003985{
David Teigland46b43ee2008-01-08 16:24:00 -06003986 if (!dlm_is_member(ls, ms->m_header.h_nodeid)) {
3987 log_debug(ls, "ignore non-member message %d from %d %x %x %d",
3988 ms->m_type, ms->m_header.h_nodeid, ms->m_lkid,
3989 ms->m_remid, ms->m_result);
3990 return;
3991 }
3992
David Teiglande7fd4172006-01-18 09:30:29 +00003993 switch (ms->m_type) {
3994
3995 /* messages sent to a master node */
3996
3997 case DLM_MSG_REQUEST:
3998 receive_request(ls, ms);
3999 break;
4000
4001 case DLM_MSG_CONVERT:
4002 receive_convert(ls, ms);
4003 break;
4004
4005 case DLM_MSG_UNLOCK:
4006 receive_unlock(ls, ms);
4007 break;
4008
4009 case DLM_MSG_CANCEL:
4010 receive_cancel(ls, ms);
4011 break;
4012
4013 /* messages sent from a master node (replies to above) */
4014
4015 case DLM_MSG_REQUEST_REPLY:
4016 receive_request_reply(ls, ms);
4017 break;
4018
4019 case DLM_MSG_CONVERT_REPLY:
4020 receive_convert_reply(ls, ms);
4021 break;
4022
4023 case DLM_MSG_UNLOCK_REPLY:
4024 receive_unlock_reply(ls, ms);
4025 break;
4026
4027 case DLM_MSG_CANCEL_REPLY:
4028 receive_cancel_reply(ls, ms);
4029 break;
4030
4031 /* messages sent from a master node (only two types of async msg) */
4032
4033 case DLM_MSG_GRANT:
4034 receive_grant(ls, ms);
4035 break;
4036
4037 case DLM_MSG_BAST:
4038 receive_bast(ls, ms);
4039 break;
4040
4041 /* messages sent to a dir node */
4042
4043 case DLM_MSG_LOOKUP:
4044 receive_lookup(ls, ms);
4045 break;
4046
4047 case DLM_MSG_REMOVE:
4048 receive_remove(ls, ms);
4049 break;
4050
4051 /* messages sent from a dir node (remove has no reply) */
4052
4053 case DLM_MSG_LOOKUP_REPLY:
4054 receive_lookup_reply(ls, ms);
4055 break;
4056
David Teigland84991372007-03-30 15:02:40 -05004057 /* other messages */
4058
4059 case DLM_MSG_PURGE:
4060 receive_purge(ls, ms);
4061 break;
4062
David Teiglande7fd4172006-01-18 09:30:29 +00004063 default:
4064 log_error(ls, "unknown message type %d", ms->m_type);
4065 }
David Teiglande7fd4172006-01-18 09:30:29 +00004066}
4067
David Teiglandc36258b2007-09-27 15:53:38 -05004068/* If the lockspace is in recovery mode (locking stopped), then normal
4069 messages are saved on the requestqueue for processing after recovery is
4070 done. When not in recovery mode, we wait for dlm_recoverd to drain saved
4071 messages off the requestqueue before we process new ones. This occurs right
4072 after recovery completes when we transition from saving all messages on
4073 requestqueue, to processing all the saved messages, to processing new
4074 messages as they arrive. */
David Teiglande7fd4172006-01-18 09:30:29 +00004075
David Teiglandc36258b2007-09-27 15:53:38 -05004076static void dlm_receive_message(struct dlm_ls *ls, struct dlm_message *ms,
4077 int nodeid)
4078{
4079 if (dlm_locking_stopped(ls)) {
Al Viro8b0d8e02008-01-25 00:28:28 -05004080 dlm_add_requestqueue(ls, nodeid, ms);
David Teiglandc36258b2007-09-27 15:53:38 -05004081 } else {
4082 dlm_wait_requestqueue(ls);
4083 _receive_message(ls, ms);
4084 }
4085}
4086
4087/* This is called by dlm_recoverd to process messages that were saved on
4088 the requestqueue. */
4089
4090void dlm_receive_message_saved(struct dlm_ls *ls, struct dlm_message *ms)
4091{
4092 _receive_message(ls, ms);
4093}
4094
4095/* This is called by the midcomms layer when something is received for
4096 the lockspace. It could be either a MSG (normal message sent as part of
4097 standard locking activity) or an RCOM (recovery message sent as part of
4098 lockspace recovery). */
4099
Al Viroeef7d732008-01-25 00:58:46 -05004100void dlm_receive_buffer(union dlm_packet *p, int nodeid)
David Teiglandc36258b2007-09-27 15:53:38 -05004101{
Al Viroeef7d732008-01-25 00:58:46 -05004102 struct dlm_header *hd = &p->header;
David Teiglandc36258b2007-09-27 15:53:38 -05004103 struct dlm_ls *ls;
4104 int type = 0;
4105
4106 switch (hd->h_cmd) {
4107 case DLM_MSG:
Al Viroeef7d732008-01-25 00:58:46 -05004108 dlm_message_in(&p->message);
4109 type = p->message.m_type;
David Teiglandc36258b2007-09-27 15:53:38 -05004110 break;
4111 case DLM_RCOM:
Al Viroeef7d732008-01-25 00:58:46 -05004112 dlm_rcom_in(&p->rcom);
4113 type = p->rcom.rc_type;
David Teiglandc36258b2007-09-27 15:53:38 -05004114 break;
4115 default:
4116 log_print("invalid h_cmd %d from %u", hd->h_cmd, nodeid);
4117 return;
4118 }
4119
4120 if (hd->h_nodeid != nodeid) {
4121 log_print("invalid h_nodeid %d from %d lockspace %x",
4122 hd->h_nodeid, nodeid, hd->h_lockspace);
4123 return;
4124 }
4125
4126 ls = dlm_find_lockspace_global(hd->h_lockspace);
4127 if (!ls) {
David Teigland594199e2008-01-16 11:03:41 -06004128 if (dlm_config.ci_log_debug)
4129 log_print("invalid lockspace %x from %d cmd %d type %d",
4130 hd->h_lockspace, nodeid, hd->h_cmd, type);
David Teiglandc36258b2007-09-27 15:53:38 -05004131
4132 if (hd->h_cmd == DLM_RCOM && type == DLM_RCOM_STATUS)
Al Viroeef7d732008-01-25 00:58:46 -05004133 dlm_send_ls_not_ready(nodeid, &p->rcom);
David Teiglandc36258b2007-09-27 15:53:38 -05004134 return;
4135 }
4136
4137 /* this rwsem allows dlm_ls_stop() to wait for all dlm_recv threads to
4138 be inactive (in this ls) before transitioning to recovery mode */
4139
4140 down_read(&ls->ls_recv_active);
4141 if (hd->h_cmd == DLM_MSG)
Al Viroeef7d732008-01-25 00:58:46 -05004142 dlm_receive_message(ls, &p->message, nodeid);
David Teiglandc36258b2007-09-27 15:53:38 -05004143 else
Al Viroeef7d732008-01-25 00:58:46 -05004144 dlm_receive_rcom(ls, &p->rcom, nodeid);
David Teiglandc36258b2007-09-27 15:53:38 -05004145 up_read(&ls->ls_recv_active);
4146
4147 dlm_put_lockspace(ls);
4148}
David Teiglande7fd4172006-01-18 09:30:29 +00004149
David Teigland2a7ce0e2011-04-04 15:19:59 -05004150static void recover_convert_waiter(struct dlm_ls *ls, struct dlm_lkb *lkb,
4151 struct dlm_message *ms_stub)
David Teiglande7fd4172006-01-18 09:30:29 +00004152{
4153 if (middle_conversion(lkb)) {
4154 hold_lkb(lkb);
David Teigland2a7ce0e2011-04-04 15:19:59 -05004155 memset(ms_stub, 0, sizeof(struct dlm_message));
4156 ms_stub->m_flags = DLM_IFL_STUB_MS;
4157 ms_stub->m_type = DLM_MSG_CONVERT_REPLY;
4158 ms_stub->m_result = -EINPROGRESS;
4159 ms_stub->m_header.h_nodeid = lkb->lkb_nodeid;
4160 _receive_convert_reply(lkb, ms_stub);
David Teiglande7fd4172006-01-18 09:30:29 +00004161
4162 /* Same special case as in receive_rcom_lock_args() */
4163 lkb->lkb_grmode = DLM_LOCK_IV;
4164 rsb_set_flag(lkb->lkb_resource, RSB_RECOVER_CONVERT);
4165 unhold_lkb(lkb);
4166
4167 } else if (lkb->lkb_rqmode >= lkb->lkb_grmode) {
4168 lkb->lkb_flags |= DLM_IFL_RESEND;
4169 }
4170
4171 /* lkb->lkb_rqmode < lkb->lkb_grmode shouldn't happen since down
4172 conversions are async; there's no reply from the remote master */
4173}
4174
4175/* A waiting lkb needs recovery if the master node has failed, or
4176 the master node is changing (only when no directory is used) */
4177
4178static int waiter_needs_recovery(struct dlm_ls *ls, struct dlm_lkb *lkb)
4179{
4180 if (dlm_is_removed(ls, lkb->lkb_nodeid))
4181 return 1;
4182
4183 if (!dlm_no_directory(ls))
4184 return 0;
4185
4186 if (dlm_dir_nodeid(lkb->lkb_resource) != lkb->lkb_nodeid)
4187 return 1;
4188
4189 return 0;
4190}
4191
4192/* Recovery for locks that are waiting for replies from nodes that are now
4193 gone. We can just complete unlocks and cancels by faking a reply from the
4194 dead node. Requests and up-conversions we flag to be resent after
4195 recovery. Down-conversions can just be completed with a fake reply like
4196 unlocks. Conversions between PR and CW need special attention. */
4197
4198void dlm_recover_waiters_pre(struct dlm_ls *ls)
4199{
4200 struct dlm_lkb *lkb, *safe;
David Teigland2a7ce0e2011-04-04 15:19:59 -05004201 struct dlm_message *ms_stub;
David Teigland601342c2008-01-07 16:15:05 -06004202 int wait_type, stub_unlock_result, stub_cancel_result;
David Teiglande7fd4172006-01-18 09:30:29 +00004203
David Teiglanda22ca482011-07-11 08:40:53 -05004204 ms_stub = kmalloc(sizeof(struct dlm_message), GFP_KERNEL);
David Teigland2a7ce0e2011-04-04 15:19:59 -05004205 if (!ms_stub) {
4206 log_error(ls, "dlm_recover_waiters_pre no mem");
4207 return;
4208 }
4209
David Teigland90135922006-01-20 08:47:07 +00004210 mutex_lock(&ls->ls_waiters_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +00004211
4212 list_for_each_entry_safe(lkb, safe, &ls->ls_waiters, lkb_wait_reply) {
David Teigland2a7ce0e2011-04-04 15:19:59 -05004213
4214 /* exclude debug messages about unlocks because there can be so
4215 many and they aren't very interesting */
4216
4217 if (lkb->lkb_wait_type != DLM_MSG_UNLOCK) {
4218 log_debug(ls, "recover_waiter %x nodeid %d "
4219 "msg %d to %d", lkb->lkb_id, lkb->lkb_nodeid,
4220 lkb->lkb_wait_type, lkb->lkb_wait_nodeid);
4221 }
David Teiglande7fd4172006-01-18 09:30:29 +00004222
4223 /* all outstanding lookups, regardless of destination will be
4224 resent after recovery is done */
4225
4226 if (lkb->lkb_wait_type == DLM_MSG_LOOKUP) {
4227 lkb->lkb_flags |= DLM_IFL_RESEND;
4228 continue;
4229 }
4230
4231 if (!waiter_needs_recovery(ls, lkb))
4232 continue;
4233
David Teigland601342c2008-01-07 16:15:05 -06004234 wait_type = lkb->lkb_wait_type;
4235 stub_unlock_result = -DLM_EUNLOCK;
4236 stub_cancel_result = -DLM_ECANCEL;
4237
4238 /* Main reply may have been received leaving a zero wait_type,
4239 but a reply for the overlapping op may not have been
4240 received. In that case we need to fake the appropriate
4241 reply for the overlap op. */
4242
4243 if (!wait_type) {
4244 if (is_overlap_cancel(lkb)) {
4245 wait_type = DLM_MSG_CANCEL;
4246 if (lkb->lkb_grmode == DLM_LOCK_IV)
4247 stub_cancel_result = 0;
4248 }
4249 if (is_overlap_unlock(lkb)) {
4250 wait_type = DLM_MSG_UNLOCK;
4251 if (lkb->lkb_grmode == DLM_LOCK_IV)
4252 stub_unlock_result = -ENOENT;
4253 }
4254
4255 log_debug(ls, "rwpre overlap %x %x %d %d %d",
4256 lkb->lkb_id, lkb->lkb_flags, wait_type,
4257 stub_cancel_result, stub_unlock_result);
4258 }
4259
4260 switch (wait_type) {
David Teiglande7fd4172006-01-18 09:30:29 +00004261
4262 case DLM_MSG_REQUEST:
4263 lkb->lkb_flags |= DLM_IFL_RESEND;
4264 break;
4265
4266 case DLM_MSG_CONVERT:
David Teigland2a7ce0e2011-04-04 15:19:59 -05004267 recover_convert_waiter(ls, lkb, ms_stub);
David Teiglande7fd4172006-01-18 09:30:29 +00004268 break;
4269
4270 case DLM_MSG_UNLOCK:
4271 hold_lkb(lkb);
David Teigland2a7ce0e2011-04-04 15:19:59 -05004272 memset(ms_stub, 0, sizeof(struct dlm_message));
4273 ms_stub->m_flags = DLM_IFL_STUB_MS;
4274 ms_stub->m_type = DLM_MSG_UNLOCK_REPLY;
4275 ms_stub->m_result = stub_unlock_result;
4276 ms_stub->m_header.h_nodeid = lkb->lkb_nodeid;
4277 _receive_unlock_reply(lkb, ms_stub);
David Teiglandb3f58d82006-02-28 11:16:37 -05004278 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00004279 break;
4280
4281 case DLM_MSG_CANCEL:
4282 hold_lkb(lkb);
David Teigland2a7ce0e2011-04-04 15:19:59 -05004283 memset(ms_stub, 0, sizeof(struct dlm_message));
4284 ms_stub->m_flags = DLM_IFL_STUB_MS;
4285 ms_stub->m_type = DLM_MSG_CANCEL_REPLY;
4286 ms_stub->m_result = stub_cancel_result;
4287 ms_stub->m_header.h_nodeid = lkb->lkb_nodeid;
4288 _receive_cancel_reply(lkb, ms_stub);
David Teiglandb3f58d82006-02-28 11:16:37 -05004289 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00004290 break;
4291
4292 default:
David Teigland601342c2008-01-07 16:15:05 -06004293 log_error(ls, "invalid lkb wait_type %d %d",
4294 lkb->lkb_wait_type, wait_type);
David Teiglande7fd4172006-01-18 09:30:29 +00004295 }
David Teigland81456802006-07-25 14:05:09 -05004296 schedule();
David Teiglande7fd4172006-01-18 09:30:29 +00004297 }
David Teigland90135922006-01-20 08:47:07 +00004298 mutex_unlock(&ls->ls_waiters_mutex);
David Teigland2a7ce0e2011-04-04 15:19:59 -05004299 kfree(ms_stub);
David Teiglande7fd4172006-01-18 09:30:29 +00004300}
4301
David Teiglandef0c2bb2007-03-28 09:56:46 -05004302static struct dlm_lkb *find_resend_waiter(struct dlm_ls *ls)
David Teiglande7fd4172006-01-18 09:30:29 +00004303{
4304 struct dlm_lkb *lkb;
David Teiglandef0c2bb2007-03-28 09:56:46 -05004305 int found = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00004306
David Teigland90135922006-01-20 08:47:07 +00004307 mutex_lock(&ls->ls_waiters_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +00004308 list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
4309 if (lkb->lkb_flags & DLM_IFL_RESEND) {
David Teiglandef0c2bb2007-03-28 09:56:46 -05004310 hold_lkb(lkb);
4311 found = 1;
David Teiglande7fd4172006-01-18 09:30:29 +00004312 break;
4313 }
4314 }
David Teigland90135922006-01-20 08:47:07 +00004315 mutex_unlock(&ls->ls_waiters_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +00004316
David Teiglandef0c2bb2007-03-28 09:56:46 -05004317 if (!found)
David Teiglande7fd4172006-01-18 09:30:29 +00004318 lkb = NULL;
David Teiglandef0c2bb2007-03-28 09:56:46 -05004319 return lkb;
David Teiglande7fd4172006-01-18 09:30:29 +00004320}
4321
4322/* Deal with lookups and lkb's marked RESEND from _pre. We may now be the
4323 master or dir-node for r. Processing the lkb may result in it being placed
4324 back on waiters. */
4325
David Teiglandef0c2bb2007-03-28 09:56:46 -05004326/* We do this after normal locking has been enabled and any saved messages
4327 (in requestqueue) have been processed. We should be confident that at
4328 this point we won't get or process a reply to any of these waiting
4329 operations. But, new ops may be coming in on the rsbs/locks here from
4330 userspace or remotely. */
4331
4332/* there may have been an overlap unlock/cancel prior to recovery or after
4333 recovery. if before, the lkb may still have a pos wait_count; if after, the
4334 overlap flag would just have been set and nothing new sent. we can be
4335 confident here than any replies to either the initial op or overlap ops
4336 prior to recovery have been received. */
4337
David Teiglande7fd4172006-01-18 09:30:29 +00004338int dlm_recover_waiters_post(struct dlm_ls *ls)
4339{
4340 struct dlm_lkb *lkb;
4341 struct dlm_rsb *r;
David Teiglandef0c2bb2007-03-28 09:56:46 -05004342 int error = 0, mstype, err, oc, ou;
David Teiglande7fd4172006-01-18 09:30:29 +00004343
4344 while (1) {
4345 if (dlm_locking_stopped(ls)) {
4346 log_debug(ls, "recover_waiters_post aborted");
4347 error = -EINTR;
4348 break;
4349 }
4350
David Teiglandef0c2bb2007-03-28 09:56:46 -05004351 lkb = find_resend_waiter(ls);
4352 if (!lkb)
David Teiglande7fd4172006-01-18 09:30:29 +00004353 break;
4354
4355 r = lkb->lkb_resource;
David Teiglandef0c2bb2007-03-28 09:56:46 -05004356 hold_rsb(r);
4357 lock_rsb(r);
4358
4359 mstype = lkb->lkb_wait_type;
4360 oc = is_overlap_cancel(lkb);
4361 ou = is_overlap_unlock(lkb);
4362 err = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00004363
David Teigland2a7ce0e2011-04-04 15:19:59 -05004364 log_debug(ls, "recover_waiter %x nodeid %d msg %d r_nodeid %d",
4365 lkb->lkb_id, lkb->lkb_nodeid, mstype, r->res_nodeid);
David Teiglande7fd4172006-01-18 09:30:29 +00004366
David Teiglandef0c2bb2007-03-28 09:56:46 -05004367 /* At this point we assume that we won't get a reply to any
4368 previous op or overlap op on this lock. First, do a big
4369 remove_from_waiters() for all previous ops. */
David Teiglande7fd4172006-01-18 09:30:29 +00004370
David Teiglandef0c2bb2007-03-28 09:56:46 -05004371 lkb->lkb_flags &= ~DLM_IFL_RESEND;
4372 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
4373 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
4374 lkb->lkb_wait_type = 0;
4375 lkb->lkb_wait_count = 0;
4376 mutex_lock(&ls->ls_waiters_mutex);
4377 list_del_init(&lkb->lkb_wait_reply);
4378 mutex_unlock(&ls->ls_waiters_mutex);
4379 unhold_lkb(lkb); /* for waiters list */
David Teiglande7fd4172006-01-18 09:30:29 +00004380
David Teiglandef0c2bb2007-03-28 09:56:46 -05004381 if (oc || ou) {
4382 /* do an unlock or cancel instead of resending */
4383 switch (mstype) {
4384 case DLM_MSG_LOOKUP:
4385 case DLM_MSG_REQUEST:
4386 queue_cast(r, lkb, ou ? -DLM_EUNLOCK :
4387 -DLM_ECANCEL);
4388 unhold_lkb(lkb); /* undoes create_lkb() */
4389 break;
4390 case DLM_MSG_CONVERT:
4391 if (oc) {
4392 queue_cast(r, lkb, -DLM_ECANCEL);
4393 } else {
4394 lkb->lkb_exflags |= DLM_LKF_FORCEUNLOCK;
4395 _unlock_lock(r, lkb);
4396 }
4397 break;
4398 default:
4399 err = 1;
4400 }
4401 } else {
4402 switch (mstype) {
4403 case DLM_MSG_LOOKUP:
4404 case DLM_MSG_REQUEST:
4405 _request_lock(r, lkb);
4406 if (is_master(r))
4407 confirm_master(r, 0);
4408 break;
4409 case DLM_MSG_CONVERT:
4410 _convert_lock(r, lkb);
4411 break;
4412 default:
4413 err = 1;
4414 }
David Teiglande7fd4172006-01-18 09:30:29 +00004415 }
David Teiglandef0c2bb2007-03-28 09:56:46 -05004416
4417 if (err)
4418 log_error(ls, "recover_waiters_post %x %d %x %d %d",
4419 lkb->lkb_id, mstype, lkb->lkb_flags, oc, ou);
4420 unlock_rsb(r);
4421 put_rsb(r);
4422 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00004423 }
4424
4425 return error;
4426}
4427
4428static void purge_queue(struct dlm_rsb *r, struct list_head *queue,
4429 int (*test)(struct dlm_ls *ls, struct dlm_lkb *lkb))
4430{
4431 struct dlm_ls *ls = r->res_ls;
4432 struct dlm_lkb *lkb, *safe;
4433
4434 list_for_each_entry_safe(lkb, safe, queue, lkb_statequeue) {
4435 if (test(ls, lkb)) {
David Teigland97a35d12006-05-02 13:34:03 -04004436 rsb_set_flag(r, RSB_LOCKS_PURGED);
David Teiglande7fd4172006-01-18 09:30:29 +00004437 del_lkb(r, lkb);
4438 /* this put should free the lkb */
David Teiglandb3f58d82006-02-28 11:16:37 -05004439 if (!dlm_put_lkb(lkb))
David Teiglande7fd4172006-01-18 09:30:29 +00004440 log_error(ls, "purged lkb not released");
4441 }
4442 }
4443}
4444
4445static int purge_dead_test(struct dlm_ls *ls, struct dlm_lkb *lkb)
4446{
4447 return (is_master_copy(lkb) && dlm_is_removed(ls, lkb->lkb_nodeid));
4448}
4449
4450static int purge_mstcpy_test(struct dlm_ls *ls, struct dlm_lkb *lkb)
4451{
4452 return is_master_copy(lkb);
4453}
4454
4455static void purge_dead_locks(struct dlm_rsb *r)
4456{
4457 purge_queue(r, &r->res_grantqueue, &purge_dead_test);
4458 purge_queue(r, &r->res_convertqueue, &purge_dead_test);
4459 purge_queue(r, &r->res_waitqueue, &purge_dead_test);
4460}
4461
4462void dlm_purge_mstcpy_locks(struct dlm_rsb *r)
4463{
4464 purge_queue(r, &r->res_grantqueue, &purge_mstcpy_test);
4465 purge_queue(r, &r->res_convertqueue, &purge_mstcpy_test);
4466 purge_queue(r, &r->res_waitqueue, &purge_mstcpy_test);
4467}
4468
4469/* Get rid of locks held by nodes that are gone. */
4470
4471int dlm_purge_locks(struct dlm_ls *ls)
4472{
4473 struct dlm_rsb *r;
4474
4475 log_debug(ls, "dlm_purge_locks");
4476
4477 down_write(&ls->ls_root_sem);
4478 list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
4479 hold_rsb(r);
4480 lock_rsb(r);
4481 if (is_master(r))
4482 purge_dead_locks(r);
4483 unlock_rsb(r);
4484 unhold_rsb(r);
4485
4486 schedule();
4487 }
4488 up_write(&ls->ls_root_sem);
4489
4490 return 0;
4491}
4492
David Teigland97a35d12006-05-02 13:34:03 -04004493static struct dlm_rsb *find_purged_rsb(struct dlm_ls *ls, int bucket)
4494{
Bob Peterson9beb3bf2011-10-26 15:24:55 -05004495 struct rb_node *n;
David Teigland97a35d12006-05-02 13:34:03 -04004496 struct dlm_rsb *r, *r_ret = NULL;
4497
David Teiglandc7be7612009-01-07 16:50:41 -06004498 spin_lock(&ls->ls_rsbtbl[bucket].lock);
Bob Peterson9beb3bf2011-10-26 15:24:55 -05004499 for (n = rb_first(&ls->ls_rsbtbl[bucket].keep); n; n = rb_next(n)) {
4500 r = rb_entry(n, struct dlm_rsb, res_hashnode);
David Teigland97a35d12006-05-02 13:34:03 -04004501 if (!rsb_flag(r, RSB_LOCKS_PURGED))
4502 continue;
4503 hold_rsb(r);
4504 rsb_clear_flag(r, RSB_LOCKS_PURGED);
4505 r_ret = r;
4506 break;
4507 }
David Teiglandc7be7612009-01-07 16:50:41 -06004508 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teigland97a35d12006-05-02 13:34:03 -04004509 return r_ret;
4510}
4511
4512void dlm_grant_after_purge(struct dlm_ls *ls)
David Teiglande7fd4172006-01-18 09:30:29 +00004513{
4514 struct dlm_rsb *r;
David Teigland2b4e9262006-07-25 13:59:48 -05004515 int bucket = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00004516
David Teigland2b4e9262006-07-25 13:59:48 -05004517 while (1) {
4518 r = find_purged_rsb(ls, bucket);
4519 if (!r) {
4520 if (bucket == ls->ls_rsbtbl_size - 1)
4521 break;
4522 bucket++;
David Teigland97a35d12006-05-02 13:34:03 -04004523 continue;
David Teigland2b4e9262006-07-25 13:59:48 -05004524 }
David Teigland97a35d12006-05-02 13:34:03 -04004525 lock_rsb(r);
4526 if (is_master(r)) {
4527 grant_pending_locks(r);
4528 confirm_master(r, 0);
David Teiglande7fd4172006-01-18 09:30:29 +00004529 }
David Teigland97a35d12006-05-02 13:34:03 -04004530 unlock_rsb(r);
4531 put_rsb(r);
David Teigland2b4e9262006-07-25 13:59:48 -05004532 schedule();
David Teiglande7fd4172006-01-18 09:30:29 +00004533 }
David Teiglande7fd4172006-01-18 09:30:29 +00004534}
4535
4536static struct dlm_lkb *search_remid_list(struct list_head *head, int nodeid,
4537 uint32_t remid)
4538{
4539 struct dlm_lkb *lkb;
4540
4541 list_for_each_entry(lkb, head, lkb_statequeue) {
4542 if (lkb->lkb_nodeid == nodeid && lkb->lkb_remid == remid)
4543 return lkb;
4544 }
4545 return NULL;
4546}
4547
4548static struct dlm_lkb *search_remid(struct dlm_rsb *r, int nodeid,
4549 uint32_t remid)
4550{
4551 struct dlm_lkb *lkb;
4552
4553 lkb = search_remid_list(&r->res_grantqueue, nodeid, remid);
4554 if (lkb)
4555 return lkb;
4556 lkb = search_remid_list(&r->res_convertqueue, nodeid, remid);
4557 if (lkb)
4558 return lkb;
4559 lkb = search_remid_list(&r->res_waitqueue, nodeid, remid);
4560 if (lkb)
4561 return lkb;
4562 return NULL;
4563}
4564
Al Viroae773d02008-01-25 19:55:09 -05004565/* needs at least dlm_rcom + rcom_lock */
David Teiglande7fd4172006-01-18 09:30:29 +00004566static int receive_rcom_lock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
4567 struct dlm_rsb *r, struct dlm_rcom *rc)
4568{
4569 struct rcom_lock *rl = (struct rcom_lock *) rc->rc_buf;
David Teiglande7fd4172006-01-18 09:30:29 +00004570
4571 lkb->lkb_nodeid = rc->rc_header.h_nodeid;
Al Viro163a1852008-01-25 02:08:26 -05004572 lkb->lkb_ownpid = le32_to_cpu(rl->rl_ownpid);
4573 lkb->lkb_remid = le32_to_cpu(rl->rl_lkid);
4574 lkb->lkb_exflags = le32_to_cpu(rl->rl_exflags);
4575 lkb->lkb_flags = le32_to_cpu(rl->rl_flags) & 0x0000FFFF;
David Teiglande7fd4172006-01-18 09:30:29 +00004576 lkb->lkb_flags |= DLM_IFL_MSTCPY;
Al Viro163a1852008-01-25 02:08:26 -05004577 lkb->lkb_lvbseq = le32_to_cpu(rl->rl_lvbseq);
David Teiglande7fd4172006-01-18 09:30:29 +00004578 lkb->lkb_rqmode = rl->rl_rqmode;
4579 lkb->lkb_grmode = rl->rl_grmode;
4580 /* don't set lkb_status because add_lkb wants to itself */
4581
David Teigland8304d6f2011-02-21 14:58:21 -06004582 lkb->lkb_bastfn = (rl->rl_asts & DLM_CB_BAST) ? &fake_bastfn : NULL;
4583 lkb->lkb_astfn = (rl->rl_asts & DLM_CB_CAST) ? &fake_astfn : NULL;
David Teiglande7fd4172006-01-18 09:30:29 +00004584
David Teiglande7fd4172006-01-18 09:30:29 +00004585 if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
Al Viroa5dd0632008-01-25 20:22:22 -05004586 int lvblen = rc->rc_header.h_length - sizeof(struct dlm_rcom) -
4587 sizeof(struct rcom_lock);
4588 if (lvblen > ls->ls_lvblen)
4589 return -EINVAL;
David Teigland52bda2b2007-11-07 09:06:49 -06004590 lkb->lkb_lvbptr = dlm_allocate_lvb(ls);
David Teiglande7fd4172006-01-18 09:30:29 +00004591 if (!lkb->lkb_lvbptr)
4592 return -ENOMEM;
David Teiglande7fd4172006-01-18 09:30:29 +00004593 memcpy(lkb->lkb_lvbptr, rl->rl_lvb, lvblen);
4594 }
4595
4596 /* Conversions between PR and CW (middle modes) need special handling.
4597 The real granted mode of these converting locks cannot be determined
4598 until all locks have been rebuilt on the rsb (recover_conversion) */
4599
Al Viro163a1852008-01-25 02:08:26 -05004600 if (rl->rl_wait_type == cpu_to_le16(DLM_MSG_CONVERT) &&
4601 middle_conversion(lkb)) {
David Teiglande7fd4172006-01-18 09:30:29 +00004602 rl->rl_status = DLM_LKSTS_CONVERT;
4603 lkb->lkb_grmode = DLM_LOCK_IV;
4604 rsb_set_flag(r, RSB_RECOVER_CONVERT);
4605 }
4606
4607 return 0;
4608}
4609
4610/* This lkb may have been recovered in a previous aborted recovery so we need
4611 to check if the rsb already has an lkb with the given remote nodeid/lkid.
4612 If so we just send back a standard reply. If not, we create a new lkb with
4613 the given values and send back our lkid. We send back our lkid by sending
4614 back the rcom_lock struct we got but with the remid field filled in. */
4615
Al Viroae773d02008-01-25 19:55:09 -05004616/* needs at least dlm_rcom + rcom_lock */
David Teiglande7fd4172006-01-18 09:30:29 +00004617int dlm_recover_master_copy(struct dlm_ls *ls, struct dlm_rcom *rc)
4618{
4619 struct rcom_lock *rl = (struct rcom_lock *) rc->rc_buf;
4620 struct dlm_rsb *r;
4621 struct dlm_lkb *lkb;
4622 int error;
4623
4624 if (rl->rl_parent_lkid) {
4625 error = -EOPNOTSUPP;
4626 goto out;
4627 }
4628
Al Viro163a1852008-01-25 02:08:26 -05004629 error = find_rsb(ls, rl->rl_name, le16_to_cpu(rl->rl_namelen),
4630 R_MASTER, &r);
David Teiglande7fd4172006-01-18 09:30:29 +00004631 if (error)
4632 goto out;
4633
4634 lock_rsb(r);
4635
Al Viro163a1852008-01-25 02:08:26 -05004636 lkb = search_remid(r, rc->rc_header.h_nodeid, le32_to_cpu(rl->rl_lkid));
David Teiglande7fd4172006-01-18 09:30:29 +00004637 if (lkb) {
4638 error = -EEXIST;
4639 goto out_remid;
4640 }
4641
4642 error = create_lkb(ls, &lkb);
4643 if (error)
4644 goto out_unlock;
4645
4646 error = receive_rcom_lock_args(ls, lkb, r, rc);
4647 if (error) {
David Teiglandb3f58d82006-02-28 11:16:37 -05004648 __put_lkb(ls, lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00004649 goto out_unlock;
4650 }
4651
4652 attach_lkb(r, lkb);
4653 add_lkb(r, lkb, rl->rl_status);
4654 error = 0;
4655
4656 out_remid:
4657 /* this is the new value returned to the lock holder for
4658 saving in its process-copy lkb */
Al Viro163a1852008-01-25 02:08:26 -05004659 rl->rl_remid = cpu_to_le32(lkb->lkb_id);
David Teiglande7fd4172006-01-18 09:30:29 +00004660
4661 out_unlock:
4662 unlock_rsb(r);
4663 put_rsb(r);
4664 out:
4665 if (error)
Al Viro163a1852008-01-25 02:08:26 -05004666 log_debug(ls, "recover_master_copy %d %x", error,
4667 le32_to_cpu(rl->rl_lkid));
4668 rl->rl_result = cpu_to_le32(error);
David Teiglande7fd4172006-01-18 09:30:29 +00004669 return error;
4670}
4671
Al Viroae773d02008-01-25 19:55:09 -05004672/* needs at least dlm_rcom + rcom_lock */
David Teiglande7fd4172006-01-18 09:30:29 +00004673int dlm_recover_process_copy(struct dlm_ls *ls, struct dlm_rcom *rc)
4674{
4675 struct rcom_lock *rl = (struct rcom_lock *) rc->rc_buf;
4676 struct dlm_rsb *r;
4677 struct dlm_lkb *lkb;
4678 int error;
4679
Al Viro163a1852008-01-25 02:08:26 -05004680 error = find_lkb(ls, le32_to_cpu(rl->rl_lkid), &lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00004681 if (error) {
Al Viro163a1852008-01-25 02:08:26 -05004682 log_error(ls, "recover_process_copy no lkid %x",
4683 le32_to_cpu(rl->rl_lkid));
David Teiglande7fd4172006-01-18 09:30:29 +00004684 return error;
4685 }
4686
4687 DLM_ASSERT(is_process_copy(lkb), dlm_print_lkb(lkb););
4688
Al Viro163a1852008-01-25 02:08:26 -05004689 error = le32_to_cpu(rl->rl_result);
David Teiglande7fd4172006-01-18 09:30:29 +00004690
4691 r = lkb->lkb_resource;
4692 hold_rsb(r);
4693 lock_rsb(r);
4694
4695 switch (error) {
David Teiglanddc200a82006-12-13 10:36:37 -06004696 case -EBADR:
4697 /* There's a chance the new master received our lock before
4698 dlm_recover_master_reply(), this wouldn't happen if we did
4699 a barrier between recover_masters and recover_locks. */
4700 log_debug(ls, "master copy not ready %x r %lx %s", lkb->lkb_id,
4701 (unsigned long)r, r->res_name);
4702 dlm_send_rcom_lock(r, lkb);
4703 goto out;
David Teiglande7fd4172006-01-18 09:30:29 +00004704 case -EEXIST:
4705 log_debug(ls, "master copy exists %x", lkb->lkb_id);
4706 /* fall through */
4707 case 0:
Al Viro163a1852008-01-25 02:08:26 -05004708 lkb->lkb_remid = le32_to_cpu(rl->rl_remid);
David Teiglande7fd4172006-01-18 09:30:29 +00004709 break;
4710 default:
4711 log_error(ls, "dlm_recover_process_copy unknown error %d %x",
4712 error, lkb->lkb_id);
4713 }
4714
4715 /* an ack for dlm_recover_locks() which waits for replies from
4716 all the locks it sends to new masters */
4717 dlm_recovered_lock(r);
David Teiglanddc200a82006-12-13 10:36:37 -06004718 out:
David Teiglande7fd4172006-01-18 09:30:29 +00004719 unlock_rsb(r);
4720 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05004721 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00004722
4723 return 0;
4724}
4725
David Teigland597d0ca2006-07-12 16:44:04 -05004726int dlm_user_request(struct dlm_ls *ls, struct dlm_user_args *ua,
4727 int mode, uint32_t flags, void *name, unsigned int namelen,
David Teiglandd7db9232007-05-18 09:00:32 -05004728 unsigned long timeout_cs)
David Teigland597d0ca2006-07-12 16:44:04 -05004729{
4730 struct dlm_lkb *lkb;
4731 struct dlm_args args;
4732 int error;
4733
David Teigland85e86ed2007-05-18 08:58:15 -05004734 dlm_lock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05004735
4736 error = create_lkb(ls, &lkb);
4737 if (error) {
4738 kfree(ua);
4739 goto out;
4740 }
4741
4742 if (flags & DLM_LKF_VALBLK) {
David Teigland573c24c2009-11-30 16:34:43 -06004743 ua->lksb.sb_lvbptr = kzalloc(DLM_USER_LVB_LEN, GFP_NOFS);
David Teigland597d0ca2006-07-12 16:44:04 -05004744 if (!ua->lksb.sb_lvbptr) {
4745 kfree(ua);
4746 __put_lkb(ls, lkb);
4747 error = -ENOMEM;
4748 goto out;
4749 }
4750 }
4751
David Teigland52bda2b2007-11-07 09:06:49 -06004752 /* After ua is attached to lkb it will be freed by dlm_free_lkb().
David Teigland597d0ca2006-07-12 16:44:04 -05004753 When DLM_IFL_USER is set, the dlm knows that this is a userspace
4754 lock and that lkb_astparam is the dlm_user_args structure. */
4755
David Teiglandd7db9232007-05-18 09:00:32 -05004756 error = set_lock_args(mode, &ua->lksb, flags, namelen, timeout_cs,
David Teiglande5dae542008-02-06 00:35:45 -06004757 fake_astfn, ua, fake_bastfn, &args);
David Teigland597d0ca2006-07-12 16:44:04 -05004758 lkb->lkb_flags |= DLM_IFL_USER;
David Teigland597d0ca2006-07-12 16:44:04 -05004759
4760 if (error) {
4761 __put_lkb(ls, lkb);
4762 goto out;
4763 }
4764
4765 error = request_lock(ls, lkb, name, namelen, &args);
4766
4767 switch (error) {
4768 case 0:
4769 break;
4770 case -EINPROGRESS:
4771 error = 0;
4772 break;
4773 case -EAGAIN:
4774 error = 0;
4775 /* fall through */
4776 default:
4777 __put_lkb(ls, lkb);
4778 goto out;
4779 }
4780
4781 /* add this new lkb to the per-process list of locks */
4782 spin_lock(&ua->proc->locks_spin);
David Teiglandef0c2bb2007-03-28 09:56:46 -05004783 hold_lkb(lkb);
David Teigland597d0ca2006-07-12 16:44:04 -05004784 list_add_tail(&lkb->lkb_ownqueue, &ua->proc->locks);
4785 spin_unlock(&ua->proc->locks_spin);
4786 out:
David Teigland85e86ed2007-05-18 08:58:15 -05004787 dlm_unlock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05004788 return error;
4789}
4790
4791int dlm_user_convert(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
David Teiglandd7db9232007-05-18 09:00:32 -05004792 int mode, uint32_t flags, uint32_t lkid, char *lvb_in,
4793 unsigned long timeout_cs)
David Teigland597d0ca2006-07-12 16:44:04 -05004794{
4795 struct dlm_lkb *lkb;
4796 struct dlm_args args;
4797 struct dlm_user_args *ua;
4798 int error;
4799
David Teigland85e86ed2007-05-18 08:58:15 -05004800 dlm_lock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05004801
4802 error = find_lkb(ls, lkid, &lkb);
4803 if (error)
4804 goto out;
4805
4806 /* user can change the params on its lock when it converts it, or
4807 add an lvb that didn't exist before */
4808
David Teiglandd292c0c2008-02-06 23:27:04 -06004809 ua = lkb->lkb_ua;
David Teigland597d0ca2006-07-12 16:44:04 -05004810
4811 if (flags & DLM_LKF_VALBLK && !ua->lksb.sb_lvbptr) {
David Teigland573c24c2009-11-30 16:34:43 -06004812 ua->lksb.sb_lvbptr = kzalloc(DLM_USER_LVB_LEN, GFP_NOFS);
David Teigland597d0ca2006-07-12 16:44:04 -05004813 if (!ua->lksb.sb_lvbptr) {
4814 error = -ENOMEM;
4815 goto out_put;
4816 }
4817 }
4818 if (lvb_in && ua->lksb.sb_lvbptr)
4819 memcpy(ua->lksb.sb_lvbptr, lvb_in, DLM_USER_LVB_LEN);
4820
David Teiglandd7db9232007-05-18 09:00:32 -05004821 ua->xid = ua_tmp->xid;
David Teigland597d0ca2006-07-12 16:44:04 -05004822 ua->castparam = ua_tmp->castparam;
4823 ua->castaddr = ua_tmp->castaddr;
4824 ua->bastparam = ua_tmp->bastparam;
4825 ua->bastaddr = ua_tmp->bastaddr;
Patrick Caulfield10948eb2006-08-23 09:49:31 +01004826 ua->user_lksb = ua_tmp->user_lksb;
David Teigland597d0ca2006-07-12 16:44:04 -05004827
David Teiglandd7db9232007-05-18 09:00:32 -05004828 error = set_lock_args(mode, &ua->lksb, flags, 0, timeout_cs,
David Teiglande5dae542008-02-06 00:35:45 -06004829 fake_astfn, ua, fake_bastfn, &args);
David Teigland597d0ca2006-07-12 16:44:04 -05004830 if (error)
4831 goto out_put;
4832
4833 error = convert_lock(ls, lkb, &args);
4834
David Teiglandc85d65e2007-05-18 09:01:26 -05004835 if (error == -EINPROGRESS || error == -EAGAIN || error == -EDEADLK)
David Teigland597d0ca2006-07-12 16:44:04 -05004836 error = 0;
4837 out_put:
4838 dlm_put_lkb(lkb);
4839 out:
David Teigland85e86ed2007-05-18 08:58:15 -05004840 dlm_unlock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05004841 kfree(ua_tmp);
4842 return error;
4843}
4844
4845int dlm_user_unlock(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
4846 uint32_t flags, uint32_t lkid, char *lvb_in)
4847{
4848 struct dlm_lkb *lkb;
4849 struct dlm_args args;
4850 struct dlm_user_args *ua;
4851 int error;
4852
David Teigland85e86ed2007-05-18 08:58:15 -05004853 dlm_lock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05004854
4855 error = find_lkb(ls, lkid, &lkb);
4856 if (error)
4857 goto out;
4858
David Teiglandd292c0c2008-02-06 23:27:04 -06004859 ua = lkb->lkb_ua;
David Teigland597d0ca2006-07-12 16:44:04 -05004860
4861 if (lvb_in && ua->lksb.sb_lvbptr)
4862 memcpy(ua->lksb.sb_lvbptr, lvb_in, DLM_USER_LVB_LEN);
Patrick Caulfieldb434eda2007-10-01 15:28:42 +01004863 if (ua_tmp->castparam)
4864 ua->castparam = ua_tmp->castparam;
Patrick Caulfieldcc346d52006-08-08 10:34:40 -04004865 ua->user_lksb = ua_tmp->user_lksb;
David Teigland597d0ca2006-07-12 16:44:04 -05004866
4867 error = set_unlock_args(flags, ua, &args);
4868 if (error)
4869 goto out_put;
4870
4871 error = unlock_lock(ls, lkb, &args);
4872
4873 if (error == -DLM_EUNLOCK)
4874 error = 0;
David Teiglandef0c2bb2007-03-28 09:56:46 -05004875 /* from validate_unlock_args() */
4876 if (error == -EBUSY && (flags & DLM_LKF_FORCEUNLOCK))
4877 error = 0;
David Teigland597d0ca2006-07-12 16:44:04 -05004878 if (error)
4879 goto out_put;
4880
4881 spin_lock(&ua->proc->locks_spin);
David Teigland23e8e1a2011-04-05 13:16:24 -05004882 /* dlm_user_add_cb() may have already taken lkb off the proc list */
David Teiglanda1bc86e2007-01-15 10:34:52 -06004883 if (!list_empty(&lkb->lkb_ownqueue))
4884 list_move(&lkb->lkb_ownqueue, &ua->proc->unlocking);
David Teigland597d0ca2006-07-12 16:44:04 -05004885 spin_unlock(&ua->proc->locks_spin);
David Teigland597d0ca2006-07-12 16:44:04 -05004886 out_put:
4887 dlm_put_lkb(lkb);
4888 out:
David Teigland85e86ed2007-05-18 08:58:15 -05004889 dlm_unlock_recovery(ls);
David Teiglandef0c2bb2007-03-28 09:56:46 -05004890 kfree(ua_tmp);
David Teigland597d0ca2006-07-12 16:44:04 -05004891 return error;
4892}
4893
4894int dlm_user_cancel(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
4895 uint32_t flags, uint32_t lkid)
4896{
4897 struct dlm_lkb *lkb;
4898 struct dlm_args args;
4899 struct dlm_user_args *ua;
4900 int error;
4901
David Teigland85e86ed2007-05-18 08:58:15 -05004902 dlm_lock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05004903
4904 error = find_lkb(ls, lkid, &lkb);
4905 if (error)
4906 goto out;
4907
David Teiglandd292c0c2008-02-06 23:27:04 -06004908 ua = lkb->lkb_ua;
Patrick Caulfieldb434eda2007-10-01 15:28:42 +01004909 if (ua_tmp->castparam)
4910 ua->castparam = ua_tmp->castparam;
Patrick Caulfieldc059f702006-08-23 10:24:03 +01004911 ua->user_lksb = ua_tmp->user_lksb;
David Teigland597d0ca2006-07-12 16:44:04 -05004912
4913 error = set_unlock_args(flags, ua, &args);
4914 if (error)
4915 goto out_put;
4916
4917 error = cancel_lock(ls, lkb, &args);
4918
4919 if (error == -DLM_ECANCEL)
4920 error = 0;
David Teiglandef0c2bb2007-03-28 09:56:46 -05004921 /* from validate_unlock_args() */
4922 if (error == -EBUSY)
4923 error = 0;
David Teigland597d0ca2006-07-12 16:44:04 -05004924 out_put:
4925 dlm_put_lkb(lkb);
4926 out:
David Teigland85e86ed2007-05-18 08:58:15 -05004927 dlm_unlock_recovery(ls);
David Teiglandef0c2bb2007-03-28 09:56:46 -05004928 kfree(ua_tmp);
David Teigland597d0ca2006-07-12 16:44:04 -05004929 return error;
4930}
4931
David Teigland8b4021f2007-05-29 08:46:00 -05004932int dlm_user_deadlock(struct dlm_ls *ls, uint32_t flags, uint32_t lkid)
4933{
4934 struct dlm_lkb *lkb;
4935 struct dlm_args args;
4936 struct dlm_user_args *ua;
4937 struct dlm_rsb *r;
4938 int error;
4939
4940 dlm_lock_recovery(ls);
4941
4942 error = find_lkb(ls, lkid, &lkb);
4943 if (error)
4944 goto out;
4945
David Teiglandd292c0c2008-02-06 23:27:04 -06004946 ua = lkb->lkb_ua;
David Teigland8b4021f2007-05-29 08:46:00 -05004947
4948 error = set_unlock_args(flags, ua, &args);
4949 if (error)
4950 goto out_put;
4951
4952 /* same as cancel_lock(), but set DEADLOCK_CANCEL after lock_rsb */
4953
4954 r = lkb->lkb_resource;
4955 hold_rsb(r);
4956 lock_rsb(r);
4957
4958 error = validate_unlock_args(lkb, &args);
4959 if (error)
4960 goto out_r;
4961 lkb->lkb_flags |= DLM_IFL_DEADLOCK_CANCEL;
4962
4963 error = _cancel_lock(r, lkb);
4964 out_r:
4965 unlock_rsb(r);
4966 put_rsb(r);
4967
4968 if (error == -DLM_ECANCEL)
4969 error = 0;
4970 /* from validate_unlock_args() */
4971 if (error == -EBUSY)
4972 error = 0;
4973 out_put:
4974 dlm_put_lkb(lkb);
4975 out:
4976 dlm_unlock_recovery(ls);
4977 return error;
4978}
4979
David Teiglandef0c2bb2007-03-28 09:56:46 -05004980/* lkb's that are removed from the waiters list by revert are just left on the
4981 orphans list with the granted orphan locks, to be freed by purge */
4982
David Teigland597d0ca2006-07-12 16:44:04 -05004983static int orphan_proc_lock(struct dlm_ls *ls, struct dlm_lkb *lkb)
4984{
David Teiglandef0c2bb2007-03-28 09:56:46 -05004985 struct dlm_args args;
4986 int error;
David Teigland597d0ca2006-07-12 16:44:04 -05004987
David Teiglandef0c2bb2007-03-28 09:56:46 -05004988 hold_lkb(lkb);
4989 mutex_lock(&ls->ls_orphans_mutex);
4990 list_add_tail(&lkb->lkb_ownqueue, &ls->ls_orphans);
4991 mutex_unlock(&ls->ls_orphans_mutex);
David Teigland597d0ca2006-07-12 16:44:04 -05004992
David Teiglandd292c0c2008-02-06 23:27:04 -06004993 set_unlock_args(0, lkb->lkb_ua, &args);
David Teiglandef0c2bb2007-03-28 09:56:46 -05004994
4995 error = cancel_lock(ls, lkb, &args);
4996 if (error == -DLM_ECANCEL)
4997 error = 0;
4998 return error;
David Teigland597d0ca2006-07-12 16:44:04 -05004999}
5000
5001/* The force flag allows the unlock to go ahead even if the lkb isn't granted.
5002 Regardless of what rsb queue the lock is on, it's removed and freed. */
5003
5004static int unlock_proc_lock(struct dlm_ls *ls, struct dlm_lkb *lkb)
5005{
David Teigland597d0ca2006-07-12 16:44:04 -05005006 struct dlm_args args;
5007 int error;
5008
David Teiglandd292c0c2008-02-06 23:27:04 -06005009 set_unlock_args(DLM_LKF_FORCEUNLOCK, lkb->lkb_ua, &args);
David Teigland597d0ca2006-07-12 16:44:04 -05005010
5011 error = unlock_lock(ls, lkb, &args);
5012 if (error == -DLM_EUNLOCK)
5013 error = 0;
5014 return error;
5015}
5016
David Teiglandef0c2bb2007-03-28 09:56:46 -05005017/* We have to release clear_proc_locks mutex before calling unlock_proc_lock()
5018 (which does lock_rsb) due to deadlock with receiving a message that does
David Teigland23e8e1a2011-04-05 13:16:24 -05005019 lock_rsb followed by dlm_user_add_cb() */
David Teiglandef0c2bb2007-03-28 09:56:46 -05005020
5021static struct dlm_lkb *del_proc_lock(struct dlm_ls *ls,
5022 struct dlm_user_proc *proc)
5023{
5024 struct dlm_lkb *lkb = NULL;
5025
5026 mutex_lock(&ls->ls_clear_proc_locks);
5027 if (list_empty(&proc->locks))
5028 goto out;
5029
5030 lkb = list_entry(proc->locks.next, struct dlm_lkb, lkb_ownqueue);
5031 list_del_init(&lkb->lkb_ownqueue);
5032
5033 if (lkb->lkb_exflags & DLM_LKF_PERSISTENT)
5034 lkb->lkb_flags |= DLM_IFL_ORPHAN;
5035 else
5036 lkb->lkb_flags |= DLM_IFL_DEAD;
5037 out:
5038 mutex_unlock(&ls->ls_clear_proc_locks);
5039 return lkb;
5040}
5041
David Teigland23e8e1a2011-04-05 13:16:24 -05005042/* The ls_clear_proc_locks mutex protects against dlm_user_add_cb() which
David Teigland597d0ca2006-07-12 16:44:04 -05005043 1) references lkb->ua which we free here and 2) adds lkbs to proc->asts,
5044 which we clear here. */
5045
5046/* proc CLOSING flag is set so no more device_reads should look at proc->asts
5047 list, and no more device_writes should add lkb's to proc->locks list; so we
5048 shouldn't need to take asts_spin or locks_spin here. this assumes that
5049 device reads/writes/closes are serialized -- FIXME: we may need to serialize
5050 them ourself. */
5051
5052void dlm_clear_proc_locks(struct dlm_ls *ls, struct dlm_user_proc *proc)
5053{
5054 struct dlm_lkb *lkb, *safe;
5055
David Teigland85e86ed2007-05-18 08:58:15 -05005056 dlm_lock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05005057
David Teiglandef0c2bb2007-03-28 09:56:46 -05005058 while (1) {
5059 lkb = del_proc_lock(ls, proc);
5060 if (!lkb)
5061 break;
David Teigland84d8cd62007-05-29 08:44:23 -05005062 del_timeout(lkb);
David Teiglandef0c2bb2007-03-28 09:56:46 -05005063 if (lkb->lkb_exflags & DLM_LKF_PERSISTENT)
David Teigland597d0ca2006-07-12 16:44:04 -05005064 orphan_proc_lock(ls, lkb);
David Teiglandef0c2bb2007-03-28 09:56:46 -05005065 else
David Teigland597d0ca2006-07-12 16:44:04 -05005066 unlock_proc_lock(ls, lkb);
David Teigland597d0ca2006-07-12 16:44:04 -05005067
5068 /* this removes the reference for the proc->locks list
5069 added by dlm_user_request, it may result in the lkb
5070 being freed */
5071
5072 dlm_put_lkb(lkb);
5073 }
David Teiglanda1bc86e2007-01-15 10:34:52 -06005074
David Teiglandef0c2bb2007-03-28 09:56:46 -05005075 mutex_lock(&ls->ls_clear_proc_locks);
5076
David Teiglanda1bc86e2007-01-15 10:34:52 -06005077 /* in-progress unlocks */
5078 list_for_each_entry_safe(lkb, safe, &proc->unlocking, lkb_ownqueue) {
5079 list_del_init(&lkb->lkb_ownqueue);
5080 lkb->lkb_flags |= DLM_IFL_DEAD;
5081 dlm_put_lkb(lkb);
5082 }
5083
David Teigland23e8e1a2011-04-05 13:16:24 -05005084 list_for_each_entry_safe(lkb, safe, &proc->asts, lkb_cb_list) {
David Teigland8304d6f2011-02-21 14:58:21 -06005085 memset(&lkb->lkb_callbacks, 0,
5086 sizeof(struct dlm_callback) * DLM_CALLBACKS_SIZE);
David Teigland23e8e1a2011-04-05 13:16:24 -05005087 list_del_init(&lkb->lkb_cb_list);
David Teiglanda1bc86e2007-01-15 10:34:52 -06005088 dlm_put_lkb(lkb);
5089 }
5090
David Teigland597d0ca2006-07-12 16:44:04 -05005091 mutex_unlock(&ls->ls_clear_proc_locks);
David Teigland85e86ed2007-05-18 08:58:15 -05005092 dlm_unlock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05005093}
David Teiglanda1bc86e2007-01-15 10:34:52 -06005094
David Teigland84991372007-03-30 15:02:40 -05005095static void purge_proc_locks(struct dlm_ls *ls, struct dlm_user_proc *proc)
5096{
5097 struct dlm_lkb *lkb, *safe;
5098
5099 while (1) {
5100 lkb = NULL;
5101 spin_lock(&proc->locks_spin);
5102 if (!list_empty(&proc->locks)) {
5103 lkb = list_entry(proc->locks.next, struct dlm_lkb,
5104 lkb_ownqueue);
5105 list_del_init(&lkb->lkb_ownqueue);
5106 }
5107 spin_unlock(&proc->locks_spin);
5108
5109 if (!lkb)
5110 break;
5111
5112 lkb->lkb_flags |= DLM_IFL_DEAD;
5113 unlock_proc_lock(ls, lkb);
5114 dlm_put_lkb(lkb); /* ref from proc->locks list */
5115 }
5116
5117 spin_lock(&proc->locks_spin);
5118 list_for_each_entry_safe(lkb, safe, &proc->unlocking, lkb_ownqueue) {
5119 list_del_init(&lkb->lkb_ownqueue);
5120 lkb->lkb_flags |= DLM_IFL_DEAD;
5121 dlm_put_lkb(lkb);
5122 }
5123 spin_unlock(&proc->locks_spin);
5124
5125 spin_lock(&proc->asts_spin);
David Teigland23e8e1a2011-04-05 13:16:24 -05005126 list_for_each_entry_safe(lkb, safe, &proc->asts, lkb_cb_list) {
David Teigland8304d6f2011-02-21 14:58:21 -06005127 memset(&lkb->lkb_callbacks, 0,
5128 sizeof(struct dlm_callback) * DLM_CALLBACKS_SIZE);
David Teigland23e8e1a2011-04-05 13:16:24 -05005129 list_del_init(&lkb->lkb_cb_list);
David Teigland84991372007-03-30 15:02:40 -05005130 dlm_put_lkb(lkb);
5131 }
5132 spin_unlock(&proc->asts_spin);
5133}
5134
5135/* pid of 0 means purge all orphans */
5136
5137static void do_purge(struct dlm_ls *ls, int nodeid, int pid)
5138{
5139 struct dlm_lkb *lkb, *safe;
5140
5141 mutex_lock(&ls->ls_orphans_mutex);
5142 list_for_each_entry_safe(lkb, safe, &ls->ls_orphans, lkb_ownqueue) {
5143 if (pid && lkb->lkb_ownpid != pid)
5144 continue;
5145 unlock_proc_lock(ls, lkb);
5146 list_del_init(&lkb->lkb_ownqueue);
5147 dlm_put_lkb(lkb);
5148 }
5149 mutex_unlock(&ls->ls_orphans_mutex);
5150}
5151
5152static int send_purge(struct dlm_ls *ls, int nodeid, int pid)
5153{
5154 struct dlm_message *ms;
5155 struct dlm_mhandle *mh;
5156 int error;
5157
5158 error = _create_message(ls, sizeof(struct dlm_message), nodeid,
5159 DLM_MSG_PURGE, &ms, &mh);
5160 if (error)
5161 return error;
5162 ms->m_nodeid = nodeid;
5163 ms->m_pid = pid;
5164
5165 return send_message(mh, ms);
5166}
5167
5168int dlm_user_purge(struct dlm_ls *ls, struct dlm_user_proc *proc,
5169 int nodeid, int pid)
5170{
5171 int error = 0;
5172
5173 if (nodeid != dlm_our_nodeid()) {
5174 error = send_purge(ls, nodeid, pid);
5175 } else {
David Teigland85e86ed2007-05-18 08:58:15 -05005176 dlm_lock_recovery(ls);
David Teigland84991372007-03-30 15:02:40 -05005177 if (pid == current->pid)
5178 purge_proc_locks(ls, proc);
5179 else
5180 do_purge(ls, nodeid, pid);
David Teigland85e86ed2007-05-18 08:58:15 -05005181 dlm_unlock_recovery(ls);
David Teigland84991372007-03-30 15:02:40 -05005182 }
5183 return error;
5184}
5185