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