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,
815 lockres->l_ops->bast);
816 if (status != DLM_NORMAL) {
817 ocfs2_log_dlm_error("dlmlock", status, lockres);
819 ocfs2_recover_from_dlm_error(lockres, 1);
822 mlog(0, "lock %s, successfull return from dlmlock\n", lockres->l_name);
829 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
835 spin_lock_irqsave(&lockres->l_lock, flags);
836 ret = lockres->l_flags & flag;
837 spin_unlock_irqrestore(&lockres->l_lock, flags);
842 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
845 wait_event(lockres->l_event,
846 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
849 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
852 wait_event(lockres->l_event,
853 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
856 /* predict what lock level we'll be dropping down to on behalf
857 * of another node, and return true if the currently wanted
858 * level will be compatible with it. */
859 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
862 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
864 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
867 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
869 INIT_LIST_HEAD(&mw->mw_item);
870 init_completion(&mw->mw_complete);
873 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
875 wait_for_completion(&mw->mw_complete);
876 /* Re-arm the completion in case we want to wait on it again */
877 INIT_COMPLETION(mw->mw_complete);
878 return mw->mw_status;
881 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
882 struct ocfs2_mask_waiter *mw,
886 BUG_ON(!list_empty(&mw->mw_item));
888 assert_spin_locked(&lockres->l_lock);
890 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
895 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
896 * if the mask still hadn't reached its goal */
897 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
898 struct ocfs2_mask_waiter *mw)
903 spin_lock_irqsave(&lockres->l_lock, flags);
904 if (!list_empty(&mw->mw_item)) {
905 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
908 list_del_init(&mw->mw_item);
909 init_completion(&mw->mw_complete);
911 spin_unlock_irqrestore(&lockres->l_lock, flags);
917 static int ocfs2_cluster_lock(struct ocfs2_super *osb,
918 struct ocfs2_lock_res *lockres,
923 struct ocfs2_mask_waiter mw;
924 enum dlm_status status;
925 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
926 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
931 ocfs2_init_mask_waiter(&mw);
936 if (catch_signals && signal_pending(current)) {
941 spin_lock_irqsave(&lockres->l_lock, flags);
943 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
944 "Cluster lock called on freeing lockres %s! flags "
945 "0x%lx\n", lockres->l_name, lockres->l_flags);
947 /* We only compare against the currently granted level
948 * here. If the lock is blocked waiting on a downconvert,
949 * we'll get caught below. */
950 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
951 level > lockres->l_level) {
952 /* is someone sitting in dlm_lock? If so, wait on
954 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
959 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
960 /* lock has not been created yet. */
961 spin_unlock_irqrestore(&lockres->l_lock, flags);
963 ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
971 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
972 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
973 /* is the lock is currently blocked on behalf of
975 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
980 if (level > lockres->l_level) {
981 if (lockres->l_action != OCFS2_AST_INVALID)
982 mlog(ML_ERROR, "lockres %s has action %u pending\n",
983 lockres->l_name, lockres->l_action);
985 lockres->l_action = OCFS2_AST_CONVERT;
986 lockres->l_requested = level;
987 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
988 spin_unlock_irqrestore(&lockres->l_lock, flags);
990 BUG_ON(level == LKM_IVMODE);
991 BUG_ON(level == LKM_NLMODE);
993 mlog(0, "lock %s, convert from %d to level = %d\n",
994 lockres->l_name, lockres->l_level, level);
996 /* call dlm_lock to upgrade lock now */
997 status = dlmlock(osb->dlm,
1000 lkm_flags|LKM_CONVERT|LKM_VALBLK,
1002 lockres->l_ops->ast,
1004 lockres->l_ops->bast);
1005 if (status != DLM_NORMAL) {
1006 if ((lkm_flags & LKM_NOQUEUE) &&
1007 (status == DLM_NOTQUEUED))
1010 ocfs2_log_dlm_error("dlmlock", status,
1014 ocfs2_recover_from_dlm_error(lockres, 1);
1018 mlog(0, "lock %s, successfull return from dlmlock\n",
1021 /* At this point we've gone inside the dlm and need to
1022 * complete our work regardless. */
1025 /* wait for busy to clear and carry on */
1029 /* Ok, if we get here then we're good to go. */
1030 ocfs2_inc_holders(lockres, level);
1034 spin_unlock_irqrestore(&lockres->l_lock, flags);
1037 * This is helping work around a lock inversion between the page lock
1038 * and dlm locks. One path holds the page lock while calling aops
1039 * which block acquiring dlm locks. The voting thread holds dlm
1040 * locks while acquiring page locks while down converting data locks.
1041 * This block is helping an aop path notice the inversion and back
1042 * off to unlock its page lock before trying the dlm lock again.
1044 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1045 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1047 if (lockres_remove_mask_waiter(lockres, &mw))
1053 ret = ocfs2_wait_for_mask(&mw);
1063 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1064 struct ocfs2_lock_res *lockres,
1067 unsigned long flags;
1070 spin_lock_irqsave(&lockres->l_lock, flags);
1071 ocfs2_dec_holders(lockres, level);
1072 ocfs2_vote_on_unlock(osb, lockres);
1073 spin_unlock_irqrestore(&lockres->l_lock, flags);
1077 static int ocfs2_create_new_inode_lock(struct inode *inode,
1078 struct ocfs2_lock_res *lockres)
1080 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1081 unsigned long flags;
1083 spin_lock_irqsave(&lockres->l_lock, flags);
1084 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1085 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1086 spin_unlock_irqrestore(&lockres->l_lock, flags);
1088 return ocfs2_lock_create(osb, lockres, LKM_EXMODE, LKM_LOCAL);
1091 /* Grants us an EX lock on the data and metadata resources, skipping
1092 * the normal cluster directory lookup. Use this ONLY on newly created
1093 * inodes which other nodes can't possibly see, and which haven't been
1094 * hashed in the inode hash yet. This can give us a good performance
1095 * increase as it'll skip the network broadcast normally associated
1096 * with creating a new lock resource. */
1097 int ocfs2_create_new_inode_locks(struct inode *inode)
1102 BUG_ON(!ocfs2_inode_is_new(inode));
1106 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1108 /* NOTE: That we don't increment any of the holder counts, nor
1109 * do we add anything to a journal handle. Since this is
1110 * supposed to be a new inode which the cluster doesn't know
1111 * about yet, there is no need to. As far as the LVB handling
1112 * is concerned, this is basically like acquiring an EX lock
1113 * on a resource which has an invalid one -- we'll set it
1114 * valid when we release the EX. */
1116 ret = ocfs2_create_new_inode_lock(inode,
1117 &OCFS2_I(inode)->ip_rw_lockres);
1123 ret = ocfs2_create_new_inode_lock(inode,
1124 &OCFS2_I(inode)->ip_meta_lockres);
1130 ret = ocfs2_create_new_inode_lock(inode,
1131 &OCFS2_I(inode)->ip_data_lockres);
1142 int ocfs2_rw_lock(struct inode *inode, int write)
1145 struct ocfs2_lock_res *lockres;
1151 mlog(0, "inode %llu take %s RW lock\n",
1152 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1153 write ? "EXMODE" : "PRMODE");
1155 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1157 level = write ? LKM_EXMODE : LKM_PRMODE;
1159 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1168 void ocfs2_rw_unlock(struct inode *inode, int write)
1170 int level = write ? LKM_EXMODE : LKM_PRMODE;
1171 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1175 mlog(0, "inode %llu drop %s RW lock\n",
1176 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1177 write ? "EXMODE" : "PRMODE");
1179 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1184 int ocfs2_data_lock_full(struct inode *inode,
1188 int status = 0, level;
1189 struct ocfs2_lock_res *lockres;
1195 mlog(0, "inode %llu take %s DATA lock\n",
1196 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1197 write ? "EXMODE" : "PRMODE");
1199 /* We'll allow faking a readonly data lock for
1201 if (ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb))) {
1209 lockres = &OCFS2_I(inode)->ip_data_lockres;
1211 level = write ? LKM_EXMODE : LKM_PRMODE;
1213 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level,
1215 if (status < 0 && status != -EAGAIN)
1223 /* see ocfs2_meta_lock_with_page() */
1224 int ocfs2_data_lock_with_page(struct inode *inode,
1230 ret = ocfs2_data_lock_full(inode, write, OCFS2_LOCK_NONBLOCK);
1231 if (ret == -EAGAIN) {
1233 if (ocfs2_data_lock(inode, write) == 0)
1234 ocfs2_data_unlock(inode, write);
1235 ret = AOP_TRUNCATED_PAGE;
1241 static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
1242 struct ocfs2_lock_res *lockres)
1248 /* If we know that another node is waiting on our lock, kick
1249 * the vote thread * pre-emptively when we reach a release
1251 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1252 switch(lockres->l_blocking) {
1254 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1258 if (!lockres->l_ex_holders)
1267 ocfs2_kick_vote_thread(osb);
1272 void ocfs2_data_unlock(struct inode *inode,
1275 int level = write ? LKM_EXMODE : LKM_PRMODE;
1276 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_data_lockres;
1280 mlog(0, "inode %llu drop %s DATA lock\n",
1281 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1282 write ? "EXMODE" : "PRMODE");
1284 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1285 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1290 #define OCFS2_SEC_BITS 34
1291 #define OCFS2_SEC_SHIFT (64 - 34)
1292 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1294 /* LVB only has room for 64 bits of time here so we pack it for
1296 static u64 ocfs2_pack_timespec(struct timespec *spec)
1299 u64 sec = spec->tv_sec;
1300 u32 nsec = spec->tv_nsec;
1302 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1307 /* Call this with the lockres locked. I am reasonably sure we don't
1308 * need ip_lock in this function as anyone who would be changing those
1309 * values is supposed to be blocked in ocfs2_meta_lock right now. */
1310 static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1312 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1313 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1314 struct ocfs2_meta_lvb *lvb;
1318 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1320 lvb->lvb_version = cpu_to_be32(OCFS2_LVB_VERSION);
1321 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1322 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1323 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1324 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1325 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1326 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1327 lvb->lvb_iatime_packed =
1328 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1329 lvb->lvb_ictime_packed =
1330 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1331 lvb->lvb_imtime_packed =
1332 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1333 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
1335 mlog_meta_lvb(0, lockres);
1340 static void ocfs2_unpack_timespec(struct timespec *spec,
1343 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1344 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1347 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1349 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1350 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1351 struct ocfs2_meta_lvb *lvb;
1355 mlog_meta_lvb(0, lockres);
1357 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1359 /* We're safe here without the lockres lock... */
1360 spin_lock(&oi->ip_lock);
1361 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1362 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1364 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1365 ocfs2_set_inode_flags(inode);
1367 /* fast-symlinks are a special case */
1368 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1369 inode->i_blocks = 0;
1372 ocfs2_align_bytes_to_sectors(i_size_read(inode));
1374 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1375 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1376 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1377 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1378 ocfs2_unpack_timespec(&inode->i_atime,
1379 be64_to_cpu(lvb->lvb_iatime_packed));
1380 ocfs2_unpack_timespec(&inode->i_mtime,
1381 be64_to_cpu(lvb->lvb_imtime_packed));
1382 ocfs2_unpack_timespec(&inode->i_ctime,
1383 be64_to_cpu(lvb->lvb_ictime_packed));
1384 spin_unlock(&oi->ip_lock);
1389 static inline int ocfs2_meta_lvb_is_trustable(struct ocfs2_lock_res *lockres)
1391 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1393 if (be32_to_cpu(lvb->lvb_version) == OCFS2_LVB_VERSION)
1398 /* Determine whether a lock resource needs to be refreshed, and
1399 * arbitrate who gets to refresh it.
1401 * 0 means no refresh needed.
1403 * > 0 means you need to refresh this and you MUST call
1404 * ocfs2_complete_lock_res_refresh afterwards. */
1405 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1407 unsigned long flags;
1413 spin_lock_irqsave(&lockres->l_lock, flags);
1414 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1415 spin_unlock_irqrestore(&lockres->l_lock, flags);
1419 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1420 spin_unlock_irqrestore(&lockres->l_lock, flags);
1422 ocfs2_wait_on_refreshing_lock(lockres);
1426 /* Ok, I'll be the one to refresh this lock. */
1427 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1428 spin_unlock_irqrestore(&lockres->l_lock, flags);
1436 /* If status is non zero, I'll mark it as not being in refresh
1437 * anymroe, but i won't clear the needs refresh flag. */
1438 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1441 unsigned long flags;
1444 spin_lock_irqsave(&lockres->l_lock, flags);
1445 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1447 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1448 spin_unlock_irqrestore(&lockres->l_lock, flags);
1450 wake_up(&lockres->l_event);
1455 /* may or may not return a bh if it went to disk. */
1456 static int ocfs2_meta_lock_update(struct inode *inode,
1457 struct buffer_head **bh)
1460 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1461 struct ocfs2_lock_res *lockres;
1462 struct ocfs2_dinode *fe;
1466 spin_lock(&oi->ip_lock);
1467 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1468 mlog(0, "Orphaned inode %llu was deleted while we "
1469 "were waiting on a lock. ip_flags = 0x%x\n",
1470 (unsigned long long)oi->ip_blkno, oi->ip_flags);
1471 spin_unlock(&oi->ip_lock);
1475 spin_unlock(&oi->ip_lock);
1477 lockres = &oi->ip_meta_lockres;
1479 if (!ocfs2_should_refresh_lock_res(lockres))
1482 /* This will discard any caching information we might have had
1483 * for the inode metadata. */
1484 ocfs2_metadata_cache_purge(inode);
1486 /* will do nothing for inode types that don't use the extent
1487 * map (directories, bitmap files, etc) */
1488 ocfs2_extent_map_trunc(inode, 0);
1490 if (ocfs2_meta_lvb_is_trustable(lockres)) {
1491 mlog(0, "Trusting LVB on inode %llu\n",
1492 (unsigned long long)oi->ip_blkno);
1493 ocfs2_refresh_inode_from_lvb(inode);
1495 /* Boo, we have to go to disk. */
1496 /* read bh, cast, ocfs2_refresh_inode */
1497 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1498 bh, OCFS2_BH_CACHED, inode);
1503 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1505 /* This is a good chance to make sure we're not
1506 * locking an invalid object.
1508 * We bug on a stale inode here because we checked
1509 * above whether it was wiped from disk. The wiping
1510 * node provides a guarantee that we receive that
1511 * message and can mark the inode before dropping any
1512 * locks associated with it. */
1513 if (!OCFS2_IS_VALID_DINODE(fe)) {
1514 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1518 mlog_bug_on_msg(inode->i_generation !=
1519 le32_to_cpu(fe->i_generation),
1520 "Invalid dinode %llu disk generation: %u "
1521 "inode->i_generation: %u\n",
1522 (unsigned long long)oi->ip_blkno,
1523 le32_to_cpu(fe->i_generation),
1524 inode->i_generation);
1525 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1526 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
1527 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1528 (unsigned long long)oi->ip_blkno,
1529 (unsigned long long)le64_to_cpu(fe->i_dtime),
1530 le32_to_cpu(fe->i_flags));
1532 ocfs2_refresh_inode(inode, fe);
1537 ocfs2_complete_lock_res_refresh(lockres, status);
1543 static int ocfs2_assign_bh(struct inode *inode,
1544 struct buffer_head **ret_bh,
1545 struct buffer_head *passed_bh)
1550 /* Ok, the update went to disk for us, use the
1552 *ret_bh = passed_bh;
1558 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1559 OCFS2_I(inode)->ip_blkno,
1570 * returns < 0 error if the callback will never be called, otherwise
1571 * the result of the lock will be communicated via the callback.
1573 int ocfs2_meta_lock_full(struct inode *inode,
1574 struct ocfs2_journal_handle *handle,
1575 struct buffer_head **ret_bh,
1579 int status, level, dlm_flags, acquired;
1580 struct ocfs2_lock_res *lockres;
1581 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1582 struct buffer_head *local_bh = NULL;
1588 mlog(0, "inode %llu, take %s META lock\n",
1589 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1590 ex ? "EXMODE" : "PRMODE");
1594 /* We'll allow faking a readonly metadata lock for
1596 if (ocfs2_is_hard_readonly(osb)) {
1602 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1603 wait_event(osb->recovery_event,
1604 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1607 lockres = &OCFS2_I(inode)->ip_meta_lockres;
1608 level = ex ? LKM_EXMODE : LKM_PRMODE;
1610 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
1611 dlm_flags |= LKM_NOQUEUE;
1613 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1615 if (status != -EAGAIN && status != -EIOCBRETRY)
1620 /* Notify the error cleanup path to drop the cluster lock. */
1623 /* We wait twice because a node may have died while we were in
1624 * the lower dlm layers. The second time though, we've
1625 * committed to owning this lock so we don't allow signals to
1626 * abort the operation. */
1627 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1628 wait_event(osb->recovery_event,
1629 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1631 /* This is fun. The caller may want a bh back, or it may
1632 * not. ocfs2_meta_lock_update definitely wants one in, but
1633 * may or may not read one, depending on what's in the
1634 * LVB. The result of all of this is that we've *only* gone to
1635 * disk if we have to, so the complexity is worthwhile. */
1636 status = ocfs2_meta_lock_update(inode, &local_bh);
1638 if (status != -ENOENT)
1644 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
1652 status = ocfs2_handle_add_lock(handle, inode);
1659 if (ret_bh && (*ret_bh)) {
1664 ocfs2_meta_unlock(inode, ex);
1675 * This is working around a lock inversion between tasks acquiring DLM locks
1676 * while holding a page lock and the vote thread which blocks dlm lock acquiry
1677 * while acquiring page locks.
1679 * ** These _with_page variantes are only intended to be called from aop
1680 * methods that hold page locks and return a very specific *positive* error
1681 * code that aop methods pass up to the VFS -- test for errors with != 0. **
1683 * The DLM is called such that it returns -EAGAIN if it would have blocked
1684 * waiting for the vote thread. In that case we unlock our page so the vote
1685 * thread can make progress. Once we've done this we have to return
1686 * AOP_TRUNCATED_PAGE so the aop method that called us can bubble that back up
1687 * into the VFS who will then immediately retry the aop call.
1689 * We do a blocking lock and immediate unlock before returning, though, so that
1690 * the lock has a great chance of being cached on this node by the time the VFS
1691 * calls back to retry the aop. This has a potential to livelock as nodes
1692 * ping locks back and forth, but that's a risk we're willing to take to avoid
1693 * the lock inversion simply.
1695 int ocfs2_meta_lock_with_page(struct inode *inode,
1696 struct ocfs2_journal_handle *handle,
1697 struct buffer_head **ret_bh,
1703 ret = ocfs2_meta_lock_full(inode, handle, ret_bh, ex,
1704 OCFS2_LOCK_NONBLOCK);
1705 if (ret == -EAGAIN) {
1707 if (ocfs2_meta_lock(inode, handle, ret_bh, ex) == 0)
1708 ocfs2_meta_unlock(inode, ex);
1709 ret = AOP_TRUNCATED_PAGE;
1715 void ocfs2_meta_unlock(struct inode *inode,
1718 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1719 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
1723 mlog(0, "inode %llu drop %s META lock\n",
1724 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1725 ex ? "EXMODE" : "PRMODE");
1727 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1728 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1733 int ocfs2_super_lock(struct ocfs2_super *osb,
1737 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1738 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1739 struct buffer_head *bh;
1740 struct ocfs2_slot_info *si = osb->slot_info;
1744 if (ocfs2_is_hard_readonly(osb))
1747 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
1753 /* The super block lock path is really in the best position to
1754 * know when resources covered by the lock need to be
1755 * refreshed, so we do it here. Of course, making sense of
1756 * everything is up to the caller :) */
1757 status = ocfs2_should_refresh_lock_res(lockres);
1764 status = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0,
1767 ocfs2_update_slot_info(si);
1769 ocfs2_complete_lock_res_refresh(lockres, status);
1779 void ocfs2_super_unlock(struct ocfs2_super *osb,
1782 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1783 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1785 ocfs2_cluster_unlock(osb, lockres, level);
1788 int ocfs2_rename_lock(struct ocfs2_super *osb)
1791 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1793 if (ocfs2_is_hard_readonly(osb))
1796 status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
1803 void ocfs2_rename_unlock(struct ocfs2_super *osb)
1805 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1807 ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
1810 /* Reference counting of the dlm debug structure. We want this because
1811 * open references on the debug inodes can live on after a mount, so
1812 * we can't rely on the ocfs2_super to always exist. */
1813 static void ocfs2_dlm_debug_free(struct kref *kref)
1815 struct ocfs2_dlm_debug *dlm_debug;
1817 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
1822 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
1825 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
1828 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
1830 kref_get(&debug->d_refcnt);
1833 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
1835 struct ocfs2_dlm_debug *dlm_debug;
1837 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
1839 mlog_errno(-ENOMEM);
1843 kref_init(&dlm_debug->d_refcnt);
1844 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
1845 dlm_debug->d_locking_state = NULL;
1850 /* Access to this is arbitrated for us via seq_file->sem. */
1851 struct ocfs2_dlm_seq_priv {
1852 struct ocfs2_dlm_debug *p_dlm_debug;
1853 struct ocfs2_lock_res p_iter_res;
1854 struct ocfs2_lock_res p_tmp_res;
1857 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
1858 struct ocfs2_dlm_seq_priv *priv)
1860 struct ocfs2_lock_res *iter, *ret = NULL;
1861 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
1863 assert_spin_locked(&ocfs2_dlm_tracking_lock);
1865 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
1866 /* discover the head of the list */
1867 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
1868 mlog(0, "End of list found, %p\n", ret);
1872 /* We track our "dummy" iteration lockres' by a NULL
1874 if (iter->l_ops != NULL) {
1883 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
1885 struct ocfs2_dlm_seq_priv *priv = m->private;
1886 struct ocfs2_lock_res *iter;
1888 spin_lock(&ocfs2_dlm_tracking_lock);
1889 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
1891 /* Since lockres' have the lifetime of their container
1892 * (which can be inodes, ocfs2_supers, etc) we want to
1893 * copy this out to a temporary lockres while still
1894 * under the spinlock. Obviously after this we can't
1895 * trust any pointers on the copy returned, but that's
1896 * ok as the information we want isn't typically held
1898 priv->p_tmp_res = *iter;
1899 iter = &priv->p_tmp_res;
1901 spin_unlock(&ocfs2_dlm_tracking_lock);
1906 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
1910 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
1912 struct ocfs2_dlm_seq_priv *priv = m->private;
1913 struct ocfs2_lock_res *iter = v;
1914 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
1916 spin_lock(&ocfs2_dlm_tracking_lock);
1917 iter = ocfs2_dlm_next_res(iter, priv);
1918 list_del_init(&dummy->l_debug_list);
1920 list_add(&dummy->l_debug_list, &iter->l_debug_list);
1921 priv->p_tmp_res = *iter;
1922 iter = &priv->p_tmp_res;
1924 spin_unlock(&ocfs2_dlm_tracking_lock);
1929 /* So that debugfs.ocfs2 can determine which format is being used */
1930 #define OCFS2_DLM_DEBUG_STR_VERSION 1
1931 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
1935 struct ocfs2_lock_res *lockres = v;
1940 seq_printf(m, "0x%x\t"
1950 OCFS2_DLM_DEBUG_STR_VERSION,
1951 OCFS2_LOCK_ID_MAX_LEN, lockres->l_name,
1955 lockres->l_unlock_action,
1956 lockres->l_ro_holders,
1957 lockres->l_ex_holders,
1958 lockres->l_requested,
1959 lockres->l_blocking);
1961 /* Dump the raw LVB */
1962 lvb = lockres->l_lksb.lvb;
1963 for(i = 0; i < DLM_LVB_LEN; i++)
1964 seq_printf(m, "0x%x\t", lvb[i]);
1967 seq_printf(m, "\n");
1971 static struct seq_operations ocfs2_dlm_seq_ops = {
1972 .start = ocfs2_dlm_seq_start,
1973 .stop = ocfs2_dlm_seq_stop,
1974 .next = ocfs2_dlm_seq_next,
1975 .show = ocfs2_dlm_seq_show,
1978 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
1980 struct seq_file *seq = (struct seq_file *) file->private_data;
1981 struct ocfs2_dlm_seq_priv *priv = seq->private;
1982 struct ocfs2_lock_res *res = &priv->p_iter_res;
1984 ocfs2_remove_lockres_tracking(res);
1985 ocfs2_put_dlm_debug(priv->p_dlm_debug);
1986 return seq_release_private(inode, file);
1989 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
1992 struct ocfs2_dlm_seq_priv *priv;
1993 struct seq_file *seq;
1994 struct ocfs2_super *osb;
1996 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2002 osb = (struct ocfs2_super *) inode->u.generic_ip;
2003 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2004 priv->p_dlm_debug = osb->osb_dlm_debug;
2005 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2007 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2014 seq = (struct seq_file *) file->private_data;
2015 seq->private = priv;
2017 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2024 static const struct file_operations ocfs2_dlm_debug_fops = {
2025 .open = ocfs2_dlm_debug_open,
2026 .release = ocfs2_dlm_debug_release,
2028 .llseek = seq_lseek,
2031 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2034 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2036 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2038 osb->osb_debug_root,
2040 &ocfs2_dlm_debug_fops);
2041 if (!dlm_debug->d_locking_state) {
2044 "Unable to create locking state debugfs file.\n");
2048 ocfs2_get_dlm_debug(dlm_debug);
2053 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2055 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2058 debugfs_remove(dlm_debug->d_locking_state);
2059 ocfs2_put_dlm_debug(dlm_debug);
2063 int ocfs2_dlm_init(struct ocfs2_super *osb)
2067 struct dlm_ctxt *dlm;
2071 status = ocfs2_dlm_init_debug(osb);
2077 /* launch vote thread */
2078 osb->vote_task = kthread_run(ocfs2_vote_thread, osb, "ocfs2vote");
2079 if (IS_ERR(osb->vote_task)) {
2080 status = PTR_ERR(osb->vote_task);
2081 osb->vote_task = NULL;
2086 /* used by the dlm code to make message headers unique, each
2087 * node in this domain must agree on this. */
2088 dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
2090 /* for now, uuid == domain */
2091 dlm = dlm_register_domain(osb->uuid_str, dlm_key);
2093 status = PTR_ERR(dlm);
2098 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2099 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2101 dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
2108 ocfs2_dlm_shutdown_debug(osb);
2110 kthread_stop(osb->vote_task);
2117 void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2121 dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
2123 ocfs2_drop_osb_locks(osb);
2125 if (osb->vote_task) {
2126 kthread_stop(osb->vote_task);
2127 osb->vote_task = NULL;
2130 ocfs2_lock_res_free(&osb->osb_super_lockres);
2131 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2133 dlm_unregister_domain(osb->dlm);
2136 ocfs2_dlm_shutdown_debug(osb);
2141 static void ocfs2_unlock_ast_func(void *opaque, enum dlm_status status)
2143 struct ocfs2_lock_res *lockres = opaque;
2144 unsigned long flags;
2148 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2149 lockres->l_unlock_action);
2151 spin_lock_irqsave(&lockres->l_lock, flags);
2152 /* We tried to cancel a convert request, but it was already
2153 * granted. All we want to do here is clear our unlock
2154 * state. The wake_up call done at the bottom is redundant
2155 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2156 * hurt anything anyway */
2157 if (status == DLM_CANCELGRANT &&
2158 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2159 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2161 /* We don't clear the busy flag in this case as it
2162 * should have been cleared by the ast which the dlm
2164 goto complete_unlock;
2167 if (status != DLM_NORMAL) {
2168 mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
2169 "unlock_action %d\n", status, lockres->l_name,
2170 lockres->l_unlock_action);
2171 spin_unlock_irqrestore(&lockres->l_lock, flags);
2175 switch(lockres->l_unlock_action) {
2176 case OCFS2_UNLOCK_CANCEL_CONVERT:
2177 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2178 lockres->l_action = OCFS2_AST_INVALID;
2180 case OCFS2_UNLOCK_DROP_LOCK:
2181 lockres->l_level = LKM_IVMODE;
2187 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2189 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2190 spin_unlock_irqrestore(&lockres->l_lock, flags);
2192 wake_up(&lockres->l_event);
2197 typedef void (ocfs2_pre_drop_cb_t)(struct ocfs2_lock_res *, void *);
2199 struct drop_lock_cb {
2200 ocfs2_pre_drop_cb_t *drop_func;
2204 static int ocfs2_drop_lock(struct ocfs2_super *osb,
2205 struct ocfs2_lock_res *lockres,
2206 struct drop_lock_cb *dcb)
2208 enum dlm_status status;
2209 unsigned long flags;
2211 /* We didn't get anywhere near actually using this lockres. */
2212 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2215 spin_lock_irqsave(&lockres->l_lock, flags);
2217 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2218 "lockres %s, flags 0x%lx\n",
2219 lockres->l_name, lockres->l_flags);
2221 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2222 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2223 "%u, unlock_action = %u\n",
2224 lockres->l_name, lockres->l_flags, lockres->l_action,
2225 lockres->l_unlock_action);
2227 spin_unlock_irqrestore(&lockres->l_lock, flags);
2229 /* XXX: Today we just wait on any busy
2230 * locks... Perhaps we need to cancel converts in the
2232 ocfs2_wait_on_busy_lock(lockres);
2234 spin_lock_irqsave(&lockres->l_lock, flags);
2238 dcb->drop_func(lockres, dcb->drop_data);
2240 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2241 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2243 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2244 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2246 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2247 spin_unlock_irqrestore(&lockres->l_lock, flags);
2251 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2253 /* make sure we never get here while waiting for an ast to
2255 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2257 /* is this necessary? */
2258 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2259 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2260 spin_unlock_irqrestore(&lockres->l_lock, flags);
2262 mlog(0, "lock %s\n", lockres->l_name);
2264 status = dlmunlock(osb->dlm, &lockres->l_lksb, LKM_VALBLK,
2265 lockres->l_ops->unlock_ast, lockres);
2266 if (status != DLM_NORMAL) {
2267 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2268 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2269 dlm_print_one_lock(lockres->l_lksb.lockid);
2272 mlog(0, "lock %s, successfull return from dlmunlock\n",
2275 ocfs2_wait_on_busy_lock(lockres);
2281 /* Mark the lockres as being dropped. It will no longer be
2282 * queued if blocking, but we still may have to wait on it
2283 * being dequeued from the vote thread before we can consider
2286 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2287 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2290 struct ocfs2_mask_waiter mw;
2291 unsigned long flags;
2293 ocfs2_init_mask_waiter(&mw);
2295 spin_lock_irqsave(&lockres->l_lock, flags);
2296 lockres->l_flags |= OCFS2_LOCK_FREEING;
2297 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2298 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2299 spin_unlock_irqrestore(&lockres->l_lock, flags);
2301 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2303 status = ocfs2_wait_for_mask(&mw);
2307 spin_lock_irqsave(&lockres->l_lock, flags);
2309 spin_unlock_irqrestore(&lockres->l_lock, flags);
2312 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2318 ocfs2_mark_lockres_freeing(&osb->osb_super_lockres);
2320 status = ocfs2_drop_lock(osb, &osb->osb_super_lockres, NULL);
2324 ocfs2_mark_lockres_freeing(&osb->osb_rename_lockres);
2326 status = ocfs2_drop_lock(osb, &osb->osb_rename_lockres, NULL);
2333 static void ocfs2_meta_pre_drop(struct ocfs2_lock_res *lockres, void *data)
2335 struct inode *inode = data;
2337 /* the metadata lock requires a bit more work as we have an
2338 * LVB to worry about. */
2339 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2340 lockres->l_level == LKM_EXMODE &&
2341 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2342 __ocfs2_stuff_meta_lvb(inode);
2345 int ocfs2_drop_inode_locks(struct inode *inode)
2348 struct drop_lock_cb meta_dcb = { ocfs2_meta_pre_drop, inode, };
2352 /* No need to call ocfs2_mark_lockres_freeing here -
2353 * ocfs2_clear_inode has done it for us. */
2355 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2356 &OCFS2_I(inode)->ip_data_lockres,
2363 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2364 &OCFS2_I(inode)->ip_meta_lockres,
2368 if (err < 0 && !status)
2371 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2372 &OCFS2_I(inode)->ip_rw_lockres,
2376 if (err < 0 && !status)
2383 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2386 assert_spin_locked(&lockres->l_lock);
2388 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
2390 if (lockres->l_level <= new_level) {
2391 mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
2392 lockres->l_level, new_level);
2396 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2397 lockres->l_name, new_level, lockres->l_blocking);
2399 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2400 lockres->l_requested = new_level;
2401 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2404 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2405 struct ocfs2_lock_res *lockres,
2409 int ret, dlm_flags = LKM_CONVERT;
2410 enum dlm_status status;
2415 dlm_flags |= LKM_VALBLK;
2417 status = dlmlock(osb->dlm,
2422 lockres->l_ops->ast,
2424 lockres->l_ops->bast);
2425 if (status != DLM_NORMAL) {
2426 ocfs2_log_dlm_error("dlmlock", status, lockres);
2428 ocfs2_recover_from_dlm_error(lockres, 1);
2438 /* returns 1 when the caller should unlock and call dlmunlock */
2439 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2440 struct ocfs2_lock_res *lockres)
2442 assert_spin_locked(&lockres->l_lock);
2445 mlog(0, "lock %s\n", lockres->l_name);
2447 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2448 /* If we're already trying to cancel a lock conversion
2449 * then just drop the spinlock and allow the caller to
2450 * requeue this lock. */
2452 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2456 /* were we in a convert when we got the bast fire? */
2457 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2458 lockres->l_action != OCFS2_AST_DOWNCONVERT);
2459 /* set things up for the unlockast to know to just
2460 * clear out the ast_action and unset busy, etc. */
2461 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2463 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2464 "lock %s, invalid flags: 0x%lx\n",
2465 lockres->l_name, lockres->l_flags);
2470 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2471 struct ocfs2_lock_res *lockres)
2474 enum dlm_status status;
2477 mlog(0, "lock %s\n", lockres->l_name);
2480 status = dlmunlock(osb->dlm,
2483 lockres->l_ops->unlock_ast,
2485 if (status != DLM_NORMAL) {
2486 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2488 ocfs2_recover_from_dlm_error(lockres, 0);
2491 mlog(0, "lock %s return from dlmunlock\n", lockres->l_name);
2497 static inline int ocfs2_can_downconvert_meta_lock(struct inode *inode,
2498 struct ocfs2_lock_res *lockres,
2505 BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
2507 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2509 mlog(0, "lockres %s currently being refreshed -- backing "
2510 "off!\n", lockres->l_name);
2511 } else if (new_level == LKM_PRMODE)
2512 ret = !lockres->l_ex_holders &&
2513 ocfs2_inode_fully_checkpointed(inode);
2514 else /* Must be NLMODE we're converting to. */
2515 ret = !lockres->l_ro_holders && !lockres->l_ex_holders &&
2516 ocfs2_inode_fully_checkpointed(inode);
2522 static int ocfs2_do_unblock_meta(struct inode *inode,
2528 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
2529 unsigned long flags;
2531 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2535 spin_lock_irqsave(&lockres->l_lock, flags);
2537 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2539 mlog(0, "l_level=%d, l_blocking=%d\n", lockres->l_level,
2540 lockres->l_blocking);
2542 BUG_ON(lockres->l_level != LKM_EXMODE &&
2543 lockres->l_level != LKM_PRMODE);
2545 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2547 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2548 spin_unlock_irqrestore(&lockres->l_lock, flags);
2550 ret = ocfs2_cancel_convert(osb, lockres);
2557 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2559 mlog(0, "l_level=%d, l_blocking=%d, new_level=%d\n",
2560 lockres->l_level, lockres->l_blocking, new_level);
2562 if (ocfs2_can_downconvert_meta_lock(inode, lockres, new_level)) {
2563 if (lockres->l_level == LKM_EXMODE)
2566 /* If the lock hasn't been refreshed yet (rare), then
2567 * our memory inode values are old and we skip
2568 * stuffing the lvb. There's no need to actually clear
2569 * out the lvb here as it's value is still valid. */
2570 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2572 __ocfs2_stuff_meta_lvb(inode);
2574 mlog(0, "lockres %s: downconverting stale lock!\n",
2577 mlog(0, "calling ocfs2_downconvert_lock with l_level=%d, "
2578 "l_blocking=%d, new_level=%d\n",
2579 lockres->l_level, lockres->l_blocking, new_level);
2581 ocfs2_prepare_downconvert(lockres, new_level);
2582 spin_unlock_irqrestore(&lockres->l_lock, flags);
2583 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
2586 if (!ocfs2_inode_fully_checkpointed(inode))
2587 ocfs2_start_checkpoint(osb);
2590 spin_unlock_irqrestore(&lockres->l_lock, flags);
2597 static int ocfs2_generic_unblock_lock(struct ocfs2_super *osb,
2598 struct ocfs2_lock_res *lockres,
2600 ocfs2_convert_worker_t *worker)
2602 unsigned long flags;
2609 spin_lock_irqsave(&lockres->l_lock, flags);
2611 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2614 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2616 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2617 spin_unlock_irqrestore(&lockres->l_lock, flags);
2619 ret = ocfs2_cancel_convert(osb, lockres);
2626 /* if we're blocking an exclusive and we have *any* holders,
2628 if ((lockres->l_blocking == LKM_EXMODE)
2629 && (lockres->l_ex_holders || lockres->l_ro_holders)) {
2630 spin_unlock_irqrestore(&lockres->l_lock, flags);
2636 /* If it's a PR we're blocking, then only
2637 * requeue if we've got any EX holders */
2638 if (lockres->l_blocking == LKM_PRMODE &&
2639 lockres->l_ex_holders) {
2640 spin_unlock_irqrestore(&lockres->l_lock, flags);
2646 /* If we get here, then we know that there are no more
2647 * incompatible holders (and anyone asking for an incompatible
2648 * lock is blocked). We can now downconvert the lock */
2652 /* Some lockres types want to do a bit of work before
2653 * downconverting a lock. Allow that here. The worker function
2654 * may sleep, so we save off a copy of what we're blocking as
2655 * it may change while we're not holding the spin lock. */
2656 blocking = lockres->l_blocking;
2657 spin_unlock_irqrestore(&lockres->l_lock, flags);
2659 worker(lockres, blocking);
2661 spin_lock_irqsave(&lockres->l_lock, flags);
2662 if (blocking != lockres->l_blocking) {
2663 /* If this changed underneath us, then we can't drop
2670 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2672 ocfs2_prepare_downconvert(lockres, new_level);
2673 spin_unlock_irqrestore(&lockres->l_lock, flags);
2674 ret = ocfs2_downconvert_lock(osb, lockres, new_level, 0);
2680 static void ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
2683 struct inode *inode;
2684 struct address_space *mapping;
2688 inode = ocfs2_lock_res_inode(lockres);
2689 mapping = inode->i_mapping;
2691 if (filemap_fdatawrite(mapping)) {
2692 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
2693 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2695 sync_mapping_buffers(mapping);
2696 if (blocking == LKM_EXMODE) {
2697 truncate_inode_pages(mapping, 0);
2698 unmap_mapping_range(mapping, 0, 0, 0);
2700 /* We only need to wait on the I/O if we're not also
2701 * truncating pages because truncate_inode_pages waits
2702 * for us above. We don't truncate pages if we're
2703 * blocking anything < EXMODE because we want to keep
2704 * them around in that case. */
2705 filemap_fdatawait(mapping);
2711 int ocfs2_unblock_data(struct ocfs2_lock_res *lockres,
2715 struct inode *inode;
2716 struct ocfs2_super *osb;
2720 inode = ocfs2_lock_res_inode(lockres);
2721 osb = OCFS2_SB(inode->i_sb);
2723 mlog(0, "unblock inode %llu\n",
2724 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2726 status = ocfs2_generic_unblock_lock(osb,
2729 ocfs2_data_convert_worker);
2733 mlog(0, "inode %llu, requeue = %d\n",
2734 (unsigned long long)OCFS2_I(inode)->ip_blkno, *requeue);
2740 static int ocfs2_unblock_inode_lock(struct ocfs2_lock_res *lockres,
2744 struct inode *inode;
2748 mlog(0, "Unblock lockres %s\n", lockres->l_name);
2750 inode = ocfs2_lock_res_inode(lockres);
2752 status = ocfs2_generic_unblock_lock(OCFS2_SB(inode->i_sb),
2764 int ocfs2_unblock_meta(struct ocfs2_lock_res *lockres,
2768 struct inode *inode;
2772 inode = ocfs2_lock_res_inode(lockres);
2774 mlog(0, "unblock inode %llu\n",
2775 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2777 status = ocfs2_do_unblock_meta(inode, requeue);
2781 mlog(0, "inode %llu, requeue = %d\n",
2782 (unsigned long long)OCFS2_I(inode)->ip_blkno, *requeue);
2788 /* Generic unblock function for any lockres whose private data is an
2789 * ocfs2_super pointer. */
2790 static int ocfs2_unblock_osb_lock(struct ocfs2_lock_res *lockres,
2794 struct ocfs2_super *osb;
2798 mlog(0, "Unblock lockres %s\n", lockres->l_name);
2800 osb = ocfs2_lock_res_super(lockres);
2802 status = ocfs2_generic_unblock_lock(osb,
2813 void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
2814 struct ocfs2_lock_res *lockres)
2818 unsigned long flags;
2820 /* Our reference to the lockres in this function can be
2821 * considered valid until we remove the OCFS2_LOCK_QUEUED
2827 BUG_ON(!lockres->l_ops);
2828 BUG_ON(!lockres->l_ops->unblock);
2830 mlog(0, "lockres %s blocked.\n", lockres->l_name);
2832 /* Detect whether a lock has been marked as going away while
2833 * the vote thread was processing other things. A lock can
2834 * still be marked with OCFS2_LOCK_FREEING after this check,
2835 * but short circuiting here will still save us some
2837 spin_lock_irqsave(&lockres->l_lock, flags);
2838 if (lockres->l_flags & OCFS2_LOCK_FREEING)
2840 spin_unlock_irqrestore(&lockres->l_lock, flags);
2842 status = lockres->l_ops->unblock(lockres, &requeue);
2846 spin_lock_irqsave(&lockres->l_lock, flags);
2848 if (lockres->l_flags & OCFS2_LOCK_FREEING || !requeue) {
2849 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
2851 ocfs2_schedule_blocked_lock(osb, lockres);
2853 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
2854 requeue ? "yes" : "no");
2855 spin_unlock_irqrestore(&lockres->l_lock, flags);
2860 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
2861 struct ocfs2_lock_res *lockres)
2865 assert_spin_locked(&lockres->l_lock);
2867 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
2868 /* Do not schedule a lock for downconvert when it's on
2869 * the way to destruction - any nodes wanting access
2870 * to the resource will get it soon. */
2871 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
2872 lockres->l_name, lockres->l_flags);
2876 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
2878 spin_lock(&osb->vote_task_lock);
2879 if (list_empty(&lockres->l_blocked_list)) {
2880 list_add_tail(&lockres->l_blocked_list,
2881 &osb->blocked_lock_list);
2882 osb->blocked_lock_count++;
2884 spin_unlock(&osb->vote_task_lock);
2889 /* This aids in debugging situations where a bad LVB might be involved. */
2890 void ocfs2_dump_meta_lvb_info(u64 level,
2891 const char *function,
2893 struct ocfs2_lock_res *lockres)
2895 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
2897 mlog(level, "LVB information for %s (called from %s:%u):\n",
2898 lockres->l_name, function, line);
2899 mlog(level, "version: %u, clusters: %u\n",
2900 be32_to_cpu(lvb->lvb_version), be32_to_cpu(lvb->lvb_iclusters));
2901 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
2902 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
2903 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
2904 be16_to_cpu(lvb->lvb_imode));
2905 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
2906 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
2907 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
2908 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
2909 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
2910 be32_to_cpu(lvb->lvb_iattr));