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