ocfs2: remove an overly aggressive BUG() in dlmfs
[pandora-kernel.git] / fs / ocfs2 / dlm / userdlm.c
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
30 #include <linux/signal.h>
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
49 static 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
61 static 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
68 static 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... */
76 static inline struct dlm_ctxt *
77 dlm_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
87 static struct inode *
88 user_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
98 static 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. */
114 static 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
125 static 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
142         /* we're downconverting. */
143         if (lockres->l_requested < lockres->l_level) {
144                 if (lockres->l_requested <=
145                     user_highest_compat_lock_level(lockres->l_blocking)) {
146                         lockres->l_blocking = LKM_NLMODE;
147                         lockres->l_flags &= ~USER_LOCK_BLOCKED;
148                 }
149         }
150
151         lockres->l_level = lockres->l_requested;
152         lockres->l_requested = LKM_IVMODE;
153         lockres->l_flags |= USER_LOCK_ATTACHED;
154         lockres->l_flags &= ~USER_LOCK_BUSY;
155
156         spin_unlock(&lockres->l_lock);
157
158         wake_up(&lockres->l_event);
159 }
160
161 static inline void user_dlm_grab_inode_ref(struct user_lock_res *lockres)
162 {
163         struct inode *inode;
164         inode = user_dlm_inode_from_user_lockres(lockres);
165         if (!igrab(inode))
166                 BUG();
167 }
168
169 static void user_dlm_unblock_lock(void *opaque);
170
171 static void __user_dlm_queue_lockres(struct user_lock_res *lockres)
172 {
173         if (!(lockres->l_flags & USER_LOCK_QUEUED)) {
174                 user_dlm_grab_inode_ref(lockres);
175
176                 INIT_WORK(&lockres->l_work, user_dlm_unblock_lock,
177                           lockres);
178
179                 queue_work(user_dlm_worker, &lockres->l_work);
180                 lockres->l_flags |= USER_LOCK_QUEUED;
181         }
182 }
183
184 static void __user_dlm_cond_queue_lockres(struct user_lock_res *lockres)
185 {
186         int queue = 0;
187
188         if (!(lockres->l_flags & USER_LOCK_BLOCKED))
189                 return;
190
191         switch (lockres->l_blocking) {
192         case LKM_EXMODE:
193                 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
194                         queue = 1;
195                 break;
196         case LKM_PRMODE:
197                 if (!lockres->l_ex_holders)
198                         queue = 1;
199                 break;
200         default:
201                 BUG();
202         }
203
204         if (queue)
205                 __user_dlm_queue_lockres(lockres);
206 }
207
208 static void user_bast(void *opaque, int level)
209 {
210         struct user_lock_res *lockres = opaque;
211
212         mlog(0, "Blocking AST fired for lockres %s. Blocking level %d\n",
213                 lockres->l_name, level);
214
215         spin_lock(&lockres->l_lock);
216         lockres->l_flags |= USER_LOCK_BLOCKED;
217         if (level > lockres->l_blocking)
218                 lockres->l_blocking = level;
219
220         __user_dlm_queue_lockres(lockres);
221         spin_unlock(&lockres->l_lock);
222
223         wake_up(&lockres->l_event);
224 }
225
226 static void user_unlock_ast(void *opaque, enum dlm_status status)
227 {
228         struct user_lock_res *lockres = opaque;
229
230         mlog(0, "UNLOCK AST called on lock %s\n", lockres->l_name);
231
232         if (status != DLM_NORMAL)
233                 mlog(ML_ERROR, "Dlm returns status %d\n", status);
234
235         spin_lock(&lockres->l_lock);
236         if (lockres->l_flags & USER_LOCK_IN_TEARDOWN)
237                 lockres->l_level = LKM_IVMODE;
238         else {
239                 lockres->l_requested = LKM_IVMODE; /* cancel an
240                                                     * upconvert
241                                                     * request. */
242                 lockres->l_flags &= ~USER_LOCK_IN_CANCEL;
243                 /* we want the unblock thread to look at it again
244                  * now. */
245                 __user_dlm_queue_lockres(lockres);
246         }
247
248         lockres->l_flags &= ~USER_LOCK_BUSY;
249         spin_unlock(&lockres->l_lock);
250
251         wake_up(&lockres->l_event);
252 }
253
254 static inline void user_dlm_drop_inode_ref(struct user_lock_res *lockres)
255 {
256         struct inode *inode;
257         inode = user_dlm_inode_from_user_lockres(lockres);
258         iput(inode);
259 }
260
261 static void user_dlm_unblock_lock(void *opaque)
262 {
263         int new_level, status;
264         struct user_lock_res *lockres = (struct user_lock_res *) opaque;
265         struct dlm_ctxt *dlm = dlm_ctxt_from_user_lockres(lockres);
266
267         mlog(0, "processing lockres %s\n", lockres->l_name);
268
269         spin_lock(&lockres->l_lock);
270
271         mlog_bug_on_msg(!(lockres->l_flags & USER_LOCK_QUEUED),
272                         "Lockres %s, flags 0x%x\n",
273                         lockres->l_name, lockres->l_flags);
274
275         /* notice that we don't clear USER_LOCK_BLOCKED here. If it's
276          * set, we want user_ast clear it. */
277         lockres->l_flags &= ~USER_LOCK_QUEUED;
278
279         /* It's valid to get here and no longer be blocked - if we get
280          * several basts in a row, we might be queued by the first
281          * one, the unblock thread might run and clear the queued
282          * flag, and finally we might get another bast which re-queues
283          * us before our ast for the downconvert is called. */
284         if (!(lockres->l_flags & USER_LOCK_BLOCKED)) {
285                 mlog(0, "Lockres %s, flags 0x%x: queued but not blocking\n",
286                         lockres->l_name, lockres->l_flags);
287                 spin_unlock(&lockres->l_lock);
288                 goto drop_ref;
289         }
290
291         if (lockres->l_flags & USER_LOCK_IN_TEARDOWN) {
292                 mlog(0, "lock is in teardown so we do nothing\n");
293                 spin_unlock(&lockres->l_lock);
294                 goto drop_ref;
295         }
296
297         if (lockres->l_flags & USER_LOCK_BUSY) {
298                 mlog(0, "BUSY flag detected...\n");
299                 if (lockres->l_flags & USER_LOCK_IN_CANCEL) {
300                         spin_unlock(&lockres->l_lock);
301                         goto drop_ref;
302                 }
303
304                 lockres->l_flags |= USER_LOCK_IN_CANCEL;
305                 spin_unlock(&lockres->l_lock);
306
307                 status = dlmunlock(dlm,
308                                    &lockres->l_lksb,
309                                    LKM_CANCEL,
310                                    user_unlock_ast,
311                                    lockres);
312                 if (status == DLM_CANCELGRANT) {
313                         /* If we got this, then the ast was fired
314                          * before we could cancel. We cleanup our
315                          * state, and restart the function. */
316                         spin_lock(&lockres->l_lock);
317                         lockres->l_flags &= ~USER_LOCK_IN_CANCEL;
318                         spin_unlock(&lockres->l_lock);
319                 } else if (status != DLM_NORMAL)
320                         user_log_dlm_error("dlmunlock", status, lockres);
321                 goto drop_ref;
322         }
323
324         /* If there are still incompat holders, we can exit safely
325          * without worrying about re-queueing this lock as that will
326          * happen on the last call to user_cluster_unlock. */
327         if ((lockres->l_blocking == LKM_EXMODE)
328             && (lockres->l_ex_holders || lockres->l_ro_holders)) {
329                 spin_unlock(&lockres->l_lock);
330                 mlog(0, "can't downconvert for ex: ro = %u, ex = %u\n",
331                         lockres->l_ro_holders, lockres->l_ex_holders);
332                 goto drop_ref;
333         }
334
335         if ((lockres->l_blocking == LKM_PRMODE)
336             && lockres->l_ex_holders) {
337                 spin_unlock(&lockres->l_lock);
338                 mlog(0, "can't downconvert for pr: ex = %u\n",
339                         lockres->l_ex_holders);
340                 goto drop_ref;
341         }
342
343         /* yay, we can downconvert now. */
344         new_level = user_highest_compat_lock_level(lockres->l_blocking);
345         lockres->l_requested = new_level;
346         lockres->l_flags |= USER_LOCK_BUSY;
347         mlog(0, "Downconvert lock from %d to %d\n",
348                 lockres->l_level, new_level);
349         spin_unlock(&lockres->l_lock);
350
351         /* need lock downconvert request now... */
352         status = dlmlock(dlm,
353                          new_level,
354                          &lockres->l_lksb,
355                          LKM_CONVERT|LKM_VALBLK,
356                          lockres->l_name,
357                          user_ast,
358                          lockres,
359                          user_bast);
360         if (status != DLM_NORMAL) {
361                 user_log_dlm_error("dlmlock", status, lockres);
362                 user_recover_from_dlm_error(lockres);
363         }
364
365 drop_ref:
366         user_dlm_drop_inode_ref(lockres);
367 }
368
369 static inline void user_dlm_inc_holders(struct user_lock_res *lockres,
370                                         int level)
371 {
372         switch(level) {
373         case LKM_EXMODE:
374                 lockres->l_ex_holders++;
375                 break;
376         case LKM_PRMODE:
377                 lockres->l_ro_holders++;
378                 break;
379         default:
380                 BUG();
381         }
382 }
383
384 /* predict what lock level we'll be dropping down to on behalf
385  * of another node, and return true if the currently wanted
386  * level will be compatible with it. */
387 static inline int
388 user_may_continue_on_blocked_lock(struct user_lock_res *lockres,
389                                   int wanted)
390 {
391         BUG_ON(!(lockres->l_flags & USER_LOCK_BLOCKED));
392
393         return wanted <= user_highest_compat_lock_level(lockres->l_blocking);
394 }
395
396 int user_dlm_cluster_lock(struct user_lock_res *lockres,
397                           int level,
398                           int lkm_flags)
399 {
400         int status, local_flags;
401         struct dlm_ctxt *dlm = dlm_ctxt_from_user_lockres(lockres);
402
403         if (level != LKM_EXMODE &&
404             level != LKM_PRMODE) {
405                 mlog(ML_ERROR, "lockres %s: invalid request!\n",
406                      lockres->l_name);
407                 status = -EINVAL;
408                 goto bail;
409         }
410
411         mlog(0, "lockres %s: asking for %s lock, passed flags = 0x%x\n",
412                 lockres->l_name,
413                 (level == LKM_EXMODE) ? "LKM_EXMODE" : "LKM_PRMODE",
414                 lkm_flags);
415
416 again:
417         if (signal_pending(current)) {
418                 status = -ERESTARTSYS;
419                 goto bail;
420         }
421
422         spin_lock(&lockres->l_lock);
423
424         /* We only compare against the currently granted level
425          * here. If the lock is blocked waiting on a downconvert,
426          * we'll get caught below. */
427         if ((lockres->l_flags & USER_LOCK_BUSY) &&
428             (level > lockres->l_level)) {
429                 /* is someone sitting in dlm_lock? If so, wait on
430                  * them. */
431                 spin_unlock(&lockres->l_lock);
432
433                 user_wait_on_busy_lock(lockres);
434                 goto again;
435         }
436
437         if ((lockres->l_flags & USER_LOCK_BLOCKED) &&
438             (!user_may_continue_on_blocked_lock(lockres, level))) {
439                 /* is the lock is currently blocked on behalf of
440                  * another node */
441                 spin_unlock(&lockres->l_lock);
442
443                 user_wait_on_blocked_lock(lockres);
444                 goto again;
445         }
446
447         if (level > lockres->l_level) {
448                 local_flags = lkm_flags | LKM_VALBLK;
449                 if (lockres->l_level != LKM_IVMODE)
450                         local_flags |= LKM_CONVERT;
451
452                 lockres->l_requested = level;
453                 lockres->l_flags |= USER_LOCK_BUSY;
454                 spin_unlock(&lockres->l_lock);
455
456                 BUG_ON(level == LKM_IVMODE);
457                 BUG_ON(level == LKM_NLMODE);
458
459                 mlog(0, "lock %s, get lock from %d to level = %d\n",
460                         lockres->l_name, lockres->l_level, level);
461
462                 /* call dlm_lock to upgrade lock now */
463                 status = dlmlock(dlm,
464                                  level,
465                                  &lockres->l_lksb,
466                                  local_flags,
467                                  lockres->l_name,
468                                  user_ast,
469                                  lockres,
470                                  user_bast);
471                 if (status != DLM_NORMAL) {
472                         if ((lkm_flags & LKM_NOQUEUE) &&
473                             (status == DLM_NOTQUEUED))
474                                 status = -EAGAIN;
475                         else {
476                                 user_log_dlm_error("dlmlock", status, lockres);
477                                 status = -EINVAL;
478                         }
479                         user_recover_from_dlm_error(lockres);
480                         goto bail;
481                 }
482
483                 mlog(0, "lock %s, successfull return from dlmlock\n",
484                         lockres->l_name);
485
486                 user_wait_on_busy_lock(lockres);
487                 goto again;
488         }
489
490         user_dlm_inc_holders(lockres, level);
491         spin_unlock(&lockres->l_lock);
492
493         mlog(0, "lockres %s: Got %s lock!\n", lockres->l_name,
494                 (level == LKM_EXMODE) ? "LKM_EXMODE" : "LKM_PRMODE");
495
496         status = 0;
497 bail:
498         return status;
499 }
500
501 static inline void user_dlm_dec_holders(struct user_lock_res *lockres,
502                                         int level)
503 {
504         switch(level) {
505         case LKM_EXMODE:
506                 BUG_ON(!lockres->l_ex_holders);
507                 lockres->l_ex_holders--;
508                 break;
509         case LKM_PRMODE:
510                 BUG_ON(!lockres->l_ro_holders);
511                 lockres->l_ro_holders--;
512                 break;
513         default:
514                 BUG();
515         }
516 }
517
518 void user_dlm_cluster_unlock(struct user_lock_res *lockres,
519                              int level)
520 {
521         if (level != LKM_EXMODE &&
522             level != LKM_PRMODE) {
523                 mlog(ML_ERROR, "lockres %s: invalid request!\n", lockres->l_name);
524                 return;
525         }
526
527         mlog(0, "lockres %s: dropping %s lock\n", lockres->l_name,
528                 (level == LKM_EXMODE) ? "LKM_EXMODE" : "LKM_PRMODE");
529
530         spin_lock(&lockres->l_lock);
531         user_dlm_dec_holders(lockres, level);
532         __user_dlm_cond_queue_lockres(lockres);
533         spin_unlock(&lockres->l_lock);
534 }
535
536 void user_dlm_write_lvb(struct inode *inode,
537                         const char *val,
538                         unsigned int len)
539 {
540         struct user_lock_res *lockres = &DLMFS_I(inode)->ip_lockres;
541         char *lvb = lockres->l_lksb.lvb;
542
543         BUG_ON(len > DLM_LVB_LEN);
544
545         spin_lock(&lockres->l_lock);
546
547         BUG_ON(lockres->l_level < LKM_EXMODE);
548         memcpy(lvb, val, len);
549
550         spin_unlock(&lockres->l_lock);
551 }
552
553 void user_dlm_read_lvb(struct inode *inode,
554                        char *val,
555                        unsigned int len)
556 {
557         struct user_lock_res *lockres = &DLMFS_I(inode)->ip_lockres;
558         char *lvb = lockres->l_lksb.lvb;
559
560         BUG_ON(len > DLM_LVB_LEN);
561
562         spin_lock(&lockres->l_lock);
563
564         BUG_ON(lockres->l_level < LKM_PRMODE);
565         memcpy(val, lvb, len);
566
567         spin_unlock(&lockres->l_lock);
568 }
569
570 void user_dlm_lock_res_init(struct user_lock_res *lockres,
571                             struct dentry *dentry)
572 {
573         memset(lockres, 0, sizeof(*lockres));
574
575         spin_lock_init(&lockres->l_lock);
576         init_waitqueue_head(&lockres->l_event);
577         lockres->l_level = LKM_IVMODE;
578         lockres->l_requested = LKM_IVMODE;
579         lockres->l_blocking = LKM_IVMODE;
580
581         /* should have been checked before getting here. */
582         BUG_ON(dentry->d_name.len >= USER_DLM_LOCK_ID_MAX_LEN);
583
584         memcpy(lockres->l_name,
585                dentry->d_name.name,
586                dentry->d_name.len);
587 }
588
589 int user_dlm_destroy_lock(struct user_lock_res *lockres)
590 {
591         int status = -EBUSY;
592         struct dlm_ctxt *dlm = dlm_ctxt_from_user_lockres(lockres);
593
594         mlog(0, "asked to destroy %s\n", lockres->l_name);
595
596         spin_lock(&lockres->l_lock);
597         while (lockres->l_flags & USER_LOCK_BUSY) {
598                 spin_unlock(&lockres->l_lock);
599
600                 mlog(0, "lock %s is busy\n", lockres->l_name);
601
602                 user_wait_on_busy_lock(lockres);
603
604                 spin_lock(&lockres->l_lock);
605         }
606
607         if (lockres->l_ro_holders || lockres->l_ex_holders) {
608                 spin_unlock(&lockres->l_lock);
609                 mlog(0, "lock %s has holders\n", lockres->l_name);
610                 goto bail;
611         }
612
613         status = 0;
614         if (!(lockres->l_flags & USER_LOCK_ATTACHED)) {
615                 spin_unlock(&lockres->l_lock);
616                 mlog(0, "lock %s is not attached\n", lockres->l_name);
617                 goto bail;
618         }
619
620         lockres->l_flags &= ~USER_LOCK_ATTACHED;
621         lockres->l_flags |= USER_LOCK_BUSY;
622         lockres->l_flags |= USER_LOCK_IN_TEARDOWN;
623         spin_unlock(&lockres->l_lock);
624
625         mlog(0, "unlocking lockres %s\n", lockres->l_name);
626         status = dlmunlock(dlm,
627                            &lockres->l_lksb,
628                            LKM_VALBLK,
629                            user_unlock_ast,
630                            lockres);
631         if (status != DLM_NORMAL) {
632                 user_log_dlm_error("dlmunlock", status, lockres);
633                 status = -EINVAL;
634                 goto bail;
635         }
636
637         user_wait_on_busy_lock(lockres);
638
639         status = 0;
640 bail:
641         return status;
642 }
643
644 struct dlm_ctxt *user_dlm_register_context(struct qstr *name)
645 {
646         struct dlm_ctxt *dlm;
647         u32 dlm_key;
648         char *domain;
649
650         domain = kmalloc(name->len + 1, GFP_KERNEL);
651         if (!domain) {
652                 mlog_errno(-ENOMEM);
653                 return ERR_PTR(-ENOMEM);
654         }
655
656         dlm_key = crc32_le(0, name->name, name->len);
657
658         snprintf(domain, name->len + 1, "%.*s", name->len, name->name);
659
660         dlm = dlm_register_domain(domain, dlm_key);
661         if (IS_ERR(dlm))
662                 mlog_errno(PTR_ERR(dlm));
663
664         kfree(domain);
665         return dlm;
666 }
667
668 void user_dlm_unregister_context(struct dlm_ctxt *dlm)
669 {
670         dlm_unregister_domain(dlm);
671 }