]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/ocfs2/dlm/userdlm.c
ocfs2: catch an invalid ast case in dlmfs
[mirror_ubuntu-artful-kernel.git] / fs / ocfs2 / dlm / userdlm.c
CommitLineData
8df08c89
MF
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * userdlm.c
5 *
6 * Code which implements the kernel side of a minimal userspace
7 * interface to our DLM.
8 *
9 * Many of the functions here are pared down versions of dlmglue.c
10 * functions.
11 *
12 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public
16 * License as published by the Free Software Foundation; either
17 * version 2 of the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public
25 * License along with this program; if not, write to the
26 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 * Boston, MA 021110-1307, USA.
28 */
29
aee93ac4 30#include <linux/signal.h>
8df08c89
MF
31
32#include <linux/module.h>
33#include <linux/fs.h>
34#include <linux/types.h>
35#include <linux/crc32.h>
36
37
38#include "cluster/nodemanager.h"
39#include "cluster/heartbeat.h"
40#include "cluster/tcp.h"
41
42#include "dlmapi.h"
43
44#include "userdlm.h"
45
46#define MLOG_MASK_PREFIX ML_DLMFS
47#include "cluster/masklog.h"
48
49static inline int user_check_wait_flag(struct user_lock_res *lockres,
50 int flag)
51{
52 int ret;
53
54 spin_lock(&lockres->l_lock);
55 ret = lockres->l_flags & flag;
56 spin_unlock(&lockres->l_lock);
57
58 return ret;
59}
60
61static inline void user_wait_on_busy_lock(struct user_lock_res *lockres)
62
63{
64 wait_event(lockres->l_event,
65 !user_check_wait_flag(lockres, USER_LOCK_BUSY));
66}
67
68static inline void user_wait_on_blocked_lock(struct user_lock_res *lockres)
69
70{
71 wait_event(lockres->l_event,
72 !user_check_wait_flag(lockres, USER_LOCK_BLOCKED));
73}
74
75/* I heart container_of... */
76static inline struct dlm_ctxt *
77dlm_ctxt_from_user_lockres(struct user_lock_res *lockres)
78{
79 struct dlmfs_inode_private *ip;
80
81 ip = container_of(lockres,
82 struct dlmfs_inode_private,
83 ip_lockres);
84 return ip->ip_dlm;
85}
86
87static struct inode *
88user_dlm_inode_from_user_lockres(struct user_lock_res *lockres)
89{
90 struct dlmfs_inode_private *ip;
91
92 ip = container_of(lockres,
93 struct dlmfs_inode_private,
94 ip_lockres);
95 return &ip->ip_vfs_inode;
96}
97
98static inline void user_recover_from_dlm_error(struct user_lock_res *lockres)
99{
100 spin_lock(&lockres->l_lock);
101 lockres->l_flags &= ~USER_LOCK_BUSY;
102 spin_unlock(&lockres->l_lock);
103}
104
105#define user_log_dlm_error(_func, _stat, _lockres) do { \
106 mlog(ML_ERROR, "Dlm error \"%s\" while calling %s on " \
107 "resource %s: %s\n", dlm_errname(_stat), _func, \
108 _lockres->l_name, dlm_errmsg(_stat)); \
109} while (0)
110
111/* WARNING: This function lives in a world where the only three lock
112 * levels are EX, PR, and NL. It *will* have to be adjusted when more
113 * lock types are added. */
114static inline int user_highest_compat_lock_level(int level)
115{
116 int new_level = LKM_EXMODE;
117
118 if (level == LKM_EXMODE)
119 new_level = LKM_NLMODE;
120 else if (level == LKM_PRMODE)
121 new_level = LKM_PRMODE;
122 return new_level;
123}
124
125static void user_ast(void *opaque)
126{
127 struct user_lock_res *lockres = opaque;
128 struct dlm_lockstatus *lksb;
129
130 mlog(0, "AST fired for lockres %s\n", lockres->l_name);
131
132 spin_lock(&lockres->l_lock);
133
134 lksb = &(lockres->l_lksb);
135 if (lksb->status != DLM_NORMAL) {
136 mlog(ML_ERROR, "lksb status value of %u on lockres %s\n",
137 lksb->status, lockres->l_name);
138 spin_unlock(&lockres->l_lock);
139 return;
140 }
141
cc6eb725
MF
142 mlog_bug_on_msg(lockres->l_requested == LKM_IVMODE,
143 "Lockres %s, requested ivmode. flags 0x%x\n",
144 lockres->l_name, lockres->l_flags);
145
8df08c89
MF
146 /* we're downconverting. */
147 if (lockres->l_requested < lockres->l_level) {
148 if (lockres->l_requested <=
149 user_highest_compat_lock_level(lockres->l_blocking)) {
150 lockres->l_blocking = LKM_NLMODE;
151 lockres->l_flags &= ~USER_LOCK_BLOCKED;
152 }
153 }
154
155 lockres->l_level = lockres->l_requested;
156 lockres->l_requested = LKM_IVMODE;
157 lockres->l_flags |= USER_LOCK_ATTACHED;
158 lockres->l_flags &= ~USER_LOCK_BUSY;
159
160 spin_unlock(&lockres->l_lock);
161
162 wake_up(&lockres->l_event);
163}
164
165static inline void user_dlm_grab_inode_ref(struct user_lock_res *lockres)
166{
167 struct inode *inode;
168 inode = user_dlm_inode_from_user_lockres(lockres);
169 if (!igrab(inode))
170 BUG();
171}
172
173static void user_dlm_unblock_lock(void *opaque);
174
175static void __user_dlm_queue_lockres(struct user_lock_res *lockres)
176{
177 if (!(lockres->l_flags & USER_LOCK_QUEUED)) {
178 user_dlm_grab_inode_ref(lockres);
179
180 INIT_WORK(&lockres->l_work, user_dlm_unblock_lock,
181 lockres);
182
183 queue_work(user_dlm_worker, &lockres->l_work);
184 lockres->l_flags |= USER_LOCK_QUEUED;
185 }
186}
187
188static void __user_dlm_cond_queue_lockres(struct user_lock_res *lockres)
189{
190 int queue = 0;
191
192 if (!(lockres->l_flags & USER_LOCK_BLOCKED))
193 return;
194
195 switch (lockres->l_blocking) {
196 case LKM_EXMODE:
197 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
198 queue = 1;
199 break;
200 case LKM_PRMODE:
201 if (!lockres->l_ex_holders)
202 queue = 1;
203 break;
204 default:
205 BUG();
206 }
207
208 if (queue)
209 __user_dlm_queue_lockres(lockres);
210}
211
212static void user_bast(void *opaque, int level)
213{
214 struct user_lock_res *lockres = opaque;
215
216 mlog(0, "Blocking AST fired for lockres %s. Blocking level %d\n",
217 lockres->l_name, level);
218
219 spin_lock(&lockres->l_lock);
220 lockres->l_flags |= USER_LOCK_BLOCKED;
221 if (level > lockres->l_blocking)
222 lockres->l_blocking = level;
223
224 __user_dlm_queue_lockres(lockres);
225 spin_unlock(&lockres->l_lock);
226
227 wake_up(&lockres->l_event);
228}
229
230static void user_unlock_ast(void *opaque, enum dlm_status status)
231{
232 struct user_lock_res *lockres = opaque;
233
234 mlog(0, "UNLOCK AST called on lock %s\n", lockres->l_name);
235
236 if (status != DLM_NORMAL)
237 mlog(ML_ERROR, "Dlm returns status %d\n", status);
238
239 spin_lock(&lockres->l_lock);
240 if (lockres->l_flags & USER_LOCK_IN_TEARDOWN)
241 lockres->l_level = LKM_IVMODE;
242 else {
243 lockres->l_requested = LKM_IVMODE; /* cancel an
244 * upconvert
245 * request. */
246 lockres->l_flags &= ~USER_LOCK_IN_CANCEL;
247 /* we want the unblock thread to look at it again
248 * now. */
249 __user_dlm_queue_lockres(lockres);
250 }
251
252 lockres->l_flags &= ~USER_LOCK_BUSY;
253 spin_unlock(&lockres->l_lock);
254
255 wake_up(&lockres->l_event);
256}
257
258static inline void user_dlm_drop_inode_ref(struct user_lock_res *lockres)
259{
260 struct inode *inode;
261 inode = user_dlm_inode_from_user_lockres(lockres);
262 iput(inode);
263}
264
265static void user_dlm_unblock_lock(void *opaque)
266{
267 int new_level, status;
268 struct user_lock_res *lockres = (struct user_lock_res *) opaque;
269 struct dlm_ctxt *dlm = dlm_ctxt_from_user_lockres(lockres);
270
271 mlog(0, "processing lockres %s\n", lockres->l_name);
272
273 spin_lock(&lockres->l_lock);
274
1f7bc828
MF
275 mlog_bug_on_msg(!(lockres->l_flags & USER_LOCK_QUEUED),
276 "Lockres %s, flags 0x%x\n",
277 lockres->l_name, lockres->l_flags);
8df08c89 278
1f7bc828
MF
279 /* notice that we don't clear USER_LOCK_BLOCKED here. If it's
280 * set, we want user_ast clear it. */
8df08c89
MF
281 lockres->l_flags &= ~USER_LOCK_QUEUED;
282
1f7bc828
MF
283 /* It's valid to get here and no longer be blocked - if we get
284 * several basts in a row, we might be queued by the first
285 * one, the unblock thread might run and clear the queued
286 * flag, and finally we might get another bast which re-queues
287 * us before our ast for the downconvert is called. */
288 if (!(lockres->l_flags & USER_LOCK_BLOCKED)) {
289 mlog(0, "Lockres %s, flags 0x%x: queued but not blocking\n",
290 lockres->l_name, lockres->l_flags);
291 spin_unlock(&lockres->l_lock);
292 goto drop_ref;
293 }
294
8df08c89
MF
295 if (lockres->l_flags & USER_LOCK_IN_TEARDOWN) {
296 mlog(0, "lock is in teardown so we do nothing\n");
297 spin_unlock(&lockres->l_lock);
298 goto drop_ref;
299 }
300
301 if (lockres->l_flags & USER_LOCK_BUSY) {
302 mlog(0, "BUSY flag detected...\n");
303 if (lockres->l_flags & USER_LOCK_IN_CANCEL) {
304 spin_unlock(&lockres->l_lock);
305 goto drop_ref;
306 }
307
308 lockres->l_flags |= USER_LOCK_IN_CANCEL;
309 spin_unlock(&lockres->l_lock);
310
311 status = dlmunlock(dlm,
312 &lockres->l_lksb,
313 LKM_CANCEL,
314 user_unlock_ast,
315 lockres);
316 if (status == DLM_CANCELGRANT) {
317 /* If we got this, then the ast was fired
318 * before we could cancel. We cleanup our
319 * state, and restart the function. */
320 spin_lock(&lockres->l_lock);
321 lockres->l_flags &= ~USER_LOCK_IN_CANCEL;
322 spin_unlock(&lockres->l_lock);
323 } else if (status != DLM_NORMAL)
324 user_log_dlm_error("dlmunlock", status, lockres);
325 goto drop_ref;
326 }
327
328 /* If there are still incompat holders, we can exit safely
329 * without worrying about re-queueing this lock as that will
330 * happen on the last call to user_cluster_unlock. */
331 if ((lockres->l_blocking == LKM_EXMODE)
332 && (lockres->l_ex_holders || lockres->l_ro_holders)) {
333 spin_unlock(&lockres->l_lock);
334 mlog(0, "can't downconvert for ex: ro = %u, ex = %u\n",
335 lockres->l_ro_holders, lockres->l_ex_holders);
336 goto drop_ref;
337 }
338
339 if ((lockres->l_blocking == LKM_PRMODE)
340 && lockres->l_ex_holders) {
341 spin_unlock(&lockres->l_lock);
342 mlog(0, "can't downconvert for pr: ex = %u\n",
343 lockres->l_ex_holders);
344 goto drop_ref;
345 }
346
347 /* yay, we can downconvert now. */
348 new_level = user_highest_compat_lock_level(lockres->l_blocking);
349 lockres->l_requested = new_level;
350 lockres->l_flags |= USER_LOCK_BUSY;
351 mlog(0, "Downconvert lock from %d to %d\n",
352 lockres->l_level, new_level);
353 spin_unlock(&lockres->l_lock);
354
355 /* need lock downconvert request now... */
356 status = dlmlock(dlm,
357 new_level,
358 &lockres->l_lksb,
359 LKM_CONVERT|LKM_VALBLK,
360 lockres->l_name,
361 user_ast,
362 lockres,
363 user_bast);
364 if (status != DLM_NORMAL) {
365 user_log_dlm_error("dlmlock", status, lockres);
366 user_recover_from_dlm_error(lockres);
367 }
368
369drop_ref:
370 user_dlm_drop_inode_ref(lockres);
371}
372
373static inline void user_dlm_inc_holders(struct user_lock_res *lockres,
374 int level)
375{
376 switch(level) {
377 case LKM_EXMODE:
378 lockres->l_ex_holders++;
379 break;
380 case LKM_PRMODE:
381 lockres->l_ro_holders++;
382 break;
383 default:
384 BUG();
385 }
386}
387
388/* predict what lock level we'll be dropping down to on behalf
389 * of another node, and return true if the currently wanted
390 * level will be compatible with it. */
391static inline int
392user_may_continue_on_blocked_lock(struct user_lock_res *lockres,
393 int wanted)
394{
395 BUG_ON(!(lockres->l_flags & USER_LOCK_BLOCKED));
396
397 return wanted <= user_highest_compat_lock_level(lockres->l_blocking);
398}
399
400int user_dlm_cluster_lock(struct user_lock_res *lockres,
401 int level,
402 int lkm_flags)
403{
404 int status, local_flags;
405 struct dlm_ctxt *dlm = dlm_ctxt_from_user_lockres(lockres);
406
407 if (level != LKM_EXMODE &&
408 level != LKM_PRMODE) {
409 mlog(ML_ERROR, "lockres %s: invalid request!\n",
410 lockres->l_name);
411 status = -EINVAL;
412 goto bail;
413 }
414
415 mlog(0, "lockres %s: asking for %s lock, passed flags = 0x%x\n",
416 lockres->l_name,
417 (level == LKM_EXMODE) ? "LKM_EXMODE" : "LKM_PRMODE",
418 lkm_flags);
419
420again:
421 if (signal_pending(current)) {
422 status = -ERESTARTSYS;
423 goto bail;
424 }
425
426 spin_lock(&lockres->l_lock);
427
428 /* We only compare against the currently granted level
429 * here. If the lock is blocked waiting on a downconvert,
430 * we'll get caught below. */
431 if ((lockres->l_flags & USER_LOCK_BUSY) &&
432 (level > lockres->l_level)) {
433 /* is someone sitting in dlm_lock? If so, wait on
434 * them. */
435 spin_unlock(&lockres->l_lock);
436
437 user_wait_on_busy_lock(lockres);
438 goto again;
439 }
440
441 if ((lockres->l_flags & USER_LOCK_BLOCKED) &&
442 (!user_may_continue_on_blocked_lock(lockres, level))) {
443 /* is the lock is currently blocked on behalf of
444 * another node */
445 spin_unlock(&lockres->l_lock);
446
447 user_wait_on_blocked_lock(lockres);
448 goto again;
449 }
450
451 if (level > lockres->l_level) {
452 local_flags = lkm_flags | LKM_VALBLK;
453 if (lockres->l_level != LKM_IVMODE)
454 local_flags |= LKM_CONVERT;
455
456 lockres->l_requested = level;
457 lockres->l_flags |= USER_LOCK_BUSY;
458 spin_unlock(&lockres->l_lock);
459
460 BUG_ON(level == LKM_IVMODE);
461 BUG_ON(level == LKM_NLMODE);
462
463 mlog(0, "lock %s, get lock from %d to level = %d\n",
464 lockres->l_name, lockres->l_level, level);
465
466 /* call dlm_lock to upgrade lock now */
467 status = dlmlock(dlm,
468 level,
469 &lockres->l_lksb,
470 local_flags,
471 lockres->l_name,
472 user_ast,
473 lockres,
474 user_bast);
475 if (status != DLM_NORMAL) {
476 if ((lkm_flags & LKM_NOQUEUE) &&
477 (status == DLM_NOTQUEUED))
478 status = -EAGAIN;
479 else {
480 user_log_dlm_error("dlmlock", status, lockres);
481 status = -EINVAL;
482 }
483 user_recover_from_dlm_error(lockres);
484 goto bail;
485 }
486
487 mlog(0, "lock %s, successfull return from dlmlock\n",
488 lockres->l_name);
489
490 user_wait_on_busy_lock(lockres);
491 goto again;
492 }
493
494 user_dlm_inc_holders(lockres, level);
495 spin_unlock(&lockres->l_lock);
496
497 mlog(0, "lockres %s: Got %s lock!\n", lockres->l_name,
498 (level == LKM_EXMODE) ? "LKM_EXMODE" : "LKM_PRMODE");
499
500 status = 0;
501bail:
502 return status;
503}
504
505static inline void user_dlm_dec_holders(struct user_lock_res *lockres,
506 int level)
507{
508 switch(level) {
509 case LKM_EXMODE:
510 BUG_ON(!lockres->l_ex_holders);
511 lockres->l_ex_holders--;
512 break;
513 case LKM_PRMODE:
514 BUG_ON(!lockres->l_ro_holders);
515 lockres->l_ro_holders--;
516 break;
517 default:
518 BUG();
519 }
520}
521
522void user_dlm_cluster_unlock(struct user_lock_res *lockres,
523 int level)
524{
525 if (level != LKM_EXMODE &&
526 level != LKM_PRMODE) {
527 mlog(ML_ERROR, "lockres %s: invalid request!\n", lockres->l_name);
528 return;
529 }
530
531 mlog(0, "lockres %s: dropping %s lock\n", lockres->l_name,
532 (level == LKM_EXMODE) ? "LKM_EXMODE" : "LKM_PRMODE");
533
534 spin_lock(&lockres->l_lock);
535 user_dlm_dec_holders(lockres, level);
536 __user_dlm_cond_queue_lockres(lockres);
537 spin_unlock(&lockres->l_lock);
538}
539
540void user_dlm_write_lvb(struct inode *inode,
541 const char *val,
542 unsigned int len)
543{
544 struct user_lock_res *lockres = &DLMFS_I(inode)->ip_lockres;
545 char *lvb = lockres->l_lksb.lvb;
546
547 BUG_ON(len > DLM_LVB_LEN);
548
549 spin_lock(&lockres->l_lock);
550
551 BUG_ON(lockres->l_level < LKM_EXMODE);
552 memcpy(lvb, val, len);
553
554 spin_unlock(&lockres->l_lock);
555}
556
557void user_dlm_read_lvb(struct inode *inode,
558 char *val,
559 unsigned int len)
560{
561 struct user_lock_res *lockres = &DLMFS_I(inode)->ip_lockres;
562 char *lvb = lockres->l_lksb.lvb;
563
564 BUG_ON(len > DLM_LVB_LEN);
565
566 spin_lock(&lockres->l_lock);
567
568 BUG_ON(lockres->l_level < LKM_PRMODE);
569 memcpy(val, lvb, len);
570
571 spin_unlock(&lockres->l_lock);
572}
573
574void user_dlm_lock_res_init(struct user_lock_res *lockres,
575 struct dentry *dentry)
576{
577 memset(lockres, 0, sizeof(*lockres));
578
579 spin_lock_init(&lockres->l_lock);
580 init_waitqueue_head(&lockres->l_event);
581 lockres->l_level = LKM_IVMODE;
582 lockres->l_requested = LKM_IVMODE;
583 lockres->l_blocking = LKM_IVMODE;
584
585 /* should have been checked before getting here. */
586 BUG_ON(dentry->d_name.len >= USER_DLM_LOCK_ID_MAX_LEN);
587
588 memcpy(lockres->l_name,
589 dentry->d_name.name,
590 dentry->d_name.len);
591}
592
593int user_dlm_destroy_lock(struct user_lock_res *lockres)
594{
595 int status = -EBUSY;
596 struct dlm_ctxt *dlm = dlm_ctxt_from_user_lockres(lockres);
597
598 mlog(0, "asked to destroy %s\n", lockres->l_name);
599
600 spin_lock(&lockres->l_lock);
601 while (lockres->l_flags & USER_LOCK_BUSY) {
602 spin_unlock(&lockres->l_lock);
603
604 mlog(0, "lock %s is busy\n", lockres->l_name);
605
606 user_wait_on_busy_lock(lockres);
607
608 spin_lock(&lockres->l_lock);
609 }
610
611 if (lockres->l_ro_holders || lockres->l_ex_holders) {
612 spin_unlock(&lockres->l_lock);
613 mlog(0, "lock %s has holders\n", lockres->l_name);
614 goto bail;
615 }
616
617 status = 0;
618 if (!(lockres->l_flags & USER_LOCK_ATTACHED)) {
619 spin_unlock(&lockres->l_lock);
620 mlog(0, "lock %s is not attached\n", lockres->l_name);
621 goto bail;
622 }
623
624 lockres->l_flags &= ~USER_LOCK_ATTACHED;
625 lockres->l_flags |= USER_LOCK_BUSY;
626 lockres->l_flags |= USER_LOCK_IN_TEARDOWN;
627 spin_unlock(&lockres->l_lock);
628
629 mlog(0, "unlocking lockres %s\n", lockres->l_name);
630 status = dlmunlock(dlm,
631 &lockres->l_lksb,
632 LKM_VALBLK,
633 user_unlock_ast,
634 lockres);
635 if (status != DLM_NORMAL) {
636 user_log_dlm_error("dlmunlock", status, lockres);
637 status = -EINVAL;
638 goto bail;
639 }
640
641 user_wait_on_busy_lock(lockres);
642
643 status = 0;
644bail:
645 return status;
646}
647
648struct dlm_ctxt *user_dlm_register_context(struct qstr *name)
649{
650 struct dlm_ctxt *dlm;
651 u32 dlm_key;
652 char *domain;
653
654 domain = kmalloc(name->len + 1, GFP_KERNEL);
655 if (!domain) {
656 mlog_errno(-ENOMEM);
657 return ERR_PTR(-ENOMEM);
658 }
659
660 dlm_key = crc32_le(0, name->name, name->len);
661
662 snprintf(domain, name->len + 1, "%.*s", name->len, name->name);
663
664 dlm = dlm_register_domain(domain, dlm_key);
665 if (IS_ERR(dlm))
666 mlog_errno(PTR_ERR(dlm));
667
668 kfree(domain);
669 return dlm;
670}
671
672void user_dlm_unregister_context(struct dlm_ctxt *dlm)
673{
674 dlm_unregister_domain(dlm);
675}