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