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"
52 #include "heartbeat.h"
60 #include "buffer_head_io.h"
62 struct ocfs2_mask_waiter {
63 struct list_head mw_item;
65 struct completion mw_complete;
66 unsigned long mw_mask;
67 unsigned long mw_goal;
70 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
71 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
74 * Return value from ->downconvert_worker functions.
76 * These control the precise actions of ocfs2_unblock_lock()
77 * and ocfs2_process_blocked_lock()
80 enum ocfs2_unblock_action {
81 UNBLOCK_CONTINUE = 0, /* Continue downconvert */
82 UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire
83 * ->post_unlock callback */
84 UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire
85 * ->post_unlock() callback. */
88 struct ocfs2_unblock_ctl {
90 enum ocfs2_unblock_action unblock_action;
93 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
95 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
97 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
100 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
103 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
104 struct ocfs2_lock_res *lockres);
107 * OCFS2 Lock Resource Operations
109 * These fine tune the behavior of the generic dlmglue locking infrastructure.
111 * The most basic of lock types can point ->l_priv to their respective
112 * struct ocfs2_super and allow the default actions to manage things.
114 * Right now, each lock type also needs to implement an init function,
115 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
116 * should be called when the lock is no longer needed (i.e., object
119 struct ocfs2_lock_res_ops {
121 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
122 * this callback if ->l_priv is not an ocfs2_super pointer
124 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
127 * Optionally called in the downconvert (or "vote") thread
128 * after a successful downconvert. The lockres will not be
129 * referenced after this callback is called, so it is safe to
132 * The exact semantics of when this is called are controlled
133 * by ->downconvert_worker()
135 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
138 * Allow a lock type to add checks to determine whether it is
139 * safe to downconvert a lock. Return 0 to re-queue the
140 * downconvert at a later time, nonzero to continue.
142 * For most locks, the default checks that there are no
143 * incompatible holders are sufficient.
145 * Called with the lockres spinlock held.
147 int (*check_downconvert)(struct ocfs2_lock_res *, int);
150 * Allows a lock type to populate the lock value block. This
151 * is called on downconvert, and when we drop a lock.
153 * Locks that want to use this should set LOCK_TYPE_USES_LVB
154 * in the flags field.
156 * Called with the lockres spinlock held.
158 void (*set_lvb)(struct ocfs2_lock_res *);
161 * Called from the downconvert thread when it is determined
162 * that a lock will be downconverted. This is called without
163 * any locks held so the function can do work that might
164 * schedule (syncing out data, etc).
166 * This should return any one of the ocfs2_unblock_action
167 * values, depending on what it wants the thread to do.
169 int (*downconvert_worker)(struct ocfs2_lock_res *, int);
172 * LOCK_TYPE_* flags which describe the specific requirements
173 * of a lock type. Descriptions of each individual flag follow.
179 * Some locks want to "refresh" potentially stale data when a
180 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
181 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
182 * individual lockres l_flags member from the ast function. It is
183 * expected that the locking wrapper will clear the
184 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
186 #define LOCK_TYPE_REQUIRES_REFRESH 0x1
189 * Indicate that a lock type makes use of the lock value block. The
190 * ->set_lvb lock type callback must be defined.
192 #define LOCK_TYPE_USES_LVB 0x2
194 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
195 .get_osb = ocfs2_get_inode_osb,
199 static struct ocfs2_lock_res_ops ocfs2_inode_meta_lops = {
200 .get_osb = ocfs2_get_inode_osb,
201 .check_downconvert = ocfs2_check_meta_downconvert,
202 .set_lvb = ocfs2_set_meta_lvb,
203 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
206 static struct ocfs2_lock_res_ops ocfs2_inode_data_lops = {
207 .get_osb = ocfs2_get_inode_osb,
208 .downconvert_worker = ocfs2_data_convert_worker,
212 static struct ocfs2_lock_res_ops ocfs2_super_lops = {
213 .flags = LOCK_TYPE_REQUIRES_REFRESH,
216 static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
220 static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
221 .get_osb = ocfs2_get_dentry_osb,
222 .post_unlock = ocfs2_dentry_post_unlock,
223 .downconvert_worker = ocfs2_dentry_convert_worker,
227 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
229 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
230 lockres->l_type == OCFS2_LOCK_TYPE_DATA ||
231 lockres->l_type == OCFS2_LOCK_TYPE_RW;
234 static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
236 BUG_ON(!ocfs2_is_inode_lock(lockres));
238 return (struct inode *) lockres->l_priv;
241 static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
243 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
245 return (struct ocfs2_dentry_lock *)lockres->l_priv;
248 static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
250 if (lockres->l_ops->get_osb)
251 return lockres->l_ops->get_osb(lockres);
253 return (struct ocfs2_super *)lockres->l_priv;
256 static int ocfs2_lock_create(struct ocfs2_super *osb,
257 struct ocfs2_lock_res *lockres,
260 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
262 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
263 struct ocfs2_lock_res *lockres,
265 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
266 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
267 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
268 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
269 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
270 struct ocfs2_lock_res *lockres);
271 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
273 #define ocfs2_log_dlm_error(_func, _stat, _lockres) do { \
274 mlog(ML_ERROR, "Dlm error \"%s\" while calling %s on " \
275 "resource %s: %s\n", dlm_errname(_stat), _func, \
276 _lockres->l_name, dlm_errmsg(_stat)); \
278 static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
279 struct ocfs2_lock_res *lockres);
280 static int ocfs2_meta_lock_update(struct inode *inode,
281 struct buffer_head **bh);
282 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
283 static inline int ocfs2_highest_compat_lock_level(int level);
285 static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
294 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
296 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
297 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
298 (long long)blkno, generation);
300 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
302 mlog(0, "built lock resource with name: %s\n", name);
307 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
309 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
310 struct ocfs2_dlm_debug *dlm_debug)
312 mlog(0, "Add tracking for lockres %s\n", res->l_name);
314 spin_lock(&ocfs2_dlm_tracking_lock);
315 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
316 spin_unlock(&ocfs2_dlm_tracking_lock);
319 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
321 spin_lock(&ocfs2_dlm_tracking_lock);
322 if (!list_empty(&res->l_debug_list))
323 list_del_init(&res->l_debug_list);
324 spin_unlock(&ocfs2_dlm_tracking_lock);
327 static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
328 struct ocfs2_lock_res *res,
329 enum ocfs2_lock_type type,
330 struct ocfs2_lock_res_ops *ops,
337 res->l_level = LKM_IVMODE;
338 res->l_requested = LKM_IVMODE;
339 res->l_blocking = LKM_IVMODE;
340 res->l_action = OCFS2_AST_INVALID;
341 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
343 res->l_flags = OCFS2_LOCK_INITIALIZED;
345 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
348 void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
350 /* This also clears out the lock status block */
351 memset(res, 0, sizeof(struct ocfs2_lock_res));
352 spin_lock_init(&res->l_lock);
353 init_waitqueue_head(&res->l_event);
354 INIT_LIST_HEAD(&res->l_blocked_list);
355 INIT_LIST_HEAD(&res->l_mask_waiters);
358 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
359 enum ocfs2_lock_type type,
360 unsigned int generation,
363 struct ocfs2_lock_res_ops *ops;
366 case OCFS2_LOCK_TYPE_RW:
367 ops = &ocfs2_inode_rw_lops;
369 case OCFS2_LOCK_TYPE_META:
370 ops = &ocfs2_inode_meta_lops;
372 case OCFS2_LOCK_TYPE_DATA:
373 ops = &ocfs2_inode_data_lops;
376 mlog_bug_on_msg(1, "type: %d\n", type);
377 ops = NULL; /* thanks, gcc */
381 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
382 generation, res->l_name);
383 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
386 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
388 struct inode *inode = ocfs2_lock_res_inode(lockres);
390 return OCFS2_SB(inode->i_sb);
393 static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
395 __be64 inode_blkno_be;
397 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
400 return be64_to_cpu(inode_blkno_be);
403 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
405 struct ocfs2_dentry_lock *dl = lockres->l_priv;
407 return OCFS2_SB(dl->dl_inode->i_sb);
410 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
411 u64 parent, struct inode *inode)
414 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
415 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
416 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
418 ocfs2_lock_res_init_once(lockres);
421 * Unfortunately, the standard lock naming scheme won't work
422 * here because we have two 16 byte values to use. Instead,
423 * we'll stuff the inode number as a binary value. We still
424 * want error prints to show something without garbling the
425 * display, so drop a null byte in there before the inode
426 * number. A future version of OCFS2 will likely use all
427 * binary lock names. The stringified names have been a
428 * tremendous aid in debugging, but now that the debugfs
429 * interface exists, we can mangle things there if need be.
431 * NOTE: We also drop the standard "pad" value (the total lock
432 * name size stays the same though - the last part is all
433 * zeros due to the memset in ocfs2_lock_res_init_once()
435 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
437 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
440 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
442 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
445 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
446 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
450 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
451 struct ocfs2_super *osb)
453 /* Superblock lockres doesn't come from a slab so we call init
454 * once on it manually. */
455 ocfs2_lock_res_init_once(res);
456 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
458 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
459 &ocfs2_super_lops, osb);
462 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
463 struct ocfs2_super *osb)
465 /* Rename lockres doesn't come from a slab so we call init
466 * once on it manually. */
467 ocfs2_lock_res_init_once(res);
468 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
469 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
470 &ocfs2_rename_lops, osb);
473 void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
477 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
480 ocfs2_remove_lockres_tracking(res);
482 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
483 "Lockres %s is on the blocked list\n",
485 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
486 "Lockres %s has mask waiters pending\n",
488 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
489 "Lockres %s is locked\n",
491 mlog_bug_on_msg(res->l_ro_holders,
492 "Lockres %s has %u ro holders\n",
493 res->l_name, res->l_ro_holders);
494 mlog_bug_on_msg(res->l_ex_holders,
495 "Lockres %s has %u ex holders\n",
496 res->l_name, res->l_ex_holders);
498 /* Need to clear out the lock status block for the dlm */
499 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
505 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
514 lockres->l_ex_holders++;
517 lockres->l_ro_holders++;
526 static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
535 BUG_ON(!lockres->l_ex_holders);
536 lockres->l_ex_holders--;
539 BUG_ON(!lockres->l_ro_holders);
540 lockres->l_ro_holders--;
548 /* WARNING: This function lives in a world where the only three lock
549 * levels are EX, PR, and NL. It *will* have to be adjusted when more
550 * lock types are added. */
551 static inline int ocfs2_highest_compat_lock_level(int level)
553 int new_level = LKM_EXMODE;
555 if (level == LKM_EXMODE)
556 new_level = LKM_NLMODE;
557 else if (level == LKM_PRMODE)
558 new_level = LKM_PRMODE;
562 static void lockres_set_flags(struct ocfs2_lock_res *lockres,
563 unsigned long newflags)
565 struct list_head *pos, *tmp;
566 struct ocfs2_mask_waiter *mw;
568 assert_spin_locked(&lockres->l_lock);
570 lockres->l_flags = newflags;
572 list_for_each_safe(pos, tmp, &lockres->l_mask_waiters) {
573 mw = list_entry(pos, struct ocfs2_mask_waiter, mw_item);
574 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
577 list_del_init(&mw->mw_item);
579 complete(&mw->mw_complete);
582 static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
584 lockres_set_flags(lockres, lockres->l_flags | or);
586 static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
589 lockres_set_flags(lockres, lockres->l_flags & ~clear);
592 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
596 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
597 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
598 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
599 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
601 lockres->l_level = lockres->l_requested;
602 if (lockres->l_level <=
603 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
604 lockres->l_blocking = LKM_NLMODE;
605 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
607 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
612 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
616 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
617 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
619 /* Convert from RO to EX doesn't really need anything as our
620 * information is already up to data. Convert from NL to
621 * *anything* however should mark ourselves as needing an
623 if (lockres->l_level == LKM_NLMODE &&
624 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
625 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
627 lockres->l_level = lockres->l_requested;
628 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
633 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
637 BUG_ON((!lockres->l_flags & OCFS2_LOCK_BUSY));
638 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
640 if (lockres->l_requested > LKM_NLMODE &&
641 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
642 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
643 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
645 lockres->l_level = lockres->l_requested;
646 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
647 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
652 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
655 int needs_downconvert = 0;
658 assert_spin_locked(&lockres->l_lock);
660 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
662 if (level > lockres->l_blocking) {
663 /* only schedule a downconvert if we haven't already scheduled
664 * one that goes low enough to satisfy the level we're
665 * blocking. this also catches the case where we get
667 if (ocfs2_highest_compat_lock_level(level) <
668 ocfs2_highest_compat_lock_level(lockres->l_blocking))
669 needs_downconvert = 1;
671 lockres->l_blocking = level;
674 mlog_exit(needs_downconvert);
675 return needs_downconvert;
678 static void ocfs2_blocking_ast(void *opaque, int level)
680 struct ocfs2_lock_res *lockres = opaque;
681 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
682 int needs_downconvert;
685 BUG_ON(level <= LKM_NLMODE);
687 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
688 lockres->l_name, level, lockres->l_level,
689 ocfs2_lock_type_string(lockres->l_type));
691 spin_lock_irqsave(&lockres->l_lock, flags);
692 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
693 if (needs_downconvert)
694 ocfs2_schedule_blocked_lock(osb, lockres);
695 spin_unlock_irqrestore(&lockres->l_lock, flags);
697 wake_up(&lockres->l_event);
699 ocfs2_kick_vote_thread(osb);
702 static void ocfs2_locking_ast(void *opaque)
704 struct ocfs2_lock_res *lockres = opaque;
705 struct dlm_lockstatus *lksb = &lockres->l_lksb;
708 spin_lock_irqsave(&lockres->l_lock, flags);
710 if (lksb->status != DLM_NORMAL) {
711 mlog(ML_ERROR, "lockres %s: lksb status value of %u!\n",
712 lockres->l_name, lksb->status);
713 spin_unlock_irqrestore(&lockres->l_lock, flags);
717 switch(lockres->l_action) {
718 case OCFS2_AST_ATTACH:
719 ocfs2_generic_handle_attach_action(lockres);
720 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
722 case OCFS2_AST_CONVERT:
723 ocfs2_generic_handle_convert_action(lockres);
725 case OCFS2_AST_DOWNCONVERT:
726 ocfs2_generic_handle_downconvert_action(lockres);
729 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
730 "lockres flags = 0x%lx, unlock action: %u\n",
731 lockres->l_name, lockres->l_action, lockres->l_flags,
732 lockres->l_unlock_action);
736 /* set it to something invalid so if we get called again we
738 lockres->l_action = OCFS2_AST_INVALID;
740 wake_up(&lockres->l_event);
741 spin_unlock_irqrestore(&lockres->l_lock, flags);
744 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
750 spin_lock_irqsave(&lockres->l_lock, flags);
751 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
753 lockres->l_action = OCFS2_AST_INVALID;
755 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
756 spin_unlock_irqrestore(&lockres->l_lock, flags);
758 wake_up(&lockres->l_event);
762 /* Note: If we detect another process working on the lock (i.e.,
763 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
764 * to do the right thing in that case.
766 static int ocfs2_lock_create(struct ocfs2_super *osb,
767 struct ocfs2_lock_res *lockres,
772 enum dlm_status status;
777 mlog(0, "lock %s, level = %d, flags = %d\n", lockres->l_name, level,
780 spin_lock_irqsave(&lockres->l_lock, flags);
781 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
782 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
783 spin_unlock_irqrestore(&lockres->l_lock, flags);
787 lockres->l_action = OCFS2_AST_ATTACH;
788 lockres->l_requested = level;
789 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
790 spin_unlock_irqrestore(&lockres->l_lock, flags);
792 status = dlmlock(osb->dlm,
797 OCFS2_LOCK_ID_MAX_LEN - 1,
801 if (status != DLM_NORMAL) {
802 ocfs2_log_dlm_error("dlmlock", status, lockres);
804 ocfs2_recover_from_dlm_error(lockres, 1);
807 mlog(0, "lock %s, successfull return from dlmlock\n", lockres->l_name);
814 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
820 spin_lock_irqsave(&lockres->l_lock, flags);
821 ret = lockres->l_flags & flag;
822 spin_unlock_irqrestore(&lockres->l_lock, flags);
827 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
830 wait_event(lockres->l_event,
831 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
834 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
837 wait_event(lockres->l_event,
838 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
841 /* predict what lock level we'll be dropping down to on behalf
842 * of another node, and return true if the currently wanted
843 * level will be compatible with it. */
844 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
847 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
849 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
852 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
854 INIT_LIST_HEAD(&mw->mw_item);
855 init_completion(&mw->mw_complete);
858 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
860 wait_for_completion(&mw->mw_complete);
861 /* Re-arm the completion in case we want to wait on it again */
862 INIT_COMPLETION(mw->mw_complete);
863 return mw->mw_status;
866 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
867 struct ocfs2_mask_waiter *mw,
871 BUG_ON(!list_empty(&mw->mw_item));
873 assert_spin_locked(&lockres->l_lock);
875 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
880 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
881 * if the mask still hadn't reached its goal */
882 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
883 struct ocfs2_mask_waiter *mw)
888 spin_lock_irqsave(&lockres->l_lock, flags);
889 if (!list_empty(&mw->mw_item)) {
890 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
893 list_del_init(&mw->mw_item);
894 init_completion(&mw->mw_complete);
896 spin_unlock_irqrestore(&lockres->l_lock, flags);
902 static int ocfs2_cluster_lock(struct ocfs2_super *osb,
903 struct ocfs2_lock_res *lockres,
908 struct ocfs2_mask_waiter mw;
909 enum dlm_status status;
910 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
911 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
916 ocfs2_init_mask_waiter(&mw);
918 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
919 lkm_flags |= LKM_VALBLK;
924 if (catch_signals && signal_pending(current)) {
929 spin_lock_irqsave(&lockres->l_lock, flags);
931 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
932 "Cluster lock called on freeing lockres %s! flags "
933 "0x%lx\n", lockres->l_name, lockres->l_flags);
935 /* We only compare against the currently granted level
936 * here. If the lock is blocked waiting on a downconvert,
937 * we'll get caught below. */
938 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
939 level > lockres->l_level) {
940 /* is someone sitting in dlm_lock? If so, wait on
942 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
947 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
948 /* lock has not been created yet. */
949 spin_unlock_irqrestore(&lockres->l_lock, flags);
951 ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
959 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
960 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
961 /* is the lock is currently blocked on behalf of
963 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
968 if (level > lockres->l_level) {
969 if (lockres->l_action != OCFS2_AST_INVALID)
970 mlog(ML_ERROR, "lockres %s has action %u pending\n",
971 lockres->l_name, lockres->l_action);
973 lockres->l_action = OCFS2_AST_CONVERT;
974 lockres->l_requested = level;
975 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
976 spin_unlock_irqrestore(&lockres->l_lock, flags);
978 BUG_ON(level == LKM_IVMODE);
979 BUG_ON(level == LKM_NLMODE);
981 mlog(0, "lock %s, convert from %d to level = %d\n",
982 lockres->l_name, lockres->l_level, level);
984 /* call dlm_lock to upgrade lock now */
985 status = dlmlock(osb->dlm,
988 lkm_flags|LKM_CONVERT,
990 OCFS2_LOCK_ID_MAX_LEN - 1,
994 if (status != DLM_NORMAL) {
995 if ((lkm_flags & LKM_NOQUEUE) &&
996 (status == DLM_NOTQUEUED))
999 ocfs2_log_dlm_error("dlmlock", status,
1003 ocfs2_recover_from_dlm_error(lockres, 1);
1007 mlog(0, "lock %s, successfull return from dlmlock\n",
1010 /* At this point we've gone inside the dlm and need to
1011 * complete our work regardless. */
1014 /* wait for busy to clear and carry on */
1018 /* Ok, if we get here then we're good to go. */
1019 ocfs2_inc_holders(lockres, level);
1023 spin_unlock_irqrestore(&lockres->l_lock, flags);
1026 * This is helping work around a lock inversion between the page lock
1027 * and dlm locks. One path holds the page lock while calling aops
1028 * which block acquiring dlm locks. The voting thread holds dlm
1029 * locks while acquiring page locks while down converting data locks.
1030 * This block is helping an aop path notice the inversion and back
1031 * off to unlock its page lock before trying the dlm lock again.
1033 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1034 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1036 if (lockres_remove_mask_waiter(lockres, &mw))
1042 ret = ocfs2_wait_for_mask(&mw);
1052 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1053 struct ocfs2_lock_res *lockres,
1056 unsigned long flags;
1059 spin_lock_irqsave(&lockres->l_lock, flags);
1060 ocfs2_dec_holders(lockres, level);
1061 ocfs2_vote_on_unlock(osb, lockres);
1062 spin_unlock_irqrestore(&lockres->l_lock, flags);
1066 int ocfs2_create_new_lock(struct ocfs2_super *osb,
1067 struct ocfs2_lock_res *lockres,
1071 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1072 unsigned long flags;
1073 int lkm_flags = local ? LKM_LOCAL : 0;
1075 spin_lock_irqsave(&lockres->l_lock, flags);
1076 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1077 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1078 spin_unlock_irqrestore(&lockres->l_lock, flags);
1080 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
1083 /* Grants us an EX lock on the data and metadata resources, skipping
1084 * the normal cluster directory lookup. Use this ONLY on newly created
1085 * inodes which other nodes can't possibly see, and which haven't been
1086 * hashed in the inode hash yet. This can give us a good performance
1087 * increase as it'll skip the network broadcast normally associated
1088 * with creating a new lock resource. */
1089 int ocfs2_create_new_inode_locks(struct inode *inode)
1092 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1095 BUG_ON(!ocfs2_inode_is_new(inode));
1099 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1101 /* NOTE: That we don't increment any of the holder counts, nor
1102 * do we add anything to a journal handle. Since this is
1103 * supposed to be a new inode which the cluster doesn't know
1104 * about yet, there is no need to. As far as the LVB handling
1105 * is concerned, this is basically like acquiring an EX lock
1106 * on a resource which has an invalid one -- we'll set it
1107 * valid when we release the EX. */
1109 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
1116 * We don't want to use LKM_LOCAL on a meta data lock as they
1117 * don't use a generation in their lock names.
1119 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_meta_lockres, 1, 0);
1125 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_data_lockres, 1, 1);
1136 int ocfs2_rw_lock(struct inode *inode, int write)
1139 struct ocfs2_lock_res *lockres;
1145 mlog(0, "inode %llu take %s RW lock\n",
1146 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1147 write ? "EXMODE" : "PRMODE");
1149 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1151 level = write ? LKM_EXMODE : LKM_PRMODE;
1153 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1162 void ocfs2_rw_unlock(struct inode *inode, int write)
1164 int level = write ? LKM_EXMODE : LKM_PRMODE;
1165 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1169 mlog(0, "inode %llu drop %s RW lock\n",
1170 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1171 write ? "EXMODE" : "PRMODE");
1173 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1178 int ocfs2_data_lock_full(struct inode *inode,
1182 int status = 0, level;
1183 struct ocfs2_lock_res *lockres;
1189 mlog(0, "inode %llu take %s DATA lock\n",
1190 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1191 write ? "EXMODE" : "PRMODE");
1193 /* We'll allow faking a readonly data lock for
1195 if (ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb))) {
1203 lockres = &OCFS2_I(inode)->ip_data_lockres;
1205 level = write ? LKM_EXMODE : LKM_PRMODE;
1207 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level,
1209 if (status < 0 && status != -EAGAIN)
1217 /* see ocfs2_meta_lock_with_page() */
1218 int ocfs2_data_lock_with_page(struct inode *inode,
1224 ret = ocfs2_data_lock_full(inode, write, OCFS2_LOCK_NONBLOCK);
1225 if (ret == -EAGAIN) {
1227 if (ocfs2_data_lock(inode, write) == 0)
1228 ocfs2_data_unlock(inode, write);
1229 ret = AOP_TRUNCATED_PAGE;
1235 static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
1236 struct ocfs2_lock_res *lockres)
1242 /* If we know that another node is waiting on our lock, kick
1243 * the vote thread * pre-emptively when we reach a release
1245 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1246 switch(lockres->l_blocking) {
1248 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1252 if (!lockres->l_ex_holders)
1261 ocfs2_kick_vote_thread(osb);
1266 void ocfs2_data_unlock(struct inode *inode,
1269 int level = write ? LKM_EXMODE : LKM_PRMODE;
1270 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_data_lockres;
1274 mlog(0, "inode %llu drop %s DATA lock\n",
1275 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1276 write ? "EXMODE" : "PRMODE");
1278 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1279 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1284 #define OCFS2_SEC_BITS 34
1285 #define OCFS2_SEC_SHIFT (64 - 34)
1286 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1288 /* LVB only has room for 64 bits of time here so we pack it for
1290 static u64 ocfs2_pack_timespec(struct timespec *spec)
1293 u64 sec = spec->tv_sec;
1294 u32 nsec = spec->tv_nsec;
1296 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1301 /* Call this with the lockres locked. I am reasonably sure we don't
1302 * need ip_lock in this function as anyone who would be changing those
1303 * values is supposed to be blocked in ocfs2_meta_lock right now. */
1304 static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1306 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1307 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1308 struct ocfs2_meta_lvb *lvb;
1312 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1315 * Invalidate the LVB of a deleted inode - this way other
1316 * nodes are forced to go to disk and discover the new inode
1319 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1320 lvb->lvb_version = 0;
1324 lvb->lvb_version = OCFS2_LVB_VERSION;
1325 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1326 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1327 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1328 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1329 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1330 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1331 lvb->lvb_iatime_packed =
1332 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1333 lvb->lvb_ictime_packed =
1334 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1335 lvb->lvb_imtime_packed =
1336 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1337 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
1338 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
1341 mlog_meta_lvb(0, lockres);
1346 static void ocfs2_unpack_timespec(struct timespec *spec,
1349 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1350 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1353 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1355 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1356 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1357 struct ocfs2_meta_lvb *lvb;
1361 mlog_meta_lvb(0, lockres);
1363 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1365 /* We're safe here without the lockres lock... */
1366 spin_lock(&oi->ip_lock);
1367 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1368 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1370 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1371 ocfs2_set_inode_flags(inode);
1373 /* fast-symlinks are a special case */
1374 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1375 inode->i_blocks = 0;
1378 ocfs2_align_bytes_to_sectors(i_size_read(inode));
1380 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1381 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1382 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1383 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1384 ocfs2_unpack_timespec(&inode->i_atime,
1385 be64_to_cpu(lvb->lvb_iatime_packed));
1386 ocfs2_unpack_timespec(&inode->i_mtime,
1387 be64_to_cpu(lvb->lvb_imtime_packed));
1388 ocfs2_unpack_timespec(&inode->i_ctime,
1389 be64_to_cpu(lvb->lvb_ictime_packed));
1390 spin_unlock(&oi->ip_lock);
1395 static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1396 struct ocfs2_lock_res *lockres)
1398 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1400 if (lvb->lvb_version == OCFS2_LVB_VERSION
1401 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
1406 /* Determine whether a lock resource needs to be refreshed, and
1407 * arbitrate who gets to refresh it.
1409 * 0 means no refresh needed.
1411 * > 0 means you need to refresh this and you MUST call
1412 * ocfs2_complete_lock_res_refresh afterwards. */
1413 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1415 unsigned long flags;
1421 spin_lock_irqsave(&lockres->l_lock, flags);
1422 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1423 spin_unlock_irqrestore(&lockres->l_lock, flags);
1427 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1428 spin_unlock_irqrestore(&lockres->l_lock, flags);
1430 ocfs2_wait_on_refreshing_lock(lockres);
1434 /* Ok, I'll be the one to refresh this lock. */
1435 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1436 spin_unlock_irqrestore(&lockres->l_lock, flags);
1444 /* If status is non zero, I'll mark it as not being in refresh
1445 * anymroe, but i won't clear the needs refresh flag. */
1446 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1449 unsigned long flags;
1452 spin_lock_irqsave(&lockres->l_lock, flags);
1453 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1455 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1456 spin_unlock_irqrestore(&lockres->l_lock, flags);
1458 wake_up(&lockres->l_event);
1463 /* may or may not return a bh if it went to disk. */
1464 static int ocfs2_meta_lock_update(struct inode *inode,
1465 struct buffer_head **bh)
1468 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1469 struct ocfs2_lock_res *lockres;
1470 struct ocfs2_dinode *fe;
1474 spin_lock(&oi->ip_lock);
1475 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1476 mlog(0, "Orphaned inode %llu was deleted while we "
1477 "were waiting on a lock. ip_flags = 0x%x\n",
1478 (unsigned long long)oi->ip_blkno, oi->ip_flags);
1479 spin_unlock(&oi->ip_lock);
1483 spin_unlock(&oi->ip_lock);
1485 lockres = &oi->ip_meta_lockres;
1487 if (!ocfs2_should_refresh_lock_res(lockres))
1490 /* This will discard any caching information we might have had
1491 * for the inode metadata. */
1492 ocfs2_metadata_cache_purge(inode);
1494 /* will do nothing for inode types that don't use the extent
1495 * map (directories, bitmap files, etc) */
1496 ocfs2_extent_map_trunc(inode, 0);
1498 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
1499 mlog(0, "Trusting LVB on inode %llu\n",
1500 (unsigned long long)oi->ip_blkno);
1501 ocfs2_refresh_inode_from_lvb(inode);
1503 /* Boo, we have to go to disk. */
1504 /* read bh, cast, ocfs2_refresh_inode */
1505 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1506 bh, OCFS2_BH_CACHED, inode);
1511 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1513 /* This is a good chance to make sure we're not
1514 * locking an invalid object.
1516 * We bug on a stale inode here because we checked
1517 * above whether it was wiped from disk. The wiping
1518 * node provides a guarantee that we receive that
1519 * message and can mark the inode before dropping any
1520 * locks associated with it. */
1521 if (!OCFS2_IS_VALID_DINODE(fe)) {
1522 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1526 mlog_bug_on_msg(inode->i_generation !=
1527 le32_to_cpu(fe->i_generation),
1528 "Invalid dinode %llu disk generation: %u "
1529 "inode->i_generation: %u\n",
1530 (unsigned long long)oi->ip_blkno,
1531 le32_to_cpu(fe->i_generation),
1532 inode->i_generation);
1533 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1534 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
1535 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1536 (unsigned long long)oi->ip_blkno,
1537 (unsigned long long)le64_to_cpu(fe->i_dtime),
1538 le32_to_cpu(fe->i_flags));
1540 ocfs2_refresh_inode(inode, fe);
1545 ocfs2_complete_lock_res_refresh(lockres, status);
1551 static int ocfs2_assign_bh(struct inode *inode,
1552 struct buffer_head **ret_bh,
1553 struct buffer_head *passed_bh)
1558 /* Ok, the update went to disk for us, use the
1560 *ret_bh = passed_bh;
1566 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1567 OCFS2_I(inode)->ip_blkno,
1578 * returns < 0 error if the callback will never be called, otherwise
1579 * the result of the lock will be communicated via the callback.
1581 int ocfs2_meta_lock_full(struct inode *inode,
1582 struct ocfs2_journal_handle *handle,
1583 struct buffer_head **ret_bh,
1587 int status, level, dlm_flags, acquired;
1588 struct ocfs2_lock_res *lockres;
1589 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1590 struct buffer_head *local_bh = NULL;
1596 mlog(0, "inode %llu, take %s META lock\n",
1597 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1598 ex ? "EXMODE" : "PRMODE");
1602 /* We'll allow faking a readonly metadata lock for
1604 if (ocfs2_is_hard_readonly(osb)) {
1610 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1611 wait_event(osb->recovery_event,
1612 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1615 lockres = &OCFS2_I(inode)->ip_meta_lockres;
1616 level = ex ? LKM_EXMODE : LKM_PRMODE;
1618 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
1619 dlm_flags |= LKM_NOQUEUE;
1621 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1623 if (status != -EAGAIN && status != -EIOCBRETRY)
1628 /* Notify the error cleanup path to drop the cluster lock. */
1631 /* We wait twice because a node may have died while we were in
1632 * the lower dlm layers. The second time though, we've
1633 * committed to owning this lock so we don't allow signals to
1634 * abort the operation. */
1635 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1636 wait_event(osb->recovery_event,
1637 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1640 * We only see this flag if we're being called from
1641 * ocfs2_read_locked_inode(). It means we're locking an inode
1642 * which hasn't been populated yet, so clear the refresh flag
1643 * and let the caller handle it.
1645 if (inode->i_state & I_NEW) {
1647 ocfs2_complete_lock_res_refresh(lockres, 0);
1651 /* This is fun. The caller may want a bh back, or it may
1652 * not. ocfs2_meta_lock_update definitely wants one in, but
1653 * may or may not read one, depending on what's in the
1654 * LVB. The result of all of this is that we've *only* gone to
1655 * disk if we have to, so the complexity is worthwhile. */
1656 status = ocfs2_meta_lock_update(inode, &local_bh);
1658 if (status != -ENOENT)
1664 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
1672 status = ocfs2_handle_add_lock(handle, inode);
1679 if (ret_bh && (*ret_bh)) {
1684 ocfs2_meta_unlock(inode, ex);
1695 * This is working around a lock inversion between tasks acquiring DLM locks
1696 * while holding a page lock and the vote thread which blocks dlm lock acquiry
1697 * while acquiring page locks.
1699 * ** These _with_page variantes are only intended to be called from aop
1700 * methods that hold page locks and return a very specific *positive* error
1701 * code that aop methods pass up to the VFS -- test for errors with != 0. **
1703 * The DLM is called such that it returns -EAGAIN if it would have blocked
1704 * waiting for the vote thread. In that case we unlock our page so the vote
1705 * thread can make progress. Once we've done this we have to return
1706 * AOP_TRUNCATED_PAGE so the aop method that called us can bubble that back up
1707 * into the VFS who will then immediately retry the aop call.
1709 * We do a blocking lock and immediate unlock before returning, though, so that
1710 * the lock has a great chance of being cached on this node by the time the VFS
1711 * calls back to retry the aop. This has a potential to livelock as nodes
1712 * ping locks back and forth, but that's a risk we're willing to take to avoid
1713 * the lock inversion simply.
1715 int ocfs2_meta_lock_with_page(struct inode *inode,
1716 struct ocfs2_journal_handle *handle,
1717 struct buffer_head **ret_bh,
1723 ret = ocfs2_meta_lock_full(inode, handle, ret_bh, ex,
1724 OCFS2_LOCK_NONBLOCK);
1725 if (ret == -EAGAIN) {
1727 if (ocfs2_meta_lock(inode, handle, ret_bh, ex) == 0)
1728 ocfs2_meta_unlock(inode, ex);
1729 ret = AOP_TRUNCATED_PAGE;
1735 void ocfs2_meta_unlock(struct inode *inode,
1738 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1739 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
1743 mlog(0, "inode %llu drop %s META lock\n",
1744 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1745 ex ? "EXMODE" : "PRMODE");
1747 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1748 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1753 int ocfs2_super_lock(struct ocfs2_super *osb,
1757 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1758 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1759 struct buffer_head *bh;
1760 struct ocfs2_slot_info *si = osb->slot_info;
1764 if (ocfs2_is_hard_readonly(osb))
1767 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
1773 /* The super block lock path is really in the best position to
1774 * know when resources covered by the lock need to be
1775 * refreshed, so we do it here. Of course, making sense of
1776 * everything is up to the caller :) */
1777 status = ocfs2_should_refresh_lock_res(lockres);
1784 status = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0,
1787 ocfs2_update_slot_info(si);
1789 ocfs2_complete_lock_res_refresh(lockres, status);
1799 void ocfs2_super_unlock(struct ocfs2_super *osb,
1802 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1803 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1805 ocfs2_cluster_unlock(osb, lockres, level);
1808 int ocfs2_rename_lock(struct ocfs2_super *osb)
1811 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1813 if (ocfs2_is_hard_readonly(osb))
1816 status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
1823 void ocfs2_rename_unlock(struct ocfs2_super *osb)
1825 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1827 ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
1830 int ocfs2_dentry_lock(struct dentry *dentry, int ex)
1833 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1834 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1835 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1839 if (ocfs2_is_hard_readonly(osb))
1842 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
1849 void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
1851 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1852 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1853 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1855 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
1858 /* Reference counting of the dlm debug structure. We want this because
1859 * open references on the debug inodes can live on after a mount, so
1860 * we can't rely on the ocfs2_super to always exist. */
1861 static void ocfs2_dlm_debug_free(struct kref *kref)
1863 struct ocfs2_dlm_debug *dlm_debug;
1865 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
1870 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
1873 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
1876 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
1878 kref_get(&debug->d_refcnt);
1881 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
1883 struct ocfs2_dlm_debug *dlm_debug;
1885 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
1887 mlog_errno(-ENOMEM);
1891 kref_init(&dlm_debug->d_refcnt);
1892 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
1893 dlm_debug->d_locking_state = NULL;
1898 /* Access to this is arbitrated for us via seq_file->sem. */
1899 struct ocfs2_dlm_seq_priv {
1900 struct ocfs2_dlm_debug *p_dlm_debug;
1901 struct ocfs2_lock_res p_iter_res;
1902 struct ocfs2_lock_res p_tmp_res;
1905 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
1906 struct ocfs2_dlm_seq_priv *priv)
1908 struct ocfs2_lock_res *iter, *ret = NULL;
1909 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
1911 assert_spin_locked(&ocfs2_dlm_tracking_lock);
1913 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
1914 /* discover the head of the list */
1915 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
1916 mlog(0, "End of list found, %p\n", ret);
1920 /* We track our "dummy" iteration lockres' by a NULL
1922 if (iter->l_ops != NULL) {
1931 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
1933 struct ocfs2_dlm_seq_priv *priv = m->private;
1934 struct ocfs2_lock_res *iter;
1936 spin_lock(&ocfs2_dlm_tracking_lock);
1937 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
1939 /* Since lockres' have the lifetime of their container
1940 * (which can be inodes, ocfs2_supers, etc) we want to
1941 * copy this out to a temporary lockres while still
1942 * under the spinlock. Obviously after this we can't
1943 * trust any pointers on the copy returned, but that's
1944 * ok as the information we want isn't typically held
1946 priv->p_tmp_res = *iter;
1947 iter = &priv->p_tmp_res;
1949 spin_unlock(&ocfs2_dlm_tracking_lock);
1954 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
1958 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
1960 struct ocfs2_dlm_seq_priv *priv = m->private;
1961 struct ocfs2_lock_res *iter = v;
1962 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
1964 spin_lock(&ocfs2_dlm_tracking_lock);
1965 iter = ocfs2_dlm_next_res(iter, priv);
1966 list_del_init(&dummy->l_debug_list);
1968 list_add(&dummy->l_debug_list, &iter->l_debug_list);
1969 priv->p_tmp_res = *iter;
1970 iter = &priv->p_tmp_res;
1972 spin_unlock(&ocfs2_dlm_tracking_lock);
1977 /* So that debugfs.ocfs2 can determine which format is being used */
1978 #define OCFS2_DLM_DEBUG_STR_VERSION 1
1979 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
1983 struct ocfs2_lock_res *lockres = v;
1988 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
1990 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
1991 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
1993 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
1995 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
1997 seq_printf(m, "%d\t"
2008 lockres->l_unlock_action,
2009 lockres->l_ro_holders,
2010 lockres->l_ex_holders,
2011 lockres->l_requested,
2012 lockres->l_blocking);
2014 /* Dump the raw LVB */
2015 lvb = lockres->l_lksb.lvb;
2016 for(i = 0; i < DLM_LVB_LEN; i++)
2017 seq_printf(m, "0x%x\t", lvb[i]);
2020 seq_printf(m, "\n");
2024 static struct seq_operations ocfs2_dlm_seq_ops = {
2025 .start = ocfs2_dlm_seq_start,
2026 .stop = ocfs2_dlm_seq_stop,
2027 .next = ocfs2_dlm_seq_next,
2028 .show = ocfs2_dlm_seq_show,
2031 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2033 struct seq_file *seq = (struct seq_file *) file->private_data;
2034 struct ocfs2_dlm_seq_priv *priv = seq->private;
2035 struct ocfs2_lock_res *res = &priv->p_iter_res;
2037 ocfs2_remove_lockres_tracking(res);
2038 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2039 return seq_release_private(inode, file);
2042 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2045 struct ocfs2_dlm_seq_priv *priv;
2046 struct seq_file *seq;
2047 struct ocfs2_super *osb;
2049 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2055 osb = inode->i_private;
2056 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2057 priv->p_dlm_debug = osb->osb_dlm_debug;
2058 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2060 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2067 seq = (struct seq_file *) file->private_data;
2068 seq->private = priv;
2070 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2077 static const struct file_operations ocfs2_dlm_debug_fops = {
2078 .open = ocfs2_dlm_debug_open,
2079 .release = ocfs2_dlm_debug_release,
2081 .llseek = seq_lseek,
2084 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2087 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2089 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2091 osb->osb_debug_root,
2093 &ocfs2_dlm_debug_fops);
2094 if (!dlm_debug->d_locking_state) {
2097 "Unable to create locking state debugfs file.\n");
2101 ocfs2_get_dlm_debug(dlm_debug);
2106 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2108 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2111 debugfs_remove(dlm_debug->d_locking_state);
2112 ocfs2_put_dlm_debug(dlm_debug);
2116 int ocfs2_dlm_init(struct ocfs2_super *osb)
2120 struct dlm_ctxt *dlm;
2124 status = ocfs2_dlm_init_debug(osb);
2130 /* launch vote thread */
2131 osb->vote_task = kthread_run(ocfs2_vote_thread, osb, "ocfs2vote");
2132 if (IS_ERR(osb->vote_task)) {
2133 status = PTR_ERR(osb->vote_task);
2134 osb->vote_task = NULL;
2139 /* used by the dlm code to make message headers unique, each
2140 * node in this domain must agree on this. */
2141 dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
2143 /* for now, uuid == domain */
2144 dlm = dlm_register_domain(osb->uuid_str, dlm_key);
2146 status = PTR_ERR(dlm);
2151 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2152 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2154 dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
2161 ocfs2_dlm_shutdown_debug(osb);
2163 kthread_stop(osb->vote_task);
2170 void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2174 dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
2176 ocfs2_drop_osb_locks(osb);
2178 if (osb->vote_task) {
2179 kthread_stop(osb->vote_task);
2180 osb->vote_task = NULL;
2183 ocfs2_lock_res_free(&osb->osb_super_lockres);
2184 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2186 dlm_unregister_domain(osb->dlm);
2189 ocfs2_dlm_shutdown_debug(osb);
2194 static void ocfs2_unlock_ast(void *opaque, enum dlm_status status)
2196 struct ocfs2_lock_res *lockres = opaque;
2197 unsigned long flags;
2201 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2202 lockres->l_unlock_action);
2204 spin_lock_irqsave(&lockres->l_lock, flags);
2205 /* We tried to cancel a convert request, but it was already
2206 * granted. All we want to do here is clear our unlock
2207 * state. The wake_up call done at the bottom is redundant
2208 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2209 * hurt anything anyway */
2210 if (status == DLM_CANCELGRANT &&
2211 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2212 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2214 /* We don't clear the busy flag in this case as it
2215 * should have been cleared by the ast which the dlm
2217 goto complete_unlock;
2220 if (status != DLM_NORMAL) {
2221 mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
2222 "unlock_action %d\n", status, lockres->l_name,
2223 lockres->l_unlock_action);
2224 spin_unlock_irqrestore(&lockres->l_lock, flags);
2228 switch(lockres->l_unlock_action) {
2229 case OCFS2_UNLOCK_CANCEL_CONVERT:
2230 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2231 lockres->l_action = OCFS2_AST_INVALID;
2233 case OCFS2_UNLOCK_DROP_LOCK:
2234 lockres->l_level = LKM_IVMODE;
2240 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2242 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2243 spin_unlock_irqrestore(&lockres->l_lock, flags);
2245 wake_up(&lockres->l_event);
2250 static int ocfs2_drop_lock(struct ocfs2_super *osb,
2251 struct ocfs2_lock_res *lockres)
2253 enum dlm_status status;
2254 unsigned long flags;
2257 /* We didn't get anywhere near actually using this lockres. */
2258 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2261 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
2262 lkm_flags |= LKM_VALBLK;
2264 spin_lock_irqsave(&lockres->l_lock, flags);
2266 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2267 "lockres %s, flags 0x%lx\n",
2268 lockres->l_name, lockres->l_flags);
2270 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2271 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2272 "%u, unlock_action = %u\n",
2273 lockres->l_name, lockres->l_flags, lockres->l_action,
2274 lockres->l_unlock_action);
2276 spin_unlock_irqrestore(&lockres->l_lock, flags);
2278 /* XXX: Today we just wait on any busy
2279 * locks... Perhaps we need to cancel converts in the
2281 ocfs2_wait_on_busy_lock(lockres);
2283 spin_lock_irqsave(&lockres->l_lock, flags);
2286 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2287 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2288 lockres->l_level == LKM_EXMODE &&
2289 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2290 lockres->l_ops->set_lvb(lockres);
2293 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2294 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2296 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2297 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2299 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2300 spin_unlock_irqrestore(&lockres->l_lock, flags);
2304 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2306 /* make sure we never get here while waiting for an ast to
2308 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2310 /* is this necessary? */
2311 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2312 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2313 spin_unlock_irqrestore(&lockres->l_lock, flags);
2315 mlog(0, "lock %s\n", lockres->l_name);
2317 status = dlmunlock(osb->dlm, &lockres->l_lksb, lkm_flags,
2318 ocfs2_unlock_ast, lockres);
2319 if (status != DLM_NORMAL) {
2320 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2321 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2322 dlm_print_one_lock(lockres->l_lksb.lockid);
2325 mlog(0, "lock %s, successfull return from dlmunlock\n",
2328 ocfs2_wait_on_busy_lock(lockres);
2334 /* Mark the lockres as being dropped. It will no longer be
2335 * queued if blocking, but we still may have to wait on it
2336 * being dequeued from the vote thread before we can consider
2339 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2340 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2343 struct ocfs2_mask_waiter mw;
2344 unsigned long flags;
2346 ocfs2_init_mask_waiter(&mw);
2348 spin_lock_irqsave(&lockres->l_lock, flags);
2349 lockres->l_flags |= OCFS2_LOCK_FREEING;
2350 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2351 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2352 spin_unlock_irqrestore(&lockres->l_lock, flags);
2354 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2356 status = ocfs2_wait_for_mask(&mw);
2360 spin_lock_irqsave(&lockres->l_lock, flags);
2362 spin_unlock_irqrestore(&lockres->l_lock, flags);
2365 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2366 struct ocfs2_lock_res *lockres)
2370 ocfs2_mark_lockres_freeing(lockres);
2371 ret = ocfs2_drop_lock(osb, lockres);
2376 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2378 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2379 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
2382 int ocfs2_drop_inode_locks(struct inode *inode)
2388 /* No need to call ocfs2_mark_lockres_freeing here -
2389 * ocfs2_clear_inode has done it for us. */
2391 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2392 &OCFS2_I(inode)->ip_data_lockres);
2398 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2399 &OCFS2_I(inode)->ip_meta_lockres);
2402 if (err < 0 && !status)
2405 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2406 &OCFS2_I(inode)->ip_rw_lockres);
2409 if (err < 0 && !status)
2416 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2419 assert_spin_locked(&lockres->l_lock);
2421 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
2423 if (lockres->l_level <= new_level) {
2424 mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
2425 lockres->l_level, new_level);
2429 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2430 lockres->l_name, new_level, lockres->l_blocking);
2432 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2433 lockres->l_requested = new_level;
2434 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2437 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2438 struct ocfs2_lock_res *lockres,
2442 int ret, dlm_flags = LKM_CONVERT;
2443 enum dlm_status status;
2448 dlm_flags |= LKM_VALBLK;
2450 status = dlmlock(osb->dlm,
2455 OCFS2_LOCK_ID_MAX_LEN - 1,
2458 ocfs2_blocking_ast);
2459 if (status != DLM_NORMAL) {
2460 ocfs2_log_dlm_error("dlmlock", status, lockres);
2462 ocfs2_recover_from_dlm_error(lockres, 1);
2472 /* returns 1 when the caller should unlock and call dlmunlock */
2473 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2474 struct ocfs2_lock_res *lockres)
2476 assert_spin_locked(&lockres->l_lock);
2479 mlog(0, "lock %s\n", lockres->l_name);
2481 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2482 /* If we're already trying to cancel a lock conversion
2483 * then just drop the spinlock and allow the caller to
2484 * requeue this lock. */
2486 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2490 /* were we in a convert when we got the bast fire? */
2491 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2492 lockres->l_action != OCFS2_AST_DOWNCONVERT);
2493 /* set things up for the unlockast to know to just
2494 * clear out the ast_action and unset busy, etc. */
2495 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2497 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2498 "lock %s, invalid flags: 0x%lx\n",
2499 lockres->l_name, lockres->l_flags);
2504 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2505 struct ocfs2_lock_res *lockres)
2508 enum dlm_status status;
2511 mlog(0, "lock %s\n", lockres->l_name);
2514 status = dlmunlock(osb->dlm,
2519 if (status != DLM_NORMAL) {
2520 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2522 ocfs2_recover_from_dlm_error(lockres, 0);
2525 mlog(0, "lock %s return from dlmunlock\n", lockres->l_name);
2531 static int ocfs2_unblock_lock(struct ocfs2_super *osb,
2532 struct ocfs2_lock_res *lockres,
2533 struct ocfs2_unblock_ctl *ctl)
2535 unsigned long flags;
2543 spin_lock_irqsave(&lockres->l_lock, flags);
2545 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2548 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2550 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2551 spin_unlock_irqrestore(&lockres->l_lock, flags);
2553 ret = ocfs2_cancel_convert(osb, lockres);
2560 /* if we're blocking an exclusive and we have *any* holders,
2562 if ((lockres->l_blocking == LKM_EXMODE)
2563 && (lockres->l_ex_holders || lockres->l_ro_holders))
2566 /* If it's a PR we're blocking, then only
2567 * requeue if we've got any EX holders */
2568 if (lockres->l_blocking == LKM_PRMODE &&
2569 lockres->l_ex_holders)
2573 * Can we get a lock in this state if the holder counts are
2574 * zero? The meta data unblock code used to check this.
2576 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
2577 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
2580 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2582 if (lockres->l_ops->check_downconvert
2583 && !lockres->l_ops->check_downconvert(lockres, new_level))
2586 /* If we get here, then we know that there are no more
2587 * incompatible holders (and anyone asking for an incompatible
2588 * lock is blocked). We can now downconvert the lock */
2589 if (!lockres->l_ops->downconvert_worker)
2592 /* Some lockres types want to do a bit of work before
2593 * downconverting a lock. Allow that here. The worker function
2594 * may sleep, so we save off a copy of what we're blocking as
2595 * it may change while we're not holding the spin lock. */
2596 blocking = lockres->l_blocking;
2597 spin_unlock_irqrestore(&lockres->l_lock, flags);
2599 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
2601 if (ctl->unblock_action == UNBLOCK_STOP_POST)
2604 spin_lock_irqsave(&lockres->l_lock, flags);
2605 if (blocking != lockres->l_blocking) {
2606 /* If this changed underneath us, then we can't drop
2614 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2615 if (lockres->l_level == LKM_EXMODE)
2619 * We only set the lvb if the lock has been fully
2620 * refreshed - otherwise we risk setting stale
2621 * data. Otherwise, there's no need to actually clear
2622 * out the lvb here as it's value is still valid.
2624 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2625 lockres->l_ops->set_lvb(lockres);
2628 ocfs2_prepare_downconvert(lockres, new_level);
2629 spin_unlock_irqrestore(&lockres->l_lock, flags);
2630 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
2636 spin_unlock_irqrestore(&lockres->l_lock, flags);
2643 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
2646 struct inode *inode;
2647 struct address_space *mapping;
2649 inode = ocfs2_lock_res_inode(lockres);
2650 mapping = inode->i_mapping;
2652 if (filemap_fdatawrite(mapping)) {
2653 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
2654 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2656 sync_mapping_buffers(mapping);
2657 if (blocking == LKM_EXMODE) {
2658 truncate_inode_pages(mapping, 0);
2659 unmap_mapping_range(mapping, 0, 0, 0);
2661 /* We only need to wait on the I/O if we're not also
2662 * truncating pages because truncate_inode_pages waits
2663 * for us above. We don't truncate pages if we're
2664 * blocking anything < EXMODE because we want to keep
2665 * them around in that case. */
2666 filemap_fdatawait(mapping);
2669 return UNBLOCK_CONTINUE;
2672 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
2675 struct inode *inode = ocfs2_lock_res_inode(lockres);
2676 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
2678 BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
2679 BUG_ON(lockres->l_level != LKM_EXMODE && !checkpointed);
2684 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
2688 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
2690 struct inode *inode = ocfs2_lock_res_inode(lockres);
2692 __ocfs2_stuff_meta_lvb(inode);
2696 * Does the final reference drop on our dentry lock. Right now this
2697 * happens in the vote thread, but we could choose to simplify the
2698 * dlmglue API and push these off to the ocfs2_wq in the future.
2700 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
2701 struct ocfs2_lock_res *lockres)
2703 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2704 ocfs2_dentry_lock_put(osb, dl);
2708 * d_delete() matching dentries before the lock downconvert.
2710 * At this point, any process waiting to destroy the
2711 * dentry_lock due to last ref count is stopped by the
2712 * OCFS2_LOCK_QUEUED flag.
2714 * We have two potential problems
2716 * 1) If we do the last reference drop on our dentry_lock (via dput)
2717 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
2718 * the downconvert to finish. Instead we take an elevated
2719 * reference and push the drop until after we've completed our
2720 * unblock processing.
2722 * 2) There might be another process with a final reference,
2723 * waiting on us to finish processing. If this is the case, we
2724 * detect it and exit out - there's no more dentries anyway.
2726 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
2729 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2730 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
2731 struct dentry *dentry;
2732 unsigned long flags;
2736 * This node is blocking another node from getting a read
2737 * lock. This happens when we've renamed within a
2738 * directory. We've forced the other nodes to d_delete(), but
2739 * we never actually dropped our lock because it's still
2740 * valid. The downconvert code will retain a PR for this node,
2741 * so there's no further work to do.
2743 if (blocking == LKM_PRMODE)
2744 return UNBLOCK_CONTINUE;
2747 * Mark this inode as potentially orphaned. The code in
2748 * ocfs2_delete_inode() will figure out whether it actually
2749 * needs to be freed or not.
2751 spin_lock(&oi->ip_lock);
2752 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
2753 spin_unlock(&oi->ip_lock);
2756 * Yuck. We need to make sure however that the check of
2757 * OCFS2_LOCK_FREEING and the extra reference are atomic with
2758 * respect to a reference decrement or the setting of that
2761 spin_lock_irqsave(&lockres->l_lock, flags);
2762 spin_lock(&dentry_attach_lock);
2763 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
2768 spin_unlock(&dentry_attach_lock);
2769 spin_unlock_irqrestore(&lockres->l_lock, flags);
2771 mlog(0, "extra_ref = %d\n", extra_ref);
2774 * We have a process waiting on us in ocfs2_dentry_iput(),
2775 * which means we can't have any more outstanding
2776 * aliases. There's no need to do any more work.
2779 return UNBLOCK_CONTINUE;
2781 spin_lock(&dentry_attach_lock);
2783 dentry = ocfs2_find_local_alias(dl->dl_inode,
2784 dl->dl_parent_blkno, 1);
2787 spin_unlock(&dentry_attach_lock);
2789 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
2790 dentry->d_name.name);
2793 * The following dcache calls may do an
2794 * iput(). Normally we don't want that from the
2795 * downconverting thread, but in this case it's ok
2796 * because the requesting node already has an
2797 * exclusive lock on the inode, so it can't be queued
2798 * for a downconvert.
2803 spin_lock(&dentry_attach_lock);
2805 spin_unlock(&dentry_attach_lock);
2808 * If we are the last holder of this dentry lock, there is no
2809 * reason to downconvert so skip straight to the unlock.
2811 if (dl->dl_count == 1)
2812 return UNBLOCK_STOP_POST;
2814 return UNBLOCK_CONTINUE_POST;
2817 void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
2818 struct ocfs2_lock_res *lockres)
2821 struct ocfs2_unblock_ctl ctl = {0, 0,};
2822 unsigned long flags;
2824 /* Our reference to the lockres in this function can be
2825 * considered valid until we remove the OCFS2_LOCK_QUEUED
2831 BUG_ON(!lockres->l_ops);
2833 mlog(0, "lockres %s blocked.\n", lockres->l_name);
2835 /* Detect whether a lock has been marked as going away while
2836 * the vote thread was processing other things. A lock can
2837 * still be marked with OCFS2_LOCK_FREEING after this check,
2838 * but short circuiting here will still save us some
2840 spin_lock_irqsave(&lockres->l_lock, flags);
2841 if (lockres->l_flags & OCFS2_LOCK_FREEING)
2843 spin_unlock_irqrestore(&lockres->l_lock, flags);
2845 status = ocfs2_unblock_lock(osb, lockres, &ctl);
2849 spin_lock_irqsave(&lockres->l_lock, flags);
2851 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
2852 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
2854 ocfs2_schedule_blocked_lock(osb, lockres);
2856 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
2857 ctl.requeue ? "yes" : "no");
2858 spin_unlock_irqrestore(&lockres->l_lock, flags);
2860 if (ctl.unblock_action != UNBLOCK_CONTINUE
2861 && lockres->l_ops->post_unlock)
2862 lockres->l_ops->post_unlock(osb, lockres);
2867 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
2868 struct ocfs2_lock_res *lockres)
2872 assert_spin_locked(&lockres->l_lock);
2874 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
2875 /* Do not schedule a lock for downconvert when it's on
2876 * the way to destruction - any nodes wanting access
2877 * to the resource will get it soon. */
2878 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
2879 lockres->l_name, lockres->l_flags);
2883 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
2885 spin_lock(&osb->vote_task_lock);
2886 if (list_empty(&lockres->l_blocked_list)) {
2887 list_add_tail(&lockres->l_blocked_list,
2888 &osb->blocked_lock_list);
2889 osb->blocked_lock_count++;
2891 spin_unlock(&osb->vote_task_lock);
2896 /* This aids in debugging situations where a bad LVB might be involved. */
2897 void ocfs2_dump_meta_lvb_info(u64 level,
2898 const char *function,
2900 struct ocfs2_lock_res *lockres)
2902 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
2904 mlog(level, "LVB information for %s (called from %s:%u):\n",
2905 lockres->l_name, function, line);
2906 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
2907 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
2908 be32_to_cpu(lvb->lvb_igeneration));
2909 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
2910 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
2911 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
2912 be16_to_cpu(lvb->lvb_imode));
2913 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
2914 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
2915 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
2916 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
2917 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
2918 be32_to_cpu(lvb->lvb_iattr));