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 ocfs2_convert_worker_t functions.
76 * These control the precise actions of ocfs2_generic_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_unblock_meta(struct ocfs2_lock_res *lockres,
94 struct ocfs2_unblock_ctl *ctl);
95 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
97 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
99 static int ocfs2_unblock_data(struct ocfs2_lock_res *lockres,
100 struct ocfs2_unblock_ctl *ctl);
101 static int ocfs2_unblock_inode_lock(struct ocfs2_lock_res *lockres,
102 struct ocfs2_unblock_ctl *ctl);
103 static int ocfs2_unblock_dentry_lock(struct ocfs2_lock_res *lockres,
104 struct ocfs2_unblock_ctl *ctl);
105 static int ocfs2_unblock_osb_lock(struct ocfs2_lock_res *lockres,
106 struct ocfs2_unblock_ctl *ctl);
108 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
109 struct ocfs2_lock_res *lockres);
112 * OCFS2 Lock Resource Operations
114 * These fine tune the behavior of the generic dlmglue locking infrastructure.
116 struct ocfs2_lock_res_ops {
118 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
119 * this callback if ->l_priv is not an ocfs2_super pointer
121 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
122 int (*unblock)(struct ocfs2_lock_res *, struct ocfs2_unblock_ctl *);
123 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
126 * Allow a lock type to add checks to determine whether it is
127 * safe to downconvert a lock. Return 0 to re-queue the
128 * downconvert at a later time, nonzero to continue.
130 * For most locks, the default checks that there are no
131 * incompatible holders are sufficient.
133 * Called with the lockres spinlock held.
135 int (*check_downconvert)(struct ocfs2_lock_res *, int);
138 * Allows a lock type to populate the lock value block. This
139 * is called on downconvert, and when we drop a lock.
141 * Locks that want to use this should set LOCK_TYPE_USES_LVB
142 * in the flags field.
144 * Called with the lockres spinlock held.
146 void (*set_lvb)(struct ocfs2_lock_res *);
149 * LOCK_TYPE_* flags which describe the specific requirements
150 * of a lock type. Descriptions of each individual flag follow.
156 * Some locks want to "refresh" potentially stale data when a
157 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
158 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
159 * individual lockres l_flags member from the ast function. It is
160 * expected that the locking wrapper will clear the
161 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
163 #define LOCK_TYPE_REQUIRES_REFRESH 0x1
166 * Indicate that a lock type makes use of the lock value block. The
167 * ->set_lvb lock type callback must be defined.
169 #define LOCK_TYPE_USES_LVB 0x2
171 typedef int (ocfs2_convert_worker_t)(struct ocfs2_lock_res *, int);
172 static int ocfs2_generic_unblock_lock(struct ocfs2_super *osb,
173 struct ocfs2_lock_res *lockres,
174 struct ocfs2_unblock_ctl *ctl,
175 ocfs2_convert_worker_t *worker);
177 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
178 .get_osb = ocfs2_get_inode_osb,
179 .unblock = ocfs2_unblock_inode_lock,
183 static struct ocfs2_lock_res_ops ocfs2_inode_meta_lops = {
184 .get_osb = ocfs2_get_inode_osb,
185 .unblock = ocfs2_unblock_meta,
186 .check_downconvert = ocfs2_check_meta_downconvert,
187 .set_lvb = ocfs2_set_meta_lvb,
188 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
191 static struct ocfs2_lock_res_ops ocfs2_inode_data_lops = {
192 .get_osb = ocfs2_get_inode_osb,
193 .unblock = ocfs2_unblock_data,
197 static struct ocfs2_lock_res_ops ocfs2_super_lops = {
198 .unblock = ocfs2_unblock_osb_lock,
199 .flags = LOCK_TYPE_REQUIRES_REFRESH,
202 static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
203 .unblock = ocfs2_unblock_osb_lock,
207 static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
208 .get_osb = ocfs2_get_dentry_osb,
209 .unblock = ocfs2_unblock_dentry_lock,
210 .post_unlock = ocfs2_dentry_post_unlock,
214 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
216 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
217 lockres->l_type == OCFS2_LOCK_TYPE_DATA ||
218 lockres->l_type == OCFS2_LOCK_TYPE_RW;
221 static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
223 BUG_ON(!ocfs2_is_inode_lock(lockres));
225 return (struct inode *) lockres->l_priv;
228 static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
230 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
232 return (struct ocfs2_dentry_lock *)lockres->l_priv;
235 static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
237 if (lockres->l_ops->get_osb)
238 return lockres->l_ops->get_osb(lockres);
240 return (struct ocfs2_super *)lockres->l_priv;
243 static int ocfs2_lock_create(struct ocfs2_super *osb,
244 struct ocfs2_lock_res *lockres,
247 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
249 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
250 struct ocfs2_lock_res *lockres,
252 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
253 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
254 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
255 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
256 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
257 struct ocfs2_lock_res *lockres);
258 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
260 #define ocfs2_log_dlm_error(_func, _stat, _lockres) do { \
261 mlog(ML_ERROR, "Dlm error \"%s\" while calling %s on " \
262 "resource %s: %s\n", dlm_errname(_stat), _func, \
263 _lockres->l_name, dlm_errmsg(_stat)); \
265 static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
266 struct ocfs2_lock_res *lockres);
267 static int ocfs2_meta_lock_update(struct inode *inode,
268 struct buffer_head **bh);
269 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
270 static inline int ocfs2_highest_compat_lock_level(int level);
271 static inline int ocfs2_can_downconvert_meta_lock(struct inode *inode,
272 struct ocfs2_lock_res *lockres,
275 static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
284 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
286 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
287 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
288 (long long)blkno, generation);
290 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
292 mlog(0, "built lock resource with name: %s\n", name);
297 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
299 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
300 struct ocfs2_dlm_debug *dlm_debug)
302 mlog(0, "Add tracking for lockres %s\n", res->l_name);
304 spin_lock(&ocfs2_dlm_tracking_lock);
305 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
306 spin_unlock(&ocfs2_dlm_tracking_lock);
309 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
311 spin_lock(&ocfs2_dlm_tracking_lock);
312 if (!list_empty(&res->l_debug_list))
313 list_del_init(&res->l_debug_list);
314 spin_unlock(&ocfs2_dlm_tracking_lock);
317 static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
318 struct ocfs2_lock_res *res,
319 enum ocfs2_lock_type type,
320 struct ocfs2_lock_res_ops *ops,
327 res->l_level = LKM_IVMODE;
328 res->l_requested = LKM_IVMODE;
329 res->l_blocking = LKM_IVMODE;
330 res->l_action = OCFS2_AST_INVALID;
331 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
333 res->l_flags = OCFS2_LOCK_INITIALIZED;
335 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
338 void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
340 /* This also clears out the lock status block */
341 memset(res, 0, sizeof(struct ocfs2_lock_res));
342 spin_lock_init(&res->l_lock);
343 init_waitqueue_head(&res->l_event);
344 INIT_LIST_HEAD(&res->l_blocked_list);
345 INIT_LIST_HEAD(&res->l_mask_waiters);
348 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
349 enum ocfs2_lock_type type,
350 unsigned int generation,
353 struct ocfs2_lock_res_ops *ops;
356 case OCFS2_LOCK_TYPE_RW:
357 ops = &ocfs2_inode_rw_lops;
359 case OCFS2_LOCK_TYPE_META:
360 ops = &ocfs2_inode_meta_lops;
362 case OCFS2_LOCK_TYPE_DATA:
363 ops = &ocfs2_inode_data_lops;
366 mlog_bug_on_msg(1, "type: %d\n", type);
367 ops = NULL; /* thanks, gcc */
371 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
372 generation, res->l_name);
373 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
376 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
378 struct inode *inode = ocfs2_lock_res_inode(lockres);
380 return OCFS2_SB(inode->i_sb);
383 static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
385 __be64 inode_blkno_be;
387 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
390 return be64_to_cpu(inode_blkno_be);
393 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
395 struct ocfs2_dentry_lock *dl = lockres->l_priv;
397 return OCFS2_SB(dl->dl_inode->i_sb);
400 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
401 u64 parent, struct inode *inode)
404 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
405 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
406 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
408 ocfs2_lock_res_init_once(lockres);
411 * Unfortunately, the standard lock naming scheme won't work
412 * here because we have two 16 byte values to use. Instead,
413 * we'll stuff the inode number as a binary value. We still
414 * want error prints to show something without garbling the
415 * display, so drop a null byte in there before the inode
416 * number. A future version of OCFS2 will likely use all
417 * binary lock names. The stringified names have been a
418 * tremendous aid in debugging, but now that the debugfs
419 * interface exists, we can mangle things there if need be.
421 * NOTE: We also drop the standard "pad" value (the total lock
422 * name size stays the same though - the last part is all
423 * zeros due to the memset in ocfs2_lock_res_init_once()
425 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
427 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
430 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
432 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
435 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
436 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
440 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
441 struct ocfs2_super *osb)
443 /* Superblock lockres doesn't come from a slab so we call init
444 * once on it manually. */
445 ocfs2_lock_res_init_once(res);
446 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
448 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
449 &ocfs2_super_lops, osb);
452 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
453 struct ocfs2_super *osb)
455 /* Rename lockres doesn't come from a slab so we call init
456 * once on it manually. */
457 ocfs2_lock_res_init_once(res);
458 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
459 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
460 &ocfs2_rename_lops, osb);
463 void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
467 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
470 ocfs2_remove_lockres_tracking(res);
472 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
473 "Lockres %s is on the blocked list\n",
475 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
476 "Lockres %s has mask waiters pending\n",
478 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
479 "Lockres %s is locked\n",
481 mlog_bug_on_msg(res->l_ro_holders,
482 "Lockres %s has %u ro holders\n",
483 res->l_name, res->l_ro_holders);
484 mlog_bug_on_msg(res->l_ex_holders,
485 "Lockres %s has %u ex holders\n",
486 res->l_name, res->l_ex_holders);
488 /* Need to clear out the lock status block for the dlm */
489 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
495 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
504 lockres->l_ex_holders++;
507 lockres->l_ro_holders++;
516 static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
525 BUG_ON(!lockres->l_ex_holders);
526 lockres->l_ex_holders--;
529 BUG_ON(!lockres->l_ro_holders);
530 lockres->l_ro_holders--;
538 /* WARNING: This function lives in a world where the only three lock
539 * levels are EX, PR, and NL. It *will* have to be adjusted when more
540 * lock types are added. */
541 static inline int ocfs2_highest_compat_lock_level(int level)
543 int new_level = LKM_EXMODE;
545 if (level == LKM_EXMODE)
546 new_level = LKM_NLMODE;
547 else if (level == LKM_PRMODE)
548 new_level = LKM_PRMODE;
552 static void lockres_set_flags(struct ocfs2_lock_res *lockres,
553 unsigned long newflags)
555 struct list_head *pos, *tmp;
556 struct ocfs2_mask_waiter *mw;
558 assert_spin_locked(&lockres->l_lock);
560 lockres->l_flags = newflags;
562 list_for_each_safe(pos, tmp, &lockres->l_mask_waiters) {
563 mw = list_entry(pos, struct ocfs2_mask_waiter, mw_item);
564 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
567 list_del_init(&mw->mw_item);
569 complete(&mw->mw_complete);
572 static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
574 lockres_set_flags(lockres, lockres->l_flags | or);
576 static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
579 lockres_set_flags(lockres, lockres->l_flags & ~clear);
582 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
586 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
587 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
588 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
589 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
591 lockres->l_level = lockres->l_requested;
592 if (lockres->l_level <=
593 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
594 lockres->l_blocking = LKM_NLMODE;
595 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
597 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
602 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
606 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
607 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
609 /* Convert from RO to EX doesn't really need anything as our
610 * information is already up to data. Convert from NL to
611 * *anything* however should mark ourselves as needing an
613 if (lockres->l_level == LKM_NLMODE &&
614 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
615 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
617 lockres->l_level = lockres->l_requested;
618 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
623 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
627 BUG_ON((!lockres->l_flags & OCFS2_LOCK_BUSY));
628 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
630 if (lockres->l_requested > LKM_NLMODE &&
631 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
632 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
633 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
635 lockres->l_level = lockres->l_requested;
636 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
637 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
642 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
645 int needs_downconvert = 0;
648 assert_spin_locked(&lockres->l_lock);
650 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
652 if (level > lockres->l_blocking) {
653 /* only schedule a downconvert if we haven't already scheduled
654 * one that goes low enough to satisfy the level we're
655 * blocking. this also catches the case where we get
657 if (ocfs2_highest_compat_lock_level(level) <
658 ocfs2_highest_compat_lock_level(lockres->l_blocking))
659 needs_downconvert = 1;
661 lockres->l_blocking = level;
664 mlog_exit(needs_downconvert);
665 return needs_downconvert;
668 static void ocfs2_blocking_ast(void *opaque, int level)
670 struct ocfs2_lock_res *lockres = opaque;
671 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
672 int needs_downconvert;
675 BUG_ON(level <= LKM_NLMODE);
677 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
678 lockres->l_name, level, lockres->l_level,
679 ocfs2_lock_type_string(lockres->l_type));
681 spin_lock_irqsave(&lockres->l_lock, flags);
682 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
683 if (needs_downconvert)
684 ocfs2_schedule_blocked_lock(osb, lockres);
685 spin_unlock_irqrestore(&lockres->l_lock, flags);
687 wake_up(&lockres->l_event);
689 ocfs2_kick_vote_thread(osb);
692 static void ocfs2_locking_ast(void *opaque)
694 struct ocfs2_lock_res *lockres = opaque;
695 struct dlm_lockstatus *lksb = &lockres->l_lksb;
698 spin_lock_irqsave(&lockres->l_lock, flags);
700 if (lksb->status != DLM_NORMAL) {
701 mlog(ML_ERROR, "lockres %s: lksb status value of %u!\n",
702 lockres->l_name, lksb->status);
703 spin_unlock_irqrestore(&lockres->l_lock, flags);
707 switch(lockres->l_action) {
708 case OCFS2_AST_ATTACH:
709 ocfs2_generic_handle_attach_action(lockres);
710 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
712 case OCFS2_AST_CONVERT:
713 ocfs2_generic_handle_convert_action(lockres);
715 case OCFS2_AST_DOWNCONVERT:
716 ocfs2_generic_handle_downconvert_action(lockres);
719 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
720 "lockres flags = 0x%lx, unlock action: %u\n",
721 lockres->l_name, lockres->l_action, lockres->l_flags,
722 lockres->l_unlock_action);
726 /* set it to something invalid so if we get called again we
728 lockres->l_action = OCFS2_AST_INVALID;
730 wake_up(&lockres->l_event);
731 spin_unlock_irqrestore(&lockres->l_lock, flags);
734 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
740 spin_lock_irqsave(&lockres->l_lock, flags);
741 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
743 lockres->l_action = OCFS2_AST_INVALID;
745 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
746 spin_unlock_irqrestore(&lockres->l_lock, flags);
748 wake_up(&lockres->l_event);
752 /* Note: If we detect another process working on the lock (i.e.,
753 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
754 * to do the right thing in that case.
756 static int ocfs2_lock_create(struct ocfs2_super *osb,
757 struct ocfs2_lock_res *lockres,
762 enum dlm_status status;
767 mlog(0, "lock %s, level = %d, flags = %d\n", lockres->l_name, level,
770 spin_lock_irqsave(&lockres->l_lock, flags);
771 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
772 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
773 spin_unlock_irqrestore(&lockres->l_lock, flags);
777 lockres->l_action = OCFS2_AST_ATTACH;
778 lockres->l_requested = level;
779 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
780 spin_unlock_irqrestore(&lockres->l_lock, flags);
782 status = dlmlock(osb->dlm,
787 OCFS2_LOCK_ID_MAX_LEN - 1,
791 if (status != DLM_NORMAL) {
792 ocfs2_log_dlm_error("dlmlock", status, lockres);
794 ocfs2_recover_from_dlm_error(lockres, 1);
797 mlog(0, "lock %s, successfull return from dlmlock\n", lockres->l_name);
804 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
810 spin_lock_irqsave(&lockres->l_lock, flags);
811 ret = lockres->l_flags & flag;
812 spin_unlock_irqrestore(&lockres->l_lock, flags);
817 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
820 wait_event(lockres->l_event,
821 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
824 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
827 wait_event(lockres->l_event,
828 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
831 /* predict what lock level we'll be dropping down to on behalf
832 * of another node, and return true if the currently wanted
833 * level will be compatible with it. */
834 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
837 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
839 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
842 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
844 INIT_LIST_HEAD(&mw->mw_item);
845 init_completion(&mw->mw_complete);
848 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
850 wait_for_completion(&mw->mw_complete);
851 /* Re-arm the completion in case we want to wait on it again */
852 INIT_COMPLETION(mw->mw_complete);
853 return mw->mw_status;
856 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
857 struct ocfs2_mask_waiter *mw,
861 BUG_ON(!list_empty(&mw->mw_item));
863 assert_spin_locked(&lockres->l_lock);
865 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
870 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
871 * if the mask still hadn't reached its goal */
872 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
873 struct ocfs2_mask_waiter *mw)
878 spin_lock_irqsave(&lockres->l_lock, flags);
879 if (!list_empty(&mw->mw_item)) {
880 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
883 list_del_init(&mw->mw_item);
884 init_completion(&mw->mw_complete);
886 spin_unlock_irqrestore(&lockres->l_lock, flags);
892 static int ocfs2_cluster_lock(struct ocfs2_super *osb,
893 struct ocfs2_lock_res *lockres,
898 struct ocfs2_mask_waiter mw;
899 enum dlm_status status;
900 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
901 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
906 ocfs2_init_mask_waiter(&mw);
908 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
909 lkm_flags |= LKM_VALBLK;
914 if (catch_signals && signal_pending(current)) {
919 spin_lock_irqsave(&lockres->l_lock, flags);
921 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
922 "Cluster lock called on freeing lockres %s! flags "
923 "0x%lx\n", lockres->l_name, lockres->l_flags);
925 /* We only compare against the currently granted level
926 * here. If the lock is blocked waiting on a downconvert,
927 * we'll get caught below. */
928 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
929 level > lockres->l_level) {
930 /* is someone sitting in dlm_lock? If so, wait on
932 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
937 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
938 /* lock has not been created yet. */
939 spin_unlock_irqrestore(&lockres->l_lock, flags);
941 ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
949 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
950 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
951 /* is the lock is currently blocked on behalf of
953 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
958 if (level > lockres->l_level) {
959 if (lockres->l_action != OCFS2_AST_INVALID)
960 mlog(ML_ERROR, "lockres %s has action %u pending\n",
961 lockres->l_name, lockres->l_action);
963 lockres->l_action = OCFS2_AST_CONVERT;
964 lockres->l_requested = level;
965 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
966 spin_unlock_irqrestore(&lockres->l_lock, flags);
968 BUG_ON(level == LKM_IVMODE);
969 BUG_ON(level == LKM_NLMODE);
971 mlog(0, "lock %s, convert from %d to level = %d\n",
972 lockres->l_name, lockres->l_level, level);
974 /* call dlm_lock to upgrade lock now */
975 status = dlmlock(osb->dlm,
978 lkm_flags|LKM_CONVERT,
980 OCFS2_LOCK_ID_MAX_LEN - 1,
984 if (status != DLM_NORMAL) {
985 if ((lkm_flags & LKM_NOQUEUE) &&
986 (status == DLM_NOTQUEUED))
989 ocfs2_log_dlm_error("dlmlock", status,
993 ocfs2_recover_from_dlm_error(lockres, 1);
997 mlog(0, "lock %s, successfull return from dlmlock\n",
1000 /* At this point we've gone inside the dlm and need to
1001 * complete our work regardless. */
1004 /* wait for busy to clear and carry on */
1008 /* Ok, if we get here then we're good to go. */
1009 ocfs2_inc_holders(lockres, level);
1013 spin_unlock_irqrestore(&lockres->l_lock, flags);
1016 * This is helping work around a lock inversion between the page lock
1017 * and dlm locks. One path holds the page lock while calling aops
1018 * which block acquiring dlm locks. The voting thread holds dlm
1019 * locks while acquiring page locks while down converting data locks.
1020 * This block is helping an aop path notice the inversion and back
1021 * off to unlock its page lock before trying the dlm lock again.
1023 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1024 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1026 if (lockres_remove_mask_waiter(lockres, &mw))
1032 ret = ocfs2_wait_for_mask(&mw);
1042 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1043 struct ocfs2_lock_res *lockres,
1046 unsigned long flags;
1049 spin_lock_irqsave(&lockres->l_lock, flags);
1050 ocfs2_dec_holders(lockres, level);
1051 ocfs2_vote_on_unlock(osb, lockres);
1052 spin_unlock_irqrestore(&lockres->l_lock, flags);
1056 int ocfs2_create_new_lock(struct ocfs2_super *osb,
1057 struct ocfs2_lock_res *lockres,
1061 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1062 unsigned long flags;
1063 int lkm_flags = local ? LKM_LOCAL : 0;
1065 spin_lock_irqsave(&lockres->l_lock, flags);
1066 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1067 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1068 spin_unlock_irqrestore(&lockres->l_lock, flags);
1070 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
1073 /* Grants us an EX lock on the data and metadata resources, skipping
1074 * the normal cluster directory lookup. Use this ONLY on newly created
1075 * inodes which other nodes can't possibly see, and which haven't been
1076 * hashed in the inode hash yet. This can give us a good performance
1077 * increase as it'll skip the network broadcast normally associated
1078 * with creating a new lock resource. */
1079 int ocfs2_create_new_inode_locks(struct inode *inode)
1082 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1085 BUG_ON(!ocfs2_inode_is_new(inode));
1089 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1091 /* NOTE: That we don't increment any of the holder counts, nor
1092 * do we add anything to a journal handle. Since this is
1093 * supposed to be a new inode which the cluster doesn't know
1094 * about yet, there is no need to. As far as the LVB handling
1095 * is concerned, this is basically like acquiring an EX lock
1096 * on a resource which has an invalid one -- we'll set it
1097 * valid when we release the EX. */
1099 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
1106 * We don't want to use LKM_LOCAL on a meta data lock as they
1107 * don't use a generation in their lock names.
1109 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_meta_lockres, 1, 0);
1115 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_data_lockres, 1, 1);
1126 int ocfs2_rw_lock(struct inode *inode, int write)
1129 struct ocfs2_lock_res *lockres;
1135 mlog(0, "inode %llu take %s RW lock\n",
1136 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1137 write ? "EXMODE" : "PRMODE");
1139 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1141 level = write ? LKM_EXMODE : LKM_PRMODE;
1143 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1152 void ocfs2_rw_unlock(struct inode *inode, int write)
1154 int level = write ? LKM_EXMODE : LKM_PRMODE;
1155 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1159 mlog(0, "inode %llu drop %s RW lock\n",
1160 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1161 write ? "EXMODE" : "PRMODE");
1163 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1168 int ocfs2_data_lock_full(struct inode *inode,
1172 int status = 0, level;
1173 struct ocfs2_lock_res *lockres;
1179 mlog(0, "inode %llu take %s DATA lock\n",
1180 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1181 write ? "EXMODE" : "PRMODE");
1183 /* We'll allow faking a readonly data lock for
1185 if (ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb))) {
1193 lockres = &OCFS2_I(inode)->ip_data_lockres;
1195 level = write ? LKM_EXMODE : LKM_PRMODE;
1197 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level,
1199 if (status < 0 && status != -EAGAIN)
1207 /* see ocfs2_meta_lock_with_page() */
1208 int ocfs2_data_lock_with_page(struct inode *inode,
1214 ret = ocfs2_data_lock_full(inode, write, OCFS2_LOCK_NONBLOCK);
1215 if (ret == -EAGAIN) {
1217 if (ocfs2_data_lock(inode, write) == 0)
1218 ocfs2_data_unlock(inode, write);
1219 ret = AOP_TRUNCATED_PAGE;
1225 static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
1226 struct ocfs2_lock_res *lockres)
1232 /* If we know that another node is waiting on our lock, kick
1233 * the vote thread * pre-emptively when we reach a release
1235 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1236 switch(lockres->l_blocking) {
1238 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1242 if (!lockres->l_ex_holders)
1251 ocfs2_kick_vote_thread(osb);
1256 void ocfs2_data_unlock(struct inode *inode,
1259 int level = write ? LKM_EXMODE : LKM_PRMODE;
1260 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_data_lockres;
1264 mlog(0, "inode %llu drop %s DATA lock\n",
1265 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1266 write ? "EXMODE" : "PRMODE");
1268 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1269 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1274 #define OCFS2_SEC_BITS 34
1275 #define OCFS2_SEC_SHIFT (64 - 34)
1276 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1278 /* LVB only has room for 64 bits of time here so we pack it for
1280 static u64 ocfs2_pack_timespec(struct timespec *spec)
1283 u64 sec = spec->tv_sec;
1284 u32 nsec = spec->tv_nsec;
1286 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1291 /* Call this with the lockres locked. I am reasonably sure we don't
1292 * need ip_lock in this function as anyone who would be changing those
1293 * values is supposed to be blocked in ocfs2_meta_lock right now. */
1294 static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1296 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1297 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1298 struct ocfs2_meta_lvb *lvb;
1302 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1305 * Invalidate the LVB of a deleted inode - this way other
1306 * nodes are forced to go to disk and discover the new inode
1309 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1310 lvb->lvb_version = 0;
1314 lvb->lvb_version = OCFS2_LVB_VERSION;
1315 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1316 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1317 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1318 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1319 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1320 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1321 lvb->lvb_iatime_packed =
1322 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1323 lvb->lvb_ictime_packed =
1324 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1325 lvb->lvb_imtime_packed =
1326 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1327 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
1328 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
1331 mlog_meta_lvb(0, lockres);
1336 static void ocfs2_unpack_timespec(struct timespec *spec,
1339 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1340 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1343 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1345 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1346 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1347 struct ocfs2_meta_lvb *lvb;
1351 mlog_meta_lvb(0, lockres);
1353 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1355 /* We're safe here without the lockres lock... */
1356 spin_lock(&oi->ip_lock);
1357 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1358 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1360 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1361 ocfs2_set_inode_flags(inode);
1363 /* fast-symlinks are a special case */
1364 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1365 inode->i_blocks = 0;
1368 ocfs2_align_bytes_to_sectors(i_size_read(inode));
1370 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1371 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1372 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1373 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1374 ocfs2_unpack_timespec(&inode->i_atime,
1375 be64_to_cpu(lvb->lvb_iatime_packed));
1376 ocfs2_unpack_timespec(&inode->i_mtime,
1377 be64_to_cpu(lvb->lvb_imtime_packed));
1378 ocfs2_unpack_timespec(&inode->i_ctime,
1379 be64_to_cpu(lvb->lvb_ictime_packed));
1380 spin_unlock(&oi->ip_lock);
1385 static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1386 struct ocfs2_lock_res *lockres)
1388 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1390 if (lvb->lvb_version == OCFS2_LVB_VERSION
1391 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
1396 /* Determine whether a lock resource needs to be refreshed, and
1397 * arbitrate who gets to refresh it.
1399 * 0 means no refresh needed.
1401 * > 0 means you need to refresh this and you MUST call
1402 * ocfs2_complete_lock_res_refresh afterwards. */
1403 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1405 unsigned long flags;
1411 spin_lock_irqsave(&lockres->l_lock, flags);
1412 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1413 spin_unlock_irqrestore(&lockres->l_lock, flags);
1417 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1418 spin_unlock_irqrestore(&lockres->l_lock, flags);
1420 ocfs2_wait_on_refreshing_lock(lockres);
1424 /* Ok, I'll be the one to refresh this lock. */
1425 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1426 spin_unlock_irqrestore(&lockres->l_lock, flags);
1434 /* If status is non zero, I'll mark it as not being in refresh
1435 * anymroe, but i won't clear the needs refresh flag. */
1436 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1439 unsigned long flags;
1442 spin_lock_irqsave(&lockres->l_lock, flags);
1443 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1445 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1446 spin_unlock_irqrestore(&lockres->l_lock, flags);
1448 wake_up(&lockres->l_event);
1453 /* may or may not return a bh if it went to disk. */
1454 static int ocfs2_meta_lock_update(struct inode *inode,
1455 struct buffer_head **bh)
1458 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1459 struct ocfs2_lock_res *lockres;
1460 struct ocfs2_dinode *fe;
1464 spin_lock(&oi->ip_lock);
1465 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1466 mlog(0, "Orphaned inode %llu was deleted while we "
1467 "were waiting on a lock. ip_flags = 0x%x\n",
1468 (unsigned long long)oi->ip_blkno, oi->ip_flags);
1469 spin_unlock(&oi->ip_lock);
1473 spin_unlock(&oi->ip_lock);
1475 lockres = &oi->ip_meta_lockres;
1477 if (!ocfs2_should_refresh_lock_res(lockres))
1480 /* This will discard any caching information we might have had
1481 * for the inode metadata. */
1482 ocfs2_metadata_cache_purge(inode);
1484 /* will do nothing for inode types that don't use the extent
1485 * map (directories, bitmap files, etc) */
1486 ocfs2_extent_map_trunc(inode, 0);
1488 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
1489 mlog(0, "Trusting LVB on inode %llu\n",
1490 (unsigned long long)oi->ip_blkno);
1491 ocfs2_refresh_inode_from_lvb(inode);
1493 /* Boo, we have to go to disk. */
1494 /* read bh, cast, ocfs2_refresh_inode */
1495 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1496 bh, OCFS2_BH_CACHED, inode);
1501 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1503 /* This is a good chance to make sure we're not
1504 * locking an invalid object.
1506 * We bug on a stale inode here because we checked
1507 * above whether it was wiped from disk. The wiping
1508 * node provides a guarantee that we receive that
1509 * message and can mark the inode before dropping any
1510 * locks associated with it. */
1511 if (!OCFS2_IS_VALID_DINODE(fe)) {
1512 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1516 mlog_bug_on_msg(inode->i_generation !=
1517 le32_to_cpu(fe->i_generation),
1518 "Invalid dinode %llu disk generation: %u "
1519 "inode->i_generation: %u\n",
1520 (unsigned long long)oi->ip_blkno,
1521 le32_to_cpu(fe->i_generation),
1522 inode->i_generation);
1523 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1524 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
1525 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1526 (unsigned long long)oi->ip_blkno,
1527 (unsigned long long)le64_to_cpu(fe->i_dtime),
1528 le32_to_cpu(fe->i_flags));
1530 ocfs2_refresh_inode(inode, fe);
1535 ocfs2_complete_lock_res_refresh(lockres, status);
1541 static int ocfs2_assign_bh(struct inode *inode,
1542 struct buffer_head **ret_bh,
1543 struct buffer_head *passed_bh)
1548 /* Ok, the update went to disk for us, use the
1550 *ret_bh = passed_bh;
1556 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1557 OCFS2_I(inode)->ip_blkno,
1568 * returns < 0 error if the callback will never be called, otherwise
1569 * the result of the lock will be communicated via the callback.
1571 int ocfs2_meta_lock_full(struct inode *inode,
1572 struct ocfs2_journal_handle *handle,
1573 struct buffer_head **ret_bh,
1577 int status, level, dlm_flags, acquired;
1578 struct ocfs2_lock_res *lockres;
1579 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1580 struct buffer_head *local_bh = NULL;
1586 mlog(0, "inode %llu, take %s META lock\n",
1587 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1588 ex ? "EXMODE" : "PRMODE");
1592 /* We'll allow faking a readonly metadata lock for
1594 if (ocfs2_is_hard_readonly(osb)) {
1600 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1601 wait_event(osb->recovery_event,
1602 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1605 lockres = &OCFS2_I(inode)->ip_meta_lockres;
1606 level = ex ? LKM_EXMODE : LKM_PRMODE;
1608 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
1609 dlm_flags |= LKM_NOQUEUE;
1611 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1613 if (status != -EAGAIN && status != -EIOCBRETRY)
1618 /* Notify the error cleanup path to drop the cluster lock. */
1621 /* We wait twice because a node may have died while we were in
1622 * the lower dlm layers. The second time though, we've
1623 * committed to owning this lock so we don't allow signals to
1624 * abort the operation. */
1625 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1626 wait_event(osb->recovery_event,
1627 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1630 * We only see this flag if we're being called from
1631 * ocfs2_read_locked_inode(). It means we're locking an inode
1632 * which hasn't been populated yet, so clear the refresh flag
1633 * and let the caller handle it.
1635 if (inode->i_state & I_NEW) {
1637 ocfs2_complete_lock_res_refresh(lockres, 0);
1641 /* This is fun. The caller may want a bh back, or it may
1642 * not. ocfs2_meta_lock_update definitely wants one in, but
1643 * may or may not read one, depending on what's in the
1644 * LVB. The result of all of this is that we've *only* gone to
1645 * disk if we have to, so the complexity is worthwhile. */
1646 status = ocfs2_meta_lock_update(inode, &local_bh);
1648 if (status != -ENOENT)
1654 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
1662 status = ocfs2_handle_add_lock(handle, inode);
1669 if (ret_bh && (*ret_bh)) {
1674 ocfs2_meta_unlock(inode, ex);
1685 * This is working around a lock inversion between tasks acquiring DLM locks
1686 * while holding a page lock and the vote thread which blocks dlm lock acquiry
1687 * while acquiring page locks.
1689 * ** These _with_page variantes are only intended to be called from aop
1690 * methods that hold page locks and return a very specific *positive* error
1691 * code that aop methods pass up to the VFS -- test for errors with != 0. **
1693 * The DLM is called such that it returns -EAGAIN if it would have blocked
1694 * waiting for the vote thread. In that case we unlock our page so the vote
1695 * thread can make progress. Once we've done this we have to return
1696 * AOP_TRUNCATED_PAGE so the aop method that called us can bubble that back up
1697 * into the VFS who will then immediately retry the aop call.
1699 * We do a blocking lock and immediate unlock before returning, though, so that
1700 * the lock has a great chance of being cached on this node by the time the VFS
1701 * calls back to retry the aop. This has a potential to livelock as nodes
1702 * ping locks back and forth, but that's a risk we're willing to take to avoid
1703 * the lock inversion simply.
1705 int ocfs2_meta_lock_with_page(struct inode *inode,
1706 struct ocfs2_journal_handle *handle,
1707 struct buffer_head **ret_bh,
1713 ret = ocfs2_meta_lock_full(inode, handle, ret_bh, ex,
1714 OCFS2_LOCK_NONBLOCK);
1715 if (ret == -EAGAIN) {
1717 if (ocfs2_meta_lock(inode, handle, ret_bh, ex) == 0)
1718 ocfs2_meta_unlock(inode, ex);
1719 ret = AOP_TRUNCATED_PAGE;
1725 void ocfs2_meta_unlock(struct inode *inode,
1728 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1729 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
1733 mlog(0, "inode %llu drop %s META lock\n",
1734 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1735 ex ? "EXMODE" : "PRMODE");
1737 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1738 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1743 int ocfs2_super_lock(struct ocfs2_super *osb,
1747 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1748 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1749 struct buffer_head *bh;
1750 struct ocfs2_slot_info *si = osb->slot_info;
1754 if (ocfs2_is_hard_readonly(osb))
1757 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
1763 /* The super block lock path is really in the best position to
1764 * know when resources covered by the lock need to be
1765 * refreshed, so we do it here. Of course, making sense of
1766 * everything is up to the caller :) */
1767 status = ocfs2_should_refresh_lock_res(lockres);
1774 status = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0,
1777 ocfs2_update_slot_info(si);
1779 ocfs2_complete_lock_res_refresh(lockres, status);
1789 void ocfs2_super_unlock(struct ocfs2_super *osb,
1792 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1793 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1795 ocfs2_cluster_unlock(osb, lockres, level);
1798 int ocfs2_rename_lock(struct ocfs2_super *osb)
1801 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1803 if (ocfs2_is_hard_readonly(osb))
1806 status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
1813 void ocfs2_rename_unlock(struct ocfs2_super *osb)
1815 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1817 ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
1820 int ocfs2_dentry_lock(struct dentry *dentry, int ex)
1823 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1824 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1825 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1829 if (ocfs2_is_hard_readonly(osb))
1832 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
1839 void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
1841 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1842 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1843 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1845 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
1848 /* Reference counting of the dlm debug structure. We want this because
1849 * open references on the debug inodes can live on after a mount, so
1850 * we can't rely on the ocfs2_super to always exist. */
1851 static void ocfs2_dlm_debug_free(struct kref *kref)
1853 struct ocfs2_dlm_debug *dlm_debug;
1855 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
1860 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
1863 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
1866 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
1868 kref_get(&debug->d_refcnt);
1871 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
1873 struct ocfs2_dlm_debug *dlm_debug;
1875 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
1877 mlog_errno(-ENOMEM);
1881 kref_init(&dlm_debug->d_refcnt);
1882 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
1883 dlm_debug->d_locking_state = NULL;
1888 /* Access to this is arbitrated for us via seq_file->sem. */
1889 struct ocfs2_dlm_seq_priv {
1890 struct ocfs2_dlm_debug *p_dlm_debug;
1891 struct ocfs2_lock_res p_iter_res;
1892 struct ocfs2_lock_res p_tmp_res;
1895 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
1896 struct ocfs2_dlm_seq_priv *priv)
1898 struct ocfs2_lock_res *iter, *ret = NULL;
1899 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
1901 assert_spin_locked(&ocfs2_dlm_tracking_lock);
1903 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
1904 /* discover the head of the list */
1905 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
1906 mlog(0, "End of list found, %p\n", ret);
1910 /* We track our "dummy" iteration lockres' by a NULL
1912 if (iter->l_ops != NULL) {
1921 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
1923 struct ocfs2_dlm_seq_priv *priv = m->private;
1924 struct ocfs2_lock_res *iter;
1926 spin_lock(&ocfs2_dlm_tracking_lock);
1927 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
1929 /* Since lockres' have the lifetime of their container
1930 * (which can be inodes, ocfs2_supers, etc) we want to
1931 * copy this out to a temporary lockres while still
1932 * under the spinlock. Obviously after this we can't
1933 * trust any pointers on the copy returned, but that's
1934 * ok as the information we want isn't typically held
1936 priv->p_tmp_res = *iter;
1937 iter = &priv->p_tmp_res;
1939 spin_unlock(&ocfs2_dlm_tracking_lock);
1944 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
1948 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
1950 struct ocfs2_dlm_seq_priv *priv = m->private;
1951 struct ocfs2_lock_res *iter = v;
1952 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
1954 spin_lock(&ocfs2_dlm_tracking_lock);
1955 iter = ocfs2_dlm_next_res(iter, priv);
1956 list_del_init(&dummy->l_debug_list);
1958 list_add(&dummy->l_debug_list, &iter->l_debug_list);
1959 priv->p_tmp_res = *iter;
1960 iter = &priv->p_tmp_res;
1962 spin_unlock(&ocfs2_dlm_tracking_lock);
1967 /* So that debugfs.ocfs2 can determine which format is being used */
1968 #define OCFS2_DLM_DEBUG_STR_VERSION 1
1969 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
1973 struct ocfs2_lock_res *lockres = v;
1978 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
1980 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
1981 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
1983 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
1985 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
1987 seq_printf(m, "%d\t"
1998 lockres->l_unlock_action,
1999 lockres->l_ro_holders,
2000 lockres->l_ex_holders,
2001 lockres->l_requested,
2002 lockres->l_blocking);
2004 /* Dump the raw LVB */
2005 lvb = lockres->l_lksb.lvb;
2006 for(i = 0; i < DLM_LVB_LEN; i++)
2007 seq_printf(m, "0x%x\t", lvb[i]);
2010 seq_printf(m, "\n");
2014 static struct seq_operations ocfs2_dlm_seq_ops = {
2015 .start = ocfs2_dlm_seq_start,
2016 .stop = ocfs2_dlm_seq_stop,
2017 .next = ocfs2_dlm_seq_next,
2018 .show = ocfs2_dlm_seq_show,
2021 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2023 struct seq_file *seq = (struct seq_file *) file->private_data;
2024 struct ocfs2_dlm_seq_priv *priv = seq->private;
2025 struct ocfs2_lock_res *res = &priv->p_iter_res;
2027 ocfs2_remove_lockres_tracking(res);
2028 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2029 return seq_release_private(inode, file);
2032 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2035 struct ocfs2_dlm_seq_priv *priv;
2036 struct seq_file *seq;
2037 struct ocfs2_super *osb;
2039 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2045 osb = (struct ocfs2_super *) inode->u.generic_ip;
2046 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2047 priv->p_dlm_debug = osb->osb_dlm_debug;
2048 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2050 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2057 seq = (struct seq_file *) file->private_data;
2058 seq->private = priv;
2060 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2067 static const struct file_operations ocfs2_dlm_debug_fops = {
2068 .open = ocfs2_dlm_debug_open,
2069 .release = ocfs2_dlm_debug_release,
2071 .llseek = seq_lseek,
2074 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2077 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2079 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2081 osb->osb_debug_root,
2083 &ocfs2_dlm_debug_fops);
2084 if (!dlm_debug->d_locking_state) {
2087 "Unable to create locking state debugfs file.\n");
2091 ocfs2_get_dlm_debug(dlm_debug);
2096 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2098 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2101 debugfs_remove(dlm_debug->d_locking_state);
2102 ocfs2_put_dlm_debug(dlm_debug);
2106 int ocfs2_dlm_init(struct ocfs2_super *osb)
2110 struct dlm_ctxt *dlm;
2114 status = ocfs2_dlm_init_debug(osb);
2120 /* launch vote thread */
2121 osb->vote_task = kthread_run(ocfs2_vote_thread, osb, "ocfs2vote");
2122 if (IS_ERR(osb->vote_task)) {
2123 status = PTR_ERR(osb->vote_task);
2124 osb->vote_task = NULL;
2129 /* used by the dlm code to make message headers unique, each
2130 * node in this domain must agree on this. */
2131 dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
2133 /* for now, uuid == domain */
2134 dlm = dlm_register_domain(osb->uuid_str, dlm_key);
2136 status = PTR_ERR(dlm);
2141 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2142 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2144 dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
2151 ocfs2_dlm_shutdown_debug(osb);
2153 kthread_stop(osb->vote_task);
2160 void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2164 dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
2166 ocfs2_drop_osb_locks(osb);
2168 if (osb->vote_task) {
2169 kthread_stop(osb->vote_task);
2170 osb->vote_task = NULL;
2173 ocfs2_lock_res_free(&osb->osb_super_lockres);
2174 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2176 dlm_unregister_domain(osb->dlm);
2179 ocfs2_dlm_shutdown_debug(osb);
2184 static void ocfs2_unlock_ast(void *opaque, enum dlm_status status)
2186 struct ocfs2_lock_res *lockres = opaque;
2187 unsigned long flags;
2191 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2192 lockres->l_unlock_action);
2194 spin_lock_irqsave(&lockres->l_lock, flags);
2195 /* We tried to cancel a convert request, but it was already
2196 * granted. All we want to do here is clear our unlock
2197 * state. The wake_up call done at the bottom is redundant
2198 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2199 * hurt anything anyway */
2200 if (status == DLM_CANCELGRANT &&
2201 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2202 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2204 /* We don't clear the busy flag in this case as it
2205 * should have been cleared by the ast which the dlm
2207 goto complete_unlock;
2210 if (status != DLM_NORMAL) {
2211 mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
2212 "unlock_action %d\n", status, lockres->l_name,
2213 lockres->l_unlock_action);
2214 spin_unlock_irqrestore(&lockres->l_lock, flags);
2218 switch(lockres->l_unlock_action) {
2219 case OCFS2_UNLOCK_CANCEL_CONVERT:
2220 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2221 lockres->l_action = OCFS2_AST_INVALID;
2223 case OCFS2_UNLOCK_DROP_LOCK:
2224 lockres->l_level = LKM_IVMODE;
2230 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2232 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2233 spin_unlock_irqrestore(&lockres->l_lock, flags);
2235 wake_up(&lockres->l_event);
2240 typedef void (ocfs2_pre_drop_cb_t)(struct ocfs2_lock_res *, void *);
2242 struct drop_lock_cb {
2243 ocfs2_pre_drop_cb_t *drop_func;
2247 static int ocfs2_drop_lock(struct ocfs2_super *osb,
2248 struct ocfs2_lock_res *lockres,
2249 struct drop_lock_cb *dcb)
2251 enum dlm_status status;
2252 unsigned long flags;
2255 /* We didn't get anywhere near actually using this lockres. */
2256 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2259 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
2260 lkm_flags |= LKM_VALBLK;
2262 spin_lock_irqsave(&lockres->l_lock, flags);
2264 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2265 "lockres %s, flags 0x%lx\n",
2266 lockres->l_name, lockres->l_flags);
2268 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2269 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2270 "%u, unlock_action = %u\n",
2271 lockres->l_name, lockres->l_flags, lockres->l_action,
2272 lockres->l_unlock_action);
2274 spin_unlock_irqrestore(&lockres->l_lock, flags);
2276 /* XXX: Today we just wait on any busy
2277 * locks... Perhaps we need to cancel converts in the
2279 ocfs2_wait_on_busy_lock(lockres);
2281 spin_lock_irqsave(&lockres->l_lock, flags);
2285 dcb->drop_func(lockres, dcb->drop_data);
2287 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2288 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2290 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2291 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2293 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2294 spin_unlock_irqrestore(&lockres->l_lock, flags);
2298 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2300 /* make sure we never get here while waiting for an ast to
2302 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2304 /* is this necessary? */
2305 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2306 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2307 spin_unlock_irqrestore(&lockres->l_lock, flags);
2309 mlog(0, "lock %s\n", lockres->l_name);
2311 status = dlmunlock(osb->dlm, &lockres->l_lksb, lkm_flags,
2312 ocfs2_unlock_ast, lockres);
2313 if (status != DLM_NORMAL) {
2314 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2315 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2316 dlm_print_one_lock(lockres->l_lksb.lockid);
2319 mlog(0, "lock %s, successfull return from dlmunlock\n",
2322 ocfs2_wait_on_busy_lock(lockres);
2328 /* Mark the lockres as being dropped. It will no longer be
2329 * queued if blocking, but we still may have to wait on it
2330 * being dequeued from the vote thread before we can consider
2333 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2334 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2337 struct ocfs2_mask_waiter mw;
2338 unsigned long flags;
2340 ocfs2_init_mask_waiter(&mw);
2342 spin_lock_irqsave(&lockres->l_lock, flags);
2343 lockres->l_flags |= OCFS2_LOCK_FREEING;
2344 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2345 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2346 spin_unlock_irqrestore(&lockres->l_lock, flags);
2348 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2350 status = ocfs2_wait_for_mask(&mw);
2354 spin_lock_irqsave(&lockres->l_lock, flags);
2356 spin_unlock_irqrestore(&lockres->l_lock, flags);
2359 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2360 struct ocfs2_lock_res *lockres)
2364 ocfs2_mark_lockres_freeing(lockres);
2365 ret = ocfs2_drop_lock(osb, lockres, NULL);
2370 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2372 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2373 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
2376 static void ocfs2_meta_pre_drop(struct ocfs2_lock_res *lockres, void *data)
2378 struct inode *inode = data;
2380 /* the metadata lock requires a bit more work as we have an
2381 * LVB to worry about. */
2382 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2383 lockres->l_level == LKM_EXMODE &&
2384 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2385 __ocfs2_stuff_meta_lvb(inode);
2388 int ocfs2_drop_inode_locks(struct inode *inode)
2391 struct drop_lock_cb meta_dcb = { ocfs2_meta_pre_drop, inode, };
2395 /* No need to call ocfs2_mark_lockres_freeing here -
2396 * ocfs2_clear_inode has done it for us. */
2398 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2399 &OCFS2_I(inode)->ip_data_lockres,
2406 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2407 &OCFS2_I(inode)->ip_meta_lockres,
2411 if (err < 0 && !status)
2414 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2415 &OCFS2_I(inode)->ip_rw_lockres,
2419 if (err < 0 && !status)
2426 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2429 assert_spin_locked(&lockres->l_lock);
2431 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
2433 if (lockres->l_level <= new_level) {
2434 mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
2435 lockres->l_level, new_level);
2439 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2440 lockres->l_name, new_level, lockres->l_blocking);
2442 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2443 lockres->l_requested = new_level;
2444 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2447 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2448 struct ocfs2_lock_res *lockres,
2452 int ret, dlm_flags = LKM_CONVERT;
2453 enum dlm_status status;
2458 dlm_flags |= LKM_VALBLK;
2460 status = dlmlock(osb->dlm,
2465 OCFS2_LOCK_ID_MAX_LEN - 1,
2468 ocfs2_blocking_ast);
2469 if (status != DLM_NORMAL) {
2470 ocfs2_log_dlm_error("dlmlock", status, lockres);
2472 ocfs2_recover_from_dlm_error(lockres, 1);
2482 /* returns 1 when the caller should unlock and call dlmunlock */
2483 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2484 struct ocfs2_lock_res *lockres)
2486 assert_spin_locked(&lockres->l_lock);
2489 mlog(0, "lock %s\n", lockres->l_name);
2491 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2492 /* If we're already trying to cancel a lock conversion
2493 * then just drop the spinlock and allow the caller to
2494 * requeue this lock. */
2496 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2500 /* were we in a convert when we got the bast fire? */
2501 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2502 lockres->l_action != OCFS2_AST_DOWNCONVERT);
2503 /* set things up for the unlockast to know to just
2504 * clear out the ast_action and unset busy, etc. */
2505 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2507 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2508 "lock %s, invalid flags: 0x%lx\n",
2509 lockres->l_name, lockres->l_flags);
2514 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2515 struct ocfs2_lock_res *lockres)
2518 enum dlm_status status;
2521 mlog(0, "lock %s\n", lockres->l_name);
2524 status = dlmunlock(osb->dlm,
2529 if (status != DLM_NORMAL) {
2530 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2532 ocfs2_recover_from_dlm_error(lockres, 0);
2535 mlog(0, "lock %s return from dlmunlock\n", lockres->l_name);
2541 static inline int ocfs2_can_downconvert_meta_lock(struct inode *inode,
2542 struct ocfs2_lock_res *lockres,
2549 BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
2551 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2553 mlog(0, "lockres %s currently being refreshed -- backing "
2554 "off!\n", lockres->l_name);
2555 } else if (new_level == LKM_PRMODE)
2556 ret = !lockres->l_ex_holders &&
2557 ocfs2_inode_fully_checkpointed(inode);
2558 else /* Must be NLMODE we're converting to. */
2559 ret = !lockres->l_ro_holders && !lockres->l_ex_holders &&
2560 ocfs2_inode_fully_checkpointed(inode);
2566 static int ocfs2_do_unblock_meta(struct inode *inode,
2572 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
2573 unsigned long flags;
2575 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2579 spin_lock_irqsave(&lockres->l_lock, flags);
2581 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2583 mlog(0, "l_level=%d, l_blocking=%d\n", lockres->l_level,
2584 lockres->l_blocking);
2586 BUG_ON(lockres->l_level != LKM_EXMODE &&
2587 lockres->l_level != LKM_PRMODE);
2589 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2591 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2592 spin_unlock_irqrestore(&lockres->l_lock, flags);
2594 ret = ocfs2_cancel_convert(osb, lockres);
2601 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2603 mlog(0, "l_level=%d, l_blocking=%d, new_level=%d\n",
2604 lockres->l_level, lockres->l_blocking, new_level);
2606 if (ocfs2_can_downconvert_meta_lock(inode, lockres, new_level)) {
2607 if (lockres->l_level == LKM_EXMODE)
2610 /* If the lock hasn't been refreshed yet (rare), then
2611 * our memory inode values are old and we skip
2612 * stuffing the lvb. There's no need to actually clear
2613 * out the lvb here as it's value is still valid. */
2614 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2616 __ocfs2_stuff_meta_lvb(inode);
2618 mlog(0, "lockres %s: downconverting stale lock!\n",
2621 mlog(0, "calling ocfs2_downconvert_lock with l_level=%d, "
2622 "l_blocking=%d, new_level=%d\n",
2623 lockres->l_level, lockres->l_blocking, new_level);
2625 ocfs2_prepare_downconvert(lockres, new_level);
2626 spin_unlock_irqrestore(&lockres->l_lock, flags);
2627 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
2630 if (!ocfs2_inode_fully_checkpointed(inode))
2631 ocfs2_start_checkpoint(osb);
2634 spin_unlock_irqrestore(&lockres->l_lock, flags);
2641 static int ocfs2_generic_unblock_lock(struct ocfs2_super *osb,
2642 struct ocfs2_lock_res *lockres,
2643 struct ocfs2_unblock_ctl *ctl,
2644 ocfs2_convert_worker_t *worker)
2646 unsigned long flags;
2654 spin_lock_irqsave(&lockres->l_lock, flags);
2656 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2659 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2661 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2662 spin_unlock_irqrestore(&lockres->l_lock, flags);
2664 ret = ocfs2_cancel_convert(osb, lockres);
2671 /* if we're blocking an exclusive and we have *any* holders,
2673 if ((lockres->l_blocking == LKM_EXMODE)
2674 && (lockres->l_ex_holders || lockres->l_ro_holders))
2677 /* If it's a PR we're blocking, then only
2678 * requeue if we've got any EX holders */
2679 if (lockres->l_blocking == LKM_PRMODE &&
2680 lockres->l_ex_holders)
2684 * Can we get a lock in this state if the holder counts are
2685 * zero? The meta data unblock code used to check this.
2687 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
2688 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
2691 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2693 if (lockres->l_ops->check_downconvert
2694 && !lockres->l_ops->check_downconvert(lockres, new_level))
2697 /* If we get here, then we know that there are no more
2698 * incompatible holders (and anyone asking for an incompatible
2699 * lock is blocked). We can now downconvert the lock */
2703 /* Some lockres types want to do a bit of work before
2704 * downconverting a lock. Allow that here. The worker function
2705 * may sleep, so we save off a copy of what we're blocking as
2706 * it may change while we're not holding the spin lock. */
2707 blocking = lockres->l_blocking;
2708 spin_unlock_irqrestore(&lockres->l_lock, flags);
2710 ctl->unblock_action = worker(lockres, blocking);
2712 if (ctl->unblock_action == UNBLOCK_STOP_POST)
2715 spin_lock_irqsave(&lockres->l_lock, flags);
2716 if (blocking != lockres->l_blocking) {
2717 /* If this changed underneath us, then we can't drop
2725 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2726 if (lockres->l_level == LKM_EXMODE)
2730 * We only set the lvb if the lock has been fully
2731 * refreshed - otherwise we risk setting stale
2732 * data. Otherwise, there's no need to actually clear
2733 * out the lvb here as it's value is still valid.
2735 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2736 lockres->l_ops->set_lvb(lockres);
2739 ocfs2_prepare_downconvert(lockres, new_level);
2740 spin_unlock_irqrestore(&lockres->l_lock, flags);
2741 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
2747 spin_unlock_irqrestore(&lockres->l_lock, flags);
2754 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
2757 struct inode *inode;
2758 struct address_space *mapping;
2760 inode = ocfs2_lock_res_inode(lockres);
2761 mapping = inode->i_mapping;
2763 if (filemap_fdatawrite(mapping)) {
2764 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
2765 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2767 sync_mapping_buffers(mapping);
2768 if (blocking == LKM_EXMODE) {
2769 truncate_inode_pages(mapping, 0);
2770 unmap_mapping_range(mapping, 0, 0, 0);
2772 /* We only need to wait on the I/O if we're not also
2773 * truncating pages because truncate_inode_pages waits
2774 * for us above. We don't truncate pages if we're
2775 * blocking anything < EXMODE because we want to keep
2776 * them around in that case. */
2777 filemap_fdatawait(mapping);
2780 return UNBLOCK_CONTINUE;
2783 int ocfs2_unblock_data(struct ocfs2_lock_res *lockres,
2784 struct ocfs2_unblock_ctl *ctl)
2787 struct inode *inode;
2788 struct ocfs2_super *osb;
2792 inode = ocfs2_lock_res_inode(lockres);
2793 osb = OCFS2_SB(inode->i_sb);
2795 mlog(0, "unblock inode %llu\n",
2796 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2798 status = ocfs2_generic_unblock_lock(osb, lockres, ctl,
2799 ocfs2_data_convert_worker);
2803 mlog(0, "inode %llu, requeue = %d\n",
2804 (unsigned long long)OCFS2_I(inode)->ip_blkno, ctl->requeue);
2810 static int ocfs2_unblock_inode_lock(struct ocfs2_lock_res *lockres,
2811 struct ocfs2_unblock_ctl *ctl)
2814 struct inode *inode;
2818 mlog(0, "Unblock lockres %s\n", lockres->l_name);
2820 inode = ocfs2_lock_res_inode(lockres);
2822 status = ocfs2_generic_unblock_lock(OCFS2_SB(inode->i_sb),
2823 lockres, ctl, NULL);
2831 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
2834 struct inode *inode = ocfs2_lock_res_inode(lockres);
2835 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
2837 BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
2838 BUG_ON(lockres->l_level != LKM_EXMODE && !checkpointed);
2843 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
2847 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
2849 struct inode *inode = ocfs2_lock_res_inode(lockres);
2851 __ocfs2_stuff_meta_lvb(inode);
2854 static int ocfs2_unblock_meta(struct ocfs2_lock_res *lockres,
2855 struct ocfs2_unblock_ctl *ctl)
2858 struct inode *inode;
2862 inode = ocfs2_lock_res_inode(lockres);
2864 mlog(0, "unblock inode %llu\n",
2865 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2867 status = ocfs2_generic_unblock_lock(OCFS2_SB(inode->i_sb),
2868 lockres, ctl, NULL);
2872 mlog(0, "inode %llu, requeue = %d\n",
2873 (unsigned long long)OCFS2_I(inode)->ip_blkno, ctl->requeue);
2880 * Does the final reference drop on our dentry lock. Right now this
2881 * happens in the vote thread, but we could choose to simplify the
2882 * dlmglue API and push these off to the ocfs2_wq in the future.
2884 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
2885 struct ocfs2_lock_res *lockres)
2887 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2888 ocfs2_dentry_lock_put(osb, dl);
2892 * d_delete() matching dentries before the lock downconvert.
2894 * At this point, any process waiting to destroy the
2895 * dentry_lock due to last ref count is stopped by the
2896 * OCFS2_LOCK_QUEUED flag.
2898 * We have two potential problems
2900 * 1) If we do the last reference drop on our dentry_lock (via dput)
2901 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
2902 * the downconvert to finish. Instead we take an elevated
2903 * reference and push the drop until after we've completed our
2904 * unblock processing.
2906 * 2) There might be another process with a final reference,
2907 * waiting on us to finish processing. If this is the case, we
2908 * detect it and exit out - there's no more dentries anyway.
2910 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
2913 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2914 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
2915 struct dentry *dentry;
2916 unsigned long flags;
2920 * This node is blocking another node from getting a read
2921 * lock. This happens when we've renamed within a
2922 * directory. We've forced the other nodes to d_delete(), but
2923 * we never actually dropped our lock because it's still
2924 * valid. The downconvert code will retain a PR for this node,
2925 * so there's no further work to do.
2927 if (blocking == LKM_PRMODE)
2928 return UNBLOCK_CONTINUE;
2931 * Mark this inode as potentially orphaned. The code in
2932 * ocfs2_delete_inode() will figure out whether it actually
2933 * needs to be freed or not.
2935 spin_lock(&oi->ip_lock);
2936 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
2937 spin_unlock(&oi->ip_lock);
2940 * Yuck. We need to make sure however that the check of
2941 * OCFS2_LOCK_FREEING and the extra reference are atomic with
2942 * respect to a reference decrement or the setting of that
2945 spin_lock_irqsave(&lockres->l_lock, flags);
2946 spin_lock(&dentry_attach_lock);
2947 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
2952 spin_unlock(&dentry_attach_lock);
2953 spin_unlock_irqrestore(&lockres->l_lock, flags);
2955 mlog(0, "extra_ref = %d\n", extra_ref);
2958 * We have a process waiting on us in ocfs2_dentry_iput(),
2959 * which means we can't have any more outstanding
2960 * aliases. There's no need to do any more work.
2963 return UNBLOCK_CONTINUE;
2965 spin_lock(&dentry_attach_lock);
2967 dentry = ocfs2_find_local_alias(dl->dl_inode,
2968 dl->dl_parent_blkno, 1);
2971 spin_unlock(&dentry_attach_lock);
2973 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
2974 dentry->d_name.name);
2977 * The following dcache calls may do an
2978 * iput(). Normally we don't want that from the
2979 * downconverting thread, but in this case it's ok
2980 * because the requesting node already has an
2981 * exclusive lock on the inode, so it can't be queued
2982 * for a downconvert.
2987 spin_lock(&dentry_attach_lock);
2989 spin_unlock(&dentry_attach_lock);
2992 * If we are the last holder of this dentry lock, there is no
2993 * reason to downconvert so skip straight to the unlock.
2995 if (dl->dl_count == 1)
2996 return UNBLOCK_STOP_POST;
2998 return UNBLOCK_CONTINUE_POST;
3001 static int ocfs2_unblock_dentry_lock(struct ocfs2_lock_res *lockres,
3002 struct ocfs2_unblock_ctl *ctl)
3005 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3006 struct ocfs2_super *osb = OCFS2_SB(dl->dl_inode->i_sb);
3008 mlog(0, "unblock dentry lock: %llu\n",
3009 (unsigned long long)OCFS2_I(dl->dl_inode)->ip_blkno);
3011 ret = ocfs2_generic_unblock_lock(osb,
3014 ocfs2_dentry_convert_worker);
3018 mlog(0, "requeue = %d, post = %d\n", ctl->requeue, ctl->unblock_action);
3023 /* Generic unblock function for any lockres whose private data is an
3024 * ocfs2_super pointer. */
3025 static int ocfs2_unblock_osb_lock(struct ocfs2_lock_res *lockres,
3026 struct ocfs2_unblock_ctl *ctl)
3029 struct ocfs2_super *osb;
3033 mlog(0, "Unblock lockres %s\n", lockres->l_name);
3035 osb = ocfs2_get_lockres_osb(lockres);
3037 status = ocfs2_generic_unblock_lock(osb,
3048 void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3049 struct ocfs2_lock_res *lockres)
3052 struct ocfs2_unblock_ctl ctl = {0, 0,};
3053 unsigned long flags;
3055 /* Our reference to the lockres in this function can be
3056 * considered valid until we remove the OCFS2_LOCK_QUEUED
3062 BUG_ON(!lockres->l_ops);
3063 BUG_ON(!lockres->l_ops->unblock);
3065 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3067 /* Detect whether a lock has been marked as going away while
3068 * the vote thread was processing other things. A lock can
3069 * still be marked with OCFS2_LOCK_FREEING after this check,
3070 * but short circuiting here will still save us some
3072 spin_lock_irqsave(&lockres->l_lock, flags);
3073 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3075 spin_unlock_irqrestore(&lockres->l_lock, flags);
3077 status = lockres->l_ops->unblock(lockres, &ctl);
3081 spin_lock_irqsave(&lockres->l_lock, flags);
3083 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
3084 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3086 ocfs2_schedule_blocked_lock(osb, lockres);
3088 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
3089 ctl.requeue ? "yes" : "no");
3090 spin_unlock_irqrestore(&lockres->l_lock, flags);
3092 if (ctl.unblock_action != UNBLOCK_CONTINUE
3093 && lockres->l_ops->post_unlock)
3094 lockres->l_ops->post_unlock(osb, lockres);
3099 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3100 struct ocfs2_lock_res *lockres)
3104 assert_spin_locked(&lockres->l_lock);
3106 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3107 /* Do not schedule a lock for downconvert when it's on
3108 * the way to destruction - any nodes wanting access
3109 * to the resource will get it soon. */
3110 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3111 lockres->l_name, lockres->l_flags);
3115 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3117 spin_lock(&osb->vote_task_lock);
3118 if (list_empty(&lockres->l_blocked_list)) {
3119 list_add_tail(&lockres->l_blocked_list,
3120 &osb->blocked_lock_list);
3121 osb->blocked_lock_count++;
3123 spin_unlock(&osb->vote_task_lock);
3128 /* This aids in debugging situations where a bad LVB might be involved. */
3129 void ocfs2_dump_meta_lvb_info(u64 level,
3130 const char *function,
3132 struct ocfs2_lock_res *lockres)
3134 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
3136 mlog(level, "LVB information for %s (called from %s:%u):\n",
3137 lockres->l_name, function, line);
3138 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
3139 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
3140 be32_to_cpu(lvb->lvb_igeneration));
3141 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
3142 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
3143 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
3144 be16_to_cpu(lvb->lvb_imode));
3145 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
3146 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
3147 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
3148 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
3149 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
3150 be32_to_cpu(lvb->lvb_iattr));