1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Code which implements an OCFS2 specific interface to our DLM.
8 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
26 #include <linux/types.h>
27 #include <linux/slab.h>
28 #include <linux/highmem.h>
30 #include <linux/smp_lock.h>
31 #include <linux/crc32.h>
32 #include <linux/kthread.h>
33 #include <linux/pagemap.h>
34 #include <linux/debugfs.h>
35 #include <linux/seq_file.h>
37 #include <cluster/heartbeat.h>
38 #include <cluster/nodemanager.h>
39 #include <cluster/tcp.h>
41 #include <dlm/dlmapi.h>
43 #define MLOG_MASK_PREFIX ML_DLM_GLUE
44 #include <cluster/masklog.h>
51 #include "extent_map.h"
52 #include "heartbeat.h"
60 #include "buffer_head_io.h"
62 struct ocfs2_mask_waiter {
63 struct list_head mw_item;
65 struct completion mw_complete;
66 unsigned long mw_mask;
67 unsigned long mw_goal;
70 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
71 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
74 * Return value from ocfs2_convert_worker_t functions.
76 * These control the precise actions of ocfs2_generic_unblock_lock()
77 * and ocfs2_process_blocked_lock()
80 enum ocfs2_unblock_action {
81 UNBLOCK_CONTINUE = 0, /* Continue downconvert */
82 UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire
83 * ->post_unlock callback */
84 UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire
85 * ->post_unlock() callback. */
88 struct ocfs2_unblock_ctl {
90 enum ocfs2_unblock_action unblock_action;
93 static int ocfs2_unblock_meta(struct ocfs2_lock_res *lockres,
94 struct ocfs2_unblock_ctl *ctl);
95 static int ocfs2_unblock_data(struct ocfs2_lock_res *lockres,
96 struct ocfs2_unblock_ctl *ctl);
97 static int ocfs2_unblock_inode_lock(struct ocfs2_lock_res *lockres,
98 struct ocfs2_unblock_ctl *ctl);
99 static int ocfs2_unblock_dentry_lock(struct ocfs2_lock_res *lockres,
100 struct ocfs2_unblock_ctl *ctl);
101 static int ocfs2_unblock_osb_lock(struct ocfs2_lock_res *lockres,
102 struct ocfs2_unblock_ctl *ctl);
104 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
105 struct ocfs2_lock_res *lockres);
108 * OCFS2 Lock Resource Operations
110 * These fine tune the behavior of the generic dlmglue locking infrastructure.
112 struct ocfs2_lock_res_ops {
114 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
115 * this callback if ->l_priv is not an ocfs2_super pointer
117 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
118 int (*unblock)(struct ocfs2_lock_res *, struct ocfs2_unblock_ctl *);
119 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
122 * LOCK_TYPE_* flags which describe the specific requirements
123 * of a lock type. Descriptions of each individual flag follow.
129 * Some locks want to "refresh" potentially stale data when a
130 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
131 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
132 * individual lockres l_flags member from the ast function. It is
133 * expected that the locking wrapper will clear the
134 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
136 #define LOCK_TYPE_REQUIRES_REFRESH 0x1
139 * Indicate that a lock type makes use of the lock value block.
141 #define LOCK_TYPE_USES_LVB 0x2
143 typedef int (ocfs2_convert_worker_t)(struct ocfs2_lock_res *, int);
144 static int ocfs2_generic_unblock_lock(struct ocfs2_super *osb,
145 struct ocfs2_lock_res *lockres,
146 struct ocfs2_unblock_ctl *ctl,
147 ocfs2_convert_worker_t *worker);
149 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
150 .get_osb = ocfs2_get_inode_osb,
151 .unblock = ocfs2_unblock_inode_lock,
155 static struct ocfs2_lock_res_ops ocfs2_inode_meta_lops = {
156 .get_osb = ocfs2_get_inode_osb,
157 .unblock = ocfs2_unblock_meta,
158 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
161 static struct ocfs2_lock_res_ops ocfs2_inode_data_lops = {
162 .get_osb = ocfs2_get_inode_osb,
163 .unblock = ocfs2_unblock_data,
167 static struct ocfs2_lock_res_ops ocfs2_super_lops = {
168 .unblock = ocfs2_unblock_osb_lock,
169 .flags = LOCK_TYPE_REQUIRES_REFRESH,
172 static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
173 .unblock = ocfs2_unblock_osb_lock,
177 static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
178 .get_osb = ocfs2_get_dentry_osb,
179 .unblock = ocfs2_unblock_dentry_lock,
180 .post_unlock = ocfs2_dentry_post_unlock,
184 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
186 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
187 lockres->l_type == OCFS2_LOCK_TYPE_DATA ||
188 lockres->l_type == OCFS2_LOCK_TYPE_RW;
191 static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
193 BUG_ON(!ocfs2_is_inode_lock(lockres));
195 return (struct inode *) lockres->l_priv;
198 static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
200 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
202 return (struct ocfs2_dentry_lock *)lockres->l_priv;
205 static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
207 if (lockres->l_ops->get_osb)
208 return lockres->l_ops->get_osb(lockres);
210 return (struct ocfs2_super *)lockres->l_priv;
213 static int ocfs2_lock_create(struct ocfs2_super *osb,
214 struct ocfs2_lock_res *lockres,
217 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
219 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
220 struct ocfs2_lock_res *lockres,
222 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
223 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
224 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
225 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
226 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
227 struct ocfs2_lock_res *lockres);
228 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
230 #define ocfs2_log_dlm_error(_func, _stat, _lockres) do { \
231 mlog(ML_ERROR, "Dlm error \"%s\" while calling %s on " \
232 "resource %s: %s\n", dlm_errname(_stat), _func, \
233 _lockres->l_name, dlm_errmsg(_stat)); \
235 static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
236 struct ocfs2_lock_res *lockres);
237 static int ocfs2_meta_lock_update(struct inode *inode,
238 struct buffer_head **bh);
239 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
240 static inline int ocfs2_highest_compat_lock_level(int level);
241 static inline int ocfs2_can_downconvert_meta_lock(struct inode *inode,
242 struct ocfs2_lock_res *lockres,
245 static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
254 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
256 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
257 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
258 (long long)blkno, generation);
260 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
262 mlog(0, "built lock resource with name: %s\n", name);
267 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
269 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
270 struct ocfs2_dlm_debug *dlm_debug)
272 mlog(0, "Add tracking for lockres %s\n", res->l_name);
274 spin_lock(&ocfs2_dlm_tracking_lock);
275 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
276 spin_unlock(&ocfs2_dlm_tracking_lock);
279 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
281 spin_lock(&ocfs2_dlm_tracking_lock);
282 if (!list_empty(&res->l_debug_list))
283 list_del_init(&res->l_debug_list);
284 spin_unlock(&ocfs2_dlm_tracking_lock);
287 static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
288 struct ocfs2_lock_res *res,
289 enum ocfs2_lock_type type,
290 struct ocfs2_lock_res_ops *ops,
297 res->l_level = LKM_IVMODE;
298 res->l_requested = LKM_IVMODE;
299 res->l_blocking = LKM_IVMODE;
300 res->l_action = OCFS2_AST_INVALID;
301 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
303 res->l_flags = OCFS2_LOCK_INITIALIZED;
305 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
308 void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
310 /* This also clears out the lock status block */
311 memset(res, 0, sizeof(struct ocfs2_lock_res));
312 spin_lock_init(&res->l_lock);
313 init_waitqueue_head(&res->l_event);
314 INIT_LIST_HEAD(&res->l_blocked_list);
315 INIT_LIST_HEAD(&res->l_mask_waiters);
318 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
319 enum ocfs2_lock_type type,
320 unsigned int generation,
323 struct ocfs2_lock_res_ops *ops;
326 case OCFS2_LOCK_TYPE_RW:
327 ops = &ocfs2_inode_rw_lops;
329 case OCFS2_LOCK_TYPE_META:
330 ops = &ocfs2_inode_meta_lops;
332 case OCFS2_LOCK_TYPE_DATA:
333 ops = &ocfs2_inode_data_lops;
336 mlog_bug_on_msg(1, "type: %d\n", type);
337 ops = NULL; /* thanks, gcc */
341 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
342 generation, res->l_name);
343 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
346 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
348 struct inode *inode = ocfs2_lock_res_inode(lockres);
350 return OCFS2_SB(inode->i_sb);
353 static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
355 __be64 inode_blkno_be;
357 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
360 return be64_to_cpu(inode_blkno_be);
363 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
365 struct ocfs2_dentry_lock *dl = lockres->l_priv;
367 return OCFS2_SB(dl->dl_inode->i_sb);
370 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
371 u64 parent, struct inode *inode)
374 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
375 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
376 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
378 ocfs2_lock_res_init_once(lockres);
381 * Unfortunately, the standard lock naming scheme won't work
382 * here because we have two 16 byte values to use. Instead,
383 * we'll stuff the inode number as a binary value. We still
384 * want error prints to show something without garbling the
385 * display, so drop a null byte in there before the inode
386 * number. A future version of OCFS2 will likely use all
387 * binary lock names. The stringified names have been a
388 * tremendous aid in debugging, but now that the debugfs
389 * interface exists, we can mangle things there if need be.
391 * NOTE: We also drop the standard "pad" value (the total lock
392 * name size stays the same though - the last part is all
393 * zeros due to the memset in ocfs2_lock_res_init_once()
395 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
397 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
400 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
402 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
405 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
406 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
410 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
411 struct ocfs2_super *osb)
413 /* Superblock lockres doesn't come from a slab so we call init
414 * once on it manually. */
415 ocfs2_lock_res_init_once(res);
416 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
418 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
419 &ocfs2_super_lops, osb);
422 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
423 struct ocfs2_super *osb)
425 /* Rename lockres doesn't come from a slab so we call init
426 * once on it manually. */
427 ocfs2_lock_res_init_once(res);
428 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
429 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
430 &ocfs2_rename_lops, osb);
433 void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
437 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
440 ocfs2_remove_lockres_tracking(res);
442 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
443 "Lockres %s is on the blocked list\n",
445 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
446 "Lockres %s has mask waiters pending\n",
448 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
449 "Lockres %s is locked\n",
451 mlog_bug_on_msg(res->l_ro_holders,
452 "Lockres %s has %u ro holders\n",
453 res->l_name, res->l_ro_holders);
454 mlog_bug_on_msg(res->l_ex_holders,
455 "Lockres %s has %u ex holders\n",
456 res->l_name, res->l_ex_holders);
458 /* Need to clear out the lock status block for the dlm */
459 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
465 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
474 lockres->l_ex_holders++;
477 lockres->l_ro_holders++;
486 static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
495 BUG_ON(!lockres->l_ex_holders);
496 lockres->l_ex_holders--;
499 BUG_ON(!lockres->l_ro_holders);
500 lockres->l_ro_holders--;
508 /* WARNING: This function lives in a world where the only three lock
509 * levels are EX, PR, and NL. It *will* have to be adjusted when more
510 * lock types are added. */
511 static inline int ocfs2_highest_compat_lock_level(int level)
513 int new_level = LKM_EXMODE;
515 if (level == LKM_EXMODE)
516 new_level = LKM_NLMODE;
517 else if (level == LKM_PRMODE)
518 new_level = LKM_PRMODE;
522 static void lockres_set_flags(struct ocfs2_lock_res *lockres,
523 unsigned long newflags)
525 struct list_head *pos, *tmp;
526 struct ocfs2_mask_waiter *mw;
528 assert_spin_locked(&lockres->l_lock);
530 lockres->l_flags = newflags;
532 list_for_each_safe(pos, tmp, &lockres->l_mask_waiters) {
533 mw = list_entry(pos, struct ocfs2_mask_waiter, mw_item);
534 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
537 list_del_init(&mw->mw_item);
539 complete(&mw->mw_complete);
542 static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
544 lockres_set_flags(lockres, lockres->l_flags | or);
546 static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
549 lockres_set_flags(lockres, lockres->l_flags & ~clear);
552 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
556 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
557 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
558 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
559 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
561 lockres->l_level = lockres->l_requested;
562 if (lockres->l_level <=
563 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
564 lockres->l_blocking = LKM_NLMODE;
565 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
567 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
572 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
576 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
577 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
579 /* Convert from RO to EX doesn't really need anything as our
580 * information is already up to data. Convert from NL to
581 * *anything* however should mark ourselves as needing an
583 if (lockres->l_level == LKM_NLMODE &&
584 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
585 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
587 lockres->l_level = lockres->l_requested;
588 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
593 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
597 BUG_ON((!lockres->l_flags & OCFS2_LOCK_BUSY));
598 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
600 if (lockres->l_requested > LKM_NLMODE &&
601 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
602 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
603 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
605 lockres->l_level = lockres->l_requested;
606 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
607 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
612 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
615 int needs_downconvert = 0;
618 assert_spin_locked(&lockres->l_lock);
620 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
622 if (level > lockres->l_blocking) {
623 /* only schedule a downconvert if we haven't already scheduled
624 * one that goes low enough to satisfy the level we're
625 * blocking. this also catches the case where we get
627 if (ocfs2_highest_compat_lock_level(level) <
628 ocfs2_highest_compat_lock_level(lockres->l_blocking))
629 needs_downconvert = 1;
631 lockres->l_blocking = level;
634 mlog_exit(needs_downconvert);
635 return needs_downconvert;
638 static void ocfs2_blocking_ast(void *opaque, int level)
640 struct ocfs2_lock_res *lockres = opaque;
641 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
642 int needs_downconvert;
645 BUG_ON(level <= LKM_NLMODE);
647 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
648 lockres->l_name, level, lockres->l_level,
649 ocfs2_lock_type_string(lockres->l_type));
651 spin_lock_irqsave(&lockres->l_lock, flags);
652 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
653 if (needs_downconvert)
654 ocfs2_schedule_blocked_lock(osb, lockres);
655 spin_unlock_irqrestore(&lockres->l_lock, flags);
657 wake_up(&lockres->l_event);
659 ocfs2_kick_vote_thread(osb);
662 static void ocfs2_locking_ast(void *opaque)
664 struct ocfs2_lock_res *lockres = opaque;
665 struct dlm_lockstatus *lksb = &lockres->l_lksb;
668 spin_lock_irqsave(&lockres->l_lock, flags);
670 if (lksb->status != DLM_NORMAL) {
671 mlog(ML_ERROR, "lockres %s: lksb status value of %u!\n",
672 lockres->l_name, lksb->status);
673 spin_unlock_irqrestore(&lockres->l_lock, flags);
677 switch(lockres->l_action) {
678 case OCFS2_AST_ATTACH:
679 ocfs2_generic_handle_attach_action(lockres);
680 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
682 case OCFS2_AST_CONVERT:
683 ocfs2_generic_handle_convert_action(lockres);
685 case OCFS2_AST_DOWNCONVERT:
686 ocfs2_generic_handle_downconvert_action(lockres);
689 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
690 "lockres flags = 0x%lx, unlock action: %u\n",
691 lockres->l_name, lockres->l_action, lockres->l_flags,
692 lockres->l_unlock_action);
696 /* set it to something invalid so if we get called again we
698 lockres->l_action = OCFS2_AST_INVALID;
700 wake_up(&lockres->l_event);
701 spin_unlock_irqrestore(&lockres->l_lock, flags);
704 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
710 spin_lock_irqsave(&lockres->l_lock, flags);
711 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
713 lockres->l_action = OCFS2_AST_INVALID;
715 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
716 spin_unlock_irqrestore(&lockres->l_lock, flags);
718 wake_up(&lockres->l_event);
722 /* Note: If we detect another process working on the lock (i.e.,
723 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
724 * to do the right thing in that case.
726 static int ocfs2_lock_create(struct ocfs2_super *osb,
727 struct ocfs2_lock_res *lockres,
732 enum dlm_status status;
737 mlog(0, "lock %s, level = %d, flags = %d\n", lockres->l_name, level,
740 spin_lock_irqsave(&lockres->l_lock, flags);
741 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
742 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
743 spin_unlock_irqrestore(&lockres->l_lock, flags);
747 lockres->l_action = OCFS2_AST_ATTACH;
748 lockres->l_requested = level;
749 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
750 spin_unlock_irqrestore(&lockres->l_lock, flags);
752 status = dlmlock(osb->dlm,
757 OCFS2_LOCK_ID_MAX_LEN - 1,
761 if (status != DLM_NORMAL) {
762 ocfs2_log_dlm_error("dlmlock", status, lockres);
764 ocfs2_recover_from_dlm_error(lockres, 1);
767 mlog(0, "lock %s, successfull return from dlmlock\n", lockres->l_name);
774 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
780 spin_lock_irqsave(&lockres->l_lock, flags);
781 ret = lockres->l_flags & flag;
782 spin_unlock_irqrestore(&lockres->l_lock, flags);
787 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
790 wait_event(lockres->l_event,
791 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
794 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
797 wait_event(lockres->l_event,
798 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
801 /* predict what lock level we'll be dropping down to on behalf
802 * of another node, and return true if the currently wanted
803 * level will be compatible with it. */
804 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
807 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
809 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
812 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
814 INIT_LIST_HEAD(&mw->mw_item);
815 init_completion(&mw->mw_complete);
818 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
820 wait_for_completion(&mw->mw_complete);
821 /* Re-arm the completion in case we want to wait on it again */
822 INIT_COMPLETION(mw->mw_complete);
823 return mw->mw_status;
826 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
827 struct ocfs2_mask_waiter *mw,
831 BUG_ON(!list_empty(&mw->mw_item));
833 assert_spin_locked(&lockres->l_lock);
835 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
840 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
841 * if the mask still hadn't reached its goal */
842 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
843 struct ocfs2_mask_waiter *mw)
848 spin_lock_irqsave(&lockres->l_lock, flags);
849 if (!list_empty(&mw->mw_item)) {
850 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
853 list_del_init(&mw->mw_item);
854 init_completion(&mw->mw_complete);
856 spin_unlock_irqrestore(&lockres->l_lock, flags);
862 static int ocfs2_cluster_lock(struct ocfs2_super *osb,
863 struct ocfs2_lock_res *lockres,
868 struct ocfs2_mask_waiter mw;
869 enum dlm_status status;
870 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
871 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
876 ocfs2_init_mask_waiter(&mw);
878 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
879 lkm_flags |= LKM_VALBLK;
884 if (catch_signals && signal_pending(current)) {
889 spin_lock_irqsave(&lockres->l_lock, flags);
891 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
892 "Cluster lock called on freeing lockres %s! flags "
893 "0x%lx\n", lockres->l_name, lockres->l_flags);
895 /* We only compare against the currently granted level
896 * here. If the lock is blocked waiting on a downconvert,
897 * we'll get caught below. */
898 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
899 level > lockres->l_level) {
900 /* is someone sitting in dlm_lock? If so, wait on
902 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
907 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
908 /* lock has not been created yet. */
909 spin_unlock_irqrestore(&lockres->l_lock, flags);
911 ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
919 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
920 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
921 /* is the lock is currently blocked on behalf of
923 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
928 if (level > lockres->l_level) {
929 if (lockres->l_action != OCFS2_AST_INVALID)
930 mlog(ML_ERROR, "lockres %s has action %u pending\n",
931 lockres->l_name, lockres->l_action);
933 lockres->l_action = OCFS2_AST_CONVERT;
934 lockres->l_requested = level;
935 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
936 spin_unlock_irqrestore(&lockres->l_lock, flags);
938 BUG_ON(level == LKM_IVMODE);
939 BUG_ON(level == LKM_NLMODE);
941 mlog(0, "lock %s, convert from %d to level = %d\n",
942 lockres->l_name, lockres->l_level, level);
944 /* call dlm_lock to upgrade lock now */
945 status = dlmlock(osb->dlm,
948 lkm_flags|LKM_CONVERT,
950 OCFS2_LOCK_ID_MAX_LEN - 1,
954 if (status != DLM_NORMAL) {
955 if ((lkm_flags & LKM_NOQUEUE) &&
956 (status == DLM_NOTQUEUED))
959 ocfs2_log_dlm_error("dlmlock", status,
963 ocfs2_recover_from_dlm_error(lockres, 1);
967 mlog(0, "lock %s, successfull return from dlmlock\n",
970 /* At this point we've gone inside the dlm and need to
971 * complete our work regardless. */
974 /* wait for busy to clear and carry on */
978 /* Ok, if we get here then we're good to go. */
979 ocfs2_inc_holders(lockres, level);
983 spin_unlock_irqrestore(&lockres->l_lock, flags);
986 * This is helping work around a lock inversion between the page lock
987 * and dlm locks. One path holds the page lock while calling aops
988 * which block acquiring dlm locks. The voting thread holds dlm
989 * locks while acquiring page locks while down converting data locks.
990 * This block is helping an aop path notice the inversion and back
991 * off to unlock its page lock before trying the dlm lock again.
993 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
994 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
996 if (lockres_remove_mask_waiter(lockres, &mw))
1002 ret = ocfs2_wait_for_mask(&mw);
1012 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1013 struct ocfs2_lock_res *lockres,
1016 unsigned long flags;
1019 spin_lock_irqsave(&lockres->l_lock, flags);
1020 ocfs2_dec_holders(lockres, level);
1021 ocfs2_vote_on_unlock(osb, lockres);
1022 spin_unlock_irqrestore(&lockres->l_lock, flags);
1026 int ocfs2_create_new_lock(struct ocfs2_super *osb,
1027 struct ocfs2_lock_res *lockres,
1031 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1032 unsigned long flags;
1033 int lkm_flags = local ? LKM_LOCAL : 0;
1035 spin_lock_irqsave(&lockres->l_lock, flags);
1036 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1037 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1038 spin_unlock_irqrestore(&lockres->l_lock, flags);
1040 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
1043 /* Grants us an EX lock on the data and metadata resources, skipping
1044 * the normal cluster directory lookup. Use this ONLY on newly created
1045 * inodes which other nodes can't possibly see, and which haven't been
1046 * hashed in the inode hash yet. This can give us a good performance
1047 * increase as it'll skip the network broadcast normally associated
1048 * with creating a new lock resource. */
1049 int ocfs2_create_new_inode_locks(struct inode *inode)
1052 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1055 BUG_ON(!ocfs2_inode_is_new(inode));
1059 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1061 /* NOTE: That we don't increment any of the holder counts, nor
1062 * do we add anything to a journal handle. Since this is
1063 * supposed to be a new inode which the cluster doesn't know
1064 * about yet, there is no need to. As far as the LVB handling
1065 * is concerned, this is basically like acquiring an EX lock
1066 * on a resource which has an invalid one -- we'll set it
1067 * valid when we release the EX. */
1069 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
1076 * We don't want to use LKM_LOCAL on a meta data lock as they
1077 * don't use a generation in their lock names.
1079 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_meta_lockres, 1, 0);
1085 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_data_lockres, 1, 1);
1096 int ocfs2_rw_lock(struct inode *inode, int write)
1099 struct ocfs2_lock_res *lockres;
1105 mlog(0, "inode %llu take %s RW lock\n",
1106 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1107 write ? "EXMODE" : "PRMODE");
1109 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1111 level = write ? LKM_EXMODE : LKM_PRMODE;
1113 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1122 void ocfs2_rw_unlock(struct inode *inode, int write)
1124 int level = write ? LKM_EXMODE : LKM_PRMODE;
1125 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1129 mlog(0, "inode %llu drop %s RW lock\n",
1130 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1131 write ? "EXMODE" : "PRMODE");
1133 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1138 int ocfs2_data_lock_full(struct inode *inode,
1142 int status = 0, level;
1143 struct ocfs2_lock_res *lockres;
1149 mlog(0, "inode %llu take %s DATA lock\n",
1150 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1151 write ? "EXMODE" : "PRMODE");
1153 /* We'll allow faking a readonly data lock for
1155 if (ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb))) {
1163 lockres = &OCFS2_I(inode)->ip_data_lockres;
1165 level = write ? LKM_EXMODE : LKM_PRMODE;
1167 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level,
1169 if (status < 0 && status != -EAGAIN)
1177 /* see ocfs2_meta_lock_with_page() */
1178 int ocfs2_data_lock_with_page(struct inode *inode,
1184 ret = ocfs2_data_lock_full(inode, write, OCFS2_LOCK_NONBLOCK);
1185 if (ret == -EAGAIN) {
1187 if (ocfs2_data_lock(inode, write) == 0)
1188 ocfs2_data_unlock(inode, write);
1189 ret = AOP_TRUNCATED_PAGE;
1195 static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
1196 struct ocfs2_lock_res *lockres)
1202 /* If we know that another node is waiting on our lock, kick
1203 * the vote thread * pre-emptively when we reach a release
1205 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1206 switch(lockres->l_blocking) {
1208 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1212 if (!lockres->l_ex_holders)
1221 ocfs2_kick_vote_thread(osb);
1226 void ocfs2_data_unlock(struct inode *inode,
1229 int level = write ? LKM_EXMODE : LKM_PRMODE;
1230 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_data_lockres;
1234 mlog(0, "inode %llu drop %s DATA lock\n",
1235 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1236 write ? "EXMODE" : "PRMODE");
1238 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1239 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1244 #define OCFS2_SEC_BITS 34
1245 #define OCFS2_SEC_SHIFT (64 - 34)
1246 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1248 /* LVB only has room for 64 bits of time here so we pack it for
1250 static u64 ocfs2_pack_timespec(struct timespec *spec)
1253 u64 sec = spec->tv_sec;
1254 u32 nsec = spec->tv_nsec;
1256 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1261 /* Call this with the lockres locked. I am reasonably sure we don't
1262 * need ip_lock in this function as anyone who would be changing those
1263 * values is supposed to be blocked in ocfs2_meta_lock right now. */
1264 static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1266 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1267 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1268 struct ocfs2_meta_lvb *lvb;
1272 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1275 * Invalidate the LVB of a deleted inode - this way other
1276 * nodes are forced to go to disk and discover the new inode
1279 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1280 lvb->lvb_version = 0;
1284 lvb->lvb_version = OCFS2_LVB_VERSION;
1285 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1286 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1287 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1288 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1289 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1290 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1291 lvb->lvb_iatime_packed =
1292 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1293 lvb->lvb_ictime_packed =
1294 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1295 lvb->lvb_imtime_packed =
1296 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1297 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
1298 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
1301 mlog_meta_lvb(0, lockres);
1306 static void ocfs2_unpack_timespec(struct timespec *spec,
1309 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1310 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1313 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1315 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1316 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1317 struct ocfs2_meta_lvb *lvb;
1321 mlog_meta_lvb(0, lockres);
1323 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1325 /* We're safe here without the lockres lock... */
1326 spin_lock(&oi->ip_lock);
1327 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1328 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1330 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1331 ocfs2_set_inode_flags(inode);
1333 /* fast-symlinks are a special case */
1334 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1335 inode->i_blocks = 0;
1338 ocfs2_align_bytes_to_sectors(i_size_read(inode));
1340 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1341 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1342 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1343 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1344 ocfs2_unpack_timespec(&inode->i_atime,
1345 be64_to_cpu(lvb->lvb_iatime_packed));
1346 ocfs2_unpack_timespec(&inode->i_mtime,
1347 be64_to_cpu(lvb->lvb_imtime_packed));
1348 ocfs2_unpack_timespec(&inode->i_ctime,
1349 be64_to_cpu(lvb->lvb_ictime_packed));
1350 spin_unlock(&oi->ip_lock);
1355 static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1356 struct ocfs2_lock_res *lockres)
1358 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1360 if (lvb->lvb_version == OCFS2_LVB_VERSION
1361 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
1366 /* Determine whether a lock resource needs to be refreshed, and
1367 * arbitrate who gets to refresh it.
1369 * 0 means no refresh needed.
1371 * > 0 means you need to refresh this and you MUST call
1372 * ocfs2_complete_lock_res_refresh afterwards. */
1373 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1375 unsigned long flags;
1381 spin_lock_irqsave(&lockres->l_lock, flags);
1382 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1383 spin_unlock_irqrestore(&lockres->l_lock, flags);
1387 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1388 spin_unlock_irqrestore(&lockres->l_lock, flags);
1390 ocfs2_wait_on_refreshing_lock(lockres);
1394 /* Ok, I'll be the one to refresh this lock. */
1395 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1396 spin_unlock_irqrestore(&lockres->l_lock, flags);
1404 /* If status is non zero, I'll mark it as not being in refresh
1405 * anymroe, but i won't clear the needs refresh flag. */
1406 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1409 unsigned long flags;
1412 spin_lock_irqsave(&lockres->l_lock, flags);
1413 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1415 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1416 spin_unlock_irqrestore(&lockres->l_lock, flags);
1418 wake_up(&lockres->l_event);
1423 /* may or may not return a bh if it went to disk. */
1424 static int ocfs2_meta_lock_update(struct inode *inode,
1425 struct buffer_head **bh)
1428 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1429 struct ocfs2_lock_res *lockres;
1430 struct ocfs2_dinode *fe;
1434 spin_lock(&oi->ip_lock);
1435 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1436 mlog(0, "Orphaned inode %llu was deleted while we "
1437 "were waiting on a lock. ip_flags = 0x%x\n",
1438 (unsigned long long)oi->ip_blkno, oi->ip_flags);
1439 spin_unlock(&oi->ip_lock);
1443 spin_unlock(&oi->ip_lock);
1445 lockres = &oi->ip_meta_lockres;
1447 if (!ocfs2_should_refresh_lock_res(lockres))
1450 /* This will discard any caching information we might have had
1451 * for the inode metadata. */
1452 ocfs2_metadata_cache_purge(inode);
1454 /* will do nothing for inode types that don't use the extent
1455 * map (directories, bitmap files, etc) */
1456 ocfs2_extent_map_trunc(inode, 0);
1458 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
1459 mlog(0, "Trusting LVB on inode %llu\n",
1460 (unsigned long long)oi->ip_blkno);
1461 ocfs2_refresh_inode_from_lvb(inode);
1463 /* Boo, we have to go to disk. */
1464 /* read bh, cast, ocfs2_refresh_inode */
1465 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1466 bh, OCFS2_BH_CACHED, inode);
1471 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1473 /* This is a good chance to make sure we're not
1474 * locking an invalid object.
1476 * We bug on a stale inode here because we checked
1477 * above whether it was wiped from disk. The wiping
1478 * node provides a guarantee that we receive that
1479 * message and can mark the inode before dropping any
1480 * locks associated with it. */
1481 if (!OCFS2_IS_VALID_DINODE(fe)) {
1482 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1486 mlog_bug_on_msg(inode->i_generation !=
1487 le32_to_cpu(fe->i_generation),
1488 "Invalid dinode %llu disk generation: %u "
1489 "inode->i_generation: %u\n",
1490 (unsigned long long)oi->ip_blkno,
1491 le32_to_cpu(fe->i_generation),
1492 inode->i_generation);
1493 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1494 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
1495 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1496 (unsigned long long)oi->ip_blkno,
1497 (unsigned long long)le64_to_cpu(fe->i_dtime),
1498 le32_to_cpu(fe->i_flags));
1500 ocfs2_refresh_inode(inode, fe);
1505 ocfs2_complete_lock_res_refresh(lockres, status);
1511 static int ocfs2_assign_bh(struct inode *inode,
1512 struct buffer_head **ret_bh,
1513 struct buffer_head *passed_bh)
1518 /* Ok, the update went to disk for us, use the
1520 *ret_bh = passed_bh;
1526 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1527 OCFS2_I(inode)->ip_blkno,
1538 * returns < 0 error if the callback will never be called, otherwise
1539 * the result of the lock will be communicated via the callback.
1541 int ocfs2_meta_lock_full(struct inode *inode,
1542 struct ocfs2_journal_handle *handle,
1543 struct buffer_head **ret_bh,
1547 int status, level, dlm_flags, acquired;
1548 struct ocfs2_lock_res *lockres;
1549 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1550 struct buffer_head *local_bh = NULL;
1556 mlog(0, "inode %llu, take %s META lock\n",
1557 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1558 ex ? "EXMODE" : "PRMODE");
1562 /* We'll allow faking a readonly metadata lock for
1564 if (ocfs2_is_hard_readonly(osb)) {
1570 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1571 wait_event(osb->recovery_event,
1572 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1575 lockres = &OCFS2_I(inode)->ip_meta_lockres;
1576 level = ex ? LKM_EXMODE : LKM_PRMODE;
1578 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
1579 dlm_flags |= LKM_NOQUEUE;
1581 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1583 if (status != -EAGAIN && status != -EIOCBRETRY)
1588 /* Notify the error cleanup path to drop the cluster lock. */
1591 /* We wait twice because a node may have died while we were in
1592 * the lower dlm layers. The second time though, we've
1593 * committed to owning this lock so we don't allow signals to
1594 * abort the operation. */
1595 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1596 wait_event(osb->recovery_event,
1597 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1600 * We only see this flag if we're being called from
1601 * ocfs2_read_locked_inode(). It means we're locking an inode
1602 * which hasn't been populated yet, so clear the refresh flag
1603 * and let the caller handle it.
1605 if (inode->i_state & I_NEW) {
1607 ocfs2_complete_lock_res_refresh(lockres, 0);
1611 /* This is fun. The caller may want a bh back, or it may
1612 * not. ocfs2_meta_lock_update definitely wants one in, but
1613 * may or may not read one, depending on what's in the
1614 * LVB. The result of all of this is that we've *only* gone to
1615 * disk if we have to, so the complexity is worthwhile. */
1616 status = ocfs2_meta_lock_update(inode, &local_bh);
1618 if (status != -ENOENT)
1624 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
1632 status = ocfs2_handle_add_lock(handle, inode);
1639 if (ret_bh && (*ret_bh)) {
1644 ocfs2_meta_unlock(inode, ex);
1655 * This is working around a lock inversion between tasks acquiring DLM locks
1656 * while holding a page lock and the vote thread which blocks dlm lock acquiry
1657 * while acquiring page locks.
1659 * ** These _with_page variantes are only intended to be called from aop
1660 * methods that hold page locks and return a very specific *positive* error
1661 * code that aop methods pass up to the VFS -- test for errors with != 0. **
1663 * The DLM is called such that it returns -EAGAIN if it would have blocked
1664 * waiting for the vote thread. In that case we unlock our page so the vote
1665 * thread can make progress. Once we've done this we have to return
1666 * AOP_TRUNCATED_PAGE so the aop method that called us can bubble that back up
1667 * into the VFS who will then immediately retry the aop call.
1669 * We do a blocking lock and immediate unlock before returning, though, so that
1670 * the lock has a great chance of being cached on this node by the time the VFS
1671 * calls back to retry the aop. This has a potential to livelock as nodes
1672 * ping locks back and forth, but that's a risk we're willing to take to avoid
1673 * the lock inversion simply.
1675 int ocfs2_meta_lock_with_page(struct inode *inode,
1676 struct ocfs2_journal_handle *handle,
1677 struct buffer_head **ret_bh,
1683 ret = ocfs2_meta_lock_full(inode, handle, ret_bh, ex,
1684 OCFS2_LOCK_NONBLOCK);
1685 if (ret == -EAGAIN) {
1687 if (ocfs2_meta_lock(inode, handle, ret_bh, ex) == 0)
1688 ocfs2_meta_unlock(inode, ex);
1689 ret = AOP_TRUNCATED_PAGE;
1695 void ocfs2_meta_unlock(struct inode *inode,
1698 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1699 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
1703 mlog(0, "inode %llu drop %s META lock\n",
1704 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1705 ex ? "EXMODE" : "PRMODE");
1707 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1708 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1713 int ocfs2_super_lock(struct ocfs2_super *osb,
1717 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1718 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1719 struct buffer_head *bh;
1720 struct ocfs2_slot_info *si = osb->slot_info;
1724 if (ocfs2_is_hard_readonly(osb))
1727 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
1733 /* The super block lock path is really in the best position to
1734 * know when resources covered by the lock need to be
1735 * refreshed, so we do it here. Of course, making sense of
1736 * everything is up to the caller :) */
1737 status = ocfs2_should_refresh_lock_res(lockres);
1744 status = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0,
1747 ocfs2_update_slot_info(si);
1749 ocfs2_complete_lock_res_refresh(lockres, status);
1759 void ocfs2_super_unlock(struct ocfs2_super *osb,
1762 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1763 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1765 ocfs2_cluster_unlock(osb, lockres, level);
1768 int ocfs2_rename_lock(struct ocfs2_super *osb)
1771 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1773 if (ocfs2_is_hard_readonly(osb))
1776 status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
1783 void ocfs2_rename_unlock(struct ocfs2_super *osb)
1785 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1787 ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
1790 int ocfs2_dentry_lock(struct dentry *dentry, int ex)
1793 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1794 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1795 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1799 if (ocfs2_is_hard_readonly(osb))
1802 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
1809 void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
1811 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1812 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1813 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1815 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
1818 /* Reference counting of the dlm debug structure. We want this because
1819 * open references on the debug inodes can live on after a mount, so
1820 * we can't rely on the ocfs2_super to always exist. */
1821 static void ocfs2_dlm_debug_free(struct kref *kref)
1823 struct ocfs2_dlm_debug *dlm_debug;
1825 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
1830 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
1833 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
1836 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
1838 kref_get(&debug->d_refcnt);
1841 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
1843 struct ocfs2_dlm_debug *dlm_debug;
1845 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
1847 mlog_errno(-ENOMEM);
1851 kref_init(&dlm_debug->d_refcnt);
1852 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
1853 dlm_debug->d_locking_state = NULL;
1858 /* Access to this is arbitrated for us via seq_file->sem. */
1859 struct ocfs2_dlm_seq_priv {
1860 struct ocfs2_dlm_debug *p_dlm_debug;
1861 struct ocfs2_lock_res p_iter_res;
1862 struct ocfs2_lock_res p_tmp_res;
1865 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
1866 struct ocfs2_dlm_seq_priv *priv)
1868 struct ocfs2_lock_res *iter, *ret = NULL;
1869 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
1871 assert_spin_locked(&ocfs2_dlm_tracking_lock);
1873 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
1874 /* discover the head of the list */
1875 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
1876 mlog(0, "End of list found, %p\n", ret);
1880 /* We track our "dummy" iteration lockres' by a NULL
1882 if (iter->l_ops != NULL) {
1891 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
1893 struct ocfs2_dlm_seq_priv *priv = m->private;
1894 struct ocfs2_lock_res *iter;
1896 spin_lock(&ocfs2_dlm_tracking_lock);
1897 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
1899 /* Since lockres' have the lifetime of their container
1900 * (which can be inodes, ocfs2_supers, etc) we want to
1901 * copy this out to a temporary lockres while still
1902 * under the spinlock. Obviously after this we can't
1903 * trust any pointers on the copy returned, but that's
1904 * ok as the information we want isn't typically held
1906 priv->p_tmp_res = *iter;
1907 iter = &priv->p_tmp_res;
1909 spin_unlock(&ocfs2_dlm_tracking_lock);
1914 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
1918 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
1920 struct ocfs2_dlm_seq_priv *priv = m->private;
1921 struct ocfs2_lock_res *iter = v;
1922 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
1924 spin_lock(&ocfs2_dlm_tracking_lock);
1925 iter = ocfs2_dlm_next_res(iter, priv);
1926 list_del_init(&dummy->l_debug_list);
1928 list_add(&dummy->l_debug_list, &iter->l_debug_list);
1929 priv->p_tmp_res = *iter;
1930 iter = &priv->p_tmp_res;
1932 spin_unlock(&ocfs2_dlm_tracking_lock);
1937 /* So that debugfs.ocfs2 can determine which format is being used */
1938 #define OCFS2_DLM_DEBUG_STR_VERSION 1
1939 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
1943 struct ocfs2_lock_res *lockres = v;
1948 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
1950 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
1951 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
1953 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
1955 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
1957 seq_printf(m, "%d\t"
1968 lockres->l_unlock_action,
1969 lockres->l_ro_holders,
1970 lockres->l_ex_holders,
1971 lockres->l_requested,
1972 lockres->l_blocking);
1974 /* Dump the raw LVB */
1975 lvb = lockres->l_lksb.lvb;
1976 for(i = 0; i < DLM_LVB_LEN; i++)
1977 seq_printf(m, "0x%x\t", lvb[i]);
1980 seq_printf(m, "\n");
1984 static struct seq_operations ocfs2_dlm_seq_ops = {
1985 .start = ocfs2_dlm_seq_start,
1986 .stop = ocfs2_dlm_seq_stop,
1987 .next = ocfs2_dlm_seq_next,
1988 .show = ocfs2_dlm_seq_show,
1991 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
1993 struct seq_file *seq = (struct seq_file *) file->private_data;
1994 struct ocfs2_dlm_seq_priv *priv = seq->private;
1995 struct ocfs2_lock_res *res = &priv->p_iter_res;
1997 ocfs2_remove_lockres_tracking(res);
1998 ocfs2_put_dlm_debug(priv->p_dlm_debug);
1999 return seq_release_private(inode, file);
2002 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2005 struct ocfs2_dlm_seq_priv *priv;
2006 struct seq_file *seq;
2007 struct ocfs2_super *osb;
2009 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2015 osb = (struct ocfs2_super *) inode->u.generic_ip;
2016 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2017 priv->p_dlm_debug = osb->osb_dlm_debug;
2018 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2020 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2027 seq = (struct seq_file *) file->private_data;
2028 seq->private = priv;
2030 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2037 static const struct file_operations ocfs2_dlm_debug_fops = {
2038 .open = ocfs2_dlm_debug_open,
2039 .release = ocfs2_dlm_debug_release,
2041 .llseek = seq_lseek,
2044 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2047 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2049 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2051 osb->osb_debug_root,
2053 &ocfs2_dlm_debug_fops);
2054 if (!dlm_debug->d_locking_state) {
2057 "Unable to create locking state debugfs file.\n");
2061 ocfs2_get_dlm_debug(dlm_debug);
2066 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2068 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2071 debugfs_remove(dlm_debug->d_locking_state);
2072 ocfs2_put_dlm_debug(dlm_debug);
2076 int ocfs2_dlm_init(struct ocfs2_super *osb)
2080 struct dlm_ctxt *dlm;
2084 status = ocfs2_dlm_init_debug(osb);
2090 /* launch vote thread */
2091 osb->vote_task = kthread_run(ocfs2_vote_thread, osb, "ocfs2vote");
2092 if (IS_ERR(osb->vote_task)) {
2093 status = PTR_ERR(osb->vote_task);
2094 osb->vote_task = NULL;
2099 /* used by the dlm code to make message headers unique, each
2100 * node in this domain must agree on this. */
2101 dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
2103 /* for now, uuid == domain */
2104 dlm = dlm_register_domain(osb->uuid_str, dlm_key);
2106 status = PTR_ERR(dlm);
2111 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2112 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2114 dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
2121 ocfs2_dlm_shutdown_debug(osb);
2123 kthread_stop(osb->vote_task);
2130 void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2134 dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
2136 ocfs2_drop_osb_locks(osb);
2138 if (osb->vote_task) {
2139 kthread_stop(osb->vote_task);
2140 osb->vote_task = NULL;
2143 ocfs2_lock_res_free(&osb->osb_super_lockres);
2144 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2146 dlm_unregister_domain(osb->dlm);
2149 ocfs2_dlm_shutdown_debug(osb);
2154 static void ocfs2_unlock_ast(void *opaque, enum dlm_status status)
2156 struct ocfs2_lock_res *lockres = opaque;
2157 unsigned long flags;
2161 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2162 lockres->l_unlock_action);
2164 spin_lock_irqsave(&lockres->l_lock, flags);
2165 /* We tried to cancel a convert request, but it was already
2166 * granted. All we want to do here is clear our unlock
2167 * state. The wake_up call done at the bottom is redundant
2168 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2169 * hurt anything anyway */
2170 if (status == DLM_CANCELGRANT &&
2171 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2172 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2174 /* We don't clear the busy flag in this case as it
2175 * should have been cleared by the ast which the dlm
2177 goto complete_unlock;
2180 if (status != DLM_NORMAL) {
2181 mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
2182 "unlock_action %d\n", status, lockres->l_name,
2183 lockres->l_unlock_action);
2184 spin_unlock_irqrestore(&lockres->l_lock, flags);
2188 switch(lockres->l_unlock_action) {
2189 case OCFS2_UNLOCK_CANCEL_CONVERT:
2190 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2191 lockres->l_action = OCFS2_AST_INVALID;
2193 case OCFS2_UNLOCK_DROP_LOCK:
2194 lockres->l_level = LKM_IVMODE;
2200 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2202 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2203 spin_unlock_irqrestore(&lockres->l_lock, flags);
2205 wake_up(&lockres->l_event);
2210 typedef void (ocfs2_pre_drop_cb_t)(struct ocfs2_lock_res *, void *);
2212 struct drop_lock_cb {
2213 ocfs2_pre_drop_cb_t *drop_func;
2217 static int ocfs2_drop_lock(struct ocfs2_super *osb,
2218 struct ocfs2_lock_res *lockres,
2219 struct drop_lock_cb *dcb)
2221 enum dlm_status status;
2222 unsigned long flags;
2225 /* We didn't get anywhere near actually using this lockres. */
2226 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2229 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
2230 lkm_flags |= LKM_VALBLK;
2232 spin_lock_irqsave(&lockres->l_lock, flags);
2234 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2235 "lockres %s, flags 0x%lx\n",
2236 lockres->l_name, lockres->l_flags);
2238 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2239 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2240 "%u, unlock_action = %u\n",
2241 lockres->l_name, lockres->l_flags, lockres->l_action,
2242 lockres->l_unlock_action);
2244 spin_unlock_irqrestore(&lockres->l_lock, flags);
2246 /* XXX: Today we just wait on any busy
2247 * locks... Perhaps we need to cancel converts in the
2249 ocfs2_wait_on_busy_lock(lockres);
2251 spin_lock_irqsave(&lockres->l_lock, flags);
2255 dcb->drop_func(lockres, dcb->drop_data);
2257 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2258 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2260 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2261 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2263 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2264 spin_unlock_irqrestore(&lockres->l_lock, flags);
2268 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2270 /* make sure we never get here while waiting for an ast to
2272 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2274 /* is this necessary? */
2275 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2276 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2277 spin_unlock_irqrestore(&lockres->l_lock, flags);
2279 mlog(0, "lock %s\n", lockres->l_name);
2281 status = dlmunlock(osb->dlm, &lockres->l_lksb, lkm_flags,
2282 ocfs2_unlock_ast, lockres);
2283 if (status != DLM_NORMAL) {
2284 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2285 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2286 dlm_print_one_lock(lockres->l_lksb.lockid);
2289 mlog(0, "lock %s, successfull return from dlmunlock\n",
2292 ocfs2_wait_on_busy_lock(lockres);
2298 /* Mark the lockres as being dropped. It will no longer be
2299 * queued if blocking, but we still may have to wait on it
2300 * being dequeued from the vote thread before we can consider
2303 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2304 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2307 struct ocfs2_mask_waiter mw;
2308 unsigned long flags;
2310 ocfs2_init_mask_waiter(&mw);
2312 spin_lock_irqsave(&lockres->l_lock, flags);
2313 lockres->l_flags |= OCFS2_LOCK_FREEING;
2314 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2315 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2316 spin_unlock_irqrestore(&lockres->l_lock, flags);
2318 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2320 status = ocfs2_wait_for_mask(&mw);
2324 spin_lock_irqsave(&lockres->l_lock, flags);
2326 spin_unlock_irqrestore(&lockres->l_lock, flags);
2329 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2330 struct ocfs2_lock_res *lockres)
2334 ocfs2_mark_lockres_freeing(lockres);
2335 ret = ocfs2_drop_lock(osb, lockres, NULL);
2340 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2342 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2343 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
2346 static void ocfs2_meta_pre_drop(struct ocfs2_lock_res *lockres, void *data)
2348 struct inode *inode = data;
2350 /* the metadata lock requires a bit more work as we have an
2351 * LVB to worry about. */
2352 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2353 lockres->l_level == LKM_EXMODE &&
2354 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2355 __ocfs2_stuff_meta_lvb(inode);
2358 int ocfs2_drop_inode_locks(struct inode *inode)
2361 struct drop_lock_cb meta_dcb = { ocfs2_meta_pre_drop, inode, };
2365 /* No need to call ocfs2_mark_lockres_freeing here -
2366 * ocfs2_clear_inode has done it for us. */
2368 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2369 &OCFS2_I(inode)->ip_data_lockres,
2376 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2377 &OCFS2_I(inode)->ip_meta_lockres,
2381 if (err < 0 && !status)
2384 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2385 &OCFS2_I(inode)->ip_rw_lockres,
2389 if (err < 0 && !status)
2396 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2399 assert_spin_locked(&lockres->l_lock);
2401 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
2403 if (lockres->l_level <= new_level) {
2404 mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
2405 lockres->l_level, new_level);
2409 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2410 lockres->l_name, new_level, lockres->l_blocking);
2412 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2413 lockres->l_requested = new_level;
2414 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2417 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2418 struct ocfs2_lock_res *lockres,
2422 int ret, dlm_flags = LKM_CONVERT;
2423 enum dlm_status status;
2428 dlm_flags |= LKM_VALBLK;
2430 status = dlmlock(osb->dlm,
2435 OCFS2_LOCK_ID_MAX_LEN - 1,
2438 ocfs2_blocking_ast);
2439 if (status != DLM_NORMAL) {
2440 ocfs2_log_dlm_error("dlmlock", status, lockres);
2442 ocfs2_recover_from_dlm_error(lockres, 1);
2452 /* returns 1 when the caller should unlock and call dlmunlock */
2453 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2454 struct ocfs2_lock_res *lockres)
2456 assert_spin_locked(&lockres->l_lock);
2459 mlog(0, "lock %s\n", lockres->l_name);
2461 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2462 /* If we're already trying to cancel a lock conversion
2463 * then just drop the spinlock and allow the caller to
2464 * requeue this lock. */
2466 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2470 /* were we in a convert when we got the bast fire? */
2471 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2472 lockres->l_action != OCFS2_AST_DOWNCONVERT);
2473 /* set things up for the unlockast to know to just
2474 * clear out the ast_action and unset busy, etc. */
2475 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2477 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2478 "lock %s, invalid flags: 0x%lx\n",
2479 lockres->l_name, lockres->l_flags);
2484 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2485 struct ocfs2_lock_res *lockres)
2488 enum dlm_status status;
2491 mlog(0, "lock %s\n", lockres->l_name);
2494 status = dlmunlock(osb->dlm,
2499 if (status != DLM_NORMAL) {
2500 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2502 ocfs2_recover_from_dlm_error(lockres, 0);
2505 mlog(0, "lock %s return from dlmunlock\n", lockres->l_name);
2511 static inline int ocfs2_can_downconvert_meta_lock(struct inode *inode,
2512 struct ocfs2_lock_res *lockres,
2519 BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
2521 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2523 mlog(0, "lockres %s currently being refreshed -- backing "
2524 "off!\n", lockres->l_name);
2525 } else if (new_level == LKM_PRMODE)
2526 ret = !lockres->l_ex_holders &&
2527 ocfs2_inode_fully_checkpointed(inode);
2528 else /* Must be NLMODE we're converting to. */
2529 ret = !lockres->l_ro_holders && !lockres->l_ex_holders &&
2530 ocfs2_inode_fully_checkpointed(inode);
2536 static int ocfs2_do_unblock_meta(struct inode *inode,
2542 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
2543 unsigned long flags;
2545 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2549 spin_lock_irqsave(&lockres->l_lock, flags);
2551 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2553 mlog(0, "l_level=%d, l_blocking=%d\n", lockres->l_level,
2554 lockres->l_blocking);
2556 BUG_ON(lockres->l_level != LKM_EXMODE &&
2557 lockres->l_level != LKM_PRMODE);
2559 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2561 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2562 spin_unlock_irqrestore(&lockres->l_lock, flags);
2564 ret = ocfs2_cancel_convert(osb, lockres);
2571 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2573 mlog(0, "l_level=%d, l_blocking=%d, new_level=%d\n",
2574 lockres->l_level, lockres->l_blocking, new_level);
2576 if (ocfs2_can_downconvert_meta_lock(inode, lockres, new_level)) {
2577 if (lockres->l_level == LKM_EXMODE)
2580 /* If the lock hasn't been refreshed yet (rare), then
2581 * our memory inode values are old and we skip
2582 * stuffing the lvb. There's no need to actually clear
2583 * out the lvb here as it's value is still valid. */
2584 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2586 __ocfs2_stuff_meta_lvb(inode);
2588 mlog(0, "lockres %s: downconverting stale lock!\n",
2591 mlog(0, "calling ocfs2_downconvert_lock with l_level=%d, "
2592 "l_blocking=%d, new_level=%d\n",
2593 lockres->l_level, lockres->l_blocking, new_level);
2595 ocfs2_prepare_downconvert(lockres, new_level);
2596 spin_unlock_irqrestore(&lockres->l_lock, flags);
2597 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
2600 if (!ocfs2_inode_fully_checkpointed(inode))
2601 ocfs2_start_checkpoint(osb);
2604 spin_unlock_irqrestore(&lockres->l_lock, flags);
2611 static int ocfs2_generic_unblock_lock(struct ocfs2_super *osb,
2612 struct ocfs2_lock_res *lockres,
2613 struct ocfs2_unblock_ctl *ctl,
2614 ocfs2_convert_worker_t *worker)
2616 unsigned long flags;
2623 spin_lock_irqsave(&lockres->l_lock, flags);
2625 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2628 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2630 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2631 spin_unlock_irqrestore(&lockres->l_lock, flags);
2633 ret = ocfs2_cancel_convert(osb, lockres);
2640 /* if we're blocking an exclusive and we have *any* holders,
2642 if ((lockres->l_blocking == LKM_EXMODE)
2643 && (lockres->l_ex_holders || lockres->l_ro_holders))
2646 /* If it's a PR we're blocking, then only
2647 * requeue if we've got any EX holders */
2648 if (lockres->l_blocking == LKM_PRMODE &&
2649 lockres->l_ex_holders)
2653 * Can we get a lock in this state if the holder counts are
2654 * zero? The meta data unblock code used to check this.
2656 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
2657 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
2660 /* If we get here, then we know that there are no more
2661 * incompatible holders (and anyone asking for an incompatible
2662 * lock is blocked). We can now downconvert the lock */
2666 /* Some lockres types want to do a bit of work before
2667 * downconverting a lock. Allow that here. The worker function
2668 * may sleep, so we save off a copy of what we're blocking as
2669 * it may change while we're not holding the spin lock. */
2670 blocking = lockres->l_blocking;
2671 spin_unlock_irqrestore(&lockres->l_lock, flags);
2673 ctl->unblock_action = worker(lockres, blocking);
2675 if (ctl->unblock_action == UNBLOCK_STOP_POST)
2678 spin_lock_irqsave(&lockres->l_lock, flags);
2679 if (blocking != lockres->l_blocking) {
2680 /* If this changed underneath us, then we can't drop
2687 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2689 ocfs2_prepare_downconvert(lockres, new_level);
2690 spin_unlock_irqrestore(&lockres->l_lock, flags);
2691 ret = ocfs2_downconvert_lock(osb, lockres, new_level, 0);
2697 spin_unlock_irqrestore(&lockres->l_lock, flags);
2704 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
2707 struct inode *inode;
2708 struct address_space *mapping;
2710 inode = ocfs2_lock_res_inode(lockres);
2711 mapping = inode->i_mapping;
2713 if (filemap_fdatawrite(mapping)) {
2714 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
2715 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2717 sync_mapping_buffers(mapping);
2718 if (blocking == LKM_EXMODE) {
2719 truncate_inode_pages(mapping, 0);
2720 unmap_mapping_range(mapping, 0, 0, 0);
2722 /* We only need to wait on the I/O if we're not also
2723 * truncating pages because truncate_inode_pages waits
2724 * for us above. We don't truncate pages if we're
2725 * blocking anything < EXMODE because we want to keep
2726 * them around in that case. */
2727 filemap_fdatawait(mapping);
2730 return UNBLOCK_CONTINUE;
2733 int ocfs2_unblock_data(struct ocfs2_lock_res *lockres,
2734 struct ocfs2_unblock_ctl *ctl)
2737 struct inode *inode;
2738 struct ocfs2_super *osb;
2742 inode = ocfs2_lock_res_inode(lockres);
2743 osb = OCFS2_SB(inode->i_sb);
2745 mlog(0, "unblock inode %llu\n",
2746 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2748 status = ocfs2_generic_unblock_lock(osb, lockres, ctl,
2749 ocfs2_data_convert_worker);
2753 mlog(0, "inode %llu, requeue = %d\n",
2754 (unsigned long long)OCFS2_I(inode)->ip_blkno, ctl->requeue);
2760 static int ocfs2_unblock_inode_lock(struct ocfs2_lock_res *lockres,
2761 struct ocfs2_unblock_ctl *ctl)
2764 struct inode *inode;
2768 mlog(0, "Unblock lockres %s\n", lockres->l_name);
2770 inode = ocfs2_lock_res_inode(lockres);
2772 status = ocfs2_generic_unblock_lock(OCFS2_SB(inode->i_sb),
2773 lockres, ctl, NULL);
2781 static int ocfs2_unblock_meta(struct ocfs2_lock_res *lockres,
2782 struct ocfs2_unblock_ctl *ctl)
2785 struct inode *inode;
2789 inode = ocfs2_lock_res_inode(lockres);
2791 mlog(0, "unblock inode %llu\n",
2792 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2794 status = ocfs2_do_unblock_meta(inode, &ctl->requeue);
2798 mlog(0, "inode %llu, requeue = %d\n",
2799 (unsigned long long)OCFS2_I(inode)->ip_blkno, ctl->requeue);
2806 * Does the final reference drop on our dentry lock. Right now this
2807 * happens in the vote thread, but we could choose to simplify the
2808 * dlmglue API and push these off to the ocfs2_wq in the future.
2810 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
2811 struct ocfs2_lock_res *lockres)
2813 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2814 ocfs2_dentry_lock_put(osb, dl);
2818 * d_delete() matching dentries before the lock downconvert.
2820 * At this point, any process waiting to destroy the
2821 * dentry_lock due to last ref count is stopped by the
2822 * OCFS2_LOCK_QUEUED flag.
2824 * We have two potential problems
2826 * 1) If we do the last reference drop on our dentry_lock (via dput)
2827 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
2828 * the downconvert to finish. Instead we take an elevated
2829 * reference and push the drop until after we've completed our
2830 * unblock processing.
2832 * 2) There might be another process with a final reference,
2833 * waiting on us to finish processing. If this is the case, we
2834 * detect it and exit out - there's no more dentries anyway.
2836 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
2839 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2840 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
2841 struct dentry *dentry;
2842 unsigned long flags;
2846 * This node is blocking another node from getting a read
2847 * lock. This happens when we've renamed within a
2848 * directory. We've forced the other nodes to d_delete(), but
2849 * we never actually dropped our lock because it's still
2850 * valid. The downconvert code will retain a PR for this node,
2851 * so there's no further work to do.
2853 if (blocking == LKM_PRMODE)
2854 return UNBLOCK_CONTINUE;
2857 * Mark this inode as potentially orphaned. The code in
2858 * ocfs2_delete_inode() will figure out whether it actually
2859 * needs to be freed or not.
2861 spin_lock(&oi->ip_lock);
2862 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
2863 spin_unlock(&oi->ip_lock);
2866 * Yuck. We need to make sure however that the check of
2867 * OCFS2_LOCK_FREEING and the extra reference are atomic with
2868 * respect to a reference decrement or the setting of that
2871 spin_lock_irqsave(&lockres->l_lock, flags);
2872 spin_lock(&dentry_attach_lock);
2873 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
2878 spin_unlock(&dentry_attach_lock);
2879 spin_unlock_irqrestore(&lockres->l_lock, flags);
2881 mlog(0, "extra_ref = %d\n", extra_ref);
2884 * We have a process waiting on us in ocfs2_dentry_iput(),
2885 * which means we can't have any more outstanding
2886 * aliases. There's no need to do any more work.
2889 return UNBLOCK_CONTINUE;
2891 spin_lock(&dentry_attach_lock);
2893 dentry = ocfs2_find_local_alias(dl->dl_inode,
2894 dl->dl_parent_blkno, 1);
2897 spin_unlock(&dentry_attach_lock);
2899 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
2900 dentry->d_name.name);
2903 * The following dcache calls may do an
2904 * iput(). Normally we don't want that from the
2905 * downconverting thread, but in this case it's ok
2906 * because the requesting node already has an
2907 * exclusive lock on the inode, so it can't be queued
2908 * for a downconvert.
2913 spin_lock(&dentry_attach_lock);
2915 spin_unlock(&dentry_attach_lock);
2918 * If we are the last holder of this dentry lock, there is no
2919 * reason to downconvert so skip straight to the unlock.
2921 if (dl->dl_count == 1)
2922 return UNBLOCK_STOP_POST;
2924 return UNBLOCK_CONTINUE_POST;
2927 static int ocfs2_unblock_dentry_lock(struct ocfs2_lock_res *lockres,
2928 struct ocfs2_unblock_ctl *ctl)
2931 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2932 struct ocfs2_super *osb = OCFS2_SB(dl->dl_inode->i_sb);
2934 mlog(0, "unblock dentry lock: %llu\n",
2935 (unsigned long long)OCFS2_I(dl->dl_inode)->ip_blkno);
2937 ret = ocfs2_generic_unblock_lock(osb,
2940 ocfs2_dentry_convert_worker);
2944 mlog(0, "requeue = %d, post = %d\n", ctl->requeue, ctl->unblock_action);
2949 /* Generic unblock function for any lockres whose private data is an
2950 * ocfs2_super pointer. */
2951 static int ocfs2_unblock_osb_lock(struct ocfs2_lock_res *lockres,
2952 struct ocfs2_unblock_ctl *ctl)
2955 struct ocfs2_super *osb;
2959 mlog(0, "Unblock lockres %s\n", lockres->l_name);
2961 osb = ocfs2_get_lockres_osb(lockres);
2963 status = ocfs2_generic_unblock_lock(osb,
2974 void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
2975 struct ocfs2_lock_res *lockres)
2978 struct ocfs2_unblock_ctl ctl = {0, 0,};
2979 unsigned long flags;
2981 /* Our reference to the lockres in this function can be
2982 * considered valid until we remove the OCFS2_LOCK_QUEUED
2988 BUG_ON(!lockres->l_ops);
2989 BUG_ON(!lockres->l_ops->unblock);
2991 mlog(0, "lockres %s blocked.\n", lockres->l_name);
2993 /* Detect whether a lock has been marked as going away while
2994 * the vote thread was processing other things. A lock can
2995 * still be marked with OCFS2_LOCK_FREEING after this check,
2996 * but short circuiting here will still save us some
2998 spin_lock_irqsave(&lockres->l_lock, flags);
2999 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3001 spin_unlock_irqrestore(&lockres->l_lock, flags);
3003 status = lockres->l_ops->unblock(lockres, &ctl);
3007 spin_lock_irqsave(&lockres->l_lock, flags);
3009 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
3010 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3012 ocfs2_schedule_blocked_lock(osb, lockres);
3014 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
3015 ctl.requeue ? "yes" : "no");
3016 spin_unlock_irqrestore(&lockres->l_lock, flags);
3018 if (ctl.unblock_action != UNBLOCK_CONTINUE
3019 && lockres->l_ops->post_unlock)
3020 lockres->l_ops->post_unlock(osb, lockres);
3025 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3026 struct ocfs2_lock_res *lockres)
3030 assert_spin_locked(&lockres->l_lock);
3032 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3033 /* Do not schedule a lock for downconvert when it's on
3034 * the way to destruction - any nodes wanting access
3035 * to the resource will get it soon. */
3036 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3037 lockres->l_name, lockres->l_flags);
3041 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3043 spin_lock(&osb->vote_task_lock);
3044 if (list_empty(&lockres->l_blocked_list)) {
3045 list_add_tail(&lockres->l_blocked_list,
3046 &osb->blocked_lock_list);
3047 osb->blocked_lock_count++;
3049 spin_unlock(&osb->vote_task_lock);
3054 /* This aids in debugging situations where a bad LVB might be involved. */
3055 void ocfs2_dump_meta_lvb_info(u64 level,
3056 const char *function,
3058 struct ocfs2_lock_res *lockres)
3060 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
3062 mlog(level, "LVB information for %s (called from %s:%u):\n",
3063 lockres->l_name, function, line);
3064 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
3065 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
3066 be32_to_cpu(lvb->lvb_igeneration));
3067 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
3068 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
3069 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
3070 be16_to_cpu(lvb->lvb_imode));
3071 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
3072 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
3073 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
3074 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
3075 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
3076 be32_to_cpu(lvb->lvb_iattr));