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