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>
50 #include "extent_map.h"
51 #include "heartbeat.h"
59 #include "buffer_head_io.h"
61 struct ocfs2_mask_waiter {
62 struct list_head mw_item;
64 struct completion mw_complete;
65 unsigned long mw_mask;
66 unsigned long mw_goal;
69 static void ocfs2_inode_ast_func(void *opaque);
70 static void ocfs2_inode_bast_func(void *opaque,
72 static void ocfs2_super_ast_func(void *opaque);
73 static void ocfs2_super_bast_func(void *opaque,
75 static void ocfs2_rename_ast_func(void *opaque);
76 static void ocfs2_rename_bast_func(void *opaque,
79 /* so far, all locks have gotten along with the same unlock ast */
80 static void ocfs2_unlock_ast_func(void *opaque,
81 enum dlm_status status);
82 static int ocfs2_do_unblock_meta(struct inode *inode,
84 static int ocfs2_unblock_meta(struct ocfs2_lock_res *lockres,
86 static int ocfs2_unblock_data(struct ocfs2_lock_res *lockres,
88 static int ocfs2_unblock_inode_lock(struct ocfs2_lock_res *lockres,
90 static int ocfs2_unblock_osb_lock(struct ocfs2_lock_res *lockres,
92 typedef void (ocfs2_convert_worker_t)(struct ocfs2_lock_res *, int);
93 static int ocfs2_generic_unblock_lock(struct ocfs2_super *osb,
94 struct ocfs2_lock_res *lockres,
96 ocfs2_convert_worker_t *worker);
98 struct ocfs2_lock_res_ops {
100 void (*bast)(void *, int);
101 void (*unlock_ast)(void *, enum dlm_status);
102 int (*unblock)(struct ocfs2_lock_res *, int *);
105 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
106 .ast = ocfs2_inode_ast_func,
107 .bast = ocfs2_inode_bast_func,
108 .unlock_ast = ocfs2_unlock_ast_func,
109 .unblock = ocfs2_unblock_inode_lock,
112 static struct ocfs2_lock_res_ops ocfs2_inode_meta_lops = {
113 .ast = ocfs2_inode_ast_func,
114 .bast = ocfs2_inode_bast_func,
115 .unlock_ast = ocfs2_unlock_ast_func,
116 .unblock = ocfs2_unblock_meta,
119 static void ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
122 static struct ocfs2_lock_res_ops ocfs2_inode_data_lops = {
123 .ast = ocfs2_inode_ast_func,
124 .bast = ocfs2_inode_bast_func,
125 .unlock_ast = ocfs2_unlock_ast_func,
126 .unblock = ocfs2_unblock_data,
129 static struct ocfs2_lock_res_ops ocfs2_super_lops = {
130 .ast = ocfs2_super_ast_func,
131 .bast = ocfs2_super_bast_func,
132 .unlock_ast = ocfs2_unlock_ast_func,
133 .unblock = ocfs2_unblock_osb_lock,
136 static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
137 .ast = ocfs2_rename_ast_func,
138 .bast = ocfs2_rename_bast_func,
139 .unlock_ast = ocfs2_unlock_ast_func,
140 .unblock = ocfs2_unblock_osb_lock,
143 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
145 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
146 lockres->l_type == OCFS2_LOCK_TYPE_DATA ||
147 lockres->l_type == OCFS2_LOCK_TYPE_RW;
150 static inline int ocfs2_is_super_lock(struct ocfs2_lock_res *lockres)
152 return lockres->l_type == OCFS2_LOCK_TYPE_SUPER;
155 static inline int ocfs2_is_rename_lock(struct ocfs2_lock_res *lockres)
157 return lockres->l_type == OCFS2_LOCK_TYPE_RENAME;
160 static inline struct ocfs2_super *ocfs2_lock_res_super(struct ocfs2_lock_res *lockres)
162 BUG_ON(!ocfs2_is_super_lock(lockres)
163 && !ocfs2_is_rename_lock(lockres));
165 return (struct ocfs2_super *) lockres->l_priv;
168 static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
170 BUG_ON(!ocfs2_is_inode_lock(lockres));
172 return (struct inode *) lockres->l_priv;
175 static int ocfs2_lock_create(struct ocfs2_super *osb,
176 struct ocfs2_lock_res *lockres,
179 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
181 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
182 struct ocfs2_lock_res *lockres,
184 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
185 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
186 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
187 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
188 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
189 struct ocfs2_lock_res *lockres);
190 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
192 #define ocfs2_log_dlm_error(_func, _stat, _lockres) do { \
193 mlog(ML_ERROR, "Dlm error \"%s\" while calling %s on " \
194 "resource %s: %s\n", dlm_errname(_stat), _func, \
195 _lockres->l_name, dlm_errmsg(_stat)); \
197 static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
198 struct ocfs2_lock_res *lockres);
199 static int ocfs2_meta_lock_update(struct inode *inode,
200 struct buffer_head **bh);
201 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
202 static inline int ocfs2_highest_compat_lock_level(int level);
203 static inline int ocfs2_can_downconvert_meta_lock(struct inode *inode,
204 struct ocfs2_lock_res *lockres,
207 static char *ocfs2_lock_type_strings[] = {
208 [OCFS2_LOCK_TYPE_META] = "Meta",
209 [OCFS2_LOCK_TYPE_DATA] = "Data",
210 [OCFS2_LOCK_TYPE_SUPER] = "Super",
211 [OCFS2_LOCK_TYPE_RENAME] = "Rename",
212 /* Need to differntiate from [R]ename.. serializing writes is the
213 * important job it does, anyway. */
214 [OCFS2_LOCK_TYPE_RW] = "Write/Read",
217 static char *ocfs2_lock_type_string(enum ocfs2_lock_type type)
219 mlog_bug_on_msg(type >= OCFS2_NUM_LOCK_TYPES, "%d\n", type);
220 return ocfs2_lock_type_strings[type];
223 static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
232 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
234 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
235 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
236 (long long)blkno, generation);
238 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
240 mlog(0, "built lock resource with name: %s\n", name);
245 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
247 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
248 struct ocfs2_dlm_debug *dlm_debug)
250 mlog(0, "Add tracking for lockres %s\n", res->l_name);
252 spin_lock(&ocfs2_dlm_tracking_lock);
253 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
254 spin_unlock(&ocfs2_dlm_tracking_lock);
257 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
259 spin_lock(&ocfs2_dlm_tracking_lock);
260 if (!list_empty(&res->l_debug_list))
261 list_del_init(&res->l_debug_list);
262 spin_unlock(&ocfs2_dlm_tracking_lock);
265 static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
266 struct ocfs2_lock_res *res,
267 enum ocfs2_lock_type type,
270 struct ocfs2_lock_res_ops *ops,
273 ocfs2_build_lock_name(type, blkno, generation, res->l_name);
279 res->l_level = LKM_IVMODE;
280 res->l_requested = LKM_IVMODE;
281 res->l_blocking = LKM_IVMODE;
282 res->l_action = OCFS2_AST_INVALID;
283 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
285 res->l_flags = OCFS2_LOCK_INITIALIZED;
287 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
290 void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
292 /* This also clears out the lock status block */
293 memset(res, 0, sizeof(struct ocfs2_lock_res));
294 spin_lock_init(&res->l_lock);
295 init_waitqueue_head(&res->l_event);
296 INIT_LIST_HEAD(&res->l_blocked_list);
297 INIT_LIST_HEAD(&res->l_mask_waiters);
300 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
301 enum ocfs2_lock_type type,
304 struct ocfs2_lock_res_ops *ops;
307 case OCFS2_LOCK_TYPE_RW:
308 ops = &ocfs2_inode_rw_lops;
310 case OCFS2_LOCK_TYPE_META:
311 ops = &ocfs2_inode_meta_lops;
313 case OCFS2_LOCK_TYPE_DATA:
314 ops = &ocfs2_inode_data_lops;
317 mlog_bug_on_msg(1, "type: %d\n", type);
318 ops = NULL; /* thanks, gcc */
322 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type,
323 OCFS2_I(inode)->ip_blkno,
324 inode->i_generation, ops, inode);
327 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
328 struct ocfs2_super *osb)
330 /* Superblock lockres doesn't come from a slab so we call init
331 * once on it manually. */
332 ocfs2_lock_res_init_once(res);
333 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
334 OCFS2_SUPER_BLOCK_BLKNO, 0,
335 &ocfs2_super_lops, osb);
338 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
339 struct ocfs2_super *osb)
341 /* Rename lockres doesn't come from a slab so we call init
342 * once on it manually. */
343 ocfs2_lock_res_init_once(res);
344 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME, 0, 0,
345 &ocfs2_rename_lops, osb);
348 void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
352 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
355 ocfs2_remove_lockres_tracking(res);
357 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
358 "Lockres %s is on the blocked list\n",
360 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
361 "Lockres %s has mask waiters pending\n",
363 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
364 "Lockres %s is locked\n",
366 mlog_bug_on_msg(res->l_ro_holders,
367 "Lockres %s has %u ro holders\n",
368 res->l_name, res->l_ro_holders);
369 mlog_bug_on_msg(res->l_ex_holders,
370 "Lockres %s has %u ex holders\n",
371 res->l_name, res->l_ex_holders);
373 /* Need to clear out the lock status block for the dlm */
374 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
380 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
389 lockres->l_ex_holders++;
392 lockres->l_ro_holders++;
401 static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
410 BUG_ON(!lockres->l_ex_holders);
411 lockres->l_ex_holders--;
414 BUG_ON(!lockres->l_ro_holders);
415 lockres->l_ro_holders--;
423 /* WARNING: This function lives in a world where the only three lock
424 * levels are EX, PR, and NL. It *will* have to be adjusted when more
425 * lock types are added. */
426 static inline int ocfs2_highest_compat_lock_level(int level)
428 int new_level = LKM_EXMODE;
430 if (level == LKM_EXMODE)
431 new_level = LKM_NLMODE;
432 else if (level == LKM_PRMODE)
433 new_level = LKM_PRMODE;
437 static void lockres_set_flags(struct ocfs2_lock_res *lockres,
438 unsigned long newflags)
440 struct list_head *pos, *tmp;
441 struct ocfs2_mask_waiter *mw;
443 assert_spin_locked(&lockres->l_lock);
445 lockres->l_flags = newflags;
447 list_for_each_safe(pos, tmp, &lockres->l_mask_waiters) {
448 mw = list_entry(pos, struct ocfs2_mask_waiter, mw_item);
449 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
452 list_del_init(&mw->mw_item);
454 complete(&mw->mw_complete);
457 static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
459 lockres_set_flags(lockres, lockres->l_flags | or);
461 static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
464 lockres_set_flags(lockres, lockres->l_flags & ~clear);
467 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
471 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
472 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
473 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
474 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
476 lockres->l_level = lockres->l_requested;
477 if (lockres->l_level <=
478 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
479 lockres->l_blocking = LKM_NLMODE;
480 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
482 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
487 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
491 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
492 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
494 /* Convert from RO to EX doesn't really need anything as our
495 * information is already up to data. Convert from NL to
496 * *anything* however should mark ourselves as needing an
498 if (lockres->l_level == LKM_NLMODE)
499 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
501 lockres->l_level = lockres->l_requested;
502 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
507 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
511 BUG_ON((!lockres->l_flags & OCFS2_LOCK_BUSY));
512 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
514 if (lockres->l_requested > LKM_NLMODE &&
515 !(lockres->l_flags & OCFS2_LOCK_LOCAL))
516 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
518 lockres->l_level = lockres->l_requested;
519 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
520 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
525 static void ocfs2_inode_ast_func(void *opaque)
527 struct ocfs2_lock_res *lockres = opaque;
529 struct dlm_lockstatus *lksb;
534 inode = ocfs2_lock_res_inode(lockres);
536 mlog(0, "AST fired for inode %llu, l_action = %u, type = %s\n",
537 (unsigned long long)OCFS2_I(inode)->ip_blkno, lockres->l_action,
538 ocfs2_lock_type_string(lockres->l_type));
540 BUG_ON(!ocfs2_is_inode_lock(lockres));
542 spin_lock_irqsave(&lockres->l_lock, flags);
544 lksb = &(lockres->l_lksb);
545 if (lksb->status != DLM_NORMAL) {
546 mlog(ML_ERROR, "ocfs2_inode_ast_func: lksb status value of %u "
547 "on inode %llu\n", lksb->status,
548 (unsigned long long)OCFS2_I(inode)->ip_blkno);
549 spin_unlock_irqrestore(&lockres->l_lock, flags);
554 switch(lockres->l_action) {
555 case OCFS2_AST_ATTACH:
556 ocfs2_generic_handle_attach_action(lockres);
557 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
559 case OCFS2_AST_CONVERT:
560 ocfs2_generic_handle_convert_action(lockres);
562 case OCFS2_AST_DOWNCONVERT:
563 ocfs2_generic_handle_downconvert_action(lockres);
566 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
567 "lockres flags = 0x%lx, unlock action: %u\n",
568 lockres->l_name, lockres->l_action, lockres->l_flags,
569 lockres->l_unlock_action);
574 /* data and rw locking ignores refresh flag for now. */
575 if (lockres->l_type != OCFS2_LOCK_TYPE_META)
576 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
578 /* set it to something invalid so if we get called again we
580 lockres->l_action = OCFS2_AST_INVALID;
581 spin_unlock_irqrestore(&lockres->l_lock, flags);
582 wake_up(&lockres->l_event);
587 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
590 int needs_downconvert = 0;
593 assert_spin_locked(&lockres->l_lock);
595 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
597 if (level > lockres->l_blocking) {
598 /* only schedule a downconvert if we haven't already scheduled
599 * one that goes low enough to satisfy the level we're
600 * blocking. this also catches the case where we get
602 if (ocfs2_highest_compat_lock_level(level) <
603 ocfs2_highest_compat_lock_level(lockres->l_blocking))
604 needs_downconvert = 1;
606 lockres->l_blocking = level;
609 mlog_exit(needs_downconvert);
610 return needs_downconvert;
613 static void ocfs2_generic_bast_func(struct ocfs2_super *osb,
614 struct ocfs2_lock_res *lockres,
617 int needs_downconvert;
622 BUG_ON(level <= LKM_NLMODE);
624 spin_lock_irqsave(&lockres->l_lock, flags);
625 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
626 if (needs_downconvert)
627 ocfs2_schedule_blocked_lock(osb, lockres);
628 spin_unlock_irqrestore(&lockres->l_lock, flags);
630 ocfs2_kick_vote_thread(osb);
632 wake_up(&lockres->l_event);
636 static void ocfs2_inode_bast_func(void *opaque, int level)
638 struct ocfs2_lock_res *lockres = opaque;
640 struct ocfs2_super *osb;
644 BUG_ON(!ocfs2_is_inode_lock(lockres));
646 inode = ocfs2_lock_res_inode(lockres);
647 osb = OCFS2_SB(inode->i_sb);
649 mlog(0, "BAST fired for inode %llu, blocking %d, level %d type %s\n",
650 (unsigned long long)OCFS2_I(inode)->ip_blkno, level,
651 lockres->l_level, ocfs2_lock_type_string(lockres->l_type));
653 ocfs2_generic_bast_func(osb, lockres, level);
658 static void ocfs2_generic_ast_func(struct ocfs2_lock_res *lockres,
661 struct dlm_lockstatus *lksb = &lockres->l_lksb;
664 spin_lock_irqsave(&lockres->l_lock, flags);
666 if (lksb->status != DLM_NORMAL) {
667 mlog(ML_ERROR, "lockres %s: lksb status value of %u!\n",
668 lockres->l_name, lksb->status);
669 spin_unlock_irqrestore(&lockres->l_lock, flags);
673 switch(lockres->l_action) {
674 case OCFS2_AST_ATTACH:
675 ocfs2_generic_handle_attach_action(lockres);
677 case OCFS2_AST_CONVERT:
678 ocfs2_generic_handle_convert_action(lockres);
680 case OCFS2_AST_DOWNCONVERT:
681 ocfs2_generic_handle_downconvert_action(lockres);
688 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
690 /* set it to something invalid so if we get called again we
692 lockres->l_action = OCFS2_AST_INVALID;
693 spin_unlock_irqrestore(&lockres->l_lock, flags);
695 wake_up(&lockres->l_event);
698 static void ocfs2_super_ast_func(void *opaque)
700 struct ocfs2_lock_res *lockres = opaque;
703 mlog(0, "Superblock AST fired\n");
705 BUG_ON(!ocfs2_is_super_lock(lockres));
706 ocfs2_generic_ast_func(lockres, 0);
711 static void ocfs2_super_bast_func(void *opaque,
714 struct ocfs2_lock_res *lockres = opaque;
715 struct ocfs2_super *osb;
718 mlog(0, "Superblock BAST fired\n");
720 BUG_ON(!ocfs2_is_super_lock(lockres));
721 osb = ocfs2_lock_res_super(lockres);
722 ocfs2_generic_bast_func(osb, lockres, level);
727 static void ocfs2_rename_ast_func(void *opaque)
729 struct ocfs2_lock_res *lockres = opaque;
733 mlog(0, "Rename AST fired\n");
735 BUG_ON(!ocfs2_is_rename_lock(lockres));
737 ocfs2_generic_ast_func(lockres, 1);
742 static void ocfs2_rename_bast_func(void *opaque,
745 struct ocfs2_lock_res *lockres = opaque;
746 struct ocfs2_super *osb;
750 mlog(0, "Rename BAST fired\n");
752 BUG_ON(!ocfs2_is_rename_lock(lockres));
754 osb = ocfs2_lock_res_super(lockres);
755 ocfs2_generic_bast_func(osb, lockres, level);
760 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
766 spin_lock_irqsave(&lockres->l_lock, flags);
767 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
769 lockres->l_action = OCFS2_AST_INVALID;
771 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
772 spin_unlock_irqrestore(&lockres->l_lock, flags);
774 wake_up(&lockres->l_event);
778 /* Note: If we detect another process working on the lock (i.e.,
779 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
780 * to do the right thing in that case.
782 static int ocfs2_lock_create(struct ocfs2_super *osb,
783 struct ocfs2_lock_res *lockres,
788 enum dlm_status status;
793 mlog(0, "lock %s, level = %d, flags = %d\n", lockres->l_name, level,
796 spin_lock_irqsave(&lockres->l_lock, flags);
797 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
798 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
799 spin_unlock_irqrestore(&lockres->l_lock, flags);
803 lockres->l_action = OCFS2_AST_ATTACH;
804 lockres->l_requested = level;
805 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
806 spin_unlock_irqrestore(&lockres->l_lock, flags);
808 status = dlmlock(osb->dlm,
813 OCFS2_LOCK_ID_MAX_LEN - 1,
816 lockres->l_ops->bast);
817 if (status != DLM_NORMAL) {
818 ocfs2_log_dlm_error("dlmlock", status, lockres);
820 ocfs2_recover_from_dlm_error(lockres, 1);
823 mlog(0, "lock %s, successfull return from dlmlock\n", lockres->l_name);
830 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
836 spin_lock_irqsave(&lockres->l_lock, flags);
837 ret = lockres->l_flags & flag;
838 spin_unlock_irqrestore(&lockres->l_lock, flags);
843 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
846 wait_event(lockres->l_event,
847 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
850 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
853 wait_event(lockres->l_event,
854 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
857 /* predict what lock level we'll be dropping down to on behalf
858 * of another node, and return true if the currently wanted
859 * level will be compatible with it. */
860 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
863 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
865 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
868 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
870 INIT_LIST_HEAD(&mw->mw_item);
871 init_completion(&mw->mw_complete);
874 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
876 wait_for_completion(&mw->mw_complete);
877 /* Re-arm the completion in case we want to wait on it again */
878 INIT_COMPLETION(mw->mw_complete);
879 return mw->mw_status;
882 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
883 struct ocfs2_mask_waiter *mw,
887 BUG_ON(!list_empty(&mw->mw_item));
889 assert_spin_locked(&lockres->l_lock);
891 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
896 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
897 * if the mask still hadn't reached its goal */
898 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
899 struct ocfs2_mask_waiter *mw)
904 spin_lock_irqsave(&lockres->l_lock, flags);
905 if (!list_empty(&mw->mw_item)) {
906 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
909 list_del_init(&mw->mw_item);
910 init_completion(&mw->mw_complete);
912 spin_unlock_irqrestore(&lockres->l_lock, flags);
918 static int ocfs2_cluster_lock(struct ocfs2_super *osb,
919 struct ocfs2_lock_res *lockres,
924 struct ocfs2_mask_waiter mw;
925 enum dlm_status status;
926 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
927 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
932 ocfs2_init_mask_waiter(&mw);
937 if (catch_signals && signal_pending(current)) {
942 spin_lock_irqsave(&lockres->l_lock, flags);
944 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
945 "Cluster lock called on freeing lockres %s! flags "
946 "0x%lx\n", lockres->l_name, lockres->l_flags);
948 /* We only compare against the currently granted level
949 * here. If the lock is blocked waiting on a downconvert,
950 * we'll get caught below. */
951 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
952 level > lockres->l_level) {
953 /* is someone sitting in dlm_lock? If so, wait on
955 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
960 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
961 /* lock has not been created yet. */
962 spin_unlock_irqrestore(&lockres->l_lock, flags);
964 ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
972 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
973 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
974 /* is the lock is currently blocked on behalf of
976 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
981 if (level > lockres->l_level) {
982 if (lockres->l_action != OCFS2_AST_INVALID)
983 mlog(ML_ERROR, "lockres %s has action %u pending\n",
984 lockres->l_name, lockres->l_action);
986 lockres->l_action = OCFS2_AST_CONVERT;
987 lockres->l_requested = level;
988 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
989 spin_unlock_irqrestore(&lockres->l_lock, flags);
991 BUG_ON(level == LKM_IVMODE);
992 BUG_ON(level == LKM_NLMODE);
994 mlog(0, "lock %s, convert from %d to level = %d\n",
995 lockres->l_name, lockres->l_level, level);
997 /* call dlm_lock to upgrade lock now */
998 status = dlmlock(osb->dlm,
1001 lkm_flags|LKM_CONVERT|LKM_VALBLK,
1003 OCFS2_LOCK_ID_MAX_LEN - 1,
1004 lockres->l_ops->ast,
1006 lockres->l_ops->bast);
1007 if (status != DLM_NORMAL) {
1008 if ((lkm_flags & LKM_NOQUEUE) &&
1009 (status == DLM_NOTQUEUED))
1012 ocfs2_log_dlm_error("dlmlock", status,
1016 ocfs2_recover_from_dlm_error(lockres, 1);
1020 mlog(0, "lock %s, successfull return from dlmlock\n",
1023 /* At this point we've gone inside the dlm and need to
1024 * complete our work regardless. */
1027 /* wait for busy to clear and carry on */
1031 /* Ok, if we get here then we're good to go. */
1032 ocfs2_inc_holders(lockres, level);
1036 spin_unlock_irqrestore(&lockres->l_lock, flags);
1039 * This is helping work around a lock inversion between the page lock
1040 * and dlm locks. One path holds the page lock while calling aops
1041 * which block acquiring dlm locks. The voting thread holds dlm
1042 * locks while acquiring page locks while down converting data locks.
1043 * This block is helping an aop path notice the inversion and back
1044 * off to unlock its page lock before trying the dlm lock again.
1046 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1047 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1049 if (lockres_remove_mask_waiter(lockres, &mw))
1055 ret = ocfs2_wait_for_mask(&mw);
1065 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1066 struct ocfs2_lock_res *lockres,
1069 unsigned long flags;
1072 spin_lock_irqsave(&lockres->l_lock, flags);
1073 ocfs2_dec_holders(lockres, level);
1074 ocfs2_vote_on_unlock(osb, lockres);
1075 spin_unlock_irqrestore(&lockres->l_lock, flags);
1079 static int ocfs2_create_new_inode_lock(struct inode *inode,
1080 struct ocfs2_lock_res *lockres)
1082 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1083 unsigned long flags;
1085 spin_lock_irqsave(&lockres->l_lock, flags);
1086 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1087 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1088 spin_unlock_irqrestore(&lockres->l_lock, flags);
1090 return ocfs2_lock_create(osb, lockres, LKM_EXMODE, LKM_LOCAL);
1093 /* Grants us an EX lock on the data and metadata resources, skipping
1094 * the normal cluster directory lookup. Use this ONLY on newly created
1095 * inodes which other nodes can't possibly see, and which haven't been
1096 * hashed in the inode hash yet. This can give us a good performance
1097 * increase as it'll skip the network broadcast normally associated
1098 * with creating a new lock resource. */
1099 int ocfs2_create_new_inode_locks(struct inode *inode)
1104 BUG_ON(!ocfs2_inode_is_new(inode));
1108 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1110 /* NOTE: That we don't increment any of the holder counts, nor
1111 * do we add anything to a journal handle. Since this is
1112 * supposed to be a new inode which the cluster doesn't know
1113 * about yet, there is no need to. As far as the LVB handling
1114 * is concerned, this is basically like acquiring an EX lock
1115 * on a resource which has an invalid one -- we'll set it
1116 * valid when we release the EX. */
1118 ret = ocfs2_create_new_inode_lock(inode,
1119 &OCFS2_I(inode)->ip_rw_lockres);
1125 ret = ocfs2_create_new_inode_lock(inode,
1126 &OCFS2_I(inode)->ip_meta_lockres);
1132 ret = ocfs2_create_new_inode_lock(inode,
1133 &OCFS2_I(inode)->ip_data_lockres);
1144 int ocfs2_rw_lock(struct inode *inode, int write)
1147 struct ocfs2_lock_res *lockres;
1153 mlog(0, "inode %llu take %s RW lock\n",
1154 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1155 write ? "EXMODE" : "PRMODE");
1157 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1159 level = write ? LKM_EXMODE : LKM_PRMODE;
1161 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1170 void ocfs2_rw_unlock(struct inode *inode, int write)
1172 int level = write ? LKM_EXMODE : LKM_PRMODE;
1173 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1177 mlog(0, "inode %llu drop %s RW lock\n",
1178 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1179 write ? "EXMODE" : "PRMODE");
1181 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1186 int ocfs2_data_lock_full(struct inode *inode,
1190 int status = 0, level;
1191 struct ocfs2_lock_res *lockres;
1197 mlog(0, "inode %llu take %s DATA lock\n",
1198 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1199 write ? "EXMODE" : "PRMODE");
1201 /* We'll allow faking a readonly data lock for
1203 if (ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb))) {
1211 lockres = &OCFS2_I(inode)->ip_data_lockres;
1213 level = write ? LKM_EXMODE : LKM_PRMODE;
1215 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level,
1217 if (status < 0 && status != -EAGAIN)
1225 /* see ocfs2_meta_lock_with_page() */
1226 int ocfs2_data_lock_with_page(struct inode *inode,
1232 ret = ocfs2_data_lock_full(inode, write, OCFS2_LOCK_NONBLOCK);
1233 if (ret == -EAGAIN) {
1235 if (ocfs2_data_lock(inode, write) == 0)
1236 ocfs2_data_unlock(inode, write);
1237 ret = AOP_TRUNCATED_PAGE;
1243 static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
1244 struct ocfs2_lock_res *lockres)
1250 /* If we know that another node is waiting on our lock, kick
1251 * the vote thread * pre-emptively when we reach a release
1253 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1254 switch(lockres->l_blocking) {
1256 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1260 if (!lockres->l_ex_holders)
1269 ocfs2_kick_vote_thread(osb);
1274 void ocfs2_data_unlock(struct inode *inode,
1277 int level = write ? LKM_EXMODE : LKM_PRMODE;
1278 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_data_lockres;
1282 mlog(0, "inode %llu drop %s DATA lock\n",
1283 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1284 write ? "EXMODE" : "PRMODE");
1286 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1287 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1292 #define OCFS2_SEC_BITS 34
1293 #define OCFS2_SEC_SHIFT (64 - 34)
1294 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1296 /* LVB only has room for 64 bits of time here so we pack it for
1298 static u64 ocfs2_pack_timespec(struct timespec *spec)
1301 u64 sec = spec->tv_sec;
1302 u32 nsec = spec->tv_nsec;
1304 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1309 /* Call this with the lockres locked. I am reasonably sure we don't
1310 * need ip_lock in this function as anyone who would be changing those
1311 * values is supposed to be blocked in ocfs2_meta_lock right now. */
1312 static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1314 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1315 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1316 struct ocfs2_meta_lvb *lvb;
1320 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1322 lvb->lvb_version = cpu_to_be32(OCFS2_LVB_VERSION);
1323 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1324 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1325 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1326 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1327 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1328 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1329 lvb->lvb_iatime_packed =
1330 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1331 lvb->lvb_ictime_packed =
1332 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1333 lvb->lvb_imtime_packed =
1334 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1335 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
1337 mlog_meta_lvb(0, lockres);
1342 static void ocfs2_unpack_timespec(struct timespec *spec,
1345 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1346 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1349 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1351 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1352 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1353 struct ocfs2_meta_lvb *lvb;
1357 mlog_meta_lvb(0, lockres);
1359 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1361 /* We're safe here without the lockres lock... */
1362 spin_lock(&oi->ip_lock);
1363 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1364 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1366 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1367 ocfs2_set_inode_flags(inode);
1369 /* fast-symlinks are a special case */
1370 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1371 inode->i_blocks = 0;
1374 ocfs2_align_bytes_to_sectors(i_size_read(inode));
1376 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1377 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1378 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1379 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1380 ocfs2_unpack_timespec(&inode->i_atime,
1381 be64_to_cpu(lvb->lvb_iatime_packed));
1382 ocfs2_unpack_timespec(&inode->i_mtime,
1383 be64_to_cpu(lvb->lvb_imtime_packed));
1384 ocfs2_unpack_timespec(&inode->i_ctime,
1385 be64_to_cpu(lvb->lvb_ictime_packed));
1386 spin_unlock(&oi->ip_lock);
1391 static inline int ocfs2_meta_lvb_is_trustable(struct ocfs2_lock_res *lockres)
1393 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1395 if (be32_to_cpu(lvb->lvb_version) == OCFS2_LVB_VERSION)
1400 /* Determine whether a lock resource needs to be refreshed, and
1401 * arbitrate who gets to refresh it.
1403 * 0 means no refresh needed.
1405 * > 0 means you need to refresh this and you MUST call
1406 * ocfs2_complete_lock_res_refresh afterwards. */
1407 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1409 unsigned long flags;
1415 spin_lock_irqsave(&lockres->l_lock, flags);
1416 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1417 spin_unlock_irqrestore(&lockres->l_lock, flags);
1421 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1422 spin_unlock_irqrestore(&lockres->l_lock, flags);
1424 ocfs2_wait_on_refreshing_lock(lockres);
1428 /* Ok, I'll be the one to refresh this lock. */
1429 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1430 spin_unlock_irqrestore(&lockres->l_lock, flags);
1438 /* If status is non zero, I'll mark it as not being in refresh
1439 * anymroe, but i won't clear the needs refresh flag. */
1440 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1443 unsigned long flags;
1446 spin_lock_irqsave(&lockres->l_lock, flags);
1447 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1449 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1450 spin_unlock_irqrestore(&lockres->l_lock, flags);
1452 wake_up(&lockres->l_event);
1457 /* may or may not return a bh if it went to disk. */
1458 static int ocfs2_meta_lock_update(struct inode *inode,
1459 struct buffer_head **bh)
1462 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1463 struct ocfs2_lock_res *lockres;
1464 struct ocfs2_dinode *fe;
1468 spin_lock(&oi->ip_lock);
1469 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1470 mlog(0, "Orphaned inode %llu was deleted while we "
1471 "were waiting on a lock. ip_flags = 0x%x\n",
1472 (unsigned long long)oi->ip_blkno, oi->ip_flags);
1473 spin_unlock(&oi->ip_lock);
1477 spin_unlock(&oi->ip_lock);
1479 lockres = &oi->ip_meta_lockres;
1481 if (!ocfs2_should_refresh_lock_res(lockres))
1484 /* This will discard any caching information we might have had
1485 * for the inode metadata. */
1486 ocfs2_metadata_cache_purge(inode);
1488 /* will do nothing for inode types that don't use the extent
1489 * map (directories, bitmap files, etc) */
1490 ocfs2_extent_map_trunc(inode, 0);
1492 if (ocfs2_meta_lvb_is_trustable(lockres)) {
1493 mlog(0, "Trusting LVB on inode %llu\n",
1494 (unsigned long long)oi->ip_blkno);
1495 ocfs2_refresh_inode_from_lvb(inode);
1497 /* Boo, we have to go to disk. */
1498 /* read bh, cast, ocfs2_refresh_inode */
1499 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1500 bh, OCFS2_BH_CACHED, inode);
1505 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1507 /* This is a good chance to make sure we're not
1508 * locking an invalid object.
1510 * We bug on a stale inode here because we checked
1511 * above whether it was wiped from disk. The wiping
1512 * node provides a guarantee that we receive that
1513 * message and can mark the inode before dropping any
1514 * locks associated with it. */
1515 if (!OCFS2_IS_VALID_DINODE(fe)) {
1516 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1520 mlog_bug_on_msg(inode->i_generation !=
1521 le32_to_cpu(fe->i_generation),
1522 "Invalid dinode %llu disk generation: %u "
1523 "inode->i_generation: %u\n",
1524 (unsigned long long)oi->ip_blkno,
1525 le32_to_cpu(fe->i_generation),
1526 inode->i_generation);
1527 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1528 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
1529 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1530 (unsigned long long)oi->ip_blkno,
1531 (unsigned long long)le64_to_cpu(fe->i_dtime),
1532 le32_to_cpu(fe->i_flags));
1534 ocfs2_refresh_inode(inode, fe);
1539 ocfs2_complete_lock_res_refresh(lockres, status);
1545 static int ocfs2_assign_bh(struct inode *inode,
1546 struct buffer_head **ret_bh,
1547 struct buffer_head *passed_bh)
1552 /* Ok, the update went to disk for us, use the
1554 *ret_bh = passed_bh;
1560 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1561 OCFS2_I(inode)->ip_blkno,
1572 * returns < 0 error if the callback will never be called, otherwise
1573 * the result of the lock will be communicated via the callback.
1575 int ocfs2_meta_lock_full(struct inode *inode,
1576 struct ocfs2_journal_handle *handle,
1577 struct buffer_head **ret_bh,
1581 int status, level, dlm_flags, acquired;
1582 struct ocfs2_lock_res *lockres;
1583 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1584 struct buffer_head *local_bh = NULL;
1590 mlog(0, "inode %llu, take %s META lock\n",
1591 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1592 ex ? "EXMODE" : "PRMODE");
1596 /* We'll allow faking a readonly metadata lock for
1598 if (ocfs2_is_hard_readonly(osb)) {
1604 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1605 wait_event(osb->recovery_event,
1606 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1609 lockres = &OCFS2_I(inode)->ip_meta_lockres;
1610 level = ex ? LKM_EXMODE : LKM_PRMODE;
1612 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
1613 dlm_flags |= LKM_NOQUEUE;
1615 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1617 if (status != -EAGAIN && status != -EIOCBRETRY)
1622 /* Notify the error cleanup path to drop the cluster lock. */
1625 /* We wait twice because a node may have died while we were in
1626 * the lower dlm layers. The second time though, we've
1627 * committed to owning this lock so we don't allow signals to
1628 * abort the operation. */
1629 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1630 wait_event(osb->recovery_event,
1631 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1633 /* This is fun. The caller may want a bh back, or it may
1634 * not. ocfs2_meta_lock_update definitely wants one in, but
1635 * may or may not read one, depending on what's in the
1636 * LVB. The result of all of this is that we've *only* gone to
1637 * disk if we have to, so the complexity is worthwhile. */
1638 status = ocfs2_meta_lock_update(inode, &local_bh);
1640 if (status != -ENOENT)
1646 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
1654 status = ocfs2_handle_add_lock(handle, inode);
1661 if (ret_bh && (*ret_bh)) {
1666 ocfs2_meta_unlock(inode, ex);
1677 * This is working around a lock inversion between tasks acquiring DLM locks
1678 * while holding a page lock and the vote thread which blocks dlm lock acquiry
1679 * while acquiring page locks.
1681 * ** These _with_page variantes are only intended to be called from aop
1682 * methods that hold page locks and return a very specific *positive* error
1683 * code that aop methods pass up to the VFS -- test for errors with != 0. **
1685 * The DLM is called such that it returns -EAGAIN if it would have blocked
1686 * waiting for the vote thread. In that case we unlock our page so the vote
1687 * thread can make progress. Once we've done this we have to return
1688 * AOP_TRUNCATED_PAGE so the aop method that called us can bubble that back up
1689 * into the VFS who will then immediately retry the aop call.
1691 * We do a blocking lock and immediate unlock before returning, though, so that
1692 * the lock has a great chance of being cached on this node by the time the VFS
1693 * calls back to retry the aop. This has a potential to livelock as nodes
1694 * ping locks back and forth, but that's a risk we're willing to take to avoid
1695 * the lock inversion simply.
1697 int ocfs2_meta_lock_with_page(struct inode *inode,
1698 struct ocfs2_journal_handle *handle,
1699 struct buffer_head **ret_bh,
1705 ret = ocfs2_meta_lock_full(inode, handle, ret_bh, ex,
1706 OCFS2_LOCK_NONBLOCK);
1707 if (ret == -EAGAIN) {
1709 if (ocfs2_meta_lock(inode, handle, ret_bh, ex) == 0)
1710 ocfs2_meta_unlock(inode, ex);
1711 ret = AOP_TRUNCATED_PAGE;
1717 void ocfs2_meta_unlock(struct inode *inode,
1720 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1721 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
1725 mlog(0, "inode %llu drop %s META lock\n",
1726 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1727 ex ? "EXMODE" : "PRMODE");
1729 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1730 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1735 int ocfs2_super_lock(struct ocfs2_super *osb,
1739 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1740 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1741 struct buffer_head *bh;
1742 struct ocfs2_slot_info *si = osb->slot_info;
1746 if (ocfs2_is_hard_readonly(osb))
1749 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
1755 /* The super block lock path is really in the best position to
1756 * know when resources covered by the lock need to be
1757 * refreshed, so we do it here. Of course, making sense of
1758 * everything is up to the caller :) */
1759 status = ocfs2_should_refresh_lock_res(lockres);
1766 status = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0,
1769 ocfs2_update_slot_info(si);
1771 ocfs2_complete_lock_res_refresh(lockres, status);
1781 void ocfs2_super_unlock(struct ocfs2_super *osb,
1784 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1785 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1787 ocfs2_cluster_unlock(osb, lockres, level);
1790 int ocfs2_rename_lock(struct ocfs2_super *osb)
1793 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1795 if (ocfs2_is_hard_readonly(osb))
1798 status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
1805 void ocfs2_rename_unlock(struct ocfs2_super *osb)
1807 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1809 ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
1812 /* Reference counting of the dlm debug structure. We want this because
1813 * open references on the debug inodes can live on after a mount, so
1814 * we can't rely on the ocfs2_super to always exist. */
1815 static void ocfs2_dlm_debug_free(struct kref *kref)
1817 struct ocfs2_dlm_debug *dlm_debug;
1819 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
1824 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
1827 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
1830 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
1832 kref_get(&debug->d_refcnt);
1835 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
1837 struct ocfs2_dlm_debug *dlm_debug;
1839 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
1841 mlog_errno(-ENOMEM);
1845 kref_init(&dlm_debug->d_refcnt);
1846 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
1847 dlm_debug->d_locking_state = NULL;
1852 /* Access to this is arbitrated for us via seq_file->sem. */
1853 struct ocfs2_dlm_seq_priv {
1854 struct ocfs2_dlm_debug *p_dlm_debug;
1855 struct ocfs2_lock_res p_iter_res;
1856 struct ocfs2_lock_res p_tmp_res;
1859 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
1860 struct ocfs2_dlm_seq_priv *priv)
1862 struct ocfs2_lock_res *iter, *ret = NULL;
1863 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
1865 assert_spin_locked(&ocfs2_dlm_tracking_lock);
1867 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
1868 /* discover the head of the list */
1869 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
1870 mlog(0, "End of list found, %p\n", ret);
1874 /* We track our "dummy" iteration lockres' by a NULL
1876 if (iter->l_ops != NULL) {
1885 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
1887 struct ocfs2_dlm_seq_priv *priv = m->private;
1888 struct ocfs2_lock_res *iter;
1890 spin_lock(&ocfs2_dlm_tracking_lock);
1891 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
1893 /* Since lockres' have the lifetime of their container
1894 * (which can be inodes, ocfs2_supers, etc) we want to
1895 * copy this out to a temporary lockres while still
1896 * under the spinlock. Obviously after this we can't
1897 * trust any pointers on the copy returned, but that's
1898 * ok as the information we want isn't typically held
1900 priv->p_tmp_res = *iter;
1901 iter = &priv->p_tmp_res;
1903 spin_unlock(&ocfs2_dlm_tracking_lock);
1908 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
1912 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
1914 struct ocfs2_dlm_seq_priv *priv = m->private;
1915 struct ocfs2_lock_res *iter = v;
1916 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
1918 spin_lock(&ocfs2_dlm_tracking_lock);
1919 iter = ocfs2_dlm_next_res(iter, priv);
1920 list_del_init(&dummy->l_debug_list);
1922 list_add(&dummy->l_debug_list, &iter->l_debug_list);
1923 priv->p_tmp_res = *iter;
1924 iter = &priv->p_tmp_res;
1926 spin_unlock(&ocfs2_dlm_tracking_lock);
1931 /* So that debugfs.ocfs2 can determine which format is being used */
1932 #define OCFS2_DLM_DEBUG_STR_VERSION 1
1933 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
1937 struct ocfs2_lock_res *lockres = v;
1942 seq_printf(m, "0x%x\t"
1952 OCFS2_DLM_DEBUG_STR_VERSION,
1953 OCFS2_LOCK_ID_MAX_LEN, lockres->l_name,
1957 lockres->l_unlock_action,
1958 lockres->l_ro_holders,
1959 lockres->l_ex_holders,
1960 lockres->l_requested,
1961 lockres->l_blocking);
1963 /* Dump the raw LVB */
1964 lvb = lockres->l_lksb.lvb;
1965 for(i = 0; i < DLM_LVB_LEN; i++)
1966 seq_printf(m, "0x%x\t", lvb[i]);
1969 seq_printf(m, "\n");
1973 static struct seq_operations ocfs2_dlm_seq_ops = {
1974 .start = ocfs2_dlm_seq_start,
1975 .stop = ocfs2_dlm_seq_stop,
1976 .next = ocfs2_dlm_seq_next,
1977 .show = ocfs2_dlm_seq_show,
1980 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
1982 struct seq_file *seq = (struct seq_file *) file->private_data;
1983 struct ocfs2_dlm_seq_priv *priv = seq->private;
1984 struct ocfs2_lock_res *res = &priv->p_iter_res;
1986 ocfs2_remove_lockres_tracking(res);
1987 ocfs2_put_dlm_debug(priv->p_dlm_debug);
1988 return seq_release_private(inode, file);
1991 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
1994 struct ocfs2_dlm_seq_priv *priv;
1995 struct seq_file *seq;
1996 struct ocfs2_super *osb;
1998 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2004 osb = (struct ocfs2_super *) inode->u.generic_ip;
2005 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2006 priv->p_dlm_debug = osb->osb_dlm_debug;
2007 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2009 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2016 seq = (struct seq_file *) file->private_data;
2017 seq->private = priv;
2019 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2026 static const struct file_operations ocfs2_dlm_debug_fops = {
2027 .open = ocfs2_dlm_debug_open,
2028 .release = ocfs2_dlm_debug_release,
2030 .llseek = seq_lseek,
2033 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2036 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2038 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2040 osb->osb_debug_root,
2042 &ocfs2_dlm_debug_fops);
2043 if (!dlm_debug->d_locking_state) {
2046 "Unable to create locking state debugfs file.\n");
2050 ocfs2_get_dlm_debug(dlm_debug);
2055 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2057 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2060 debugfs_remove(dlm_debug->d_locking_state);
2061 ocfs2_put_dlm_debug(dlm_debug);
2065 int ocfs2_dlm_init(struct ocfs2_super *osb)
2069 struct dlm_ctxt *dlm;
2073 status = ocfs2_dlm_init_debug(osb);
2079 /* launch vote thread */
2080 osb->vote_task = kthread_run(ocfs2_vote_thread, osb, "ocfs2vote");
2081 if (IS_ERR(osb->vote_task)) {
2082 status = PTR_ERR(osb->vote_task);
2083 osb->vote_task = NULL;
2088 /* used by the dlm code to make message headers unique, each
2089 * node in this domain must agree on this. */
2090 dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
2092 /* for now, uuid == domain */
2093 dlm = dlm_register_domain(osb->uuid_str, dlm_key);
2095 status = PTR_ERR(dlm);
2100 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2101 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2103 dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
2110 ocfs2_dlm_shutdown_debug(osb);
2112 kthread_stop(osb->vote_task);
2119 void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2123 dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
2125 ocfs2_drop_osb_locks(osb);
2127 if (osb->vote_task) {
2128 kthread_stop(osb->vote_task);
2129 osb->vote_task = NULL;
2132 ocfs2_lock_res_free(&osb->osb_super_lockres);
2133 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2135 dlm_unregister_domain(osb->dlm);
2138 ocfs2_dlm_shutdown_debug(osb);
2143 static void ocfs2_unlock_ast_func(void *opaque, enum dlm_status status)
2145 struct ocfs2_lock_res *lockres = opaque;
2146 unsigned long flags;
2150 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2151 lockres->l_unlock_action);
2153 spin_lock_irqsave(&lockres->l_lock, flags);
2154 /* We tried to cancel a convert request, but it was already
2155 * granted. All we want to do here is clear our unlock
2156 * state. The wake_up call done at the bottom is redundant
2157 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2158 * hurt anything anyway */
2159 if (status == DLM_CANCELGRANT &&
2160 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2161 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2163 /* We don't clear the busy flag in this case as it
2164 * should have been cleared by the ast which the dlm
2166 goto complete_unlock;
2169 if (status != DLM_NORMAL) {
2170 mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
2171 "unlock_action %d\n", status, lockres->l_name,
2172 lockres->l_unlock_action);
2173 spin_unlock_irqrestore(&lockres->l_lock, flags);
2177 switch(lockres->l_unlock_action) {
2178 case OCFS2_UNLOCK_CANCEL_CONVERT:
2179 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2180 lockres->l_action = OCFS2_AST_INVALID;
2182 case OCFS2_UNLOCK_DROP_LOCK:
2183 lockres->l_level = LKM_IVMODE;
2189 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2191 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2192 spin_unlock_irqrestore(&lockres->l_lock, flags);
2194 wake_up(&lockres->l_event);
2199 typedef void (ocfs2_pre_drop_cb_t)(struct ocfs2_lock_res *, void *);
2201 struct drop_lock_cb {
2202 ocfs2_pre_drop_cb_t *drop_func;
2206 static int ocfs2_drop_lock(struct ocfs2_super *osb,
2207 struct ocfs2_lock_res *lockres,
2208 struct drop_lock_cb *dcb)
2210 enum dlm_status status;
2211 unsigned long flags;
2213 /* We didn't get anywhere near actually using this lockres. */
2214 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2217 spin_lock_irqsave(&lockres->l_lock, flags);
2219 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2220 "lockres %s, flags 0x%lx\n",
2221 lockres->l_name, lockres->l_flags);
2223 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2224 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2225 "%u, unlock_action = %u\n",
2226 lockres->l_name, lockres->l_flags, lockres->l_action,
2227 lockres->l_unlock_action);
2229 spin_unlock_irqrestore(&lockres->l_lock, flags);
2231 /* XXX: Today we just wait on any busy
2232 * locks... Perhaps we need to cancel converts in the
2234 ocfs2_wait_on_busy_lock(lockres);
2236 spin_lock_irqsave(&lockres->l_lock, flags);
2240 dcb->drop_func(lockres, dcb->drop_data);
2242 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2243 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2245 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2246 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2248 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2249 spin_unlock_irqrestore(&lockres->l_lock, flags);
2253 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2255 /* make sure we never get here while waiting for an ast to
2257 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2259 /* is this necessary? */
2260 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2261 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2262 spin_unlock_irqrestore(&lockres->l_lock, flags);
2264 mlog(0, "lock %s\n", lockres->l_name);
2266 status = dlmunlock(osb->dlm, &lockres->l_lksb, LKM_VALBLK,
2267 lockres->l_ops->unlock_ast, lockres);
2268 if (status != DLM_NORMAL) {
2269 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2270 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2271 dlm_print_one_lock(lockres->l_lksb.lockid);
2274 mlog(0, "lock %s, successfull return from dlmunlock\n",
2277 ocfs2_wait_on_busy_lock(lockres);
2283 /* Mark the lockres as being dropped. It will no longer be
2284 * queued if blocking, but we still may have to wait on it
2285 * being dequeued from the vote thread before we can consider
2288 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2289 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2292 struct ocfs2_mask_waiter mw;
2293 unsigned long flags;
2295 ocfs2_init_mask_waiter(&mw);
2297 spin_lock_irqsave(&lockres->l_lock, flags);
2298 lockres->l_flags |= OCFS2_LOCK_FREEING;
2299 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2300 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2301 spin_unlock_irqrestore(&lockres->l_lock, flags);
2303 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2305 status = ocfs2_wait_for_mask(&mw);
2309 spin_lock_irqsave(&lockres->l_lock, flags);
2311 spin_unlock_irqrestore(&lockres->l_lock, flags);
2314 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2320 ocfs2_mark_lockres_freeing(&osb->osb_super_lockres);
2322 status = ocfs2_drop_lock(osb, &osb->osb_super_lockres, NULL);
2326 ocfs2_mark_lockres_freeing(&osb->osb_rename_lockres);
2328 status = ocfs2_drop_lock(osb, &osb->osb_rename_lockres, NULL);
2335 static void ocfs2_meta_pre_drop(struct ocfs2_lock_res *lockres, void *data)
2337 struct inode *inode = data;
2339 /* the metadata lock requires a bit more work as we have an
2340 * LVB to worry about. */
2341 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2342 lockres->l_level == LKM_EXMODE &&
2343 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2344 __ocfs2_stuff_meta_lvb(inode);
2347 int ocfs2_drop_inode_locks(struct inode *inode)
2350 struct drop_lock_cb meta_dcb = { ocfs2_meta_pre_drop, inode, };
2354 /* No need to call ocfs2_mark_lockres_freeing here -
2355 * ocfs2_clear_inode has done it for us. */
2357 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2358 &OCFS2_I(inode)->ip_data_lockres,
2365 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2366 &OCFS2_I(inode)->ip_meta_lockres,
2370 if (err < 0 && !status)
2373 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2374 &OCFS2_I(inode)->ip_rw_lockres,
2378 if (err < 0 && !status)
2385 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2388 assert_spin_locked(&lockres->l_lock);
2390 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
2392 if (lockres->l_level <= new_level) {
2393 mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
2394 lockres->l_level, new_level);
2398 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2399 lockres->l_name, new_level, lockres->l_blocking);
2401 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2402 lockres->l_requested = new_level;
2403 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2406 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2407 struct ocfs2_lock_res *lockres,
2411 int ret, dlm_flags = LKM_CONVERT;
2412 enum dlm_status status;
2417 dlm_flags |= LKM_VALBLK;
2419 status = dlmlock(osb->dlm,
2424 OCFS2_LOCK_ID_MAX_LEN - 1,
2425 lockres->l_ops->ast,
2427 lockres->l_ops->bast);
2428 if (status != DLM_NORMAL) {
2429 ocfs2_log_dlm_error("dlmlock", status, lockres);
2431 ocfs2_recover_from_dlm_error(lockres, 1);
2441 /* returns 1 when the caller should unlock and call dlmunlock */
2442 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2443 struct ocfs2_lock_res *lockres)
2445 assert_spin_locked(&lockres->l_lock);
2448 mlog(0, "lock %s\n", lockres->l_name);
2450 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2451 /* If we're already trying to cancel a lock conversion
2452 * then just drop the spinlock and allow the caller to
2453 * requeue this lock. */
2455 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2459 /* were we in a convert when we got the bast fire? */
2460 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2461 lockres->l_action != OCFS2_AST_DOWNCONVERT);
2462 /* set things up for the unlockast to know to just
2463 * clear out the ast_action and unset busy, etc. */
2464 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2466 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2467 "lock %s, invalid flags: 0x%lx\n",
2468 lockres->l_name, lockres->l_flags);
2473 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2474 struct ocfs2_lock_res *lockres)
2477 enum dlm_status status;
2480 mlog(0, "lock %s\n", lockres->l_name);
2483 status = dlmunlock(osb->dlm,
2486 lockres->l_ops->unlock_ast,
2488 if (status != DLM_NORMAL) {
2489 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2491 ocfs2_recover_from_dlm_error(lockres, 0);
2494 mlog(0, "lock %s return from dlmunlock\n", lockres->l_name);
2500 static inline int ocfs2_can_downconvert_meta_lock(struct inode *inode,
2501 struct ocfs2_lock_res *lockres,
2508 BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
2510 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2512 mlog(0, "lockres %s currently being refreshed -- backing "
2513 "off!\n", lockres->l_name);
2514 } else if (new_level == LKM_PRMODE)
2515 ret = !lockres->l_ex_holders &&
2516 ocfs2_inode_fully_checkpointed(inode);
2517 else /* Must be NLMODE we're converting to. */
2518 ret = !lockres->l_ro_holders && !lockres->l_ex_holders &&
2519 ocfs2_inode_fully_checkpointed(inode);
2525 static int ocfs2_do_unblock_meta(struct inode *inode,
2531 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
2532 unsigned long flags;
2534 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2538 spin_lock_irqsave(&lockres->l_lock, flags);
2540 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2542 mlog(0, "l_level=%d, l_blocking=%d\n", lockres->l_level,
2543 lockres->l_blocking);
2545 BUG_ON(lockres->l_level != LKM_EXMODE &&
2546 lockres->l_level != LKM_PRMODE);
2548 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2550 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2551 spin_unlock_irqrestore(&lockres->l_lock, flags);
2553 ret = ocfs2_cancel_convert(osb, lockres);
2560 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2562 mlog(0, "l_level=%d, l_blocking=%d, new_level=%d\n",
2563 lockres->l_level, lockres->l_blocking, new_level);
2565 if (ocfs2_can_downconvert_meta_lock(inode, lockres, new_level)) {
2566 if (lockres->l_level == LKM_EXMODE)
2569 /* If the lock hasn't been refreshed yet (rare), then
2570 * our memory inode values are old and we skip
2571 * stuffing the lvb. There's no need to actually clear
2572 * out the lvb here as it's value is still valid. */
2573 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2575 __ocfs2_stuff_meta_lvb(inode);
2577 mlog(0, "lockres %s: downconverting stale lock!\n",
2580 mlog(0, "calling ocfs2_downconvert_lock with l_level=%d, "
2581 "l_blocking=%d, new_level=%d\n",
2582 lockres->l_level, lockres->l_blocking, new_level);
2584 ocfs2_prepare_downconvert(lockres, new_level);
2585 spin_unlock_irqrestore(&lockres->l_lock, flags);
2586 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
2589 if (!ocfs2_inode_fully_checkpointed(inode))
2590 ocfs2_start_checkpoint(osb);
2593 spin_unlock_irqrestore(&lockres->l_lock, flags);
2600 static int ocfs2_generic_unblock_lock(struct ocfs2_super *osb,
2601 struct ocfs2_lock_res *lockres,
2603 ocfs2_convert_worker_t *worker)
2605 unsigned long flags;
2612 spin_lock_irqsave(&lockres->l_lock, flags);
2614 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2617 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2619 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2620 spin_unlock_irqrestore(&lockres->l_lock, flags);
2622 ret = ocfs2_cancel_convert(osb, lockres);
2629 /* if we're blocking an exclusive and we have *any* holders,
2631 if ((lockres->l_blocking == LKM_EXMODE)
2632 && (lockres->l_ex_holders || lockres->l_ro_holders)) {
2633 spin_unlock_irqrestore(&lockres->l_lock, flags);
2639 /* If it's a PR we're blocking, then only
2640 * requeue if we've got any EX holders */
2641 if (lockres->l_blocking == LKM_PRMODE &&
2642 lockres->l_ex_holders) {
2643 spin_unlock_irqrestore(&lockres->l_lock, flags);
2649 /* If we get here, then we know that there are no more
2650 * incompatible holders (and anyone asking for an incompatible
2651 * lock is blocked). We can now downconvert the lock */
2655 /* Some lockres types want to do a bit of work before
2656 * downconverting a lock. Allow that here. The worker function
2657 * may sleep, so we save off a copy of what we're blocking as
2658 * it may change while we're not holding the spin lock. */
2659 blocking = lockres->l_blocking;
2660 spin_unlock_irqrestore(&lockres->l_lock, flags);
2662 worker(lockres, blocking);
2664 spin_lock_irqsave(&lockres->l_lock, flags);
2665 if (blocking != lockres->l_blocking) {
2666 /* If this changed underneath us, then we can't drop
2673 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2675 ocfs2_prepare_downconvert(lockres, new_level);
2676 spin_unlock_irqrestore(&lockres->l_lock, flags);
2677 ret = ocfs2_downconvert_lock(osb, lockres, new_level, 0);
2683 static void ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
2686 struct inode *inode;
2687 struct address_space *mapping;
2691 inode = ocfs2_lock_res_inode(lockres);
2692 mapping = inode->i_mapping;
2694 if (filemap_fdatawrite(mapping)) {
2695 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
2696 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2698 sync_mapping_buffers(mapping);
2699 if (blocking == LKM_EXMODE) {
2700 truncate_inode_pages(mapping, 0);
2701 unmap_mapping_range(mapping, 0, 0, 0);
2703 /* We only need to wait on the I/O if we're not also
2704 * truncating pages because truncate_inode_pages waits
2705 * for us above. We don't truncate pages if we're
2706 * blocking anything < EXMODE because we want to keep
2707 * them around in that case. */
2708 filemap_fdatawait(mapping);
2714 int ocfs2_unblock_data(struct ocfs2_lock_res *lockres,
2718 struct inode *inode;
2719 struct ocfs2_super *osb;
2723 inode = ocfs2_lock_res_inode(lockres);
2724 osb = OCFS2_SB(inode->i_sb);
2726 mlog(0, "unblock inode %llu\n",
2727 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2729 status = ocfs2_generic_unblock_lock(osb,
2732 ocfs2_data_convert_worker);
2736 mlog(0, "inode %llu, requeue = %d\n",
2737 (unsigned long long)OCFS2_I(inode)->ip_blkno, *requeue);
2743 static int ocfs2_unblock_inode_lock(struct ocfs2_lock_res *lockres,
2747 struct inode *inode;
2751 mlog(0, "Unblock lockres %s\n", lockres->l_name);
2753 inode = ocfs2_lock_res_inode(lockres);
2755 status = ocfs2_generic_unblock_lock(OCFS2_SB(inode->i_sb),
2767 int ocfs2_unblock_meta(struct ocfs2_lock_res *lockres,
2771 struct inode *inode;
2775 inode = ocfs2_lock_res_inode(lockres);
2777 mlog(0, "unblock inode %llu\n",
2778 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2780 status = ocfs2_do_unblock_meta(inode, requeue);
2784 mlog(0, "inode %llu, requeue = %d\n",
2785 (unsigned long long)OCFS2_I(inode)->ip_blkno, *requeue);
2791 /* Generic unblock function for any lockres whose private data is an
2792 * ocfs2_super pointer. */
2793 static int ocfs2_unblock_osb_lock(struct ocfs2_lock_res *lockres,
2797 struct ocfs2_super *osb;
2801 mlog(0, "Unblock lockres %s\n", lockres->l_name);
2803 osb = ocfs2_lock_res_super(lockres);
2805 status = ocfs2_generic_unblock_lock(osb,
2816 void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
2817 struct ocfs2_lock_res *lockres)
2821 unsigned long flags;
2823 /* Our reference to the lockres in this function can be
2824 * considered valid until we remove the OCFS2_LOCK_QUEUED
2830 BUG_ON(!lockres->l_ops);
2831 BUG_ON(!lockres->l_ops->unblock);
2833 mlog(0, "lockres %s blocked.\n", lockres->l_name);
2835 /* Detect whether a lock has been marked as going away while
2836 * the vote thread was processing other things. A lock can
2837 * still be marked with OCFS2_LOCK_FREEING after this check,
2838 * but short circuiting here will still save us some
2840 spin_lock_irqsave(&lockres->l_lock, flags);
2841 if (lockres->l_flags & OCFS2_LOCK_FREEING)
2843 spin_unlock_irqrestore(&lockres->l_lock, flags);
2845 status = lockres->l_ops->unblock(lockres, &requeue);
2849 spin_lock_irqsave(&lockres->l_lock, flags);
2851 if (lockres->l_flags & OCFS2_LOCK_FREEING || !requeue) {
2852 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
2854 ocfs2_schedule_blocked_lock(osb, lockres);
2856 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
2857 requeue ? "yes" : "no");
2858 spin_unlock_irqrestore(&lockres->l_lock, flags);
2863 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
2864 struct ocfs2_lock_res *lockres)
2868 assert_spin_locked(&lockres->l_lock);
2870 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
2871 /* Do not schedule a lock for downconvert when it's on
2872 * the way to destruction - any nodes wanting access
2873 * to the resource will get it soon. */
2874 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
2875 lockres->l_name, lockres->l_flags);
2879 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
2881 spin_lock(&osb->vote_task_lock);
2882 if (list_empty(&lockres->l_blocked_list)) {
2883 list_add_tail(&lockres->l_blocked_list,
2884 &osb->blocked_lock_list);
2885 osb->blocked_lock_count++;
2887 spin_unlock(&osb->vote_task_lock);
2892 /* This aids in debugging situations where a bad LVB might be involved. */
2893 void ocfs2_dump_meta_lvb_info(u64 level,
2894 const char *function,
2896 struct ocfs2_lock_res *lockres)
2898 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
2900 mlog(level, "LVB information for %s (called from %s:%u):\n",
2901 lockres->l_name, function, line);
2902 mlog(level, "version: %u, clusters: %u\n",
2903 be32_to_cpu(lvb->lvb_version), be32_to_cpu(lvb->lvb_iclusters));
2904 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
2905 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
2906 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
2907 be16_to_cpu(lvb->lvb_imode));
2908 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
2909 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
2910 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
2911 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
2912 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
2913 be32_to_cpu(lvb->lvb_iattr));