1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Code which implements an OCFS2 specific interface to our DLM.
8 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
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.
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.
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.
26 #include <linux/types.h>
27 #include <linux/slab.h>
28 #include <linux/highmem.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>
37 #include <cluster/heartbeat.h>
38 #include <cluster/nodemanager.h>
39 #include <cluster/tcp.h>
41 #include <dlm/dlmapi.h>
43 #define MLOG_MASK_PREFIX ML_DLM_GLUE
44 #include <cluster/masklog.h>
51 #include "extent_map.h"
53 #include "heartbeat.h"
61 #include "buffer_head_io.h"
63 struct ocfs2_mask_waiter {
64 struct list_head mw_item;
66 struct completion mw_complete;
67 unsigned long mw_mask;
68 unsigned long mw_goal;
71 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
72 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
75 * Return value from ->downconvert_worker functions.
77 * These control the precise actions of ocfs2_unblock_lock()
78 * and ocfs2_process_blocked_lock()
81 enum ocfs2_unblock_action {
82 UNBLOCK_CONTINUE = 0, /* Continue downconvert */
83 UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire
84 * ->post_unlock callback */
85 UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire
86 * ->post_unlock() callback. */
89 struct ocfs2_unblock_ctl {
91 enum ocfs2_unblock_action unblock_action;
94 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
96 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
98 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
101 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
104 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
105 struct ocfs2_lock_res *lockres);
108 * OCFS2 Lock Resource Operations
110 * These fine tune the behavior of the generic dlmglue locking infrastructure.
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.
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
120 struct ocfs2_lock_res_ops {
122 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
123 * this callback if ->l_priv is not an ocfs2_super pointer
125 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
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
133 * The exact semantics of when this is called are controlled
134 * by ->downconvert_worker()
136 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
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.
143 * For most locks, the default checks that there are no
144 * incompatible holders are sufficient.
146 * Called with the lockres spinlock held.
148 int (*check_downconvert)(struct ocfs2_lock_res *, int);
151 * Allows a lock type to populate the lock value block. This
152 * is called on downconvert, and when we drop a lock.
154 * Locks that want to use this should set LOCK_TYPE_USES_LVB
155 * in the flags field.
157 * Called with the lockres spinlock held.
159 void (*set_lvb)(struct ocfs2_lock_res *);
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).
167 * This should return any one of the ocfs2_unblock_action
168 * values, depending on what it wants the thread to do.
170 int (*downconvert_worker)(struct ocfs2_lock_res *, int);
173 * LOCK_TYPE_* flags which describe the specific requirements
174 * of a lock type. Descriptions of each individual flag follow.
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.
187 #define LOCK_TYPE_REQUIRES_REFRESH 0x1
190 * Indicate that a lock type makes use of the lock value block. The
191 * ->set_lvb lock type callback must be defined.
193 #define LOCK_TYPE_USES_LVB 0x2
195 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
196 .get_osb = ocfs2_get_inode_osb,
200 static struct ocfs2_lock_res_ops ocfs2_inode_meta_lops = {
201 .get_osb = ocfs2_get_inode_osb,
202 .check_downconvert = ocfs2_check_meta_downconvert,
203 .set_lvb = ocfs2_set_meta_lvb,
204 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
207 static struct ocfs2_lock_res_ops ocfs2_inode_data_lops = {
208 .get_osb = ocfs2_get_inode_osb,
209 .downconvert_worker = ocfs2_data_convert_worker,
213 static struct ocfs2_lock_res_ops ocfs2_super_lops = {
214 .flags = LOCK_TYPE_REQUIRES_REFRESH,
217 static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
221 static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
222 .get_osb = ocfs2_get_dentry_osb,
223 .post_unlock = ocfs2_dentry_post_unlock,
224 .downconvert_worker = ocfs2_dentry_convert_worker,
228 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
230 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
231 lockres->l_type == OCFS2_LOCK_TYPE_DATA ||
232 lockres->l_type == OCFS2_LOCK_TYPE_RW;
235 static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
237 BUG_ON(!ocfs2_is_inode_lock(lockres));
239 return (struct inode *) lockres->l_priv;
242 static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
244 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
246 return (struct ocfs2_dentry_lock *)lockres->l_priv;
249 static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
251 if (lockres->l_ops->get_osb)
252 return lockres->l_ops->get_osb(lockres);
254 return (struct ocfs2_super *)lockres->l_priv;
257 static int ocfs2_lock_create(struct ocfs2_super *osb,
258 struct ocfs2_lock_res *lockres,
261 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
263 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
264 struct ocfs2_lock_res *lockres,
266 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
267 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
268 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
269 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
270 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
271 struct ocfs2_lock_res *lockres);
272 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
274 #define ocfs2_log_dlm_error(_func, _stat, _lockres) do { \
275 mlog(ML_ERROR, "Dlm error \"%s\" while calling %s on " \
276 "resource %s: %s\n", dlm_errname(_stat), _func, \
277 _lockres->l_name, dlm_errmsg(_stat)); \
279 static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
280 struct ocfs2_lock_res *lockres);
281 static int ocfs2_meta_lock_update(struct inode *inode,
282 struct buffer_head **bh);
283 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
284 static inline int ocfs2_highest_compat_lock_level(int level);
286 static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
295 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
297 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
298 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
299 (long long)blkno, generation);
301 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
303 mlog(0, "built lock resource with name: %s\n", name);
308 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
310 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
311 struct ocfs2_dlm_debug *dlm_debug)
313 mlog(0, "Add tracking for lockres %s\n", res->l_name);
315 spin_lock(&ocfs2_dlm_tracking_lock);
316 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
317 spin_unlock(&ocfs2_dlm_tracking_lock);
320 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
322 spin_lock(&ocfs2_dlm_tracking_lock);
323 if (!list_empty(&res->l_debug_list))
324 list_del_init(&res->l_debug_list);
325 spin_unlock(&ocfs2_dlm_tracking_lock);
328 static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
329 struct ocfs2_lock_res *res,
330 enum ocfs2_lock_type type,
331 struct ocfs2_lock_res_ops *ops,
338 res->l_level = LKM_IVMODE;
339 res->l_requested = LKM_IVMODE;
340 res->l_blocking = LKM_IVMODE;
341 res->l_action = OCFS2_AST_INVALID;
342 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
344 res->l_flags = OCFS2_LOCK_INITIALIZED;
346 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
349 void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
351 /* This also clears out the lock status block */
352 memset(res, 0, sizeof(struct ocfs2_lock_res));
353 spin_lock_init(&res->l_lock);
354 init_waitqueue_head(&res->l_event);
355 INIT_LIST_HEAD(&res->l_blocked_list);
356 INIT_LIST_HEAD(&res->l_mask_waiters);
359 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
360 enum ocfs2_lock_type type,
361 unsigned int generation,
364 struct ocfs2_lock_res_ops *ops;
367 case OCFS2_LOCK_TYPE_RW:
368 ops = &ocfs2_inode_rw_lops;
370 case OCFS2_LOCK_TYPE_META:
371 ops = &ocfs2_inode_meta_lops;
373 case OCFS2_LOCK_TYPE_DATA:
374 ops = &ocfs2_inode_data_lops;
377 mlog_bug_on_msg(1, "type: %d\n", type);
378 ops = NULL; /* thanks, gcc */
382 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
383 generation, res->l_name);
384 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
387 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
389 struct inode *inode = ocfs2_lock_res_inode(lockres);
391 return OCFS2_SB(inode->i_sb);
394 static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
396 __be64 inode_blkno_be;
398 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
401 return be64_to_cpu(inode_blkno_be);
404 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
406 struct ocfs2_dentry_lock *dl = lockres->l_priv;
408 return OCFS2_SB(dl->dl_inode->i_sb);
411 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
412 u64 parent, struct inode *inode)
415 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
416 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
417 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
419 ocfs2_lock_res_init_once(lockres);
422 * Unfortunately, the standard lock naming scheme won't work
423 * here because we have two 16 byte values to use. Instead,
424 * we'll stuff the inode number as a binary value. We still
425 * want error prints to show something without garbling the
426 * display, so drop a null byte in there before the inode
427 * number. A future version of OCFS2 will likely use all
428 * binary lock names. The stringified names have been a
429 * tremendous aid in debugging, but now that the debugfs
430 * interface exists, we can mangle things there if need be.
432 * NOTE: We also drop the standard "pad" value (the total lock
433 * name size stays the same though - the last part is all
434 * zeros due to the memset in ocfs2_lock_res_init_once()
436 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
438 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
441 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
443 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
446 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
447 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
451 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
452 struct ocfs2_super *osb)
454 /* Superblock lockres doesn't come from a slab so we call init
455 * once on it manually. */
456 ocfs2_lock_res_init_once(res);
457 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
459 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
460 &ocfs2_super_lops, osb);
463 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
464 struct ocfs2_super *osb)
466 /* Rename lockres doesn't come from a slab so we call init
467 * once on it manually. */
468 ocfs2_lock_res_init_once(res);
469 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
470 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
471 &ocfs2_rename_lops, osb);
474 void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
478 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
481 ocfs2_remove_lockres_tracking(res);
483 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
484 "Lockres %s is on the blocked list\n",
486 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
487 "Lockres %s has mask waiters pending\n",
489 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
490 "Lockres %s is locked\n",
492 mlog_bug_on_msg(res->l_ro_holders,
493 "Lockres %s has %u ro holders\n",
494 res->l_name, res->l_ro_holders);
495 mlog_bug_on_msg(res->l_ex_holders,
496 "Lockres %s has %u ex holders\n",
497 res->l_name, res->l_ex_holders);
499 /* Need to clear out the lock status block for the dlm */
500 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
506 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
515 lockres->l_ex_holders++;
518 lockres->l_ro_holders++;
527 static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
536 BUG_ON(!lockres->l_ex_holders);
537 lockres->l_ex_holders--;
540 BUG_ON(!lockres->l_ro_holders);
541 lockres->l_ro_holders--;
549 /* WARNING: This function lives in a world where the only three lock
550 * levels are EX, PR, and NL. It *will* have to be adjusted when more
551 * lock types are added. */
552 static inline int ocfs2_highest_compat_lock_level(int level)
554 int new_level = LKM_EXMODE;
556 if (level == LKM_EXMODE)
557 new_level = LKM_NLMODE;
558 else if (level == LKM_PRMODE)
559 new_level = LKM_PRMODE;
563 static void lockres_set_flags(struct ocfs2_lock_res *lockres,
564 unsigned long newflags)
566 struct list_head *pos, *tmp;
567 struct ocfs2_mask_waiter *mw;
569 assert_spin_locked(&lockres->l_lock);
571 lockres->l_flags = newflags;
573 list_for_each_safe(pos, tmp, &lockres->l_mask_waiters) {
574 mw = list_entry(pos, struct ocfs2_mask_waiter, mw_item);
575 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
578 list_del_init(&mw->mw_item);
580 complete(&mw->mw_complete);
583 static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
585 lockres_set_flags(lockres, lockres->l_flags | or);
587 static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
590 lockres_set_flags(lockres, lockres->l_flags & ~clear);
593 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
597 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
598 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
599 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
600 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
602 lockres->l_level = lockres->l_requested;
603 if (lockres->l_level <=
604 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
605 lockres->l_blocking = LKM_NLMODE;
606 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
608 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
613 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
617 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
618 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
620 /* Convert from RO to EX doesn't really need anything as our
621 * information is already up to data. Convert from NL to
622 * *anything* however should mark ourselves as needing an
624 if (lockres->l_level == LKM_NLMODE &&
625 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
626 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
628 lockres->l_level = lockres->l_requested;
629 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
634 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
638 BUG_ON((!lockres->l_flags & OCFS2_LOCK_BUSY));
639 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
641 if (lockres->l_requested > LKM_NLMODE &&
642 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
643 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
644 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
646 lockres->l_level = lockres->l_requested;
647 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
648 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
653 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
656 int needs_downconvert = 0;
659 assert_spin_locked(&lockres->l_lock);
661 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
663 if (level > lockres->l_blocking) {
664 /* only schedule a downconvert if we haven't already scheduled
665 * one that goes low enough to satisfy the level we're
666 * blocking. this also catches the case where we get
668 if (ocfs2_highest_compat_lock_level(level) <
669 ocfs2_highest_compat_lock_level(lockres->l_blocking))
670 needs_downconvert = 1;
672 lockres->l_blocking = level;
675 mlog_exit(needs_downconvert);
676 return needs_downconvert;
679 static void ocfs2_blocking_ast(void *opaque, int level)
681 struct ocfs2_lock_res *lockres = opaque;
682 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
683 int needs_downconvert;
686 BUG_ON(level <= LKM_NLMODE);
688 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
689 lockres->l_name, level, lockres->l_level,
690 ocfs2_lock_type_string(lockres->l_type));
692 spin_lock_irqsave(&lockres->l_lock, flags);
693 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
694 if (needs_downconvert)
695 ocfs2_schedule_blocked_lock(osb, lockres);
696 spin_unlock_irqrestore(&lockres->l_lock, flags);
698 wake_up(&lockres->l_event);
700 ocfs2_kick_vote_thread(osb);
703 static void ocfs2_locking_ast(void *opaque)
705 struct ocfs2_lock_res *lockres = opaque;
706 struct dlm_lockstatus *lksb = &lockres->l_lksb;
709 spin_lock_irqsave(&lockres->l_lock, flags);
711 if (lksb->status != DLM_NORMAL) {
712 mlog(ML_ERROR, "lockres %s: lksb status value of %u!\n",
713 lockres->l_name, lksb->status);
714 spin_unlock_irqrestore(&lockres->l_lock, flags);
718 switch(lockres->l_action) {
719 case OCFS2_AST_ATTACH:
720 ocfs2_generic_handle_attach_action(lockres);
721 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
723 case OCFS2_AST_CONVERT:
724 ocfs2_generic_handle_convert_action(lockres);
726 case OCFS2_AST_DOWNCONVERT:
727 ocfs2_generic_handle_downconvert_action(lockres);
730 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
731 "lockres flags = 0x%lx, unlock action: %u\n",
732 lockres->l_name, lockres->l_action, lockres->l_flags,
733 lockres->l_unlock_action);
737 /* set it to something invalid so if we get called again we
739 lockres->l_action = OCFS2_AST_INVALID;
741 wake_up(&lockres->l_event);
742 spin_unlock_irqrestore(&lockres->l_lock, flags);
745 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
751 spin_lock_irqsave(&lockres->l_lock, flags);
752 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
754 lockres->l_action = OCFS2_AST_INVALID;
756 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
757 spin_unlock_irqrestore(&lockres->l_lock, flags);
759 wake_up(&lockres->l_event);
763 /* Note: If we detect another process working on the lock (i.e.,
764 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
765 * to do the right thing in that case.
767 static int ocfs2_lock_create(struct ocfs2_super *osb,
768 struct ocfs2_lock_res *lockres,
773 enum dlm_status status = DLM_NORMAL;
778 mlog(0, "lock %s, level = %d, flags = %d\n", lockres->l_name, level,
781 spin_lock_irqsave(&lockres->l_lock, flags);
782 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
783 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
784 spin_unlock_irqrestore(&lockres->l_lock, flags);
788 lockres->l_action = OCFS2_AST_ATTACH;
789 lockres->l_requested = level;
790 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
791 spin_unlock_irqrestore(&lockres->l_lock, flags);
793 status = dlmlock(osb->dlm,
798 OCFS2_LOCK_ID_MAX_LEN - 1,
802 if (status != DLM_NORMAL) {
803 ocfs2_log_dlm_error("dlmlock", status, lockres);
805 ocfs2_recover_from_dlm_error(lockres, 1);
808 mlog(0, "lock %s, successfull return from dlmlock\n", lockres->l_name);
815 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
821 spin_lock_irqsave(&lockres->l_lock, flags);
822 ret = lockres->l_flags & flag;
823 spin_unlock_irqrestore(&lockres->l_lock, flags);
828 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
831 wait_event(lockres->l_event,
832 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
835 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
838 wait_event(lockres->l_event,
839 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
842 /* predict what lock level we'll be dropping down to on behalf
843 * of another node, and return true if the currently wanted
844 * level will be compatible with it. */
845 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
848 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
850 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
853 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
855 INIT_LIST_HEAD(&mw->mw_item);
856 init_completion(&mw->mw_complete);
859 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
861 wait_for_completion(&mw->mw_complete);
862 /* Re-arm the completion in case we want to wait on it again */
863 INIT_COMPLETION(mw->mw_complete);
864 return mw->mw_status;
867 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
868 struct ocfs2_mask_waiter *mw,
872 BUG_ON(!list_empty(&mw->mw_item));
874 assert_spin_locked(&lockres->l_lock);
876 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
881 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
882 * if the mask still hadn't reached its goal */
883 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
884 struct ocfs2_mask_waiter *mw)
889 spin_lock_irqsave(&lockres->l_lock, flags);
890 if (!list_empty(&mw->mw_item)) {
891 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
894 list_del_init(&mw->mw_item);
895 init_completion(&mw->mw_complete);
897 spin_unlock_irqrestore(&lockres->l_lock, flags);
903 static int ocfs2_cluster_lock(struct ocfs2_super *osb,
904 struct ocfs2_lock_res *lockres,
909 struct ocfs2_mask_waiter mw;
910 enum dlm_status status;
911 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
912 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
917 ocfs2_init_mask_waiter(&mw);
919 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
920 lkm_flags |= LKM_VALBLK;
925 if (catch_signals && signal_pending(current)) {
930 spin_lock_irqsave(&lockres->l_lock, flags);
932 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
933 "Cluster lock called on freeing lockres %s! flags "
934 "0x%lx\n", lockres->l_name, lockres->l_flags);
936 /* We only compare against the currently granted level
937 * here. If the lock is blocked waiting on a downconvert,
938 * we'll get caught below. */
939 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
940 level > lockres->l_level) {
941 /* is someone sitting in dlm_lock? If so, wait on
943 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
948 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
949 /* lock has not been created yet. */
950 spin_unlock_irqrestore(&lockres->l_lock, flags);
952 ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
960 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
961 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
962 /* is the lock is currently blocked on behalf of
964 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
969 if (level > lockres->l_level) {
970 if (lockres->l_action != OCFS2_AST_INVALID)
971 mlog(ML_ERROR, "lockres %s has action %u pending\n",
972 lockres->l_name, lockres->l_action);
974 lockres->l_action = OCFS2_AST_CONVERT;
975 lockres->l_requested = level;
976 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
977 spin_unlock_irqrestore(&lockres->l_lock, flags);
979 BUG_ON(level == LKM_IVMODE);
980 BUG_ON(level == LKM_NLMODE);
982 mlog(0, "lock %s, convert from %d to level = %d\n",
983 lockres->l_name, lockres->l_level, level);
985 /* call dlm_lock to upgrade lock now */
986 status = dlmlock(osb->dlm,
989 lkm_flags|LKM_CONVERT,
991 OCFS2_LOCK_ID_MAX_LEN - 1,
995 if (status != DLM_NORMAL) {
996 if ((lkm_flags & LKM_NOQUEUE) &&
997 (status == DLM_NOTQUEUED))
1000 ocfs2_log_dlm_error("dlmlock", status,
1004 ocfs2_recover_from_dlm_error(lockres, 1);
1008 mlog(0, "lock %s, successfull return from dlmlock\n",
1011 /* At this point we've gone inside the dlm and need to
1012 * complete our work regardless. */
1015 /* wait for busy to clear and carry on */
1019 /* Ok, if we get here then we're good to go. */
1020 ocfs2_inc_holders(lockres, level);
1024 spin_unlock_irqrestore(&lockres->l_lock, flags);
1027 * This is helping work around a lock inversion between the page lock
1028 * and dlm locks. One path holds the page lock while calling aops
1029 * which block acquiring dlm locks. The voting thread holds dlm
1030 * locks while acquiring page locks while down converting data locks.
1031 * This block is helping an aop path notice the inversion and back
1032 * off to unlock its page lock before trying the dlm lock again.
1034 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1035 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1037 if (lockres_remove_mask_waiter(lockres, &mw))
1043 ret = ocfs2_wait_for_mask(&mw);
1053 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1054 struct ocfs2_lock_res *lockres,
1057 unsigned long flags;
1060 spin_lock_irqsave(&lockres->l_lock, flags);
1061 ocfs2_dec_holders(lockres, level);
1062 ocfs2_vote_on_unlock(osb, lockres);
1063 spin_unlock_irqrestore(&lockres->l_lock, flags);
1067 static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1068 struct ocfs2_lock_res *lockres,
1072 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1073 unsigned long flags;
1074 int lkm_flags = local ? LKM_LOCAL : 0;
1076 spin_lock_irqsave(&lockres->l_lock, flags);
1077 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1078 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1079 spin_unlock_irqrestore(&lockres->l_lock, flags);
1081 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
1084 /* Grants us an EX lock on the data and metadata resources, skipping
1085 * the normal cluster directory lookup. Use this ONLY on newly created
1086 * inodes which other nodes can't possibly see, and which haven't been
1087 * hashed in the inode hash yet. This can give us a good performance
1088 * increase as it'll skip the network broadcast normally associated
1089 * with creating a new lock resource. */
1090 int ocfs2_create_new_inode_locks(struct inode *inode)
1093 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1096 BUG_ON(!ocfs2_inode_is_new(inode));
1100 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1102 /* NOTE: That we don't increment any of the holder counts, nor
1103 * do we add anything to a journal handle. Since this is
1104 * supposed to be a new inode which the cluster doesn't know
1105 * about yet, there is no need to. As far as the LVB handling
1106 * is concerned, this is basically like acquiring an EX lock
1107 * on a resource which has an invalid one -- we'll set it
1108 * valid when we release the EX. */
1110 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
1117 * We don't want to use LKM_LOCAL on a meta data lock as they
1118 * don't use a generation in their lock names.
1120 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_meta_lockres, 1, 0);
1126 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_data_lockres, 1, 1);
1137 int ocfs2_rw_lock(struct inode *inode, int write)
1140 struct ocfs2_lock_res *lockres;
1141 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1147 mlog(0, "inode %llu take %s RW lock\n",
1148 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1149 write ? "EXMODE" : "PRMODE");
1151 if (ocfs2_mount_local(osb))
1154 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1156 level = write ? LKM_EXMODE : LKM_PRMODE;
1158 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1167 void ocfs2_rw_unlock(struct inode *inode, int write)
1169 int level = write ? LKM_EXMODE : LKM_PRMODE;
1170 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1171 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1175 mlog(0, "inode %llu drop %s RW lock\n",
1176 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1177 write ? "EXMODE" : "PRMODE");
1179 if (!ocfs2_mount_local(osb))
1180 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1185 int ocfs2_data_lock_full(struct inode *inode,
1189 int status = 0, level;
1190 struct ocfs2_lock_res *lockres;
1191 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1197 mlog(0, "inode %llu take %s DATA lock\n",
1198 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1199 write ? "EXMODE" : "PRMODE");
1201 /* We'll allow faking a readonly data lock for
1203 if (ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb))) {
1211 if (ocfs2_mount_local(osb))
1214 lockres = &OCFS2_I(inode)->ip_data_lockres;
1216 level = write ? LKM_EXMODE : LKM_PRMODE;
1218 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level,
1220 if (status < 0 && status != -EAGAIN)
1228 /* see ocfs2_meta_lock_with_page() */
1229 int ocfs2_data_lock_with_page(struct inode *inode,
1235 ret = ocfs2_data_lock_full(inode, write, OCFS2_LOCK_NONBLOCK);
1236 if (ret == -EAGAIN) {
1238 if (ocfs2_data_lock(inode, write) == 0)
1239 ocfs2_data_unlock(inode, write);
1240 ret = AOP_TRUNCATED_PAGE;
1246 static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
1247 struct ocfs2_lock_res *lockres)
1253 /* If we know that another node is waiting on our lock, kick
1254 * the vote thread * pre-emptively when we reach a release
1256 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1257 switch(lockres->l_blocking) {
1259 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1263 if (!lockres->l_ex_holders)
1272 ocfs2_kick_vote_thread(osb);
1277 void ocfs2_data_unlock(struct inode *inode,
1280 int level = write ? LKM_EXMODE : LKM_PRMODE;
1281 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_data_lockres;
1282 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1286 mlog(0, "inode %llu drop %s DATA lock\n",
1287 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1288 write ? "EXMODE" : "PRMODE");
1290 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
1291 !ocfs2_mount_local(osb))
1292 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1297 #define OCFS2_SEC_BITS 34
1298 #define OCFS2_SEC_SHIFT (64 - 34)
1299 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1301 /* LVB only has room for 64 bits of time here so we pack it for
1303 static u64 ocfs2_pack_timespec(struct timespec *spec)
1306 u64 sec = spec->tv_sec;
1307 u32 nsec = spec->tv_nsec;
1309 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1314 /* Call this with the lockres locked. I am reasonably sure we don't
1315 * need ip_lock in this function as anyone who would be changing those
1316 * values is supposed to be blocked in ocfs2_meta_lock right now. */
1317 static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1319 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1320 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1321 struct ocfs2_meta_lvb *lvb;
1325 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1328 * Invalidate the LVB of a deleted inode - this way other
1329 * nodes are forced to go to disk and discover the new inode
1332 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1333 lvb->lvb_version = 0;
1337 lvb->lvb_version = OCFS2_LVB_VERSION;
1338 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1339 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1340 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1341 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1342 lvb->lvb_itag = cpu_to_be16(inode->i_tag);
1343 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1344 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1345 lvb->lvb_iatime_packed =
1346 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1347 lvb->lvb_ictime_packed =
1348 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1349 lvb->lvb_imtime_packed =
1350 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1351 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
1352 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
1355 mlog_meta_lvb(0, lockres);
1360 static void ocfs2_unpack_timespec(struct timespec *spec,
1363 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1364 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1367 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1369 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1370 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1371 struct ocfs2_meta_lvb *lvb;
1375 mlog_meta_lvb(0, lockres);
1377 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1379 /* We're safe here without the lockres lock... */
1380 spin_lock(&oi->ip_lock);
1381 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1382 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1384 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1385 ocfs2_set_inode_flags(inode);
1387 /* fast-symlinks are a special case */
1388 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1389 inode->i_blocks = 0;
1392 ocfs2_align_bytes_to_sectors(i_size_read(inode));
1394 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1395 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1396 inode->i_tag = be16_to_cpu(lvb->lvb_itag);
1397 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1398 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1399 ocfs2_unpack_timespec(&inode->i_atime,
1400 be64_to_cpu(lvb->lvb_iatime_packed));
1401 ocfs2_unpack_timespec(&inode->i_mtime,
1402 be64_to_cpu(lvb->lvb_imtime_packed));
1403 ocfs2_unpack_timespec(&inode->i_ctime,
1404 be64_to_cpu(lvb->lvb_ictime_packed));
1405 spin_unlock(&oi->ip_lock);
1410 static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1411 struct ocfs2_lock_res *lockres)
1413 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1415 if (lvb->lvb_version == OCFS2_LVB_VERSION
1416 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
1421 /* Determine whether a lock resource needs to be refreshed, and
1422 * arbitrate who gets to refresh it.
1424 * 0 means no refresh needed.
1426 * > 0 means you need to refresh this and you MUST call
1427 * ocfs2_complete_lock_res_refresh afterwards. */
1428 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1430 unsigned long flags;
1436 spin_lock_irqsave(&lockres->l_lock, flags);
1437 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1438 spin_unlock_irqrestore(&lockres->l_lock, flags);
1442 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1443 spin_unlock_irqrestore(&lockres->l_lock, flags);
1445 ocfs2_wait_on_refreshing_lock(lockres);
1449 /* Ok, I'll be the one to refresh this lock. */
1450 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1451 spin_unlock_irqrestore(&lockres->l_lock, flags);
1459 /* If status is non zero, I'll mark it as not being in refresh
1460 * anymroe, but i won't clear the needs refresh flag. */
1461 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1464 unsigned long flags;
1467 spin_lock_irqsave(&lockres->l_lock, flags);
1468 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1470 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1471 spin_unlock_irqrestore(&lockres->l_lock, flags);
1473 wake_up(&lockres->l_event);
1478 /* may or may not return a bh if it went to disk. */
1479 static int ocfs2_meta_lock_update(struct inode *inode,
1480 struct buffer_head **bh)
1483 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1484 struct ocfs2_lock_res *lockres = NULL;
1485 struct ocfs2_dinode *fe;
1486 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1490 spin_lock(&oi->ip_lock);
1491 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1492 mlog(0, "Orphaned inode %llu was deleted while we "
1493 "were waiting on a lock. ip_flags = 0x%x\n",
1494 (unsigned long long)oi->ip_blkno, oi->ip_flags);
1495 spin_unlock(&oi->ip_lock);
1499 spin_unlock(&oi->ip_lock);
1501 if (!ocfs2_mount_local(osb)) {
1502 lockres = &oi->ip_meta_lockres;
1504 if (!ocfs2_should_refresh_lock_res(lockres))
1508 /* This will discard any caching information we might have had
1509 * for the inode metadata. */
1510 ocfs2_metadata_cache_purge(inode);
1512 /* will do nothing for inode types that don't use the extent
1513 * map (directories, bitmap files, etc) */
1514 ocfs2_extent_map_trunc(inode, 0);
1516 if (lockres && ocfs2_meta_lvb_is_trustable(inode, lockres)) {
1517 mlog(0, "Trusting LVB on inode %llu\n",
1518 (unsigned long long)oi->ip_blkno);
1519 ocfs2_refresh_inode_from_lvb(inode);
1521 /* Boo, we have to go to disk. */
1522 /* read bh, cast, ocfs2_refresh_inode */
1523 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1524 bh, OCFS2_BH_CACHED, inode);
1529 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1531 /* This is a good chance to make sure we're not
1532 * locking an invalid object.
1534 * We bug on a stale inode here because we checked
1535 * above whether it was wiped from disk. The wiping
1536 * node provides a guarantee that we receive that
1537 * message and can mark the inode before dropping any
1538 * locks associated with it. */
1539 if (!OCFS2_IS_VALID_DINODE(fe)) {
1540 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1544 mlog_bug_on_msg(inode->i_generation !=
1545 le32_to_cpu(fe->i_generation),
1546 "Invalid dinode %llu disk generation: %u "
1547 "inode->i_generation: %u\n",
1548 (unsigned long long)oi->ip_blkno,
1549 le32_to_cpu(fe->i_generation),
1550 inode->i_generation);
1551 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1552 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
1553 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1554 (unsigned long long)oi->ip_blkno,
1555 (unsigned long long)le64_to_cpu(fe->i_dtime),
1556 le32_to_cpu(fe->i_flags));
1558 ocfs2_refresh_inode(inode, fe);
1564 ocfs2_complete_lock_res_refresh(lockres, status);
1570 static int ocfs2_assign_bh(struct inode *inode,
1571 struct buffer_head **ret_bh,
1572 struct buffer_head *passed_bh)
1577 /* Ok, the update went to disk for us, use the
1579 *ret_bh = passed_bh;
1585 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1586 OCFS2_I(inode)->ip_blkno,
1597 * returns < 0 error if the callback will never be called, otherwise
1598 * the result of the lock will be communicated via the callback.
1600 int ocfs2_meta_lock_full(struct inode *inode,
1601 struct buffer_head **ret_bh,
1605 int status, level, dlm_flags, acquired;
1606 struct ocfs2_lock_res *lockres = NULL;
1607 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1608 struct buffer_head *local_bh = NULL;
1614 mlog(0, "inode %llu, take %s META lock\n",
1615 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1616 ex ? "EXMODE" : "PRMODE");
1620 /* We'll allow faking a readonly metadata lock for
1622 if (ocfs2_is_hard_readonly(osb)) {
1628 if (ocfs2_mount_local(osb))
1631 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1632 wait_event(osb->recovery_event,
1633 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1636 lockres = &OCFS2_I(inode)->ip_meta_lockres;
1637 level = ex ? LKM_EXMODE : LKM_PRMODE;
1639 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
1640 dlm_flags |= LKM_NOQUEUE;
1642 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1644 if (status != -EAGAIN && status != -EIOCBRETRY)
1649 /* Notify the error cleanup path to drop the cluster lock. */
1652 /* We wait twice because a node may have died while we were in
1653 * the lower dlm layers. The second time though, we've
1654 * committed to owning this lock so we don't allow signals to
1655 * abort the operation. */
1656 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1657 wait_event(osb->recovery_event,
1658 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1662 * We only see this flag if we're being called from
1663 * ocfs2_read_locked_inode(). It means we're locking an inode
1664 * which hasn't been populated yet, so clear the refresh flag
1665 * and let the caller handle it.
1667 if (inode->i_state & I_NEW) {
1670 ocfs2_complete_lock_res_refresh(lockres, 0);
1674 /* This is fun. The caller may want a bh back, or it may
1675 * not. ocfs2_meta_lock_update definitely wants one in, but
1676 * may or may not read one, depending on what's in the
1677 * LVB. The result of all of this is that we've *only* gone to
1678 * disk if we have to, so the complexity is worthwhile. */
1679 status = ocfs2_meta_lock_update(inode, &local_bh);
1681 if (status != -ENOENT)
1687 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
1696 if (ret_bh && (*ret_bh)) {
1701 ocfs2_meta_unlock(inode, ex);
1712 * This is working around a lock inversion between tasks acquiring DLM locks
1713 * while holding a page lock and the vote thread which blocks dlm lock acquiry
1714 * while acquiring page locks.
1716 * ** These _with_page variantes are only intended to be called from aop
1717 * methods that hold page locks and return a very specific *positive* error
1718 * code that aop methods pass up to the VFS -- test for errors with != 0. **
1720 * The DLM is called such that it returns -EAGAIN if it would have blocked
1721 * waiting for the vote thread. In that case we unlock our page so the vote
1722 * thread can make progress. Once we've done this we have to return
1723 * AOP_TRUNCATED_PAGE so the aop method that called us can bubble that back up
1724 * into the VFS who will then immediately retry the aop call.
1726 * We do a blocking lock and immediate unlock before returning, though, so that
1727 * the lock has a great chance of being cached on this node by the time the VFS
1728 * calls back to retry the aop. This has a potential to livelock as nodes
1729 * ping locks back and forth, but that's a risk we're willing to take to avoid
1730 * the lock inversion simply.
1732 int ocfs2_meta_lock_with_page(struct inode *inode,
1733 struct buffer_head **ret_bh,
1739 ret = ocfs2_meta_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
1740 if (ret == -EAGAIN) {
1742 if (ocfs2_meta_lock(inode, ret_bh, ex) == 0)
1743 ocfs2_meta_unlock(inode, ex);
1744 ret = AOP_TRUNCATED_PAGE;
1750 int ocfs2_meta_lock_atime(struct inode *inode,
1751 struct vfsmount *vfsmnt,
1757 ret = ocfs2_meta_lock(inode, NULL, 0);
1764 * If we should update atime, we will get EX lock,
1765 * otherwise we just get PR lock.
1767 if (ocfs2_should_update_atime(inode, vfsmnt)) {
1768 struct buffer_head *bh = NULL;
1770 ocfs2_meta_unlock(inode, 0);
1771 ret = ocfs2_meta_lock(inode, &bh, 1);
1777 if (ocfs2_should_update_atime(inode, vfsmnt))
1778 ocfs2_update_inode_atime(inode, bh);
1788 void ocfs2_meta_unlock(struct inode *inode,
1791 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1792 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
1793 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1797 mlog(0, "inode %llu drop %s META lock\n",
1798 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1799 ex ? "EXMODE" : "PRMODE");
1801 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
1802 !ocfs2_mount_local(osb))
1803 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1808 int ocfs2_super_lock(struct ocfs2_super *osb,
1812 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1813 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1814 struct buffer_head *bh;
1815 struct ocfs2_slot_info *si = osb->slot_info;
1819 if (ocfs2_is_hard_readonly(osb))
1822 if (ocfs2_mount_local(osb))
1825 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
1831 /* The super block lock path is really in the best position to
1832 * know when resources covered by the lock need to be
1833 * refreshed, so we do it here. Of course, making sense of
1834 * everything is up to the caller :) */
1835 status = ocfs2_should_refresh_lock_res(lockres);
1842 status = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0,
1845 ocfs2_update_slot_info(si);
1847 ocfs2_complete_lock_res_refresh(lockres, status);
1857 void ocfs2_super_unlock(struct ocfs2_super *osb,
1860 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1861 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1863 if (!ocfs2_mount_local(osb))
1864 ocfs2_cluster_unlock(osb, lockres, level);
1867 int ocfs2_rename_lock(struct ocfs2_super *osb)
1870 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1872 if (ocfs2_is_hard_readonly(osb))
1875 if (ocfs2_mount_local(osb))
1878 status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
1885 void ocfs2_rename_unlock(struct ocfs2_super *osb)
1887 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1889 if (!ocfs2_mount_local(osb))
1890 ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
1893 int ocfs2_dentry_lock(struct dentry *dentry, int ex)
1896 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1897 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1898 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1902 if (ocfs2_is_hard_readonly(osb))
1905 if (ocfs2_mount_local(osb))
1908 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
1915 void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
1917 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1918 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1919 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1921 if (!ocfs2_mount_local(osb))
1922 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
1925 /* Reference counting of the dlm debug structure. We want this because
1926 * open references on the debug inodes can live on after a mount, so
1927 * we can't rely on the ocfs2_super to always exist. */
1928 static void ocfs2_dlm_debug_free(struct kref *kref)
1930 struct ocfs2_dlm_debug *dlm_debug;
1932 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
1937 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
1940 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
1943 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
1945 kref_get(&debug->d_refcnt);
1948 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
1950 struct ocfs2_dlm_debug *dlm_debug;
1952 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
1954 mlog_errno(-ENOMEM);
1958 kref_init(&dlm_debug->d_refcnt);
1959 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
1960 dlm_debug->d_locking_state = NULL;
1965 /* Access to this is arbitrated for us via seq_file->sem. */
1966 struct ocfs2_dlm_seq_priv {
1967 struct ocfs2_dlm_debug *p_dlm_debug;
1968 struct ocfs2_lock_res p_iter_res;
1969 struct ocfs2_lock_res p_tmp_res;
1972 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
1973 struct ocfs2_dlm_seq_priv *priv)
1975 struct ocfs2_lock_res *iter, *ret = NULL;
1976 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
1978 assert_spin_locked(&ocfs2_dlm_tracking_lock);
1980 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
1981 /* discover the head of the list */
1982 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
1983 mlog(0, "End of list found, %p\n", ret);
1987 /* We track our "dummy" iteration lockres' by a NULL
1989 if (iter->l_ops != NULL) {
1998 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2000 struct ocfs2_dlm_seq_priv *priv = m->private;
2001 struct ocfs2_lock_res *iter;
2003 spin_lock(&ocfs2_dlm_tracking_lock);
2004 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2006 /* Since lockres' have the lifetime of their container
2007 * (which can be inodes, ocfs2_supers, etc) we want to
2008 * copy this out to a temporary lockres while still
2009 * under the spinlock. Obviously after this we can't
2010 * trust any pointers on the copy returned, but that's
2011 * ok as the information we want isn't typically held
2013 priv->p_tmp_res = *iter;
2014 iter = &priv->p_tmp_res;
2016 spin_unlock(&ocfs2_dlm_tracking_lock);
2021 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2025 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2027 struct ocfs2_dlm_seq_priv *priv = m->private;
2028 struct ocfs2_lock_res *iter = v;
2029 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2031 spin_lock(&ocfs2_dlm_tracking_lock);
2032 iter = ocfs2_dlm_next_res(iter, priv);
2033 list_del_init(&dummy->l_debug_list);
2035 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2036 priv->p_tmp_res = *iter;
2037 iter = &priv->p_tmp_res;
2039 spin_unlock(&ocfs2_dlm_tracking_lock);
2044 /* So that debugfs.ocfs2 can determine which format is being used */
2045 #define OCFS2_DLM_DEBUG_STR_VERSION 1
2046 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2050 struct ocfs2_lock_res *lockres = v;
2055 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2057 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2058 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2060 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2062 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2064 seq_printf(m, "%d\t"
2075 lockres->l_unlock_action,
2076 lockres->l_ro_holders,
2077 lockres->l_ex_holders,
2078 lockres->l_requested,
2079 lockres->l_blocking);
2081 /* Dump the raw LVB */
2082 lvb = lockres->l_lksb.lvb;
2083 for(i = 0; i < DLM_LVB_LEN; i++)
2084 seq_printf(m, "0x%x\t", lvb[i]);
2087 seq_printf(m, "\n");
2091 static struct seq_operations ocfs2_dlm_seq_ops = {
2092 .start = ocfs2_dlm_seq_start,
2093 .stop = ocfs2_dlm_seq_stop,
2094 .next = ocfs2_dlm_seq_next,
2095 .show = ocfs2_dlm_seq_show,
2098 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2100 struct seq_file *seq = (struct seq_file *) file->private_data;
2101 struct ocfs2_dlm_seq_priv *priv = seq->private;
2102 struct ocfs2_lock_res *res = &priv->p_iter_res;
2104 ocfs2_remove_lockres_tracking(res);
2105 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2106 return seq_release_private(inode, file);
2109 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2112 struct ocfs2_dlm_seq_priv *priv;
2113 struct seq_file *seq;
2114 struct ocfs2_super *osb;
2116 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2122 osb = inode->i_private;
2123 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2124 priv->p_dlm_debug = osb->osb_dlm_debug;
2125 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2127 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2134 seq = (struct seq_file *) file->private_data;
2135 seq->private = priv;
2137 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2144 static const struct file_operations ocfs2_dlm_debug_fops = {
2145 .open = ocfs2_dlm_debug_open,
2146 .release = ocfs2_dlm_debug_release,
2148 .llseek = seq_lseek,
2151 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2154 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2156 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2158 osb->osb_debug_root,
2160 &ocfs2_dlm_debug_fops);
2161 if (!dlm_debug->d_locking_state) {
2164 "Unable to create locking state debugfs file.\n");
2168 ocfs2_get_dlm_debug(dlm_debug);
2173 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2175 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2178 debugfs_remove(dlm_debug->d_locking_state);
2179 ocfs2_put_dlm_debug(dlm_debug);
2183 int ocfs2_dlm_init(struct ocfs2_super *osb)
2187 struct dlm_ctxt *dlm = NULL;
2191 if (ocfs2_mount_local(osb))
2194 status = ocfs2_dlm_init_debug(osb);
2200 /* launch vote thread */
2201 osb->vote_task = kthread_run(ocfs2_vote_thread, osb, "ocfs2vote");
2202 if (IS_ERR(osb->vote_task)) {
2203 status = PTR_ERR(osb->vote_task);
2204 osb->vote_task = NULL;
2209 /* used by the dlm code to make message headers unique, each
2210 * node in this domain must agree on this. */
2211 dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
2213 /* for now, uuid == domain */
2214 dlm = dlm_register_domain(osb->uuid_str, dlm_key);
2216 status = PTR_ERR(dlm);
2221 dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
2224 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2225 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2232 ocfs2_dlm_shutdown_debug(osb);
2234 kthread_stop(osb->vote_task);
2241 void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2245 dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
2247 ocfs2_drop_osb_locks(osb);
2249 if (osb->vote_task) {
2250 kthread_stop(osb->vote_task);
2251 osb->vote_task = NULL;
2254 ocfs2_lock_res_free(&osb->osb_super_lockres);
2255 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2257 dlm_unregister_domain(osb->dlm);
2260 ocfs2_dlm_shutdown_debug(osb);
2265 static void ocfs2_unlock_ast(void *opaque, enum dlm_status status)
2267 struct ocfs2_lock_res *lockres = opaque;
2268 unsigned long flags;
2272 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2273 lockres->l_unlock_action);
2275 spin_lock_irqsave(&lockres->l_lock, flags);
2276 /* We tried to cancel a convert request, but it was already
2277 * granted. All we want to do here is clear our unlock
2278 * state. The wake_up call done at the bottom is redundant
2279 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2280 * hurt anything anyway */
2281 if (status == DLM_CANCELGRANT &&
2282 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2283 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2285 /* We don't clear the busy flag in this case as it
2286 * should have been cleared by the ast which the dlm
2288 goto complete_unlock;
2291 if (status != DLM_NORMAL) {
2292 mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
2293 "unlock_action %d\n", status, lockres->l_name,
2294 lockres->l_unlock_action);
2295 spin_unlock_irqrestore(&lockres->l_lock, flags);
2299 switch(lockres->l_unlock_action) {
2300 case OCFS2_UNLOCK_CANCEL_CONVERT:
2301 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2302 lockres->l_action = OCFS2_AST_INVALID;
2304 case OCFS2_UNLOCK_DROP_LOCK:
2305 lockres->l_level = LKM_IVMODE;
2311 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2313 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2314 spin_unlock_irqrestore(&lockres->l_lock, flags);
2316 wake_up(&lockres->l_event);
2321 static int ocfs2_drop_lock(struct ocfs2_super *osb,
2322 struct ocfs2_lock_res *lockres)
2324 enum dlm_status status;
2325 unsigned long flags;
2328 /* We didn't get anywhere near actually using this lockres. */
2329 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2332 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
2333 lkm_flags |= LKM_VALBLK;
2335 spin_lock_irqsave(&lockres->l_lock, flags);
2337 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2338 "lockres %s, flags 0x%lx\n",
2339 lockres->l_name, lockres->l_flags);
2341 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2342 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2343 "%u, unlock_action = %u\n",
2344 lockres->l_name, lockres->l_flags, lockres->l_action,
2345 lockres->l_unlock_action);
2347 spin_unlock_irqrestore(&lockres->l_lock, flags);
2349 /* XXX: Today we just wait on any busy
2350 * locks... Perhaps we need to cancel converts in the
2352 ocfs2_wait_on_busy_lock(lockres);
2354 spin_lock_irqsave(&lockres->l_lock, flags);
2357 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2358 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2359 lockres->l_level == LKM_EXMODE &&
2360 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2361 lockres->l_ops->set_lvb(lockres);
2364 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2365 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2367 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2368 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2370 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2371 spin_unlock_irqrestore(&lockres->l_lock, flags);
2375 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2377 /* make sure we never get here while waiting for an ast to
2379 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2381 /* is this necessary? */
2382 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2383 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2384 spin_unlock_irqrestore(&lockres->l_lock, flags);
2386 mlog(0, "lock %s\n", lockres->l_name);
2388 status = dlmunlock(osb->dlm, &lockres->l_lksb, lkm_flags,
2389 ocfs2_unlock_ast, lockres);
2390 if (status != DLM_NORMAL) {
2391 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2392 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2393 dlm_print_one_lock(lockres->l_lksb.lockid);
2396 mlog(0, "lock %s, successfull return from dlmunlock\n",
2399 ocfs2_wait_on_busy_lock(lockres);
2405 /* Mark the lockres as being dropped. It will no longer be
2406 * queued if blocking, but we still may have to wait on it
2407 * being dequeued from the vote thread before we can consider
2410 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2411 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2414 struct ocfs2_mask_waiter mw;
2415 unsigned long flags;
2417 ocfs2_init_mask_waiter(&mw);
2419 spin_lock_irqsave(&lockres->l_lock, flags);
2420 lockres->l_flags |= OCFS2_LOCK_FREEING;
2421 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2422 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2423 spin_unlock_irqrestore(&lockres->l_lock, flags);
2425 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2427 status = ocfs2_wait_for_mask(&mw);
2431 spin_lock_irqsave(&lockres->l_lock, flags);
2433 spin_unlock_irqrestore(&lockres->l_lock, flags);
2436 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2437 struct ocfs2_lock_res *lockres)
2441 ocfs2_mark_lockres_freeing(lockres);
2442 ret = ocfs2_drop_lock(osb, lockres);
2447 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2449 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2450 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
2453 int ocfs2_drop_inode_locks(struct inode *inode)
2459 /* No need to call ocfs2_mark_lockres_freeing here -
2460 * ocfs2_clear_inode has done it for us. */
2462 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2463 &OCFS2_I(inode)->ip_data_lockres);
2469 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2470 &OCFS2_I(inode)->ip_meta_lockres);
2473 if (err < 0 && !status)
2476 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2477 &OCFS2_I(inode)->ip_rw_lockres);
2480 if (err < 0 && !status)
2487 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2490 assert_spin_locked(&lockres->l_lock);
2492 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
2494 if (lockres->l_level <= new_level) {
2495 mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
2496 lockres->l_level, new_level);
2500 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2501 lockres->l_name, new_level, lockres->l_blocking);
2503 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2504 lockres->l_requested = new_level;
2505 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2508 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2509 struct ocfs2_lock_res *lockres,
2513 int ret, dlm_flags = LKM_CONVERT;
2514 enum dlm_status status;
2519 dlm_flags |= LKM_VALBLK;
2521 status = dlmlock(osb->dlm,
2526 OCFS2_LOCK_ID_MAX_LEN - 1,
2529 ocfs2_blocking_ast);
2530 if (status != DLM_NORMAL) {
2531 ocfs2_log_dlm_error("dlmlock", status, lockres);
2533 ocfs2_recover_from_dlm_error(lockres, 1);
2543 /* returns 1 when the caller should unlock and call dlmunlock */
2544 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2545 struct ocfs2_lock_res *lockres)
2547 assert_spin_locked(&lockres->l_lock);
2550 mlog(0, "lock %s\n", lockres->l_name);
2552 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2553 /* If we're already trying to cancel a lock conversion
2554 * then just drop the spinlock and allow the caller to
2555 * requeue this lock. */
2557 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2561 /* were we in a convert when we got the bast fire? */
2562 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2563 lockres->l_action != OCFS2_AST_DOWNCONVERT);
2564 /* set things up for the unlockast to know to just
2565 * clear out the ast_action and unset busy, etc. */
2566 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2568 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2569 "lock %s, invalid flags: 0x%lx\n",
2570 lockres->l_name, lockres->l_flags);
2575 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2576 struct ocfs2_lock_res *lockres)
2579 enum dlm_status status;
2582 mlog(0, "lock %s\n", lockres->l_name);
2585 status = dlmunlock(osb->dlm,
2590 if (status != DLM_NORMAL) {
2591 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2593 ocfs2_recover_from_dlm_error(lockres, 0);
2596 mlog(0, "lock %s return from dlmunlock\n", lockres->l_name);
2602 static int ocfs2_unblock_lock(struct ocfs2_super *osb,
2603 struct ocfs2_lock_res *lockres,
2604 struct ocfs2_unblock_ctl *ctl)
2606 unsigned long flags;
2614 spin_lock_irqsave(&lockres->l_lock, flags);
2616 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2619 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2621 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2622 spin_unlock_irqrestore(&lockres->l_lock, flags);
2624 ret = ocfs2_cancel_convert(osb, lockres);
2631 /* if we're blocking an exclusive and we have *any* holders,
2633 if ((lockres->l_blocking == LKM_EXMODE)
2634 && (lockres->l_ex_holders || lockres->l_ro_holders))
2637 /* If it's a PR we're blocking, then only
2638 * requeue if we've got any EX holders */
2639 if (lockres->l_blocking == LKM_PRMODE &&
2640 lockres->l_ex_holders)
2644 * Can we get a lock in this state if the holder counts are
2645 * zero? The meta data unblock code used to check this.
2647 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
2648 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
2651 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2653 if (lockres->l_ops->check_downconvert
2654 && !lockres->l_ops->check_downconvert(lockres, new_level))
2657 /* If we get here, then we know that there are no more
2658 * incompatible holders (and anyone asking for an incompatible
2659 * lock is blocked). We can now downconvert the lock */
2660 if (!lockres->l_ops->downconvert_worker)
2663 /* Some lockres types want to do a bit of work before
2664 * downconverting a lock. Allow that here. The worker function
2665 * may sleep, so we save off a copy of what we're blocking as
2666 * it may change while we're not holding the spin lock. */
2667 blocking = lockres->l_blocking;
2668 spin_unlock_irqrestore(&lockres->l_lock, flags);
2670 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
2672 if (ctl->unblock_action == UNBLOCK_STOP_POST)
2675 spin_lock_irqsave(&lockres->l_lock, flags);
2676 if (blocking != lockres->l_blocking) {
2677 /* If this changed underneath us, then we can't drop
2685 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2686 if (lockres->l_level == LKM_EXMODE)
2690 * We only set the lvb if the lock has been fully
2691 * refreshed - otherwise we risk setting stale
2692 * data. Otherwise, there's no need to actually clear
2693 * out the lvb here as it's value is still valid.
2695 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2696 lockres->l_ops->set_lvb(lockres);
2699 ocfs2_prepare_downconvert(lockres, new_level);
2700 spin_unlock_irqrestore(&lockres->l_lock, flags);
2701 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
2707 spin_unlock_irqrestore(&lockres->l_lock, flags);
2714 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
2717 struct inode *inode;
2718 struct address_space *mapping;
2720 inode = ocfs2_lock_res_inode(lockres);
2721 mapping = inode->i_mapping;
2724 * We need this before the filemap_fdatawrite() so that it can
2725 * transfer the dirty bit from the PTE to the
2726 * page. Unfortunately this means that even for EX->PR
2727 * downconverts, we'll lose our mappings and have to build
2730 unmap_mapping_range(mapping, 0, 0, 0);
2732 if (filemap_fdatawrite(mapping)) {
2733 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
2734 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2736 sync_mapping_buffers(mapping);
2737 if (blocking == LKM_EXMODE) {
2738 truncate_inode_pages(mapping, 0);
2740 /* We only need to wait on the I/O if we're not also
2741 * truncating pages because truncate_inode_pages waits
2742 * for us above. We don't truncate pages if we're
2743 * blocking anything < EXMODE because we want to keep
2744 * them around in that case. */
2745 filemap_fdatawait(mapping);
2748 return UNBLOCK_CONTINUE;
2751 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
2754 struct inode *inode = ocfs2_lock_res_inode(lockres);
2755 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
2757 BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
2758 BUG_ON(lockres->l_level != LKM_EXMODE && !checkpointed);
2763 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
2767 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
2769 struct inode *inode = ocfs2_lock_res_inode(lockres);
2771 __ocfs2_stuff_meta_lvb(inode);
2775 * Does the final reference drop on our dentry lock. Right now this
2776 * happens in the vote thread, but we could choose to simplify the
2777 * dlmglue API and push these off to the ocfs2_wq in the future.
2779 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
2780 struct ocfs2_lock_res *lockres)
2782 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2783 ocfs2_dentry_lock_put(osb, dl);
2787 * d_delete() matching dentries before the lock downconvert.
2789 * At this point, any process waiting to destroy the
2790 * dentry_lock due to last ref count is stopped by the
2791 * OCFS2_LOCK_QUEUED flag.
2793 * We have two potential problems
2795 * 1) If we do the last reference drop on our dentry_lock (via dput)
2796 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
2797 * the downconvert to finish. Instead we take an elevated
2798 * reference and push the drop until after we've completed our
2799 * unblock processing.
2801 * 2) There might be another process with a final reference,
2802 * waiting on us to finish processing. If this is the case, we
2803 * detect it and exit out - there's no more dentries anyway.
2805 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
2808 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2809 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
2810 struct dentry *dentry;
2811 unsigned long flags;
2815 * This node is blocking another node from getting a read
2816 * lock. This happens when we've renamed within a
2817 * directory. We've forced the other nodes to d_delete(), but
2818 * we never actually dropped our lock because it's still
2819 * valid. The downconvert code will retain a PR for this node,
2820 * so there's no further work to do.
2822 if (blocking == LKM_PRMODE)
2823 return UNBLOCK_CONTINUE;
2826 * Mark this inode as potentially orphaned. The code in
2827 * ocfs2_delete_inode() will figure out whether it actually
2828 * needs to be freed or not.
2830 spin_lock(&oi->ip_lock);
2831 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
2832 spin_unlock(&oi->ip_lock);
2835 * Yuck. We need to make sure however that the check of
2836 * OCFS2_LOCK_FREEING and the extra reference are atomic with
2837 * respect to a reference decrement or the setting of that
2840 spin_lock_irqsave(&lockres->l_lock, flags);
2841 spin_lock(&dentry_attach_lock);
2842 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
2847 spin_unlock(&dentry_attach_lock);
2848 spin_unlock_irqrestore(&lockres->l_lock, flags);
2850 mlog(0, "extra_ref = %d\n", extra_ref);
2853 * We have a process waiting on us in ocfs2_dentry_iput(),
2854 * which means we can't have any more outstanding
2855 * aliases. There's no need to do any more work.
2858 return UNBLOCK_CONTINUE;
2860 spin_lock(&dentry_attach_lock);
2862 dentry = ocfs2_find_local_alias(dl->dl_inode,
2863 dl->dl_parent_blkno, 1);
2866 spin_unlock(&dentry_attach_lock);
2868 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
2869 dentry->d_name.name);
2872 * The following dcache calls may do an
2873 * iput(). Normally we don't want that from the
2874 * downconverting thread, but in this case it's ok
2875 * because the requesting node already has an
2876 * exclusive lock on the inode, so it can't be queued
2877 * for a downconvert.
2882 spin_lock(&dentry_attach_lock);
2884 spin_unlock(&dentry_attach_lock);
2887 * If we are the last holder of this dentry lock, there is no
2888 * reason to downconvert so skip straight to the unlock.
2890 if (dl->dl_count == 1)
2891 return UNBLOCK_STOP_POST;
2893 return UNBLOCK_CONTINUE_POST;
2896 void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
2897 struct ocfs2_lock_res *lockres)
2900 struct ocfs2_unblock_ctl ctl = {0, 0,};
2901 unsigned long flags;
2903 /* Our reference to the lockres in this function can be
2904 * considered valid until we remove the OCFS2_LOCK_QUEUED
2910 BUG_ON(!lockres->l_ops);
2912 mlog(0, "lockres %s blocked.\n", lockres->l_name);
2914 /* Detect whether a lock has been marked as going away while
2915 * the vote thread was processing other things. A lock can
2916 * still be marked with OCFS2_LOCK_FREEING after this check,
2917 * but short circuiting here will still save us some
2919 spin_lock_irqsave(&lockres->l_lock, flags);
2920 if (lockres->l_flags & OCFS2_LOCK_FREEING)
2922 spin_unlock_irqrestore(&lockres->l_lock, flags);
2924 status = ocfs2_unblock_lock(osb, lockres, &ctl);
2928 spin_lock_irqsave(&lockres->l_lock, flags);
2930 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
2931 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
2933 ocfs2_schedule_blocked_lock(osb, lockres);
2935 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
2936 ctl.requeue ? "yes" : "no");
2937 spin_unlock_irqrestore(&lockres->l_lock, flags);
2939 if (ctl.unblock_action != UNBLOCK_CONTINUE
2940 && lockres->l_ops->post_unlock)
2941 lockres->l_ops->post_unlock(osb, lockres);
2946 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
2947 struct ocfs2_lock_res *lockres)
2951 assert_spin_locked(&lockres->l_lock);
2953 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
2954 /* Do not schedule a lock for downconvert when it's on
2955 * the way to destruction - any nodes wanting access
2956 * to the resource will get it soon. */
2957 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
2958 lockres->l_name, lockres->l_flags);
2962 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
2964 spin_lock(&osb->vote_task_lock);
2965 if (list_empty(&lockres->l_blocked_list)) {
2966 list_add_tail(&lockres->l_blocked_list,
2967 &osb->blocked_lock_list);
2968 osb->blocked_lock_count++;
2970 spin_unlock(&osb->vote_task_lock);
2975 /* This aids in debugging situations where a bad LVB might be involved. */
2976 void ocfs2_dump_meta_lvb_info(u64 level,
2977 const char *function,
2979 struct ocfs2_lock_res *lockres)
2981 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
2983 mlog(level, "LVB information for %s (called from %s:%u):\n",
2984 lockres->l_name, function, line);
2985 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
2986 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
2987 be32_to_cpu(lvb->lvb_igeneration));
2988 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
2989 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
2990 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
2991 be16_to_cpu(lvb->lvb_imode));
2992 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
2993 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
2994 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
2995 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
2996 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
2997 be32_to_cpu(lvb->lvb_iattr));