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