]>
Commit | Line | Data |
---|---|---|
6714d8e8 KH |
1 | /* -*- mode: c; c-basic-offset: 8; -*- |
2 | * vim: noexpandtab sw=8 ts=8 sts=0: | |
3 | * | |
4 | * dlmlock.c | |
5 | * | |
6 | * underlying calls for lock creation | |
7 | * | |
8 | * Copyright (C) 2004 Oracle. All rights reserved. | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU General Public | |
12 | * License as published by the Free Software Foundation; either | |
13 | * version 2 of the License, or (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | * General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public | |
21 | * License along with this program; if not, write to the | |
22 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | * Boston, MA 021110-1307, USA. | |
24 | * | |
25 | */ | |
26 | ||
27 | ||
28 | #include <linux/module.h> | |
29 | #include <linux/fs.h> | |
30 | #include <linux/types.h> | |
31 | #include <linux/slab.h> | |
32 | #include <linux/highmem.h> | |
33 | #include <linux/utsname.h> | |
34 | #include <linux/init.h> | |
35 | #include <linux/sysctl.h> | |
36 | #include <linux/random.h> | |
37 | #include <linux/blkdev.h> | |
38 | #include <linux/socket.h> | |
39 | #include <linux/inet.h> | |
40 | #include <linux/spinlock.h> | |
41 | #include <linux/delay.h> | |
42 | ||
43 | ||
44 | #include "cluster/heartbeat.h" | |
45 | #include "cluster/nodemanager.h" | |
46 | #include "cluster/tcp.h" | |
47 | ||
48 | #include "dlmapi.h" | |
49 | #include "dlmcommon.h" | |
50 | ||
51 | #include "dlmconvert.h" | |
52 | ||
53 | #define MLOG_MASK_PREFIX ML_DLM | |
54 | #include "cluster/masklog.h" | |
55 | ||
56 | static spinlock_t dlm_cookie_lock = SPIN_LOCK_UNLOCKED; | |
57 | static u64 dlm_next_cookie = 1; | |
58 | ||
59 | static enum dlm_status dlm_send_remote_lock_request(struct dlm_ctxt *dlm, | |
60 | struct dlm_lock_resource *res, | |
61 | struct dlm_lock *lock, int flags); | |
62 | static void dlm_init_lock(struct dlm_lock *newlock, int type, | |
63 | u8 node, u64 cookie); | |
64 | static void dlm_lock_release(struct kref *kref); | |
65 | static void dlm_lock_detach_lockres(struct dlm_lock *lock); | |
66 | ||
67 | /* Tell us whether we can grant a new lock request. | |
68 | * locking: | |
69 | * caller needs: res->spinlock | |
70 | * taken: none | |
71 | * held on exit: none | |
72 | * returns: 1 if the lock can be granted, 0 otherwise. | |
73 | */ | |
74 | static int dlm_can_grant_new_lock(struct dlm_lock_resource *res, | |
75 | struct dlm_lock *lock) | |
76 | { | |
77 | struct list_head *iter; | |
78 | struct dlm_lock *tmplock; | |
79 | ||
80 | list_for_each(iter, &res->granted) { | |
81 | tmplock = list_entry(iter, struct dlm_lock, list); | |
82 | ||
83 | if (!dlm_lock_compatible(tmplock->ml.type, lock->ml.type)) | |
84 | return 0; | |
85 | } | |
86 | ||
87 | list_for_each(iter, &res->converting) { | |
88 | tmplock = list_entry(iter, struct dlm_lock, list); | |
89 | ||
90 | if (!dlm_lock_compatible(tmplock->ml.type, lock->ml.type)) | |
91 | return 0; | |
92 | } | |
93 | ||
94 | return 1; | |
95 | } | |
96 | ||
97 | /* performs lock creation at the lockres master site | |
98 | * locking: | |
99 | * caller needs: none | |
100 | * taken: takes and drops res->spinlock | |
101 | * held on exit: none | |
102 | * returns: DLM_NORMAL, DLM_NOTQUEUED | |
103 | */ | |
104 | static enum dlm_status dlmlock_master(struct dlm_ctxt *dlm, | |
105 | struct dlm_lock_resource *res, | |
106 | struct dlm_lock *lock, int flags) | |
107 | { | |
108 | int call_ast = 0, kick_thread = 0; | |
109 | enum dlm_status status = DLM_NORMAL; | |
110 | ||
111 | mlog_entry("type=%d\n", lock->ml.type); | |
112 | ||
113 | spin_lock(&res->spinlock); | |
114 | /* if called from dlm_create_lock_handler, need to | |
115 | * ensure it will not sleep in dlm_wait_on_lockres */ | |
116 | status = __dlm_lockres_state_to_status(res); | |
117 | if (status != DLM_NORMAL && | |
118 | lock->ml.node != dlm->node_num) { | |
119 | /* erf. state changed after lock was dropped. */ | |
120 | spin_unlock(&res->spinlock); | |
121 | dlm_error(status); | |
122 | return status; | |
123 | } | |
124 | __dlm_wait_on_lockres(res); | |
125 | __dlm_lockres_reserve_ast(res); | |
126 | ||
127 | if (dlm_can_grant_new_lock(res, lock)) { | |
128 | mlog(0, "I can grant this lock right away\n"); | |
129 | /* got it right away */ | |
130 | lock->lksb->status = DLM_NORMAL; | |
131 | status = DLM_NORMAL; | |
132 | dlm_lock_get(lock); | |
133 | list_add_tail(&lock->list, &res->granted); | |
134 | ||
135 | /* for the recovery lock, we can't allow the ast | |
136 | * to be queued since the dlmthread is already | |
137 | * frozen. but the recovery lock is always locked | |
138 | * with LKM_NOQUEUE so we do not need the ast in | |
139 | * this special case */ | |
140 | if (!dlm_is_recovery_lock(res->lockname.name, | |
141 | res->lockname.len)) { | |
142 | kick_thread = 1; | |
143 | call_ast = 1; | |
c03872f5 KH |
144 | } else { |
145 | mlog(0, "%s: returning DLM_NORMAL to " | |
146 | "node %u for reco lock\n", dlm->name, | |
147 | lock->ml.node); | |
6714d8e8 KH |
148 | } |
149 | } else { | |
150 | /* for NOQUEUE request, unless we get the | |
151 | * lock right away, return DLM_NOTQUEUED */ | |
c03872f5 | 152 | if (flags & LKM_NOQUEUE) { |
6714d8e8 | 153 | status = DLM_NOTQUEUED; |
c03872f5 KH |
154 | if (dlm_is_recovery_lock(res->lockname.name, |
155 | res->lockname.len)) { | |
156 | mlog(0, "%s: returning NOTQUEUED to " | |
157 | "node %u for reco lock\n", dlm->name, | |
158 | lock->ml.node); | |
159 | } | |
160 | } else { | |
6714d8e8 KH |
161 | dlm_lock_get(lock); |
162 | list_add_tail(&lock->list, &res->blocked); | |
163 | kick_thread = 1; | |
164 | } | |
165 | } | |
166 | ||
167 | spin_unlock(&res->spinlock); | |
168 | wake_up(&res->wq); | |
169 | ||
170 | /* either queue the ast or release it */ | |
171 | if (call_ast) | |
172 | dlm_queue_ast(dlm, lock); | |
173 | else | |
174 | dlm_lockres_release_ast(dlm, res); | |
175 | ||
176 | dlm_lockres_calc_usage(dlm, res); | |
177 | if (kick_thread) | |
178 | dlm_kick_thread(dlm, res); | |
179 | ||
180 | return status; | |
181 | } | |
182 | ||
183 | void dlm_revert_pending_lock(struct dlm_lock_resource *res, | |
184 | struct dlm_lock *lock) | |
185 | { | |
186 | /* remove from local queue if it failed */ | |
187 | list_del_init(&lock->list); | |
188 | lock->lksb->flags &= ~DLM_LKSB_GET_LVB; | |
189 | } | |
190 | ||
191 | ||
192 | /* | |
193 | * locking: | |
194 | * caller needs: none | |
195 | * taken: takes and drops res->spinlock | |
196 | * held on exit: none | |
197 | * returns: DLM_DENIED, DLM_RECOVERING, or net status | |
198 | */ | |
199 | static enum dlm_status dlmlock_remote(struct dlm_ctxt *dlm, | |
200 | struct dlm_lock_resource *res, | |
201 | struct dlm_lock *lock, int flags) | |
202 | { | |
203 | enum dlm_status status = DLM_DENIED; | |
204 | ||
205 | mlog_entry("type=%d\n", lock->ml.type); | |
206 | mlog(0, "lockres %.*s, flags = 0x%x\n", res->lockname.len, | |
207 | res->lockname.name, flags); | |
208 | ||
209 | spin_lock(&res->spinlock); | |
210 | ||
211 | /* will exit this call with spinlock held */ | |
212 | __dlm_wait_on_lockres(res); | |
213 | res->state |= DLM_LOCK_RES_IN_PROGRESS; | |
214 | ||
215 | /* add lock to local (secondary) queue */ | |
216 | dlm_lock_get(lock); | |
217 | list_add_tail(&lock->list, &res->blocked); | |
218 | lock->lock_pending = 1; | |
219 | spin_unlock(&res->spinlock); | |
220 | ||
221 | /* spec seems to say that you will get DLM_NORMAL when the lock | |
222 | * has been queued, meaning we need to wait for a reply here. */ | |
223 | status = dlm_send_remote_lock_request(dlm, res, lock, flags); | |
224 | ||
225 | spin_lock(&res->spinlock); | |
226 | res->state &= ~DLM_LOCK_RES_IN_PROGRESS; | |
227 | lock->lock_pending = 0; | |
228 | if (status != DLM_NORMAL) { | |
229 | if (status != DLM_NOTQUEUED) | |
230 | dlm_error(status); | |
231 | dlm_revert_pending_lock(res, lock); | |
232 | dlm_lock_put(lock); | |
558c70c5 KH |
233 | } else if (dlm_is_recovery_lock(res->lockname.name, |
234 | res->lockname.len)) { | |
235 | /* special case for the $RECOVERY lock. | |
236 | * there will never be an AST delivered to put | |
237 | * this lock on the proper secondary queue | |
238 | * (granted), so do it manually. */ | |
239 | mlog(0, "%s: $RECOVERY lock for this node (%u) is " | |
240 | "mastered by %u; got lock, manually granting (no ast)\n", | |
241 | dlm->name, dlm->node_num, res->owner); | |
f116629d | 242 | list_move_tail(&lock->list, &res->granted); |
6714d8e8 KH |
243 | } |
244 | spin_unlock(&res->spinlock); | |
245 | ||
246 | dlm_lockres_calc_usage(dlm, res); | |
247 | ||
248 | wake_up(&res->wq); | |
249 | return status; | |
250 | } | |
251 | ||
252 | ||
253 | /* for remote lock creation. | |
254 | * locking: | |
255 | * caller needs: none, but need res->state & DLM_LOCK_RES_IN_PROGRESS | |
256 | * taken: none | |
257 | * held on exit: none | |
258 | * returns: DLM_NOLOCKMGR, or net status | |
259 | */ | |
260 | static enum dlm_status dlm_send_remote_lock_request(struct dlm_ctxt *dlm, | |
261 | struct dlm_lock_resource *res, | |
262 | struct dlm_lock *lock, int flags) | |
263 | { | |
264 | struct dlm_create_lock create; | |
265 | int tmpret, status = 0; | |
266 | enum dlm_status ret; | |
267 | ||
268 | mlog_entry_void(); | |
269 | ||
270 | memset(&create, 0, sizeof(create)); | |
271 | create.node_idx = dlm->node_num; | |
272 | create.requested_type = lock->ml.type; | |
273 | create.cookie = lock->ml.cookie; | |
274 | create.namelen = res->lockname.len; | |
275 | create.flags = cpu_to_be32(flags); | |
276 | memcpy(create.name, res->lockname.name, create.namelen); | |
277 | ||
278 | tmpret = o2net_send_message(DLM_CREATE_LOCK_MSG, dlm->key, &create, | |
279 | sizeof(create), res->owner, &status); | |
280 | if (tmpret >= 0) { | |
281 | // successfully sent and received | |
282 | ret = status; // this is already a dlm_status | |
e4eb0368 KH |
283 | if (ret == DLM_RECOVERING) { |
284 | mlog(ML_ERROR, "%s:%.*s: BUG. this is a stale lockres " | |
285 | "no longer owned by %u. that node is coming back " | |
286 | "up currently.\n", dlm->name, create.namelen, | |
287 | create.name, res->owner); | |
288 | dlm_print_one_lock_resource(res); | |
289 | BUG(); | |
290 | } | |
6714d8e8 KH |
291 | } else { |
292 | mlog_errno(tmpret); | |
293 | if (dlm_is_host_down(tmpret)) { | |
294 | ret = DLM_RECOVERING; | |
295 | mlog(0, "node %u died so returning DLM_RECOVERING " | |
296 | "from lock message!\n", res->owner); | |
297 | } else { | |
298 | ret = dlm_err_to_dlm_status(tmpret); | |
299 | } | |
300 | } | |
301 | ||
302 | return ret; | |
303 | } | |
304 | ||
305 | void dlm_lock_get(struct dlm_lock *lock) | |
306 | { | |
307 | kref_get(&lock->lock_refs); | |
308 | } | |
309 | ||
310 | void dlm_lock_put(struct dlm_lock *lock) | |
311 | { | |
312 | kref_put(&lock->lock_refs, dlm_lock_release); | |
313 | } | |
314 | ||
315 | static void dlm_lock_release(struct kref *kref) | |
316 | { | |
317 | struct dlm_lock *lock; | |
318 | ||
319 | lock = container_of(kref, struct dlm_lock, lock_refs); | |
320 | ||
321 | BUG_ON(!list_empty(&lock->list)); | |
322 | BUG_ON(!list_empty(&lock->ast_list)); | |
323 | BUG_ON(!list_empty(&lock->bast_list)); | |
324 | BUG_ON(lock->ast_pending); | |
325 | BUG_ON(lock->bast_pending); | |
326 | ||
327 | dlm_lock_detach_lockres(lock); | |
328 | ||
329 | if (lock->lksb_kernel_allocated) { | |
330 | mlog(0, "freeing kernel-allocated lksb\n"); | |
331 | kfree(lock->lksb); | |
332 | } | |
333 | kfree(lock); | |
334 | } | |
335 | ||
336 | /* associate a lock with it's lockres, getting a ref on the lockres */ | |
337 | void dlm_lock_attach_lockres(struct dlm_lock *lock, | |
338 | struct dlm_lock_resource *res) | |
339 | { | |
340 | dlm_lockres_get(res); | |
341 | lock->lockres = res; | |
342 | } | |
343 | ||
344 | /* drop ref on lockres, if there is still one associated with lock */ | |
345 | static void dlm_lock_detach_lockres(struct dlm_lock *lock) | |
346 | { | |
347 | struct dlm_lock_resource *res; | |
348 | ||
349 | res = lock->lockres; | |
350 | if (res) { | |
351 | lock->lockres = NULL; | |
352 | mlog(0, "removing lock's lockres reference\n"); | |
353 | dlm_lockres_put(res); | |
354 | } | |
355 | } | |
356 | ||
357 | static void dlm_init_lock(struct dlm_lock *newlock, int type, | |
358 | u8 node, u64 cookie) | |
359 | { | |
360 | INIT_LIST_HEAD(&newlock->list); | |
361 | INIT_LIST_HEAD(&newlock->ast_list); | |
362 | INIT_LIST_HEAD(&newlock->bast_list); | |
363 | spin_lock_init(&newlock->spinlock); | |
364 | newlock->ml.type = type; | |
365 | newlock->ml.convert_type = LKM_IVMODE; | |
366 | newlock->ml.highest_blocked = LKM_IVMODE; | |
367 | newlock->ml.node = node; | |
368 | newlock->ml.pad1 = 0; | |
369 | newlock->ml.list = 0; | |
370 | newlock->ml.flags = 0; | |
371 | newlock->ast = NULL; | |
372 | newlock->bast = NULL; | |
373 | newlock->astdata = NULL; | |
374 | newlock->ml.cookie = cpu_to_be64(cookie); | |
375 | newlock->ast_pending = 0; | |
376 | newlock->bast_pending = 0; | |
377 | newlock->convert_pending = 0; | |
378 | newlock->lock_pending = 0; | |
379 | newlock->unlock_pending = 0; | |
380 | newlock->cancel_pending = 0; | |
381 | newlock->lksb_kernel_allocated = 0; | |
382 | ||
383 | kref_init(&newlock->lock_refs); | |
384 | } | |
385 | ||
386 | struct dlm_lock * dlm_new_lock(int type, u8 node, u64 cookie, | |
387 | struct dlm_lockstatus *lksb) | |
388 | { | |
389 | struct dlm_lock *lock; | |
390 | int kernel_allocated = 0; | |
391 | ||
392 | lock = kcalloc(1, sizeof(*lock), GFP_KERNEL); | |
393 | if (!lock) | |
394 | return NULL; | |
395 | ||
396 | if (!lksb) { | |
397 | /* zero memory only if kernel-allocated */ | |
398 | lksb = kcalloc(1, sizeof(*lksb), GFP_KERNEL); | |
399 | if (!lksb) { | |
400 | kfree(lock); | |
401 | return NULL; | |
402 | } | |
403 | kernel_allocated = 1; | |
404 | } | |
405 | ||
406 | dlm_init_lock(lock, type, node, cookie); | |
407 | if (kernel_allocated) | |
408 | lock->lksb_kernel_allocated = 1; | |
409 | lock->lksb = lksb; | |
410 | lksb->lockid = lock; | |
411 | return lock; | |
412 | } | |
413 | ||
414 | /* handler for lock creation net message | |
415 | * locking: | |
416 | * caller needs: none | |
417 | * taken: takes and drops res->spinlock | |
418 | * held on exit: none | |
419 | * returns: DLM_NORMAL, DLM_SYSERR, DLM_IVLOCKID, DLM_NOTQUEUED | |
420 | */ | |
421 | int dlm_create_lock_handler(struct o2net_msg *msg, u32 len, void *data) | |
422 | { | |
423 | struct dlm_ctxt *dlm = data; | |
424 | struct dlm_create_lock *create = (struct dlm_create_lock *)msg->buf; | |
425 | struct dlm_lock_resource *res = NULL; | |
426 | struct dlm_lock *newlock = NULL; | |
427 | struct dlm_lockstatus *lksb = NULL; | |
428 | enum dlm_status status = DLM_NORMAL; | |
429 | char *name; | |
430 | unsigned int namelen; | |
431 | ||
432 | BUG_ON(!dlm); | |
433 | ||
434 | mlog_entry_void(); | |
435 | ||
436 | if (!dlm_grab(dlm)) | |
437 | return DLM_REJECTED; | |
438 | ||
6714d8e8 KH |
439 | name = create->name; |
440 | namelen = create->namelen; | |
e4eb0368 KH |
441 | status = DLM_RECOVERING; |
442 | if (!dlm_domain_fully_joined(dlm)) { | |
443 | mlog(ML_ERROR, "Domain %s not fully joined, but node %u is " | |
444 | "sending a create_lock message for lock %.*s!\n", | |
445 | dlm->name, create->node_idx, namelen, name); | |
446 | dlm_error(status); | |
447 | goto leave; | |
448 | } | |
6714d8e8 KH |
449 | |
450 | status = DLM_IVBUFLEN; | |
451 | if (namelen > DLM_LOCKID_NAME_MAX) { | |
452 | dlm_error(status); | |
453 | goto leave; | |
454 | } | |
455 | ||
456 | status = DLM_SYSERR; | |
457 | newlock = dlm_new_lock(create->requested_type, | |
458 | create->node_idx, | |
459 | be64_to_cpu(create->cookie), NULL); | |
460 | if (!newlock) { | |
461 | dlm_error(status); | |
462 | goto leave; | |
463 | } | |
464 | ||
465 | lksb = newlock->lksb; | |
466 | ||
467 | if (be32_to_cpu(create->flags) & LKM_GET_LVB) { | |
468 | lksb->flags |= DLM_LKSB_GET_LVB; | |
469 | mlog(0, "set DLM_LKSB_GET_LVB flag\n"); | |
470 | } | |
471 | ||
472 | status = DLM_IVLOCKID; | |
473 | res = dlm_lookup_lockres(dlm, name, namelen); | |
474 | if (!res) { | |
475 | dlm_error(status); | |
476 | goto leave; | |
477 | } | |
478 | ||
479 | spin_lock(&res->spinlock); | |
480 | status = __dlm_lockres_state_to_status(res); | |
481 | spin_unlock(&res->spinlock); | |
482 | ||
483 | if (status != DLM_NORMAL) { | |
484 | mlog(0, "lockres recovering/migrating/in-progress\n"); | |
485 | goto leave; | |
486 | } | |
487 | ||
488 | dlm_lock_attach_lockres(newlock, res); | |
489 | ||
490 | status = dlmlock_master(dlm, res, newlock, be32_to_cpu(create->flags)); | |
491 | leave: | |
492 | if (status != DLM_NORMAL) | |
493 | if (newlock) | |
494 | dlm_lock_put(newlock); | |
495 | ||
496 | if (res) | |
497 | dlm_lockres_put(res); | |
498 | ||
499 | dlm_put(dlm); | |
500 | ||
501 | return status; | |
502 | } | |
503 | ||
504 | ||
505 | /* fetch next node-local (u8 nodenum + u56 cookie) into u64 */ | |
506 | static inline void dlm_get_next_cookie(u8 node_num, u64 *cookie) | |
507 | { | |
508 | u64 tmpnode = node_num; | |
509 | ||
510 | /* shift single byte of node num into top 8 bits */ | |
511 | tmpnode <<= 56; | |
512 | ||
513 | spin_lock(&dlm_cookie_lock); | |
514 | *cookie = (dlm_next_cookie | tmpnode); | |
515 | if (++dlm_next_cookie & 0xff00000000000000ull) { | |
516 | mlog(0, "This node's cookie will now wrap!\n"); | |
517 | dlm_next_cookie = 1; | |
518 | } | |
519 | spin_unlock(&dlm_cookie_lock); | |
520 | } | |
521 | ||
522 | enum dlm_status dlmlock(struct dlm_ctxt *dlm, int mode, | |
523 | struct dlm_lockstatus *lksb, int flags, | |
524 | const char *name, dlm_astlockfunc_t *ast, void *data, | |
525 | dlm_bastlockfunc_t *bast) | |
526 | { | |
527 | enum dlm_status status; | |
528 | struct dlm_lock_resource *res = NULL; | |
529 | struct dlm_lock *lock = NULL; | |
530 | int convert = 0, recovery = 0; | |
531 | ||
532 | /* yes this function is a mess. | |
533 | * TODO: clean this up. lots of common code in the | |
534 | * lock and convert paths, especially in the retry blocks */ | |
535 | if (!lksb) { | |
536 | dlm_error(DLM_BADARGS); | |
537 | return DLM_BADARGS; | |
538 | } | |
539 | ||
540 | status = DLM_BADPARAM; | |
541 | if (mode != LKM_EXMODE && mode != LKM_PRMODE && mode != LKM_NLMODE) { | |
542 | dlm_error(status); | |
543 | goto error; | |
544 | } | |
545 | ||
546 | if (flags & ~LKM_VALID_FLAGS) { | |
547 | dlm_error(status); | |
548 | goto error; | |
549 | } | |
550 | ||
551 | convert = (flags & LKM_CONVERT); | |
552 | recovery = (flags & LKM_RECOVERY); | |
553 | ||
554 | if (recovery && | |
555 | (!dlm_is_recovery_lock(name, strlen(name)) || convert) ) { | |
556 | dlm_error(status); | |
557 | goto error; | |
558 | } | |
559 | if (convert && (flags & LKM_LOCAL)) { | |
560 | mlog(ML_ERROR, "strange LOCAL convert request!\n"); | |
561 | goto error; | |
562 | } | |
563 | ||
564 | if (convert) { | |
565 | /* CONVERT request */ | |
566 | ||
567 | /* if converting, must pass in a valid dlm_lock */ | |
568 | lock = lksb->lockid; | |
569 | if (!lock) { | |
570 | mlog(ML_ERROR, "NULL lock pointer in convert " | |
571 | "request\n"); | |
572 | goto error; | |
573 | } | |
574 | ||
575 | res = lock->lockres; | |
576 | if (!res) { | |
577 | mlog(ML_ERROR, "NULL lockres pointer in convert " | |
578 | "request\n"); | |
579 | goto error; | |
580 | } | |
581 | dlm_lockres_get(res); | |
582 | ||
583 | /* XXX: for ocfs2 purposes, the ast/bast/astdata/lksb are | |
584 | * static after the original lock call. convert requests will | |
585 | * ensure that everything is the same, or return DLM_BADARGS. | |
586 | * this means that DLM_DENIED_NOASTS will never be returned. | |
587 | */ | |
588 | if (lock->lksb != lksb || lock->ast != ast || | |
589 | lock->bast != bast || lock->astdata != data) { | |
590 | status = DLM_BADARGS; | |
591 | mlog(ML_ERROR, "new args: lksb=%p, ast=%p, bast=%p, " | |
592 | "astdata=%p\n", lksb, ast, bast, data); | |
593 | mlog(ML_ERROR, "orig args: lksb=%p, ast=%p, bast=%p, " | |
594 | "astdata=%p\n", lock->lksb, lock->ast, | |
595 | lock->bast, lock->astdata); | |
596 | goto error; | |
597 | } | |
598 | retry_convert: | |
599 | dlm_wait_for_recovery(dlm); | |
600 | ||
601 | if (res->owner == dlm->node_num) | |
602 | status = dlmconvert_master(dlm, res, lock, flags, mode); | |
603 | else | |
604 | status = dlmconvert_remote(dlm, res, lock, flags, mode); | |
605 | if (status == DLM_RECOVERING || status == DLM_MIGRATING || | |
606 | status == DLM_FORWARD) { | |
607 | /* for now, see how this works without sleeping | |
608 | * and just retry right away. I suspect the reco | |
609 | * or migration will complete fast enough that | |
610 | * no waiting will be necessary */ | |
611 | mlog(0, "retrying convert with migration/recovery/" | |
612 | "in-progress\n"); | |
613 | msleep(100); | |
614 | goto retry_convert; | |
615 | } | |
616 | } else { | |
617 | u64 tmpcookie; | |
618 | ||
619 | /* LOCK request */ | |
620 | status = DLM_BADARGS; | |
621 | if (!name) { | |
622 | dlm_error(status); | |
623 | goto error; | |
624 | } | |
625 | ||
626 | status = DLM_IVBUFLEN; | |
627 | if (strlen(name) > DLM_LOCKID_NAME_MAX || strlen(name) < 1) { | |
628 | dlm_error(status); | |
629 | goto error; | |
630 | } | |
631 | ||
632 | dlm_get_next_cookie(dlm->node_num, &tmpcookie); | |
633 | lock = dlm_new_lock(mode, dlm->node_num, tmpcookie, lksb); | |
634 | if (!lock) { | |
635 | dlm_error(status); | |
636 | goto error; | |
637 | } | |
638 | ||
639 | if (!recovery) | |
640 | dlm_wait_for_recovery(dlm); | |
641 | ||
642 | /* find or create the lock resource */ | |
643 | res = dlm_get_lock_resource(dlm, name, flags); | |
644 | if (!res) { | |
645 | status = DLM_IVLOCKID; | |
646 | dlm_error(status); | |
647 | goto error; | |
648 | } | |
649 | ||
650 | mlog(0, "type=%d, flags = 0x%x\n", mode, flags); | |
651 | mlog(0, "creating lock: lock=%p res=%p\n", lock, res); | |
652 | ||
653 | dlm_lock_attach_lockres(lock, res); | |
654 | lock->ast = ast; | |
655 | lock->bast = bast; | |
656 | lock->astdata = data; | |
657 | ||
658 | retry_lock: | |
659 | if (flags & LKM_VALBLK) { | |
660 | mlog(0, "LKM_VALBLK passed by caller\n"); | |
661 | ||
662 | /* LVB requests for non PR, PW or EX locks are | |
663 | * ignored. */ | |
664 | if (mode < LKM_PRMODE) | |
665 | flags &= ~LKM_VALBLK; | |
666 | else { | |
667 | flags |= LKM_GET_LVB; | |
668 | lock->lksb->flags |= DLM_LKSB_GET_LVB; | |
669 | } | |
670 | } | |
671 | ||
672 | if (res->owner == dlm->node_num) | |
673 | status = dlmlock_master(dlm, res, lock, flags); | |
674 | else | |
675 | status = dlmlock_remote(dlm, res, lock, flags); | |
676 | ||
677 | if (status == DLM_RECOVERING || status == DLM_MIGRATING || | |
678 | status == DLM_FORWARD) { | |
679 | mlog(0, "retrying lock with migration/" | |
680 | "recovery/in progress\n"); | |
681 | msleep(100); | |
44465a7d KH |
682 | /* no waiting for dlm_reco_thread */ |
683 | if (recovery) { | |
684 | if (status == DLM_RECOVERING) { | |
685 | mlog(0, "%s: got RECOVERING " | |
686 | "for $REOCVERY lock, master " | |
687 | "was %u\n", dlm->name, | |
688 | res->owner); | |
689 | dlm_wait_for_node_death(dlm, res->owner, | |
690 | DLM_NODE_DEATH_WAIT_MAX); | |
691 | } | |
692 | } else { | |
693 | dlm_wait_for_recovery(dlm); | |
694 | } | |
6714d8e8 KH |
695 | goto retry_lock; |
696 | } | |
697 | ||
698 | if (status != DLM_NORMAL) { | |
699 | lock->lksb->flags &= ~DLM_LKSB_GET_LVB; | |
700 | if (status != DLM_NOTQUEUED) | |
701 | dlm_error(status); | |
702 | goto error; | |
703 | } | |
704 | } | |
705 | ||
706 | error: | |
707 | if (status != DLM_NORMAL) { | |
708 | if (lock && !convert) | |
709 | dlm_lock_put(lock); | |
710 | // this is kind of unnecessary | |
711 | lksb->status = status; | |
712 | } | |
713 | ||
714 | /* put lockres ref from the convert path | |
715 | * or from dlm_get_lock_resource */ | |
716 | if (res) | |
717 | dlm_lockres_put(res); | |
718 | ||
719 | return status; | |
720 | } | |
721 | EXPORT_SYMBOL_GPL(dlmlock); |