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_imode = cpu_to_be16(inode->i_mode);
1343 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1344 lvb->lvb_iatime_packed =
1345 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1346 lvb->lvb_ictime_packed =
1347 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1348 lvb->lvb_imtime_packed =
1349 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1350 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
1351 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
1354 mlog_meta_lvb(0, lockres);
1359 static void ocfs2_unpack_timespec(struct timespec *spec,
1362 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1363 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1366 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1368 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1369 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1370 struct ocfs2_meta_lvb *lvb;
1374 mlog_meta_lvb(0, lockres);
1376 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1378 /* We're safe here without the lockres lock... */
1379 spin_lock(&oi->ip_lock);
1380 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1381 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1383 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1384 ocfs2_set_inode_flags(inode);
1386 /* fast-symlinks are a special case */
1387 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1388 inode->i_blocks = 0;
1391 ocfs2_align_bytes_to_sectors(i_size_read(inode));
1393 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1394 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1395 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1396 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1397 ocfs2_unpack_timespec(&inode->i_atime,
1398 be64_to_cpu(lvb->lvb_iatime_packed));
1399 ocfs2_unpack_timespec(&inode->i_mtime,
1400 be64_to_cpu(lvb->lvb_imtime_packed));
1401 ocfs2_unpack_timespec(&inode->i_ctime,
1402 be64_to_cpu(lvb->lvb_ictime_packed));
1403 spin_unlock(&oi->ip_lock);
1408 static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1409 struct ocfs2_lock_res *lockres)
1411 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1413 if (lvb->lvb_version == OCFS2_LVB_VERSION
1414 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
1419 /* Determine whether a lock resource needs to be refreshed, and
1420 * arbitrate who gets to refresh it.
1422 * 0 means no refresh needed.
1424 * > 0 means you need to refresh this and you MUST call
1425 * ocfs2_complete_lock_res_refresh afterwards. */
1426 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1428 unsigned long flags;
1434 spin_lock_irqsave(&lockres->l_lock, flags);
1435 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1436 spin_unlock_irqrestore(&lockres->l_lock, flags);
1440 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1441 spin_unlock_irqrestore(&lockres->l_lock, flags);
1443 ocfs2_wait_on_refreshing_lock(lockres);
1447 /* Ok, I'll be the one to refresh this lock. */
1448 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1449 spin_unlock_irqrestore(&lockres->l_lock, flags);
1457 /* If status is non zero, I'll mark it as not being in refresh
1458 * anymroe, but i won't clear the needs refresh flag. */
1459 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1462 unsigned long flags;
1465 spin_lock_irqsave(&lockres->l_lock, flags);
1466 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1468 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1469 spin_unlock_irqrestore(&lockres->l_lock, flags);
1471 wake_up(&lockres->l_event);
1476 /* may or may not return a bh if it went to disk. */
1477 static int ocfs2_meta_lock_update(struct inode *inode,
1478 struct buffer_head **bh)
1481 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1482 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1483 struct ocfs2_dinode *fe;
1484 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1488 if (ocfs2_mount_local(osb))
1491 spin_lock(&oi->ip_lock);
1492 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1493 mlog(0, "Orphaned inode %llu was deleted while we "
1494 "were waiting on a lock. ip_flags = 0x%x\n",
1495 (unsigned long long)oi->ip_blkno, oi->ip_flags);
1496 spin_unlock(&oi->ip_lock);
1500 spin_unlock(&oi->ip_lock);
1502 if (!ocfs2_should_refresh_lock_res(lockres))
1505 /* This will discard any caching information we might have had
1506 * for the inode metadata. */
1507 ocfs2_metadata_cache_purge(inode);
1509 /* will do nothing for inode types that don't use the extent
1510 * map (bitmap files, etc) */
1511 ocfs2_extent_map_trunc(inode, 0);
1513 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
1514 mlog(0, "Trusting LVB on inode %llu\n",
1515 (unsigned long long)oi->ip_blkno);
1516 ocfs2_refresh_inode_from_lvb(inode);
1518 /* Boo, we have to go to disk. */
1519 /* read bh, cast, ocfs2_refresh_inode */
1520 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1521 bh, OCFS2_BH_CACHED, inode);
1526 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1528 /* This is a good chance to make sure we're not
1529 * locking an invalid object.
1531 * We bug on a stale inode here because we checked
1532 * above whether it was wiped from disk. The wiping
1533 * node provides a guarantee that we receive that
1534 * message and can mark the inode before dropping any
1535 * locks associated with it. */
1536 if (!OCFS2_IS_VALID_DINODE(fe)) {
1537 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1541 mlog_bug_on_msg(inode->i_generation !=
1542 le32_to_cpu(fe->i_generation),
1543 "Invalid dinode %llu disk generation: %u "
1544 "inode->i_generation: %u\n",
1545 (unsigned long long)oi->ip_blkno,
1546 le32_to_cpu(fe->i_generation),
1547 inode->i_generation);
1548 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1549 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
1550 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1551 (unsigned long long)oi->ip_blkno,
1552 (unsigned long long)le64_to_cpu(fe->i_dtime),
1553 le32_to_cpu(fe->i_flags));
1555 ocfs2_refresh_inode(inode, fe);
1560 ocfs2_complete_lock_res_refresh(lockres, status);
1566 static int ocfs2_assign_bh(struct inode *inode,
1567 struct buffer_head **ret_bh,
1568 struct buffer_head *passed_bh)
1573 /* Ok, the update went to disk for us, use the
1575 *ret_bh = passed_bh;
1581 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1582 OCFS2_I(inode)->ip_blkno,
1593 * returns < 0 error if the callback will never be called, otherwise
1594 * the result of the lock will be communicated via the callback.
1596 int ocfs2_meta_lock_full(struct inode *inode,
1597 struct buffer_head **ret_bh,
1601 int status, level, dlm_flags, acquired;
1602 struct ocfs2_lock_res *lockres = NULL;
1603 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1604 struct buffer_head *local_bh = NULL;
1610 mlog(0, "inode %llu, take %s META lock\n",
1611 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1612 ex ? "EXMODE" : "PRMODE");
1616 /* We'll allow faking a readonly metadata lock for
1618 if (ocfs2_is_hard_readonly(osb)) {
1624 if (ocfs2_mount_local(osb))
1627 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1628 wait_event(osb->recovery_event,
1629 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1631 lockres = &OCFS2_I(inode)->ip_meta_lockres;
1632 level = ex ? LKM_EXMODE : LKM_PRMODE;
1634 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
1635 dlm_flags |= LKM_NOQUEUE;
1637 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1639 if (status != -EAGAIN && status != -EIOCBRETRY)
1644 /* Notify the error cleanup path to drop the cluster lock. */
1647 /* We wait twice because a node may have died while we were in
1648 * the lower dlm layers. The second time though, we've
1649 * committed to owning this lock so we don't allow signals to
1650 * abort the operation. */
1651 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1652 wait_event(osb->recovery_event,
1653 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1657 * We only see this flag if we're being called from
1658 * ocfs2_read_locked_inode(). It means we're locking an inode
1659 * which hasn't been populated yet, so clear the refresh flag
1660 * and let the caller handle it.
1662 if (inode->i_state & I_NEW) {
1665 ocfs2_complete_lock_res_refresh(lockres, 0);
1669 /* This is fun. The caller may want a bh back, or it may
1670 * not. ocfs2_meta_lock_update definitely wants one in, but
1671 * may or may not read one, depending on what's in the
1672 * LVB. The result of all of this is that we've *only* gone to
1673 * disk if we have to, so the complexity is worthwhile. */
1674 status = ocfs2_meta_lock_update(inode, &local_bh);
1676 if (status != -ENOENT)
1682 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
1691 if (ret_bh && (*ret_bh)) {
1696 ocfs2_meta_unlock(inode, ex);
1707 * This is working around a lock inversion between tasks acquiring DLM locks
1708 * while holding a page lock and the vote thread which blocks dlm lock acquiry
1709 * while acquiring page locks.
1711 * ** These _with_page variantes are only intended to be called from aop
1712 * methods that hold page locks and return a very specific *positive* error
1713 * code that aop methods pass up to the VFS -- test for errors with != 0. **
1715 * The DLM is called such that it returns -EAGAIN if it would have blocked
1716 * waiting for the vote thread. In that case we unlock our page so the vote
1717 * thread can make progress. Once we've done this we have to return
1718 * AOP_TRUNCATED_PAGE so the aop method that called us can bubble that back up
1719 * into the VFS who will then immediately retry the aop call.
1721 * We do a blocking lock and immediate unlock before returning, though, so that
1722 * the lock has a great chance of being cached on this node by the time the VFS
1723 * calls back to retry the aop. This has a potential to livelock as nodes
1724 * ping locks back and forth, but that's a risk we're willing to take to avoid
1725 * the lock inversion simply.
1727 int ocfs2_meta_lock_with_page(struct inode *inode,
1728 struct buffer_head **ret_bh,
1734 ret = ocfs2_meta_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
1735 if (ret == -EAGAIN) {
1737 if (ocfs2_meta_lock(inode, ret_bh, ex) == 0)
1738 ocfs2_meta_unlock(inode, ex);
1739 ret = AOP_TRUNCATED_PAGE;
1745 int ocfs2_meta_lock_atime(struct inode *inode,
1746 struct vfsmount *vfsmnt,
1752 ret = ocfs2_meta_lock(inode, NULL, 0);
1759 * If we should update atime, we will get EX lock,
1760 * otherwise we just get PR lock.
1762 if (ocfs2_should_update_atime(inode, vfsmnt)) {
1763 struct buffer_head *bh = NULL;
1765 ocfs2_meta_unlock(inode, 0);
1766 ret = ocfs2_meta_lock(inode, &bh, 1);
1772 if (ocfs2_should_update_atime(inode, vfsmnt))
1773 ocfs2_update_inode_atime(inode, bh);
1783 void ocfs2_meta_unlock(struct inode *inode,
1786 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1787 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
1788 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1792 mlog(0, "inode %llu drop %s META lock\n",
1793 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1794 ex ? "EXMODE" : "PRMODE");
1796 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
1797 !ocfs2_mount_local(osb))
1798 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1803 int ocfs2_super_lock(struct ocfs2_super *osb,
1807 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1808 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1809 struct buffer_head *bh;
1810 struct ocfs2_slot_info *si = osb->slot_info;
1814 if (ocfs2_is_hard_readonly(osb))
1817 if (ocfs2_mount_local(osb))
1820 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
1826 /* The super block lock path is really in the best position to
1827 * know when resources covered by the lock need to be
1828 * refreshed, so we do it here. Of course, making sense of
1829 * everything is up to the caller :) */
1830 status = ocfs2_should_refresh_lock_res(lockres);
1837 status = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0,
1840 ocfs2_update_slot_info(si);
1842 ocfs2_complete_lock_res_refresh(lockres, status);
1852 void ocfs2_super_unlock(struct ocfs2_super *osb,
1855 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1856 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1858 if (!ocfs2_mount_local(osb))
1859 ocfs2_cluster_unlock(osb, lockres, level);
1862 int ocfs2_rename_lock(struct ocfs2_super *osb)
1865 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1867 if (ocfs2_is_hard_readonly(osb))
1870 if (ocfs2_mount_local(osb))
1873 status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
1880 void ocfs2_rename_unlock(struct ocfs2_super *osb)
1882 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1884 if (!ocfs2_mount_local(osb))
1885 ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
1888 int ocfs2_dentry_lock(struct dentry *dentry, int ex)
1891 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1892 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1893 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1897 if (ocfs2_is_hard_readonly(osb))
1900 if (ocfs2_mount_local(osb))
1903 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
1910 void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
1912 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1913 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1914 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1916 if (!ocfs2_mount_local(osb))
1917 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
1920 /* Reference counting of the dlm debug structure. We want this because
1921 * open references on the debug inodes can live on after a mount, so
1922 * we can't rely on the ocfs2_super to always exist. */
1923 static void ocfs2_dlm_debug_free(struct kref *kref)
1925 struct ocfs2_dlm_debug *dlm_debug;
1927 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
1932 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
1935 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
1938 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
1940 kref_get(&debug->d_refcnt);
1943 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
1945 struct ocfs2_dlm_debug *dlm_debug;
1947 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
1949 mlog_errno(-ENOMEM);
1953 kref_init(&dlm_debug->d_refcnt);
1954 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
1955 dlm_debug->d_locking_state = NULL;
1960 /* Access to this is arbitrated for us via seq_file->sem. */
1961 struct ocfs2_dlm_seq_priv {
1962 struct ocfs2_dlm_debug *p_dlm_debug;
1963 struct ocfs2_lock_res p_iter_res;
1964 struct ocfs2_lock_res p_tmp_res;
1967 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
1968 struct ocfs2_dlm_seq_priv *priv)
1970 struct ocfs2_lock_res *iter, *ret = NULL;
1971 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
1973 assert_spin_locked(&ocfs2_dlm_tracking_lock);
1975 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
1976 /* discover the head of the list */
1977 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
1978 mlog(0, "End of list found, %p\n", ret);
1982 /* We track our "dummy" iteration lockres' by a NULL
1984 if (iter->l_ops != NULL) {
1993 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
1995 struct ocfs2_dlm_seq_priv *priv = m->private;
1996 struct ocfs2_lock_res *iter;
1998 spin_lock(&ocfs2_dlm_tracking_lock);
1999 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2001 /* Since lockres' have the lifetime of their container
2002 * (which can be inodes, ocfs2_supers, etc) we want to
2003 * copy this out to a temporary lockres while still
2004 * under the spinlock. Obviously after this we can't
2005 * trust any pointers on the copy returned, but that's
2006 * ok as the information we want isn't typically held
2008 priv->p_tmp_res = *iter;
2009 iter = &priv->p_tmp_res;
2011 spin_unlock(&ocfs2_dlm_tracking_lock);
2016 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2020 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2022 struct ocfs2_dlm_seq_priv *priv = m->private;
2023 struct ocfs2_lock_res *iter = v;
2024 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2026 spin_lock(&ocfs2_dlm_tracking_lock);
2027 iter = ocfs2_dlm_next_res(iter, priv);
2028 list_del_init(&dummy->l_debug_list);
2030 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2031 priv->p_tmp_res = *iter;
2032 iter = &priv->p_tmp_res;
2034 spin_unlock(&ocfs2_dlm_tracking_lock);
2039 /* So that debugfs.ocfs2 can determine which format is being used */
2040 #define OCFS2_DLM_DEBUG_STR_VERSION 1
2041 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2045 struct ocfs2_lock_res *lockres = v;
2050 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2052 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2053 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2055 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2057 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2059 seq_printf(m, "%d\t"
2070 lockres->l_unlock_action,
2071 lockres->l_ro_holders,
2072 lockres->l_ex_holders,
2073 lockres->l_requested,
2074 lockres->l_blocking);
2076 /* Dump the raw LVB */
2077 lvb = lockres->l_lksb.lvb;
2078 for(i = 0; i < DLM_LVB_LEN; i++)
2079 seq_printf(m, "0x%x\t", lvb[i]);
2082 seq_printf(m, "\n");
2086 static struct seq_operations ocfs2_dlm_seq_ops = {
2087 .start = ocfs2_dlm_seq_start,
2088 .stop = ocfs2_dlm_seq_stop,
2089 .next = ocfs2_dlm_seq_next,
2090 .show = ocfs2_dlm_seq_show,
2093 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2095 struct seq_file *seq = (struct seq_file *) file->private_data;
2096 struct ocfs2_dlm_seq_priv *priv = seq->private;
2097 struct ocfs2_lock_res *res = &priv->p_iter_res;
2099 ocfs2_remove_lockres_tracking(res);
2100 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2101 return seq_release_private(inode, file);
2104 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2107 struct ocfs2_dlm_seq_priv *priv;
2108 struct seq_file *seq;
2109 struct ocfs2_super *osb;
2111 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2117 osb = inode->i_private;
2118 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2119 priv->p_dlm_debug = osb->osb_dlm_debug;
2120 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2122 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2129 seq = (struct seq_file *) file->private_data;
2130 seq->private = priv;
2132 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2139 static const struct file_operations ocfs2_dlm_debug_fops = {
2140 .open = ocfs2_dlm_debug_open,
2141 .release = ocfs2_dlm_debug_release,
2143 .llseek = seq_lseek,
2146 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2149 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2151 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2153 osb->osb_debug_root,
2155 &ocfs2_dlm_debug_fops);
2156 if (!dlm_debug->d_locking_state) {
2159 "Unable to create locking state debugfs file.\n");
2163 ocfs2_get_dlm_debug(dlm_debug);
2168 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2170 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2173 debugfs_remove(dlm_debug->d_locking_state);
2174 ocfs2_put_dlm_debug(dlm_debug);
2178 int ocfs2_dlm_init(struct ocfs2_super *osb)
2182 struct dlm_ctxt *dlm = NULL;
2186 if (ocfs2_mount_local(osb))
2189 status = ocfs2_dlm_init_debug(osb);
2195 /* launch vote thread */
2196 osb->vote_task = kthread_run(ocfs2_vote_thread, osb, "ocfs2vote");
2197 if (IS_ERR(osb->vote_task)) {
2198 status = PTR_ERR(osb->vote_task);
2199 osb->vote_task = NULL;
2204 /* used by the dlm code to make message headers unique, each
2205 * node in this domain must agree on this. */
2206 dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
2208 /* for now, uuid == domain */
2209 dlm = dlm_register_domain(osb->uuid_str, dlm_key);
2211 status = PTR_ERR(dlm);
2216 dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
2219 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2220 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2227 ocfs2_dlm_shutdown_debug(osb);
2229 kthread_stop(osb->vote_task);
2236 void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2240 dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
2242 ocfs2_drop_osb_locks(osb);
2244 if (osb->vote_task) {
2245 kthread_stop(osb->vote_task);
2246 osb->vote_task = NULL;
2249 ocfs2_lock_res_free(&osb->osb_super_lockres);
2250 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2252 dlm_unregister_domain(osb->dlm);
2255 ocfs2_dlm_shutdown_debug(osb);
2260 static void ocfs2_unlock_ast(void *opaque, enum dlm_status status)
2262 struct ocfs2_lock_res *lockres = opaque;
2263 unsigned long flags;
2267 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2268 lockres->l_unlock_action);
2270 spin_lock_irqsave(&lockres->l_lock, flags);
2271 /* We tried to cancel a convert request, but it was already
2272 * granted. All we want to do here is clear our unlock
2273 * state. The wake_up call done at the bottom is redundant
2274 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2275 * hurt anything anyway */
2276 if (status == DLM_CANCELGRANT &&
2277 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2278 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2280 /* We don't clear the busy flag in this case as it
2281 * should have been cleared by the ast which the dlm
2283 goto complete_unlock;
2286 if (status != DLM_NORMAL) {
2287 mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
2288 "unlock_action %d\n", status, lockres->l_name,
2289 lockres->l_unlock_action);
2290 spin_unlock_irqrestore(&lockres->l_lock, flags);
2294 switch(lockres->l_unlock_action) {
2295 case OCFS2_UNLOCK_CANCEL_CONVERT:
2296 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2297 lockres->l_action = OCFS2_AST_INVALID;
2299 case OCFS2_UNLOCK_DROP_LOCK:
2300 lockres->l_level = LKM_IVMODE;
2306 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2308 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2309 spin_unlock_irqrestore(&lockres->l_lock, flags);
2311 wake_up(&lockres->l_event);
2316 static int ocfs2_drop_lock(struct ocfs2_super *osb,
2317 struct ocfs2_lock_res *lockres)
2319 enum dlm_status status;
2320 unsigned long flags;
2323 /* We didn't get anywhere near actually using this lockres. */
2324 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2327 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
2328 lkm_flags |= LKM_VALBLK;
2330 spin_lock_irqsave(&lockres->l_lock, flags);
2332 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2333 "lockres %s, flags 0x%lx\n",
2334 lockres->l_name, lockres->l_flags);
2336 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2337 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2338 "%u, unlock_action = %u\n",
2339 lockres->l_name, lockres->l_flags, lockres->l_action,
2340 lockres->l_unlock_action);
2342 spin_unlock_irqrestore(&lockres->l_lock, flags);
2344 /* XXX: Today we just wait on any busy
2345 * locks... Perhaps we need to cancel converts in the
2347 ocfs2_wait_on_busy_lock(lockres);
2349 spin_lock_irqsave(&lockres->l_lock, flags);
2352 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2353 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2354 lockres->l_level == LKM_EXMODE &&
2355 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2356 lockres->l_ops->set_lvb(lockres);
2359 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2360 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2362 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2363 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2365 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2366 spin_unlock_irqrestore(&lockres->l_lock, flags);
2370 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2372 /* make sure we never get here while waiting for an ast to
2374 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2376 /* is this necessary? */
2377 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2378 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2379 spin_unlock_irqrestore(&lockres->l_lock, flags);
2381 mlog(0, "lock %s\n", lockres->l_name);
2383 status = dlmunlock(osb->dlm, &lockres->l_lksb, lkm_flags,
2384 ocfs2_unlock_ast, lockres);
2385 if (status != DLM_NORMAL) {
2386 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2387 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2388 dlm_print_one_lock(lockres->l_lksb.lockid);
2391 mlog(0, "lock %s, successfull return from dlmunlock\n",
2394 ocfs2_wait_on_busy_lock(lockres);
2400 /* Mark the lockres as being dropped. It will no longer be
2401 * queued if blocking, but we still may have to wait on it
2402 * being dequeued from the vote thread before we can consider
2405 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2406 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2409 struct ocfs2_mask_waiter mw;
2410 unsigned long flags;
2412 ocfs2_init_mask_waiter(&mw);
2414 spin_lock_irqsave(&lockres->l_lock, flags);
2415 lockres->l_flags |= OCFS2_LOCK_FREEING;
2416 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2417 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2418 spin_unlock_irqrestore(&lockres->l_lock, flags);
2420 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2422 status = ocfs2_wait_for_mask(&mw);
2426 spin_lock_irqsave(&lockres->l_lock, flags);
2428 spin_unlock_irqrestore(&lockres->l_lock, flags);
2431 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2432 struct ocfs2_lock_res *lockres)
2436 ocfs2_mark_lockres_freeing(lockres);
2437 ret = ocfs2_drop_lock(osb, lockres);
2442 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2444 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2445 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
2448 int ocfs2_drop_inode_locks(struct inode *inode)
2454 /* No need to call ocfs2_mark_lockres_freeing here -
2455 * ocfs2_clear_inode has done it for us. */
2457 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2458 &OCFS2_I(inode)->ip_data_lockres);
2464 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2465 &OCFS2_I(inode)->ip_meta_lockres);
2468 if (err < 0 && !status)
2471 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2472 &OCFS2_I(inode)->ip_rw_lockres);
2475 if (err < 0 && !status)
2482 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2485 assert_spin_locked(&lockres->l_lock);
2487 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
2489 if (lockres->l_level <= new_level) {
2490 mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
2491 lockres->l_level, new_level);
2495 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2496 lockres->l_name, new_level, lockres->l_blocking);
2498 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2499 lockres->l_requested = new_level;
2500 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2503 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2504 struct ocfs2_lock_res *lockres,
2508 int ret, dlm_flags = LKM_CONVERT;
2509 enum dlm_status status;
2514 dlm_flags |= LKM_VALBLK;
2516 status = dlmlock(osb->dlm,
2521 OCFS2_LOCK_ID_MAX_LEN - 1,
2524 ocfs2_blocking_ast);
2525 if (status != DLM_NORMAL) {
2526 ocfs2_log_dlm_error("dlmlock", status, lockres);
2528 ocfs2_recover_from_dlm_error(lockres, 1);
2538 /* returns 1 when the caller should unlock and call dlmunlock */
2539 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2540 struct ocfs2_lock_res *lockres)
2542 assert_spin_locked(&lockres->l_lock);
2545 mlog(0, "lock %s\n", lockres->l_name);
2547 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2548 /* If we're already trying to cancel a lock conversion
2549 * then just drop the spinlock and allow the caller to
2550 * requeue this lock. */
2552 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2556 /* were we in a convert when we got the bast fire? */
2557 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2558 lockres->l_action != OCFS2_AST_DOWNCONVERT);
2559 /* set things up for the unlockast to know to just
2560 * clear out the ast_action and unset busy, etc. */
2561 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2563 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2564 "lock %s, invalid flags: 0x%lx\n",
2565 lockres->l_name, lockres->l_flags);
2570 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2571 struct ocfs2_lock_res *lockres)
2574 enum dlm_status status;
2577 mlog(0, "lock %s\n", lockres->l_name);
2580 status = dlmunlock(osb->dlm,
2585 if (status != DLM_NORMAL) {
2586 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2588 ocfs2_recover_from_dlm_error(lockres, 0);
2591 mlog(0, "lock %s return from dlmunlock\n", lockres->l_name);
2597 static int ocfs2_unblock_lock(struct ocfs2_super *osb,
2598 struct ocfs2_lock_res *lockres,
2599 struct ocfs2_unblock_ctl *ctl)
2601 unsigned long flags;
2609 spin_lock_irqsave(&lockres->l_lock, flags);
2611 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2614 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2616 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2617 spin_unlock_irqrestore(&lockres->l_lock, flags);
2619 ret = ocfs2_cancel_convert(osb, lockres);
2626 /* if we're blocking an exclusive and we have *any* holders,
2628 if ((lockres->l_blocking == LKM_EXMODE)
2629 && (lockres->l_ex_holders || lockres->l_ro_holders))
2632 /* If it's a PR we're blocking, then only
2633 * requeue if we've got any EX holders */
2634 if (lockres->l_blocking == LKM_PRMODE &&
2635 lockres->l_ex_holders)
2639 * Can we get a lock in this state if the holder counts are
2640 * zero? The meta data unblock code used to check this.
2642 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
2643 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
2646 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2648 if (lockres->l_ops->check_downconvert
2649 && !lockres->l_ops->check_downconvert(lockres, new_level))
2652 /* If we get here, then we know that there are no more
2653 * incompatible holders (and anyone asking for an incompatible
2654 * lock is blocked). We can now downconvert the lock */
2655 if (!lockres->l_ops->downconvert_worker)
2658 /* Some lockres types want to do a bit of work before
2659 * downconverting a lock. Allow that here. The worker function
2660 * may sleep, so we save off a copy of what we're blocking as
2661 * it may change while we're not holding the spin lock. */
2662 blocking = lockres->l_blocking;
2663 spin_unlock_irqrestore(&lockres->l_lock, flags);
2665 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
2667 if (ctl->unblock_action == UNBLOCK_STOP_POST)
2670 spin_lock_irqsave(&lockres->l_lock, flags);
2671 if (blocking != lockres->l_blocking) {
2672 /* If this changed underneath us, then we can't drop
2680 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2681 if (lockres->l_level == LKM_EXMODE)
2685 * We only set the lvb if the lock has been fully
2686 * refreshed - otherwise we risk setting stale
2687 * data. Otherwise, there's no need to actually clear
2688 * out the lvb here as it's value is still valid.
2690 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2691 lockres->l_ops->set_lvb(lockres);
2694 ocfs2_prepare_downconvert(lockres, new_level);
2695 spin_unlock_irqrestore(&lockres->l_lock, flags);
2696 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
2702 spin_unlock_irqrestore(&lockres->l_lock, flags);
2709 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
2712 struct inode *inode;
2713 struct address_space *mapping;
2715 inode = ocfs2_lock_res_inode(lockres);
2716 mapping = inode->i_mapping;
2719 * We need this before the filemap_fdatawrite() so that it can
2720 * transfer the dirty bit from the PTE to the
2721 * page. Unfortunately this means that even for EX->PR
2722 * downconverts, we'll lose our mappings and have to build
2725 unmap_mapping_range(mapping, 0, 0, 0);
2727 if (filemap_fdatawrite(mapping)) {
2728 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
2729 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2731 sync_mapping_buffers(mapping);
2732 if (blocking == LKM_EXMODE) {
2733 truncate_inode_pages(mapping, 0);
2735 /* We only need to wait on the I/O if we're not also
2736 * truncating pages because truncate_inode_pages waits
2737 * for us above. We don't truncate pages if we're
2738 * blocking anything < EXMODE because we want to keep
2739 * them around in that case. */
2740 filemap_fdatawait(mapping);
2743 return UNBLOCK_CONTINUE;
2746 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
2749 struct inode *inode = ocfs2_lock_res_inode(lockres);
2750 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
2752 BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
2753 BUG_ON(lockres->l_level != LKM_EXMODE && !checkpointed);
2758 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
2762 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
2764 struct inode *inode = ocfs2_lock_res_inode(lockres);
2766 __ocfs2_stuff_meta_lvb(inode);
2770 * Does the final reference drop on our dentry lock. Right now this
2771 * happens in the vote thread, but we could choose to simplify the
2772 * dlmglue API and push these off to the ocfs2_wq in the future.
2774 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
2775 struct ocfs2_lock_res *lockres)
2777 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2778 ocfs2_dentry_lock_put(osb, dl);
2782 * d_delete() matching dentries before the lock downconvert.
2784 * At this point, any process waiting to destroy the
2785 * dentry_lock due to last ref count is stopped by the
2786 * OCFS2_LOCK_QUEUED flag.
2788 * We have two potential problems
2790 * 1) If we do the last reference drop on our dentry_lock (via dput)
2791 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
2792 * the downconvert to finish. Instead we take an elevated
2793 * reference and push the drop until after we've completed our
2794 * unblock processing.
2796 * 2) There might be another process with a final reference,
2797 * waiting on us to finish processing. If this is the case, we
2798 * detect it and exit out - there's no more dentries anyway.
2800 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
2803 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2804 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
2805 struct dentry *dentry;
2806 unsigned long flags;
2810 * This node is blocking another node from getting a read
2811 * lock. This happens when we've renamed within a
2812 * directory. We've forced the other nodes to d_delete(), but
2813 * we never actually dropped our lock because it's still
2814 * valid. The downconvert code will retain a PR for this node,
2815 * so there's no further work to do.
2817 if (blocking == LKM_PRMODE)
2818 return UNBLOCK_CONTINUE;
2821 * Mark this inode as potentially orphaned. The code in
2822 * ocfs2_delete_inode() will figure out whether it actually
2823 * needs to be freed or not.
2825 spin_lock(&oi->ip_lock);
2826 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
2827 spin_unlock(&oi->ip_lock);
2830 * Yuck. We need to make sure however that the check of
2831 * OCFS2_LOCK_FREEING and the extra reference are atomic with
2832 * respect to a reference decrement or the setting of that
2835 spin_lock_irqsave(&lockres->l_lock, flags);
2836 spin_lock(&dentry_attach_lock);
2837 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
2842 spin_unlock(&dentry_attach_lock);
2843 spin_unlock_irqrestore(&lockres->l_lock, flags);
2845 mlog(0, "extra_ref = %d\n", extra_ref);
2848 * We have a process waiting on us in ocfs2_dentry_iput(),
2849 * which means we can't have any more outstanding
2850 * aliases. There's no need to do any more work.
2853 return UNBLOCK_CONTINUE;
2855 spin_lock(&dentry_attach_lock);
2857 dentry = ocfs2_find_local_alias(dl->dl_inode,
2858 dl->dl_parent_blkno, 1);
2861 spin_unlock(&dentry_attach_lock);
2863 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
2864 dentry->d_name.name);
2867 * The following dcache calls may do an
2868 * iput(). Normally we don't want that from the
2869 * downconverting thread, but in this case it's ok
2870 * because the requesting node already has an
2871 * exclusive lock on the inode, so it can't be queued
2872 * for a downconvert.
2877 spin_lock(&dentry_attach_lock);
2879 spin_unlock(&dentry_attach_lock);
2882 * If we are the last holder of this dentry lock, there is no
2883 * reason to downconvert so skip straight to the unlock.
2885 if (dl->dl_count == 1)
2886 return UNBLOCK_STOP_POST;
2888 return UNBLOCK_CONTINUE_POST;
2891 void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
2892 struct ocfs2_lock_res *lockres)
2895 struct ocfs2_unblock_ctl ctl = {0, 0,};
2896 unsigned long flags;
2898 /* Our reference to the lockres in this function can be
2899 * considered valid until we remove the OCFS2_LOCK_QUEUED
2905 BUG_ON(!lockres->l_ops);
2907 mlog(0, "lockres %s blocked.\n", lockres->l_name);
2909 /* Detect whether a lock has been marked as going away while
2910 * the vote thread was processing other things. A lock can
2911 * still be marked with OCFS2_LOCK_FREEING after this check,
2912 * but short circuiting here will still save us some
2914 spin_lock_irqsave(&lockres->l_lock, flags);
2915 if (lockres->l_flags & OCFS2_LOCK_FREEING)
2917 spin_unlock_irqrestore(&lockres->l_lock, flags);
2919 status = ocfs2_unblock_lock(osb, lockres, &ctl);
2923 spin_lock_irqsave(&lockres->l_lock, flags);
2925 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
2926 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
2928 ocfs2_schedule_blocked_lock(osb, lockres);
2930 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
2931 ctl.requeue ? "yes" : "no");
2932 spin_unlock_irqrestore(&lockres->l_lock, flags);
2934 if (ctl.unblock_action != UNBLOCK_CONTINUE
2935 && lockres->l_ops->post_unlock)
2936 lockres->l_ops->post_unlock(osb, lockres);
2941 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
2942 struct ocfs2_lock_res *lockres)
2946 assert_spin_locked(&lockres->l_lock);
2948 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
2949 /* Do not schedule a lock for downconvert when it's on
2950 * the way to destruction - any nodes wanting access
2951 * to the resource will get it soon. */
2952 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
2953 lockres->l_name, lockres->l_flags);
2957 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
2959 spin_lock(&osb->vote_task_lock);
2960 if (list_empty(&lockres->l_blocked_list)) {
2961 list_add_tail(&lockres->l_blocked_list,
2962 &osb->blocked_lock_list);
2963 osb->blocked_lock_count++;
2965 spin_unlock(&osb->vote_task_lock);
2970 /* This aids in debugging situations where a bad LVB might be involved. */
2971 void ocfs2_dump_meta_lvb_info(u64 level,
2972 const char *function,
2974 struct ocfs2_lock_res *lockres)
2976 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
2978 mlog(level, "LVB information for %s (called from %s:%u):\n",
2979 lockres->l_name, function, line);
2980 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
2981 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
2982 be32_to_cpu(lvb->lvb_igeneration));
2983 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
2984 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
2985 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
2986 be16_to_cpu(lvb->lvb_imode));
2987 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
2988 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
2989 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
2990 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
2991 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
2992 be32_to_cpu(lvb->lvb_iattr));