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