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