fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / fs / ocfs2 / dlmglue.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * dlmglue.c
5  *
6  * Code which implements an OCFS2 specific interface to our DLM.
7  *
8  * Copyright (C) 2003, 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 #include <linux/types.h>
27 #include <linux/slab.h>
28 #include <linux/highmem.h>
29 #include <linux/mm.h>
30 #include <linux/smp_lock.h>
31 #include <linux/crc32.h>
32 #include <linux/kthread.h>
33 #include <linux/pagemap.h>
34 #include <linux/debugfs.h>
35 #include <linux/seq_file.h>
36
37 #include <cluster/heartbeat.h>
38 #include <cluster/nodemanager.h>
39 #include <cluster/tcp.h>
40
41 #include <dlm/dlmapi.h>
42
43 #define MLOG_MASK_PREFIX ML_DLM_GLUE
44 #include <cluster/masklog.h>
45
46 #include "ocfs2.h"
47
48 #include "alloc.h"
49 #include "dcache.h"
50 #include "dlmglue.h"
51 #include "extent_map.h"
52 #include "file.h"
53 #include "heartbeat.h"
54 #include "inode.h"
55 #include "journal.h"
56 #include "slot_map.h"
57 #include "super.h"
58 #include "uptodate.h"
59 #include "vote.h"
60
61 #include "buffer_head_io.h"
62
63 struct ocfs2_mask_waiter {
64         struct list_head        mw_item;
65         int                     mw_status;
66         struct completion       mw_complete;
67         unsigned long           mw_mask;
68         unsigned long           mw_goal;
69 };
70
71 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
72 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
73
74 /*
75  * Return value from ->downconvert_worker functions.
76  *
77  * These control the precise actions of ocfs2_unblock_lock()
78  * and ocfs2_process_blocked_lock()
79  *
80  */
81 enum ocfs2_unblock_action {
82         UNBLOCK_CONTINUE        = 0, /* Continue downconvert */
83         UNBLOCK_CONTINUE_POST   = 1, /* Continue downconvert, fire
84                                       * ->post_unlock callback */
85         UNBLOCK_STOP_POST       = 2, /* Do not downconvert, fire
86                                       * ->post_unlock() callback. */
87 };
88
89 struct ocfs2_unblock_ctl {
90         int requeue;
91         enum ocfs2_unblock_action unblock_action;
92 };
93
94 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
95                                         int new_level);
96 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
97
98 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
99                                      int blocking);
100
101 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
102                                        int blocking);
103
104 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
105                                      struct ocfs2_lock_res *lockres);
106
107 /*
108  * OCFS2 Lock Resource Operations
109  *
110  * These fine tune the behavior of the generic dlmglue locking infrastructure.
111  *
112  * The most basic of lock types can point ->l_priv to their respective
113  * struct ocfs2_super and allow the default actions to manage things.
114  *
115  * Right now, each lock type also needs to implement an init function,
116  * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
117  * should be called when the lock is no longer needed (i.e., object
118  * destruction time).
119  */
120 struct ocfs2_lock_res_ops {
121         /*
122          * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
123          * this callback if ->l_priv is not an ocfs2_super pointer
124          */
125         struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
126
127         /*
128          * Optionally called in the downconvert (or "vote") thread
129          * after a successful downconvert. The lockres will not be
130          * referenced after this callback is called, so it is safe to
131          * free memory, etc.
132          *
133          * The exact semantics of when this is called are controlled
134          * by ->downconvert_worker()
135          */
136         void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
137
138         /*
139          * Allow a lock type to add checks to determine whether it is
140          * safe to downconvert a lock. Return 0 to re-queue the
141          * downconvert at a later time, nonzero to continue.
142          *
143          * For most locks, the default checks that there are no
144          * incompatible holders are sufficient.
145          *
146          * Called with the lockres spinlock held.
147          */
148         int (*check_downconvert)(struct ocfs2_lock_res *, int);
149
150         /*
151          * Allows a lock type to populate the lock value block. This
152          * is called on downconvert, and when we drop a lock.
153          *
154          * Locks that want to use this should set LOCK_TYPE_USES_LVB
155          * in the flags field.
156          *
157          * Called with the lockres spinlock held.
158          */
159         void (*set_lvb)(struct ocfs2_lock_res *);
160
161         /*
162          * Called from the downconvert thread when it is determined
163          * that a lock will be downconverted. This is called without
164          * any locks held so the function can do work that might
165          * schedule (syncing out data, etc).
166          *
167          * This should return any one of the ocfs2_unblock_action
168          * values, depending on what it wants the thread to do.
169          */
170         int (*downconvert_worker)(struct ocfs2_lock_res *, int);
171
172         /*
173          * LOCK_TYPE_* flags which describe the specific requirements
174          * of a lock type. Descriptions of each individual flag follow.
175          */
176         int flags;
177 };
178
179 /*
180  * Some locks want to "refresh" potentially stale data when a
181  * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
182  * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
183  * individual lockres l_flags member from the ast function. It is
184  * expected that the locking wrapper will clear the
185  * OCFS2_LOCK_NEEDS_REFRESH flag when done.
186  */
187 #define LOCK_TYPE_REQUIRES_REFRESH 0x1
188
189 /*
190  * Indicate that a lock type makes use of the lock value block. The
191  * ->set_lvb lock type callback must be defined.
192  */
193 #define LOCK_TYPE_USES_LVB              0x2
194
195 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
196         .get_osb        = ocfs2_get_inode_osb,
197         .flags          = 0,
198 };
199
200 static struct ocfs2_lock_res_ops ocfs2_inode_meta_lops = {
201         .get_osb        = ocfs2_get_inode_osb,
202         .check_downconvert = ocfs2_check_meta_downconvert,
203         .set_lvb        = ocfs2_set_meta_lvb,
204         .flags          = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
205 };
206
207 static struct ocfs2_lock_res_ops ocfs2_inode_data_lops = {
208         .get_osb        = ocfs2_get_inode_osb,
209         .downconvert_worker = ocfs2_data_convert_worker,
210         .flags          = 0,
211 };
212
213 static struct ocfs2_lock_res_ops ocfs2_super_lops = {
214         .flags          = LOCK_TYPE_REQUIRES_REFRESH,
215 };
216
217 static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
218         .flags          = 0,
219 };
220
221 static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
222         .get_osb        = ocfs2_get_dentry_osb,
223         .post_unlock    = ocfs2_dentry_post_unlock,
224         .downconvert_worker = ocfs2_dentry_convert_worker,
225         .flags          = 0,
226 };
227
228 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
229 {
230         return lockres->l_type == OCFS2_LOCK_TYPE_META ||
231                 lockres->l_type == OCFS2_LOCK_TYPE_DATA ||
232                 lockres->l_type == OCFS2_LOCK_TYPE_RW;
233 }
234
235 static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
236 {
237         BUG_ON(!ocfs2_is_inode_lock(lockres));
238
239         return (struct inode *) lockres->l_priv;
240 }
241
242 static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
243 {
244         BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
245
246         return (struct ocfs2_dentry_lock *)lockres->l_priv;
247 }
248
249 static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
250 {
251         if (lockres->l_ops->get_osb)
252                 return lockres->l_ops->get_osb(lockres);
253
254         return (struct ocfs2_super *)lockres->l_priv;
255 }
256
257 static int ocfs2_lock_create(struct ocfs2_super *osb,
258                              struct ocfs2_lock_res *lockres,
259                              int level,
260                              int dlm_flags);
261 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
262                                                      int wanted);
263 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
264                                  struct ocfs2_lock_res *lockres,
265                                  int level);
266 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
267 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
268 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
269 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
270 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
271                                         struct ocfs2_lock_res *lockres);
272 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
273                                                 int convert);
274 #define ocfs2_log_dlm_error(_func, _stat, _lockres) do {        \
275         mlog(ML_ERROR, "Dlm error \"%s\" while calling %s on "  \
276                 "resource %s: %s\n", dlm_errname(_stat), _func, \
277                 _lockres->l_name, dlm_errmsg(_stat));           \
278 } while (0)
279 static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
280                                  struct ocfs2_lock_res *lockres);
281 static int ocfs2_meta_lock_update(struct inode *inode,
282                                   struct buffer_head **bh);
283 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
284 static inline int ocfs2_highest_compat_lock_level(int level);
285
286 static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
287                                   u64 blkno,
288                                   u32 generation,
289                                   char *name)
290 {
291         int len;
292
293         mlog_entry_void();
294
295         BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
296
297         len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
298                        ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
299                        (long long)blkno, generation);
300
301         BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
302
303         mlog(0, "built lock resource with name: %s\n", name);
304
305         mlog_exit_void();
306 }
307
308 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
309
310 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
311                                        struct ocfs2_dlm_debug *dlm_debug)
312 {
313         mlog(0, "Add tracking for lockres %s\n", res->l_name);
314
315         spin_lock(&ocfs2_dlm_tracking_lock);
316         list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
317         spin_unlock(&ocfs2_dlm_tracking_lock);
318 }
319
320 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
321 {
322         spin_lock(&ocfs2_dlm_tracking_lock);
323         if (!list_empty(&res->l_debug_list))
324                 list_del_init(&res->l_debug_list);
325         spin_unlock(&ocfs2_dlm_tracking_lock);
326 }
327
328 static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
329                                        struct ocfs2_lock_res *res,
330                                        enum ocfs2_lock_type type,
331                                        struct ocfs2_lock_res_ops *ops,
332                                        void *priv)
333 {
334         res->l_type          = type;
335         res->l_ops           = ops;
336         res->l_priv          = priv;
337
338         res->l_level         = LKM_IVMODE;
339         res->l_requested     = LKM_IVMODE;
340         res->l_blocking      = LKM_IVMODE;
341         res->l_action        = OCFS2_AST_INVALID;
342         res->l_unlock_action = OCFS2_UNLOCK_INVALID;
343
344         res->l_flags         = OCFS2_LOCK_INITIALIZED;
345
346         ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
347 }
348
349 void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
350 {
351         /* This also clears out the lock status block */
352         memset(res, 0, sizeof(struct ocfs2_lock_res));
353         spin_lock_init(&res->l_lock);
354         init_waitqueue_head(&res->l_event);
355         INIT_LIST_HEAD(&res->l_blocked_list);
356         INIT_LIST_HEAD(&res->l_mask_waiters);
357 }
358
359 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
360                                enum ocfs2_lock_type type,
361                                unsigned int generation,
362                                struct inode *inode)
363 {
364         struct ocfs2_lock_res_ops *ops;
365
366         switch(type) {
367                 case OCFS2_LOCK_TYPE_RW:
368                         ops = &ocfs2_inode_rw_lops;
369                         break;
370                 case OCFS2_LOCK_TYPE_META:
371                         ops = &ocfs2_inode_meta_lops;
372                         break;
373                 case OCFS2_LOCK_TYPE_DATA:
374                         ops = &ocfs2_inode_data_lops;
375                         break;
376                 default:
377                         mlog_bug_on_msg(1, "type: %d\n", type);
378                         ops = NULL; /* thanks, gcc */
379                         break;
380         };
381
382         ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
383                               generation, res->l_name);
384         ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
385 }
386
387 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
388 {
389         struct inode *inode = ocfs2_lock_res_inode(lockres);
390
391         return OCFS2_SB(inode->i_sb);
392 }
393
394 static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
395 {
396         __be64 inode_blkno_be;
397
398         memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
399                sizeof(__be64));
400
401         return be64_to_cpu(inode_blkno_be);
402 }
403
404 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
405 {
406         struct ocfs2_dentry_lock *dl = lockres->l_priv;
407
408         return OCFS2_SB(dl->dl_inode->i_sb);
409 }
410
411 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
412                                 u64 parent, struct inode *inode)
413 {
414         int len;
415         u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
416         __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
417         struct ocfs2_lock_res *lockres = &dl->dl_lockres;
418
419         ocfs2_lock_res_init_once(lockres);
420
421         /*
422          * Unfortunately, the standard lock naming scheme won't work
423          * here because we have two 16 byte values to use. Instead,
424          * we'll stuff the inode number as a binary value. We still
425          * want error prints to show something without garbling the
426          * display, so drop a null byte in there before the inode
427          * number. A future version of OCFS2 will likely use all
428          * binary lock names. The stringified names have been a
429          * tremendous aid in debugging, but now that the debugfs
430          * interface exists, we can mangle things there if need be.
431          *
432          * NOTE: We also drop the standard "pad" value (the total lock
433          * name size stays the same though - the last part is all
434          * zeros due to the memset in ocfs2_lock_res_init_once()
435          */
436         len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
437                        "%c%016llx",
438                        ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
439                        (long long)parent);
440
441         BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
442
443         memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
444                sizeof(__be64));
445
446         ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
447                                    OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
448                                    dl);
449 }
450
451 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
452                                       struct ocfs2_super *osb)
453 {
454         /* Superblock lockres doesn't come from a slab so we call init
455          * once on it manually.  */
456         ocfs2_lock_res_init_once(res);
457         ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
458                               0, res->l_name);
459         ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
460                                    &ocfs2_super_lops, osb);
461 }
462
463 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
464                                        struct ocfs2_super *osb)
465 {
466         /* Rename lockres doesn't come from a slab so we call init
467          * once on it manually.  */
468         ocfs2_lock_res_init_once(res);
469         ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
470         ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
471                                    &ocfs2_rename_lops, osb);
472 }
473
474 void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
475 {
476         mlog_entry_void();
477
478         if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
479                 return;
480
481         ocfs2_remove_lockres_tracking(res);
482
483         mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
484                         "Lockres %s is on the blocked list\n",
485                         res->l_name);
486         mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
487                         "Lockres %s has mask waiters pending\n",
488                         res->l_name);
489         mlog_bug_on_msg(spin_is_locked(&res->l_lock),
490                         "Lockres %s is locked\n",
491                         res->l_name);
492         mlog_bug_on_msg(res->l_ro_holders,
493                         "Lockres %s has %u ro holders\n",
494                         res->l_name, res->l_ro_holders);
495         mlog_bug_on_msg(res->l_ex_holders,
496                         "Lockres %s has %u ex holders\n",
497                         res->l_name, res->l_ex_holders);
498
499         /* Need to clear out the lock status block for the dlm */
500         memset(&res->l_lksb, 0, sizeof(res->l_lksb));
501
502         res->l_flags = 0UL;
503         mlog_exit_void();
504 }
505
506 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
507                                      int level)
508 {
509         mlog_entry_void();
510
511         BUG_ON(!lockres);
512
513         switch(level) {
514         case LKM_EXMODE:
515                 lockres->l_ex_holders++;
516                 break;
517         case LKM_PRMODE:
518                 lockres->l_ro_holders++;
519                 break;
520         default:
521                 BUG();
522         }
523
524         mlog_exit_void();
525 }
526
527 static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
528                                      int level)
529 {
530         mlog_entry_void();
531
532         BUG_ON(!lockres);
533
534         switch(level) {
535         case LKM_EXMODE:
536                 BUG_ON(!lockres->l_ex_holders);
537                 lockres->l_ex_holders--;
538                 break;
539         case LKM_PRMODE:
540                 BUG_ON(!lockres->l_ro_holders);
541                 lockres->l_ro_holders--;
542                 break;
543         default:
544                 BUG();
545         }
546         mlog_exit_void();
547 }
548
549 /* WARNING: This function lives in a world where the only three lock
550  * levels are EX, PR, and NL. It *will* have to be adjusted when more
551  * lock types are added. */
552 static inline int ocfs2_highest_compat_lock_level(int level)
553 {
554         int new_level = LKM_EXMODE;
555
556         if (level == LKM_EXMODE)
557                 new_level = LKM_NLMODE;
558         else if (level == LKM_PRMODE)
559                 new_level = LKM_PRMODE;
560         return new_level;
561 }
562
563 static void lockres_set_flags(struct ocfs2_lock_res *lockres,
564                               unsigned long newflags)
565 {
566         struct list_head *pos, *tmp;
567         struct ocfs2_mask_waiter *mw;
568
569         assert_spin_locked(&lockres->l_lock);
570
571         lockres->l_flags = newflags;
572
573         list_for_each_safe(pos, tmp, &lockres->l_mask_waiters) {
574                 mw = list_entry(pos, struct ocfs2_mask_waiter, mw_item);
575                 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
576                         continue;
577
578                 list_del_init(&mw->mw_item);
579                 mw->mw_status = 0;
580                 complete(&mw->mw_complete);
581         }
582 }
583 static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
584 {
585         lockres_set_flags(lockres, lockres->l_flags | or);
586 }
587 static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
588                                 unsigned long clear)
589 {
590         lockres_set_flags(lockres, lockres->l_flags & ~clear);
591 }
592
593 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
594 {
595         mlog_entry_void();
596
597         BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
598         BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
599         BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
600         BUG_ON(lockres->l_blocking <= LKM_NLMODE);
601
602         lockres->l_level = lockres->l_requested;
603         if (lockres->l_level <=
604             ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
605                 lockres->l_blocking = LKM_NLMODE;
606                 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
607         }
608         lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
609
610         mlog_exit_void();
611 }
612
613 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
614 {
615         mlog_entry_void();
616
617         BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
618         BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
619
620         /* Convert from RO to EX doesn't really need anything as our
621          * information is already up to data. Convert from NL to
622          * *anything* however should mark ourselves as needing an
623          * update */
624         if (lockres->l_level == LKM_NLMODE &&
625             lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
626                 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
627
628         lockres->l_level = lockres->l_requested;
629         lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
630
631         mlog_exit_void();
632 }
633
634 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
635 {
636         mlog_entry_void();
637
638         BUG_ON((!lockres->l_flags & OCFS2_LOCK_BUSY));
639         BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
640
641         if (lockres->l_requested > LKM_NLMODE &&
642             !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
643             lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
644                 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
645
646         lockres->l_level = lockres->l_requested;
647         lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
648         lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
649
650         mlog_exit_void();
651 }
652
653 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
654                                      int level)
655 {
656         int needs_downconvert = 0;
657         mlog_entry_void();
658
659         assert_spin_locked(&lockres->l_lock);
660
661         lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
662
663         if (level > lockres->l_blocking) {
664                 /* only schedule a downconvert if we haven't already scheduled
665                  * one that goes low enough to satisfy the level we're
666                  * blocking.  this also catches the case where we get
667                  * duplicate BASTs */
668                 if (ocfs2_highest_compat_lock_level(level) <
669                     ocfs2_highest_compat_lock_level(lockres->l_blocking))
670                         needs_downconvert = 1;
671
672                 lockres->l_blocking = level;
673         }
674
675         mlog_exit(needs_downconvert);
676         return needs_downconvert;
677 }
678
679 static void ocfs2_blocking_ast(void *opaque, int level)
680 {
681         struct ocfs2_lock_res *lockres = opaque;
682         struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
683         int needs_downconvert;
684         unsigned long flags;
685
686         BUG_ON(level <= LKM_NLMODE);
687
688         mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
689              lockres->l_name, level, lockres->l_level,
690              ocfs2_lock_type_string(lockres->l_type));
691
692         spin_lock_irqsave(&lockres->l_lock, flags);
693         needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
694         if (needs_downconvert)
695                 ocfs2_schedule_blocked_lock(osb, lockres);
696         spin_unlock_irqrestore(&lockres->l_lock, flags);
697
698         wake_up(&lockres->l_event);
699
700         ocfs2_kick_vote_thread(osb);
701 }
702
703 static void ocfs2_locking_ast(void *opaque)
704 {
705         struct ocfs2_lock_res *lockres = opaque;
706         struct dlm_lockstatus *lksb = &lockres->l_lksb;
707         unsigned long flags;
708
709         spin_lock_irqsave(&lockres->l_lock, flags);
710
711         if (lksb->status != DLM_NORMAL) {
712                 mlog(ML_ERROR, "lockres %s: lksb status value of %u!\n",
713                      lockres->l_name, lksb->status);
714                 spin_unlock_irqrestore(&lockres->l_lock, flags);
715                 return;
716         }
717
718         switch(lockres->l_action) {
719         case OCFS2_AST_ATTACH:
720                 ocfs2_generic_handle_attach_action(lockres);
721                 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
722                 break;
723         case OCFS2_AST_CONVERT:
724                 ocfs2_generic_handle_convert_action(lockres);
725                 break;
726         case OCFS2_AST_DOWNCONVERT:
727                 ocfs2_generic_handle_downconvert_action(lockres);
728                 break;
729         default:
730                 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
731                      "lockres flags = 0x%lx, unlock action: %u\n",
732                      lockres->l_name, lockres->l_action, lockres->l_flags,
733                      lockres->l_unlock_action);
734                 BUG();
735         }
736
737         /* set it to something invalid so if we get called again we
738          * can catch it. */
739         lockres->l_action = OCFS2_AST_INVALID;
740
741         wake_up(&lockres->l_event);
742         spin_unlock_irqrestore(&lockres->l_lock, flags);
743 }
744
745 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
746                                                 int convert)
747 {
748         unsigned long flags;
749
750         mlog_entry_void();
751         spin_lock_irqsave(&lockres->l_lock, flags);
752         lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
753         if (convert)
754                 lockres->l_action = OCFS2_AST_INVALID;
755         else
756                 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
757         spin_unlock_irqrestore(&lockres->l_lock, flags);
758
759         wake_up(&lockres->l_event);
760         mlog_exit_void();
761 }
762
763 /* Note: If we detect another process working on the lock (i.e.,
764  * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
765  * to do the right thing in that case.
766  */
767 static int ocfs2_lock_create(struct ocfs2_super *osb,
768                              struct ocfs2_lock_res *lockres,
769                              int level,
770                              int dlm_flags)
771 {
772         int ret = 0;
773         enum dlm_status status = DLM_NORMAL;
774         unsigned long flags;
775
776         mlog_entry_void();
777
778         mlog(0, "lock %s, level = %d, flags = %d\n", lockres->l_name, level,
779              dlm_flags);
780
781         spin_lock_irqsave(&lockres->l_lock, flags);
782         if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
783             (lockres->l_flags & OCFS2_LOCK_BUSY)) {
784                 spin_unlock_irqrestore(&lockres->l_lock, flags);
785                 goto bail;
786         }
787
788         lockres->l_action = OCFS2_AST_ATTACH;
789         lockres->l_requested = level;
790         lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
791         spin_unlock_irqrestore(&lockres->l_lock, flags);
792
793         status = dlmlock(osb->dlm,
794                          level,
795                          &lockres->l_lksb,
796                          dlm_flags,
797                          lockres->l_name,
798                          OCFS2_LOCK_ID_MAX_LEN - 1,
799                          ocfs2_locking_ast,
800                          lockres,
801                          ocfs2_blocking_ast);
802         if (status != DLM_NORMAL) {
803                 ocfs2_log_dlm_error("dlmlock", status, lockres);
804                 ret = -EINVAL;
805                 ocfs2_recover_from_dlm_error(lockres, 1);
806         }
807
808         mlog(0, "lock %s, successfull return from dlmlock\n", lockres->l_name);
809
810 bail:
811         mlog_exit(ret);
812         return ret;
813 }
814
815 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
816                                         int flag)
817 {
818         unsigned long flags;
819         int ret;
820
821         spin_lock_irqsave(&lockres->l_lock, flags);
822         ret = lockres->l_flags & flag;
823         spin_unlock_irqrestore(&lockres->l_lock, flags);
824
825         return ret;
826 }
827
828 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
829
830 {
831         wait_event(lockres->l_event,
832                    !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
833 }
834
835 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
836
837 {
838         wait_event(lockres->l_event,
839                    !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
840 }
841
842 /* predict what lock level we'll be dropping down to on behalf
843  * of another node, and return true if the currently wanted
844  * level will be compatible with it. */
845 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
846                                                      int wanted)
847 {
848         BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
849
850         return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
851 }
852
853 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
854 {
855         INIT_LIST_HEAD(&mw->mw_item);
856         init_completion(&mw->mw_complete);
857 }
858
859 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
860 {
861         wait_for_completion(&mw->mw_complete);
862         /* Re-arm the completion in case we want to wait on it again */
863         INIT_COMPLETION(mw->mw_complete);
864         return mw->mw_status;
865 }
866
867 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
868                                     struct ocfs2_mask_waiter *mw,
869                                     unsigned long mask,
870                                     unsigned long goal)
871 {
872         BUG_ON(!list_empty(&mw->mw_item));
873
874         assert_spin_locked(&lockres->l_lock);
875
876         list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
877         mw->mw_mask = mask;
878         mw->mw_goal = goal;
879 }
880
881 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
882  * if the mask still hadn't reached its goal */
883 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
884                                       struct ocfs2_mask_waiter *mw)
885 {
886         unsigned long flags;
887         int ret = 0;
888
889         spin_lock_irqsave(&lockres->l_lock, flags);
890         if (!list_empty(&mw->mw_item)) {
891                 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
892                         ret = -EBUSY;
893
894                 list_del_init(&mw->mw_item);
895                 init_completion(&mw->mw_complete);
896         }
897         spin_unlock_irqrestore(&lockres->l_lock, flags);
898
899         return ret;
900
901 }
902
903 static int ocfs2_cluster_lock(struct ocfs2_super *osb,
904                               struct ocfs2_lock_res *lockres,
905                               int level,
906                               int lkm_flags,
907                               int arg_flags)
908 {
909         struct ocfs2_mask_waiter mw;
910         enum dlm_status status;
911         int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
912         int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
913         unsigned long flags;
914
915         mlog_entry_void();
916
917         ocfs2_init_mask_waiter(&mw);
918
919         if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
920                 lkm_flags |= LKM_VALBLK;
921
922 again:
923         wait = 0;
924
925         if (catch_signals && signal_pending(current)) {
926                 ret = -ERESTARTSYS;
927                 goto out;
928         }
929
930         spin_lock_irqsave(&lockres->l_lock, flags);
931
932         mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
933                         "Cluster lock called on freeing lockres %s! flags "
934                         "0x%lx\n", lockres->l_name, lockres->l_flags);
935
936         /* We only compare against the currently granted level
937          * here. If the lock is blocked waiting on a downconvert,
938          * we'll get caught below. */
939         if (lockres->l_flags & OCFS2_LOCK_BUSY &&
940             level > lockres->l_level) {
941                 /* is someone sitting in dlm_lock? If so, wait on
942                  * them. */
943                 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
944                 wait = 1;
945                 goto unlock;
946         }
947
948         if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
949                 /* lock has not been created yet. */
950                 spin_unlock_irqrestore(&lockres->l_lock, flags);
951
952                 ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
953                 if (ret < 0) {
954                         mlog_errno(ret);
955                         goto out;
956                 }
957                 goto again;
958         }
959
960         if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
961             !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
962                 /* is the lock is currently blocked on behalf of
963                  * another node */
964                 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
965                 wait = 1;
966                 goto unlock;
967         }
968
969         if (level > lockres->l_level) {
970                 if (lockres->l_action != OCFS2_AST_INVALID)
971                         mlog(ML_ERROR, "lockres %s has action %u pending\n",
972                              lockres->l_name, lockres->l_action);
973
974                 lockres->l_action = OCFS2_AST_CONVERT;
975                 lockres->l_requested = level;
976                 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
977                 spin_unlock_irqrestore(&lockres->l_lock, flags);
978
979                 BUG_ON(level == LKM_IVMODE);
980                 BUG_ON(level == LKM_NLMODE);
981
982                 mlog(0, "lock %s, convert from %d to level = %d\n",
983                      lockres->l_name, lockres->l_level, level);
984
985                 /* call dlm_lock to upgrade lock now */
986                 status = dlmlock(osb->dlm,
987                                  level,
988                                  &lockres->l_lksb,
989                                  lkm_flags|LKM_CONVERT,
990                                  lockres->l_name,
991                                  OCFS2_LOCK_ID_MAX_LEN - 1,
992                                  ocfs2_locking_ast,
993                                  lockres,
994                                  ocfs2_blocking_ast);
995                 if (status != DLM_NORMAL) {
996                         if ((lkm_flags & LKM_NOQUEUE) &&
997                             (status == DLM_NOTQUEUED))
998                                 ret = -EAGAIN;
999                         else {
1000                                 ocfs2_log_dlm_error("dlmlock", status,
1001                                                     lockres);
1002                                 ret = -EINVAL;
1003                         }
1004                         ocfs2_recover_from_dlm_error(lockres, 1);
1005                         goto out;
1006                 }
1007
1008                 mlog(0, "lock %s, successfull return from dlmlock\n",
1009                      lockres->l_name);
1010
1011                 /* At this point we've gone inside the dlm and need to
1012                  * complete our work regardless. */
1013                 catch_signals = 0;
1014
1015                 /* wait for busy to clear and carry on */
1016                 goto again;
1017         }
1018
1019         /* Ok, if we get here then we're good to go. */
1020         ocfs2_inc_holders(lockres, level);
1021
1022         ret = 0;
1023 unlock:
1024         spin_unlock_irqrestore(&lockres->l_lock, flags);
1025 out:
1026         /*
1027          * This is helping work around a lock inversion between the page lock
1028          * and dlm locks.  One path holds the page lock while calling aops
1029          * which block acquiring dlm locks.  The voting thread holds dlm
1030          * locks while acquiring page locks while down converting data locks.
1031          * This block is helping an aop path notice the inversion and back
1032          * off to unlock its page lock before trying the dlm lock again.
1033          */
1034         if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1035             mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1036                 wait = 0;
1037                 if (lockres_remove_mask_waiter(lockres, &mw))
1038                         ret = -EAGAIN;
1039                 else
1040                         goto again;
1041         }
1042         if (wait) {
1043                 ret = ocfs2_wait_for_mask(&mw);
1044                 if (ret == 0)
1045                         goto again;
1046                 mlog_errno(ret);
1047         }
1048
1049         mlog_exit(ret);
1050         return ret;
1051 }
1052
1053 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1054                                  struct ocfs2_lock_res *lockres,
1055                                  int level)
1056 {
1057         unsigned long flags;
1058
1059         mlog_entry_void();
1060         spin_lock_irqsave(&lockres->l_lock, flags);
1061         ocfs2_dec_holders(lockres, level);
1062         ocfs2_vote_on_unlock(osb, lockres);
1063         spin_unlock_irqrestore(&lockres->l_lock, flags);
1064         mlog_exit_void();
1065 }
1066
1067 static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1068                                  struct ocfs2_lock_res *lockres,
1069                                  int ex,
1070                                  int local)
1071 {
1072         int level =  ex ? LKM_EXMODE : LKM_PRMODE;
1073         unsigned long flags;
1074         int lkm_flags = local ? LKM_LOCAL : 0;
1075
1076         spin_lock_irqsave(&lockres->l_lock, flags);
1077         BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1078         lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1079         spin_unlock_irqrestore(&lockres->l_lock, flags);
1080
1081         return ocfs2_lock_create(osb, lockres, level, lkm_flags);
1082 }
1083
1084 /* Grants us an EX lock on the data and metadata resources, skipping
1085  * the normal cluster directory lookup. Use this ONLY on newly created
1086  * inodes which other nodes can't possibly see, and which haven't been
1087  * hashed in the inode hash yet. This can give us a good performance
1088  * increase as it'll skip the network broadcast normally associated
1089  * with creating a new lock resource. */
1090 int ocfs2_create_new_inode_locks(struct inode *inode)
1091 {
1092         int ret;
1093         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1094
1095         BUG_ON(!inode);
1096         BUG_ON(!ocfs2_inode_is_new(inode));
1097
1098         mlog_entry_void();
1099
1100         mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1101
1102         /* NOTE: That we don't increment any of the holder counts, nor
1103          * do we add anything to a journal handle. Since this is
1104          * supposed to be a new inode which the cluster doesn't know
1105          * about yet, there is no need to.  As far as the LVB handling
1106          * is concerned, this is basically like acquiring an EX lock
1107          * on a resource which has an invalid one -- we'll set it
1108          * valid when we release the EX. */
1109
1110         ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
1111         if (ret) {
1112                 mlog_errno(ret);
1113                 goto bail;
1114         }
1115
1116         /*
1117          * We don't want to use LKM_LOCAL on a meta data lock as they
1118          * don't use a generation in their lock names.
1119          */
1120         ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_meta_lockres, 1, 0);
1121         if (ret) {
1122                 mlog_errno(ret);
1123                 goto bail;
1124         }
1125
1126         ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_data_lockres, 1, 1);
1127         if (ret) {
1128                 mlog_errno(ret);
1129                 goto bail;
1130         }
1131
1132 bail:
1133         mlog_exit(ret);
1134         return ret;
1135 }
1136
1137 int ocfs2_rw_lock(struct inode *inode, int write)
1138 {
1139         int status, level;
1140         struct ocfs2_lock_res *lockres;
1141         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1142
1143         BUG_ON(!inode);
1144
1145         mlog_entry_void();
1146
1147         mlog(0, "inode %llu take %s RW lock\n",
1148              (unsigned long long)OCFS2_I(inode)->ip_blkno,
1149              write ? "EXMODE" : "PRMODE");
1150
1151         if (ocfs2_mount_local(osb))
1152                 return 0;
1153
1154         lockres = &OCFS2_I(inode)->ip_rw_lockres;
1155
1156         level = write ? LKM_EXMODE : LKM_PRMODE;
1157
1158         status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1159                                     0);
1160         if (status < 0)
1161                 mlog_errno(status);
1162
1163         mlog_exit(status);
1164         return status;
1165 }
1166
1167 void ocfs2_rw_unlock(struct inode *inode, int write)
1168 {
1169         int level = write ? LKM_EXMODE : LKM_PRMODE;
1170         struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1171         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1172
1173         mlog_entry_void();
1174
1175         mlog(0, "inode %llu drop %s RW lock\n",
1176              (unsigned long long)OCFS2_I(inode)->ip_blkno,
1177              write ? "EXMODE" : "PRMODE");
1178
1179         if (!ocfs2_mount_local(osb))
1180                 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1181
1182         mlog_exit_void();
1183 }
1184
1185 int ocfs2_data_lock_full(struct inode *inode,
1186                          int write,
1187                          int arg_flags)
1188 {
1189         int status = 0, level;
1190         struct ocfs2_lock_res *lockres;
1191         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1192
1193         BUG_ON(!inode);
1194
1195         mlog_entry_void();
1196
1197         mlog(0, "inode %llu take %s DATA lock\n",
1198              (unsigned long long)OCFS2_I(inode)->ip_blkno,
1199              write ? "EXMODE" : "PRMODE");
1200
1201         /* We'll allow faking a readonly data lock for
1202          * rodevices. */
1203         if (ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb))) {
1204                 if (write) {
1205                         status = -EROFS;
1206                         mlog_errno(status);
1207                 }
1208                 goto out;
1209         }
1210
1211         if (ocfs2_mount_local(osb))
1212                 goto out;
1213
1214         lockres = &OCFS2_I(inode)->ip_data_lockres;
1215
1216         level = write ? LKM_EXMODE : LKM_PRMODE;
1217
1218         status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level,
1219                                     0, arg_flags);
1220         if (status < 0 && status != -EAGAIN)
1221                 mlog_errno(status);
1222
1223 out:
1224         mlog_exit(status);
1225         return status;
1226 }
1227
1228 /* see ocfs2_meta_lock_with_page() */
1229 int ocfs2_data_lock_with_page(struct inode *inode,
1230                               int write,
1231                               struct page *page)
1232 {
1233         int ret;
1234
1235         ret = ocfs2_data_lock_full(inode, write, OCFS2_LOCK_NONBLOCK);
1236         if (ret == -EAGAIN) {
1237                 unlock_page(page);
1238                 if (ocfs2_data_lock(inode, write) == 0)
1239                         ocfs2_data_unlock(inode, write);
1240                 ret = AOP_TRUNCATED_PAGE;
1241         }
1242
1243         return ret;
1244 }
1245
1246 static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
1247                                  struct ocfs2_lock_res *lockres)
1248 {
1249         int kick = 0;
1250
1251         mlog_entry_void();
1252
1253         /* If we know that another node is waiting on our lock, kick
1254          * the vote thread * pre-emptively when we reach a release
1255          * condition. */
1256         if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1257                 switch(lockres->l_blocking) {
1258                 case LKM_EXMODE:
1259                         if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1260                                 kick = 1;
1261                         break;
1262                 case LKM_PRMODE:
1263                         if (!lockres->l_ex_holders)
1264                                 kick = 1;
1265                         break;
1266                 default:
1267                         BUG();
1268                 }
1269         }
1270
1271         if (kick)
1272                 ocfs2_kick_vote_thread(osb);
1273
1274         mlog_exit_void();
1275 }
1276
1277 void ocfs2_data_unlock(struct inode *inode,
1278                        int write)
1279 {
1280         int level = write ? LKM_EXMODE : LKM_PRMODE;
1281         struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_data_lockres;
1282         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1283
1284         mlog_entry_void();
1285
1286         mlog(0, "inode %llu drop %s DATA lock\n",
1287              (unsigned long long)OCFS2_I(inode)->ip_blkno,
1288              write ? "EXMODE" : "PRMODE");
1289
1290         if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
1291             !ocfs2_mount_local(osb))
1292                 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1293
1294         mlog_exit_void();
1295 }
1296
1297 #define OCFS2_SEC_BITS   34
1298 #define OCFS2_SEC_SHIFT  (64 - 34)
1299 #define OCFS2_NSEC_MASK  ((1ULL << OCFS2_SEC_SHIFT) - 1)
1300
1301 /* LVB only has room for 64 bits of time here so we pack it for
1302  * now. */
1303 static u64 ocfs2_pack_timespec(struct timespec *spec)
1304 {
1305         u64 res;
1306         u64 sec = spec->tv_sec;
1307         u32 nsec = spec->tv_nsec;
1308
1309         res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1310
1311         return res;
1312 }
1313
1314 /* Call this with the lockres locked. I am reasonably sure we don't
1315  * need ip_lock in this function as anyone who would be changing those
1316  * values is supposed to be blocked in ocfs2_meta_lock right now. */
1317 static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1318 {
1319         struct ocfs2_inode_info *oi = OCFS2_I(inode);
1320         struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1321         struct ocfs2_meta_lvb *lvb;
1322
1323         mlog_entry_void();
1324
1325         lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1326
1327         /*
1328          * Invalidate the LVB of a deleted inode - this way other
1329          * nodes are forced to go to disk and discover the new inode
1330          * status.
1331          */
1332         if (oi->ip_flags & OCFS2_INODE_DELETED) {
1333                 lvb->lvb_version = 0;
1334                 goto out;
1335         }
1336
1337         lvb->lvb_version   = OCFS2_LVB_VERSION;
1338         lvb->lvb_isize     = cpu_to_be64(i_size_read(inode));
1339         lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1340         lvb->lvb_iuid      = cpu_to_be32(inode->i_uid);
1341         lvb->lvb_igid      = cpu_to_be32(inode->i_gid);
1342         lvb->lvb_itag      = cpu_to_be16(inode->i_tag);
1343         lvb->lvb_imode     = cpu_to_be16(inode->i_mode);
1344         lvb->lvb_inlink    = cpu_to_be16(inode->i_nlink);
1345         lvb->lvb_iatime_packed  =
1346                 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1347         lvb->lvb_ictime_packed =
1348                 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1349         lvb->lvb_imtime_packed =
1350                 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1351         lvb->lvb_iattr    = cpu_to_be32(oi->ip_attr);
1352         lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
1353
1354 out:
1355         mlog_meta_lvb(0, lockres);
1356
1357         mlog_exit_void();
1358 }
1359
1360 static void ocfs2_unpack_timespec(struct timespec *spec,
1361                                   u64 packed_time)
1362 {
1363         spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1364         spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1365 }
1366
1367 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1368 {
1369         struct ocfs2_inode_info *oi = OCFS2_I(inode);
1370         struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1371         struct ocfs2_meta_lvb *lvb;
1372
1373         mlog_entry_void();
1374
1375         mlog_meta_lvb(0, lockres);
1376
1377         lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1378
1379         /* We're safe here without the lockres lock... */
1380         spin_lock(&oi->ip_lock);
1381         oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1382         i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1383
1384         oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1385         ocfs2_set_inode_flags(inode);
1386
1387         /* fast-symlinks are a special case */
1388         if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1389                 inode->i_blocks = 0;
1390         else
1391                 inode->i_blocks =
1392                         ocfs2_align_bytes_to_sectors(i_size_read(inode));
1393
1394         inode->i_uid     = be32_to_cpu(lvb->lvb_iuid);
1395         inode->i_gid     = be32_to_cpu(lvb->lvb_igid);
1396         inode->i_tag     = be16_to_cpu(lvb->lvb_itag);
1397         inode->i_mode    = be16_to_cpu(lvb->lvb_imode);
1398         inode->i_nlink   = be16_to_cpu(lvb->lvb_inlink);
1399         ocfs2_unpack_timespec(&inode->i_atime,
1400                               be64_to_cpu(lvb->lvb_iatime_packed));
1401         ocfs2_unpack_timespec(&inode->i_mtime,
1402                               be64_to_cpu(lvb->lvb_imtime_packed));
1403         ocfs2_unpack_timespec(&inode->i_ctime,
1404                               be64_to_cpu(lvb->lvb_ictime_packed));
1405         spin_unlock(&oi->ip_lock);
1406
1407         mlog_exit_void();
1408 }
1409
1410 static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1411                                               struct ocfs2_lock_res *lockres)
1412 {
1413         struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1414
1415         if (lvb->lvb_version == OCFS2_LVB_VERSION
1416             && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
1417                 return 1;
1418         return 0;
1419 }
1420
1421 /* Determine whether a lock resource needs to be refreshed, and
1422  * arbitrate who gets to refresh it.
1423  *
1424  *   0 means no refresh needed.
1425  *
1426  *   > 0 means you need to refresh this and you MUST call
1427  *   ocfs2_complete_lock_res_refresh afterwards. */
1428 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1429 {
1430         unsigned long flags;
1431         int status = 0;
1432
1433         mlog_entry_void();
1434
1435 refresh_check:
1436         spin_lock_irqsave(&lockres->l_lock, flags);
1437         if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1438                 spin_unlock_irqrestore(&lockres->l_lock, flags);
1439                 goto bail;
1440         }
1441
1442         if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1443                 spin_unlock_irqrestore(&lockres->l_lock, flags);
1444
1445                 ocfs2_wait_on_refreshing_lock(lockres);
1446                 goto refresh_check;
1447         }
1448
1449         /* Ok, I'll be the one to refresh this lock. */
1450         lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1451         spin_unlock_irqrestore(&lockres->l_lock, flags);
1452
1453         status = 1;
1454 bail:
1455         mlog_exit(status);
1456         return status;
1457 }
1458
1459 /* If status is non zero, I'll mark it as not being in refresh
1460  * anymroe, but i won't clear the needs refresh flag. */
1461 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1462                                                    int status)
1463 {
1464         unsigned long flags;
1465         mlog_entry_void();
1466
1467         spin_lock_irqsave(&lockres->l_lock, flags);
1468         lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1469         if (!status)
1470                 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1471         spin_unlock_irqrestore(&lockres->l_lock, flags);
1472
1473         wake_up(&lockres->l_event);
1474
1475         mlog_exit_void();
1476 }
1477
1478 /* may or may not return a bh if it went to disk. */
1479 static int ocfs2_meta_lock_update(struct inode *inode,
1480                                   struct buffer_head **bh)
1481 {
1482         int status = 0;
1483         struct ocfs2_inode_info *oi = OCFS2_I(inode);
1484         struct ocfs2_lock_res *lockres = NULL;
1485         struct ocfs2_dinode *fe;
1486         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1487
1488         mlog_entry_void();
1489
1490         spin_lock(&oi->ip_lock);
1491         if (oi->ip_flags & OCFS2_INODE_DELETED) {
1492                 mlog(0, "Orphaned inode %llu was deleted while we "
1493                      "were waiting on a lock. ip_flags = 0x%x\n",
1494                      (unsigned long long)oi->ip_blkno, oi->ip_flags);
1495                 spin_unlock(&oi->ip_lock);
1496                 status = -ENOENT;
1497                 goto bail;
1498         }
1499         spin_unlock(&oi->ip_lock);
1500
1501         if (!ocfs2_mount_local(osb)) {
1502                 lockres = &oi->ip_meta_lockres;
1503
1504                 if (!ocfs2_should_refresh_lock_res(lockres))
1505                         goto bail;
1506         }
1507
1508         /* This will discard any caching information we might have had
1509          * for the inode metadata. */
1510         ocfs2_metadata_cache_purge(inode);
1511
1512         /* will do nothing for inode types that don't use the extent
1513          * map (directories, bitmap files, etc) */
1514         ocfs2_extent_map_trunc(inode, 0);
1515
1516         if (lockres && ocfs2_meta_lvb_is_trustable(inode, lockres)) {
1517                 mlog(0, "Trusting LVB on inode %llu\n",
1518                      (unsigned long long)oi->ip_blkno);
1519                 ocfs2_refresh_inode_from_lvb(inode);
1520         } else {
1521                 /* Boo, we have to go to disk. */
1522                 /* read bh, cast, ocfs2_refresh_inode */
1523                 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1524                                           bh, OCFS2_BH_CACHED, inode);
1525                 if (status < 0) {
1526                         mlog_errno(status);
1527                         goto bail_refresh;
1528                 }
1529                 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1530
1531                 /* This is a good chance to make sure we're not
1532                  * locking an invalid object.
1533                  *
1534                  * We bug on a stale inode here because we checked
1535                  * above whether it was wiped from disk. The wiping
1536                  * node provides a guarantee that we receive that
1537                  * message and can mark the inode before dropping any
1538                  * locks associated with it. */
1539                 if (!OCFS2_IS_VALID_DINODE(fe)) {
1540                         OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1541                         status = -EIO;
1542                         goto bail_refresh;
1543                 }
1544                 mlog_bug_on_msg(inode->i_generation !=
1545                                 le32_to_cpu(fe->i_generation),
1546                                 "Invalid dinode %llu disk generation: %u "
1547                                 "inode->i_generation: %u\n",
1548                                 (unsigned long long)oi->ip_blkno,
1549                                 le32_to_cpu(fe->i_generation),
1550                                 inode->i_generation);
1551                 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1552                                 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
1553                                 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1554                                 (unsigned long long)oi->ip_blkno,
1555                                 (unsigned long long)le64_to_cpu(fe->i_dtime),
1556                                 le32_to_cpu(fe->i_flags));
1557
1558                 ocfs2_refresh_inode(inode, fe);
1559         }
1560
1561         status = 0;
1562 bail_refresh:
1563         if (lockres)
1564                 ocfs2_complete_lock_res_refresh(lockres, status);
1565 bail:
1566         mlog_exit(status);
1567         return status;
1568 }
1569
1570 static int ocfs2_assign_bh(struct inode *inode,
1571                            struct buffer_head **ret_bh,
1572                            struct buffer_head *passed_bh)
1573 {
1574         int status;
1575
1576         if (passed_bh) {
1577                 /* Ok, the update went to disk for us, use the
1578                  * returned bh. */
1579                 *ret_bh = passed_bh;
1580                 get_bh(*ret_bh);
1581
1582                 return 0;
1583         }
1584
1585         status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1586                                   OCFS2_I(inode)->ip_blkno,
1587                                   ret_bh,
1588                                   OCFS2_BH_CACHED,
1589                                   inode);
1590         if (status < 0)
1591                 mlog_errno(status);
1592
1593         return status;
1594 }
1595
1596 /*
1597  * returns < 0 error if the callback will never be called, otherwise
1598  * the result of the lock will be communicated via the callback.
1599  */
1600 int ocfs2_meta_lock_full(struct inode *inode,
1601                          struct buffer_head **ret_bh,
1602                          int ex,
1603                          int arg_flags)
1604 {
1605         int status, level, dlm_flags, acquired;
1606         struct ocfs2_lock_res *lockres = NULL;
1607         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1608         struct buffer_head *local_bh = NULL;
1609
1610         BUG_ON(!inode);
1611
1612         mlog_entry_void();
1613
1614         mlog(0, "inode %llu, take %s META lock\n",
1615              (unsigned long long)OCFS2_I(inode)->ip_blkno,
1616              ex ? "EXMODE" : "PRMODE");
1617
1618         status = 0;
1619         acquired = 0;
1620         /* We'll allow faking a readonly metadata lock for
1621          * rodevices. */
1622         if (ocfs2_is_hard_readonly(osb)) {
1623                 if (ex)
1624                         status = -EROFS;
1625                 goto bail;
1626         }
1627
1628         if (ocfs2_mount_local(osb))
1629                 goto local;
1630
1631         if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1632                 wait_event(osb->recovery_event,
1633                            ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1634
1635         acquired = 0;
1636         lockres = &OCFS2_I(inode)->ip_meta_lockres;
1637         level = ex ? LKM_EXMODE : LKM_PRMODE;
1638         dlm_flags = 0;
1639         if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
1640                 dlm_flags |= LKM_NOQUEUE;
1641
1642         status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1643         if (status < 0) {
1644                 if (status != -EAGAIN && status != -EIOCBRETRY)
1645                         mlog_errno(status);
1646                 goto bail;
1647         }
1648
1649         /* Notify the error cleanup path to drop the cluster lock. */
1650         acquired = 1;
1651
1652         /* We wait twice because a node may have died while we were in
1653          * the lower dlm layers. The second time though, we've
1654          * committed to owning this lock so we don't allow signals to
1655          * abort the operation. */
1656         if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1657                 wait_event(osb->recovery_event,
1658                            ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1659
1660 local:
1661         /*
1662          * We only see this flag if we're being called from
1663          * ocfs2_read_locked_inode(). It means we're locking an inode
1664          * which hasn't been populated yet, so clear the refresh flag
1665          * and let the caller handle it.
1666          */
1667         if (inode->i_state & I_NEW) {
1668                 status = 0;
1669                 if (lockres)
1670                         ocfs2_complete_lock_res_refresh(lockres, 0);
1671                 goto bail;
1672         }
1673
1674         /* This is fun. The caller may want a bh back, or it may
1675          * not. ocfs2_meta_lock_update definitely wants one in, but
1676          * may or may not read one, depending on what's in the
1677          * LVB. The result of all of this is that we've *only* gone to
1678          * disk if we have to, so the complexity is worthwhile. */
1679         status = ocfs2_meta_lock_update(inode, &local_bh);
1680         if (status < 0) {
1681                 if (status != -ENOENT)
1682                         mlog_errno(status);
1683                 goto bail;
1684         }
1685
1686         if (ret_bh) {
1687                 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
1688                 if (status < 0) {
1689                         mlog_errno(status);
1690                         goto bail;
1691                 }
1692         }
1693
1694 bail:
1695         if (status < 0) {
1696                 if (ret_bh && (*ret_bh)) {
1697                         brelse(*ret_bh);
1698                         *ret_bh = NULL;
1699                 }
1700                 if (acquired)
1701                         ocfs2_meta_unlock(inode, ex);
1702         }
1703
1704         if (local_bh)
1705                 brelse(local_bh);
1706
1707         mlog_exit(status);
1708         return status;
1709 }
1710
1711 /*
1712  * This is working around a lock inversion between tasks acquiring DLM locks
1713  * while holding a page lock and the vote thread which blocks dlm lock acquiry
1714  * while acquiring page locks.
1715  *
1716  * ** These _with_page variantes are only intended to be called from aop
1717  * methods that hold page locks and return a very specific *positive* error
1718  * code that aop methods pass up to the VFS -- test for errors with != 0. **
1719  *
1720  * The DLM is called such that it returns -EAGAIN if it would have blocked
1721  * waiting for the vote thread.  In that case we unlock our page so the vote
1722  * thread can make progress.  Once we've done this we have to return
1723  * AOP_TRUNCATED_PAGE so the aop method that called us can bubble that back up
1724  * into the VFS who will then immediately retry the aop call.
1725  *
1726  * We do a blocking lock and immediate unlock before returning, though, so that
1727  * the lock has a great chance of being cached on this node by the time the VFS
1728  * calls back to retry the aop.    This has a potential to livelock as nodes
1729  * ping locks back and forth, but that's a risk we're willing to take to avoid
1730  * the lock inversion simply.
1731  */
1732 int ocfs2_meta_lock_with_page(struct inode *inode,
1733                               struct buffer_head **ret_bh,
1734                               int ex,
1735                               struct page *page)
1736 {
1737         int ret;
1738
1739         ret = ocfs2_meta_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
1740         if (ret == -EAGAIN) {
1741                 unlock_page(page);
1742                 if (ocfs2_meta_lock(inode, ret_bh, ex) == 0)
1743                         ocfs2_meta_unlock(inode, ex);
1744                 ret = AOP_TRUNCATED_PAGE;
1745         }
1746
1747         return ret;
1748 }
1749
1750 int ocfs2_meta_lock_atime(struct inode *inode,
1751                           struct vfsmount *vfsmnt,
1752                           int *level)
1753 {
1754         int ret;
1755
1756         mlog_entry_void();
1757         ret = ocfs2_meta_lock(inode, NULL, 0);
1758         if (ret < 0) {
1759                 mlog_errno(ret);
1760                 return ret;
1761         }
1762
1763         /*
1764          * If we should update atime, we will get EX lock,
1765          * otherwise we just get PR lock.
1766          */
1767         if (ocfs2_should_update_atime(inode, vfsmnt)) {
1768                 struct buffer_head *bh = NULL;
1769
1770                 ocfs2_meta_unlock(inode, 0);
1771                 ret = ocfs2_meta_lock(inode, &bh, 1);
1772                 if (ret < 0) {
1773                         mlog_errno(ret);
1774                         return ret;
1775                 }
1776                 *level = 1;
1777                 if (ocfs2_should_update_atime(inode, vfsmnt))
1778                         ocfs2_update_inode_atime(inode, bh);
1779                 if (bh)
1780                         brelse(bh);
1781         } else
1782                 *level = 0;
1783
1784         mlog_exit(ret);
1785         return ret;
1786 }
1787
1788 void ocfs2_meta_unlock(struct inode *inode,
1789                        int ex)
1790 {
1791         int level = ex ? LKM_EXMODE : LKM_PRMODE;
1792         struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
1793         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1794
1795         mlog_entry_void();
1796
1797         mlog(0, "inode %llu drop %s META lock\n",
1798              (unsigned long long)OCFS2_I(inode)->ip_blkno,
1799              ex ? "EXMODE" : "PRMODE");
1800
1801         if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
1802             !ocfs2_mount_local(osb))
1803                 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1804
1805         mlog_exit_void();
1806 }
1807
1808 int ocfs2_super_lock(struct ocfs2_super *osb,
1809                      int ex)
1810 {
1811         int status = 0;
1812         int level = ex ? LKM_EXMODE : LKM_PRMODE;
1813         struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1814         struct buffer_head *bh;
1815         struct ocfs2_slot_info *si = osb->slot_info;
1816
1817         mlog_entry_void();
1818
1819         if (ocfs2_is_hard_readonly(osb))
1820                 return -EROFS;
1821
1822         if (ocfs2_mount_local(osb))
1823                 goto bail;
1824
1825         status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
1826         if (status < 0) {
1827                 mlog_errno(status);
1828                 goto bail;
1829         }
1830
1831         /* The super block lock path is really in the best position to
1832          * know when resources covered by the lock need to be
1833          * refreshed, so we do it here. Of course, making sense of
1834          * everything is up to the caller :) */
1835         status = ocfs2_should_refresh_lock_res(lockres);
1836         if (status < 0) {
1837                 mlog_errno(status);
1838                 goto bail;
1839         }
1840         if (status) {
1841                 bh = si->si_bh;
1842                 status = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0,
1843                                           si->si_inode);
1844                 if (status == 0)
1845                         ocfs2_update_slot_info(si);
1846
1847                 ocfs2_complete_lock_res_refresh(lockres, status);
1848
1849                 if (status < 0)
1850                         mlog_errno(status);
1851         }
1852 bail:
1853         mlog_exit(status);
1854         return status;
1855 }
1856
1857 void ocfs2_super_unlock(struct ocfs2_super *osb,
1858                         int ex)
1859 {
1860         int level = ex ? LKM_EXMODE : LKM_PRMODE;
1861         struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1862
1863         if (!ocfs2_mount_local(osb))
1864                 ocfs2_cluster_unlock(osb, lockres, level);
1865 }
1866
1867 int ocfs2_rename_lock(struct ocfs2_super *osb)
1868 {
1869         int status;
1870         struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1871
1872         if (ocfs2_is_hard_readonly(osb))
1873                 return -EROFS;
1874
1875         if (ocfs2_mount_local(osb))
1876                 return 0;
1877
1878         status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
1879         if (status < 0)
1880                 mlog_errno(status);
1881
1882         return status;
1883 }
1884
1885 void ocfs2_rename_unlock(struct ocfs2_super *osb)
1886 {
1887         struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1888
1889         if (!ocfs2_mount_local(osb))
1890                 ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
1891 }
1892
1893 int ocfs2_dentry_lock(struct dentry *dentry, int ex)
1894 {
1895         int ret;
1896         int level = ex ? LKM_EXMODE : LKM_PRMODE;
1897         struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1898         struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1899
1900         BUG_ON(!dl);
1901
1902         if (ocfs2_is_hard_readonly(osb))
1903                 return -EROFS;
1904
1905         if (ocfs2_mount_local(osb))
1906                 return 0;
1907
1908         ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
1909         if (ret < 0)
1910                 mlog_errno(ret);
1911
1912         return ret;
1913 }
1914
1915 void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
1916 {
1917         int level = ex ? LKM_EXMODE : LKM_PRMODE;
1918         struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1919         struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1920
1921         if (!ocfs2_mount_local(osb))
1922                 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
1923 }
1924
1925 /* Reference counting of the dlm debug structure. We want this because
1926  * open references on the debug inodes can live on after a mount, so
1927  * we can't rely on the ocfs2_super to always exist. */
1928 static void ocfs2_dlm_debug_free(struct kref *kref)
1929 {
1930         struct ocfs2_dlm_debug *dlm_debug;
1931
1932         dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
1933
1934         kfree(dlm_debug);
1935 }
1936
1937 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
1938 {
1939         if (dlm_debug)
1940                 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
1941 }
1942
1943 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
1944 {
1945         kref_get(&debug->d_refcnt);
1946 }
1947
1948 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
1949 {
1950         struct ocfs2_dlm_debug *dlm_debug;
1951
1952         dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
1953         if (!dlm_debug) {
1954                 mlog_errno(-ENOMEM);
1955                 goto out;
1956         }
1957
1958         kref_init(&dlm_debug->d_refcnt);
1959         INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
1960         dlm_debug->d_locking_state = NULL;
1961 out:
1962         return dlm_debug;
1963 }
1964
1965 /* Access to this is arbitrated for us via seq_file->sem. */
1966 struct ocfs2_dlm_seq_priv {
1967         struct ocfs2_dlm_debug *p_dlm_debug;
1968         struct ocfs2_lock_res p_iter_res;
1969         struct ocfs2_lock_res p_tmp_res;
1970 };
1971
1972 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
1973                                                  struct ocfs2_dlm_seq_priv *priv)
1974 {
1975         struct ocfs2_lock_res *iter, *ret = NULL;
1976         struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
1977
1978         assert_spin_locked(&ocfs2_dlm_tracking_lock);
1979
1980         list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
1981                 /* discover the head of the list */
1982                 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
1983                         mlog(0, "End of list found, %p\n", ret);
1984                         break;
1985                 }
1986
1987                 /* We track our "dummy" iteration lockres' by a NULL
1988                  * l_ops field. */
1989                 if (iter->l_ops != NULL) {
1990                         ret = iter;
1991                         break;
1992                 }
1993         }
1994
1995         return ret;
1996 }
1997
1998 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
1999 {
2000         struct ocfs2_dlm_seq_priv *priv = m->private;
2001         struct ocfs2_lock_res *iter;
2002
2003         spin_lock(&ocfs2_dlm_tracking_lock);
2004         iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2005         if (iter) {
2006                 /* Since lockres' have the lifetime of their container
2007                  * (which can be inodes, ocfs2_supers, etc) we want to
2008                  * copy this out to a temporary lockres while still
2009                  * under the spinlock. Obviously after this we can't
2010                  * trust any pointers on the copy returned, but that's
2011                  * ok as the information we want isn't typically held
2012                  * in them. */
2013                 priv->p_tmp_res = *iter;
2014                 iter = &priv->p_tmp_res;
2015         }
2016         spin_unlock(&ocfs2_dlm_tracking_lock);
2017
2018         return iter;
2019 }
2020
2021 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2022 {
2023 }
2024
2025 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2026 {
2027         struct ocfs2_dlm_seq_priv *priv = m->private;
2028         struct ocfs2_lock_res *iter = v;
2029         struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2030
2031         spin_lock(&ocfs2_dlm_tracking_lock);
2032         iter = ocfs2_dlm_next_res(iter, priv);
2033         list_del_init(&dummy->l_debug_list);
2034         if (iter) {
2035                 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2036                 priv->p_tmp_res = *iter;
2037                 iter = &priv->p_tmp_res;
2038         }
2039         spin_unlock(&ocfs2_dlm_tracking_lock);
2040
2041         return iter;
2042 }
2043
2044 /* So that debugfs.ocfs2 can determine which format is being used */
2045 #define OCFS2_DLM_DEBUG_STR_VERSION 1
2046 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2047 {
2048         int i;
2049         char *lvb;
2050         struct ocfs2_lock_res *lockres = v;
2051
2052         if (!lockres)
2053                 return -EINVAL;
2054
2055         seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2056
2057         if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2058                 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2059                            lockres->l_name,
2060                            (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2061         else
2062                 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2063
2064         seq_printf(m, "%d\t"
2065                    "0x%lx\t"
2066                    "0x%x\t"
2067                    "0x%x\t"
2068                    "%u\t"
2069                    "%u\t"
2070                    "%d\t"
2071                    "%d\t",
2072                    lockres->l_level,
2073                    lockres->l_flags,
2074                    lockres->l_action,
2075                    lockres->l_unlock_action,
2076                    lockres->l_ro_holders,
2077                    lockres->l_ex_holders,
2078                    lockres->l_requested,
2079                    lockres->l_blocking);
2080
2081         /* Dump the raw LVB */
2082         lvb = lockres->l_lksb.lvb;
2083         for(i = 0; i < DLM_LVB_LEN; i++)
2084                 seq_printf(m, "0x%x\t", lvb[i]);
2085
2086         /* End the line */
2087         seq_printf(m, "\n");
2088         return 0;
2089 }
2090
2091 static struct seq_operations ocfs2_dlm_seq_ops = {
2092         .start =        ocfs2_dlm_seq_start,
2093         .stop =         ocfs2_dlm_seq_stop,
2094         .next =         ocfs2_dlm_seq_next,
2095         .show =         ocfs2_dlm_seq_show,
2096 };
2097
2098 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2099 {
2100         struct seq_file *seq = (struct seq_file *) file->private_data;
2101         struct ocfs2_dlm_seq_priv *priv = seq->private;
2102         struct ocfs2_lock_res *res = &priv->p_iter_res;
2103
2104         ocfs2_remove_lockres_tracking(res);
2105         ocfs2_put_dlm_debug(priv->p_dlm_debug);
2106         return seq_release_private(inode, file);
2107 }
2108
2109 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2110 {
2111         int ret;
2112         struct ocfs2_dlm_seq_priv *priv;
2113         struct seq_file *seq;
2114         struct ocfs2_super *osb;
2115
2116         priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2117         if (!priv) {
2118                 ret = -ENOMEM;
2119                 mlog_errno(ret);
2120                 goto out;
2121         }
2122         osb = inode->i_private;
2123         ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2124         priv->p_dlm_debug = osb->osb_dlm_debug;
2125         INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2126
2127         ret = seq_open(file, &ocfs2_dlm_seq_ops);
2128         if (ret) {
2129                 kfree(priv);
2130                 mlog_errno(ret);
2131                 goto out;
2132         }
2133
2134         seq = (struct seq_file *) file->private_data;
2135         seq->private = priv;
2136
2137         ocfs2_add_lockres_tracking(&priv->p_iter_res,
2138                                    priv->p_dlm_debug);
2139
2140 out:
2141         return ret;
2142 }
2143
2144 static const struct file_operations ocfs2_dlm_debug_fops = {
2145         .open =         ocfs2_dlm_debug_open,
2146         .release =      ocfs2_dlm_debug_release,
2147         .read =         seq_read,
2148         .llseek =       seq_lseek,
2149 };
2150
2151 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2152 {
2153         int ret = 0;
2154         struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2155
2156         dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2157                                                          S_IFREG|S_IRUSR,
2158                                                          osb->osb_debug_root,
2159                                                          osb,
2160                                                          &ocfs2_dlm_debug_fops);
2161         if (!dlm_debug->d_locking_state) {
2162                 ret = -EINVAL;
2163                 mlog(ML_ERROR,
2164                      "Unable to create locking state debugfs file.\n");
2165                 goto out;
2166         }
2167
2168         ocfs2_get_dlm_debug(dlm_debug);
2169 out:
2170         return ret;
2171 }
2172
2173 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2174 {
2175         struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2176
2177         if (dlm_debug) {
2178                 debugfs_remove(dlm_debug->d_locking_state);
2179                 ocfs2_put_dlm_debug(dlm_debug);
2180         }
2181 }
2182
2183 int ocfs2_dlm_init(struct ocfs2_super *osb)
2184 {
2185         int status = 0;
2186         u32 dlm_key;
2187         struct dlm_ctxt *dlm = NULL;
2188
2189         mlog_entry_void();
2190
2191         if (ocfs2_mount_local(osb))
2192                 goto local;
2193
2194         status = ocfs2_dlm_init_debug(osb);
2195         if (status < 0) {
2196                 mlog_errno(status);
2197                 goto bail;
2198         }
2199
2200         /* launch vote thread */
2201         osb->vote_task = kthread_run(ocfs2_vote_thread, osb, "ocfs2vote");
2202         if (IS_ERR(osb->vote_task)) {
2203                 status = PTR_ERR(osb->vote_task);
2204                 osb->vote_task = NULL;
2205                 mlog_errno(status);
2206                 goto bail;
2207         }
2208
2209         /* used by the dlm code to make message headers unique, each
2210          * node in this domain must agree on this. */
2211         dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
2212
2213         /* for now, uuid == domain */
2214         dlm = dlm_register_domain(osb->uuid_str, dlm_key);
2215         if (IS_ERR(dlm)) {
2216                 status = PTR_ERR(dlm);
2217                 mlog_errno(status);
2218                 goto bail;
2219         }
2220
2221         dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
2222
2223 local:
2224         ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2225         ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2226
2227         osb->dlm = dlm;
2228
2229         status = 0;
2230 bail:
2231         if (status < 0) {
2232                 ocfs2_dlm_shutdown_debug(osb);
2233                 if (osb->vote_task)
2234                         kthread_stop(osb->vote_task);
2235         }
2236
2237         mlog_exit(status);
2238         return status;
2239 }
2240
2241 void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2242 {
2243         mlog_entry_void();
2244
2245         dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
2246
2247         ocfs2_drop_osb_locks(osb);
2248
2249         if (osb->vote_task) {
2250                 kthread_stop(osb->vote_task);
2251                 osb->vote_task = NULL;
2252         }
2253
2254         ocfs2_lock_res_free(&osb->osb_super_lockres);
2255         ocfs2_lock_res_free(&osb->osb_rename_lockres);
2256
2257         dlm_unregister_domain(osb->dlm);
2258         osb->dlm = NULL;
2259
2260         ocfs2_dlm_shutdown_debug(osb);
2261
2262         mlog_exit_void();
2263 }
2264
2265 static void ocfs2_unlock_ast(void *opaque, enum dlm_status status)
2266 {
2267         struct ocfs2_lock_res *lockres = opaque;
2268         unsigned long flags;
2269
2270         mlog_entry_void();
2271
2272         mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2273              lockres->l_unlock_action);
2274
2275         spin_lock_irqsave(&lockres->l_lock, flags);
2276         /* We tried to cancel a convert request, but it was already
2277          * granted. All we want to do here is clear our unlock
2278          * state. The wake_up call done at the bottom is redundant
2279          * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2280          * hurt anything anyway */
2281         if (status == DLM_CANCELGRANT &&
2282             lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2283                 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2284
2285                 /* We don't clear the busy flag in this case as it
2286                  * should have been cleared by the ast which the dlm
2287                  * has called. */
2288                 goto complete_unlock;
2289         }
2290
2291         if (status != DLM_NORMAL) {
2292                 mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
2293                      "unlock_action %d\n", status, lockres->l_name,
2294                      lockres->l_unlock_action);
2295                 spin_unlock_irqrestore(&lockres->l_lock, flags);
2296                 return;
2297         }
2298
2299         switch(lockres->l_unlock_action) {
2300         case OCFS2_UNLOCK_CANCEL_CONVERT:
2301                 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2302                 lockres->l_action = OCFS2_AST_INVALID;
2303                 break;
2304         case OCFS2_UNLOCK_DROP_LOCK:
2305                 lockres->l_level = LKM_IVMODE;
2306                 break;
2307         default:
2308                 BUG();
2309         }
2310
2311         lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2312 complete_unlock:
2313         lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2314         spin_unlock_irqrestore(&lockres->l_lock, flags);
2315
2316         wake_up(&lockres->l_event);
2317
2318         mlog_exit_void();
2319 }
2320
2321 static int ocfs2_drop_lock(struct ocfs2_super *osb,
2322                            struct ocfs2_lock_res *lockres)
2323 {
2324         enum dlm_status status;
2325         unsigned long flags;
2326         int lkm_flags = 0;
2327
2328         /* We didn't get anywhere near actually using this lockres. */
2329         if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2330                 goto out;
2331
2332         if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
2333                 lkm_flags |= LKM_VALBLK;
2334
2335         spin_lock_irqsave(&lockres->l_lock, flags);
2336
2337         mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2338                         "lockres %s, flags 0x%lx\n",
2339                         lockres->l_name, lockres->l_flags);
2340
2341         while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2342                 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2343                      "%u, unlock_action = %u\n",
2344                      lockres->l_name, lockres->l_flags, lockres->l_action,
2345                      lockres->l_unlock_action);
2346
2347                 spin_unlock_irqrestore(&lockres->l_lock, flags);
2348
2349                 /* XXX: Today we just wait on any busy
2350                  * locks... Perhaps we need to cancel converts in the
2351                  * future? */
2352                 ocfs2_wait_on_busy_lock(lockres);
2353
2354                 spin_lock_irqsave(&lockres->l_lock, flags);
2355         }
2356
2357         if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2358                 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2359                     lockres->l_level == LKM_EXMODE &&
2360                     !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2361                         lockres->l_ops->set_lvb(lockres);
2362         }
2363
2364         if (lockres->l_flags & OCFS2_LOCK_BUSY)
2365                 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2366                      lockres->l_name);
2367         if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2368                 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2369
2370         if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2371                 spin_unlock_irqrestore(&lockres->l_lock, flags);
2372                 goto out;
2373         }
2374
2375         lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2376
2377         /* make sure we never get here while waiting for an ast to
2378          * fire. */
2379         BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2380
2381         /* is this necessary? */
2382         lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2383         lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2384         spin_unlock_irqrestore(&lockres->l_lock, flags);
2385
2386         mlog(0, "lock %s\n", lockres->l_name);
2387
2388         status = dlmunlock(osb->dlm, &lockres->l_lksb, lkm_flags,
2389                            ocfs2_unlock_ast, lockres);
2390         if (status != DLM_NORMAL) {
2391                 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2392                 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2393                 dlm_print_one_lock(lockres->l_lksb.lockid);
2394                 BUG();
2395         }
2396         mlog(0, "lock %s, successfull return from dlmunlock\n",
2397              lockres->l_name);
2398
2399         ocfs2_wait_on_busy_lock(lockres);
2400 out:
2401         mlog_exit(0);
2402         return 0;
2403 }
2404
2405 /* Mark the lockres as being dropped. It will no longer be
2406  * queued if blocking, but we still may have to wait on it
2407  * being dequeued from the vote thread before we can consider
2408  * it safe to drop. 
2409  *
2410  * You can *not* attempt to call cluster_lock on this lockres anymore. */
2411 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2412 {
2413         int status;
2414         struct ocfs2_mask_waiter mw;
2415         unsigned long flags;
2416
2417         ocfs2_init_mask_waiter(&mw);
2418
2419         spin_lock_irqsave(&lockres->l_lock, flags);
2420         lockres->l_flags |= OCFS2_LOCK_FREEING;
2421         while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2422                 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2423                 spin_unlock_irqrestore(&lockres->l_lock, flags);
2424
2425                 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2426
2427                 status = ocfs2_wait_for_mask(&mw);
2428                 if (status)
2429                         mlog_errno(status);
2430
2431                 spin_lock_irqsave(&lockres->l_lock, flags);
2432         }
2433         spin_unlock_irqrestore(&lockres->l_lock, flags);
2434 }
2435
2436 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2437                                struct ocfs2_lock_res *lockres)
2438 {
2439         int ret;
2440
2441         ocfs2_mark_lockres_freeing(lockres);
2442         ret = ocfs2_drop_lock(osb, lockres);
2443         if (ret)
2444                 mlog_errno(ret);
2445 }
2446
2447 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2448 {
2449         ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2450         ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
2451 }
2452
2453 int ocfs2_drop_inode_locks(struct inode *inode)
2454 {
2455         int status, err;
2456
2457         mlog_entry_void();
2458
2459         /* No need to call ocfs2_mark_lockres_freeing here -
2460          * ocfs2_clear_inode has done it for us. */
2461
2462         err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2463                               &OCFS2_I(inode)->ip_data_lockres);
2464         if (err < 0)
2465                 mlog_errno(err);
2466
2467         status = err;
2468
2469         err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2470                               &OCFS2_I(inode)->ip_meta_lockres);
2471         if (err < 0)
2472                 mlog_errno(err);
2473         if (err < 0 && !status)
2474                 status = err;
2475
2476         err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2477                               &OCFS2_I(inode)->ip_rw_lockres);
2478         if (err < 0)
2479                 mlog_errno(err);
2480         if (err < 0 && !status)
2481                 status = err;
2482
2483         mlog_exit(status);
2484         return status;
2485 }
2486
2487 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2488                                       int new_level)
2489 {
2490         assert_spin_locked(&lockres->l_lock);
2491
2492         BUG_ON(lockres->l_blocking <= LKM_NLMODE);
2493
2494         if (lockres->l_level <= new_level) {
2495                 mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
2496                      lockres->l_level, new_level);
2497                 BUG();
2498         }
2499
2500         mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2501              lockres->l_name, new_level, lockres->l_blocking);
2502
2503         lockres->l_action = OCFS2_AST_DOWNCONVERT;
2504         lockres->l_requested = new_level;
2505         lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2506 }
2507
2508 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2509                                   struct ocfs2_lock_res *lockres,
2510                                   int new_level,
2511                                   int lvb)
2512 {
2513         int ret, dlm_flags = LKM_CONVERT;
2514         enum dlm_status status;
2515
2516         mlog_entry_void();
2517
2518         if (lvb)
2519                 dlm_flags |= LKM_VALBLK;
2520
2521         status = dlmlock(osb->dlm,
2522                          new_level,
2523                          &lockres->l_lksb,
2524                          dlm_flags,
2525                          lockres->l_name,
2526                          OCFS2_LOCK_ID_MAX_LEN - 1,
2527                          ocfs2_locking_ast,
2528                          lockres,
2529                          ocfs2_blocking_ast);
2530         if (status != DLM_NORMAL) {
2531                 ocfs2_log_dlm_error("dlmlock", status, lockres);
2532                 ret = -EINVAL;
2533                 ocfs2_recover_from_dlm_error(lockres, 1);
2534                 goto bail;
2535         }
2536
2537         ret = 0;
2538 bail:
2539         mlog_exit(ret);
2540         return ret;
2541 }
2542
2543 /* returns 1 when the caller should unlock and call dlmunlock */
2544 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2545                                         struct ocfs2_lock_res *lockres)
2546 {
2547         assert_spin_locked(&lockres->l_lock);
2548
2549         mlog_entry_void();
2550         mlog(0, "lock %s\n", lockres->l_name);
2551
2552         if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2553                 /* If we're already trying to cancel a lock conversion
2554                  * then just drop the spinlock and allow the caller to
2555                  * requeue this lock. */
2556
2557                 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2558                 return 0;
2559         }
2560
2561         /* were we in a convert when we got the bast fire? */
2562         BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2563                lockres->l_action != OCFS2_AST_DOWNCONVERT);
2564         /* set things up for the unlockast to know to just
2565          * clear out the ast_action and unset busy, etc. */
2566         lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2567
2568         mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2569                         "lock %s, invalid flags: 0x%lx\n",
2570                         lockres->l_name, lockres->l_flags);
2571
2572         return 1;
2573 }
2574
2575 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2576                                 struct ocfs2_lock_res *lockres)
2577 {
2578         int ret;
2579         enum dlm_status status;
2580
2581         mlog_entry_void();
2582         mlog(0, "lock %s\n", lockres->l_name);
2583
2584         ret = 0;
2585         status = dlmunlock(osb->dlm,
2586                            &lockres->l_lksb,
2587                            LKM_CANCEL,
2588                            ocfs2_unlock_ast,
2589                            lockres);
2590         if (status != DLM_NORMAL) {
2591                 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2592                 ret = -EINVAL;
2593                 ocfs2_recover_from_dlm_error(lockres, 0);
2594         }
2595
2596         mlog(0, "lock %s return from dlmunlock\n", lockres->l_name);
2597
2598         mlog_exit(ret);
2599         return ret;
2600 }
2601
2602 static int ocfs2_unblock_lock(struct ocfs2_super *osb,
2603                               struct ocfs2_lock_res *lockres,
2604                               struct ocfs2_unblock_ctl *ctl)
2605 {
2606         unsigned long flags;
2607         int blocking;
2608         int new_level;
2609         int ret = 0;
2610         int set_lvb = 0;
2611
2612         mlog_entry_void();
2613
2614         spin_lock_irqsave(&lockres->l_lock, flags);
2615
2616         BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2617
2618 recheck:
2619         if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2620                 ctl->requeue = 1;
2621                 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2622                 spin_unlock_irqrestore(&lockres->l_lock, flags);
2623                 if (ret) {
2624                         ret = ocfs2_cancel_convert(osb, lockres);
2625                         if (ret < 0)
2626                                 mlog_errno(ret);
2627                 }
2628                 goto leave;
2629         }
2630
2631         /* if we're blocking an exclusive and we have *any* holders,
2632          * then requeue. */
2633         if ((lockres->l_blocking == LKM_EXMODE)
2634             && (lockres->l_ex_holders || lockres->l_ro_holders))
2635                 goto leave_requeue;
2636
2637         /* If it's a PR we're blocking, then only
2638          * requeue if we've got any EX holders */
2639         if (lockres->l_blocking == LKM_PRMODE &&
2640             lockres->l_ex_holders)
2641                 goto leave_requeue;
2642
2643         /*
2644          * Can we get a lock in this state if the holder counts are
2645          * zero? The meta data unblock code used to check this.
2646          */
2647         if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
2648             && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
2649                 goto leave_requeue;
2650
2651         new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2652
2653         if (lockres->l_ops->check_downconvert
2654             && !lockres->l_ops->check_downconvert(lockres, new_level))
2655                 goto leave_requeue;
2656
2657         /* If we get here, then we know that there are no more
2658          * incompatible holders (and anyone asking for an incompatible
2659          * lock is blocked). We can now downconvert the lock */
2660         if (!lockres->l_ops->downconvert_worker)
2661                 goto downconvert;
2662
2663         /* Some lockres types want to do a bit of work before
2664          * downconverting a lock. Allow that here. The worker function
2665          * may sleep, so we save off a copy of what we're blocking as
2666          * it may change while we're not holding the spin lock. */
2667         blocking = lockres->l_blocking;
2668         spin_unlock_irqrestore(&lockres->l_lock, flags);
2669
2670         ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
2671
2672         if (ctl->unblock_action == UNBLOCK_STOP_POST)
2673                 goto leave;
2674
2675         spin_lock_irqsave(&lockres->l_lock, flags);
2676         if (blocking != lockres->l_blocking) {
2677                 /* If this changed underneath us, then we can't drop
2678                  * it just yet. */
2679                 goto recheck;
2680         }
2681
2682 downconvert:
2683         ctl->requeue = 0;
2684
2685         if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2686                 if (lockres->l_level == LKM_EXMODE)
2687                         set_lvb = 1;
2688
2689                 /*
2690                  * We only set the lvb if the lock has been fully
2691                  * refreshed - otherwise we risk setting stale
2692                  * data. Otherwise, there's no need to actually clear
2693                  * out the lvb here as it's value is still valid.
2694                  */
2695                 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2696                         lockres->l_ops->set_lvb(lockres);
2697         }
2698
2699         ocfs2_prepare_downconvert(lockres, new_level);
2700         spin_unlock_irqrestore(&lockres->l_lock, flags);
2701         ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
2702 leave:
2703         mlog_exit(ret);
2704         return ret;
2705
2706 leave_requeue:
2707         spin_unlock_irqrestore(&lockres->l_lock, flags);
2708         ctl->requeue = 1;
2709
2710         mlog_exit(0);
2711         return 0;
2712 }
2713
2714 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
2715                                      int blocking)
2716 {
2717         struct inode *inode;
2718         struct address_space *mapping;
2719
2720         inode = ocfs2_lock_res_inode(lockres);
2721         mapping = inode->i_mapping;
2722
2723         /*
2724          * We need this before the filemap_fdatawrite() so that it can
2725          * transfer the dirty bit from the PTE to the
2726          * page. Unfortunately this means that even for EX->PR
2727          * downconverts, we'll lose our mappings and have to build
2728          * them up again.
2729          */
2730         unmap_mapping_range(mapping, 0, 0, 0);
2731
2732         if (filemap_fdatawrite(mapping)) {
2733                 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
2734                      (unsigned long long)OCFS2_I(inode)->ip_blkno);
2735         }
2736         sync_mapping_buffers(mapping);
2737         if (blocking == LKM_EXMODE) {
2738                 truncate_inode_pages(mapping, 0);
2739         } else {
2740                 /* We only need to wait on the I/O if we're not also
2741                  * truncating pages because truncate_inode_pages waits
2742                  * for us above. We don't truncate pages if we're
2743                  * blocking anything < EXMODE because we want to keep
2744                  * them around in that case. */
2745                 filemap_fdatawait(mapping);
2746         }
2747
2748         return UNBLOCK_CONTINUE;
2749 }
2750
2751 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
2752                                         int new_level)
2753 {
2754         struct inode *inode = ocfs2_lock_res_inode(lockres);
2755         int checkpointed = ocfs2_inode_fully_checkpointed(inode);
2756
2757         BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
2758         BUG_ON(lockres->l_level != LKM_EXMODE && !checkpointed);
2759
2760         if (checkpointed)
2761                 return 1;
2762
2763         ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
2764         return 0;
2765 }
2766
2767 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
2768 {
2769         struct inode *inode = ocfs2_lock_res_inode(lockres);
2770
2771         __ocfs2_stuff_meta_lvb(inode);
2772 }
2773
2774 /*
2775  * Does the final reference drop on our dentry lock. Right now this
2776  * happens in the vote thread, but we could choose to simplify the
2777  * dlmglue API and push these off to the ocfs2_wq in the future.
2778  */
2779 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
2780                                      struct ocfs2_lock_res *lockres)
2781 {
2782         struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2783         ocfs2_dentry_lock_put(osb, dl);
2784 }
2785
2786 /*
2787  * d_delete() matching dentries before the lock downconvert.
2788  *
2789  * At this point, any process waiting to destroy the
2790  * dentry_lock due to last ref count is stopped by the
2791  * OCFS2_LOCK_QUEUED flag.
2792  *
2793  * We have two potential problems
2794  *
2795  * 1) If we do the last reference drop on our dentry_lock (via dput)
2796  *    we'll wind up in ocfs2_release_dentry_lock(), waiting on
2797  *    the downconvert to finish. Instead we take an elevated
2798  *    reference and push the drop until after we've completed our
2799  *    unblock processing.
2800  *
2801  * 2) There might be another process with a final reference,
2802  *    waiting on us to finish processing. If this is the case, we
2803  *    detect it and exit out - there's no more dentries anyway.
2804  */
2805 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
2806                                        int blocking)
2807 {
2808         struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2809         struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
2810         struct dentry *dentry;
2811         unsigned long flags;
2812         int extra_ref = 0;
2813
2814         /*
2815          * This node is blocking another node from getting a read
2816          * lock. This happens when we've renamed within a
2817          * directory. We've forced the other nodes to d_delete(), but
2818          * we never actually dropped our lock because it's still
2819          * valid. The downconvert code will retain a PR for this node,
2820          * so there's no further work to do.
2821          */
2822         if (blocking == LKM_PRMODE)
2823                 return UNBLOCK_CONTINUE;
2824
2825         /*
2826          * Mark this inode as potentially orphaned. The code in
2827          * ocfs2_delete_inode() will figure out whether it actually
2828          * needs to be freed or not.
2829          */
2830         spin_lock(&oi->ip_lock);
2831         oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
2832         spin_unlock(&oi->ip_lock);
2833
2834         /*
2835          * Yuck. We need to make sure however that the check of
2836          * OCFS2_LOCK_FREEING and the extra reference are atomic with
2837          * respect to a reference decrement or the setting of that
2838          * flag.
2839          */
2840         spin_lock_irqsave(&lockres->l_lock, flags);
2841         spin_lock(&dentry_attach_lock);
2842         if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
2843             && dl->dl_count) {
2844                 dl->dl_count++;
2845                 extra_ref = 1;
2846         }
2847         spin_unlock(&dentry_attach_lock);
2848         spin_unlock_irqrestore(&lockres->l_lock, flags);
2849
2850         mlog(0, "extra_ref = %d\n", extra_ref);
2851
2852         /*
2853          * We have a process waiting on us in ocfs2_dentry_iput(),
2854          * which means we can't have any more outstanding
2855          * aliases. There's no need to do any more work.
2856          */
2857         if (!extra_ref)
2858                 return UNBLOCK_CONTINUE;
2859
2860         spin_lock(&dentry_attach_lock);
2861         while (1) {
2862                 dentry = ocfs2_find_local_alias(dl->dl_inode,
2863                                                 dl->dl_parent_blkno, 1);
2864                 if (!dentry)
2865                         break;
2866                 spin_unlock(&dentry_attach_lock);
2867
2868                 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
2869                      dentry->d_name.name);
2870
2871                 /*
2872                  * The following dcache calls may do an
2873                  * iput(). Normally we don't want that from the
2874                  * downconverting thread, but in this case it's ok
2875                  * because the requesting node already has an
2876                  * exclusive lock on the inode, so it can't be queued
2877                  * for a downconvert.
2878                  */
2879                 d_delete(dentry);
2880                 dput(dentry);
2881
2882                 spin_lock(&dentry_attach_lock);
2883         }
2884         spin_unlock(&dentry_attach_lock);
2885
2886         /*
2887          * If we are the last holder of this dentry lock, there is no
2888          * reason to downconvert so skip straight to the unlock.
2889          */
2890         if (dl->dl_count == 1)
2891                 return UNBLOCK_STOP_POST;
2892
2893         return UNBLOCK_CONTINUE_POST;
2894 }
2895
2896 void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
2897                                 struct ocfs2_lock_res *lockres)
2898 {
2899         int status;
2900         struct ocfs2_unblock_ctl ctl = {0, 0,};
2901         unsigned long flags;
2902
2903         /* Our reference to the lockres in this function can be
2904          * considered valid until we remove the OCFS2_LOCK_QUEUED
2905          * flag. */
2906
2907         mlog_entry_void();
2908
2909         BUG_ON(!lockres);
2910         BUG_ON(!lockres->l_ops);
2911
2912         mlog(0, "lockres %s blocked.\n", lockres->l_name);
2913
2914         /* Detect whether a lock has been marked as going away while
2915          * the vote thread was processing other things. A lock can
2916          * still be marked with OCFS2_LOCK_FREEING after this check,
2917          * but short circuiting here will still save us some
2918          * performance. */
2919         spin_lock_irqsave(&lockres->l_lock, flags);
2920         if (lockres->l_flags & OCFS2_LOCK_FREEING)
2921                 goto unqueue;
2922         spin_unlock_irqrestore(&lockres->l_lock, flags);
2923
2924         status = ocfs2_unblock_lock(osb, lockres, &ctl);
2925         if (status < 0)
2926                 mlog_errno(status);
2927
2928         spin_lock_irqsave(&lockres->l_lock, flags);
2929 unqueue:
2930         if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
2931                 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
2932         } else
2933                 ocfs2_schedule_blocked_lock(osb, lockres);
2934
2935         mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
2936              ctl.requeue ? "yes" : "no");
2937         spin_unlock_irqrestore(&lockres->l_lock, flags);
2938
2939         if (ctl.unblock_action != UNBLOCK_CONTINUE
2940             && lockres->l_ops->post_unlock)
2941                 lockres->l_ops->post_unlock(osb, lockres);
2942
2943         mlog_exit_void();
2944 }
2945
2946 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
2947                                         struct ocfs2_lock_res *lockres)
2948 {
2949         mlog_entry_void();
2950
2951         assert_spin_locked(&lockres->l_lock);
2952
2953         if (lockres->l_flags & OCFS2_LOCK_FREEING) {
2954                 /* Do not schedule a lock for downconvert when it's on
2955                  * the way to destruction - any nodes wanting access
2956                  * to the resource will get it soon. */
2957                 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
2958                      lockres->l_name, lockres->l_flags);
2959                 return;
2960         }
2961
2962         lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
2963
2964         spin_lock(&osb->vote_task_lock);
2965         if (list_empty(&lockres->l_blocked_list)) {
2966                 list_add_tail(&lockres->l_blocked_list,
2967                               &osb->blocked_lock_list);
2968                 osb->blocked_lock_count++;
2969         }
2970         spin_unlock(&osb->vote_task_lock);
2971
2972         mlog_exit_void();
2973 }
2974
2975 /* This aids in debugging situations where a bad LVB might be involved. */
2976 void ocfs2_dump_meta_lvb_info(u64 level,
2977                               const char *function,
2978                               unsigned int line,
2979                               struct ocfs2_lock_res *lockres)
2980 {
2981         struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
2982
2983         mlog(level, "LVB information for %s (called from %s:%u):\n",
2984              lockres->l_name, function, line);
2985         mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
2986              lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
2987              be32_to_cpu(lvb->lvb_igeneration));
2988         mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
2989              (unsigned long long)be64_to_cpu(lvb->lvb_isize),
2990              be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
2991              be16_to_cpu(lvb->lvb_imode));
2992         mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
2993              "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
2994              (long long)be64_to_cpu(lvb->lvb_iatime_packed),
2995              (long long)be64_to_cpu(lvb->lvb_ictime_packed),
2996              (long long)be64_to_cpu(lvb->lvb_imtime_packed),
2997              be32_to_cpu(lvb->lvb_iattr));
2998 }