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 void ocfs2_inode_bast_func(void *opaque,
72 static void ocfs2_dentry_bast_func(void *opaque,
74 static void ocfs2_super_bast_func(void *opaque,
76 static void ocfs2_rename_bast_func(void *opaque,
78 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
79 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
82 * Return value from ocfs2_convert_worker_t functions.
84 * These control the precise actions of ocfs2_generic_unblock_lock()
85 * and ocfs2_process_blocked_lock()
88 enum ocfs2_unblock_action {
89 UNBLOCK_CONTINUE = 0, /* Continue downconvert */
90 UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire
91 * ->post_unlock callback */
92 UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire
93 * ->post_unlock() callback. */
96 struct ocfs2_unblock_ctl {
98 enum ocfs2_unblock_action unblock_action;
101 static int ocfs2_unblock_meta(struct ocfs2_lock_res *lockres,
102 struct ocfs2_unblock_ctl *ctl);
103 static int ocfs2_unblock_data(struct ocfs2_lock_res *lockres,
104 struct ocfs2_unblock_ctl *ctl);
105 static int ocfs2_unblock_inode_lock(struct ocfs2_lock_res *lockres,
106 struct ocfs2_unblock_ctl *ctl);
107 static int ocfs2_unblock_dentry_lock(struct ocfs2_lock_res *lockres,
108 struct ocfs2_unblock_ctl *ctl);
109 static int ocfs2_unblock_osb_lock(struct ocfs2_lock_res *lockres,
110 struct ocfs2_unblock_ctl *ctl);
112 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
113 struct ocfs2_lock_res *lockres);
116 * OCFS2 Lock Resource Operations
118 * These fine tune the behavior of the generic dlmglue locking infrastructure.
120 struct ocfs2_lock_res_ops {
122 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
123 * this callback if ->l_priv is not an ocfs2_super pointer
125 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
126 void (*bast)(void *, int);
127 int (*unblock)(struct ocfs2_lock_res *, struct ocfs2_unblock_ctl *);
128 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
131 * LOCK_TYPE_* flags which describe the specific requirements
132 * of a lock type. Descriptions of each individual flag follow.
138 * Some locks want to "refresh" potentially stale data when a
139 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
140 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
141 * individual lockres l_flags member from the ast function. It is
142 * expected that the locking wrapper will clear the
143 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
145 #define LOCK_TYPE_REQUIRES_REFRESH 0x1
147 typedef int (ocfs2_convert_worker_t)(struct ocfs2_lock_res *, int);
148 static int ocfs2_generic_unblock_lock(struct ocfs2_super *osb,
149 struct ocfs2_lock_res *lockres,
150 struct ocfs2_unblock_ctl *ctl,
151 ocfs2_convert_worker_t *worker);
153 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
154 .get_osb = ocfs2_get_inode_osb,
155 .bast = ocfs2_inode_bast_func,
156 .unblock = ocfs2_unblock_inode_lock,
160 static struct ocfs2_lock_res_ops ocfs2_inode_meta_lops = {
161 .get_osb = ocfs2_get_inode_osb,
162 .bast = ocfs2_inode_bast_func,
163 .unblock = ocfs2_unblock_meta,
164 .flags = LOCK_TYPE_REQUIRES_REFRESH,
167 static struct ocfs2_lock_res_ops ocfs2_inode_data_lops = {
168 .get_osb = ocfs2_get_inode_osb,
169 .bast = ocfs2_inode_bast_func,
170 .unblock = ocfs2_unblock_data,
174 static struct ocfs2_lock_res_ops ocfs2_super_lops = {
175 .bast = ocfs2_super_bast_func,
176 .unblock = ocfs2_unblock_osb_lock,
177 .flags = LOCK_TYPE_REQUIRES_REFRESH,
180 static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
181 .bast = ocfs2_rename_bast_func,
182 .unblock = ocfs2_unblock_osb_lock,
186 static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
187 .get_osb = ocfs2_get_dentry_osb,
188 .bast = ocfs2_dentry_bast_func,
189 .unblock = ocfs2_unblock_dentry_lock,
190 .post_unlock = ocfs2_dentry_post_unlock,
194 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
196 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
197 lockres->l_type == OCFS2_LOCK_TYPE_DATA ||
198 lockres->l_type == OCFS2_LOCK_TYPE_RW;
201 static inline int ocfs2_is_super_lock(struct ocfs2_lock_res *lockres)
203 return lockres->l_type == OCFS2_LOCK_TYPE_SUPER;
206 static inline int ocfs2_is_rename_lock(struct ocfs2_lock_res *lockres)
208 return lockres->l_type == OCFS2_LOCK_TYPE_RENAME;
211 static inline struct ocfs2_super *ocfs2_lock_res_super(struct ocfs2_lock_res *lockres)
213 BUG_ON(!ocfs2_is_super_lock(lockres)
214 && !ocfs2_is_rename_lock(lockres));
216 return (struct ocfs2_super *) lockres->l_priv;
219 static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
221 BUG_ON(!ocfs2_is_inode_lock(lockres));
223 return (struct inode *) lockres->l_priv;
226 static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
228 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
230 return (struct ocfs2_dentry_lock *)lockres->l_priv;
233 static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
235 if (lockres->l_ops->get_osb)
236 return lockres->l_ops->get_osb(lockres);
238 return (struct ocfs2_super *)lockres->l_priv;
241 static int ocfs2_lock_create(struct ocfs2_super *osb,
242 struct ocfs2_lock_res *lockres,
245 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
247 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
248 struct ocfs2_lock_res *lockres,
250 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
251 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
252 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
253 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
254 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
255 struct ocfs2_lock_res *lockres);
256 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
258 #define ocfs2_log_dlm_error(_func, _stat, _lockres) do { \
259 mlog(ML_ERROR, "Dlm error \"%s\" while calling %s on " \
260 "resource %s: %s\n", dlm_errname(_stat), _func, \
261 _lockres->l_name, dlm_errmsg(_stat)); \
263 static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
264 struct ocfs2_lock_res *lockres);
265 static int ocfs2_meta_lock_update(struct inode *inode,
266 struct buffer_head **bh);
267 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
268 static inline int ocfs2_highest_compat_lock_level(int level);
269 static inline int ocfs2_can_downconvert_meta_lock(struct inode *inode,
270 struct ocfs2_lock_res *lockres,
273 static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
282 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
284 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
285 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
286 (long long)blkno, generation);
288 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
290 mlog(0, "built lock resource with name: %s\n", name);
295 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
297 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
298 struct ocfs2_dlm_debug *dlm_debug)
300 mlog(0, "Add tracking for lockres %s\n", res->l_name);
302 spin_lock(&ocfs2_dlm_tracking_lock);
303 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
304 spin_unlock(&ocfs2_dlm_tracking_lock);
307 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
309 spin_lock(&ocfs2_dlm_tracking_lock);
310 if (!list_empty(&res->l_debug_list))
311 list_del_init(&res->l_debug_list);
312 spin_unlock(&ocfs2_dlm_tracking_lock);
315 static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
316 struct ocfs2_lock_res *res,
317 enum ocfs2_lock_type type,
318 struct ocfs2_lock_res_ops *ops,
325 res->l_level = LKM_IVMODE;
326 res->l_requested = LKM_IVMODE;
327 res->l_blocking = LKM_IVMODE;
328 res->l_action = OCFS2_AST_INVALID;
329 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
331 res->l_flags = OCFS2_LOCK_INITIALIZED;
333 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
336 void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
338 /* This also clears out the lock status block */
339 memset(res, 0, sizeof(struct ocfs2_lock_res));
340 spin_lock_init(&res->l_lock);
341 init_waitqueue_head(&res->l_event);
342 INIT_LIST_HEAD(&res->l_blocked_list);
343 INIT_LIST_HEAD(&res->l_mask_waiters);
346 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
347 enum ocfs2_lock_type type,
348 unsigned int generation,
351 struct ocfs2_lock_res_ops *ops;
354 case OCFS2_LOCK_TYPE_RW:
355 ops = &ocfs2_inode_rw_lops;
357 case OCFS2_LOCK_TYPE_META:
358 ops = &ocfs2_inode_meta_lops;
360 case OCFS2_LOCK_TYPE_DATA:
361 ops = &ocfs2_inode_data_lops;
364 mlog_bug_on_msg(1, "type: %d\n", type);
365 ops = NULL; /* thanks, gcc */
369 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
370 generation, res->l_name);
371 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
374 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
376 struct inode *inode = ocfs2_lock_res_inode(lockres);
378 return OCFS2_SB(inode->i_sb);
381 static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
383 __be64 inode_blkno_be;
385 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
388 return be64_to_cpu(inode_blkno_be);
391 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
393 struct ocfs2_dentry_lock *dl = lockres->l_priv;
395 return OCFS2_SB(dl->dl_inode->i_sb);
398 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
399 u64 parent, struct inode *inode)
402 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
403 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
404 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
406 ocfs2_lock_res_init_once(lockres);
409 * Unfortunately, the standard lock naming scheme won't work
410 * here because we have two 16 byte values to use. Instead,
411 * we'll stuff the inode number as a binary value. We still
412 * want error prints to show something without garbling the
413 * display, so drop a null byte in there before the inode
414 * number. A future version of OCFS2 will likely use all
415 * binary lock names. The stringified names have been a
416 * tremendous aid in debugging, but now that the debugfs
417 * interface exists, we can mangle things there if need be.
419 * NOTE: We also drop the standard "pad" value (the total lock
420 * name size stays the same though - the last part is all
421 * zeros due to the memset in ocfs2_lock_res_init_once()
423 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
425 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
428 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
430 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
433 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
434 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
438 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
439 struct ocfs2_super *osb)
441 /* Superblock lockres doesn't come from a slab so we call init
442 * once on it manually. */
443 ocfs2_lock_res_init_once(res);
444 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
446 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
447 &ocfs2_super_lops, osb);
450 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
451 struct ocfs2_super *osb)
453 /* Rename 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_RENAME, 0, 0, res->l_name);
457 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
458 &ocfs2_rename_lops, osb);
461 void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
465 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
468 ocfs2_remove_lockres_tracking(res);
470 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
471 "Lockres %s is on the blocked list\n",
473 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
474 "Lockres %s has mask waiters pending\n",
476 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
477 "Lockres %s is locked\n",
479 mlog_bug_on_msg(res->l_ro_holders,
480 "Lockres %s has %u ro holders\n",
481 res->l_name, res->l_ro_holders);
482 mlog_bug_on_msg(res->l_ex_holders,
483 "Lockres %s has %u ex holders\n",
484 res->l_name, res->l_ex_holders);
486 /* Need to clear out the lock status block for the dlm */
487 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
493 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
502 lockres->l_ex_holders++;
505 lockres->l_ro_holders++;
514 static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
523 BUG_ON(!lockres->l_ex_holders);
524 lockres->l_ex_holders--;
527 BUG_ON(!lockres->l_ro_holders);
528 lockres->l_ro_holders--;
536 /* WARNING: This function lives in a world where the only three lock
537 * levels are EX, PR, and NL. It *will* have to be adjusted when more
538 * lock types are added. */
539 static inline int ocfs2_highest_compat_lock_level(int level)
541 int new_level = LKM_EXMODE;
543 if (level == LKM_EXMODE)
544 new_level = LKM_NLMODE;
545 else if (level == LKM_PRMODE)
546 new_level = LKM_PRMODE;
550 static void lockres_set_flags(struct ocfs2_lock_res *lockres,
551 unsigned long newflags)
553 struct list_head *pos, *tmp;
554 struct ocfs2_mask_waiter *mw;
556 assert_spin_locked(&lockres->l_lock);
558 lockres->l_flags = newflags;
560 list_for_each_safe(pos, tmp, &lockres->l_mask_waiters) {
561 mw = list_entry(pos, struct ocfs2_mask_waiter, mw_item);
562 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
565 list_del_init(&mw->mw_item);
567 complete(&mw->mw_complete);
570 static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
572 lockres_set_flags(lockres, lockres->l_flags | or);
574 static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
577 lockres_set_flags(lockres, lockres->l_flags & ~clear);
580 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
584 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
585 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
586 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
587 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
589 lockres->l_level = lockres->l_requested;
590 if (lockres->l_level <=
591 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
592 lockres->l_blocking = LKM_NLMODE;
593 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
595 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
600 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
604 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
605 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
607 /* Convert from RO to EX doesn't really need anything as our
608 * information is already up to data. Convert from NL to
609 * *anything* however should mark ourselves as needing an
611 if (lockres->l_level == LKM_NLMODE &&
612 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
613 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
615 lockres->l_level = lockres->l_requested;
616 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
621 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
625 BUG_ON((!lockres->l_flags & OCFS2_LOCK_BUSY));
626 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
628 if (lockres->l_requested > LKM_NLMODE &&
629 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
630 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
631 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
633 lockres->l_level = lockres->l_requested;
634 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
635 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
640 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
643 int needs_downconvert = 0;
646 assert_spin_locked(&lockres->l_lock);
648 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
650 if (level > lockres->l_blocking) {
651 /* only schedule a downconvert if we haven't already scheduled
652 * one that goes low enough to satisfy the level we're
653 * blocking. this also catches the case where we get
655 if (ocfs2_highest_compat_lock_level(level) <
656 ocfs2_highest_compat_lock_level(lockres->l_blocking))
657 needs_downconvert = 1;
659 lockres->l_blocking = level;
662 mlog_exit(needs_downconvert);
663 return needs_downconvert;
666 static void ocfs2_generic_bast_func(struct ocfs2_super *osb,
667 struct ocfs2_lock_res *lockres,
670 int needs_downconvert;
675 BUG_ON(level <= LKM_NLMODE);
677 spin_lock_irqsave(&lockres->l_lock, flags);
678 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
679 if (needs_downconvert)
680 ocfs2_schedule_blocked_lock(osb, lockres);
681 spin_unlock_irqrestore(&lockres->l_lock, flags);
683 wake_up(&lockres->l_event);
685 ocfs2_kick_vote_thread(osb);
690 static void ocfs2_inode_bast_func(void *opaque, int level)
692 struct ocfs2_lock_res *lockres = opaque;
694 struct ocfs2_super *osb;
698 BUG_ON(!ocfs2_is_inode_lock(lockres));
700 inode = ocfs2_lock_res_inode(lockres);
701 osb = OCFS2_SB(inode->i_sb);
703 mlog(0, "BAST fired for inode %llu, blocking %d, level %d type %s\n",
704 (unsigned long long)OCFS2_I(inode)->ip_blkno, level,
705 lockres->l_level, ocfs2_lock_type_string(lockres->l_type));
707 ocfs2_generic_bast_func(osb, lockres, level);
712 static void ocfs2_locking_ast(void *opaque)
714 struct ocfs2_lock_res *lockres = opaque;
715 struct dlm_lockstatus *lksb = &lockres->l_lksb;
718 spin_lock_irqsave(&lockres->l_lock, flags);
720 if (lksb->status != DLM_NORMAL) {
721 mlog(ML_ERROR, "lockres %s: lksb status value of %u!\n",
722 lockres->l_name, lksb->status);
723 spin_unlock_irqrestore(&lockres->l_lock, flags);
727 switch(lockres->l_action) {
728 case OCFS2_AST_ATTACH:
729 ocfs2_generic_handle_attach_action(lockres);
730 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
732 case OCFS2_AST_CONVERT:
733 ocfs2_generic_handle_convert_action(lockres);
735 case OCFS2_AST_DOWNCONVERT:
736 ocfs2_generic_handle_downconvert_action(lockres);
739 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
740 "lockres flags = 0x%lx, unlock action: %u\n",
741 lockres->l_name, lockres->l_action, lockres->l_flags,
742 lockres->l_unlock_action);
746 /* set it to something invalid so if we get called again we
748 lockres->l_action = OCFS2_AST_INVALID;
750 wake_up(&lockres->l_event);
751 spin_unlock_irqrestore(&lockres->l_lock, flags);
754 static void ocfs2_super_bast_func(void *opaque,
757 struct ocfs2_lock_res *lockres = opaque;
758 struct ocfs2_super *osb;
761 mlog(0, "Superblock BAST fired\n");
763 BUG_ON(!ocfs2_is_super_lock(lockres));
764 osb = ocfs2_lock_res_super(lockres);
765 ocfs2_generic_bast_func(osb, lockres, level);
770 static void ocfs2_rename_bast_func(void *opaque,
773 struct ocfs2_lock_res *lockres = opaque;
774 struct ocfs2_super *osb;
778 mlog(0, "Rename BAST fired\n");
780 BUG_ON(!ocfs2_is_rename_lock(lockres));
782 osb = ocfs2_lock_res_super(lockres);
783 ocfs2_generic_bast_func(osb, lockres, level);
788 static void ocfs2_dentry_bast_func(void *opaque, int level)
790 struct ocfs2_lock_res *lockres = opaque;
791 struct ocfs2_dentry_lock *dl = lockres->l_priv;
792 struct ocfs2_super *osb = OCFS2_SB(dl->dl_inode->i_sb);
794 mlog(0, "Dentry bast: level: %d, name: %s\n", level,
797 ocfs2_generic_bast_func(osb, lockres, level);
800 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
806 spin_lock_irqsave(&lockres->l_lock, flags);
807 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
809 lockres->l_action = OCFS2_AST_INVALID;
811 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
812 spin_unlock_irqrestore(&lockres->l_lock, flags);
814 wake_up(&lockres->l_event);
818 /* Note: If we detect another process working on the lock (i.e.,
819 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
820 * to do the right thing in that case.
822 static int ocfs2_lock_create(struct ocfs2_super *osb,
823 struct ocfs2_lock_res *lockres,
828 enum dlm_status status;
833 mlog(0, "lock %s, level = %d, flags = %d\n", lockres->l_name, level,
836 spin_lock_irqsave(&lockres->l_lock, flags);
837 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
838 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
839 spin_unlock_irqrestore(&lockres->l_lock, flags);
843 lockres->l_action = OCFS2_AST_ATTACH;
844 lockres->l_requested = level;
845 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
846 spin_unlock_irqrestore(&lockres->l_lock, flags);
848 status = dlmlock(osb->dlm,
853 OCFS2_LOCK_ID_MAX_LEN - 1,
856 lockres->l_ops->bast);
857 if (status != DLM_NORMAL) {
858 ocfs2_log_dlm_error("dlmlock", status, lockres);
860 ocfs2_recover_from_dlm_error(lockres, 1);
863 mlog(0, "lock %s, successfull return from dlmlock\n", lockres->l_name);
870 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
876 spin_lock_irqsave(&lockres->l_lock, flags);
877 ret = lockres->l_flags & flag;
878 spin_unlock_irqrestore(&lockres->l_lock, flags);
883 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
886 wait_event(lockres->l_event,
887 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
890 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
893 wait_event(lockres->l_event,
894 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
897 /* predict what lock level we'll be dropping down to on behalf
898 * of another node, and return true if the currently wanted
899 * level will be compatible with it. */
900 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
903 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
905 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
908 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
910 INIT_LIST_HEAD(&mw->mw_item);
911 init_completion(&mw->mw_complete);
914 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
916 wait_for_completion(&mw->mw_complete);
917 /* Re-arm the completion in case we want to wait on it again */
918 INIT_COMPLETION(mw->mw_complete);
919 return mw->mw_status;
922 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
923 struct ocfs2_mask_waiter *mw,
927 BUG_ON(!list_empty(&mw->mw_item));
929 assert_spin_locked(&lockres->l_lock);
931 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
936 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
937 * if the mask still hadn't reached its goal */
938 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
939 struct ocfs2_mask_waiter *mw)
944 spin_lock_irqsave(&lockres->l_lock, flags);
945 if (!list_empty(&mw->mw_item)) {
946 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
949 list_del_init(&mw->mw_item);
950 init_completion(&mw->mw_complete);
952 spin_unlock_irqrestore(&lockres->l_lock, flags);
958 static int ocfs2_cluster_lock(struct ocfs2_super *osb,
959 struct ocfs2_lock_res *lockres,
964 struct ocfs2_mask_waiter mw;
965 enum dlm_status status;
966 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
967 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
972 ocfs2_init_mask_waiter(&mw);
977 if (catch_signals && signal_pending(current)) {
982 spin_lock_irqsave(&lockres->l_lock, flags);
984 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
985 "Cluster lock called on freeing lockres %s! flags "
986 "0x%lx\n", lockres->l_name, lockres->l_flags);
988 /* We only compare against the currently granted level
989 * here. If the lock is blocked waiting on a downconvert,
990 * we'll get caught below. */
991 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
992 level > lockres->l_level) {
993 /* is someone sitting in dlm_lock? If so, wait on
995 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1000 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1001 /* lock has not been created yet. */
1002 spin_unlock_irqrestore(&lockres->l_lock, flags);
1004 ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
1012 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1013 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1014 /* is the lock is currently blocked on behalf of
1016 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1021 if (level > lockres->l_level) {
1022 if (lockres->l_action != OCFS2_AST_INVALID)
1023 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1024 lockres->l_name, lockres->l_action);
1026 lockres->l_action = OCFS2_AST_CONVERT;
1027 lockres->l_requested = level;
1028 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1029 spin_unlock_irqrestore(&lockres->l_lock, flags);
1031 BUG_ON(level == LKM_IVMODE);
1032 BUG_ON(level == LKM_NLMODE);
1034 mlog(0, "lock %s, convert from %d to level = %d\n",
1035 lockres->l_name, lockres->l_level, level);
1037 /* call dlm_lock to upgrade lock now */
1038 status = dlmlock(osb->dlm,
1041 lkm_flags|LKM_CONVERT|LKM_VALBLK,
1043 OCFS2_LOCK_ID_MAX_LEN - 1,
1046 lockres->l_ops->bast);
1047 if (status != DLM_NORMAL) {
1048 if ((lkm_flags & LKM_NOQUEUE) &&
1049 (status == DLM_NOTQUEUED))
1052 ocfs2_log_dlm_error("dlmlock", status,
1056 ocfs2_recover_from_dlm_error(lockres, 1);
1060 mlog(0, "lock %s, successfull return from dlmlock\n",
1063 /* At this point we've gone inside the dlm and need to
1064 * complete our work regardless. */
1067 /* wait for busy to clear and carry on */
1071 /* Ok, if we get here then we're good to go. */
1072 ocfs2_inc_holders(lockres, level);
1076 spin_unlock_irqrestore(&lockres->l_lock, flags);
1079 * This is helping work around a lock inversion between the page lock
1080 * and dlm locks. One path holds the page lock while calling aops
1081 * which block acquiring dlm locks. The voting thread holds dlm
1082 * locks while acquiring page locks while down converting data locks.
1083 * This block is helping an aop path notice the inversion and back
1084 * off to unlock its page lock before trying the dlm lock again.
1086 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1087 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1089 if (lockres_remove_mask_waiter(lockres, &mw))
1095 ret = ocfs2_wait_for_mask(&mw);
1105 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1106 struct ocfs2_lock_res *lockres,
1109 unsigned long flags;
1112 spin_lock_irqsave(&lockres->l_lock, flags);
1113 ocfs2_dec_holders(lockres, level);
1114 ocfs2_vote_on_unlock(osb, lockres);
1115 spin_unlock_irqrestore(&lockres->l_lock, flags);
1119 int ocfs2_create_new_lock(struct ocfs2_super *osb,
1120 struct ocfs2_lock_res *lockres,
1124 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1125 unsigned long flags;
1126 int lkm_flags = local ? LKM_LOCAL : 0;
1128 spin_lock_irqsave(&lockres->l_lock, flags);
1129 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1130 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1131 spin_unlock_irqrestore(&lockres->l_lock, flags);
1133 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
1136 /* Grants us an EX lock on the data and metadata resources, skipping
1137 * the normal cluster directory lookup. Use this ONLY on newly created
1138 * inodes which other nodes can't possibly see, and which haven't been
1139 * hashed in the inode hash yet. This can give us a good performance
1140 * increase as it'll skip the network broadcast normally associated
1141 * with creating a new lock resource. */
1142 int ocfs2_create_new_inode_locks(struct inode *inode)
1145 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1148 BUG_ON(!ocfs2_inode_is_new(inode));
1152 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1154 /* NOTE: That we don't increment any of the holder counts, nor
1155 * do we add anything to a journal handle. Since this is
1156 * supposed to be a new inode which the cluster doesn't know
1157 * about yet, there is no need to. As far as the LVB handling
1158 * is concerned, this is basically like acquiring an EX lock
1159 * on a resource which has an invalid one -- we'll set it
1160 * valid when we release the EX. */
1162 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
1169 * We don't want to use LKM_LOCAL on a meta data lock as they
1170 * don't use a generation in their lock names.
1172 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_meta_lockres, 1, 0);
1178 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_data_lockres, 1, 1);
1189 int ocfs2_rw_lock(struct inode *inode, int write)
1192 struct ocfs2_lock_res *lockres;
1198 mlog(0, "inode %llu take %s RW lock\n",
1199 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1200 write ? "EXMODE" : "PRMODE");
1202 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1204 level = write ? LKM_EXMODE : LKM_PRMODE;
1206 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1215 void ocfs2_rw_unlock(struct inode *inode, int write)
1217 int level = write ? LKM_EXMODE : LKM_PRMODE;
1218 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1222 mlog(0, "inode %llu drop %s RW lock\n",
1223 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1224 write ? "EXMODE" : "PRMODE");
1226 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1231 int ocfs2_data_lock_full(struct inode *inode,
1235 int status = 0, level;
1236 struct ocfs2_lock_res *lockres;
1242 mlog(0, "inode %llu take %s DATA lock\n",
1243 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1244 write ? "EXMODE" : "PRMODE");
1246 /* We'll allow faking a readonly data lock for
1248 if (ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb))) {
1256 lockres = &OCFS2_I(inode)->ip_data_lockres;
1258 level = write ? LKM_EXMODE : LKM_PRMODE;
1260 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level,
1262 if (status < 0 && status != -EAGAIN)
1270 /* see ocfs2_meta_lock_with_page() */
1271 int ocfs2_data_lock_with_page(struct inode *inode,
1277 ret = ocfs2_data_lock_full(inode, write, OCFS2_LOCK_NONBLOCK);
1278 if (ret == -EAGAIN) {
1280 if (ocfs2_data_lock(inode, write) == 0)
1281 ocfs2_data_unlock(inode, write);
1282 ret = AOP_TRUNCATED_PAGE;
1288 static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
1289 struct ocfs2_lock_res *lockres)
1295 /* If we know that another node is waiting on our lock, kick
1296 * the vote thread * pre-emptively when we reach a release
1298 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1299 switch(lockres->l_blocking) {
1301 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1305 if (!lockres->l_ex_holders)
1314 ocfs2_kick_vote_thread(osb);
1319 void ocfs2_data_unlock(struct inode *inode,
1322 int level = write ? LKM_EXMODE : LKM_PRMODE;
1323 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_data_lockres;
1327 mlog(0, "inode %llu drop %s DATA lock\n",
1328 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1329 write ? "EXMODE" : "PRMODE");
1331 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1332 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1337 #define OCFS2_SEC_BITS 34
1338 #define OCFS2_SEC_SHIFT (64 - 34)
1339 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1341 /* LVB only has room for 64 bits of time here so we pack it for
1343 static u64 ocfs2_pack_timespec(struct timespec *spec)
1346 u64 sec = spec->tv_sec;
1347 u32 nsec = spec->tv_nsec;
1349 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1354 /* Call this with the lockres locked. I am reasonably sure we don't
1355 * need ip_lock in this function as anyone who would be changing those
1356 * values is supposed to be blocked in ocfs2_meta_lock right now. */
1357 static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1359 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1360 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1361 struct ocfs2_meta_lvb *lvb;
1365 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1368 * Invalidate the LVB of a deleted inode - this way other
1369 * nodes are forced to go to disk and discover the new inode
1372 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1373 lvb->lvb_version = 0;
1377 lvb->lvb_version = OCFS2_LVB_VERSION;
1378 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1379 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1380 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1381 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1382 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1383 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1384 lvb->lvb_iatime_packed =
1385 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1386 lvb->lvb_ictime_packed =
1387 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1388 lvb->lvb_imtime_packed =
1389 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1390 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
1391 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
1394 mlog_meta_lvb(0, lockres);
1399 static void ocfs2_unpack_timespec(struct timespec *spec,
1402 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1403 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1406 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1408 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1409 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1410 struct ocfs2_meta_lvb *lvb;
1414 mlog_meta_lvb(0, lockres);
1416 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1418 /* We're safe here without the lockres lock... */
1419 spin_lock(&oi->ip_lock);
1420 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1421 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1423 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1424 ocfs2_set_inode_flags(inode);
1426 /* fast-symlinks are a special case */
1427 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1428 inode->i_blocks = 0;
1431 ocfs2_align_bytes_to_sectors(i_size_read(inode));
1433 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1434 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1435 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1436 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1437 ocfs2_unpack_timespec(&inode->i_atime,
1438 be64_to_cpu(lvb->lvb_iatime_packed));
1439 ocfs2_unpack_timespec(&inode->i_mtime,
1440 be64_to_cpu(lvb->lvb_imtime_packed));
1441 ocfs2_unpack_timespec(&inode->i_ctime,
1442 be64_to_cpu(lvb->lvb_ictime_packed));
1443 spin_unlock(&oi->ip_lock);
1448 static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1449 struct ocfs2_lock_res *lockres)
1451 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1453 if (lvb->lvb_version == OCFS2_LVB_VERSION
1454 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
1459 /* Determine whether a lock resource needs to be refreshed, and
1460 * arbitrate who gets to refresh it.
1462 * 0 means no refresh needed.
1464 * > 0 means you need to refresh this and you MUST call
1465 * ocfs2_complete_lock_res_refresh afterwards. */
1466 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1468 unsigned long flags;
1474 spin_lock_irqsave(&lockres->l_lock, flags);
1475 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1476 spin_unlock_irqrestore(&lockres->l_lock, flags);
1480 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1481 spin_unlock_irqrestore(&lockres->l_lock, flags);
1483 ocfs2_wait_on_refreshing_lock(lockres);
1487 /* Ok, I'll be the one to refresh this lock. */
1488 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1489 spin_unlock_irqrestore(&lockres->l_lock, flags);
1497 /* If status is non zero, I'll mark it as not being in refresh
1498 * anymroe, but i won't clear the needs refresh flag. */
1499 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1502 unsigned long flags;
1505 spin_lock_irqsave(&lockres->l_lock, flags);
1506 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1508 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1509 spin_unlock_irqrestore(&lockres->l_lock, flags);
1511 wake_up(&lockres->l_event);
1516 /* may or may not return a bh if it went to disk. */
1517 static int ocfs2_meta_lock_update(struct inode *inode,
1518 struct buffer_head **bh)
1521 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1522 struct ocfs2_lock_res *lockres;
1523 struct ocfs2_dinode *fe;
1527 spin_lock(&oi->ip_lock);
1528 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1529 mlog(0, "Orphaned inode %llu was deleted while we "
1530 "were waiting on a lock. ip_flags = 0x%x\n",
1531 (unsigned long long)oi->ip_blkno, oi->ip_flags);
1532 spin_unlock(&oi->ip_lock);
1536 spin_unlock(&oi->ip_lock);
1538 lockres = &oi->ip_meta_lockres;
1540 if (!ocfs2_should_refresh_lock_res(lockres))
1543 /* This will discard any caching information we might have had
1544 * for the inode metadata. */
1545 ocfs2_metadata_cache_purge(inode);
1547 /* will do nothing for inode types that don't use the extent
1548 * map (directories, bitmap files, etc) */
1549 ocfs2_extent_map_trunc(inode, 0);
1551 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
1552 mlog(0, "Trusting LVB on inode %llu\n",
1553 (unsigned long long)oi->ip_blkno);
1554 ocfs2_refresh_inode_from_lvb(inode);
1556 /* Boo, we have to go to disk. */
1557 /* read bh, cast, ocfs2_refresh_inode */
1558 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1559 bh, OCFS2_BH_CACHED, inode);
1564 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1566 /* This is a good chance to make sure we're not
1567 * locking an invalid object.
1569 * We bug on a stale inode here because we checked
1570 * above whether it was wiped from disk. The wiping
1571 * node provides a guarantee that we receive that
1572 * message and can mark the inode before dropping any
1573 * locks associated with it. */
1574 if (!OCFS2_IS_VALID_DINODE(fe)) {
1575 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1579 mlog_bug_on_msg(inode->i_generation !=
1580 le32_to_cpu(fe->i_generation),
1581 "Invalid dinode %llu disk generation: %u "
1582 "inode->i_generation: %u\n",
1583 (unsigned long long)oi->ip_blkno,
1584 le32_to_cpu(fe->i_generation),
1585 inode->i_generation);
1586 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1587 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
1588 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1589 (unsigned long long)oi->ip_blkno,
1590 (unsigned long long)le64_to_cpu(fe->i_dtime),
1591 le32_to_cpu(fe->i_flags));
1593 ocfs2_refresh_inode(inode, fe);
1598 ocfs2_complete_lock_res_refresh(lockres, status);
1604 static int ocfs2_assign_bh(struct inode *inode,
1605 struct buffer_head **ret_bh,
1606 struct buffer_head *passed_bh)
1611 /* Ok, the update went to disk for us, use the
1613 *ret_bh = passed_bh;
1619 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1620 OCFS2_I(inode)->ip_blkno,
1631 * returns < 0 error if the callback will never be called, otherwise
1632 * the result of the lock will be communicated via the callback.
1634 int ocfs2_meta_lock_full(struct inode *inode,
1635 struct ocfs2_journal_handle *handle,
1636 struct buffer_head **ret_bh,
1640 int status, level, dlm_flags, acquired;
1641 struct ocfs2_lock_res *lockres;
1642 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1643 struct buffer_head *local_bh = NULL;
1649 mlog(0, "inode %llu, take %s META lock\n",
1650 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1651 ex ? "EXMODE" : "PRMODE");
1655 /* We'll allow faking a readonly metadata lock for
1657 if (ocfs2_is_hard_readonly(osb)) {
1663 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1664 wait_event(osb->recovery_event,
1665 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1668 lockres = &OCFS2_I(inode)->ip_meta_lockres;
1669 level = ex ? LKM_EXMODE : LKM_PRMODE;
1671 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
1672 dlm_flags |= LKM_NOQUEUE;
1674 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1676 if (status != -EAGAIN && status != -EIOCBRETRY)
1681 /* Notify the error cleanup path to drop the cluster lock. */
1684 /* We wait twice because a node may have died while we were in
1685 * the lower dlm layers. The second time though, we've
1686 * committed to owning this lock so we don't allow signals to
1687 * abort the operation. */
1688 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1689 wait_event(osb->recovery_event,
1690 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1693 * We only see this flag if we're being called from
1694 * ocfs2_read_locked_inode(). It means we're locking an inode
1695 * which hasn't been populated yet, so clear the refresh flag
1696 * and let the caller handle it.
1698 if (inode->i_state & I_NEW) {
1700 ocfs2_complete_lock_res_refresh(lockres, 0);
1704 /* This is fun. The caller may want a bh back, or it may
1705 * not. ocfs2_meta_lock_update definitely wants one in, but
1706 * may or may not read one, depending on what's in the
1707 * LVB. The result of all of this is that we've *only* gone to
1708 * disk if we have to, so the complexity is worthwhile. */
1709 status = ocfs2_meta_lock_update(inode, &local_bh);
1711 if (status != -ENOENT)
1717 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
1725 status = ocfs2_handle_add_lock(handle, inode);
1732 if (ret_bh && (*ret_bh)) {
1737 ocfs2_meta_unlock(inode, ex);
1748 * This is working around a lock inversion between tasks acquiring DLM locks
1749 * while holding a page lock and the vote thread which blocks dlm lock acquiry
1750 * while acquiring page locks.
1752 * ** These _with_page variantes are only intended to be called from aop
1753 * methods that hold page locks and return a very specific *positive* error
1754 * code that aop methods pass up to the VFS -- test for errors with != 0. **
1756 * The DLM is called such that it returns -EAGAIN if it would have blocked
1757 * waiting for the vote thread. In that case we unlock our page so the vote
1758 * thread can make progress. Once we've done this we have to return
1759 * AOP_TRUNCATED_PAGE so the aop method that called us can bubble that back up
1760 * into the VFS who will then immediately retry the aop call.
1762 * We do a blocking lock and immediate unlock before returning, though, so that
1763 * the lock has a great chance of being cached on this node by the time the VFS
1764 * calls back to retry the aop. This has a potential to livelock as nodes
1765 * ping locks back and forth, but that's a risk we're willing to take to avoid
1766 * the lock inversion simply.
1768 int ocfs2_meta_lock_with_page(struct inode *inode,
1769 struct ocfs2_journal_handle *handle,
1770 struct buffer_head **ret_bh,
1776 ret = ocfs2_meta_lock_full(inode, handle, ret_bh, ex,
1777 OCFS2_LOCK_NONBLOCK);
1778 if (ret == -EAGAIN) {
1780 if (ocfs2_meta_lock(inode, handle, ret_bh, ex) == 0)
1781 ocfs2_meta_unlock(inode, ex);
1782 ret = AOP_TRUNCATED_PAGE;
1788 void ocfs2_meta_unlock(struct inode *inode,
1791 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1792 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
1796 mlog(0, "inode %llu drop %s META lock\n",
1797 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1798 ex ? "EXMODE" : "PRMODE");
1800 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1801 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1806 int ocfs2_super_lock(struct ocfs2_super *osb,
1810 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1811 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1812 struct buffer_head *bh;
1813 struct ocfs2_slot_info *si = osb->slot_info;
1817 if (ocfs2_is_hard_readonly(osb))
1820 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
1826 /* The super block lock path is really in the best position to
1827 * know when resources covered by the lock need to be
1828 * refreshed, so we do it here. Of course, making sense of
1829 * everything is up to the caller :) */
1830 status = ocfs2_should_refresh_lock_res(lockres);
1837 status = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0,
1840 ocfs2_update_slot_info(si);
1842 ocfs2_complete_lock_res_refresh(lockres, status);
1852 void ocfs2_super_unlock(struct ocfs2_super *osb,
1855 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1856 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1858 ocfs2_cluster_unlock(osb, lockres, level);
1861 int ocfs2_rename_lock(struct ocfs2_super *osb)
1864 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1866 if (ocfs2_is_hard_readonly(osb))
1869 status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
1876 void ocfs2_rename_unlock(struct ocfs2_super *osb)
1878 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1880 ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
1883 int ocfs2_dentry_lock(struct dentry *dentry, int ex)
1886 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1887 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1888 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1892 if (ocfs2_is_hard_readonly(osb))
1895 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
1902 void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
1904 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1905 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1906 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1908 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
1911 /* Reference counting of the dlm debug structure. We want this because
1912 * open references on the debug inodes can live on after a mount, so
1913 * we can't rely on the ocfs2_super to always exist. */
1914 static void ocfs2_dlm_debug_free(struct kref *kref)
1916 struct ocfs2_dlm_debug *dlm_debug;
1918 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
1923 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
1926 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
1929 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
1931 kref_get(&debug->d_refcnt);
1934 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
1936 struct ocfs2_dlm_debug *dlm_debug;
1938 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
1940 mlog_errno(-ENOMEM);
1944 kref_init(&dlm_debug->d_refcnt);
1945 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
1946 dlm_debug->d_locking_state = NULL;
1951 /* Access to this is arbitrated for us via seq_file->sem. */
1952 struct ocfs2_dlm_seq_priv {
1953 struct ocfs2_dlm_debug *p_dlm_debug;
1954 struct ocfs2_lock_res p_iter_res;
1955 struct ocfs2_lock_res p_tmp_res;
1958 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
1959 struct ocfs2_dlm_seq_priv *priv)
1961 struct ocfs2_lock_res *iter, *ret = NULL;
1962 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
1964 assert_spin_locked(&ocfs2_dlm_tracking_lock);
1966 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
1967 /* discover the head of the list */
1968 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
1969 mlog(0, "End of list found, %p\n", ret);
1973 /* We track our "dummy" iteration lockres' by a NULL
1975 if (iter->l_ops != NULL) {
1984 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
1986 struct ocfs2_dlm_seq_priv *priv = m->private;
1987 struct ocfs2_lock_res *iter;
1989 spin_lock(&ocfs2_dlm_tracking_lock);
1990 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
1992 /* Since lockres' have the lifetime of their container
1993 * (which can be inodes, ocfs2_supers, etc) we want to
1994 * copy this out to a temporary lockres while still
1995 * under the spinlock. Obviously after this we can't
1996 * trust any pointers on the copy returned, but that's
1997 * ok as the information we want isn't typically held
1999 priv->p_tmp_res = *iter;
2000 iter = &priv->p_tmp_res;
2002 spin_unlock(&ocfs2_dlm_tracking_lock);
2007 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2011 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2013 struct ocfs2_dlm_seq_priv *priv = m->private;
2014 struct ocfs2_lock_res *iter = v;
2015 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2017 spin_lock(&ocfs2_dlm_tracking_lock);
2018 iter = ocfs2_dlm_next_res(iter, priv);
2019 list_del_init(&dummy->l_debug_list);
2021 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2022 priv->p_tmp_res = *iter;
2023 iter = &priv->p_tmp_res;
2025 spin_unlock(&ocfs2_dlm_tracking_lock);
2030 /* So that debugfs.ocfs2 can determine which format is being used */
2031 #define OCFS2_DLM_DEBUG_STR_VERSION 1
2032 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2036 struct ocfs2_lock_res *lockres = v;
2041 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2043 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2044 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2046 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2048 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2050 seq_printf(m, "%d\t"
2061 lockres->l_unlock_action,
2062 lockres->l_ro_holders,
2063 lockres->l_ex_holders,
2064 lockres->l_requested,
2065 lockres->l_blocking);
2067 /* Dump the raw LVB */
2068 lvb = lockres->l_lksb.lvb;
2069 for(i = 0; i < DLM_LVB_LEN; i++)
2070 seq_printf(m, "0x%x\t", lvb[i]);
2073 seq_printf(m, "\n");
2077 static struct seq_operations ocfs2_dlm_seq_ops = {
2078 .start = ocfs2_dlm_seq_start,
2079 .stop = ocfs2_dlm_seq_stop,
2080 .next = ocfs2_dlm_seq_next,
2081 .show = ocfs2_dlm_seq_show,
2084 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2086 struct seq_file *seq = (struct seq_file *) file->private_data;
2087 struct ocfs2_dlm_seq_priv *priv = seq->private;
2088 struct ocfs2_lock_res *res = &priv->p_iter_res;
2090 ocfs2_remove_lockres_tracking(res);
2091 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2092 return seq_release_private(inode, file);
2095 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2098 struct ocfs2_dlm_seq_priv *priv;
2099 struct seq_file *seq;
2100 struct ocfs2_super *osb;
2102 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2108 osb = (struct ocfs2_super *) inode->u.generic_ip;
2109 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2110 priv->p_dlm_debug = osb->osb_dlm_debug;
2111 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2113 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2120 seq = (struct seq_file *) file->private_data;
2121 seq->private = priv;
2123 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2130 static const struct file_operations ocfs2_dlm_debug_fops = {
2131 .open = ocfs2_dlm_debug_open,
2132 .release = ocfs2_dlm_debug_release,
2134 .llseek = seq_lseek,
2137 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2140 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2142 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2144 osb->osb_debug_root,
2146 &ocfs2_dlm_debug_fops);
2147 if (!dlm_debug->d_locking_state) {
2150 "Unable to create locking state debugfs file.\n");
2154 ocfs2_get_dlm_debug(dlm_debug);
2159 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2161 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2164 debugfs_remove(dlm_debug->d_locking_state);
2165 ocfs2_put_dlm_debug(dlm_debug);
2169 int ocfs2_dlm_init(struct ocfs2_super *osb)
2173 struct dlm_ctxt *dlm;
2177 status = ocfs2_dlm_init_debug(osb);
2183 /* launch vote thread */
2184 osb->vote_task = kthread_run(ocfs2_vote_thread, osb, "ocfs2vote");
2185 if (IS_ERR(osb->vote_task)) {
2186 status = PTR_ERR(osb->vote_task);
2187 osb->vote_task = NULL;
2192 /* used by the dlm code to make message headers unique, each
2193 * node in this domain must agree on this. */
2194 dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
2196 /* for now, uuid == domain */
2197 dlm = dlm_register_domain(osb->uuid_str, dlm_key);
2199 status = PTR_ERR(dlm);
2204 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2205 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2207 dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
2214 ocfs2_dlm_shutdown_debug(osb);
2216 kthread_stop(osb->vote_task);
2223 void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2227 dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
2229 ocfs2_drop_osb_locks(osb);
2231 if (osb->vote_task) {
2232 kthread_stop(osb->vote_task);
2233 osb->vote_task = NULL;
2236 ocfs2_lock_res_free(&osb->osb_super_lockres);
2237 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2239 dlm_unregister_domain(osb->dlm);
2242 ocfs2_dlm_shutdown_debug(osb);
2247 static void ocfs2_unlock_ast(void *opaque, enum dlm_status status)
2249 struct ocfs2_lock_res *lockres = opaque;
2250 unsigned long flags;
2254 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2255 lockres->l_unlock_action);
2257 spin_lock_irqsave(&lockres->l_lock, flags);
2258 /* We tried to cancel a convert request, but it was already
2259 * granted. All we want to do here is clear our unlock
2260 * state. The wake_up call done at the bottom is redundant
2261 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2262 * hurt anything anyway */
2263 if (status == DLM_CANCELGRANT &&
2264 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2265 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2267 /* We don't clear the busy flag in this case as it
2268 * should have been cleared by the ast which the dlm
2270 goto complete_unlock;
2273 if (status != DLM_NORMAL) {
2274 mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
2275 "unlock_action %d\n", status, lockres->l_name,
2276 lockres->l_unlock_action);
2277 spin_unlock_irqrestore(&lockres->l_lock, flags);
2281 switch(lockres->l_unlock_action) {
2282 case OCFS2_UNLOCK_CANCEL_CONVERT:
2283 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2284 lockres->l_action = OCFS2_AST_INVALID;
2286 case OCFS2_UNLOCK_DROP_LOCK:
2287 lockres->l_level = LKM_IVMODE;
2293 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2295 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2296 spin_unlock_irqrestore(&lockres->l_lock, flags);
2298 wake_up(&lockres->l_event);
2303 typedef void (ocfs2_pre_drop_cb_t)(struct ocfs2_lock_res *, void *);
2305 struct drop_lock_cb {
2306 ocfs2_pre_drop_cb_t *drop_func;
2310 static int ocfs2_drop_lock(struct ocfs2_super *osb,
2311 struct ocfs2_lock_res *lockres,
2312 struct drop_lock_cb *dcb)
2314 enum dlm_status status;
2315 unsigned long flags;
2317 /* We didn't get anywhere near actually using this lockres. */
2318 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2321 spin_lock_irqsave(&lockres->l_lock, flags);
2323 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2324 "lockres %s, flags 0x%lx\n",
2325 lockres->l_name, lockres->l_flags);
2327 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2328 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2329 "%u, unlock_action = %u\n",
2330 lockres->l_name, lockres->l_flags, lockres->l_action,
2331 lockres->l_unlock_action);
2333 spin_unlock_irqrestore(&lockres->l_lock, flags);
2335 /* XXX: Today we just wait on any busy
2336 * locks... Perhaps we need to cancel converts in the
2338 ocfs2_wait_on_busy_lock(lockres);
2340 spin_lock_irqsave(&lockres->l_lock, flags);
2344 dcb->drop_func(lockres, dcb->drop_data);
2346 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2347 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2349 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2350 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2352 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2353 spin_unlock_irqrestore(&lockres->l_lock, flags);
2357 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2359 /* make sure we never get here while waiting for an ast to
2361 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2363 /* is this necessary? */
2364 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2365 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2366 spin_unlock_irqrestore(&lockres->l_lock, flags);
2368 mlog(0, "lock %s\n", lockres->l_name);
2370 status = dlmunlock(osb->dlm, &lockres->l_lksb, LKM_VALBLK,
2371 ocfs2_unlock_ast, lockres);
2372 if (status != DLM_NORMAL) {
2373 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2374 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2375 dlm_print_one_lock(lockres->l_lksb.lockid);
2378 mlog(0, "lock %s, successfull return from dlmunlock\n",
2381 ocfs2_wait_on_busy_lock(lockres);
2387 /* Mark the lockres as being dropped. It will no longer be
2388 * queued if blocking, but we still may have to wait on it
2389 * being dequeued from the vote thread before we can consider
2392 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2393 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2396 struct ocfs2_mask_waiter mw;
2397 unsigned long flags;
2399 ocfs2_init_mask_waiter(&mw);
2401 spin_lock_irqsave(&lockres->l_lock, flags);
2402 lockres->l_flags |= OCFS2_LOCK_FREEING;
2403 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2404 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2405 spin_unlock_irqrestore(&lockres->l_lock, flags);
2407 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2409 status = ocfs2_wait_for_mask(&mw);
2413 spin_lock_irqsave(&lockres->l_lock, flags);
2415 spin_unlock_irqrestore(&lockres->l_lock, flags);
2418 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2419 struct ocfs2_lock_res *lockres)
2423 ocfs2_mark_lockres_freeing(lockres);
2424 ret = ocfs2_drop_lock(osb, lockres, NULL);
2429 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2431 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2432 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
2435 static void ocfs2_meta_pre_drop(struct ocfs2_lock_res *lockres, void *data)
2437 struct inode *inode = data;
2439 /* the metadata lock requires a bit more work as we have an
2440 * LVB to worry about. */
2441 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2442 lockres->l_level == LKM_EXMODE &&
2443 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2444 __ocfs2_stuff_meta_lvb(inode);
2447 int ocfs2_drop_inode_locks(struct inode *inode)
2450 struct drop_lock_cb meta_dcb = { ocfs2_meta_pre_drop, inode, };
2454 /* No need to call ocfs2_mark_lockres_freeing here -
2455 * ocfs2_clear_inode has done it for us. */
2457 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2458 &OCFS2_I(inode)->ip_data_lockres,
2465 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2466 &OCFS2_I(inode)->ip_meta_lockres,
2470 if (err < 0 && !status)
2473 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2474 &OCFS2_I(inode)->ip_rw_lockres,
2478 if (err < 0 && !status)
2485 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2488 assert_spin_locked(&lockres->l_lock);
2490 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
2492 if (lockres->l_level <= new_level) {
2493 mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
2494 lockres->l_level, new_level);
2498 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2499 lockres->l_name, new_level, lockres->l_blocking);
2501 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2502 lockres->l_requested = new_level;
2503 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2506 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2507 struct ocfs2_lock_res *lockres,
2511 int ret, dlm_flags = LKM_CONVERT;
2512 enum dlm_status status;
2517 dlm_flags |= LKM_VALBLK;
2519 status = dlmlock(osb->dlm,
2524 OCFS2_LOCK_ID_MAX_LEN - 1,
2527 lockres->l_ops->bast);
2528 if (status != DLM_NORMAL) {
2529 ocfs2_log_dlm_error("dlmlock", status, lockres);
2531 ocfs2_recover_from_dlm_error(lockres, 1);
2541 /* returns 1 when the caller should unlock and call dlmunlock */
2542 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2543 struct ocfs2_lock_res *lockres)
2545 assert_spin_locked(&lockres->l_lock);
2548 mlog(0, "lock %s\n", lockres->l_name);
2550 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2551 /* If we're already trying to cancel a lock conversion
2552 * then just drop the spinlock and allow the caller to
2553 * requeue this lock. */
2555 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2559 /* were we in a convert when we got the bast fire? */
2560 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2561 lockres->l_action != OCFS2_AST_DOWNCONVERT);
2562 /* set things up for the unlockast to know to just
2563 * clear out the ast_action and unset busy, etc. */
2564 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2566 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2567 "lock %s, invalid flags: 0x%lx\n",
2568 lockres->l_name, lockres->l_flags);
2573 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2574 struct ocfs2_lock_res *lockres)
2577 enum dlm_status status;
2580 mlog(0, "lock %s\n", lockres->l_name);
2583 status = dlmunlock(osb->dlm,
2588 if (status != DLM_NORMAL) {
2589 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2591 ocfs2_recover_from_dlm_error(lockres, 0);
2594 mlog(0, "lock %s return from dlmunlock\n", lockres->l_name);
2600 static inline int ocfs2_can_downconvert_meta_lock(struct inode *inode,
2601 struct ocfs2_lock_res *lockres,
2608 BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
2610 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2612 mlog(0, "lockres %s currently being refreshed -- backing "
2613 "off!\n", lockres->l_name);
2614 } else if (new_level == LKM_PRMODE)
2615 ret = !lockres->l_ex_holders &&
2616 ocfs2_inode_fully_checkpointed(inode);
2617 else /* Must be NLMODE we're converting to. */
2618 ret = !lockres->l_ro_holders && !lockres->l_ex_holders &&
2619 ocfs2_inode_fully_checkpointed(inode);
2625 static int ocfs2_do_unblock_meta(struct inode *inode,
2631 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
2632 unsigned long flags;
2634 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2638 spin_lock_irqsave(&lockres->l_lock, flags);
2640 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2642 mlog(0, "l_level=%d, l_blocking=%d\n", lockres->l_level,
2643 lockres->l_blocking);
2645 BUG_ON(lockres->l_level != LKM_EXMODE &&
2646 lockres->l_level != LKM_PRMODE);
2648 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2650 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2651 spin_unlock_irqrestore(&lockres->l_lock, flags);
2653 ret = ocfs2_cancel_convert(osb, lockres);
2660 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2662 mlog(0, "l_level=%d, l_blocking=%d, new_level=%d\n",
2663 lockres->l_level, lockres->l_blocking, new_level);
2665 if (ocfs2_can_downconvert_meta_lock(inode, lockres, new_level)) {
2666 if (lockres->l_level == LKM_EXMODE)
2669 /* If the lock hasn't been refreshed yet (rare), then
2670 * our memory inode values are old and we skip
2671 * stuffing the lvb. There's no need to actually clear
2672 * out the lvb here as it's value is still valid. */
2673 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2675 __ocfs2_stuff_meta_lvb(inode);
2677 mlog(0, "lockres %s: downconverting stale lock!\n",
2680 mlog(0, "calling ocfs2_downconvert_lock with l_level=%d, "
2681 "l_blocking=%d, new_level=%d\n",
2682 lockres->l_level, lockres->l_blocking, new_level);
2684 ocfs2_prepare_downconvert(lockres, new_level);
2685 spin_unlock_irqrestore(&lockres->l_lock, flags);
2686 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
2689 if (!ocfs2_inode_fully_checkpointed(inode))
2690 ocfs2_start_checkpoint(osb);
2693 spin_unlock_irqrestore(&lockres->l_lock, flags);
2700 static int ocfs2_generic_unblock_lock(struct ocfs2_super *osb,
2701 struct ocfs2_lock_res *lockres,
2702 struct ocfs2_unblock_ctl *ctl,
2703 ocfs2_convert_worker_t *worker)
2705 unsigned long flags;
2712 spin_lock_irqsave(&lockres->l_lock, flags);
2714 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2717 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2719 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2720 spin_unlock_irqrestore(&lockres->l_lock, flags);
2722 ret = ocfs2_cancel_convert(osb, lockres);
2729 /* if we're blocking an exclusive and we have *any* holders,
2731 if ((lockres->l_blocking == LKM_EXMODE)
2732 && (lockres->l_ex_holders || lockres->l_ro_holders)) {
2733 spin_unlock_irqrestore(&lockres->l_lock, flags);
2739 /* If it's a PR we're blocking, then only
2740 * requeue if we've got any EX holders */
2741 if (lockres->l_blocking == LKM_PRMODE &&
2742 lockres->l_ex_holders) {
2743 spin_unlock_irqrestore(&lockres->l_lock, flags);
2749 /* If we get here, then we know that there are no more
2750 * incompatible holders (and anyone asking for an incompatible
2751 * lock is blocked). We can now downconvert the lock */
2755 /* Some lockres types want to do a bit of work before
2756 * downconverting a lock. Allow that here. The worker function
2757 * may sleep, so we save off a copy of what we're blocking as
2758 * it may change while we're not holding the spin lock. */
2759 blocking = lockres->l_blocking;
2760 spin_unlock_irqrestore(&lockres->l_lock, flags);
2762 ctl->unblock_action = worker(lockres, blocking);
2764 if (ctl->unblock_action == UNBLOCK_STOP_POST)
2767 spin_lock_irqsave(&lockres->l_lock, flags);
2768 if (blocking != lockres->l_blocking) {
2769 /* If this changed underneath us, then we can't drop
2776 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2778 ocfs2_prepare_downconvert(lockres, new_level);
2779 spin_unlock_irqrestore(&lockres->l_lock, flags);
2780 ret = ocfs2_downconvert_lock(osb, lockres, new_level, 0);
2786 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
2789 struct inode *inode;
2790 struct address_space *mapping;
2792 inode = ocfs2_lock_res_inode(lockres);
2793 mapping = inode->i_mapping;
2795 if (filemap_fdatawrite(mapping)) {
2796 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
2797 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2799 sync_mapping_buffers(mapping);
2800 if (blocking == LKM_EXMODE) {
2801 truncate_inode_pages(mapping, 0);
2802 unmap_mapping_range(mapping, 0, 0, 0);
2804 /* We only need to wait on the I/O if we're not also
2805 * truncating pages because truncate_inode_pages waits
2806 * for us above. We don't truncate pages if we're
2807 * blocking anything < EXMODE because we want to keep
2808 * them around in that case. */
2809 filemap_fdatawait(mapping);
2812 return UNBLOCK_CONTINUE;
2815 int ocfs2_unblock_data(struct ocfs2_lock_res *lockres,
2816 struct ocfs2_unblock_ctl *ctl)
2819 struct inode *inode;
2820 struct ocfs2_super *osb;
2824 inode = ocfs2_lock_res_inode(lockres);
2825 osb = OCFS2_SB(inode->i_sb);
2827 mlog(0, "unblock inode %llu\n",
2828 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2830 status = ocfs2_generic_unblock_lock(osb, lockres, ctl,
2831 ocfs2_data_convert_worker);
2835 mlog(0, "inode %llu, requeue = %d\n",
2836 (unsigned long long)OCFS2_I(inode)->ip_blkno, ctl->requeue);
2842 static int ocfs2_unblock_inode_lock(struct ocfs2_lock_res *lockres,
2843 struct ocfs2_unblock_ctl *ctl)
2846 struct inode *inode;
2850 mlog(0, "Unblock lockres %s\n", lockres->l_name);
2852 inode = ocfs2_lock_res_inode(lockres);
2854 status = ocfs2_generic_unblock_lock(OCFS2_SB(inode->i_sb),
2855 lockres, ctl, NULL);
2863 static int ocfs2_unblock_meta(struct ocfs2_lock_res *lockres,
2864 struct ocfs2_unblock_ctl *ctl)
2867 struct inode *inode;
2871 inode = ocfs2_lock_res_inode(lockres);
2873 mlog(0, "unblock inode %llu\n",
2874 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2876 status = ocfs2_do_unblock_meta(inode, &ctl->requeue);
2880 mlog(0, "inode %llu, requeue = %d\n",
2881 (unsigned long long)OCFS2_I(inode)->ip_blkno, ctl->requeue);
2888 * Does the final reference drop on our dentry lock. Right now this
2889 * happens in the vote thread, but we could choose to simplify the
2890 * dlmglue API and push these off to the ocfs2_wq in the future.
2892 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
2893 struct ocfs2_lock_res *lockres)
2895 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2896 ocfs2_dentry_lock_put(osb, dl);
2900 * d_delete() matching dentries before the lock downconvert.
2902 * At this point, any process waiting to destroy the
2903 * dentry_lock due to last ref count is stopped by the
2904 * OCFS2_LOCK_QUEUED flag.
2906 * We have two potential problems
2908 * 1) If we do the last reference drop on our dentry_lock (via dput)
2909 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
2910 * the downconvert to finish. Instead we take an elevated
2911 * reference and push the drop until after we've completed our
2912 * unblock processing.
2914 * 2) There might be another process with a final reference,
2915 * waiting on us to finish processing. If this is the case, we
2916 * detect it and exit out - there's no more dentries anyway.
2918 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
2921 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2922 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
2923 struct dentry *dentry;
2924 unsigned long flags;
2928 * This node is blocking another node from getting a read
2929 * lock. This happens when we've renamed within a
2930 * directory. We've forced the other nodes to d_delete(), but
2931 * we never actually dropped our lock because it's still
2932 * valid. The downconvert code will retain a PR for this node,
2933 * so there's no further work to do.
2935 if (blocking == LKM_PRMODE)
2936 return UNBLOCK_CONTINUE;
2939 * Mark this inode as potentially orphaned. The code in
2940 * ocfs2_delete_inode() will figure out whether it actually
2941 * needs to be freed or not.
2943 spin_lock(&oi->ip_lock);
2944 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
2945 spin_unlock(&oi->ip_lock);
2948 * Yuck. We need to make sure however that the check of
2949 * OCFS2_LOCK_FREEING and the extra reference are atomic with
2950 * respect to a reference decrement or the setting of that
2953 spin_lock_irqsave(&lockres->l_lock, flags);
2954 spin_lock(&dentry_attach_lock);
2955 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
2960 spin_unlock(&dentry_attach_lock);
2961 spin_unlock_irqrestore(&lockres->l_lock, flags);
2963 mlog(0, "extra_ref = %d\n", extra_ref);
2966 * We have a process waiting on us in ocfs2_dentry_iput(),
2967 * which means we can't have any more outstanding
2968 * aliases. There's no need to do any more work.
2971 return UNBLOCK_CONTINUE;
2973 spin_lock(&dentry_attach_lock);
2975 dentry = ocfs2_find_local_alias(dl->dl_inode,
2976 dl->dl_parent_blkno, 1);
2979 spin_unlock(&dentry_attach_lock);
2981 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
2982 dentry->d_name.name);
2985 * The following dcache calls may do an
2986 * iput(). Normally we don't want that from the
2987 * downconverting thread, but in this case it's ok
2988 * because the requesting node already has an
2989 * exclusive lock on the inode, so it can't be queued
2990 * for a downconvert.
2995 spin_lock(&dentry_attach_lock);
2997 spin_unlock(&dentry_attach_lock);
3000 * If we are the last holder of this dentry lock, there is no
3001 * reason to downconvert so skip straight to the unlock.
3003 if (dl->dl_count == 1)
3004 return UNBLOCK_STOP_POST;
3006 return UNBLOCK_CONTINUE_POST;
3009 static int ocfs2_unblock_dentry_lock(struct ocfs2_lock_res *lockres,
3010 struct ocfs2_unblock_ctl *ctl)
3013 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3014 struct ocfs2_super *osb = OCFS2_SB(dl->dl_inode->i_sb);
3016 mlog(0, "unblock dentry lock: %llu\n",
3017 (unsigned long long)OCFS2_I(dl->dl_inode)->ip_blkno);
3019 ret = ocfs2_generic_unblock_lock(osb,
3022 ocfs2_dentry_convert_worker);
3026 mlog(0, "requeue = %d, post = %d\n", ctl->requeue, ctl->unblock_action);
3031 /* Generic unblock function for any lockres whose private data is an
3032 * ocfs2_super pointer. */
3033 static int ocfs2_unblock_osb_lock(struct ocfs2_lock_res *lockres,
3034 struct ocfs2_unblock_ctl *ctl)
3037 struct ocfs2_super *osb;
3041 mlog(0, "Unblock lockres %s\n", lockres->l_name);
3043 osb = ocfs2_lock_res_super(lockres);
3045 status = ocfs2_generic_unblock_lock(osb,
3056 void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3057 struct ocfs2_lock_res *lockres)
3060 struct ocfs2_unblock_ctl ctl = {0, 0,};
3061 unsigned long flags;
3063 /* Our reference to the lockres in this function can be
3064 * considered valid until we remove the OCFS2_LOCK_QUEUED
3070 BUG_ON(!lockres->l_ops);
3071 BUG_ON(!lockres->l_ops->unblock);
3073 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3075 /* Detect whether a lock has been marked as going away while
3076 * the vote thread was processing other things. A lock can
3077 * still be marked with OCFS2_LOCK_FREEING after this check,
3078 * but short circuiting here will still save us some
3080 spin_lock_irqsave(&lockres->l_lock, flags);
3081 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3083 spin_unlock_irqrestore(&lockres->l_lock, flags);
3085 status = lockres->l_ops->unblock(lockres, &ctl);
3089 spin_lock_irqsave(&lockres->l_lock, flags);
3091 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
3092 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3094 ocfs2_schedule_blocked_lock(osb, lockres);
3096 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
3097 ctl.requeue ? "yes" : "no");
3098 spin_unlock_irqrestore(&lockres->l_lock, flags);
3100 if (ctl.unblock_action != UNBLOCK_CONTINUE
3101 && lockres->l_ops->post_unlock)
3102 lockres->l_ops->post_unlock(osb, lockres);
3107 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3108 struct ocfs2_lock_res *lockres)
3112 assert_spin_locked(&lockres->l_lock);
3114 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3115 /* Do not schedule a lock for downconvert when it's on
3116 * the way to destruction - any nodes wanting access
3117 * to the resource will get it soon. */
3118 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3119 lockres->l_name, lockres->l_flags);
3123 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3125 spin_lock(&osb->vote_task_lock);
3126 if (list_empty(&lockres->l_blocked_list)) {
3127 list_add_tail(&lockres->l_blocked_list,
3128 &osb->blocked_lock_list);
3129 osb->blocked_lock_count++;
3131 spin_unlock(&osb->vote_task_lock);
3136 /* This aids in debugging situations where a bad LVB might be involved. */
3137 void ocfs2_dump_meta_lvb_info(u64 level,
3138 const char *function,
3140 struct ocfs2_lock_res *lockres)
3142 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
3144 mlog(level, "LVB information for %s (called from %s:%u):\n",
3145 lockres->l_name, function, line);
3146 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
3147 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
3148 be32_to_cpu(lvb->lvb_igeneration));
3149 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
3150 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
3151 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
3152 be16_to_cpu(lvb->lvb_imode));
3153 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
3154 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
3155 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
3156 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
3157 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
3158 be32_to_cpu(lvb->lvb_iattr));