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