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