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