blob: 3c723489079afbf4d6efdc536b23b111de7eb83a [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090059#include <linux/slab.h>
David Teiglande7fd4172006-01-18 09:30:29 +000060#include "dlm_internal.h"
David Teigland597d0ca2006-07-12 16:44:04 -050061#include <linux/dlm_device.h>
David Teiglande7fd4172006-01-18 09:30:29 +000062#include "memory.h"
63#include "lowcomms.h"
64#include "requestqueue.h"
65#include "util.h"
66#include "dir.h"
67#include "member.h"
68#include "lockspace.h"
69#include "ast.h"
70#include "lock.h"
71#include "rcom.h"
72#include "recover.h"
73#include "lvb_table.h"
David Teigland597d0ca2006-07-12 16:44:04 -050074#include "user.h"
David Teiglande7fd4172006-01-18 09:30:29 +000075#include "config.h"
76
77static int send_request(struct dlm_rsb *r, struct dlm_lkb *lkb);
78static int send_convert(struct dlm_rsb *r, struct dlm_lkb *lkb);
79static int send_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb);
80static int send_cancel(struct dlm_rsb *r, struct dlm_lkb *lkb);
81static int send_grant(struct dlm_rsb *r, struct dlm_lkb *lkb);
82static int send_bast(struct dlm_rsb *r, struct dlm_lkb *lkb, int mode);
83static int send_lookup(struct dlm_rsb *r, struct dlm_lkb *lkb);
84static int send_remove(struct dlm_rsb *r);
85static int _request_lock(struct dlm_rsb *r, struct dlm_lkb *lkb);
David Teigland3ae1acf2007-05-18 08:59:31 -050086static int _cancel_lock(struct dlm_rsb *r, struct dlm_lkb *lkb);
David Teiglande7fd4172006-01-18 09:30:29 +000087static void __receive_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
88 struct dlm_message *ms);
89static int receive_extralen(struct dlm_message *ms);
David Teigland84991372007-03-30 15:02:40 -050090static void do_purge(struct dlm_ls *ls, int nodeid, int pid);
David Teigland3ae1acf2007-05-18 08:59:31 -050091static void del_timeout(struct dlm_lkb *lkb);
David Teiglande7fd4172006-01-18 09:30:29 +000092
93/*
94 * Lock compatibilty matrix - thanks Steve
95 * UN = Unlocked state. Not really a state, used as a flag
96 * PD = Padding. Used to make the matrix a nice power of two in size
97 * Other states are the same as the VMS DLM.
98 * Usage: matrix[grmode+1][rqmode+1] (although m[rq+1][gr+1] is the same)
99 */
100
101static const int __dlm_compat_matrix[8][8] = {
102 /* UN NL CR CW PR PW EX PD */
103 {1, 1, 1, 1, 1, 1, 1, 0}, /* UN */
104 {1, 1, 1, 1, 1, 1, 1, 0}, /* NL */
105 {1, 1, 1, 1, 1, 1, 0, 0}, /* CR */
106 {1, 1, 1, 1, 0, 0, 0, 0}, /* CW */
107 {1, 1, 1, 0, 1, 0, 0, 0}, /* PR */
108 {1, 1, 1, 0, 0, 0, 0, 0}, /* PW */
109 {1, 1, 0, 0, 0, 0, 0, 0}, /* EX */
110 {0, 0, 0, 0, 0, 0, 0, 0} /* PD */
111};
112
113/*
114 * This defines the direction of transfer of LVB data.
115 * Granted mode is the row; requested mode is the column.
116 * Usage: matrix[grmode+1][rqmode+1]
117 * 1 = LVB is returned to the caller
118 * 0 = LVB is written to the resource
119 * -1 = nothing happens to the LVB
120 */
121
122const int dlm_lvb_operations[8][8] = {
123 /* UN NL CR CW PR PW EX PD*/
124 { -1, 1, 1, 1, 1, 1, 1, -1 }, /* UN */
125 { -1, 1, 1, 1, 1, 1, 1, 0 }, /* NL */
126 { -1, -1, 1, 1, 1, 1, 1, 0 }, /* CR */
127 { -1, -1, -1, 1, 1, 1, 1, 0 }, /* CW */
128 { -1, -1, -1, -1, 1, 1, 1, 0 }, /* PR */
129 { -1, 0, 0, 0, 0, 0, 1, 0 }, /* PW */
130 { -1, 0, 0, 0, 0, 0, 0, 0 }, /* EX */
131 { -1, 0, 0, 0, 0, 0, 0, 0 } /* PD */
132};
David Teiglande7fd4172006-01-18 09:30:29 +0000133
134#define modes_compat(gr, rq) \
135 __dlm_compat_matrix[(gr)->lkb_grmode + 1][(rq)->lkb_rqmode + 1]
136
137int dlm_modes_compat(int mode1, int mode2)
138{
139 return __dlm_compat_matrix[mode1 + 1][mode2 + 1];
140}
141
142/*
143 * Compatibility matrix for conversions with QUECVT set.
144 * Granted mode is the row; requested mode is the column.
145 * Usage: matrix[grmode+1][rqmode+1]
146 */
147
148static const int __quecvt_compat_matrix[8][8] = {
149 /* UN NL CR CW PR PW EX PD */
150 {0, 0, 0, 0, 0, 0, 0, 0}, /* UN */
151 {0, 0, 1, 1, 1, 1, 1, 0}, /* NL */
152 {0, 0, 0, 1, 1, 1, 1, 0}, /* CR */
153 {0, 0, 0, 0, 1, 1, 1, 0}, /* CW */
154 {0, 0, 0, 1, 0, 1, 1, 0}, /* PR */
155 {0, 0, 0, 0, 0, 0, 1, 0}, /* PW */
156 {0, 0, 0, 0, 0, 0, 0, 0}, /* EX */
157 {0, 0, 0, 0, 0, 0, 0, 0} /* PD */
158};
159
David Teigland597d0ca2006-07-12 16:44:04 -0500160void dlm_print_lkb(struct dlm_lkb *lkb)
David Teiglande7fd4172006-01-18 09:30:29 +0000161{
162 printk(KERN_ERR "lkb: nodeid %d id %x remid %x exflags %x flags %x\n"
David Teigland8304d6f2011-02-21 14:58:21 -0600163 " status %d rqmode %d grmode %d wait_type %d\n",
David Teiglande7fd4172006-01-18 09:30:29 +0000164 lkb->lkb_nodeid, lkb->lkb_id, lkb->lkb_remid, lkb->lkb_exflags,
165 lkb->lkb_flags, lkb->lkb_status, lkb->lkb_rqmode,
David Teigland8304d6f2011-02-21 14:58:21 -0600166 lkb->lkb_grmode, lkb->lkb_wait_type);
David Teiglande7fd4172006-01-18 09:30:29 +0000167}
168
Adrian Bunk170e19a2008-02-13 23:29:38 +0200169static void dlm_print_rsb(struct dlm_rsb *r)
David Teiglande7fd4172006-01-18 09:30:29 +0000170{
171 printk(KERN_ERR "rsb: nodeid %d flags %lx first %x rlc %d name %s\n",
172 r->res_nodeid, r->res_flags, r->res_first_lkid,
173 r->res_recover_locks_count, r->res_name);
174}
175
David Teiglanda345da32006-08-18 11:54:25 -0500176void dlm_dump_rsb(struct dlm_rsb *r)
177{
178 struct dlm_lkb *lkb;
179
180 dlm_print_rsb(r);
181
182 printk(KERN_ERR "rsb: root_list empty %d recover_list empty %d\n",
183 list_empty(&r->res_root_list), list_empty(&r->res_recover_list));
184 printk(KERN_ERR "rsb lookup list\n");
185 list_for_each_entry(lkb, &r->res_lookup, lkb_rsb_lookup)
186 dlm_print_lkb(lkb);
187 printk(KERN_ERR "rsb grant queue:\n");
188 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue)
189 dlm_print_lkb(lkb);
190 printk(KERN_ERR "rsb convert queue:\n");
191 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue)
192 dlm_print_lkb(lkb);
193 printk(KERN_ERR "rsb wait queue:\n");
194 list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue)
195 dlm_print_lkb(lkb);
196}
197
David Teiglande7fd4172006-01-18 09:30:29 +0000198/* Threads cannot use the lockspace while it's being recovered */
199
David Teigland85e86ed2007-05-18 08:58:15 -0500200static inline void dlm_lock_recovery(struct dlm_ls *ls)
David Teiglande7fd4172006-01-18 09:30:29 +0000201{
202 down_read(&ls->ls_in_recovery);
203}
204
David Teigland85e86ed2007-05-18 08:58:15 -0500205void dlm_unlock_recovery(struct dlm_ls *ls)
David Teiglande7fd4172006-01-18 09:30:29 +0000206{
207 up_read(&ls->ls_in_recovery);
208}
209
David Teigland85e86ed2007-05-18 08:58:15 -0500210int dlm_lock_recovery_try(struct dlm_ls *ls)
David Teiglande7fd4172006-01-18 09:30:29 +0000211{
212 return down_read_trylock(&ls->ls_in_recovery);
213}
214
215static inline int can_be_queued(struct dlm_lkb *lkb)
216{
217 return !(lkb->lkb_exflags & DLM_LKF_NOQUEUE);
218}
219
220static inline int force_blocking_asts(struct dlm_lkb *lkb)
221{
222 return (lkb->lkb_exflags & DLM_LKF_NOQUEUEBAST);
223}
224
225static inline int is_demoted(struct dlm_lkb *lkb)
226{
227 return (lkb->lkb_sbflags & DLM_SBF_DEMOTED);
228}
229
David Teigland7d3c1fe2007-04-19 10:30:41 -0500230static inline int is_altmode(struct dlm_lkb *lkb)
231{
232 return (lkb->lkb_sbflags & DLM_SBF_ALTMODE);
233}
234
235static inline int is_granted(struct dlm_lkb *lkb)
236{
237 return (lkb->lkb_status == DLM_LKSTS_GRANTED);
238}
239
David Teiglande7fd4172006-01-18 09:30:29 +0000240static inline int is_remote(struct dlm_rsb *r)
241{
242 DLM_ASSERT(r->res_nodeid >= 0, dlm_print_rsb(r););
243 return !!r->res_nodeid;
244}
245
246static inline int is_process_copy(struct dlm_lkb *lkb)
247{
248 return (lkb->lkb_nodeid && !(lkb->lkb_flags & DLM_IFL_MSTCPY));
249}
250
251static inline int is_master_copy(struct dlm_lkb *lkb)
252{
253 if (lkb->lkb_flags & DLM_IFL_MSTCPY)
254 DLM_ASSERT(lkb->lkb_nodeid, dlm_print_lkb(lkb););
David Teigland90135922006-01-20 08:47:07 +0000255 return (lkb->lkb_flags & DLM_IFL_MSTCPY) ? 1 : 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000256}
257
258static inline int middle_conversion(struct dlm_lkb *lkb)
259{
260 if ((lkb->lkb_grmode==DLM_LOCK_PR && lkb->lkb_rqmode==DLM_LOCK_CW) ||
261 (lkb->lkb_rqmode==DLM_LOCK_PR && lkb->lkb_grmode==DLM_LOCK_CW))
David Teigland90135922006-01-20 08:47:07 +0000262 return 1;
263 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000264}
265
266static inline int down_conversion(struct dlm_lkb *lkb)
267{
268 return (!middle_conversion(lkb) && lkb->lkb_rqmode < lkb->lkb_grmode);
269}
270
David Teiglandef0c2bb2007-03-28 09:56:46 -0500271static inline int is_overlap_unlock(struct dlm_lkb *lkb)
272{
273 return lkb->lkb_flags & DLM_IFL_OVERLAP_UNLOCK;
274}
275
276static inline int is_overlap_cancel(struct dlm_lkb *lkb)
277{
278 return lkb->lkb_flags & DLM_IFL_OVERLAP_CANCEL;
279}
280
281static inline int is_overlap(struct dlm_lkb *lkb)
282{
283 return (lkb->lkb_flags & (DLM_IFL_OVERLAP_UNLOCK |
284 DLM_IFL_OVERLAP_CANCEL));
285}
286
David Teiglande7fd4172006-01-18 09:30:29 +0000287static void queue_cast(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
288{
289 if (is_master_copy(lkb))
290 return;
291
David Teigland3ae1acf2007-05-18 08:59:31 -0500292 del_timeout(lkb);
293
David Teiglande7fd4172006-01-18 09:30:29 +0000294 DLM_ASSERT(lkb->lkb_lksb, dlm_print_lkb(lkb););
295
David Teigland3ae1acf2007-05-18 08:59:31 -0500296 /* if the operation was a cancel, then return -DLM_ECANCEL, if a
297 timeout caused the cancel then return -ETIMEDOUT */
298 if (rv == -DLM_ECANCEL && (lkb->lkb_flags & DLM_IFL_TIMEOUT_CANCEL)) {
299 lkb->lkb_flags &= ~DLM_IFL_TIMEOUT_CANCEL;
300 rv = -ETIMEDOUT;
301 }
302
David Teigland8b4021f2007-05-29 08:46:00 -0500303 if (rv == -DLM_ECANCEL && (lkb->lkb_flags & DLM_IFL_DEADLOCK_CANCEL)) {
304 lkb->lkb_flags &= ~DLM_IFL_DEADLOCK_CANCEL;
305 rv = -EDEADLK;
306 }
307
David Teigland8304d6f2011-02-21 14:58:21 -0600308 dlm_add_ast(lkb, DLM_CB_CAST, lkb->lkb_grmode, rv, lkb->lkb_sbflags);
David Teiglande7fd4172006-01-18 09:30:29 +0000309}
310
David Teiglandef0c2bb2007-03-28 09:56:46 -0500311static inline void queue_cast_overlap(struct dlm_rsb *r, struct dlm_lkb *lkb)
312{
313 queue_cast(r, lkb,
314 is_overlap_unlock(lkb) ? -DLM_EUNLOCK : -DLM_ECANCEL);
315}
316
David Teiglande7fd4172006-01-18 09:30:29 +0000317static void queue_bast(struct dlm_rsb *r, struct dlm_lkb *lkb, int rqmode)
318{
David Teiglandb6fa8792010-02-25 12:20:57 -0600319 if (is_master_copy(lkb)) {
David Teiglande7fd4172006-01-18 09:30:29 +0000320 send_bast(r, lkb, rqmode);
David Teiglandb6fa8792010-02-25 12:20:57 -0600321 } else {
David Teigland8304d6f2011-02-21 14:58:21 -0600322 dlm_add_ast(lkb, DLM_CB_BAST, rqmode, 0, 0);
David Teiglandb6fa8792010-02-25 12:20:57 -0600323 }
David Teiglande7fd4172006-01-18 09:30:29 +0000324}
325
326/*
327 * Basic operations on rsb's and lkb's
328 */
329
330static struct dlm_rsb *create_rsb(struct dlm_ls *ls, char *name, int len)
331{
332 struct dlm_rsb *r;
333
David Teigland52bda2b2007-11-07 09:06:49 -0600334 r = dlm_allocate_rsb(ls, len);
David Teiglande7fd4172006-01-18 09:30:29 +0000335 if (!r)
336 return NULL;
337
338 r->res_ls = ls;
339 r->res_length = len;
340 memcpy(r->res_name, name, len);
David Teigland90135922006-01-20 08:47:07 +0000341 mutex_init(&r->res_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +0000342
343 INIT_LIST_HEAD(&r->res_lookup);
344 INIT_LIST_HEAD(&r->res_grantqueue);
345 INIT_LIST_HEAD(&r->res_convertqueue);
346 INIT_LIST_HEAD(&r->res_waitqueue);
347 INIT_LIST_HEAD(&r->res_root_list);
348 INIT_LIST_HEAD(&r->res_recover_list);
349
350 return r;
351}
352
353static int search_rsb_list(struct list_head *head, char *name, int len,
354 unsigned int flags, struct dlm_rsb **r_ret)
355{
356 struct dlm_rsb *r;
357 int error = 0;
358
359 list_for_each_entry(r, head, res_hashchain) {
360 if (len == r->res_length && !memcmp(name, r->res_name, len))
361 goto found;
362 }
Benny Halevy18c60c02008-06-30 19:59:14 +0300363 *r_ret = NULL;
David Teigland597d0ca2006-07-12 16:44:04 -0500364 return -EBADR;
David Teiglande7fd4172006-01-18 09:30:29 +0000365
366 found:
367 if (r->res_nodeid && (flags & R_MASTER))
368 error = -ENOTBLK;
369 *r_ret = r;
370 return error;
371}
372
373static int _search_rsb(struct dlm_ls *ls, char *name, int len, int b,
374 unsigned int flags, struct dlm_rsb **r_ret)
375{
376 struct dlm_rsb *r;
377 int error;
378
379 error = search_rsb_list(&ls->ls_rsbtbl[b].list, name, len, flags, &r);
380 if (!error) {
381 kref_get(&r->res_ref);
382 goto out;
383 }
384 error = search_rsb_list(&ls->ls_rsbtbl[b].toss, name, len, flags, &r);
385 if (error)
386 goto out;
387
388 list_move(&r->res_hashchain, &ls->ls_rsbtbl[b].list);
389
390 if (dlm_no_directory(ls))
391 goto out;
392
393 if (r->res_nodeid == -1) {
394 rsb_clear_flag(r, RSB_MASTER_UNCERTAIN);
395 r->res_first_lkid = 0;
396 } else if (r->res_nodeid > 0) {
397 rsb_set_flag(r, RSB_MASTER_UNCERTAIN);
398 r->res_first_lkid = 0;
399 } else {
400 DLM_ASSERT(r->res_nodeid == 0, dlm_print_rsb(r););
401 DLM_ASSERT(!rsb_flag(r, RSB_MASTER_UNCERTAIN),);
402 }
403 out:
404 *r_ret = r;
405 return error;
406}
407
408static int search_rsb(struct dlm_ls *ls, char *name, int len, int b,
409 unsigned int flags, struct dlm_rsb **r_ret)
410{
411 int error;
David Teiglandc7be7612009-01-07 16:50:41 -0600412 spin_lock(&ls->ls_rsbtbl[b].lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000413 error = _search_rsb(ls, name, len, b, flags, r_ret);
David Teiglandc7be7612009-01-07 16:50:41 -0600414 spin_unlock(&ls->ls_rsbtbl[b].lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000415 return error;
416}
417
418/*
419 * Find rsb in rsbtbl and potentially create/add one
420 *
421 * Delaying the release of rsb's has a similar benefit to applications keeping
422 * NL locks on an rsb, but without the guarantee that the cached master value
423 * will still be valid when the rsb is reused. Apps aren't always smart enough
424 * to keep NL locks on an rsb that they may lock again shortly; this can lead
425 * to excessive master lookups and removals if we don't delay the release.
426 *
427 * Searching for an rsb means looking through both the normal list and toss
428 * list. When found on the toss list the rsb is moved to the normal list with
429 * ref count of 1; when found on normal list the ref count is incremented.
430 */
431
432static int find_rsb(struct dlm_ls *ls, char *name, int namelen,
433 unsigned int flags, struct dlm_rsb **r_ret)
434{
Steven Whitehousea566a6b2009-06-15 08:26:48 +0100435 struct dlm_rsb *r = NULL, *tmp;
David Teiglande7fd4172006-01-18 09:30:29 +0000436 uint32_t hash, bucket;
Al Viroef58bcc2008-01-25 23:22:26 -0500437 int error = -EINVAL;
438
439 if (namelen > DLM_RESNAME_MAXLEN)
440 goto out;
David Teiglande7fd4172006-01-18 09:30:29 +0000441
442 if (dlm_no_directory(ls))
443 flags |= R_CREATE;
444
Al Viroef58bcc2008-01-25 23:22:26 -0500445 error = 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000446 hash = jhash(name, namelen, 0);
447 bucket = hash & (ls->ls_rsbtbl_size - 1);
448
449 error = search_rsb(ls, name, namelen, bucket, flags, &r);
450 if (!error)
451 goto out;
452
David Teigland597d0ca2006-07-12 16:44:04 -0500453 if (error == -EBADR && !(flags & R_CREATE))
David Teiglande7fd4172006-01-18 09:30:29 +0000454 goto out;
455
456 /* the rsb was found but wasn't a master copy */
457 if (error == -ENOTBLK)
458 goto out;
459
460 error = -ENOMEM;
461 r = create_rsb(ls, name, namelen);
462 if (!r)
463 goto out;
464
465 r->res_hash = hash;
466 r->res_bucket = bucket;
467 r->res_nodeid = -1;
468 kref_init(&r->res_ref);
469
470 /* With no directory, the master can be set immediately */
471 if (dlm_no_directory(ls)) {
472 int nodeid = dlm_dir_nodeid(r);
473 if (nodeid == dlm_our_nodeid())
474 nodeid = 0;
475 r->res_nodeid = nodeid;
476 }
477
David Teiglandc7be7612009-01-07 16:50:41 -0600478 spin_lock(&ls->ls_rsbtbl[bucket].lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000479 error = _search_rsb(ls, name, namelen, bucket, 0, &tmp);
480 if (!error) {
David Teiglandc7be7612009-01-07 16:50:41 -0600481 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teigland52bda2b2007-11-07 09:06:49 -0600482 dlm_free_rsb(r);
David Teiglande7fd4172006-01-18 09:30:29 +0000483 r = tmp;
484 goto out;
485 }
486 list_add(&r->res_hashchain, &ls->ls_rsbtbl[bucket].list);
David Teiglandc7be7612009-01-07 16:50:41 -0600487 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000488 error = 0;
489 out:
490 *r_ret = r;
491 return error;
492}
493
David Teiglande7fd4172006-01-18 09:30:29 +0000494/* This is only called to add a reference when the code already holds
495 a valid reference to the rsb, so there's no need for locking. */
496
497static inline void hold_rsb(struct dlm_rsb *r)
498{
499 kref_get(&r->res_ref);
500}
501
502void dlm_hold_rsb(struct dlm_rsb *r)
503{
504 hold_rsb(r);
505}
506
507static void toss_rsb(struct kref *kref)
508{
509 struct dlm_rsb *r = container_of(kref, struct dlm_rsb, res_ref);
510 struct dlm_ls *ls = r->res_ls;
511
512 DLM_ASSERT(list_empty(&r->res_root_list), dlm_print_rsb(r););
513 kref_init(&r->res_ref);
514 list_move(&r->res_hashchain, &ls->ls_rsbtbl[r->res_bucket].toss);
515 r->res_toss_time = jiffies;
516 if (r->res_lvbptr) {
David Teigland52bda2b2007-11-07 09:06:49 -0600517 dlm_free_lvb(r->res_lvbptr);
David Teiglande7fd4172006-01-18 09:30:29 +0000518 r->res_lvbptr = NULL;
519 }
520}
521
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300522/* When all references to the rsb are gone it's transferred to
David Teiglande7fd4172006-01-18 09:30:29 +0000523 the tossed list for later disposal. */
524
525static void put_rsb(struct dlm_rsb *r)
526{
527 struct dlm_ls *ls = r->res_ls;
528 uint32_t bucket = r->res_bucket;
529
David Teiglandc7be7612009-01-07 16:50:41 -0600530 spin_lock(&ls->ls_rsbtbl[bucket].lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000531 kref_put(&r->res_ref, toss_rsb);
David Teiglandc7be7612009-01-07 16:50:41 -0600532 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000533}
534
535void dlm_put_rsb(struct dlm_rsb *r)
536{
537 put_rsb(r);
538}
539
540/* See comment for unhold_lkb */
541
542static void unhold_rsb(struct dlm_rsb *r)
543{
544 int rv;
545 rv = kref_put(&r->res_ref, toss_rsb);
David Teiglanda345da32006-08-18 11:54:25 -0500546 DLM_ASSERT(!rv, dlm_dump_rsb(r););
David Teiglande7fd4172006-01-18 09:30:29 +0000547}
548
549static void kill_rsb(struct kref *kref)
550{
551 struct dlm_rsb *r = container_of(kref, struct dlm_rsb, res_ref);
552
553 /* All work is done after the return from kref_put() so we
554 can release the write_lock before the remove and free. */
555
David Teiglanda345da32006-08-18 11:54:25 -0500556 DLM_ASSERT(list_empty(&r->res_lookup), dlm_dump_rsb(r););
557 DLM_ASSERT(list_empty(&r->res_grantqueue), dlm_dump_rsb(r););
558 DLM_ASSERT(list_empty(&r->res_convertqueue), dlm_dump_rsb(r););
559 DLM_ASSERT(list_empty(&r->res_waitqueue), dlm_dump_rsb(r););
560 DLM_ASSERT(list_empty(&r->res_root_list), dlm_dump_rsb(r););
561 DLM_ASSERT(list_empty(&r->res_recover_list), dlm_dump_rsb(r););
David Teiglande7fd4172006-01-18 09:30:29 +0000562}
563
564/* Attaching/detaching lkb's from rsb's is for rsb reference counting.
565 The rsb must exist as long as any lkb's for it do. */
566
567static void attach_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb)
568{
569 hold_rsb(r);
570 lkb->lkb_resource = r;
571}
572
573static void detach_lkb(struct dlm_lkb *lkb)
574{
575 if (lkb->lkb_resource) {
576 put_rsb(lkb->lkb_resource);
577 lkb->lkb_resource = NULL;
578 }
579}
580
581static int create_lkb(struct dlm_ls *ls, struct dlm_lkb **lkb_ret)
582{
583 struct dlm_lkb *lkb, *tmp;
584 uint32_t lkid = 0;
585 uint16_t bucket;
586
David Teigland52bda2b2007-11-07 09:06:49 -0600587 lkb = dlm_allocate_lkb(ls);
David Teiglande7fd4172006-01-18 09:30:29 +0000588 if (!lkb)
589 return -ENOMEM;
590
591 lkb->lkb_nodeid = -1;
592 lkb->lkb_grmode = DLM_LOCK_IV;
593 kref_init(&lkb->lkb_ref);
David Teigland34e22be2006-07-18 11:24:04 -0500594 INIT_LIST_HEAD(&lkb->lkb_ownqueue);
David Teiglandef0c2bb2007-03-28 09:56:46 -0500595 INIT_LIST_HEAD(&lkb->lkb_rsb_lookup);
David Teigland3ae1acf2007-05-18 08:59:31 -0500596 INIT_LIST_HEAD(&lkb->lkb_time_list);
David Teigland8304d6f2011-02-21 14:58:21 -0600597 INIT_LIST_HEAD(&lkb->lkb_astqueue);
David Teiglande7fd4172006-01-18 09:30:29 +0000598
599 get_random_bytes(&bucket, sizeof(bucket));
600 bucket &= (ls->ls_lkbtbl_size - 1);
601
602 write_lock(&ls->ls_lkbtbl[bucket].lock);
603
604 /* counter can roll over so we must verify lkid is not in use */
605
606 while (lkid == 0) {
David Teiglandce03f122007-04-02 12:12:55 -0500607 lkid = (bucket << 16) | ls->ls_lkbtbl[bucket].counter++;
David Teiglande7fd4172006-01-18 09:30:29 +0000608
609 list_for_each_entry(tmp, &ls->ls_lkbtbl[bucket].list,
610 lkb_idtbl_list) {
611 if (tmp->lkb_id != lkid)
612 continue;
613 lkid = 0;
614 break;
615 }
616 }
617
618 lkb->lkb_id = lkid;
619 list_add(&lkb->lkb_idtbl_list, &ls->ls_lkbtbl[bucket].list);
620 write_unlock(&ls->ls_lkbtbl[bucket].lock);
621
622 *lkb_ret = lkb;
623 return 0;
624}
625
626static struct dlm_lkb *__find_lkb(struct dlm_ls *ls, uint32_t lkid)
627{
David Teiglande7fd4172006-01-18 09:30:29 +0000628 struct dlm_lkb *lkb;
David Teiglandce03f122007-04-02 12:12:55 -0500629 uint16_t bucket = (lkid >> 16);
David Teiglande7fd4172006-01-18 09:30:29 +0000630
631 list_for_each_entry(lkb, &ls->ls_lkbtbl[bucket].list, lkb_idtbl_list) {
632 if (lkb->lkb_id == lkid)
633 return lkb;
634 }
635 return NULL;
636}
637
638static int find_lkb(struct dlm_ls *ls, uint32_t lkid, struct dlm_lkb **lkb_ret)
639{
640 struct dlm_lkb *lkb;
David Teiglandce03f122007-04-02 12:12:55 -0500641 uint16_t bucket = (lkid >> 16);
David Teiglande7fd4172006-01-18 09:30:29 +0000642
643 if (bucket >= ls->ls_lkbtbl_size)
644 return -EBADSLT;
645
646 read_lock(&ls->ls_lkbtbl[bucket].lock);
647 lkb = __find_lkb(ls, lkid);
648 if (lkb)
649 kref_get(&lkb->lkb_ref);
650 read_unlock(&ls->ls_lkbtbl[bucket].lock);
651
652 *lkb_ret = lkb;
653 return lkb ? 0 : -ENOENT;
654}
655
656static void kill_lkb(struct kref *kref)
657{
658 struct dlm_lkb *lkb = container_of(kref, struct dlm_lkb, lkb_ref);
659
660 /* All work is done after the return from kref_put() so we
661 can release the write_lock before the detach_lkb */
662
663 DLM_ASSERT(!lkb->lkb_status, dlm_print_lkb(lkb););
664}
665
David Teiglandb3f58d82006-02-28 11:16:37 -0500666/* __put_lkb() is used when an lkb may not have an rsb attached to
667 it so we need to provide the lockspace explicitly */
668
669static int __put_lkb(struct dlm_ls *ls, struct dlm_lkb *lkb)
David Teiglande7fd4172006-01-18 09:30:29 +0000670{
David Teiglandce03f122007-04-02 12:12:55 -0500671 uint16_t bucket = (lkb->lkb_id >> 16);
David Teiglande7fd4172006-01-18 09:30:29 +0000672
673 write_lock(&ls->ls_lkbtbl[bucket].lock);
674 if (kref_put(&lkb->lkb_ref, kill_lkb)) {
675 list_del(&lkb->lkb_idtbl_list);
676 write_unlock(&ls->ls_lkbtbl[bucket].lock);
677
678 detach_lkb(lkb);
679
680 /* for local/process lkbs, lvbptr points to caller's lksb */
681 if (lkb->lkb_lvbptr && is_master_copy(lkb))
David Teigland52bda2b2007-11-07 09:06:49 -0600682 dlm_free_lvb(lkb->lkb_lvbptr);
683 dlm_free_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +0000684 return 1;
685 } else {
686 write_unlock(&ls->ls_lkbtbl[bucket].lock);
687 return 0;
688 }
689}
690
691int dlm_put_lkb(struct dlm_lkb *lkb)
692{
David Teiglandb3f58d82006-02-28 11:16:37 -0500693 struct dlm_ls *ls;
694
695 DLM_ASSERT(lkb->lkb_resource, dlm_print_lkb(lkb););
696 DLM_ASSERT(lkb->lkb_resource->res_ls, dlm_print_lkb(lkb););
697
698 ls = lkb->lkb_resource->res_ls;
699 return __put_lkb(ls, lkb);
David Teiglande7fd4172006-01-18 09:30:29 +0000700}
701
702/* This is only called to add a reference when the code already holds
703 a valid reference to the lkb, so there's no need for locking. */
704
705static inline void hold_lkb(struct dlm_lkb *lkb)
706{
707 kref_get(&lkb->lkb_ref);
708}
709
710/* This is called when we need to remove a reference and are certain
711 it's not the last ref. e.g. del_lkb is always called between a
712 find_lkb/put_lkb and is always the inverse of a previous add_lkb.
713 put_lkb would work fine, but would involve unnecessary locking */
714
715static inline void unhold_lkb(struct dlm_lkb *lkb)
716{
717 int rv;
718 rv = kref_put(&lkb->lkb_ref, kill_lkb);
719 DLM_ASSERT(!rv, dlm_print_lkb(lkb););
720}
721
722static void lkb_add_ordered(struct list_head *new, struct list_head *head,
723 int mode)
724{
725 struct dlm_lkb *lkb = NULL;
726
727 list_for_each_entry(lkb, head, lkb_statequeue)
728 if (lkb->lkb_rqmode < mode)
729 break;
730
Dan Carpenter99fb19d2010-03-22 15:03:54 +0300731 __list_add(new, lkb->lkb_statequeue.prev, &lkb->lkb_statequeue);
David Teiglande7fd4172006-01-18 09:30:29 +0000732}
733
734/* add/remove lkb to rsb's grant/convert/wait queue */
735
736static void add_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb, int status)
737{
738 kref_get(&lkb->lkb_ref);
739
740 DLM_ASSERT(!lkb->lkb_status, dlm_print_lkb(lkb););
741
David Teiglandeeda4182008-12-09 14:12:21 -0600742 lkb->lkb_timestamp = ktime_get();
743
David Teiglande7fd4172006-01-18 09:30:29 +0000744 lkb->lkb_status = status;
745
746 switch (status) {
747 case DLM_LKSTS_WAITING:
748 if (lkb->lkb_exflags & DLM_LKF_HEADQUE)
749 list_add(&lkb->lkb_statequeue, &r->res_waitqueue);
750 else
751 list_add_tail(&lkb->lkb_statequeue, &r->res_waitqueue);
752 break;
753 case DLM_LKSTS_GRANTED:
754 /* convention says granted locks kept in order of grmode */
755 lkb_add_ordered(&lkb->lkb_statequeue, &r->res_grantqueue,
756 lkb->lkb_grmode);
757 break;
758 case DLM_LKSTS_CONVERT:
759 if (lkb->lkb_exflags & DLM_LKF_HEADQUE)
760 list_add(&lkb->lkb_statequeue, &r->res_convertqueue);
761 else
762 list_add_tail(&lkb->lkb_statequeue,
763 &r->res_convertqueue);
764 break;
765 default:
766 DLM_ASSERT(0, dlm_print_lkb(lkb); printk("sts=%d\n", status););
767 }
768}
769
770static void del_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb)
771{
772 lkb->lkb_status = 0;
773 list_del(&lkb->lkb_statequeue);
774 unhold_lkb(lkb);
775}
776
777static void move_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb, int sts)
778{
779 hold_lkb(lkb);
780 del_lkb(r, lkb);
781 add_lkb(r, lkb, sts);
782 unhold_lkb(lkb);
783}
784
David Teiglandef0c2bb2007-03-28 09:56:46 -0500785static int msg_reply_type(int mstype)
786{
787 switch (mstype) {
788 case DLM_MSG_REQUEST:
789 return DLM_MSG_REQUEST_REPLY;
790 case DLM_MSG_CONVERT:
791 return DLM_MSG_CONVERT_REPLY;
792 case DLM_MSG_UNLOCK:
793 return DLM_MSG_UNLOCK_REPLY;
794 case DLM_MSG_CANCEL:
795 return DLM_MSG_CANCEL_REPLY;
796 case DLM_MSG_LOOKUP:
797 return DLM_MSG_LOOKUP_REPLY;
798 }
799 return -1;
800}
801
David Teiglandc6ff6692011-03-28 14:17:26 -0500802static int nodeid_warned(int nodeid, int num_nodes, int *warned)
803{
804 int i;
805
806 for (i = 0; i < num_nodes; i++) {
807 if (!warned[i]) {
808 warned[i] = nodeid;
809 return 0;
810 }
811 if (warned[i] == nodeid)
812 return 1;
813 }
814 return 0;
815}
816
817void dlm_scan_waiters(struct dlm_ls *ls)
818{
819 struct dlm_lkb *lkb;
820 ktime_t zero = ktime_set(0, 0);
821 s64 us;
822 s64 debug_maxus = 0;
823 u32 debug_scanned = 0;
824 u32 debug_expired = 0;
825 int num_nodes = 0;
826 int *warned = NULL;
827
828 if (!dlm_config.ci_waitwarn_us)
829 return;
830
831 mutex_lock(&ls->ls_waiters_mutex);
832
833 list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
834 if (ktime_equal(lkb->lkb_wait_time, zero))
835 continue;
836
837 debug_scanned++;
838
839 us = ktime_to_us(ktime_sub(ktime_get(), lkb->lkb_wait_time));
840
841 if (us < dlm_config.ci_waitwarn_us)
842 continue;
843
844 lkb->lkb_wait_time = zero;
845
846 debug_expired++;
847 if (us > debug_maxus)
848 debug_maxus = us;
849
850 if (!num_nodes) {
851 num_nodes = ls->ls_num_nodes;
Jesper Juhl5d708282011-07-10 22:54:31 +0200852 warned = kzalloc(num_nodes * sizeof(int), GFP_KERNEL);
David Teiglandc6ff6692011-03-28 14:17:26 -0500853 }
854 if (!warned)
855 continue;
856 if (nodeid_warned(lkb->lkb_wait_nodeid, num_nodes, warned))
857 continue;
858
859 log_error(ls, "waitwarn %x %lld %d us check connection to "
860 "node %d", lkb->lkb_id, (long long)us,
861 dlm_config.ci_waitwarn_us, lkb->lkb_wait_nodeid);
862 }
863 mutex_unlock(&ls->ls_waiters_mutex);
Jesper Juhl5d708282011-07-10 22:54:31 +0200864 kfree(warned);
David Teiglandc6ff6692011-03-28 14:17:26 -0500865
866 if (debug_expired)
867 log_debug(ls, "scan_waiters %u warn %u over %d us max %lld us",
868 debug_scanned, debug_expired,
869 dlm_config.ci_waitwarn_us, (long long)debug_maxus);
870}
871
David Teiglande7fd4172006-01-18 09:30:29 +0000872/* add/remove lkb from global waiters list of lkb's waiting for
873 a reply from a remote node */
874
David Teiglandc6ff6692011-03-28 14:17:26 -0500875static int add_to_waiters(struct dlm_lkb *lkb, int mstype, int to_nodeid)
David Teiglande7fd4172006-01-18 09:30:29 +0000876{
877 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
David Teiglandef0c2bb2007-03-28 09:56:46 -0500878 int error = 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000879
David Teigland90135922006-01-20 08:47:07 +0000880 mutex_lock(&ls->ls_waiters_mutex);
David Teiglandef0c2bb2007-03-28 09:56:46 -0500881
882 if (is_overlap_unlock(lkb) ||
883 (is_overlap_cancel(lkb) && (mstype == DLM_MSG_CANCEL))) {
884 error = -EINVAL;
David Teiglande7fd4172006-01-18 09:30:29 +0000885 goto out;
886 }
David Teiglandef0c2bb2007-03-28 09:56:46 -0500887
888 if (lkb->lkb_wait_type || is_overlap_cancel(lkb)) {
889 switch (mstype) {
890 case DLM_MSG_UNLOCK:
891 lkb->lkb_flags |= DLM_IFL_OVERLAP_UNLOCK;
892 break;
893 case DLM_MSG_CANCEL:
894 lkb->lkb_flags |= DLM_IFL_OVERLAP_CANCEL;
895 break;
896 default:
897 error = -EBUSY;
898 goto out;
899 }
900 lkb->lkb_wait_count++;
901 hold_lkb(lkb);
902
David Teigland43279e52009-01-28 14:37:54 -0600903 log_debug(ls, "addwait %x cur %d overlap %d count %d f %x",
David Teiglandef0c2bb2007-03-28 09:56:46 -0500904 lkb->lkb_id, lkb->lkb_wait_type, mstype,
905 lkb->lkb_wait_count, lkb->lkb_flags);
906 goto out;
907 }
908
909 DLM_ASSERT(!lkb->lkb_wait_count,
910 dlm_print_lkb(lkb);
911 printk("wait_count %d\n", lkb->lkb_wait_count););
912
913 lkb->lkb_wait_count++;
David Teiglande7fd4172006-01-18 09:30:29 +0000914 lkb->lkb_wait_type = mstype;
David Teiglandc6ff6692011-03-28 14:17:26 -0500915 lkb->lkb_wait_time = ktime_get();
916 lkb->lkb_wait_nodeid = to_nodeid; /* for debugging */
David Teiglandef0c2bb2007-03-28 09:56:46 -0500917 hold_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +0000918 list_add(&lkb->lkb_wait_reply, &ls->ls_waiters);
919 out:
David Teiglandef0c2bb2007-03-28 09:56:46 -0500920 if (error)
David Teigland43279e52009-01-28 14:37:54 -0600921 log_error(ls, "addwait error %x %d flags %x %d %d %s",
David Teiglandef0c2bb2007-03-28 09:56:46 -0500922 lkb->lkb_id, error, lkb->lkb_flags, mstype,
923 lkb->lkb_wait_type, lkb->lkb_resource->res_name);
David Teigland90135922006-01-20 08:47:07 +0000924 mutex_unlock(&ls->ls_waiters_mutex);
David Teiglandef0c2bb2007-03-28 09:56:46 -0500925 return error;
David Teiglande7fd4172006-01-18 09:30:29 +0000926}
927
David Teiglandb790c3b2007-01-24 10:21:33 -0600928/* We clear the RESEND flag because we might be taking an lkb off the waiters
929 list as part of process_requestqueue (e.g. a lookup that has an optimized
930 request reply on the requestqueue) between dlm_recover_waiters_pre() which
931 set RESEND and dlm_recover_waiters_post() */
932
David Teigland43279e52009-01-28 14:37:54 -0600933static int _remove_from_waiters(struct dlm_lkb *lkb, int mstype,
934 struct dlm_message *ms)
David Teiglande7fd4172006-01-18 09:30:29 +0000935{
David Teiglandef0c2bb2007-03-28 09:56:46 -0500936 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
937 int overlap_done = 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000938
David Teiglandef0c2bb2007-03-28 09:56:46 -0500939 if (is_overlap_unlock(lkb) && (mstype == DLM_MSG_UNLOCK_REPLY)) {
David Teigland43279e52009-01-28 14:37:54 -0600940 log_debug(ls, "remwait %x unlock_reply overlap", lkb->lkb_id);
David Teiglandef0c2bb2007-03-28 09:56:46 -0500941 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
942 overlap_done = 1;
943 goto out_del;
David Teiglande7fd4172006-01-18 09:30:29 +0000944 }
David Teiglandef0c2bb2007-03-28 09:56:46 -0500945
946 if (is_overlap_cancel(lkb) && (mstype == DLM_MSG_CANCEL_REPLY)) {
David Teigland43279e52009-01-28 14:37:54 -0600947 log_debug(ls, "remwait %x cancel_reply overlap", lkb->lkb_id);
David Teiglandef0c2bb2007-03-28 09:56:46 -0500948 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
949 overlap_done = 1;
950 goto out_del;
951 }
952
David Teigland43279e52009-01-28 14:37:54 -0600953 /* Cancel state was preemptively cleared by a successful convert,
954 see next comment, nothing to do. */
955
956 if ((mstype == DLM_MSG_CANCEL_REPLY) &&
957 (lkb->lkb_wait_type != DLM_MSG_CANCEL)) {
958 log_debug(ls, "remwait %x cancel_reply wait_type %d",
959 lkb->lkb_id, lkb->lkb_wait_type);
960 return -1;
961 }
962
963 /* Remove for the convert reply, and premptively remove for the
964 cancel reply. A convert has been granted while there's still
965 an outstanding cancel on it (the cancel is moot and the result
966 in the cancel reply should be 0). We preempt the cancel reply
967 because the app gets the convert result and then can follow up
968 with another op, like convert. This subsequent op would see the
969 lingering state of the cancel and fail with -EBUSY. */
970
971 if ((mstype == DLM_MSG_CONVERT_REPLY) &&
972 (lkb->lkb_wait_type == DLM_MSG_CONVERT) &&
973 is_overlap_cancel(lkb) && ms && !ms->m_result) {
974 log_debug(ls, "remwait %x convert_reply zap overlap_cancel",
975 lkb->lkb_id);
976 lkb->lkb_wait_type = 0;
977 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
978 lkb->lkb_wait_count--;
979 goto out_del;
980 }
981
David Teiglandef0c2bb2007-03-28 09:56:46 -0500982 /* N.B. type of reply may not always correspond to type of original
983 msg due to lookup->request optimization, verify others? */
984
985 if (lkb->lkb_wait_type) {
986 lkb->lkb_wait_type = 0;
987 goto out_del;
988 }
989
David Teigland43279e52009-01-28 14:37:54 -0600990 log_error(ls, "remwait error %x reply %d flags %x no wait_type",
991 lkb->lkb_id, mstype, lkb->lkb_flags);
David Teiglandef0c2bb2007-03-28 09:56:46 -0500992 return -1;
993
994 out_del:
995 /* the force-unlock/cancel has completed and we haven't recvd a reply
996 to the op that was in progress prior to the unlock/cancel; we
997 give up on any reply to the earlier op. FIXME: not sure when/how
998 this would happen */
999
1000 if (overlap_done && lkb->lkb_wait_type) {
David Teigland43279e52009-01-28 14:37:54 -06001001 log_error(ls, "remwait error %x reply %d wait_type %d overlap",
David Teiglandef0c2bb2007-03-28 09:56:46 -05001002 lkb->lkb_id, mstype, lkb->lkb_wait_type);
1003 lkb->lkb_wait_count--;
1004 lkb->lkb_wait_type = 0;
1005 }
1006
1007 DLM_ASSERT(lkb->lkb_wait_count, dlm_print_lkb(lkb););
1008
David Teiglandb790c3b2007-01-24 10:21:33 -06001009 lkb->lkb_flags &= ~DLM_IFL_RESEND;
David Teiglandef0c2bb2007-03-28 09:56:46 -05001010 lkb->lkb_wait_count--;
1011 if (!lkb->lkb_wait_count)
1012 list_del_init(&lkb->lkb_wait_reply);
David Teiglande7fd4172006-01-18 09:30:29 +00001013 unhold_lkb(lkb);
David Teiglandef0c2bb2007-03-28 09:56:46 -05001014 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001015}
1016
David Teiglandef0c2bb2007-03-28 09:56:46 -05001017static int remove_from_waiters(struct dlm_lkb *lkb, int mstype)
David Teiglande7fd4172006-01-18 09:30:29 +00001018{
1019 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1020 int error;
1021
David Teigland90135922006-01-20 08:47:07 +00001022 mutex_lock(&ls->ls_waiters_mutex);
David Teigland43279e52009-01-28 14:37:54 -06001023 error = _remove_from_waiters(lkb, mstype, NULL);
David Teigland90135922006-01-20 08:47:07 +00001024 mutex_unlock(&ls->ls_waiters_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +00001025 return error;
1026}
1027
David Teiglandef0c2bb2007-03-28 09:56:46 -05001028/* Handles situations where we might be processing a "fake" or "stub" reply in
1029 which we can't try to take waiters_mutex again. */
1030
1031static int remove_from_waiters_ms(struct dlm_lkb *lkb, struct dlm_message *ms)
1032{
1033 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1034 int error;
1035
David Teigland2a7ce0e2011-04-04 15:19:59 -05001036 if (ms->m_flags != DLM_IFL_STUB_MS)
David Teiglandef0c2bb2007-03-28 09:56:46 -05001037 mutex_lock(&ls->ls_waiters_mutex);
David Teigland43279e52009-01-28 14:37:54 -06001038 error = _remove_from_waiters(lkb, ms->m_type, ms);
David Teigland2a7ce0e2011-04-04 15:19:59 -05001039 if (ms->m_flags != DLM_IFL_STUB_MS)
David Teiglandef0c2bb2007-03-28 09:56:46 -05001040 mutex_unlock(&ls->ls_waiters_mutex);
1041 return error;
1042}
1043
David Teiglande7fd4172006-01-18 09:30:29 +00001044static void dir_remove(struct dlm_rsb *r)
1045{
1046 int to_nodeid;
1047
1048 if (dlm_no_directory(r->res_ls))
1049 return;
1050
1051 to_nodeid = dlm_dir_nodeid(r);
1052 if (to_nodeid != dlm_our_nodeid())
1053 send_remove(r);
1054 else
1055 dlm_dir_remove_entry(r->res_ls, to_nodeid,
1056 r->res_name, r->res_length);
1057}
1058
1059/* FIXME: shouldn't this be able to exit as soon as one non-due rsb is
1060 found since they are in order of newest to oldest? */
1061
1062static int shrink_bucket(struct dlm_ls *ls, int b)
1063{
1064 struct dlm_rsb *r;
1065 int count = 0, found;
1066
1067 for (;;) {
David Teigland90135922006-01-20 08:47:07 +00001068 found = 0;
David Teiglandc7be7612009-01-07 16:50:41 -06001069 spin_lock(&ls->ls_rsbtbl[b].lock);
David Teiglande7fd4172006-01-18 09:30:29 +00001070 list_for_each_entry_reverse(r, &ls->ls_rsbtbl[b].toss,
1071 res_hashchain) {
1072 if (!time_after_eq(jiffies, r->res_toss_time +
David Teigland68c817a2007-01-09 09:41:48 -06001073 dlm_config.ci_toss_secs * HZ))
David Teiglande7fd4172006-01-18 09:30:29 +00001074 continue;
David Teigland90135922006-01-20 08:47:07 +00001075 found = 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001076 break;
1077 }
1078
1079 if (!found) {
David Teiglandc7be7612009-01-07 16:50:41 -06001080 spin_unlock(&ls->ls_rsbtbl[b].lock);
David Teiglande7fd4172006-01-18 09:30:29 +00001081 break;
1082 }
1083
1084 if (kref_put(&r->res_ref, kill_rsb)) {
1085 list_del(&r->res_hashchain);
David Teiglandc7be7612009-01-07 16:50:41 -06001086 spin_unlock(&ls->ls_rsbtbl[b].lock);
David Teiglande7fd4172006-01-18 09:30:29 +00001087
1088 if (is_master(r))
1089 dir_remove(r);
David Teigland52bda2b2007-11-07 09:06:49 -06001090 dlm_free_rsb(r);
David Teiglande7fd4172006-01-18 09:30:29 +00001091 count++;
1092 } else {
David Teiglandc7be7612009-01-07 16:50:41 -06001093 spin_unlock(&ls->ls_rsbtbl[b].lock);
David Teiglande7fd4172006-01-18 09:30:29 +00001094 log_error(ls, "tossed rsb in use %s", r->res_name);
1095 }
1096 }
1097
1098 return count;
1099}
1100
1101void dlm_scan_rsbs(struct dlm_ls *ls)
1102{
1103 int i;
1104
David Teiglande7fd4172006-01-18 09:30:29 +00001105 for (i = 0; i < ls->ls_rsbtbl_size; i++) {
1106 shrink_bucket(ls, i);
David Teigland85e86ed2007-05-18 08:58:15 -05001107 if (dlm_locking_stopped(ls))
1108 break;
David Teiglande7fd4172006-01-18 09:30:29 +00001109 cond_resched();
1110 }
1111}
1112
David Teigland3ae1acf2007-05-18 08:59:31 -05001113static void add_timeout(struct dlm_lkb *lkb)
1114{
1115 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1116
David Teiglandeeda4182008-12-09 14:12:21 -06001117 if (is_master_copy(lkb))
David Teigland3ae1acf2007-05-18 08:59:31 -05001118 return;
David Teigland3ae1acf2007-05-18 08:59:31 -05001119
1120 if (test_bit(LSFL_TIMEWARN, &ls->ls_flags) &&
1121 !(lkb->lkb_exflags & DLM_LKF_NODLCKWT)) {
1122 lkb->lkb_flags |= DLM_IFL_WATCH_TIMEWARN;
1123 goto add_it;
1124 }
David Teigland84d8cd62007-05-29 08:44:23 -05001125 if (lkb->lkb_exflags & DLM_LKF_TIMEOUT)
1126 goto add_it;
David Teigland3ae1acf2007-05-18 08:59:31 -05001127 return;
1128
1129 add_it:
1130 DLM_ASSERT(list_empty(&lkb->lkb_time_list), dlm_print_lkb(lkb););
1131 mutex_lock(&ls->ls_timeout_mutex);
1132 hold_lkb(lkb);
David Teigland3ae1acf2007-05-18 08:59:31 -05001133 list_add_tail(&lkb->lkb_time_list, &ls->ls_timeout);
1134 mutex_unlock(&ls->ls_timeout_mutex);
1135}
1136
1137static void del_timeout(struct dlm_lkb *lkb)
1138{
1139 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
1140
1141 mutex_lock(&ls->ls_timeout_mutex);
1142 if (!list_empty(&lkb->lkb_time_list)) {
1143 list_del_init(&lkb->lkb_time_list);
1144 unhold_lkb(lkb);
1145 }
1146 mutex_unlock(&ls->ls_timeout_mutex);
1147}
1148
1149/* FIXME: is it safe to look at lkb_exflags, lkb_flags, lkb_timestamp, and
1150 lkb_lksb_timeout without lock_rsb? Note: we can't lock timeout_mutex
1151 and then lock rsb because of lock ordering in add_timeout. We may need
1152 to specify some special timeout-related bits in the lkb that are just to
1153 be accessed under the timeout_mutex. */
1154
1155void dlm_scan_timeout(struct dlm_ls *ls)
1156{
1157 struct dlm_rsb *r;
1158 struct dlm_lkb *lkb;
1159 int do_cancel, do_warn;
David Teiglandeeda4182008-12-09 14:12:21 -06001160 s64 wait_us;
David Teigland3ae1acf2007-05-18 08:59:31 -05001161
1162 for (;;) {
1163 if (dlm_locking_stopped(ls))
1164 break;
1165
1166 do_cancel = 0;
1167 do_warn = 0;
1168 mutex_lock(&ls->ls_timeout_mutex);
1169 list_for_each_entry(lkb, &ls->ls_timeout, lkb_time_list) {
1170
David Teiglandeeda4182008-12-09 14:12:21 -06001171 wait_us = ktime_to_us(ktime_sub(ktime_get(),
1172 lkb->lkb_timestamp));
1173
David Teigland3ae1acf2007-05-18 08:59:31 -05001174 if ((lkb->lkb_exflags & DLM_LKF_TIMEOUT) &&
David Teiglandeeda4182008-12-09 14:12:21 -06001175 wait_us >= (lkb->lkb_timeout_cs * 10000))
David Teigland3ae1acf2007-05-18 08:59:31 -05001176 do_cancel = 1;
1177
1178 if ((lkb->lkb_flags & DLM_IFL_WATCH_TIMEWARN) &&
David Teiglandeeda4182008-12-09 14:12:21 -06001179 wait_us >= dlm_config.ci_timewarn_cs * 10000)
David Teigland3ae1acf2007-05-18 08:59:31 -05001180 do_warn = 1;
1181
1182 if (!do_cancel && !do_warn)
1183 continue;
1184 hold_lkb(lkb);
1185 break;
1186 }
1187 mutex_unlock(&ls->ls_timeout_mutex);
1188
1189 if (!do_cancel && !do_warn)
1190 break;
1191
1192 r = lkb->lkb_resource;
1193 hold_rsb(r);
1194 lock_rsb(r);
1195
1196 if (do_warn) {
1197 /* clear flag so we only warn once */
1198 lkb->lkb_flags &= ~DLM_IFL_WATCH_TIMEWARN;
1199 if (!(lkb->lkb_exflags & DLM_LKF_TIMEOUT))
1200 del_timeout(lkb);
1201 dlm_timeout_warn(lkb);
1202 }
1203
1204 if (do_cancel) {
Steven Whitehouseb3cab7b2007-05-29 11:14:21 +01001205 log_debug(ls, "timeout cancel %x node %d %s",
David Teigland639aca42007-05-18 16:02:57 -05001206 lkb->lkb_id, lkb->lkb_nodeid, r->res_name);
David Teigland3ae1acf2007-05-18 08:59:31 -05001207 lkb->lkb_flags &= ~DLM_IFL_WATCH_TIMEWARN;
1208 lkb->lkb_flags |= DLM_IFL_TIMEOUT_CANCEL;
1209 del_timeout(lkb);
1210 _cancel_lock(r, lkb);
1211 }
1212
1213 unlock_rsb(r);
1214 unhold_rsb(r);
1215 dlm_put_lkb(lkb);
1216 }
1217}
1218
1219/* This is only called by dlm_recoverd, and we rely on dlm_ls_stop() stopping
1220 dlm_recoverd before checking/setting ls_recover_begin. */
1221
1222void dlm_adjust_timeouts(struct dlm_ls *ls)
1223{
1224 struct dlm_lkb *lkb;
David Teiglandeeda4182008-12-09 14:12:21 -06001225 u64 adj_us = jiffies_to_usecs(jiffies - ls->ls_recover_begin);
David Teigland3ae1acf2007-05-18 08:59:31 -05001226
1227 ls->ls_recover_begin = 0;
1228 mutex_lock(&ls->ls_timeout_mutex);
1229 list_for_each_entry(lkb, &ls->ls_timeout, lkb_time_list)
David Teiglandeeda4182008-12-09 14:12:21 -06001230 lkb->lkb_timestamp = ktime_add_us(lkb->lkb_timestamp, adj_us);
David Teigland3ae1acf2007-05-18 08:59:31 -05001231 mutex_unlock(&ls->ls_timeout_mutex);
David Teiglandc6ff6692011-03-28 14:17:26 -05001232
1233 if (!dlm_config.ci_waitwarn_us)
1234 return;
1235
1236 mutex_lock(&ls->ls_waiters_mutex);
1237 list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
1238 if (ktime_to_us(lkb->lkb_wait_time))
1239 lkb->lkb_wait_time = ktime_get();
1240 }
1241 mutex_unlock(&ls->ls_waiters_mutex);
David Teigland3ae1acf2007-05-18 08:59:31 -05001242}
1243
David Teiglande7fd4172006-01-18 09:30:29 +00001244/* lkb is master or local copy */
1245
1246static void set_lvb_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1247{
1248 int b, len = r->res_ls->ls_lvblen;
1249
1250 /* b=1 lvb returned to caller
1251 b=0 lvb written to rsb or invalidated
1252 b=-1 do nothing */
1253
1254 b = dlm_lvb_operations[lkb->lkb_grmode + 1][lkb->lkb_rqmode + 1];
1255
1256 if (b == 1) {
1257 if (!lkb->lkb_lvbptr)
1258 return;
1259
1260 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1261 return;
1262
1263 if (!r->res_lvbptr)
1264 return;
1265
1266 memcpy(lkb->lkb_lvbptr, r->res_lvbptr, len);
1267 lkb->lkb_lvbseq = r->res_lvbseq;
1268
1269 } else if (b == 0) {
1270 if (lkb->lkb_exflags & DLM_LKF_IVVALBLK) {
1271 rsb_set_flag(r, RSB_VALNOTVALID);
1272 return;
1273 }
1274
1275 if (!lkb->lkb_lvbptr)
1276 return;
1277
1278 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1279 return;
1280
1281 if (!r->res_lvbptr)
David Teigland52bda2b2007-11-07 09:06:49 -06001282 r->res_lvbptr = dlm_allocate_lvb(r->res_ls);
David Teiglande7fd4172006-01-18 09:30:29 +00001283
1284 if (!r->res_lvbptr)
1285 return;
1286
1287 memcpy(r->res_lvbptr, lkb->lkb_lvbptr, len);
1288 r->res_lvbseq++;
1289 lkb->lkb_lvbseq = r->res_lvbseq;
1290 rsb_clear_flag(r, RSB_VALNOTVALID);
1291 }
1292
1293 if (rsb_flag(r, RSB_VALNOTVALID))
1294 lkb->lkb_sbflags |= DLM_SBF_VALNOTVALID;
1295}
1296
1297static void set_lvb_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1298{
1299 if (lkb->lkb_grmode < DLM_LOCK_PW)
1300 return;
1301
1302 if (lkb->lkb_exflags & DLM_LKF_IVVALBLK) {
1303 rsb_set_flag(r, RSB_VALNOTVALID);
1304 return;
1305 }
1306
1307 if (!lkb->lkb_lvbptr)
1308 return;
1309
1310 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1311 return;
1312
1313 if (!r->res_lvbptr)
David Teigland52bda2b2007-11-07 09:06:49 -06001314 r->res_lvbptr = dlm_allocate_lvb(r->res_ls);
David Teiglande7fd4172006-01-18 09:30:29 +00001315
1316 if (!r->res_lvbptr)
1317 return;
1318
1319 memcpy(r->res_lvbptr, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
1320 r->res_lvbseq++;
1321 rsb_clear_flag(r, RSB_VALNOTVALID);
1322}
1323
1324/* lkb is process copy (pc) */
1325
1326static void set_lvb_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb,
1327 struct dlm_message *ms)
1328{
1329 int b;
1330
1331 if (!lkb->lkb_lvbptr)
1332 return;
1333
1334 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
1335 return;
1336
David Teigland597d0ca2006-07-12 16:44:04 -05001337 b = dlm_lvb_operations[lkb->lkb_grmode + 1][lkb->lkb_rqmode + 1];
David Teiglande7fd4172006-01-18 09:30:29 +00001338 if (b == 1) {
1339 int len = receive_extralen(ms);
Al Viroa9cc9152008-01-26 00:02:29 -05001340 if (len > DLM_RESNAME_MAXLEN)
1341 len = DLM_RESNAME_MAXLEN;
David Teiglande7fd4172006-01-18 09:30:29 +00001342 memcpy(lkb->lkb_lvbptr, ms->m_extra, len);
1343 lkb->lkb_lvbseq = ms->m_lvbseq;
1344 }
1345}
1346
1347/* Manipulate lkb's on rsb's convert/granted/waiting queues
1348 remove_lock -- used for unlock, removes lkb from granted
1349 revert_lock -- used for cancel, moves lkb from convert to granted
1350 grant_lock -- used for request and convert, adds lkb to granted or
1351 moves lkb from convert or waiting to granted
1352
1353 Each of these is used for master or local copy lkb's. There is
1354 also a _pc() variation used to make the corresponding change on
1355 a process copy (pc) lkb. */
1356
1357static void _remove_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1358{
1359 del_lkb(r, lkb);
1360 lkb->lkb_grmode = DLM_LOCK_IV;
1361 /* this unhold undoes the original ref from create_lkb()
1362 so this leads to the lkb being freed */
1363 unhold_lkb(lkb);
1364}
1365
1366static void remove_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1367{
1368 set_lvb_unlock(r, lkb);
1369 _remove_lock(r, lkb);
1370}
1371
1372static void remove_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb)
1373{
1374 _remove_lock(r, lkb);
1375}
1376
David Teiglandef0c2bb2007-03-28 09:56:46 -05001377/* returns: 0 did nothing
1378 1 moved lock to granted
1379 -1 removed lock */
1380
1381static int revert_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
David Teiglande7fd4172006-01-18 09:30:29 +00001382{
David Teiglandef0c2bb2007-03-28 09:56:46 -05001383 int rv = 0;
1384
David Teiglande7fd4172006-01-18 09:30:29 +00001385 lkb->lkb_rqmode = DLM_LOCK_IV;
1386
1387 switch (lkb->lkb_status) {
David Teigland597d0ca2006-07-12 16:44:04 -05001388 case DLM_LKSTS_GRANTED:
1389 break;
David Teiglande7fd4172006-01-18 09:30:29 +00001390 case DLM_LKSTS_CONVERT:
1391 move_lkb(r, lkb, DLM_LKSTS_GRANTED);
David Teiglandef0c2bb2007-03-28 09:56:46 -05001392 rv = 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001393 break;
1394 case DLM_LKSTS_WAITING:
1395 del_lkb(r, lkb);
1396 lkb->lkb_grmode = DLM_LOCK_IV;
1397 /* this unhold undoes the original ref from create_lkb()
1398 so this leads to the lkb being freed */
1399 unhold_lkb(lkb);
David Teiglandef0c2bb2007-03-28 09:56:46 -05001400 rv = -1;
David Teiglande7fd4172006-01-18 09:30:29 +00001401 break;
1402 default:
1403 log_print("invalid status for revert %d", lkb->lkb_status);
1404 }
David Teiglandef0c2bb2007-03-28 09:56:46 -05001405 return rv;
David Teiglande7fd4172006-01-18 09:30:29 +00001406}
1407
David Teiglandef0c2bb2007-03-28 09:56:46 -05001408static int revert_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb)
David Teiglande7fd4172006-01-18 09:30:29 +00001409{
David Teiglandef0c2bb2007-03-28 09:56:46 -05001410 return revert_lock(r, lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00001411}
1412
1413static void _grant_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1414{
1415 if (lkb->lkb_grmode != lkb->lkb_rqmode) {
1416 lkb->lkb_grmode = lkb->lkb_rqmode;
1417 if (lkb->lkb_status)
1418 move_lkb(r, lkb, DLM_LKSTS_GRANTED);
1419 else
1420 add_lkb(r, lkb, DLM_LKSTS_GRANTED);
1421 }
1422
1423 lkb->lkb_rqmode = DLM_LOCK_IV;
David Teiglande7fd4172006-01-18 09:30:29 +00001424}
1425
1426static void grant_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
1427{
1428 set_lvb_lock(r, lkb);
1429 _grant_lock(r, lkb);
1430 lkb->lkb_highbast = 0;
1431}
1432
1433static void grant_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb,
1434 struct dlm_message *ms)
1435{
1436 set_lvb_lock_pc(r, lkb, ms);
1437 _grant_lock(r, lkb);
1438}
1439
1440/* called by grant_pending_locks() which means an async grant message must
1441 be sent to the requesting node in addition to granting the lock if the
1442 lkb belongs to a remote node. */
1443
1444static void grant_lock_pending(struct dlm_rsb *r, struct dlm_lkb *lkb)
1445{
1446 grant_lock(r, lkb);
1447 if (is_master_copy(lkb))
1448 send_grant(r, lkb);
1449 else
1450 queue_cast(r, lkb, 0);
1451}
1452
David Teigland7d3c1fe2007-04-19 10:30:41 -05001453/* The special CONVDEADLK, ALTPR and ALTCW flags allow the master to
1454 change the granted/requested modes. We're munging things accordingly in
1455 the process copy.
1456 CONVDEADLK: our grmode may have been forced down to NL to resolve a
1457 conversion deadlock
1458 ALTPR/ALTCW: our rqmode may have been changed to PR or CW to become
1459 compatible with other granted locks */
1460
David Teigland2a7ce0e2011-04-04 15:19:59 -05001461static void munge_demoted(struct dlm_lkb *lkb)
David Teigland7d3c1fe2007-04-19 10:30:41 -05001462{
David Teigland7d3c1fe2007-04-19 10:30:41 -05001463 if (lkb->lkb_rqmode == DLM_LOCK_IV || lkb->lkb_grmode == DLM_LOCK_IV) {
1464 log_print("munge_demoted %x invalid modes gr %d rq %d",
1465 lkb->lkb_id, lkb->lkb_grmode, lkb->lkb_rqmode);
1466 return;
1467 }
1468
1469 lkb->lkb_grmode = DLM_LOCK_NL;
1470}
1471
1472static void munge_altmode(struct dlm_lkb *lkb, struct dlm_message *ms)
1473{
1474 if (ms->m_type != DLM_MSG_REQUEST_REPLY &&
1475 ms->m_type != DLM_MSG_GRANT) {
1476 log_print("munge_altmode %x invalid reply type %d",
1477 lkb->lkb_id, ms->m_type);
1478 return;
1479 }
1480
1481 if (lkb->lkb_exflags & DLM_LKF_ALTPR)
1482 lkb->lkb_rqmode = DLM_LOCK_PR;
1483 else if (lkb->lkb_exflags & DLM_LKF_ALTCW)
1484 lkb->lkb_rqmode = DLM_LOCK_CW;
1485 else {
1486 log_print("munge_altmode invalid exflags %x", lkb->lkb_exflags);
1487 dlm_print_lkb(lkb);
1488 }
1489}
1490
David Teiglande7fd4172006-01-18 09:30:29 +00001491static inline int first_in_list(struct dlm_lkb *lkb, struct list_head *head)
1492{
1493 struct dlm_lkb *first = list_entry(head->next, struct dlm_lkb,
1494 lkb_statequeue);
1495 if (lkb->lkb_id == first->lkb_id)
David Teigland90135922006-01-20 08:47:07 +00001496 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001497
David Teigland90135922006-01-20 08:47:07 +00001498 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001499}
1500
David Teiglande7fd4172006-01-18 09:30:29 +00001501/* Check if the given lkb conflicts with another lkb on the queue. */
1502
1503static int queue_conflict(struct list_head *head, struct dlm_lkb *lkb)
1504{
1505 struct dlm_lkb *this;
1506
1507 list_for_each_entry(this, head, lkb_statequeue) {
1508 if (this == lkb)
1509 continue;
David Teigland3bcd3682006-02-23 09:56:38 +00001510 if (!modes_compat(this, lkb))
David Teigland90135922006-01-20 08:47:07 +00001511 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001512 }
David Teigland90135922006-01-20 08:47:07 +00001513 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001514}
1515
1516/*
1517 * "A conversion deadlock arises with a pair of lock requests in the converting
1518 * queue for one resource. The granted mode of each lock blocks the requested
1519 * mode of the other lock."
1520 *
David Teiglandc85d65e2007-05-18 09:01:26 -05001521 * Part 2: if the granted mode of lkb is preventing an earlier lkb in the
1522 * convert queue from being granted, then deadlk/demote lkb.
David Teiglande7fd4172006-01-18 09:30:29 +00001523 *
1524 * Example:
1525 * Granted Queue: empty
1526 * Convert Queue: NL->EX (first lock)
1527 * PR->EX (second lock)
1528 *
1529 * The first lock can't be granted because of the granted mode of the second
1530 * lock and the second lock can't be granted because it's not first in the
David Teiglandc85d65e2007-05-18 09:01:26 -05001531 * list. We either cancel lkb's conversion (PR->EX) and return EDEADLK, or we
1532 * demote the granted mode of lkb (from PR to NL) if it has the CONVDEADLK
1533 * flag set and return DEMOTED in the lksb flags.
David Teiglande7fd4172006-01-18 09:30:29 +00001534 *
David Teiglandc85d65e2007-05-18 09:01:26 -05001535 * Originally, this function detected conv-deadlk in a more limited scope:
1536 * - if !modes_compat(lkb1, lkb2) && !modes_compat(lkb2, lkb1), or
1537 * - if lkb1 was the first entry in the queue (not just earlier), and was
1538 * blocked by the granted mode of lkb2, and there was nothing on the
1539 * granted queue preventing lkb1 from being granted immediately, i.e.
1540 * lkb2 was the only thing preventing lkb1 from being granted.
1541 *
1542 * That second condition meant we'd only say there was conv-deadlk if
1543 * resolving it (by demotion) would lead to the first lock on the convert
1544 * queue being granted right away. It allowed conversion deadlocks to exist
1545 * between locks on the convert queue while they couldn't be granted anyway.
1546 *
1547 * Now, we detect and take action on conversion deadlocks immediately when
1548 * they're created, even if they may not be immediately consequential. If
1549 * lkb1 exists anywhere in the convert queue and lkb2 comes in with a granted
1550 * mode that would prevent lkb1's conversion from being granted, we do a
1551 * deadlk/demote on lkb2 right away and don't let it onto the convert queue.
1552 * I think this means that the lkb_is_ahead condition below should always
1553 * be zero, i.e. there will never be conv-deadlk between two locks that are
1554 * both already on the convert queue.
David Teiglande7fd4172006-01-18 09:30:29 +00001555 */
1556
David Teiglandc85d65e2007-05-18 09:01:26 -05001557static int conversion_deadlock_detect(struct dlm_rsb *r, struct dlm_lkb *lkb2)
David Teiglande7fd4172006-01-18 09:30:29 +00001558{
David Teiglandc85d65e2007-05-18 09:01:26 -05001559 struct dlm_lkb *lkb1;
1560 int lkb_is_ahead = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001561
David Teiglandc85d65e2007-05-18 09:01:26 -05001562 list_for_each_entry(lkb1, &r->res_convertqueue, lkb_statequeue) {
1563 if (lkb1 == lkb2) {
1564 lkb_is_ahead = 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001565 continue;
1566 }
1567
David Teiglandc85d65e2007-05-18 09:01:26 -05001568 if (!lkb_is_ahead) {
1569 if (!modes_compat(lkb2, lkb1))
1570 return 1;
1571 } else {
1572 if (!modes_compat(lkb2, lkb1) &&
1573 !modes_compat(lkb1, lkb2))
1574 return 1;
1575 }
David Teiglande7fd4172006-01-18 09:30:29 +00001576 }
David Teigland90135922006-01-20 08:47:07 +00001577 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001578}
1579
1580/*
1581 * Return 1 if the lock can be granted, 0 otherwise.
1582 * Also detect and resolve conversion deadlocks.
1583 *
1584 * lkb is the lock to be granted
1585 *
1586 * now is 1 if the function is being called in the context of the
1587 * immediate request, it is 0 if called later, after the lock has been
1588 * queued.
1589 *
1590 * References are from chapter 6 of "VAXcluster Principles" by Roy Davis
1591 */
1592
1593static int _can_be_granted(struct dlm_rsb *r, struct dlm_lkb *lkb, int now)
1594{
1595 int8_t conv = (lkb->lkb_grmode != DLM_LOCK_IV);
1596
1597 /*
1598 * 6-10: Version 5.4 introduced an option to address the phenomenon of
1599 * a new request for a NL mode lock being blocked.
1600 *
1601 * 6-11: If the optional EXPEDITE flag is used with the new NL mode
1602 * request, then it would be granted. In essence, the use of this flag
1603 * tells the Lock Manager to expedite theis request by not considering
1604 * what may be in the CONVERTING or WAITING queues... As of this
1605 * writing, the EXPEDITE flag can be used only with new requests for NL
1606 * mode locks. This flag is not valid for conversion requests.
1607 *
1608 * A shortcut. Earlier checks return an error if EXPEDITE is used in a
1609 * conversion or used with a non-NL requested mode. We also know an
1610 * EXPEDITE request is always granted immediately, so now must always
1611 * be 1. The full condition to grant an expedite request: (now &&
1612 * !conv && lkb->rqmode == DLM_LOCK_NL && (flags & EXPEDITE)) can
1613 * therefore be shortened to just checking the flag.
1614 */
1615
1616 if (lkb->lkb_exflags & DLM_LKF_EXPEDITE)
David Teigland90135922006-01-20 08:47:07 +00001617 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001618
1619 /*
1620 * A shortcut. Without this, !queue_conflict(grantqueue, lkb) would be
1621 * added to the remaining conditions.
1622 */
1623
1624 if (queue_conflict(&r->res_grantqueue, lkb))
1625 goto out;
1626
1627 /*
1628 * 6-3: By default, a conversion request is immediately granted if the
1629 * requested mode is compatible with the modes of all other granted
1630 * locks
1631 */
1632
1633 if (queue_conflict(&r->res_convertqueue, lkb))
1634 goto out;
1635
1636 /*
1637 * 6-5: But the default algorithm for deciding whether to grant or
1638 * queue conversion requests does not by itself guarantee that such
1639 * requests are serviced on a "first come first serve" basis. This, in
1640 * turn, can lead to a phenomenon known as "indefinate postponement".
1641 *
1642 * 6-7: This issue is dealt with by using the optional QUECVT flag with
1643 * the system service employed to request a lock conversion. This flag
1644 * forces certain conversion requests to be queued, even if they are
1645 * compatible with the granted modes of other locks on the same
1646 * resource. Thus, the use of this flag results in conversion requests
1647 * being ordered on a "first come first servce" basis.
1648 *
1649 * DCT: This condition is all about new conversions being able to occur
1650 * "in place" while the lock remains on the granted queue (assuming
1651 * nothing else conflicts.) IOW if QUECVT isn't set, a conversion
1652 * doesn't _have_ to go onto the convert queue where it's processed in
1653 * order. The "now" variable is necessary to distinguish converts
1654 * being received and processed for the first time now, because once a
1655 * convert is moved to the conversion queue the condition below applies
1656 * requiring fifo granting.
1657 */
1658
1659 if (now && conv && !(lkb->lkb_exflags & DLM_LKF_QUECVT))
David Teigland90135922006-01-20 08:47:07 +00001660 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001661
1662 /*
David Teigland3bcd3682006-02-23 09:56:38 +00001663 * The NOORDER flag is set to avoid the standard vms rules on grant
1664 * order.
David Teiglande7fd4172006-01-18 09:30:29 +00001665 */
1666
1667 if (lkb->lkb_exflags & DLM_LKF_NOORDER)
David Teigland90135922006-01-20 08:47:07 +00001668 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001669
1670 /*
1671 * 6-3: Once in that queue [CONVERTING], a conversion request cannot be
1672 * granted until all other conversion requests ahead of it are granted
1673 * and/or canceled.
1674 */
1675
1676 if (!now && conv && first_in_list(lkb, &r->res_convertqueue))
David Teigland90135922006-01-20 08:47:07 +00001677 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001678
1679 /*
1680 * 6-4: By default, a new request is immediately granted only if all
1681 * three of the following conditions are satisfied when the request is
1682 * issued:
1683 * - The queue of ungranted conversion requests for the resource is
1684 * empty.
1685 * - The queue of ungranted new requests for the resource is empty.
1686 * - The mode of the new request is compatible with the most
1687 * restrictive mode of all granted locks on the resource.
1688 */
1689
1690 if (now && !conv && list_empty(&r->res_convertqueue) &&
1691 list_empty(&r->res_waitqueue))
David Teigland90135922006-01-20 08:47:07 +00001692 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001693
1694 /*
1695 * 6-4: Once a lock request is in the queue of ungranted new requests,
1696 * it cannot be granted until the queue of ungranted conversion
1697 * requests is empty, all ungranted new requests ahead of it are
1698 * granted and/or canceled, and it is compatible with the granted mode
1699 * of the most restrictive lock granted on the resource.
1700 */
1701
1702 if (!now && !conv && list_empty(&r->res_convertqueue) &&
1703 first_in_list(lkb, &r->res_waitqueue))
David Teigland90135922006-01-20 08:47:07 +00001704 return 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001705 out:
David Teigland90135922006-01-20 08:47:07 +00001706 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001707}
1708
David Teiglandc85d65e2007-05-18 09:01:26 -05001709static int can_be_granted(struct dlm_rsb *r, struct dlm_lkb *lkb, int now,
1710 int *err)
David Teiglande7fd4172006-01-18 09:30:29 +00001711{
David Teiglande7fd4172006-01-18 09:30:29 +00001712 int rv;
1713 int8_t alt = 0, rqmode = lkb->lkb_rqmode;
David Teiglandc85d65e2007-05-18 09:01:26 -05001714 int8_t is_convert = (lkb->lkb_grmode != DLM_LOCK_IV);
1715
1716 if (err)
1717 *err = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001718
1719 rv = _can_be_granted(r, lkb, now);
1720 if (rv)
1721 goto out;
1722
David Teiglandc85d65e2007-05-18 09:01:26 -05001723 /*
1724 * The CONVDEADLK flag is non-standard and tells the dlm to resolve
1725 * conversion deadlocks by demoting grmode to NL, otherwise the dlm
1726 * cancels one of the locks.
1727 */
David Teiglande7fd4172006-01-18 09:30:29 +00001728
David Teiglandc85d65e2007-05-18 09:01:26 -05001729 if (is_convert && can_be_queued(lkb) &&
1730 conversion_deadlock_detect(r, lkb)) {
1731 if (lkb->lkb_exflags & DLM_LKF_CONVDEADLK) {
1732 lkb->lkb_grmode = DLM_LOCK_NL;
1733 lkb->lkb_sbflags |= DLM_SBF_DEMOTED;
1734 } else if (!(lkb->lkb_exflags & DLM_LKF_NODLCKWT)) {
1735 if (err)
1736 *err = -EDEADLK;
1737 else {
1738 log_print("can_be_granted deadlock %x now %d",
1739 lkb->lkb_id, now);
1740 dlm_dump_rsb(r);
1741 }
1742 }
1743 goto out;
1744 }
1745
1746 /*
1747 * The ALTPR and ALTCW flags are non-standard and tell the dlm to try
1748 * to grant a request in a mode other than the normal rqmode. It's a
1749 * simple way to provide a big optimization to applications that can
1750 * use them.
1751 */
1752
1753 if (rqmode != DLM_LOCK_PR && (lkb->lkb_exflags & DLM_LKF_ALTPR))
David Teiglande7fd4172006-01-18 09:30:29 +00001754 alt = DLM_LOCK_PR;
David Teiglandc85d65e2007-05-18 09:01:26 -05001755 else if (rqmode != DLM_LOCK_CW && (lkb->lkb_exflags & DLM_LKF_ALTCW))
David Teiglande7fd4172006-01-18 09:30:29 +00001756 alt = DLM_LOCK_CW;
1757
1758 if (alt) {
1759 lkb->lkb_rqmode = alt;
1760 rv = _can_be_granted(r, lkb, now);
1761 if (rv)
1762 lkb->lkb_sbflags |= DLM_SBF_ALTMODE;
1763 else
1764 lkb->lkb_rqmode = rqmode;
1765 }
1766 out:
1767 return rv;
1768}
1769
David Teiglandc85d65e2007-05-18 09:01:26 -05001770/* FIXME: I don't think that can_be_granted() can/will demote or find deadlock
1771 for locks pending on the convert list. Once verified (watch for these
1772 log_prints), we should be able to just call _can_be_granted() and not
1773 bother with the demote/deadlk cases here (and there's no easy way to deal
1774 with a deadlk here, we'd have to generate something like grant_lock with
1775 the deadlk error.) */
1776
David Teigland36509252007-08-07 09:44:48 -05001777/* Returns the highest requested mode of all blocked conversions; sets
1778 cw if there's a blocked conversion to DLM_LOCK_CW. */
David Teiglandc85d65e2007-05-18 09:01:26 -05001779
David Teigland36509252007-08-07 09:44:48 -05001780static int grant_pending_convert(struct dlm_rsb *r, int high, int *cw)
David Teiglande7fd4172006-01-18 09:30:29 +00001781{
1782 struct dlm_lkb *lkb, *s;
1783 int hi, demoted, quit, grant_restart, demote_restart;
David Teiglandc85d65e2007-05-18 09:01:26 -05001784 int deadlk;
David Teiglande7fd4172006-01-18 09:30:29 +00001785
1786 quit = 0;
1787 restart:
1788 grant_restart = 0;
1789 demote_restart = 0;
1790 hi = DLM_LOCK_IV;
1791
1792 list_for_each_entry_safe(lkb, s, &r->res_convertqueue, lkb_statequeue) {
1793 demoted = is_demoted(lkb);
David Teiglandc85d65e2007-05-18 09:01:26 -05001794 deadlk = 0;
1795
1796 if (can_be_granted(r, lkb, 0, &deadlk)) {
David Teiglande7fd4172006-01-18 09:30:29 +00001797 grant_lock_pending(r, lkb);
1798 grant_restart = 1;
David Teiglandc85d65e2007-05-18 09:01:26 -05001799 continue;
David Teiglande7fd4172006-01-18 09:30:29 +00001800 }
David Teiglandc85d65e2007-05-18 09:01:26 -05001801
1802 if (!demoted && is_demoted(lkb)) {
1803 log_print("WARN: pending demoted %x node %d %s",
1804 lkb->lkb_id, lkb->lkb_nodeid, r->res_name);
1805 demote_restart = 1;
1806 continue;
1807 }
1808
1809 if (deadlk) {
1810 log_print("WARN: pending deadlock %x node %d %s",
1811 lkb->lkb_id, lkb->lkb_nodeid, r->res_name);
1812 dlm_dump_rsb(r);
1813 continue;
1814 }
1815
1816 hi = max_t(int, lkb->lkb_rqmode, hi);
David Teigland36509252007-08-07 09:44:48 -05001817
1818 if (cw && lkb->lkb_rqmode == DLM_LOCK_CW)
1819 *cw = 1;
David Teiglande7fd4172006-01-18 09:30:29 +00001820 }
1821
1822 if (grant_restart)
1823 goto restart;
1824 if (demote_restart && !quit) {
1825 quit = 1;
1826 goto restart;
1827 }
1828
1829 return max_t(int, high, hi);
1830}
1831
David Teigland36509252007-08-07 09:44:48 -05001832static int grant_pending_wait(struct dlm_rsb *r, int high, int *cw)
David Teiglande7fd4172006-01-18 09:30:29 +00001833{
1834 struct dlm_lkb *lkb, *s;
1835
1836 list_for_each_entry_safe(lkb, s, &r->res_waitqueue, lkb_statequeue) {
David Teiglandc85d65e2007-05-18 09:01:26 -05001837 if (can_be_granted(r, lkb, 0, NULL))
David Teiglande7fd4172006-01-18 09:30:29 +00001838 grant_lock_pending(r, lkb);
David Teigland36509252007-08-07 09:44:48 -05001839 else {
David Teiglande7fd4172006-01-18 09:30:29 +00001840 high = max_t(int, lkb->lkb_rqmode, high);
David Teigland36509252007-08-07 09:44:48 -05001841 if (lkb->lkb_rqmode == DLM_LOCK_CW)
1842 *cw = 1;
1843 }
David Teiglande7fd4172006-01-18 09:30:29 +00001844 }
1845
1846 return high;
1847}
1848
David Teigland36509252007-08-07 09:44:48 -05001849/* cw of 1 means there's a lock with a rqmode of DLM_LOCK_CW that's blocked
1850 on either the convert or waiting queue.
1851 high is the largest rqmode of all locks blocked on the convert or
1852 waiting queue. */
1853
1854static int lock_requires_bast(struct dlm_lkb *gr, int high, int cw)
1855{
1856 if (gr->lkb_grmode == DLM_LOCK_PR && cw) {
1857 if (gr->lkb_highbast < DLM_LOCK_EX)
1858 return 1;
1859 return 0;
1860 }
1861
1862 if (gr->lkb_highbast < high &&
1863 !__dlm_compat_matrix[gr->lkb_grmode+1][high+1])
1864 return 1;
1865 return 0;
1866}
1867
David Teiglande7fd4172006-01-18 09:30:29 +00001868static void grant_pending_locks(struct dlm_rsb *r)
1869{
1870 struct dlm_lkb *lkb, *s;
1871 int high = DLM_LOCK_IV;
David Teigland36509252007-08-07 09:44:48 -05001872 int cw = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00001873
David Teiglanda345da32006-08-18 11:54:25 -05001874 DLM_ASSERT(is_master(r), dlm_dump_rsb(r););
David Teiglande7fd4172006-01-18 09:30:29 +00001875
David Teigland36509252007-08-07 09:44:48 -05001876 high = grant_pending_convert(r, high, &cw);
1877 high = grant_pending_wait(r, high, &cw);
David Teiglande7fd4172006-01-18 09:30:29 +00001878
1879 if (high == DLM_LOCK_IV)
1880 return;
1881
1882 /*
1883 * If there are locks left on the wait/convert queue then send blocking
1884 * ASTs to granted locks based on the largest requested mode (high)
David Teigland36509252007-08-07 09:44:48 -05001885 * found above.
David Teiglande7fd4172006-01-18 09:30:29 +00001886 */
1887
1888 list_for_each_entry_safe(lkb, s, &r->res_grantqueue, lkb_statequeue) {
David Teiglande5dae542008-02-06 00:35:45 -06001889 if (lkb->lkb_bastfn && lock_requires_bast(lkb, high, cw)) {
David Teigland329fc4c2008-05-20 12:18:10 -05001890 if (cw && high == DLM_LOCK_PR &&
1891 lkb->lkb_grmode == DLM_LOCK_PR)
David Teigland36509252007-08-07 09:44:48 -05001892 queue_bast(r, lkb, DLM_LOCK_CW);
1893 else
1894 queue_bast(r, lkb, high);
David Teiglande7fd4172006-01-18 09:30:29 +00001895 lkb->lkb_highbast = high;
1896 }
1897 }
1898}
1899
David Teigland36509252007-08-07 09:44:48 -05001900static int modes_require_bast(struct dlm_lkb *gr, struct dlm_lkb *rq)
1901{
1902 if ((gr->lkb_grmode == DLM_LOCK_PR && rq->lkb_rqmode == DLM_LOCK_CW) ||
1903 (gr->lkb_grmode == DLM_LOCK_CW && rq->lkb_rqmode == DLM_LOCK_PR)) {
1904 if (gr->lkb_highbast < DLM_LOCK_EX)
1905 return 1;
1906 return 0;
1907 }
1908
1909 if (gr->lkb_highbast < rq->lkb_rqmode && !modes_compat(gr, rq))
1910 return 1;
1911 return 0;
1912}
1913
David Teiglande7fd4172006-01-18 09:30:29 +00001914static void send_bast_queue(struct dlm_rsb *r, struct list_head *head,
1915 struct dlm_lkb *lkb)
1916{
1917 struct dlm_lkb *gr;
1918
1919 list_for_each_entry(gr, head, lkb_statequeue) {
Steven Whitehouse314dd2a2010-09-03 10:07:48 -05001920 /* skip self when sending basts to convertqueue */
1921 if (gr == lkb)
1922 continue;
David Teiglande5dae542008-02-06 00:35:45 -06001923 if (gr->lkb_bastfn && modes_require_bast(gr, lkb)) {
David Teiglande7fd4172006-01-18 09:30:29 +00001924 queue_bast(r, gr, lkb->lkb_rqmode);
1925 gr->lkb_highbast = lkb->lkb_rqmode;
1926 }
1927 }
1928}
1929
1930static void send_blocking_asts(struct dlm_rsb *r, struct dlm_lkb *lkb)
1931{
1932 send_bast_queue(r, &r->res_grantqueue, lkb);
1933}
1934
1935static void send_blocking_asts_all(struct dlm_rsb *r, struct dlm_lkb *lkb)
1936{
1937 send_bast_queue(r, &r->res_grantqueue, lkb);
1938 send_bast_queue(r, &r->res_convertqueue, lkb);
1939}
1940
1941/* set_master(r, lkb) -- set the master nodeid of a resource
1942
1943 The purpose of this function is to set the nodeid field in the given
1944 lkb using the nodeid field in the given rsb. If the rsb's nodeid is
1945 known, it can just be copied to the lkb and the function will return
1946 0. If the rsb's nodeid is _not_ known, it needs to be looked up
1947 before it can be copied to the lkb.
1948
1949 When the rsb nodeid is being looked up remotely, the initial lkb
1950 causing the lookup is kept on the ls_waiters list waiting for the
1951 lookup reply. Other lkb's waiting for the same rsb lookup are kept
1952 on the rsb's res_lookup list until the master is verified.
1953
1954 Return values:
1955 0: nodeid is set in rsb/lkb and the caller should go ahead and use it
1956 1: the rsb master is not available and the lkb has been placed on
1957 a wait queue
1958*/
1959
1960static int set_master(struct dlm_rsb *r, struct dlm_lkb *lkb)
1961{
1962 struct dlm_ls *ls = r->res_ls;
David Teigland755b5eb2008-01-09 10:37:39 -06001963 int i, error, dir_nodeid, ret_nodeid, our_nodeid = dlm_our_nodeid();
David Teiglande7fd4172006-01-18 09:30:29 +00001964
1965 if (rsb_flag(r, RSB_MASTER_UNCERTAIN)) {
1966 rsb_clear_flag(r, RSB_MASTER_UNCERTAIN);
1967 r->res_first_lkid = lkb->lkb_id;
1968 lkb->lkb_nodeid = r->res_nodeid;
1969 return 0;
1970 }
1971
1972 if (r->res_first_lkid && r->res_first_lkid != lkb->lkb_id) {
1973 list_add_tail(&lkb->lkb_rsb_lookup, &r->res_lookup);
1974 return 1;
1975 }
1976
1977 if (r->res_nodeid == 0) {
1978 lkb->lkb_nodeid = 0;
1979 return 0;
1980 }
1981
1982 if (r->res_nodeid > 0) {
1983 lkb->lkb_nodeid = r->res_nodeid;
1984 return 0;
1985 }
1986
David Teiglanda345da32006-08-18 11:54:25 -05001987 DLM_ASSERT(r->res_nodeid == -1, dlm_dump_rsb(r););
David Teiglande7fd4172006-01-18 09:30:29 +00001988
1989 dir_nodeid = dlm_dir_nodeid(r);
1990
1991 if (dir_nodeid != our_nodeid) {
1992 r->res_first_lkid = lkb->lkb_id;
1993 send_lookup(r, lkb);
1994 return 1;
1995 }
1996
David Teigland755b5eb2008-01-09 10:37:39 -06001997 for (i = 0; i < 2; i++) {
David Teiglande7fd4172006-01-18 09:30:29 +00001998 /* It's possible for dlm_scand to remove an old rsb for
1999 this same resource from the toss list, us to create
2000 a new one, look up the master locally, and find it
2001 already exists just before dlm_scand does the
2002 dir_remove() on the previous rsb. */
2003
2004 error = dlm_dir_lookup(ls, our_nodeid, r->res_name,
2005 r->res_length, &ret_nodeid);
2006 if (!error)
2007 break;
2008 log_debug(ls, "dir_lookup error %d %s", error, r->res_name);
2009 schedule();
2010 }
David Teigland755b5eb2008-01-09 10:37:39 -06002011 if (error && error != -EEXIST)
2012 return error;
David Teiglande7fd4172006-01-18 09:30:29 +00002013
2014 if (ret_nodeid == our_nodeid) {
2015 r->res_first_lkid = 0;
2016 r->res_nodeid = 0;
2017 lkb->lkb_nodeid = 0;
2018 } else {
2019 r->res_first_lkid = lkb->lkb_id;
2020 r->res_nodeid = ret_nodeid;
2021 lkb->lkb_nodeid = ret_nodeid;
2022 }
2023 return 0;
2024}
2025
2026static void process_lookup_list(struct dlm_rsb *r)
2027{
2028 struct dlm_lkb *lkb, *safe;
2029
2030 list_for_each_entry_safe(lkb, safe, &r->res_lookup, lkb_rsb_lookup) {
David Teiglandef0c2bb2007-03-28 09:56:46 -05002031 list_del_init(&lkb->lkb_rsb_lookup);
David Teiglande7fd4172006-01-18 09:30:29 +00002032 _request_lock(r, lkb);
2033 schedule();
2034 }
2035}
2036
2037/* confirm_master -- confirm (or deny) an rsb's master nodeid */
2038
2039static void confirm_master(struct dlm_rsb *r, int error)
2040{
2041 struct dlm_lkb *lkb;
2042
2043 if (!r->res_first_lkid)
2044 return;
2045
2046 switch (error) {
2047 case 0:
2048 case -EINPROGRESS:
2049 r->res_first_lkid = 0;
2050 process_lookup_list(r);
2051 break;
2052
2053 case -EAGAIN:
David Teiglandaec64e12008-01-08 15:37:47 -06002054 case -EBADR:
2055 case -ENOTBLK:
2056 /* the remote request failed and won't be retried (it was
2057 a NOQUEUE, or has been canceled/unlocked); make a waiting
2058 lkb the first_lkid */
David Teiglande7fd4172006-01-18 09:30:29 +00002059
2060 r->res_first_lkid = 0;
2061
2062 if (!list_empty(&r->res_lookup)) {
2063 lkb = list_entry(r->res_lookup.next, struct dlm_lkb,
2064 lkb_rsb_lookup);
David Teiglandef0c2bb2007-03-28 09:56:46 -05002065 list_del_init(&lkb->lkb_rsb_lookup);
David Teiglande7fd4172006-01-18 09:30:29 +00002066 r->res_first_lkid = lkb->lkb_id;
2067 _request_lock(r, lkb);
David Teigland761b9d32008-02-21 11:25:42 -06002068 }
David Teiglande7fd4172006-01-18 09:30:29 +00002069 break;
2070
2071 default:
2072 log_error(r->res_ls, "confirm_master unknown error %d", error);
2073 }
2074}
2075
2076static int set_lock_args(int mode, struct dlm_lksb *lksb, uint32_t flags,
David Teiglande5dae542008-02-06 00:35:45 -06002077 int namelen, unsigned long timeout_cs,
2078 void (*ast) (void *astparam),
2079 void *astparam,
2080 void (*bast) (void *astparam, int mode),
2081 struct dlm_args *args)
David Teiglande7fd4172006-01-18 09:30:29 +00002082{
2083 int rv = -EINVAL;
2084
2085 /* check for invalid arg usage */
2086
2087 if (mode < 0 || mode > DLM_LOCK_EX)
2088 goto out;
2089
2090 if (!(flags & DLM_LKF_CONVERT) && (namelen > DLM_RESNAME_MAXLEN))
2091 goto out;
2092
2093 if (flags & DLM_LKF_CANCEL)
2094 goto out;
2095
2096 if (flags & DLM_LKF_QUECVT && !(flags & DLM_LKF_CONVERT))
2097 goto out;
2098
2099 if (flags & DLM_LKF_CONVDEADLK && !(flags & DLM_LKF_CONVERT))
2100 goto out;
2101
2102 if (flags & DLM_LKF_CONVDEADLK && flags & DLM_LKF_NOQUEUE)
2103 goto out;
2104
2105 if (flags & DLM_LKF_EXPEDITE && flags & DLM_LKF_CONVERT)
2106 goto out;
2107
2108 if (flags & DLM_LKF_EXPEDITE && flags & DLM_LKF_QUECVT)
2109 goto out;
2110
2111 if (flags & DLM_LKF_EXPEDITE && flags & DLM_LKF_NOQUEUE)
2112 goto out;
2113
2114 if (flags & DLM_LKF_EXPEDITE && mode != DLM_LOCK_NL)
2115 goto out;
2116
2117 if (!ast || !lksb)
2118 goto out;
2119
2120 if (flags & DLM_LKF_VALBLK && !lksb->sb_lvbptr)
2121 goto out;
2122
David Teiglande7fd4172006-01-18 09:30:29 +00002123 if (flags & DLM_LKF_CONVERT && !lksb->sb_lkid)
2124 goto out;
2125
2126 /* these args will be copied to the lkb in validate_lock_args,
2127 it cannot be done now because when converting locks, fields in
2128 an active lkb cannot be modified before locking the rsb */
2129
2130 args->flags = flags;
David Teiglande5dae542008-02-06 00:35:45 -06002131 args->astfn = ast;
2132 args->astparam = astparam;
2133 args->bastfn = bast;
David Teiglandd7db9232007-05-18 09:00:32 -05002134 args->timeout = timeout_cs;
David Teiglande7fd4172006-01-18 09:30:29 +00002135 args->mode = mode;
2136 args->lksb = lksb;
David Teiglande7fd4172006-01-18 09:30:29 +00002137 rv = 0;
2138 out:
2139 return rv;
2140}
2141
2142static int set_unlock_args(uint32_t flags, void *astarg, struct dlm_args *args)
2143{
2144 if (flags & ~(DLM_LKF_CANCEL | DLM_LKF_VALBLK | DLM_LKF_IVVALBLK |
2145 DLM_LKF_FORCEUNLOCK))
2146 return -EINVAL;
2147
David Teiglandef0c2bb2007-03-28 09:56:46 -05002148 if (flags & DLM_LKF_CANCEL && flags & DLM_LKF_FORCEUNLOCK)
2149 return -EINVAL;
2150
David Teiglande7fd4172006-01-18 09:30:29 +00002151 args->flags = flags;
David Teiglande5dae542008-02-06 00:35:45 -06002152 args->astparam = astarg;
David Teiglande7fd4172006-01-18 09:30:29 +00002153 return 0;
2154}
2155
2156static int validate_lock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
2157 struct dlm_args *args)
2158{
2159 int rv = -EINVAL;
2160
2161 if (args->flags & DLM_LKF_CONVERT) {
2162 if (lkb->lkb_flags & DLM_IFL_MSTCPY)
2163 goto out;
2164
2165 if (args->flags & DLM_LKF_QUECVT &&
2166 !__quecvt_compat_matrix[lkb->lkb_grmode+1][args->mode+1])
2167 goto out;
2168
2169 rv = -EBUSY;
2170 if (lkb->lkb_status != DLM_LKSTS_GRANTED)
2171 goto out;
2172
2173 if (lkb->lkb_wait_type)
2174 goto out;
David Teiglandef0c2bb2007-03-28 09:56:46 -05002175
2176 if (is_overlap(lkb))
2177 goto out;
David Teiglande7fd4172006-01-18 09:30:29 +00002178 }
2179
2180 lkb->lkb_exflags = args->flags;
2181 lkb->lkb_sbflags = 0;
David Teiglande5dae542008-02-06 00:35:45 -06002182 lkb->lkb_astfn = args->astfn;
David Teiglande7fd4172006-01-18 09:30:29 +00002183 lkb->lkb_astparam = args->astparam;
David Teiglande5dae542008-02-06 00:35:45 -06002184 lkb->lkb_bastfn = args->bastfn;
David Teiglande7fd4172006-01-18 09:30:29 +00002185 lkb->lkb_rqmode = args->mode;
2186 lkb->lkb_lksb = args->lksb;
2187 lkb->lkb_lvbptr = args->lksb->sb_lvbptr;
2188 lkb->lkb_ownpid = (int) current->pid;
David Teiglandd7db9232007-05-18 09:00:32 -05002189 lkb->lkb_timeout_cs = args->timeout;
David Teiglande7fd4172006-01-18 09:30:29 +00002190 rv = 0;
2191 out:
David Teigland43279e52009-01-28 14:37:54 -06002192 if (rv)
2193 log_debug(ls, "validate_lock_args %d %x %x %x %d %d %s",
2194 rv, lkb->lkb_id, lkb->lkb_flags, args->flags,
2195 lkb->lkb_status, lkb->lkb_wait_type,
2196 lkb->lkb_resource->res_name);
David Teiglande7fd4172006-01-18 09:30:29 +00002197 return rv;
2198}
2199
David Teiglandef0c2bb2007-03-28 09:56:46 -05002200/* when dlm_unlock() sees -EBUSY with CANCEL/FORCEUNLOCK it returns 0
2201 for success */
2202
2203/* note: it's valid for lkb_nodeid/res_nodeid to be -1 when we get here
2204 because there may be a lookup in progress and it's valid to do
2205 cancel/unlockf on it */
2206
David Teiglande7fd4172006-01-18 09:30:29 +00002207static int validate_unlock_args(struct dlm_lkb *lkb, struct dlm_args *args)
2208{
David Teiglandef0c2bb2007-03-28 09:56:46 -05002209 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
David Teiglande7fd4172006-01-18 09:30:29 +00002210 int rv = -EINVAL;
2211
David Teiglandef0c2bb2007-03-28 09:56:46 -05002212 if (lkb->lkb_flags & DLM_IFL_MSTCPY) {
2213 log_error(ls, "unlock on MSTCPY %x", lkb->lkb_id);
2214 dlm_print_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00002215 goto out;
David Teiglandef0c2bb2007-03-28 09:56:46 -05002216 }
David Teiglande7fd4172006-01-18 09:30:29 +00002217
David Teiglandef0c2bb2007-03-28 09:56:46 -05002218 /* an lkb may still exist even though the lock is EOL'ed due to a
2219 cancel, unlock or failed noqueue request; an app can't use these
2220 locks; return same error as if the lkid had not been found at all */
2221
2222 if (lkb->lkb_flags & DLM_IFL_ENDOFLIFE) {
2223 log_debug(ls, "unlock on ENDOFLIFE %x", lkb->lkb_id);
2224 rv = -ENOENT;
2225 goto out;
2226 }
2227
2228 /* an lkb may be waiting for an rsb lookup to complete where the
2229 lookup was initiated by another lock */
2230
David Teigland42dc1602008-01-09 10:30:45 -06002231 if (!list_empty(&lkb->lkb_rsb_lookup)) {
2232 if (args->flags & (DLM_LKF_CANCEL | DLM_LKF_FORCEUNLOCK)) {
David Teiglandef0c2bb2007-03-28 09:56:46 -05002233 log_debug(ls, "unlock on rsb_lookup %x", lkb->lkb_id);
2234 list_del_init(&lkb->lkb_rsb_lookup);
2235 queue_cast(lkb->lkb_resource, lkb,
2236 args->flags & DLM_LKF_CANCEL ?
2237 -DLM_ECANCEL : -DLM_EUNLOCK);
2238 unhold_lkb(lkb); /* undoes create_lkb() */
David Teiglandef0c2bb2007-03-28 09:56:46 -05002239 }
David Teigland42dc1602008-01-09 10:30:45 -06002240 /* caller changes -EBUSY to 0 for CANCEL and FORCEUNLOCK */
2241 rv = -EBUSY;
2242 goto out;
David Teiglandef0c2bb2007-03-28 09:56:46 -05002243 }
2244
2245 /* cancel not allowed with another cancel/unlock in progress */
2246
2247 if (args->flags & DLM_LKF_CANCEL) {
2248 if (lkb->lkb_exflags & DLM_LKF_CANCEL)
2249 goto out;
2250
2251 if (is_overlap(lkb))
2252 goto out;
2253
David Teigland3ae1acf2007-05-18 08:59:31 -05002254 /* don't let scand try to do a cancel */
2255 del_timeout(lkb);
2256
David Teiglandef0c2bb2007-03-28 09:56:46 -05002257 if (lkb->lkb_flags & DLM_IFL_RESEND) {
2258 lkb->lkb_flags |= DLM_IFL_OVERLAP_CANCEL;
2259 rv = -EBUSY;
2260 goto out;
2261 }
2262
David Teiglanda536e382009-02-27 15:23:28 -06002263 /* there's nothing to cancel */
2264 if (lkb->lkb_status == DLM_LKSTS_GRANTED &&
2265 !lkb->lkb_wait_type) {
2266 rv = -EBUSY;
2267 goto out;
2268 }
2269
David Teiglandef0c2bb2007-03-28 09:56:46 -05002270 switch (lkb->lkb_wait_type) {
2271 case DLM_MSG_LOOKUP:
2272 case DLM_MSG_REQUEST:
2273 lkb->lkb_flags |= DLM_IFL_OVERLAP_CANCEL;
2274 rv = -EBUSY;
2275 goto out;
2276 case DLM_MSG_UNLOCK:
2277 case DLM_MSG_CANCEL:
2278 goto out;
2279 }
2280 /* add_to_waiters() will set OVERLAP_CANCEL */
David Teiglande7fd4172006-01-18 09:30:29 +00002281 goto out_ok;
David Teiglandef0c2bb2007-03-28 09:56:46 -05002282 }
David Teiglande7fd4172006-01-18 09:30:29 +00002283
David Teiglandef0c2bb2007-03-28 09:56:46 -05002284 /* do we need to allow a force-unlock if there's a normal unlock
2285 already in progress? in what conditions could the normal unlock
2286 fail such that we'd want to send a force-unlock to be sure? */
David Teiglande7fd4172006-01-18 09:30:29 +00002287
David Teiglandef0c2bb2007-03-28 09:56:46 -05002288 if (args->flags & DLM_LKF_FORCEUNLOCK) {
2289 if (lkb->lkb_exflags & DLM_LKF_FORCEUNLOCK)
2290 goto out;
David Teiglande7fd4172006-01-18 09:30:29 +00002291
David Teiglandef0c2bb2007-03-28 09:56:46 -05002292 if (is_overlap_unlock(lkb))
2293 goto out;
2294
David Teigland3ae1acf2007-05-18 08:59:31 -05002295 /* don't let scand try to do a cancel */
2296 del_timeout(lkb);
2297
David Teiglandef0c2bb2007-03-28 09:56:46 -05002298 if (lkb->lkb_flags & DLM_IFL_RESEND) {
2299 lkb->lkb_flags |= DLM_IFL_OVERLAP_UNLOCK;
2300 rv = -EBUSY;
2301 goto out;
2302 }
2303
2304 switch (lkb->lkb_wait_type) {
2305 case DLM_MSG_LOOKUP:
2306 case DLM_MSG_REQUEST:
2307 lkb->lkb_flags |= DLM_IFL_OVERLAP_UNLOCK;
2308 rv = -EBUSY;
2309 goto out;
2310 case DLM_MSG_UNLOCK:
2311 goto out;
2312 }
2313 /* add_to_waiters() will set OVERLAP_UNLOCK */
2314 goto out_ok;
2315 }
2316
2317 /* normal unlock not allowed if there's any op in progress */
David Teiglande7fd4172006-01-18 09:30:29 +00002318 rv = -EBUSY;
David Teiglandef0c2bb2007-03-28 09:56:46 -05002319 if (lkb->lkb_wait_type || lkb->lkb_wait_count)
David Teiglande7fd4172006-01-18 09:30:29 +00002320 goto out;
2321
2322 out_ok:
David Teiglandef0c2bb2007-03-28 09:56:46 -05002323 /* an overlapping op shouldn't blow away exflags from other op */
2324 lkb->lkb_exflags |= args->flags;
David Teiglande7fd4172006-01-18 09:30:29 +00002325 lkb->lkb_sbflags = 0;
2326 lkb->lkb_astparam = args->astparam;
David Teiglande7fd4172006-01-18 09:30:29 +00002327 rv = 0;
2328 out:
David Teiglandef0c2bb2007-03-28 09:56:46 -05002329 if (rv)
2330 log_debug(ls, "validate_unlock_args %d %x %x %x %x %d %s", rv,
2331 lkb->lkb_id, lkb->lkb_flags, lkb->lkb_exflags,
2332 args->flags, lkb->lkb_wait_type,
2333 lkb->lkb_resource->res_name);
David Teiglande7fd4172006-01-18 09:30:29 +00002334 return rv;
2335}
2336
2337/*
2338 * Four stage 4 varieties:
2339 * do_request(), do_convert(), do_unlock(), do_cancel()
2340 * These are called on the master node for the given lock and
2341 * from the central locking logic.
2342 */
2343
2344static int do_request(struct dlm_rsb *r, struct dlm_lkb *lkb)
2345{
2346 int error = 0;
2347
David Teiglandc85d65e2007-05-18 09:01:26 -05002348 if (can_be_granted(r, lkb, 1, NULL)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002349 grant_lock(r, lkb);
2350 queue_cast(r, lkb, 0);
2351 goto out;
2352 }
2353
2354 if (can_be_queued(lkb)) {
2355 error = -EINPROGRESS;
2356 add_lkb(r, lkb, DLM_LKSTS_WAITING);
David Teigland3ae1acf2007-05-18 08:59:31 -05002357 add_timeout(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00002358 goto out;
2359 }
2360
2361 error = -EAGAIN;
David Teiglande7fd4172006-01-18 09:30:29 +00002362 queue_cast(r, lkb, -EAGAIN);
David Teiglande7fd4172006-01-18 09:30:29 +00002363 out:
2364 return error;
2365}
2366
David Teiglandcf6620a2010-02-24 11:59:23 -06002367static void do_request_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
2368 int error)
2369{
2370 switch (error) {
2371 case -EAGAIN:
2372 if (force_blocking_asts(lkb))
2373 send_blocking_asts_all(r, lkb);
2374 break;
2375 case -EINPROGRESS:
2376 send_blocking_asts(r, lkb);
2377 break;
2378 }
2379}
2380
David Teiglande7fd4172006-01-18 09:30:29 +00002381static int do_convert(struct dlm_rsb *r, struct dlm_lkb *lkb)
2382{
2383 int error = 0;
David Teiglandc85d65e2007-05-18 09:01:26 -05002384 int deadlk = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00002385
2386 /* changing an existing lock may allow others to be granted */
2387
David Teiglandc85d65e2007-05-18 09:01:26 -05002388 if (can_be_granted(r, lkb, 1, &deadlk)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002389 grant_lock(r, lkb);
2390 queue_cast(r, lkb, 0);
David Teiglande7fd4172006-01-18 09:30:29 +00002391 goto out;
2392 }
2393
David Teiglandc85d65e2007-05-18 09:01:26 -05002394 /* can_be_granted() detected that this lock would block in a conversion
2395 deadlock, so we leave it on the granted queue and return EDEADLK in
2396 the ast for the convert. */
2397
2398 if (deadlk) {
2399 /* it's left on the granted queue */
2400 log_debug(r->res_ls, "deadlock %x node %d sts%d g%d r%d %s",
2401 lkb->lkb_id, lkb->lkb_nodeid, lkb->lkb_status,
2402 lkb->lkb_grmode, lkb->lkb_rqmode, r->res_name);
2403 revert_lock(r, lkb);
2404 queue_cast(r, lkb, -EDEADLK);
2405 error = -EDEADLK;
2406 goto out;
2407 }
2408
David Teigland7d3c1fe2007-04-19 10:30:41 -05002409 /* is_demoted() means the can_be_granted() above set the grmode
2410 to NL, and left us on the granted queue. This auto-demotion
2411 (due to CONVDEADLK) might mean other locks, and/or this lock, are
2412 now grantable. We have to try to grant other converting locks
2413 before we try again to grant this one. */
2414
2415 if (is_demoted(lkb)) {
David Teigland36509252007-08-07 09:44:48 -05002416 grant_pending_convert(r, DLM_LOCK_IV, NULL);
David Teigland7d3c1fe2007-04-19 10:30:41 -05002417 if (_can_be_granted(r, lkb, 1)) {
2418 grant_lock(r, lkb);
2419 queue_cast(r, lkb, 0);
David Teigland7d3c1fe2007-04-19 10:30:41 -05002420 goto out;
2421 }
2422 /* else fall through and move to convert queue */
2423 }
2424
2425 if (can_be_queued(lkb)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002426 error = -EINPROGRESS;
2427 del_lkb(r, lkb);
2428 add_lkb(r, lkb, DLM_LKSTS_CONVERT);
David Teigland3ae1acf2007-05-18 08:59:31 -05002429 add_timeout(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00002430 goto out;
2431 }
2432
2433 error = -EAGAIN;
David Teiglande7fd4172006-01-18 09:30:29 +00002434 queue_cast(r, lkb, -EAGAIN);
David Teiglande7fd4172006-01-18 09:30:29 +00002435 out:
2436 return error;
2437}
2438
David Teiglandcf6620a2010-02-24 11:59:23 -06002439static void do_convert_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
2440 int error)
2441{
2442 switch (error) {
2443 case 0:
2444 grant_pending_locks(r);
2445 /* grant_pending_locks also sends basts */
2446 break;
2447 case -EAGAIN:
2448 if (force_blocking_asts(lkb))
2449 send_blocking_asts_all(r, lkb);
2450 break;
2451 case -EINPROGRESS:
2452 send_blocking_asts(r, lkb);
2453 break;
2454 }
2455}
2456
David Teiglande7fd4172006-01-18 09:30:29 +00002457static int do_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2458{
2459 remove_lock(r, lkb);
2460 queue_cast(r, lkb, -DLM_EUNLOCK);
David Teiglande7fd4172006-01-18 09:30:29 +00002461 return -DLM_EUNLOCK;
2462}
2463
David Teiglandcf6620a2010-02-24 11:59:23 -06002464static void do_unlock_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
2465 int error)
2466{
2467 grant_pending_locks(r);
2468}
2469
David Teiglandef0c2bb2007-03-28 09:56:46 -05002470/* returns: 0 did nothing, -DLM_ECANCEL canceled lock */
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04002471
David Teiglande7fd4172006-01-18 09:30:29 +00002472static int do_cancel(struct dlm_rsb *r, struct dlm_lkb *lkb)
2473{
David Teiglandef0c2bb2007-03-28 09:56:46 -05002474 int error;
2475
2476 error = revert_lock(r, lkb);
2477 if (error) {
2478 queue_cast(r, lkb, -DLM_ECANCEL);
David Teiglandef0c2bb2007-03-28 09:56:46 -05002479 return -DLM_ECANCEL;
2480 }
2481 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +00002482}
2483
David Teiglandcf6620a2010-02-24 11:59:23 -06002484static void do_cancel_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
2485 int error)
2486{
2487 if (error)
2488 grant_pending_locks(r);
2489}
2490
David Teiglande7fd4172006-01-18 09:30:29 +00002491/*
2492 * Four stage 3 varieties:
2493 * _request_lock(), _convert_lock(), _unlock_lock(), _cancel_lock()
2494 */
2495
2496/* add a new lkb to a possibly new rsb, called by requesting process */
2497
2498static int _request_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2499{
2500 int error;
2501
2502 /* set_master: sets lkb nodeid from r */
2503
2504 error = set_master(r, lkb);
2505 if (error < 0)
2506 goto out;
2507 if (error) {
2508 error = 0;
2509 goto out;
2510 }
2511
David Teiglandcf6620a2010-02-24 11:59:23 -06002512 if (is_remote(r)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002513 /* receive_request() calls do_request() on remote node */
2514 error = send_request(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002515 } else {
David Teiglande7fd4172006-01-18 09:30:29 +00002516 error = do_request(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002517 /* for remote locks the request_reply is sent
2518 between do_request and do_request_effects */
2519 do_request_effects(r, lkb, error);
2520 }
David Teiglande7fd4172006-01-18 09:30:29 +00002521 out:
2522 return error;
2523}
2524
David Teigland3bcd3682006-02-23 09:56:38 +00002525/* change some property of an existing lkb, e.g. mode */
David Teiglande7fd4172006-01-18 09:30:29 +00002526
2527static int _convert_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2528{
2529 int error;
2530
David Teiglandcf6620a2010-02-24 11:59:23 -06002531 if (is_remote(r)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002532 /* receive_convert() calls do_convert() on remote node */
2533 error = send_convert(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002534 } else {
David Teiglande7fd4172006-01-18 09:30:29 +00002535 error = do_convert(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002536 /* for remote locks the convert_reply is sent
2537 between do_convert and do_convert_effects */
2538 do_convert_effects(r, lkb, error);
2539 }
David Teiglande7fd4172006-01-18 09:30:29 +00002540
2541 return error;
2542}
2543
2544/* remove an existing lkb from the granted queue */
2545
2546static int _unlock_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2547{
2548 int error;
2549
David Teiglandcf6620a2010-02-24 11:59:23 -06002550 if (is_remote(r)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002551 /* receive_unlock() calls do_unlock() on remote node */
2552 error = send_unlock(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002553 } else {
David Teiglande7fd4172006-01-18 09:30:29 +00002554 error = do_unlock(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002555 /* for remote locks the unlock_reply is sent
2556 between do_unlock and do_unlock_effects */
2557 do_unlock_effects(r, lkb, error);
2558 }
David Teiglande7fd4172006-01-18 09:30:29 +00002559
2560 return error;
2561}
2562
2563/* remove an existing lkb from the convert or wait queue */
2564
2565static int _cancel_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2566{
2567 int error;
2568
David Teiglandcf6620a2010-02-24 11:59:23 -06002569 if (is_remote(r)) {
David Teiglande7fd4172006-01-18 09:30:29 +00002570 /* receive_cancel() calls do_cancel() on remote node */
2571 error = send_cancel(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002572 } else {
David Teiglande7fd4172006-01-18 09:30:29 +00002573 error = do_cancel(r, lkb);
David Teiglandcf6620a2010-02-24 11:59:23 -06002574 /* for remote locks the cancel_reply is sent
2575 between do_cancel and do_cancel_effects */
2576 do_cancel_effects(r, lkb, error);
2577 }
David Teiglande7fd4172006-01-18 09:30:29 +00002578
2579 return error;
2580}
2581
2582/*
2583 * Four stage 2 varieties:
2584 * request_lock(), convert_lock(), unlock_lock(), cancel_lock()
2585 */
2586
2587static int request_lock(struct dlm_ls *ls, struct dlm_lkb *lkb, char *name,
2588 int len, struct dlm_args *args)
2589{
2590 struct dlm_rsb *r;
2591 int error;
2592
2593 error = validate_lock_args(ls, lkb, args);
2594 if (error)
2595 goto out;
2596
2597 error = find_rsb(ls, name, len, R_CREATE, &r);
2598 if (error)
2599 goto out;
2600
2601 lock_rsb(r);
2602
2603 attach_lkb(r, lkb);
2604 lkb->lkb_lksb->sb_lkid = lkb->lkb_id;
2605
2606 error = _request_lock(r, lkb);
2607
2608 unlock_rsb(r);
2609 put_rsb(r);
2610
2611 out:
2612 return error;
2613}
2614
2615static int convert_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
2616 struct dlm_args *args)
2617{
2618 struct dlm_rsb *r;
2619 int error;
2620
2621 r = lkb->lkb_resource;
2622
2623 hold_rsb(r);
2624 lock_rsb(r);
2625
2626 error = validate_lock_args(ls, lkb, args);
2627 if (error)
2628 goto out;
2629
2630 error = _convert_lock(r, lkb);
2631 out:
2632 unlock_rsb(r);
2633 put_rsb(r);
2634 return error;
2635}
2636
2637static int unlock_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
2638 struct dlm_args *args)
2639{
2640 struct dlm_rsb *r;
2641 int error;
2642
2643 r = lkb->lkb_resource;
2644
2645 hold_rsb(r);
2646 lock_rsb(r);
2647
2648 error = validate_unlock_args(lkb, args);
2649 if (error)
2650 goto out;
2651
2652 error = _unlock_lock(r, lkb);
2653 out:
2654 unlock_rsb(r);
2655 put_rsb(r);
2656 return error;
2657}
2658
2659static int cancel_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
2660 struct dlm_args *args)
2661{
2662 struct dlm_rsb *r;
2663 int error;
2664
2665 r = lkb->lkb_resource;
2666
2667 hold_rsb(r);
2668 lock_rsb(r);
2669
2670 error = validate_unlock_args(lkb, args);
2671 if (error)
2672 goto out;
2673
2674 error = _cancel_lock(r, lkb);
2675 out:
2676 unlock_rsb(r);
2677 put_rsb(r);
2678 return error;
2679}
2680
2681/*
2682 * Two stage 1 varieties: dlm_lock() and dlm_unlock()
2683 */
2684
2685int dlm_lock(dlm_lockspace_t *lockspace,
2686 int mode,
2687 struct dlm_lksb *lksb,
2688 uint32_t flags,
2689 void *name,
2690 unsigned int namelen,
2691 uint32_t parent_lkid,
2692 void (*ast) (void *astarg),
2693 void *astarg,
David Teigland3bcd3682006-02-23 09:56:38 +00002694 void (*bast) (void *astarg, int mode))
David Teiglande7fd4172006-01-18 09:30:29 +00002695{
2696 struct dlm_ls *ls;
2697 struct dlm_lkb *lkb;
2698 struct dlm_args args;
2699 int error, convert = flags & DLM_LKF_CONVERT;
2700
2701 ls = dlm_find_lockspace_local(lockspace);
2702 if (!ls)
2703 return -EINVAL;
2704
David Teigland85e86ed2007-05-18 08:58:15 -05002705 dlm_lock_recovery(ls);
David Teiglande7fd4172006-01-18 09:30:29 +00002706
2707 if (convert)
2708 error = find_lkb(ls, lksb->sb_lkid, &lkb);
2709 else
2710 error = create_lkb(ls, &lkb);
2711
2712 if (error)
2713 goto out;
2714
David Teiglandd7db9232007-05-18 09:00:32 -05002715 error = set_lock_args(mode, lksb, flags, namelen, 0, ast,
David Teigland3bcd3682006-02-23 09:56:38 +00002716 astarg, bast, &args);
David Teiglande7fd4172006-01-18 09:30:29 +00002717 if (error)
2718 goto out_put;
2719
2720 if (convert)
2721 error = convert_lock(ls, lkb, &args);
2722 else
2723 error = request_lock(ls, lkb, name, namelen, &args);
2724
2725 if (error == -EINPROGRESS)
2726 error = 0;
2727 out_put:
2728 if (convert || error)
David Teiglandb3f58d82006-02-28 11:16:37 -05002729 __put_lkb(ls, lkb);
David Teiglandc85d65e2007-05-18 09:01:26 -05002730 if (error == -EAGAIN || error == -EDEADLK)
David Teiglande7fd4172006-01-18 09:30:29 +00002731 error = 0;
2732 out:
David Teigland85e86ed2007-05-18 08:58:15 -05002733 dlm_unlock_recovery(ls);
David Teiglande7fd4172006-01-18 09:30:29 +00002734 dlm_put_lockspace(ls);
2735 return error;
2736}
2737
2738int dlm_unlock(dlm_lockspace_t *lockspace,
2739 uint32_t lkid,
2740 uint32_t flags,
2741 struct dlm_lksb *lksb,
2742 void *astarg)
2743{
2744 struct dlm_ls *ls;
2745 struct dlm_lkb *lkb;
2746 struct dlm_args args;
2747 int error;
2748
2749 ls = dlm_find_lockspace_local(lockspace);
2750 if (!ls)
2751 return -EINVAL;
2752
David Teigland85e86ed2007-05-18 08:58:15 -05002753 dlm_lock_recovery(ls);
David Teiglande7fd4172006-01-18 09:30:29 +00002754
2755 error = find_lkb(ls, lkid, &lkb);
2756 if (error)
2757 goto out;
2758
2759 error = set_unlock_args(flags, astarg, &args);
2760 if (error)
2761 goto out_put;
2762
2763 if (flags & DLM_LKF_CANCEL)
2764 error = cancel_lock(ls, lkb, &args);
2765 else
2766 error = unlock_lock(ls, lkb, &args);
2767
2768 if (error == -DLM_EUNLOCK || error == -DLM_ECANCEL)
2769 error = 0;
David Teiglandef0c2bb2007-03-28 09:56:46 -05002770 if (error == -EBUSY && (flags & (DLM_LKF_CANCEL | DLM_LKF_FORCEUNLOCK)))
2771 error = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00002772 out_put:
David Teiglandb3f58d82006-02-28 11:16:37 -05002773 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00002774 out:
David Teigland85e86ed2007-05-18 08:58:15 -05002775 dlm_unlock_recovery(ls);
David Teiglande7fd4172006-01-18 09:30:29 +00002776 dlm_put_lockspace(ls);
2777 return error;
2778}
2779
2780/*
2781 * send/receive routines for remote operations and replies
2782 *
2783 * send_args
2784 * send_common
2785 * send_request receive_request
2786 * send_convert receive_convert
2787 * send_unlock receive_unlock
2788 * send_cancel receive_cancel
2789 * send_grant receive_grant
2790 * send_bast receive_bast
2791 * send_lookup receive_lookup
2792 * send_remove receive_remove
2793 *
2794 * send_common_reply
2795 * receive_request_reply send_request_reply
2796 * receive_convert_reply send_convert_reply
2797 * receive_unlock_reply send_unlock_reply
2798 * receive_cancel_reply send_cancel_reply
2799 * receive_lookup_reply send_lookup_reply
2800 */
2801
David Teigland7e4dac32007-04-02 09:06:41 -05002802static int _create_message(struct dlm_ls *ls, int mb_len,
2803 int to_nodeid, int mstype,
2804 struct dlm_message **ms_ret,
2805 struct dlm_mhandle **mh_ret)
2806{
2807 struct dlm_message *ms;
2808 struct dlm_mhandle *mh;
2809 char *mb;
2810
2811 /* get_buffer gives us a message handle (mh) that we need to
2812 pass into lowcomms_commit and a message buffer (mb) that we
2813 write our data into */
2814
David Teigland573c24c2009-11-30 16:34:43 -06002815 mh = dlm_lowcomms_get_buffer(to_nodeid, mb_len, GFP_NOFS, &mb);
David Teigland7e4dac32007-04-02 09:06:41 -05002816 if (!mh)
2817 return -ENOBUFS;
2818
2819 memset(mb, 0, mb_len);
2820
2821 ms = (struct dlm_message *) mb;
2822
2823 ms->m_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
2824 ms->m_header.h_lockspace = ls->ls_global_id;
2825 ms->m_header.h_nodeid = dlm_our_nodeid();
2826 ms->m_header.h_length = mb_len;
2827 ms->m_header.h_cmd = DLM_MSG;
2828
2829 ms->m_type = mstype;
2830
2831 *mh_ret = mh;
2832 *ms_ret = ms;
2833 return 0;
2834}
2835
David Teiglande7fd4172006-01-18 09:30:29 +00002836static int create_message(struct dlm_rsb *r, struct dlm_lkb *lkb,
2837 int to_nodeid, int mstype,
2838 struct dlm_message **ms_ret,
2839 struct dlm_mhandle **mh_ret)
2840{
David Teiglande7fd4172006-01-18 09:30:29 +00002841 int mb_len = sizeof(struct dlm_message);
2842
2843 switch (mstype) {
2844 case DLM_MSG_REQUEST:
2845 case DLM_MSG_LOOKUP:
2846 case DLM_MSG_REMOVE:
2847 mb_len += r->res_length;
2848 break;
2849 case DLM_MSG_CONVERT:
2850 case DLM_MSG_UNLOCK:
2851 case DLM_MSG_REQUEST_REPLY:
2852 case DLM_MSG_CONVERT_REPLY:
2853 case DLM_MSG_GRANT:
2854 if (lkb && lkb->lkb_lvbptr)
2855 mb_len += r->res_ls->ls_lvblen;
2856 break;
2857 }
2858
David Teigland7e4dac32007-04-02 09:06:41 -05002859 return _create_message(r->res_ls, mb_len, to_nodeid, mstype,
2860 ms_ret, mh_ret);
David Teiglande7fd4172006-01-18 09:30:29 +00002861}
2862
2863/* further lowcomms enhancements or alternate implementations may make
2864 the return value from this function useful at some point */
2865
2866static int send_message(struct dlm_mhandle *mh, struct dlm_message *ms)
2867{
2868 dlm_message_out(ms);
2869 dlm_lowcomms_commit_buffer(mh);
2870 return 0;
2871}
2872
2873static void send_args(struct dlm_rsb *r, struct dlm_lkb *lkb,
2874 struct dlm_message *ms)
2875{
2876 ms->m_nodeid = lkb->lkb_nodeid;
2877 ms->m_pid = lkb->lkb_ownpid;
2878 ms->m_lkid = lkb->lkb_id;
2879 ms->m_remid = lkb->lkb_remid;
2880 ms->m_exflags = lkb->lkb_exflags;
2881 ms->m_sbflags = lkb->lkb_sbflags;
2882 ms->m_flags = lkb->lkb_flags;
2883 ms->m_lvbseq = lkb->lkb_lvbseq;
2884 ms->m_status = lkb->lkb_status;
2885 ms->m_grmode = lkb->lkb_grmode;
2886 ms->m_rqmode = lkb->lkb_rqmode;
2887 ms->m_hash = r->res_hash;
2888
2889 /* m_result and m_bastmode are set from function args,
2890 not from lkb fields */
2891
David Teiglande5dae542008-02-06 00:35:45 -06002892 if (lkb->lkb_bastfn)
David Teigland8304d6f2011-02-21 14:58:21 -06002893 ms->m_asts |= DLM_CB_BAST;
David Teiglande5dae542008-02-06 00:35:45 -06002894 if (lkb->lkb_astfn)
David Teigland8304d6f2011-02-21 14:58:21 -06002895 ms->m_asts |= DLM_CB_CAST;
David Teiglande7fd4172006-01-18 09:30:29 +00002896
David Teiglandda49f362006-12-13 10:38:45 -06002897 /* compare with switch in create_message; send_remove() doesn't
2898 use send_args() */
2899
2900 switch (ms->m_type) {
2901 case DLM_MSG_REQUEST:
2902 case DLM_MSG_LOOKUP:
David Teiglande7fd4172006-01-18 09:30:29 +00002903 memcpy(ms->m_extra, r->res_name, r->res_length);
David Teiglandda49f362006-12-13 10:38:45 -06002904 break;
2905 case DLM_MSG_CONVERT:
2906 case DLM_MSG_UNLOCK:
2907 case DLM_MSG_REQUEST_REPLY:
2908 case DLM_MSG_CONVERT_REPLY:
2909 case DLM_MSG_GRANT:
2910 if (!lkb->lkb_lvbptr)
2911 break;
David Teiglande7fd4172006-01-18 09:30:29 +00002912 memcpy(ms->m_extra, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
David Teiglandda49f362006-12-13 10:38:45 -06002913 break;
2914 }
David Teiglande7fd4172006-01-18 09:30:29 +00002915}
2916
2917static int send_common(struct dlm_rsb *r, struct dlm_lkb *lkb, int mstype)
2918{
2919 struct dlm_message *ms;
2920 struct dlm_mhandle *mh;
2921 int to_nodeid, error;
2922
David Teiglandc6ff6692011-03-28 14:17:26 -05002923 to_nodeid = r->res_nodeid;
2924
2925 error = add_to_waiters(lkb, mstype, to_nodeid);
David Teiglandef0c2bb2007-03-28 09:56:46 -05002926 if (error)
2927 return error;
David Teiglande7fd4172006-01-18 09:30:29 +00002928
David Teiglande7fd4172006-01-18 09:30:29 +00002929 error = create_message(r, lkb, to_nodeid, mstype, &ms, &mh);
2930 if (error)
2931 goto fail;
2932
2933 send_args(r, lkb, ms);
2934
2935 error = send_message(mh, ms);
2936 if (error)
2937 goto fail;
2938 return 0;
2939
2940 fail:
David Teiglandef0c2bb2007-03-28 09:56:46 -05002941 remove_from_waiters(lkb, msg_reply_type(mstype));
David Teiglande7fd4172006-01-18 09:30:29 +00002942 return error;
2943}
2944
2945static int send_request(struct dlm_rsb *r, struct dlm_lkb *lkb)
2946{
2947 return send_common(r, lkb, DLM_MSG_REQUEST);
2948}
2949
2950static int send_convert(struct dlm_rsb *r, struct dlm_lkb *lkb)
2951{
2952 int error;
2953
2954 error = send_common(r, lkb, DLM_MSG_CONVERT);
2955
2956 /* down conversions go without a reply from the master */
2957 if (!error && down_conversion(lkb)) {
David Teiglandef0c2bb2007-03-28 09:56:46 -05002958 remove_from_waiters(lkb, DLM_MSG_CONVERT_REPLY);
David Teigland2a7ce0e2011-04-04 15:19:59 -05002959 r->res_ls->ls_stub_ms.m_flags = DLM_IFL_STUB_MS;
David Teiglandef0c2bb2007-03-28 09:56:46 -05002960 r->res_ls->ls_stub_ms.m_type = DLM_MSG_CONVERT_REPLY;
David Teiglande7fd4172006-01-18 09:30:29 +00002961 r->res_ls->ls_stub_ms.m_result = 0;
2962 __receive_convert_reply(r, lkb, &r->res_ls->ls_stub_ms);
2963 }
2964
2965 return error;
2966}
2967
2968/* FIXME: if this lkb is the only lock we hold on the rsb, then set
2969 MASTER_UNCERTAIN to force the next request on the rsb to confirm
2970 that the master is still correct. */
2971
2972static int send_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
2973{
2974 return send_common(r, lkb, DLM_MSG_UNLOCK);
2975}
2976
2977static int send_cancel(struct dlm_rsb *r, struct dlm_lkb *lkb)
2978{
2979 return send_common(r, lkb, DLM_MSG_CANCEL);
2980}
2981
2982static int send_grant(struct dlm_rsb *r, struct dlm_lkb *lkb)
2983{
2984 struct dlm_message *ms;
2985 struct dlm_mhandle *mh;
2986 int to_nodeid, error;
2987
2988 to_nodeid = lkb->lkb_nodeid;
2989
2990 error = create_message(r, lkb, to_nodeid, DLM_MSG_GRANT, &ms, &mh);
2991 if (error)
2992 goto out;
2993
2994 send_args(r, lkb, ms);
2995
2996 ms->m_result = 0;
2997
2998 error = send_message(mh, ms);
2999 out:
3000 return error;
3001}
3002
3003static int send_bast(struct dlm_rsb *r, struct dlm_lkb *lkb, int mode)
3004{
3005 struct dlm_message *ms;
3006 struct dlm_mhandle *mh;
3007 int to_nodeid, error;
3008
3009 to_nodeid = lkb->lkb_nodeid;
3010
3011 error = create_message(r, NULL, to_nodeid, DLM_MSG_BAST, &ms, &mh);
3012 if (error)
3013 goto out;
3014
3015 send_args(r, lkb, ms);
3016
3017 ms->m_bastmode = mode;
3018
3019 error = send_message(mh, ms);
3020 out:
3021 return error;
3022}
3023
3024static int send_lookup(struct dlm_rsb *r, struct dlm_lkb *lkb)
3025{
3026 struct dlm_message *ms;
3027 struct dlm_mhandle *mh;
3028 int to_nodeid, error;
3029
David Teiglandc6ff6692011-03-28 14:17:26 -05003030 to_nodeid = dlm_dir_nodeid(r);
3031
3032 error = add_to_waiters(lkb, DLM_MSG_LOOKUP, to_nodeid);
David Teiglandef0c2bb2007-03-28 09:56:46 -05003033 if (error)
3034 return error;
David Teiglande7fd4172006-01-18 09:30:29 +00003035
David Teiglande7fd4172006-01-18 09:30:29 +00003036 error = create_message(r, NULL, to_nodeid, DLM_MSG_LOOKUP, &ms, &mh);
3037 if (error)
3038 goto fail;
3039
3040 send_args(r, lkb, ms);
3041
3042 error = send_message(mh, ms);
3043 if (error)
3044 goto fail;
3045 return 0;
3046
3047 fail:
David Teiglandef0c2bb2007-03-28 09:56:46 -05003048 remove_from_waiters(lkb, DLM_MSG_LOOKUP_REPLY);
David Teiglande7fd4172006-01-18 09:30:29 +00003049 return error;
3050}
3051
3052static int send_remove(struct dlm_rsb *r)
3053{
3054 struct dlm_message *ms;
3055 struct dlm_mhandle *mh;
3056 int to_nodeid, error;
3057
3058 to_nodeid = dlm_dir_nodeid(r);
3059
3060 error = create_message(r, NULL, to_nodeid, DLM_MSG_REMOVE, &ms, &mh);
3061 if (error)
3062 goto out;
3063
3064 memcpy(ms->m_extra, r->res_name, r->res_length);
3065 ms->m_hash = r->res_hash;
3066
3067 error = send_message(mh, ms);
3068 out:
3069 return error;
3070}
3071
3072static int send_common_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
3073 int mstype, int rv)
3074{
3075 struct dlm_message *ms;
3076 struct dlm_mhandle *mh;
3077 int to_nodeid, error;
3078
3079 to_nodeid = lkb->lkb_nodeid;
3080
3081 error = create_message(r, lkb, to_nodeid, mstype, &ms, &mh);
3082 if (error)
3083 goto out;
3084
3085 send_args(r, lkb, ms);
3086
3087 ms->m_result = rv;
3088
3089 error = send_message(mh, ms);
3090 out:
3091 return error;
3092}
3093
3094static int send_request_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
3095{
3096 return send_common_reply(r, lkb, DLM_MSG_REQUEST_REPLY, rv);
3097}
3098
3099static int send_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
3100{
3101 return send_common_reply(r, lkb, DLM_MSG_CONVERT_REPLY, rv);
3102}
3103
3104static int send_unlock_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
3105{
3106 return send_common_reply(r, lkb, DLM_MSG_UNLOCK_REPLY, rv);
3107}
3108
3109static int send_cancel_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
3110{
3111 return send_common_reply(r, lkb, DLM_MSG_CANCEL_REPLY, rv);
3112}
3113
3114static int send_lookup_reply(struct dlm_ls *ls, struct dlm_message *ms_in,
3115 int ret_nodeid, int rv)
3116{
3117 struct dlm_rsb *r = &ls->ls_stub_rsb;
3118 struct dlm_message *ms;
3119 struct dlm_mhandle *mh;
3120 int error, nodeid = ms_in->m_header.h_nodeid;
3121
3122 error = create_message(r, NULL, nodeid, DLM_MSG_LOOKUP_REPLY, &ms, &mh);
3123 if (error)
3124 goto out;
3125
3126 ms->m_lkid = ms_in->m_lkid;
3127 ms->m_result = rv;
3128 ms->m_nodeid = ret_nodeid;
3129
3130 error = send_message(mh, ms);
3131 out:
3132 return error;
3133}
3134
3135/* which args we save from a received message depends heavily on the type
3136 of message, unlike the send side where we can safely send everything about
3137 the lkb for any type of message */
3138
3139static void receive_flags(struct dlm_lkb *lkb, struct dlm_message *ms)
3140{
3141 lkb->lkb_exflags = ms->m_exflags;
David Teigland6f90a8b12006-11-10 14:16:27 -06003142 lkb->lkb_sbflags = ms->m_sbflags;
David Teiglande7fd4172006-01-18 09:30:29 +00003143 lkb->lkb_flags = (lkb->lkb_flags & 0xFFFF0000) |
3144 (ms->m_flags & 0x0000FFFF);
3145}
3146
3147static void receive_flags_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
3148{
David Teigland2a7ce0e2011-04-04 15:19:59 -05003149 if (ms->m_flags == DLM_IFL_STUB_MS)
3150 return;
3151
David Teiglande7fd4172006-01-18 09:30:29 +00003152 lkb->lkb_sbflags = ms->m_sbflags;
3153 lkb->lkb_flags = (lkb->lkb_flags & 0xFFFF0000) |
3154 (ms->m_flags & 0x0000FFFF);
3155}
3156
3157static int receive_extralen(struct dlm_message *ms)
3158{
3159 return (ms->m_header.h_length - sizeof(struct dlm_message));
3160}
3161
David Teiglande7fd4172006-01-18 09:30:29 +00003162static int receive_lvb(struct dlm_ls *ls, struct dlm_lkb *lkb,
3163 struct dlm_message *ms)
3164{
3165 int len;
3166
3167 if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
3168 if (!lkb->lkb_lvbptr)
David Teigland52bda2b2007-11-07 09:06:49 -06003169 lkb->lkb_lvbptr = dlm_allocate_lvb(ls);
David Teiglande7fd4172006-01-18 09:30:29 +00003170 if (!lkb->lkb_lvbptr)
3171 return -ENOMEM;
3172 len = receive_extralen(ms);
Al Viroa9cc9152008-01-26 00:02:29 -05003173 if (len > DLM_RESNAME_MAXLEN)
3174 len = DLM_RESNAME_MAXLEN;
David Teiglande7fd4172006-01-18 09:30:29 +00003175 memcpy(lkb->lkb_lvbptr, ms->m_extra, len);
3176 }
3177 return 0;
3178}
3179
David Teiglande5dae542008-02-06 00:35:45 -06003180static void fake_bastfn(void *astparam, int mode)
3181{
3182 log_print("fake_bastfn should not be called");
3183}
3184
3185static void fake_astfn(void *astparam)
3186{
3187 log_print("fake_astfn should not be called");
3188}
3189
David Teiglande7fd4172006-01-18 09:30:29 +00003190static int receive_request_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
3191 struct dlm_message *ms)
3192{
3193 lkb->lkb_nodeid = ms->m_header.h_nodeid;
3194 lkb->lkb_ownpid = ms->m_pid;
3195 lkb->lkb_remid = ms->m_lkid;
3196 lkb->lkb_grmode = DLM_LOCK_IV;
3197 lkb->lkb_rqmode = ms->m_rqmode;
David Teiglande5dae542008-02-06 00:35:45 -06003198
David Teigland8304d6f2011-02-21 14:58:21 -06003199 lkb->lkb_bastfn = (ms->m_asts & DLM_CB_BAST) ? &fake_bastfn : NULL;
3200 lkb->lkb_astfn = (ms->m_asts & DLM_CB_CAST) ? &fake_astfn : NULL;
David Teiglande7fd4172006-01-18 09:30:29 +00003201
David Teigland8d07fd52006-12-13 10:39:20 -06003202 if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
3203 /* lkb was just created so there won't be an lvb yet */
David Teigland52bda2b2007-11-07 09:06:49 -06003204 lkb->lkb_lvbptr = dlm_allocate_lvb(ls);
David Teigland8d07fd52006-12-13 10:39:20 -06003205 if (!lkb->lkb_lvbptr)
3206 return -ENOMEM;
3207 }
David Teiglande7fd4172006-01-18 09:30:29 +00003208
3209 return 0;
3210}
3211
3212static int receive_convert_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
3213 struct dlm_message *ms)
3214{
David Teiglande7fd4172006-01-18 09:30:29 +00003215 if (lkb->lkb_status != DLM_LKSTS_GRANTED)
3216 return -EBUSY;
3217
David Teiglande7fd4172006-01-18 09:30:29 +00003218 if (receive_lvb(ls, lkb, ms))
3219 return -ENOMEM;
3220
3221 lkb->lkb_rqmode = ms->m_rqmode;
3222 lkb->lkb_lvbseq = ms->m_lvbseq;
3223
3224 return 0;
3225}
3226
3227static int receive_unlock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
3228 struct dlm_message *ms)
3229{
David Teiglande7fd4172006-01-18 09:30:29 +00003230 if (receive_lvb(ls, lkb, ms))
3231 return -ENOMEM;
3232 return 0;
3233}
3234
3235/* We fill in the stub-lkb fields with the info that send_xxxx_reply()
3236 uses to send a reply and that the remote end uses to process the reply. */
3237
3238static void setup_stub_lkb(struct dlm_ls *ls, struct dlm_message *ms)
3239{
3240 struct dlm_lkb *lkb = &ls->ls_stub_lkb;
3241 lkb->lkb_nodeid = ms->m_header.h_nodeid;
3242 lkb->lkb_remid = ms->m_lkid;
3243}
3244
David Teiglandc54e04b2008-01-09 09:59:41 -06003245/* This is called after the rsb is locked so that we can safely inspect
3246 fields in the lkb. */
3247
3248static int validate_message(struct dlm_lkb *lkb, struct dlm_message *ms)
3249{
3250 int from = ms->m_header.h_nodeid;
3251 int error = 0;
3252
3253 switch (ms->m_type) {
3254 case DLM_MSG_CONVERT:
3255 case DLM_MSG_UNLOCK:
3256 case DLM_MSG_CANCEL:
3257 if (!is_master_copy(lkb) || lkb->lkb_nodeid != from)
3258 error = -EINVAL;
3259 break;
3260
3261 case DLM_MSG_CONVERT_REPLY:
3262 case DLM_MSG_UNLOCK_REPLY:
3263 case DLM_MSG_CANCEL_REPLY:
3264 case DLM_MSG_GRANT:
3265 case DLM_MSG_BAST:
3266 if (!is_process_copy(lkb) || lkb->lkb_nodeid != from)
3267 error = -EINVAL;
3268 break;
3269
3270 case DLM_MSG_REQUEST_REPLY:
3271 if (!is_process_copy(lkb))
3272 error = -EINVAL;
3273 else if (lkb->lkb_nodeid != -1 && lkb->lkb_nodeid != from)
3274 error = -EINVAL;
3275 break;
3276
3277 default:
3278 error = -EINVAL;
3279 }
3280
3281 if (error)
3282 log_error(lkb->lkb_resource->res_ls,
3283 "ignore invalid message %d from %d %x %x %x %d",
3284 ms->m_type, from, lkb->lkb_id, lkb->lkb_remid,
3285 lkb->lkb_flags, lkb->lkb_nodeid);
3286 return error;
3287}
3288
David Teiglande7fd4172006-01-18 09:30:29 +00003289static void receive_request(struct dlm_ls *ls, struct dlm_message *ms)
3290{
3291 struct dlm_lkb *lkb;
3292 struct dlm_rsb *r;
3293 int error, namelen;
3294
3295 error = create_lkb(ls, &lkb);
3296 if (error)
3297 goto fail;
3298
3299 receive_flags(lkb, ms);
3300 lkb->lkb_flags |= DLM_IFL_MSTCPY;
3301 error = receive_request_args(ls, lkb, ms);
3302 if (error) {
David Teiglandb3f58d82006-02-28 11:16:37 -05003303 __put_lkb(ls, lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003304 goto fail;
3305 }
3306
3307 namelen = receive_extralen(ms);
3308
3309 error = find_rsb(ls, ms->m_extra, namelen, R_MASTER, &r);
3310 if (error) {
David Teiglandb3f58d82006-02-28 11:16:37 -05003311 __put_lkb(ls, lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003312 goto fail;
3313 }
3314
3315 lock_rsb(r);
3316
3317 attach_lkb(r, lkb);
3318 error = do_request(r, lkb);
3319 send_request_reply(r, lkb, error);
David Teiglandcf6620a2010-02-24 11:59:23 -06003320 do_request_effects(r, lkb, error);
David Teiglande7fd4172006-01-18 09:30:29 +00003321
3322 unlock_rsb(r);
3323 put_rsb(r);
3324
3325 if (error == -EINPROGRESS)
3326 error = 0;
3327 if (error)
David Teiglandb3f58d82006-02-28 11:16:37 -05003328 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003329 return;
3330
3331 fail:
3332 setup_stub_lkb(ls, ms);
3333 send_request_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
3334}
3335
3336static void receive_convert(struct dlm_ls *ls, struct dlm_message *ms)
3337{
3338 struct dlm_lkb *lkb;
3339 struct dlm_rsb *r;
David Teigland90135922006-01-20 08:47:07 +00003340 int error, reply = 1;
David Teiglande7fd4172006-01-18 09:30:29 +00003341
3342 error = find_lkb(ls, ms->m_remid, &lkb);
3343 if (error)
3344 goto fail;
3345
3346 r = lkb->lkb_resource;
3347
3348 hold_rsb(r);
3349 lock_rsb(r);
3350
David Teiglandc54e04b2008-01-09 09:59:41 -06003351 error = validate_message(lkb, ms);
3352 if (error)
3353 goto out;
3354
David Teiglande7fd4172006-01-18 09:30:29 +00003355 receive_flags(lkb, ms);
David Teiglandcf6620a2010-02-24 11:59:23 -06003356
David Teiglande7fd4172006-01-18 09:30:29 +00003357 error = receive_convert_args(ls, lkb, ms);
David Teiglandcf6620a2010-02-24 11:59:23 -06003358 if (error) {
3359 send_convert_reply(r, lkb, error);
3360 goto out;
3361 }
3362
David Teiglande7fd4172006-01-18 09:30:29 +00003363 reply = !down_conversion(lkb);
3364
3365 error = do_convert(r, lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003366 if (reply)
3367 send_convert_reply(r, lkb, error);
David Teiglandcf6620a2010-02-24 11:59:23 -06003368 do_convert_effects(r, lkb, error);
David Teiglandc54e04b2008-01-09 09:59:41 -06003369 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003370 unlock_rsb(r);
3371 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05003372 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003373 return;
3374
3375 fail:
3376 setup_stub_lkb(ls, ms);
3377 send_convert_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
3378}
3379
3380static void receive_unlock(struct dlm_ls *ls, struct dlm_message *ms)
3381{
3382 struct dlm_lkb *lkb;
3383 struct dlm_rsb *r;
3384 int error;
3385
3386 error = find_lkb(ls, ms->m_remid, &lkb);
3387 if (error)
3388 goto fail;
3389
3390 r = lkb->lkb_resource;
3391
3392 hold_rsb(r);
3393 lock_rsb(r);
3394
David Teiglandc54e04b2008-01-09 09:59:41 -06003395 error = validate_message(lkb, ms);
David Teiglande7fd4172006-01-18 09:30:29 +00003396 if (error)
3397 goto out;
3398
David Teiglandc54e04b2008-01-09 09:59:41 -06003399 receive_flags(lkb, ms);
David Teiglandcf6620a2010-02-24 11:59:23 -06003400
David Teiglandc54e04b2008-01-09 09:59:41 -06003401 error = receive_unlock_args(ls, lkb, ms);
David Teiglandcf6620a2010-02-24 11:59:23 -06003402 if (error) {
3403 send_unlock_reply(r, lkb, error);
3404 goto out;
3405 }
David Teiglande7fd4172006-01-18 09:30:29 +00003406
David Teiglandc54e04b2008-01-09 09:59:41 -06003407 error = do_unlock(r, lkb);
David Teiglandc54e04b2008-01-09 09:59:41 -06003408 send_unlock_reply(r, lkb, error);
David Teiglandcf6620a2010-02-24 11:59:23 -06003409 do_unlock_effects(r, lkb, error);
David Teiglandc54e04b2008-01-09 09:59:41 -06003410 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003411 unlock_rsb(r);
3412 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05003413 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003414 return;
3415
3416 fail:
3417 setup_stub_lkb(ls, ms);
3418 send_unlock_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
3419}
3420
3421static void receive_cancel(struct dlm_ls *ls, struct dlm_message *ms)
3422{
3423 struct dlm_lkb *lkb;
3424 struct dlm_rsb *r;
3425 int error;
3426
3427 error = find_lkb(ls, ms->m_remid, &lkb);
3428 if (error)
3429 goto fail;
3430
3431 receive_flags(lkb, ms);
3432
3433 r = lkb->lkb_resource;
3434
3435 hold_rsb(r);
3436 lock_rsb(r);
3437
David Teiglandc54e04b2008-01-09 09:59:41 -06003438 error = validate_message(lkb, ms);
3439 if (error)
3440 goto out;
3441
David Teiglande7fd4172006-01-18 09:30:29 +00003442 error = do_cancel(r, lkb);
3443 send_cancel_reply(r, lkb, error);
David Teiglandcf6620a2010-02-24 11:59:23 -06003444 do_cancel_effects(r, lkb, error);
David Teiglandc54e04b2008-01-09 09:59:41 -06003445 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003446 unlock_rsb(r);
3447 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05003448 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003449 return;
3450
3451 fail:
3452 setup_stub_lkb(ls, ms);
3453 send_cancel_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
3454}
3455
3456static void receive_grant(struct dlm_ls *ls, struct dlm_message *ms)
3457{
3458 struct dlm_lkb *lkb;
3459 struct dlm_rsb *r;
3460 int error;
3461
3462 error = find_lkb(ls, ms->m_remid, &lkb);
3463 if (error) {
David Teiglandc54e04b2008-01-09 09:59:41 -06003464 log_debug(ls, "receive_grant from %d no lkb %x",
3465 ms->m_header.h_nodeid, ms->m_remid);
David Teiglande7fd4172006-01-18 09:30:29 +00003466 return;
3467 }
David Teiglande7fd4172006-01-18 09:30:29 +00003468
3469 r = lkb->lkb_resource;
3470
3471 hold_rsb(r);
3472 lock_rsb(r);
3473
David Teiglandc54e04b2008-01-09 09:59:41 -06003474 error = validate_message(lkb, ms);
3475 if (error)
3476 goto out;
3477
David Teiglande7fd4172006-01-18 09:30:29 +00003478 receive_flags_reply(lkb, ms);
David Teigland7d3c1fe2007-04-19 10:30:41 -05003479 if (is_altmode(lkb))
3480 munge_altmode(lkb, ms);
David Teiglande7fd4172006-01-18 09:30:29 +00003481 grant_lock_pc(r, lkb, ms);
3482 queue_cast(r, lkb, 0);
David Teiglandc54e04b2008-01-09 09:59:41 -06003483 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003484 unlock_rsb(r);
3485 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05003486 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003487}
3488
3489static void receive_bast(struct dlm_ls *ls, struct dlm_message *ms)
3490{
3491 struct dlm_lkb *lkb;
3492 struct dlm_rsb *r;
3493 int error;
3494
3495 error = find_lkb(ls, ms->m_remid, &lkb);
3496 if (error) {
David Teiglandc54e04b2008-01-09 09:59:41 -06003497 log_debug(ls, "receive_bast from %d no lkb %x",
3498 ms->m_header.h_nodeid, ms->m_remid);
David Teiglande7fd4172006-01-18 09:30:29 +00003499 return;
3500 }
David Teiglande7fd4172006-01-18 09:30:29 +00003501
3502 r = lkb->lkb_resource;
3503
3504 hold_rsb(r);
3505 lock_rsb(r);
3506
David Teiglandc54e04b2008-01-09 09:59:41 -06003507 error = validate_message(lkb, ms);
3508 if (error)
3509 goto out;
David Teiglande7fd4172006-01-18 09:30:29 +00003510
David Teiglandc54e04b2008-01-09 09:59:41 -06003511 queue_bast(r, lkb, ms->m_bastmode);
3512 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003513 unlock_rsb(r);
3514 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05003515 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003516}
3517
3518static void receive_lookup(struct dlm_ls *ls, struct dlm_message *ms)
3519{
3520 int len, error, ret_nodeid, dir_nodeid, from_nodeid, our_nodeid;
3521
3522 from_nodeid = ms->m_header.h_nodeid;
3523 our_nodeid = dlm_our_nodeid();
3524
3525 len = receive_extralen(ms);
3526
3527 dir_nodeid = dlm_hash2nodeid(ls, ms->m_hash);
3528 if (dir_nodeid != our_nodeid) {
3529 log_error(ls, "lookup dir_nodeid %d from %d",
3530 dir_nodeid, from_nodeid);
3531 error = -EINVAL;
3532 ret_nodeid = -1;
3533 goto out;
3534 }
3535
3536 error = dlm_dir_lookup(ls, from_nodeid, ms->m_extra, len, &ret_nodeid);
3537
3538 /* Optimization: we're master so treat lookup as a request */
3539 if (!error && ret_nodeid == our_nodeid) {
3540 receive_request(ls, ms);
3541 return;
3542 }
3543 out:
3544 send_lookup_reply(ls, ms, ret_nodeid, error);
3545}
3546
3547static void receive_remove(struct dlm_ls *ls, struct dlm_message *ms)
3548{
3549 int len, dir_nodeid, from_nodeid;
3550
3551 from_nodeid = ms->m_header.h_nodeid;
3552
3553 len = receive_extralen(ms);
3554
3555 dir_nodeid = dlm_hash2nodeid(ls, ms->m_hash);
3556 if (dir_nodeid != dlm_our_nodeid()) {
3557 log_error(ls, "remove dir entry dir_nodeid %d from %d",
3558 dir_nodeid, from_nodeid);
3559 return;
3560 }
3561
3562 dlm_dir_remove_entry(ls, from_nodeid, ms->m_extra, len);
3563}
3564
David Teigland84991372007-03-30 15:02:40 -05003565static void receive_purge(struct dlm_ls *ls, struct dlm_message *ms)
3566{
3567 do_purge(ls, ms->m_nodeid, ms->m_pid);
3568}
3569
David Teiglande7fd4172006-01-18 09:30:29 +00003570static void receive_request_reply(struct dlm_ls *ls, struct dlm_message *ms)
3571{
3572 struct dlm_lkb *lkb;
3573 struct dlm_rsb *r;
David Teiglandef0c2bb2007-03-28 09:56:46 -05003574 int error, mstype, result;
David Teiglande7fd4172006-01-18 09:30:29 +00003575
3576 error = find_lkb(ls, ms->m_remid, &lkb);
3577 if (error) {
David Teiglandc54e04b2008-01-09 09:59:41 -06003578 log_debug(ls, "receive_request_reply from %d no lkb %x",
3579 ms->m_header.h_nodeid, ms->m_remid);
David Teiglande7fd4172006-01-18 09:30:29 +00003580 return;
3581 }
David Teiglande7fd4172006-01-18 09:30:29 +00003582
David Teiglande7fd4172006-01-18 09:30:29 +00003583 r = lkb->lkb_resource;
3584 hold_rsb(r);
3585 lock_rsb(r);
3586
David Teiglandc54e04b2008-01-09 09:59:41 -06003587 error = validate_message(lkb, ms);
3588 if (error)
3589 goto out;
3590
David Teiglandef0c2bb2007-03-28 09:56:46 -05003591 mstype = lkb->lkb_wait_type;
3592 error = remove_from_waiters(lkb, DLM_MSG_REQUEST_REPLY);
3593 if (error)
3594 goto out;
3595
David Teiglande7fd4172006-01-18 09:30:29 +00003596 /* Optimization: the dir node was also the master, so it took our
3597 lookup as a request and sent request reply instead of lookup reply */
3598 if (mstype == DLM_MSG_LOOKUP) {
3599 r->res_nodeid = ms->m_header.h_nodeid;
3600 lkb->lkb_nodeid = r->res_nodeid;
3601 }
3602
David Teiglandef0c2bb2007-03-28 09:56:46 -05003603 /* this is the value returned from do_request() on the master */
3604 result = ms->m_result;
3605
3606 switch (result) {
David Teiglande7fd4172006-01-18 09:30:29 +00003607 case -EAGAIN:
David Teiglandef0c2bb2007-03-28 09:56:46 -05003608 /* request would block (be queued) on remote master */
David Teiglande7fd4172006-01-18 09:30:29 +00003609 queue_cast(r, lkb, -EAGAIN);
3610 confirm_master(r, -EAGAIN);
David Teiglandef0c2bb2007-03-28 09:56:46 -05003611 unhold_lkb(lkb); /* undoes create_lkb() */
David Teiglande7fd4172006-01-18 09:30:29 +00003612 break;
3613
3614 case -EINPROGRESS:
3615 case 0:
3616 /* request was queued or granted on remote master */
3617 receive_flags_reply(lkb, ms);
3618 lkb->lkb_remid = ms->m_lkid;
David Teigland7d3c1fe2007-04-19 10:30:41 -05003619 if (is_altmode(lkb))
3620 munge_altmode(lkb, ms);
David Teigland3ae1acf2007-05-18 08:59:31 -05003621 if (result) {
David Teiglande7fd4172006-01-18 09:30:29 +00003622 add_lkb(r, lkb, DLM_LKSTS_WAITING);
David Teigland3ae1acf2007-05-18 08:59:31 -05003623 add_timeout(lkb);
3624 } else {
David Teiglande7fd4172006-01-18 09:30:29 +00003625 grant_lock_pc(r, lkb, ms);
3626 queue_cast(r, lkb, 0);
3627 }
David Teiglandef0c2bb2007-03-28 09:56:46 -05003628 confirm_master(r, result);
David Teiglande7fd4172006-01-18 09:30:29 +00003629 break;
3630
David Teigland597d0ca2006-07-12 16:44:04 -05003631 case -EBADR:
David Teiglande7fd4172006-01-18 09:30:29 +00003632 case -ENOTBLK:
3633 /* find_rsb failed to find rsb or rsb wasn't master */
David Teiglandef0c2bb2007-03-28 09:56:46 -05003634 log_debug(ls, "receive_request_reply %x %x master diff %d %d",
3635 lkb->lkb_id, lkb->lkb_flags, r->res_nodeid, result);
David Teiglande7fd4172006-01-18 09:30:29 +00003636 r->res_nodeid = -1;
3637 lkb->lkb_nodeid = -1;
David Teiglandef0c2bb2007-03-28 09:56:46 -05003638
3639 if (is_overlap(lkb)) {
3640 /* we'll ignore error in cancel/unlock reply */
3641 queue_cast_overlap(r, lkb);
David Teiglandaec64e12008-01-08 15:37:47 -06003642 confirm_master(r, result);
David Teiglandef0c2bb2007-03-28 09:56:46 -05003643 unhold_lkb(lkb); /* undoes create_lkb() */
3644 } else
3645 _request_lock(r, lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003646 break;
3647
3648 default:
David Teiglandef0c2bb2007-03-28 09:56:46 -05003649 log_error(ls, "receive_request_reply %x error %d",
3650 lkb->lkb_id, result);
David Teiglande7fd4172006-01-18 09:30:29 +00003651 }
3652
David Teiglandef0c2bb2007-03-28 09:56:46 -05003653 if (is_overlap_unlock(lkb) && (result == 0 || result == -EINPROGRESS)) {
3654 log_debug(ls, "receive_request_reply %x result %d unlock",
3655 lkb->lkb_id, result);
3656 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
3657 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
3658 send_unlock(r, lkb);
3659 } else if (is_overlap_cancel(lkb) && (result == -EINPROGRESS)) {
3660 log_debug(ls, "receive_request_reply %x cancel", lkb->lkb_id);
3661 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
3662 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
3663 send_cancel(r, lkb);
3664 } else {
3665 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
3666 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
3667 }
3668 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003669 unlock_rsb(r);
3670 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05003671 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003672}
3673
3674static void __receive_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
3675 struct dlm_message *ms)
3676{
David Teiglande7fd4172006-01-18 09:30:29 +00003677 /* this is the value returned from do_convert() on the master */
David Teiglandef0c2bb2007-03-28 09:56:46 -05003678 switch (ms->m_result) {
David Teiglande7fd4172006-01-18 09:30:29 +00003679 case -EAGAIN:
3680 /* convert would block (be queued) on remote master */
3681 queue_cast(r, lkb, -EAGAIN);
3682 break;
3683
David Teiglandc85d65e2007-05-18 09:01:26 -05003684 case -EDEADLK:
3685 receive_flags_reply(lkb, ms);
3686 revert_lock_pc(r, lkb);
3687 queue_cast(r, lkb, -EDEADLK);
3688 break;
3689
David Teiglande7fd4172006-01-18 09:30:29 +00003690 case -EINPROGRESS:
3691 /* convert was queued on remote master */
David Teigland7d3c1fe2007-04-19 10:30:41 -05003692 receive_flags_reply(lkb, ms);
3693 if (is_demoted(lkb))
David Teigland2a7ce0e2011-04-04 15:19:59 -05003694 munge_demoted(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003695 del_lkb(r, lkb);
3696 add_lkb(r, lkb, DLM_LKSTS_CONVERT);
David Teigland3ae1acf2007-05-18 08:59:31 -05003697 add_timeout(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003698 break;
3699
3700 case 0:
3701 /* convert was granted on remote master */
3702 receive_flags_reply(lkb, ms);
David Teigland7d3c1fe2007-04-19 10:30:41 -05003703 if (is_demoted(lkb))
David Teigland2a7ce0e2011-04-04 15:19:59 -05003704 munge_demoted(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003705 grant_lock_pc(r, lkb, ms);
3706 queue_cast(r, lkb, 0);
3707 break;
3708
3709 default:
David Teiglandef0c2bb2007-03-28 09:56:46 -05003710 log_error(r->res_ls, "receive_convert_reply %x error %d",
3711 lkb->lkb_id, ms->m_result);
David Teiglande7fd4172006-01-18 09:30:29 +00003712 }
3713}
3714
3715static void _receive_convert_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
3716{
3717 struct dlm_rsb *r = lkb->lkb_resource;
David Teiglandef0c2bb2007-03-28 09:56:46 -05003718 int error;
David Teiglande7fd4172006-01-18 09:30:29 +00003719
3720 hold_rsb(r);
3721 lock_rsb(r);
3722
David Teiglandc54e04b2008-01-09 09:59:41 -06003723 error = validate_message(lkb, ms);
3724 if (error)
3725 goto out;
3726
David Teiglandef0c2bb2007-03-28 09:56:46 -05003727 /* stub reply can happen with waiters_mutex held */
3728 error = remove_from_waiters_ms(lkb, ms);
3729 if (error)
3730 goto out;
David Teiglande7fd4172006-01-18 09:30:29 +00003731
David Teiglandef0c2bb2007-03-28 09:56:46 -05003732 __receive_convert_reply(r, lkb, ms);
3733 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003734 unlock_rsb(r);
3735 put_rsb(r);
3736}
3737
3738static void receive_convert_reply(struct dlm_ls *ls, struct dlm_message *ms)
3739{
3740 struct dlm_lkb *lkb;
3741 int error;
3742
3743 error = find_lkb(ls, ms->m_remid, &lkb);
3744 if (error) {
David Teiglandc54e04b2008-01-09 09:59:41 -06003745 log_debug(ls, "receive_convert_reply from %d no lkb %x",
3746 ms->m_header.h_nodeid, ms->m_remid);
David Teiglande7fd4172006-01-18 09:30:29 +00003747 return;
3748 }
David Teiglande7fd4172006-01-18 09:30:29 +00003749
David Teiglande7fd4172006-01-18 09:30:29 +00003750 _receive_convert_reply(lkb, ms);
David Teiglandb3f58d82006-02-28 11:16:37 -05003751 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003752}
3753
3754static void _receive_unlock_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
3755{
3756 struct dlm_rsb *r = lkb->lkb_resource;
David Teiglandef0c2bb2007-03-28 09:56:46 -05003757 int error;
David Teiglande7fd4172006-01-18 09:30:29 +00003758
3759 hold_rsb(r);
3760 lock_rsb(r);
3761
David Teiglandc54e04b2008-01-09 09:59:41 -06003762 error = validate_message(lkb, ms);
3763 if (error)
3764 goto out;
3765
David Teiglandef0c2bb2007-03-28 09:56:46 -05003766 /* stub reply can happen with waiters_mutex held */
3767 error = remove_from_waiters_ms(lkb, ms);
3768 if (error)
3769 goto out;
3770
David Teiglande7fd4172006-01-18 09:30:29 +00003771 /* this is the value returned from do_unlock() on the master */
3772
David Teiglandef0c2bb2007-03-28 09:56:46 -05003773 switch (ms->m_result) {
David Teiglande7fd4172006-01-18 09:30:29 +00003774 case -DLM_EUNLOCK:
3775 receive_flags_reply(lkb, ms);
3776 remove_lock_pc(r, lkb);
3777 queue_cast(r, lkb, -DLM_EUNLOCK);
3778 break;
David Teiglandef0c2bb2007-03-28 09:56:46 -05003779 case -ENOENT:
3780 break;
David Teiglande7fd4172006-01-18 09:30:29 +00003781 default:
David Teiglandef0c2bb2007-03-28 09:56:46 -05003782 log_error(r->res_ls, "receive_unlock_reply %x error %d",
3783 lkb->lkb_id, ms->m_result);
David Teiglande7fd4172006-01-18 09:30:29 +00003784 }
David Teiglandef0c2bb2007-03-28 09:56:46 -05003785 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003786 unlock_rsb(r);
3787 put_rsb(r);
3788}
3789
3790static void receive_unlock_reply(struct dlm_ls *ls, struct dlm_message *ms)
3791{
3792 struct dlm_lkb *lkb;
3793 int error;
3794
3795 error = find_lkb(ls, ms->m_remid, &lkb);
3796 if (error) {
David Teiglandc54e04b2008-01-09 09:59:41 -06003797 log_debug(ls, "receive_unlock_reply from %d no lkb %x",
3798 ms->m_header.h_nodeid, ms->m_remid);
David Teiglande7fd4172006-01-18 09:30:29 +00003799 return;
3800 }
David Teiglande7fd4172006-01-18 09:30:29 +00003801
David Teiglande7fd4172006-01-18 09:30:29 +00003802 _receive_unlock_reply(lkb, ms);
David Teiglandb3f58d82006-02-28 11:16:37 -05003803 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003804}
3805
3806static void _receive_cancel_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
3807{
3808 struct dlm_rsb *r = lkb->lkb_resource;
David Teiglandef0c2bb2007-03-28 09:56:46 -05003809 int error;
David Teiglande7fd4172006-01-18 09:30:29 +00003810
3811 hold_rsb(r);
3812 lock_rsb(r);
3813
David Teiglandc54e04b2008-01-09 09:59:41 -06003814 error = validate_message(lkb, ms);
3815 if (error)
3816 goto out;
3817
David Teiglandef0c2bb2007-03-28 09:56:46 -05003818 /* stub reply can happen with waiters_mutex held */
3819 error = remove_from_waiters_ms(lkb, ms);
3820 if (error)
3821 goto out;
3822
David Teiglande7fd4172006-01-18 09:30:29 +00003823 /* this is the value returned from do_cancel() on the master */
3824
David Teiglandef0c2bb2007-03-28 09:56:46 -05003825 switch (ms->m_result) {
David Teiglande7fd4172006-01-18 09:30:29 +00003826 case -DLM_ECANCEL:
3827 receive_flags_reply(lkb, ms);
3828 revert_lock_pc(r, lkb);
David Teigland84d8cd62007-05-29 08:44:23 -05003829 queue_cast(r, lkb, -DLM_ECANCEL);
David Teiglandef0c2bb2007-03-28 09:56:46 -05003830 break;
3831 case 0:
David Teiglande7fd4172006-01-18 09:30:29 +00003832 break;
3833 default:
David Teiglandef0c2bb2007-03-28 09:56:46 -05003834 log_error(r->res_ls, "receive_cancel_reply %x error %d",
3835 lkb->lkb_id, ms->m_result);
David Teiglande7fd4172006-01-18 09:30:29 +00003836 }
David Teiglandef0c2bb2007-03-28 09:56:46 -05003837 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003838 unlock_rsb(r);
3839 put_rsb(r);
3840}
3841
3842static void receive_cancel_reply(struct dlm_ls *ls, struct dlm_message *ms)
3843{
3844 struct dlm_lkb *lkb;
3845 int error;
3846
3847 error = find_lkb(ls, ms->m_remid, &lkb);
3848 if (error) {
David Teiglandc54e04b2008-01-09 09:59:41 -06003849 log_debug(ls, "receive_cancel_reply from %d no lkb %x",
3850 ms->m_header.h_nodeid, ms->m_remid);
David Teiglande7fd4172006-01-18 09:30:29 +00003851 return;
3852 }
David Teiglande7fd4172006-01-18 09:30:29 +00003853
David Teiglande7fd4172006-01-18 09:30:29 +00003854 _receive_cancel_reply(lkb, ms);
David Teiglandb3f58d82006-02-28 11:16:37 -05003855 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003856}
3857
3858static void receive_lookup_reply(struct dlm_ls *ls, struct dlm_message *ms)
3859{
3860 struct dlm_lkb *lkb;
3861 struct dlm_rsb *r;
3862 int error, ret_nodeid;
3863
3864 error = find_lkb(ls, ms->m_lkid, &lkb);
3865 if (error) {
3866 log_error(ls, "receive_lookup_reply no lkb");
3867 return;
3868 }
3869
David Teiglandef0c2bb2007-03-28 09:56:46 -05003870 /* ms->m_result is the value returned by dlm_dir_lookup on dir node
David Teiglande7fd4172006-01-18 09:30:29 +00003871 FIXME: will a non-zero error ever be returned? */
David Teiglande7fd4172006-01-18 09:30:29 +00003872
3873 r = lkb->lkb_resource;
3874 hold_rsb(r);
3875 lock_rsb(r);
3876
David Teiglandef0c2bb2007-03-28 09:56:46 -05003877 error = remove_from_waiters(lkb, DLM_MSG_LOOKUP_REPLY);
3878 if (error)
3879 goto out;
3880
David Teiglande7fd4172006-01-18 09:30:29 +00003881 ret_nodeid = ms->m_nodeid;
3882 if (ret_nodeid == dlm_our_nodeid()) {
3883 r->res_nodeid = 0;
3884 ret_nodeid = 0;
3885 r->res_first_lkid = 0;
3886 } else {
3887 /* set_master() will copy res_nodeid to lkb_nodeid */
3888 r->res_nodeid = ret_nodeid;
3889 }
3890
David Teiglandef0c2bb2007-03-28 09:56:46 -05003891 if (is_overlap(lkb)) {
3892 log_debug(ls, "receive_lookup_reply %x unlock %x",
3893 lkb->lkb_id, lkb->lkb_flags);
3894 queue_cast_overlap(r, lkb);
3895 unhold_lkb(lkb); /* undoes create_lkb() */
3896 goto out_list;
3897 }
3898
David Teiglande7fd4172006-01-18 09:30:29 +00003899 _request_lock(r, lkb);
3900
David Teiglandef0c2bb2007-03-28 09:56:46 -05003901 out_list:
David Teiglande7fd4172006-01-18 09:30:29 +00003902 if (!ret_nodeid)
3903 process_lookup_list(r);
David Teiglandef0c2bb2007-03-28 09:56:46 -05003904 out:
David Teiglande7fd4172006-01-18 09:30:29 +00003905 unlock_rsb(r);
3906 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05003907 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00003908}
3909
David Teiglandc36258b2007-09-27 15:53:38 -05003910static void _receive_message(struct dlm_ls *ls, struct dlm_message *ms)
David Teiglande7fd4172006-01-18 09:30:29 +00003911{
David Teigland46b43ee2008-01-08 16:24:00 -06003912 if (!dlm_is_member(ls, ms->m_header.h_nodeid)) {
3913 log_debug(ls, "ignore non-member message %d from %d %x %x %d",
3914 ms->m_type, ms->m_header.h_nodeid, ms->m_lkid,
3915 ms->m_remid, ms->m_result);
3916 return;
3917 }
3918
David Teiglande7fd4172006-01-18 09:30:29 +00003919 switch (ms->m_type) {
3920
3921 /* messages sent to a master node */
3922
3923 case DLM_MSG_REQUEST:
3924 receive_request(ls, ms);
3925 break;
3926
3927 case DLM_MSG_CONVERT:
3928 receive_convert(ls, ms);
3929 break;
3930
3931 case DLM_MSG_UNLOCK:
3932 receive_unlock(ls, ms);
3933 break;
3934
3935 case DLM_MSG_CANCEL:
3936 receive_cancel(ls, ms);
3937 break;
3938
3939 /* messages sent from a master node (replies to above) */
3940
3941 case DLM_MSG_REQUEST_REPLY:
3942 receive_request_reply(ls, ms);
3943 break;
3944
3945 case DLM_MSG_CONVERT_REPLY:
3946 receive_convert_reply(ls, ms);
3947 break;
3948
3949 case DLM_MSG_UNLOCK_REPLY:
3950 receive_unlock_reply(ls, ms);
3951 break;
3952
3953 case DLM_MSG_CANCEL_REPLY:
3954 receive_cancel_reply(ls, ms);
3955 break;
3956
3957 /* messages sent from a master node (only two types of async msg) */
3958
3959 case DLM_MSG_GRANT:
3960 receive_grant(ls, ms);
3961 break;
3962
3963 case DLM_MSG_BAST:
3964 receive_bast(ls, ms);
3965 break;
3966
3967 /* messages sent to a dir node */
3968
3969 case DLM_MSG_LOOKUP:
3970 receive_lookup(ls, ms);
3971 break;
3972
3973 case DLM_MSG_REMOVE:
3974 receive_remove(ls, ms);
3975 break;
3976
3977 /* messages sent from a dir node (remove has no reply) */
3978
3979 case DLM_MSG_LOOKUP_REPLY:
3980 receive_lookup_reply(ls, ms);
3981 break;
3982
David Teigland84991372007-03-30 15:02:40 -05003983 /* other messages */
3984
3985 case DLM_MSG_PURGE:
3986 receive_purge(ls, ms);
3987 break;
3988
David Teiglande7fd4172006-01-18 09:30:29 +00003989 default:
3990 log_error(ls, "unknown message type %d", ms->m_type);
3991 }
3992
David Teiglande7fd4172006-01-18 09:30:29 +00003993 dlm_astd_wake();
David Teiglande7fd4172006-01-18 09:30:29 +00003994}
3995
David Teiglandc36258b2007-09-27 15:53:38 -05003996/* If the lockspace is in recovery mode (locking stopped), then normal
3997 messages are saved on the requestqueue for processing after recovery is
3998 done. When not in recovery mode, we wait for dlm_recoverd to drain saved
3999 messages off the requestqueue before we process new ones. This occurs right
4000 after recovery completes when we transition from saving all messages on
4001 requestqueue, to processing all the saved messages, to processing new
4002 messages as they arrive. */
David Teiglande7fd4172006-01-18 09:30:29 +00004003
David Teiglandc36258b2007-09-27 15:53:38 -05004004static void dlm_receive_message(struct dlm_ls *ls, struct dlm_message *ms,
4005 int nodeid)
4006{
4007 if (dlm_locking_stopped(ls)) {
Al Viro8b0d8e02008-01-25 00:28:28 -05004008 dlm_add_requestqueue(ls, nodeid, ms);
David Teiglandc36258b2007-09-27 15:53:38 -05004009 } else {
4010 dlm_wait_requestqueue(ls);
4011 _receive_message(ls, ms);
4012 }
4013}
4014
4015/* This is called by dlm_recoverd to process messages that were saved on
4016 the requestqueue. */
4017
4018void dlm_receive_message_saved(struct dlm_ls *ls, struct dlm_message *ms)
4019{
4020 _receive_message(ls, ms);
4021}
4022
4023/* This is called by the midcomms layer when something is received for
4024 the lockspace. It could be either a MSG (normal message sent as part of
4025 standard locking activity) or an RCOM (recovery message sent as part of
4026 lockspace recovery). */
4027
Al Viroeef7d732008-01-25 00:58:46 -05004028void dlm_receive_buffer(union dlm_packet *p, int nodeid)
David Teiglandc36258b2007-09-27 15:53:38 -05004029{
Al Viroeef7d732008-01-25 00:58:46 -05004030 struct dlm_header *hd = &p->header;
David Teiglandc36258b2007-09-27 15:53:38 -05004031 struct dlm_ls *ls;
4032 int type = 0;
4033
4034 switch (hd->h_cmd) {
4035 case DLM_MSG:
Al Viroeef7d732008-01-25 00:58:46 -05004036 dlm_message_in(&p->message);
4037 type = p->message.m_type;
David Teiglandc36258b2007-09-27 15:53:38 -05004038 break;
4039 case DLM_RCOM:
Al Viroeef7d732008-01-25 00:58:46 -05004040 dlm_rcom_in(&p->rcom);
4041 type = p->rcom.rc_type;
David Teiglandc36258b2007-09-27 15:53:38 -05004042 break;
4043 default:
4044 log_print("invalid h_cmd %d from %u", hd->h_cmd, nodeid);
4045 return;
4046 }
4047
4048 if (hd->h_nodeid != nodeid) {
4049 log_print("invalid h_nodeid %d from %d lockspace %x",
4050 hd->h_nodeid, nodeid, hd->h_lockspace);
4051 return;
4052 }
4053
4054 ls = dlm_find_lockspace_global(hd->h_lockspace);
4055 if (!ls) {
David Teigland594199e2008-01-16 11:03:41 -06004056 if (dlm_config.ci_log_debug)
4057 log_print("invalid lockspace %x from %d cmd %d type %d",
4058 hd->h_lockspace, nodeid, hd->h_cmd, type);
David Teiglandc36258b2007-09-27 15:53:38 -05004059
4060 if (hd->h_cmd == DLM_RCOM && type == DLM_RCOM_STATUS)
Al Viroeef7d732008-01-25 00:58:46 -05004061 dlm_send_ls_not_ready(nodeid, &p->rcom);
David Teiglandc36258b2007-09-27 15:53:38 -05004062 return;
4063 }
4064
4065 /* this rwsem allows dlm_ls_stop() to wait for all dlm_recv threads to
4066 be inactive (in this ls) before transitioning to recovery mode */
4067
4068 down_read(&ls->ls_recv_active);
4069 if (hd->h_cmd == DLM_MSG)
Al Viroeef7d732008-01-25 00:58:46 -05004070 dlm_receive_message(ls, &p->message, nodeid);
David Teiglandc36258b2007-09-27 15:53:38 -05004071 else
Al Viroeef7d732008-01-25 00:58:46 -05004072 dlm_receive_rcom(ls, &p->rcom, nodeid);
David Teiglandc36258b2007-09-27 15:53:38 -05004073 up_read(&ls->ls_recv_active);
4074
4075 dlm_put_lockspace(ls);
4076}
David Teiglande7fd4172006-01-18 09:30:29 +00004077
David Teigland2a7ce0e2011-04-04 15:19:59 -05004078static void recover_convert_waiter(struct dlm_ls *ls, struct dlm_lkb *lkb,
4079 struct dlm_message *ms_stub)
David Teiglande7fd4172006-01-18 09:30:29 +00004080{
4081 if (middle_conversion(lkb)) {
4082 hold_lkb(lkb);
David Teigland2a7ce0e2011-04-04 15:19:59 -05004083 memset(ms_stub, 0, sizeof(struct dlm_message));
4084 ms_stub->m_flags = DLM_IFL_STUB_MS;
4085 ms_stub->m_type = DLM_MSG_CONVERT_REPLY;
4086 ms_stub->m_result = -EINPROGRESS;
4087 ms_stub->m_header.h_nodeid = lkb->lkb_nodeid;
4088 _receive_convert_reply(lkb, ms_stub);
David Teiglande7fd4172006-01-18 09:30:29 +00004089
4090 /* Same special case as in receive_rcom_lock_args() */
4091 lkb->lkb_grmode = DLM_LOCK_IV;
4092 rsb_set_flag(lkb->lkb_resource, RSB_RECOVER_CONVERT);
4093 unhold_lkb(lkb);
4094
4095 } else if (lkb->lkb_rqmode >= lkb->lkb_grmode) {
4096 lkb->lkb_flags |= DLM_IFL_RESEND;
4097 }
4098
4099 /* lkb->lkb_rqmode < lkb->lkb_grmode shouldn't happen since down
4100 conversions are async; there's no reply from the remote master */
4101}
4102
4103/* A waiting lkb needs recovery if the master node has failed, or
4104 the master node is changing (only when no directory is used) */
4105
4106static int waiter_needs_recovery(struct dlm_ls *ls, struct dlm_lkb *lkb)
4107{
4108 if (dlm_is_removed(ls, lkb->lkb_nodeid))
4109 return 1;
4110
4111 if (!dlm_no_directory(ls))
4112 return 0;
4113
4114 if (dlm_dir_nodeid(lkb->lkb_resource) != lkb->lkb_nodeid)
4115 return 1;
4116
4117 return 0;
4118}
4119
4120/* Recovery for locks that are waiting for replies from nodes that are now
4121 gone. We can just complete unlocks and cancels by faking a reply from the
4122 dead node. Requests and up-conversions we flag to be resent after
4123 recovery. Down-conversions can just be completed with a fake reply like
4124 unlocks. Conversions between PR and CW need special attention. */
4125
4126void dlm_recover_waiters_pre(struct dlm_ls *ls)
4127{
4128 struct dlm_lkb *lkb, *safe;
David Teigland2a7ce0e2011-04-04 15:19:59 -05004129 struct dlm_message *ms_stub;
David Teigland601342c2008-01-07 16:15:05 -06004130 int wait_type, stub_unlock_result, stub_cancel_result;
David Teiglande7fd4172006-01-18 09:30:29 +00004131
David Teiglanda22ca482011-07-11 08:40:53 -05004132 ms_stub = kmalloc(sizeof(struct dlm_message), GFP_KERNEL);
David Teigland2a7ce0e2011-04-04 15:19:59 -05004133 if (!ms_stub) {
4134 log_error(ls, "dlm_recover_waiters_pre no mem");
4135 return;
4136 }
4137
David Teigland90135922006-01-20 08:47:07 +00004138 mutex_lock(&ls->ls_waiters_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +00004139
4140 list_for_each_entry_safe(lkb, safe, &ls->ls_waiters, lkb_wait_reply) {
David Teigland2a7ce0e2011-04-04 15:19:59 -05004141
4142 /* exclude debug messages about unlocks because there can be so
4143 many and they aren't very interesting */
4144
4145 if (lkb->lkb_wait_type != DLM_MSG_UNLOCK) {
4146 log_debug(ls, "recover_waiter %x nodeid %d "
4147 "msg %d to %d", lkb->lkb_id, lkb->lkb_nodeid,
4148 lkb->lkb_wait_type, lkb->lkb_wait_nodeid);
4149 }
David Teiglande7fd4172006-01-18 09:30:29 +00004150
4151 /* all outstanding lookups, regardless of destination will be
4152 resent after recovery is done */
4153
4154 if (lkb->lkb_wait_type == DLM_MSG_LOOKUP) {
4155 lkb->lkb_flags |= DLM_IFL_RESEND;
4156 continue;
4157 }
4158
4159 if (!waiter_needs_recovery(ls, lkb))
4160 continue;
4161
David Teigland601342c2008-01-07 16:15:05 -06004162 wait_type = lkb->lkb_wait_type;
4163 stub_unlock_result = -DLM_EUNLOCK;
4164 stub_cancel_result = -DLM_ECANCEL;
4165
4166 /* Main reply may have been received leaving a zero wait_type,
4167 but a reply for the overlapping op may not have been
4168 received. In that case we need to fake the appropriate
4169 reply for the overlap op. */
4170
4171 if (!wait_type) {
4172 if (is_overlap_cancel(lkb)) {
4173 wait_type = DLM_MSG_CANCEL;
4174 if (lkb->lkb_grmode == DLM_LOCK_IV)
4175 stub_cancel_result = 0;
4176 }
4177 if (is_overlap_unlock(lkb)) {
4178 wait_type = DLM_MSG_UNLOCK;
4179 if (lkb->lkb_grmode == DLM_LOCK_IV)
4180 stub_unlock_result = -ENOENT;
4181 }
4182
4183 log_debug(ls, "rwpre overlap %x %x %d %d %d",
4184 lkb->lkb_id, lkb->lkb_flags, wait_type,
4185 stub_cancel_result, stub_unlock_result);
4186 }
4187
4188 switch (wait_type) {
David Teiglande7fd4172006-01-18 09:30:29 +00004189
4190 case DLM_MSG_REQUEST:
4191 lkb->lkb_flags |= DLM_IFL_RESEND;
4192 break;
4193
4194 case DLM_MSG_CONVERT:
David Teigland2a7ce0e2011-04-04 15:19:59 -05004195 recover_convert_waiter(ls, lkb, ms_stub);
David Teiglande7fd4172006-01-18 09:30:29 +00004196 break;
4197
4198 case DLM_MSG_UNLOCK:
4199 hold_lkb(lkb);
David Teigland2a7ce0e2011-04-04 15:19:59 -05004200 memset(ms_stub, 0, sizeof(struct dlm_message));
4201 ms_stub->m_flags = DLM_IFL_STUB_MS;
4202 ms_stub->m_type = DLM_MSG_UNLOCK_REPLY;
4203 ms_stub->m_result = stub_unlock_result;
4204 ms_stub->m_header.h_nodeid = lkb->lkb_nodeid;
4205 _receive_unlock_reply(lkb, ms_stub);
David Teiglandb3f58d82006-02-28 11:16:37 -05004206 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00004207 break;
4208
4209 case DLM_MSG_CANCEL:
4210 hold_lkb(lkb);
David Teigland2a7ce0e2011-04-04 15:19:59 -05004211 memset(ms_stub, 0, sizeof(struct dlm_message));
4212 ms_stub->m_flags = DLM_IFL_STUB_MS;
4213 ms_stub->m_type = DLM_MSG_CANCEL_REPLY;
4214 ms_stub->m_result = stub_cancel_result;
4215 ms_stub->m_header.h_nodeid = lkb->lkb_nodeid;
4216 _receive_cancel_reply(lkb, ms_stub);
David Teiglandb3f58d82006-02-28 11:16:37 -05004217 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00004218 break;
4219
4220 default:
David Teigland601342c2008-01-07 16:15:05 -06004221 log_error(ls, "invalid lkb wait_type %d %d",
4222 lkb->lkb_wait_type, wait_type);
David Teiglande7fd4172006-01-18 09:30:29 +00004223 }
David Teigland81456802006-07-25 14:05:09 -05004224 schedule();
David Teiglande7fd4172006-01-18 09:30:29 +00004225 }
David Teigland90135922006-01-20 08:47:07 +00004226 mutex_unlock(&ls->ls_waiters_mutex);
David Teigland2a7ce0e2011-04-04 15:19:59 -05004227 kfree(ms_stub);
David Teiglande7fd4172006-01-18 09:30:29 +00004228}
4229
David Teiglandef0c2bb2007-03-28 09:56:46 -05004230static struct dlm_lkb *find_resend_waiter(struct dlm_ls *ls)
David Teiglande7fd4172006-01-18 09:30:29 +00004231{
4232 struct dlm_lkb *lkb;
David Teiglandef0c2bb2007-03-28 09:56:46 -05004233 int found = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00004234
David Teigland90135922006-01-20 08:47:07 +00004235 mutex_lock(&ls->ls_waiters_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +00004236 list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
4237 if (lkb->lkb_flags & DLM_IFL_RESEND) {
David Teiglandef0c2bb2007-03-28 09:56:46 -05004238 hold_lkb(lkb);
4239 found = 1;
David Teiglande7fd4172006-01-18 09:30:29 +00004240 break;
4241 }
4242 }
David Teigland90135922006-01-20 08:47:07 +00004243 mutex_unlock(&ls->ls_waiters_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +00004244
David Teiglandef0c2bb2007-03-28 09:56:46 -05004245 if (!found)
David Teiglande7fd4172006-01-18 09:30:29 +00004246 lkb = NULL;
David Teiglandef0c2bb2007-03-28 09:56:46 -05004247 return lkb;
David Teiglande7fd4172006-01-18 09:30:29 +00004248}
4249
4250/* Deal with lookups and lkb's marked RESEND from _pre. We may now be the
4251 master or dir-node for r. Processing the lkb may result in it being placed
4252 back on waiters. */
4253
David Teiglandef0c2bb2007-03-28 09:56:46 -05004254/* We do this after normal locking has been enabled and any saved messages
4255 (in requestqueue) have been processed. We should be confident that at
4256 this point we won't get or process a reply to any of these waiting
4257 operations. But, new ops may be coming in on the rsbs/locks here from
4258 userspace or remotely. */
4259
4260/* there may have been an overlap unlock/cancel prior to recovery or after
4261 recovery. if before, the lkb may still have a pos wait_count; if after, the
4262 overlap flag would just have been set and nothing new sent. we can be
4263 confident here than any replies to either the initial op or overlap ops
4264 prior to recovery have been received. */
4265
David Teiglande7fd4172006-01-18 09:30:29 +00004266int dlm_recover_waiters_post(struct dlm_ls *ls)
4267{
4268 struct dlm_lkb *lkb;
4269 struct dlm_rsb *r;
David Teiglandef0c2bb2007-03-28 09:56:46 -05004270 int error = 0, mstype, err, oc, ou;
David Teiglande7fd4172006-01-18 09:30:29 +00004271
4272 while (1) {
4273 if (dlm_locking_stopped(ls)) {
4274 log_debug(ls, "recover_waiters_post aborted");
4275 error = -EINTR;
4276 break;
4277 }
4278
David Teiglandef0c2bb2007-03-28 09:56:46 -05004279 lkb = find_resend_waiter(ls);
4280 if (!lkb)
David Teiglande7fd4172006-01-18 09:30:29 +00004281 break;
4282
4283 r = lkb->lkb_resource;
David Teiglandef0c2bb2007-03-28 09:56:46 -05004284 hold_rsb(r);
4285 lock_rsb(r);
4286
4287 mstype = lkb->lkb_wait_type;
4288 oc = is_overlap_cancel(lkb);
4289 ou = is_overlap_unlock(lkb);
4290 err = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00004291
David Teigland2a7ce0e2011-04-04 15:19:59 -05004292 log_debug(ls, "recover_waiter %x nodeid %d msg %d r_nodeid %d",
4293 lkb->lkb_id, lkb->lkb_nodeid, mstype, r->res_nodeid);
David Teiglande7fd4172006-01-18 09:30:29 +00004294
David Teiglandef0c2bb2007-03-28 09:56:46 -05004295 /* At this point we assume that we won't get a reply to any
4296 previous op or overlap op on this lock. First, do a big
4297 remove_from_waiters() for all previous ops. */
David Teiglande7fd4172006-01-18 09:30:29 +00004298
David Teiglandef0c2bb2007-03-28 09:56:46 -05004299 lkb->lkb_flags &= ~DLM_IFL_RESEND;
4300 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
4301 lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
4302 lkb->lkb_wait_type = 0;
4303 lkb->lkb_wait_count = 0;
4304 mutex_lock(&ls->ls_waiters_mutex);
4305 list_del_init(&lkb->lkb_wait_reply);
4306 mutex_unlock(&ls->ls_waiters_mutex);
4307 unhold_lkb(lkb); /* for waiters list */
David Teiglande7fd4172006-01-18 09:30:29 +00004308
David Teiglandef0c2bb2007-03-28 09:56:46 -05004309 if (oc || ou) {
4310 /* do an unlock or cancel instead of resending */
4311 switch (mstype) {
4312 case DLM_MSG_LOOKUP:
4313 case DLM_MSG_REQUEST:
4314 queue_cast(r, lkb, ou ? -DLM_EUNLOCK :
4315 -DLM_ECANCEL);
4316 unhold_lkb(lkb); /* undoes create_lkb() */
4317 break;
4318 case DLM_MSG_CONVERT:
4319 if (oc) {
4320 queue_cast(r, lkb, -DLM_ECANCEL);
4321 } else {
4322 lkb->lkb_exflags |= DLM_LKF_FORCEUNLOCK;
4323 _unlock_lock(r, lkb);
4324 }
4325 break;
4326 default:
4327 err = 1;
4328 }
4329 } else {
4330 switch (mstype) {
4331 case DLM_MSG_LOOKUP:
4332 case DLM_MSG_REQUEST:
4333 _request_lock(r, lkb);
4334 if (is_master(r))
4335 confirm_master(r, 0);
4336 break;
4337 case DLM_MSG_CONVERT:
4338 _convert_lock(r, lkb);
4339 break;
4340 default:
4341 err = 1;
4342 }
David Teiglande7fd4172006-01-18 09:30:29 +00004343 }
David Teiglandef0c2bb2007-03-28 09:56:46 -05004344
4345 if (err)
4346 log_error(ls, "recover_waiters_post %x %d %x %d %d",
4347 lkb->lkb_id, mstype, lkb->lkb_flags, oc, ou);
4348 unlock_rsb(r);
4349 put_rsb(r);
4350 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00004351 }
4352
4353 return error;
4354}
4355
4356static void purge_queue(struct dlm_rsb *r, struct list_head *queue,
4357 int (*test)(struct dlm_ls *ls, struct dlm_lkb *lkb))
4358{
4359 struct dlm_ls *ls = r->res_ls;
4360 struct dlm_lkb *lkb, *safe;
4361
4362 list_for_each_entry_safe(lkb, safe, queue, lkb_statequeue) {
4363 if (test(ls, lkb)) {
David Teigland97a35d12006-05-02 13:34:03 -04004364 rsb_set_flag(r, RSB_LOCKS_PURGED);
David Teiglande7fd4172006-01-18 09:30:29 +00004365 del_lkb(r, lkb);
4366 /* this put should free the lkb */
David Teiglandb3f58d82006-02-28 11:16:37 -05004367 if (!dlm_put_lkb(lkb))
David Teiglande7fd4172006-01-18 09:30:29 +00004368 log_error(ls, "purged lkb not released");
4369 }
4370 }
4371}
4372
4373static int purge_dead_test(struct dlm_ls *ls, struct dlm_lkb *lkb)
4374{
4375 return (is_master_copy(lkb) && dlm_is_removed(ls, lkb->lkb_nodeid));
4376}
4377
4378static int purge_mstcpy_test(struct dlm_ls *ls, struct dlm_lkb *lkb)
4379{
4380 return is_master_copy(lkb);
4381}
4382
4383static void purge_dead_locks(struct dlm_rsb *r)
4384{
4385 purge_queue(r, &r->res_grantqueue, &purge_dead_test);
4386 purge_queue(r, &r->res_convertqueue, &purge_dead_test);
4387 purge_queue(r, &r->res_waitqueue, &purge_dead_test);
4388}
4389
4390void dlm_purge_mstcpy_locks(struct dlm_rsb *r)
4391{
4392 purge_queue(r, &r->res_grantqueue, &purge_mstcpy_test);
4393 purge_queue(r, &r->res_convertqueue, &purge_mstcpy_test);
4394 purge_queue(r, &r->res_waitqueue, &purge_mstcpy_test);
4395}
4396
4397/* Get rid of locks held by nodes that are gone. */
4398
4399int dlm_purge_locks(struct dlm_ls *ls)
4400{
4401 struct dlm_rsb *r;
4402
4403 log_debug(ls, "dlm_purge_locks");
4404
4405 down_write(&ls->ls_root_sem);
4406 list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
4407 hold_rsb(r);
4408 lock_rsb(r);
4409 if (is_master(r))
4410 purge_dead_locks(r);
4411 unlock_rsb(r);
4412 unhold_rsb(r);
4413
4414 schedule();
4415 }
4416 up_write(&ls->ls_root_sem);
4417
4418 return 0;
4419}
4420
David Teigland97a35d12006-05-02 13:34:03 -04004421static struct dlm_rsb *find_purged_rsb(struct dlm_ls *ls, int bucket)
4422{
4423 struct dlm_rsb *r, *r_ret = NULL;
4424
David Teiglandc7be7612009-01-07 16:50:41 -06004425 spin_lock(&ls->ls_rsbtbl[bucket].lock);
David Teigland97a35d12006-05-02 13:34:03 -04004426 list_for_each_entry(r, &ls->ls_rsbtbl[bucket].list, res_hashchain) {
4427 if (!rsb_flag(r, RSB_LOCKS_PURGED))
4428 continue;
4429 hold_rsb(r);
4430 rsb_clear_flag(r, RSB_LOCKS_PURGED);
4431 r_ret = r;
4432 break;
4433 }
David Teiglandc7be7612009-01-07 16:50:41 -06004434 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teigland97a35d12006-05-02 13:34:03 -04004435 return r_ret;
4436}
4437
4438void dlm_grant_after_purge(struct dlm_ls *ls)
David Teiglande7fd4172006-01-18 09:30:29 +00004439{
4440 struct dlm_rsb *r;
David Teigland2b4e9262006-07-25 13:59:48 -05004441 int bucket = 0;
David Teiglande7fd4172006-01-18 09:30:29 +00004442
David Teigland2b4e9262006-07-25 13:59:48 -05004443 while (1) {
4444 r = find_purged_rsb(ls, bucket);
4445 if (!r) {
4446 if (bucket == ls->ls_rsbtbl_size - 1)
4447 break;
4448 bucket++;
David Teigland97a35d12006-05-02 13:34:03 -04004449 continue;
David Teigland2b4e9262006-07-25 13:59:48 -05004450 }
David Teigland97a35d12006-05-02 13:34:03 -04004451 lock_rsb(r);
4452 if (is_master(r)) {
4453 grant_pending_locks(r);
4454 confirm_master(r, 0);
David Teiglande7fd4172006-01-18 09:30:29 +00004455 }
David Teigland97a35d12006-05-02 13:34:03 -04004456 unlock_rsb(r);
4457 put_rsb(r);
David Teigland2b4e9262006-07-25 13:59:48 -05004458 schedule();
David Teiglande7fd4172006-01-18 09:30:29 +00004459 }
David Teiglande7fd4172006-01-18 09:30:29 +00004460}
4461
4462static struct dlm_lkb *search_remid_list(struct list_head *head, int nodeid,
4463 uint32_t remid)
4464{
4465 struct dlm_lkb *lkb;
4466
4467 list_for_each_entry(lkb, head, lkb_statequeue) {
4468 if (lkb->lkb_nodeid == nodeid && lkb->lkb_remid == remid)
4469 return lkb;
4470 }
4471 return NULL;
4472}
4473
4474static struct dlm_lkb *search_remid(struct dlm_rsb *r, int nodeid,
4475 uint32_t remid)
4476{
4477 struct dlm_lkb *lkb;
4478
4479 lkb = search_remid_list(&r->res_grantqueue, nodeid, remid);
4480 if (lkb)
4481 return lkb;
4482 lkb = search_remid_list(&r->res_convertqueue, nodeid, remid);
4483 if (lkb)
4484 return lkb;
4485 lkb = search_remid_list(&r->res_waitqueue, nodeid, remid);
4486 if (lkb)
4487 return lkb;
4488 return NULL;
4489}
4490
Al Viroae773d02008-01-25 19:55:09 -05004491/* needs at least dlm_rcom + rcom_lock */
David Teiglande7fd4172006-01-18 09:30:29 +00004492static int receive_rcom_lock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
4493 struct dlm_rsb *r, struct dlm_rcom *rc)
4494{
4495 struct rcom_lock *rl = (struct rcom_lock *) rc->rc_buf;
David Teiglande7fd4172006-01-18 09:30:29 +00004496
4497 lkb->lkb_nodeid = rc->rc_header.h_nodeid;
Al Viro163a1852008-01-25 02:08:26 -05004498 lkb->lkb_ownpid = le32_to_cpu(rl->rl_ownpid);
4499 lkb->lkb_remid = le32_to_cpu(rl->rl_lkid);
4500 lkb->lkb_exflags = le32_to_cpu(rl->rl_exflags);
4501 lkb->lkb_flags = le32_to_cpu(rl->rl_flags) & 0x0000FFFF;
David Teiglande7fd4172006-01-18 09:30:29 +00004502 lkb->lkb_flags |= DLM_IFL_MSTCPY;
Al Viro163a1852008-01-25 02:08:26 -05004503 lkb->lkb_lvbseq = le32_to_cpu(rl->rl_lvbseq);
David Teiglande7fd4172006-01-18 09:30:29 +00004504 lkb->lkb_rqmode = rl->rl_rqmode;
4505 lkb->lkb_grmode = rl->rl_grmode;
4506 /* don't set lkb_status because add_lkb wants to itself */
4507
David Teigland8304d6f2011-02-21 14:58:21 -06004508 lkb->lkb_bastfn = (rl->rl_asts & DLM_CB_BAST) ? &fake_bastfn : NULL;
4509 lkb->lkb_astfn = (rl->rl_asts & DLM_CB_CAST) ? &fake_astfn : NULL;
David Teiglande7fd4172006-01-18 09:30:29 +00004510
David Teiglande7fd4172006-01-18 09:30:29 +00004511 if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
Al Viroa5dd0632008-01-25 20:22:22 -05004512 int lvblen = rc->rc_header.h_length - sizeof(struct dlm_rcom) -
4513 sizeof(struct rcom_lock);
4514 if (lvblen > ls->ls_lvblen)
4515 return -EINVAL;
David Teigland52bda2b2007-11-07 09:06:49 -06004516 lkb->lkb_lvbptr = dlm_allocate_lvb(ls);
David Teiglande7fd4172006-01-18 09:30:29 +00004517 if (!lkb->lkb_lvbptr)
4518 return -ENOMEM;
David Teiglande7fd4172006-01-18 09:30:29 +00004519 memcpy(lkb->lkb_lvbptr, rl->rl_lvb, lvblen);
4520 }
4521
4522 /* Conversions between PR and CW (middle modes) need special handling.
4523 The real granted mode of these converting locks cannot be determined
4524 until all locks have been rebuilt on the rsb (recover_conversion) */
4525
Al Viro163a1852008-01-25 02:08:26 -05004526 if (rl->rl_wait_type == cpu_to_le16(DLM_MSG_CONVERT) &&
4527 middle_conversion(lkb)) {
David Teiglande7fd4172006-01-18 09:30:29 +00004528 rl->rl_status = DLM_LKSTS_CONVERT;
4529 lkb->lkb_grmode = DLM_LOCK_IV;
4530 rsb_set_flag(r, RSB_RECOVER_CONVERT);
4531 }
4532
4533 return 0;
4534}
4535
4536/* This lkb may have been recovered in a previous aborted recovery so we need
4537 to check if the rsb already has an lkb with the given remote nodeid/lkid.
4538 If so we just send back a standard reply. If not, we create a new lkb with
4539 the given values and send back our lkid. We send back our lkid by sending
4540 back the rcom_lock struct we got but with the remid field filled in. */
4541
Al Viroae773d02008-01-25 19:55:09 -05004542/* needs at least dlm_rcom + rcom_lock */
David Teiglande7fd4172006-01-18 09:30:29 +00004543int dlm_recover_master_copy(struct dlm_ls *ls, struct dlm_rcom *rc)
4544{
4545 struct rcom_lock *rl = (struct rcom_lock *) rc->rc_buf;
4546 struct dlm_rsb *r;
4547 struct dlm_lkb *lkb;
4548 int error;
4549
4550 if (rl->rl_parent_lkid) {
4551 error = -EOPNOTSUPP;
4552 goto out;
4553 }
4554
Al Viro163a1852008-01-25 02:08:26 -05004555 error = find_rsb(ls, rl->rl_name, le16_to_cpu(rl->rl_namelen),
4556 R_MASTER, &r);
David Teiglande7fd4172006-01-18 09:30:29 +00004557 if (error)
4558 goto out;
4559
4560 lock_rsb(r);
4561
Al Viro163a1852008-01-25 02:08:26 -05004562 lkb = search_remid(r, rc->rc_header.h_nodeid, le32_to_cpu(rl->rl_lkid));
David Teiglande7fd4172006-01-18 09:30:29 +00004563 if (lkb) {
4564 error = -EEXIST;
4565 goto out_remid;
4566 }
4567
4568 error = create_lkb(ls, &lkb);
4569 if (error)
4570 goto out_unlock;
4571
4572 error = receive_rcom_lock_args(ls, lkb, r, rc);
4573 if (error) {
David Teiglandb3f58d82006-02-28 11:16:37 -05004574 __put_lkb(ls, lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00004575 goto out_unlock;
4576 }
4577
4578 attach_lkb(r, lkb);
4579 add_lkb(r, lkb, rl->rl_status);
4580 error = 0;
4581
4582 out_remid:
4583 /* this is the new value returned to the lock holder for
4584 saving in its process-copy lkb */
Al Viro163a1852008-01-25 02:08:26 -05004585 rl->rl_remid = cpu_to_le32(lkb->lkb_id);
David Teiglande7fd4172006-01-18 09:30:29 +00004586
4587 out_unlock:
4588 unlock_rsb(r);
4589 put_rsb(r);
4590 out:
4591 if (error)
Al Viro163a1852008-01-25 02:08:26 -05004592 log_debug(ls, "recover_master_copy %d %x", error,
4593 le32_to_cpu(rl->rl_lkid));
4594 rl->rl_result = cpu_to_le32(error);
David Teiglande7fd4172006-01-18 09:30:29 +00004595 return error;
4596}
4597
Al Viroae773d02008-01-25 19:55:09 -05004598/* needs at least dlm_rcom + rcom_lock */
David Teiglande7fd4172006-01-18 09:30:29 +00004599int dlm_recover_process_copy(struct dlm_ls *ls, struct dlm_rcom *rc)
4600{
4601 struct rcom_lock *rl = (struct rcom_lock *) rc->rc_buf;
4602 struct dlm_rsb *r;
4603 struct dlm_lkb *lkb;
4604 int error;
4605
Al Viro163a1852008-01-25 02:08:26 -05004606 error = find_lkb(ls, le32_to_cpu(rl->rl_lkid), &lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00004607 if (error) {
Al Viro163a1852008-01-25 02:08:26 -05004608 log_error(ls, "recover_process_copy no lkid %x",
4609 le32_to_cpu(rl->rl_lkid));
David Teiglande7fd4172006-01-18 09:30:29 +00004610 return error;
4611 }
4612
4613 DLM_ASSERT(is_process_copy(lkb), dlm_print_lkb(lkb););
4614
Al Viro163a1852008-01-25 02:08:26 -05004615 error = le32_to_cpu(rl->rl_result);
David Teiglande7fd4172006-01-18 09:30:29 +00004616
4617 r = lkb->lkb_resource;
4618 hold_rsb(r);
4619 lock_rsb(r);
4620
4621 switch (error) {
David Teiglanddc200a82006-12-13 10:36:37 -06004622 case -EBADR:
4623 /* There's a chance the new master received our lock before
4624 dlm_recover_master_reply(), this wouldn't happen if we did
4625 a barrier between recover_masters and recover_locks. */
4626 log_debug(ls, "master copy not ready %x r %lx %s", lkb->lkb_id,
4627 (unsigned long)r, r->res_name);
4628 dlm_send_rcom_lock(r, lkb);
4629 goto out;
David Teiglande7fd4172006-01-18 09:30:29 +00004630 case -EEXIST:
4631 log_debug(ls, "master copy exists %x", lkb->lkb_id);
4632 /* fall through */
4633 case 0:
Al Viro163a1852008-01-25 02:08:26 -05004634 lkb->lkb_remid = le32_to_cpu(rl->rl_remid);
David Teiglande7fd4172006-01-18 09:30:29 +00004635 break;
4636 default:
4637 log_error(ls, "dlm_recover_process_copy unknown error %d %x",
4638 error, lkb->lkb_id);
4639 }
4640
4641 /* an ack for dlm_recover_locks() which waits for replies from
4642 all the locks it sends to new masters */
4643 dlm_recovered_lock(r);
David Teiglanddc200a82006-12-13 10:36:37 -06004644 out:
David Teiglande7fd4172006-01-18 09:30:29 +00004645 unlock_rsb(r);
4646 put_rsb(r);
David Teiglandb3f58d82006-02-28 11:16:37 -05004647 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +00004648
4649 return 0;
4650}
4651
David Teigland597d0ca2006-07-12 16:44:04 -05004652int dlm_user_request(struct dlm_ls *ls, struct dlm_user_args *ua,
4653 int mode, uint32_t flags, void *name, unsigned int namelen,
David Teiglandd7db9232007-05-18 09:00:32 -05004654 unsigned long timeout_cs)
David Teigland597d0ca2006-07-12 16:44:04 -05004655{
4656 struct dlm_lkb *lkb;
4657 struct dlm_args args;
4658 int error;
4659
David Teigland85e86ed2007-05-18 08:58:15 -05004660 dlm_lock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05004661
4662 error = create_lkb(ls, &lkb);
4663 if (error) {
4664 kfree(ua);
4665 goto out;
4666 }
4667
4668 if (flags & DLM_LKF_VALBLK) {
David Teigland573c24c2009-11-30 16:34:43 -06004669 ua->lksb.sb_lvbptr = kzalloc(DLM_USER_LVB_LEN, GFP_NOFS);
David Teigland597d0ca2006-07-12 16:44:04 -05004670 if (!ua->lksb.sb_lvbptr) {
4671 kfree(ua);
4672 __put_lkb(ls, lkb);
4673 error = -ENOMEM;
4674 goto out;
4675 }
4676 }
4677
David Teigland52bda2b2007-11-07 09:06:49 -06004678 /* After ua is attached to lkb it will be freed by dlm_free_lkb().
David Teigland597d0ca2006-07-12 16:44:04 -05004679 When DLM_IFL_USER is set, the dlm knows that this is a userspace
4680 lock and that lkb_astparam is the dlm_user_args structure. */
4681
David Teiglandd7db9232007-05-18 09:00:32 -05004682 error = set_lock_args(mode, &ua->lksb, flags, namelen, timeout_cs,
David Teiglande5dae542008-02-06 00:35:45 -06004683 fake_astfn, ua, fake_bastfn, &args);
David Teigland597d0ca2006-07-12 16:44:04 -05004684 lkb->lkb_flags |= DLM_IFL_USER;
David Teigland597d0ca2006-07-12 16:44:04 -05004685
4686 if (error) {
4687 __put_lkb(ls, lkb);
4688 goto out;
4689 }
4690
4691 error = request_lock(ls, lkb, name, namelen, &args);
4692
4693 switch (error) {
4694 case 0:
4695 break;
4696 case -EINPROGRESS:
4697 error = 0;
4698 break;
4699 case -EAGAIN:
4700 error = 0;
4701 /* fall through */
4702 default:
4703 __put_lkb(ls, lkb);
4704 goto out;
4705 }
4706
4707 /* add this new lkb to the per-process list of locks */
4708 spin_lock(&ua->proc->locks_spin);
David Teiglandef0c2bb2007-03-28 09:56:46 -05004709 hold_lkb(lkb);
David Teigland597d0ca2006-07-12 16:44:04 -05004710 list_add_tail(&lkb->lkb_ownqueue, &ua->proc->locks);
4711 spin_unlock(&ua->proc->locks_spin);
4712 out:
David Teigland85e86ed2007-05-18 08:58:15 -05004713 dlm_unlock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05004714 return error;
4715}
4716
4717int dlm_user_convert(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
David Teiglandd7db9232007-05-18 09:00:32 -05004718 int mode, uint32_t flags, uint32_t lkid, char *lvb_in,
4719 unsigned long timeout_cs)
David Teigland597d0ca2006-07-12 16:44:04 -05004720{
4721 struct dlm_lkb *lkb;
4722 struct dlm_args args;
4723 struct dlm_user_args *ua;
4724 int error;
4725
David Teigland85e86ed2007-05-18 08:58:15 -05004726 dlm_lock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05004727
4728 error = find_lkb(ls, lkid, &lkb);
4729 if (error)
4730 goto out;
4731
4732 /* user can change the params on its lock when it converts it, or
4733 add an lvb that didn't exist before */
4734
David Teiglandd292c0c2008-02-06 23:27:04 -06004735 ua = lkb->lkb_ua;
David Teigland597d0ca2006-07-12 16:44:04 -05004736
4737 if (flags & DLM_LKF_VALBLK && !ua->lksb.sb_lvbptr) {
David Teigland573c24c2009-11-30 16:34:43 -06004738 ua->lksb.sb_lvbptr = kzalloc(DLM_USER_LVB_LEN, GFP_NOFS);
David Teigland597d0ca2006-07-12 16:44:04 -05004739 if (!ua->lksb.sb_lvbptr) {
4740 error = -ENOMEM;
4741 goto out_put;
4742 }
4743 }
4744 if (lvb_in && ua->lksb.sb_lvbptr)
4745 memcpy(ua->lksb.sb_lvbptr, lvb_in, DLM_USER_LVB_LEN);
4746
David Teiglandd7db9232007-05-18 09:00:32 -05004747 ua->xid = ua_tmp->xid;
David Teigland597d0ca2006-07-12 16:44:04 -05004748 ua->castparam = ua_tmp->castparam;
4749 ua->castaddr = ua_tmp->castaddr;
4750 ua->bastparam = ua_tmp->bastparam;
4751 ua->bastaddr = ua_tmp->bastaddr;
Patrick Caulfield10948eb2006-08-23 09:49:31 +01004752 ua->user_lksb = ua_tmp->user_lksb;
David Teigland597d0ca2006-07-12 16:44:04 -05004753
David Teiglandd7db9232007-05-18 09:00:32 -05004754 error = set_lock_args(mode, &ua->lksb, flags, 0, timeout_cs,
David Teiglande5dae542008-02-06 00:35:45 -06004755 fake_astfn, ua, fake_bastfn, &args);
David Teigland597d0ca2006-07-12 16:44:04 -05004756 if (error)
4757 goto out_put;
4758
4759 error = convert_lock(ls, lkb, &args);
4760
David Teiglandc85d65e2007-05-18 09:01:26 -05004761 if (error == -EINPROGRESS || error == -EAGAIN || error == -EDEADLK)
David Teigland597d0ca2006-07-12 16:44:04 -05004762 error = 0;
4763 out_put:
4764 dlm_put_lkb(lkb);
4765 out:
David Teigland85e86ed2007-05-18 08:58:15 -05004766 dlm_unlock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05004767 kfree(ua_tmp);
4768 return error;
4769}
4770
4771int dlm_user_unlock(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
4772 uint32_t flags, uint32_t lkid, char *lvb_in)
4773{
4774 struct dlm_lkb *lkb;
4775 struct dlm_args args;
4776 struct dlm_user_args *ua;
4777 int error;
4778
David Teigland85e86ed2007-05-18 08:58:15 -05004779 dlm_lock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05004780
4781 error = find_lkb(ls, lkid, &lkb);
4782 if (error)
4783 goto out;
4784
David Teiglandd292c0c2008-02-06 23:27:04 -06004785 ua = lkb->lkb_ua;
David Teigland597d0ca2006-07-12 16:44:04 -05004786
4787 if (lvb_in && ua->lksb.sb_lvbptr)
4788 memcpy(ua->lksb.sb_lvbptr, lvb_in, DLM_USER_LVB_LEN);
Patrick Caulfieldb434eda2007-10-01 15:28:42 +01004789 if (ua_tmp->castparam)
4790 ua->castparam = ua_tmp->castparam;
Patrick Caulfieldcc346d52006-08-08 10:34:40 -04004791 ua->user_lksb = ua_tmp->user_lksb;
David Teigland597d0ca2006-07-12 16:44:04 -05004792
4793 error = set_unlock_args(flags, ua, &args);
4794 if (error)
4795 goto out_put;
4796
4797 error = unlock_lock(ls, lkb, &args);
4798
4799 if (error == -DLM_EUNLOCK)
4800 error = 0;
David Teiglandef0c2bb2007-03-28 09:56:46 -05004801 /* from validate_unlock_args() */
4802 if (error == -EBUSY && (flags & DLM_LKF_FORCEUNLOCK))
4803 error = 0;
David Teigland597d0ca2006-07-12 16:44:04 -05004804 if (error)
4805 goto out_put;
4806
4807 spin_lock(&ua->proc->locks_spin);
David Teiglanda1bc86e2007-01-15 10:34:52 -06004808 /* dlm_user_add_ast() may have already taken lkb off the proc list */
4809 if (!list_empty(&lkb->lkb_ownqueue))
4810 list_move(&lkb->lkb_ownqueue, &ua->proc->unlocking);
David Teigland597d0ca2006-07-12 16:44:04 -05004811 spin_unlock(&ua->proc->locks_spin);
David Teigland597d0ca2006-07-12 16:44:04 -05004812 out_put:
4813 dlm_put_lkb(lkb);
4814 out:
David Teigland85e86ed2007-05-18 08:58:15 -05004815 dlm_unlock_recovery(ls);
David Teiglandef0c2bb2007-03-28 09:56:46 -05004816 kfree(ua_tmp);
David Teigland597d0ca2006-07-12 16:44:04 -05004817 return error;
4818}
4819
4820int dlm_user_cancel(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
4821 uint32_t flags, uint32_t lkid)
4822{
4823 struct dlm_lkb *lkb;
4824 struct dlm_args args;
4825 struct dlm_user_args *ua;
4826 int error;
4827
David Teigland85e86ed2007-05-18 08:58:15 -05004828 dlm_lock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05004829
4830 error = find_lkb(ls, lkid, &lkb);
4831 if (error)
4832 goto out;
4833
David Teiglandd292c0c2008-02-06 23:27:04 -06004834 ua = lkb->lkb_ua;
Patrick Caulfieldb434eda2007-10-01 15:28:42 +01004835 if (ua_tmp->castparam)
4836 ua->castparam = ua_tmp->castparam;
Patrick Caulfieldc059f702006-08-23 10:24:03 +01004837 ua->user_lksb = ua_tmp->user_lksb;
David Teigland597d0ca2006-07-12 16:44:04 -05004838
4839 error = set_unlock_args(flags, ua, &args);
4840 if (error)
4841 goto out_put;
4842
4843 error = cancel_lock(ls, lkb, &args);
4844
4845 if (error == -DLM_ECANCEL)
4846 error = 0;
David Teiglandef0c2bb2007-03-28 09:56:46 -05004847 /* from validate_unlock_args() */
4848 if (error == -EBUSY)
4849 error = 0;
David Teigland597d0ca2006-07-12 16:44:04 -05004850 out_put:
4851 dlm_put_lkb(lkb);
4852 out:
David Teigland85e86ed2007-05-18 08:58:15 -05004853 dlm_unlock_recovery(ls);
David Teiglandef0c2bb2007-03-28 09:56:46 -05004854 kfree(ua_tmp);
David Teigland597d0ca2006-07-12 16:44:04 -05004855 return error;
4856}
4857
David Teigland8b4021f2007-05-29 08:46:00 -05004858int dlm_user_deadlock(struct dlm_ls *ls, uint32_t flags, uint32_t lkid)
4859{
4860 struct dlm_lkb *lkb;
4861 struct dlm_args args;
4862 struct dlm_user_args *ua;
4863 struct dlm_rsb *r;
4864 int error;
4865
4866 dlm_lock_recovery(ls);
4867
4868 error = find_lkb(ls, lkid, &lkb);
4869 if (error)
4870 goto out;
4871
David Teiglandd292c0c2008-02-06 23:27:04 -06004872 ua = lkb->lkb_ua;
David Teigland8b4021f2007-05-29 08:46:00 -05004873
4874 error = set_unlock_args(flags, ua, &args);
4875 if (error)
4876 goto out_put;
4877
4878 /* same as cancel_lock(), but set DEADLOCK_CANCEL after lock_rsb */
4879
4880 r = lkb->lkb_resource;
4881 hold_rsb(r);
4882 lock_rsb(r);
4883
4884 error = validate_unlock_args(lkb, &args);
4885 if (error)
4886 goto out_r;
4887 lkb->lkb_flags |= DLM_IFL_DEADLOCK_CANCEL;
4888
4889 error = _cancel_lock(r, lkb);
4890 out_r:
4891 unlock_rsb(r);
4892 put_rsb(r);
4893
4894 if (error == -DLM_ECANCEL)
4895 error = 0;
4896 /* from validate_unlock_args() */
4897 if (error == -EBUSY)
4898 error = 0;
4899 out_put:
4900 dlm_put_lkb(lkb);
4901 out:
4902 dlm_unlock_recovery(ls);
4903 return error;
4904}
4905
David Teiglandef0c2bb2007-03-28 09:56:46 -05004906/* lkb's that are removed from the waiters list by revert are just left on the
4907 orphans list with the granted orphan locks, to be freed by purge */
4908
David Teigland597d0ca2006-07-12 16:44:04 -05004909static int orphan_proc_lock(struct dlm_ls *ls, struct dlm_lkb *lkb)
4910{
David Teiglandef0c2bb2007-03-28 09:56:46 -05004911 struct dlm_args args;
4912 int error;
David Teigland597d0ca2006-07-12 16:44:04 -05004913
David Teiglandef0c2bb2007-03-28 09:56:46 -05004914 hold_lkb(lkb);
4915 mutex_lock(&ls->ls_orphans_mutex);
4916 list_add_tail(&lkb->lkb_ownqueue, &ls->ls_orphans);
4917 mutex_unlock(&ls->ls_orphans_mutex);
David Teigland597d0ca2006-07-12 16:44:04 -05004918
David Teiglandd292c0c2008-02-06 23:27:04 -06004919 set_unlock_args(0, lkb->lkb_ua, &args);
David Teiglandef0c2bb2007-03-28 09:56:46 -05004920
4921 error = cancel_lock(ls, lkb, &args);
4922 if (error == -DLM_ECANCEL)
4923 error = 0;
4924 return error;
David Teigland597d0ca2006-07-12 16:44:04 -05004925}
4926
4927/* The force flag allows the unlock to go ahead even if the lkb isn't granted.
4928 Regardless of what rsb queue the lock is on, it's removed and freed. */
4929
4930static int unlock_proc_lock(struct dlm_ls *ls, struct dlm_lkb *lkb)
4931{
David Teigland597d0ca2006-07-12 16:44:04 -05004932 struct dlm_args args;
4933 int error;
4934
David Teiglandd292c0c2008-02-06 23:27:04 -06004935 set_unlock_args(DLM_LKF_FORCEUNLOCK, lkb->lkb_ua, &args);
David Teigland597d0ca2006-07-12 16:44:04 -05004936
4937 error = unlock_lock(ls, lkb, &args);
4938 if (error == -DLM_EUNLOCK)
4939 error = 0;
4940 return error;
4941}
4942
David Teiglandef0c2bb2007-03-28 09:56:46 -05004943/* We have to release clear_proc_locks mutex before calling unlock_proc_lock()
4944 (which does lock_rsb) due to deadlock with receiving a message that does
4945 lock_rsb followed by dlm_user_add_ast() */
4946
4947static struct dlm_lkb *del_proc_lock(struct dlm_ls *ls,
4948 struct dlm_user_proc *proc)
4949{
4950 struct dlm_lkb *lkb = NULL;
4951
4952 mutex_lock(&ls->ls_clear_proc_locks);
4953 if (list_empty(&proc->locks))
4954 goto out;
4955
4956 lkb = list_entry(proc->locks.next, struct dlm_lkb, lkb_ownqueue);
4957 list_del_init(&lkb->lkb_ownqueue);
4958
4959 if (lkb->lkb_exflags & DLM_LKF_PERSISTENT)
4960 lkb->lkb_flags |= DLM_IFL_ORPHAN;
4961 else
4962 lkb->lkb_flags |= DLM_IFL_DEAD;
4963 out:
4964 mutex_unlock(&ls->ls_clear_proc_locks);
4965 return lkb;
4966}
4967
David Teigland597d0ca2006-07-12 16:44:04 -05004968/* The ls_clear_proc_locks mutex protects against dlm_user_add_asts() which
4969 1) references lkb->ua which we free here and 2) adds lkbs to proc->asts,
4970 which we clear here. */
4971
4972/* proc CLOSING flag is set so no more device_reads should look at proc->asts
4973 list, and no more device_writes should add lkb's to proc->locks list; so we
4974 shouldn't need to take asts_spin or locks_spin here. this assumes that
4975 device reads/writes/closes are serialized -- FIXME: we may need to serialize
4976 them ourself. */
4977
4978void dlm_clear_proc_locks(struct dlm_ls *ls, struct dlm_user_proc *proc)
4979{
4980 struct dlm_lkb *lkb, *safe;
4981
David Teigland85e86ed2007-05-18 08:58:15 -05004982 dlm_lock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05004983
David Teiglandef0c2bb2007-03-28 09:56:46 -05004984 while (1) {
4985 lkb = del_proc_lock(ls, proc);
4986 if (!lkb)
4987 break;
David Teigland84d8cd62007-05-29 08:44:23 -05004988 del_timeout(lkb);
David Teiglandef0c2bb2007-03-28 09:56:46 -05004989 if (lkb->lkb_exflags & DLM_LKF_PERSISTENT)
David Teigland597d0ca2006-07-12 16:44:04 -05004990 orphan_proc_lock(ls, lkb);
David Teiglandef0c2bb2007-03-28 09:56:46 -05004991 else
David Teigland597d0ca2006-07-12 16:44:04 -05004992 unlock_proc_lock(ls, lkb);
David Teigland597d0ca2006-07-12 16:44:04 -05004993
4994 /* this removes the reference for the proc->locks list
4995 added by dlm_user_request, it may result in the lkb
4996 being freed */
4997
4998 dlm_put_lkb(lkb);
4999 }
David Teiglanda1bc86e2007-01-15 10:34:52 -06005000
David Teiglandef0c2bb2007-03-28 09:56:46 -05005001 mutex_lock(&ls->ls_clear_proc_locks);
5002
David Teiglanda1bc86e2007-01-15 10:34:52 -06005003 /* in-progress unlocks */
5004 list_for_each_entry_safe(lkb, safe, &proc->unlocking, lkb_ownqueue) {
5005 list_del_init(&lkb->lkb_ownqueue);
5006 lkb->lkb_flags |= DLM_IFL_DEAD;
5007 dlm_put_lkb(lkb);
5008 }
5009
5010 list_for_each_entry_safe(lkb, safe, &proc->asts, lkb_astqueue) {
David Teigland8304d6f2011-02-21 14:58:21 -06005011 memset(&lkb->lkb_callbacks, 0,
5012 sizeof(struct dlm_callback) * DLM_CALLBACKS_SIZE);
5013 list_del_init(&lkb->lkb_astqueue);
David Teiglanda1bc86e2007-01-15 10:34:52 -06005014 dlm_put_lkb(lkb);
5015 }
5016
David Teigland597d0ca2006-07-12 16:44:04 -05005017 mutex_unlock(&ls->ls_clear_proc_locks);
David Teigland85e86ed2007-05-18 08:58:15 -05005018 dlm_unlock_recovery(ls);
David Teigland597d0ca2006-07-12 16:44:04 -05005019}
David Teiglanda1bc86e2007-01-15 10:34:52 -06005020
David Teigland84991372007-03-30 15:02:40 -05005021static void purge_proc_locks(struct dlm_ls *ls, struct dlm_user_proc *proc)
5022{
5023 struct dlm_lkb *lkb, *safe;
5024
5025 while (1) {
5026 lkb = NULL;
5027 spin_lock(&proc->locks_spin);
5028 if (!list_empty(&proc->locks)) {
5029 lkb = list_entry(proc->locks.next, struct dlm_lkb,
5030 lkb_ownqueue);
5031 list_del_init(&lkb->lkb_ownqueue);
5032 }
5033 spin_unlock(&proc->locks_spin);
5034
5035 if (!lkb)
5036 break;
5037
5038 lkb->lkb_flags |= DLM_IFL_DEAD;
5039 unlock_proc_lock(ls, lkb);
5040 dlm_put_lkb(lkb); /* ref from proc->locks list */
5041 }
5042
5043 spin_lock(&proc->locks_spin);
5044 list_for_each_entry_safe(lkb, safe, &proc->unlocking, lkb_ownqueue) {
5045 list_del_init(&lkb->lkb_ownqueue);
5046 lkb->lkb_flags |= DLM_IFL_DEAD;
5047 dlm_put_lkb(lkb);
5048 }
5049 spin_unlock(&proc->locks_spin);
5050
5051 spin_lock(&proc->asts_spin);
5052 list_for_each_entry_safe(lkb, safe, &proc->asts, lkb_astqueue) {
David Teigland8304d6f2011-02-21 14:58:21 -06005053 memset(&lkb->lkb_callbacks, 0,
5054 sizeof(struct dlm_callback) * DLM_CALLBACKS_SIZE);
5055 list_del_init(&lkb->lkb_astqueue);
David Teigland84991372007-03-30 15:02:40 -05005056 dlm_put_lkb(lkb);
5057 }
5058 spin_unlock(&proc->asts_spin);
5059}
5060
5061/* pid of 0 means purge all orphans */
5062
5063static void do_purge(struct dlm_ls *ls, int nodeid, int pid)
5064{
5065 struct dlm_lkb *lkb, *safe;
5066
5067 mutex_lock(&ls->ls_orphans_mutex);
5068 list_for_each_entry_safe(lkb, safe, &ls->ls_orphans, lkb_ownqueue) {
5069 if (pid && lkb->lkb_ownpid != pid)
5070 continue;
5071 unlock_proc_lock(ls, lkb);
5072 list_del_init(&lkb->lkb_ownqueue);
5073 dlm_put_lkb(lkb);
5074 }
5075 mutex_unlock(&ls->ls_orphans_mutex);
5076}
5077
5078static int send_purge(struct dlm_ls *ls, int nodeid, int pid)
5079{
5080 struct dlm_message *ms;
5081 struct dlm_mhandle *mh;
5082 int error;
5083
5084 error = _create_message(ls, sizeof(struct dlm_message), nodeid,
5085 DLM_MSG_PURGE, &ms, &mh);
5086 if (error)
5087 return error;
5088 ms->m_nodeid = nodeid;
5089 ms->m_pid = pid;
5090
5091 return send_message(mh, ms);
5092}
5093
5094int dlm_user_purge(struct dlm_ls *ls, struct dlm_user_proc *proc,
5095 int nodeid, int pid)
5096{
5097 int error = 0;
5098
5099 if (nodeid != dlm_our_nodeid()) {
5100 error = send_purge(ls, nodeid, pid);
5101 } else {
David Teigland85e86ed2007-05-18 08:58:15 -05005102 dlm_lock_recovery(ls);
David Teigland84991372007-03-30 15:02:40 -05005103 if (pid == current->pid)
5104 purge_proc_locks(ls, proc);
5105 else
5106 do_purge(ls, nodeid, pid);
David Teigland85e86ed2007-05-18 08:58:15 -05005107 dlm_unlock_recovery(ls);
David Teigland84991372007-03-30 15:02:40 -05005108 }
5109 return error;
5110}
5111