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
138 typedef int (ocfs2_convert_worker_t)(struct ocfs2_lock_res *, int);
139 static int ocfs2_generic_unblock_lock(struct ocfs2_super *osb,
140 struct ocfs2_lock_res *lockres,
141 struct ocfs2_unblock_ctl *ctl,
142 ocfs2_convert_worker_t *worker);
144 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
145 .get_osb = ocfs2_get_inode_osb,
146 .unblock = ocfs2_unblock_inode_lock,
150 static struct ocfs2_lock_res_ops ocfs2_inode_meta_lops = {
151 .get_osb = ocfs2_get_inode_osb,
152 .unblock = ocfs2_unblock_meta,
153 .flags = LOCK_TYPE_REQUIRES_REFRESH,
156 static struct ocfs2_lock_res_ops ocfs2_inode_data_lops = {
157 .get_osb = ocfs2_get_inode_osb,
158 .unblock = ocfs2_unblock_data,
162 static struct ocfs2_lock_res_ops ocfs2_super_lops = {
163 .unblock = ocfs2_unblock_osb_lock,
164 .flags = LOCK_TYPE_REQUIRES_REFRESH,
167 static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
168 .unblock = ocfs2_unblock_osb_lock,
172 static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
173 .get_osb = ocfs2_get_dentry_osb,
174 .unblock = ocfs2_unblock_dentry_lock,
175 .post_unlock = ocfs2_dentry_post_unlock,
179 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
181 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
182 lockres->l_type == OCFS2_LOCK_TYPE_DATA ||
183 lockres->l_type == OCFS2_LOCK_TYPE_RW;
186 static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
188 BUG_ON(!ocfs2_is_inode_lock(lockres));
190 return (struct inode *) lockres->l_priv;
193 static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
195 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
197 return (struct ocfs2_dentry_lock *)lockres->l_priv;
200 static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
202 if (lockres->l_ops->get_osb)
203 return lockres->l_ops->get_osb(lockres);
205 return (struct ocfs2_super *)lockres->l_priv;
208 static int ocfs2_lock_create(struct ocfs2_super *osb,
209 struct ocfs2_lock_res *lockres,
212 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
214 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
215 struct ocfs2_lock_res *lockres,
217 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
218 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
219 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
220 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
221 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
222 struct ocfs2_lock_res *lockres);
223 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
225 #define ocfs2_log_dlm_error(_func, _stat, _lockres) do { \
226 mlog(ML_ERROR, "Dlm error \"%s\" while calling %s on " \
227 "resource %s: %s\n", dlm_errname(_stat), _func, \
228 _lockres->l_name, dlm_errmsg(_stat)); \
230 static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
231 struct ocfs2_lock_res *lockres);
232 static int ocfs2_meta_lock_update(struct inode *inode,
233 struct buffer_head **bh);
234 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
235 static inline int ocfs2_highest_compat_lock_level(int level);
236 static inline int ocfs2_can_downconvert_meta_lock(struct inode *inode,
237 struct ocfs2_lock_res *lockres,
240 static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
249 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
251 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
252 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
253 (long long)blkno, generation);
255 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
257 mlog(0, "built lock resource with name: %s\n", name);
262 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
264 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
265 struct ocfs2_dlm_debug *dlm_debug)
267 mlog(0, "Add tracking for lockres %s\n", res->l_name);
269 spin_lock(&ocfs2_dlm_tracking_lock);
270 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
271 spin_unlock(&ocfs2_dlm_tracking_lock);
274 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
276 spin_lock(&ocfs2_dlm_tracking_lock);
277 if (!list_empty(&res->l_debug_list))
278 list_del_init(&res->l_debug_list);
279 spin_unlock(&ocfs2_dlm_tracking_lock);
282 static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
283 struct ocfs2_lock_res *res,
284 enum ocfs2_lock_type type,
285 struct ocfs2_lock_res_ops *ops,
292 res->l_level = LKM_IVMODE;
293 res->l_requested = LKM_IVMODE;
294 res->l_blocking = LKM_IVMODE;
295 res->l_action = OCFS2_AST_INVALID;
296 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
298 res->l_flags = OCFS2_LOCK_INITIALIZED;
300 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
303 void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
305 /* This also clears out the lock status block */
306 memset(res, 0, sizeof(struct ocfs2_lock_res));
307 spin_lock_init(&res->l_lock);
308 init_waitqueue_head(&res->l_event);
309 INIT_LIST_HEAD(&res->l_blocked_list);
310 INIT_LIST_HEAD(&res->l_mask_waiters);
313 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
314 enum ocfs2_lock_type type,
315 unsigned int generation,
318 struct ocfs2_lock_res_ops *ops;
321 case OCFS2_LOCK_TYPE_RW:
322 ops = &ocfs2_inode_rw_lops;
324 case OCFS2_LOCK_TYPE_META:
325 ops = &ocfs2_inode_meta_lops;
327 case OCFS2_LOCK_TYPE_DATA:
328 ops = &ocfs2_inode_data_lops;
331 mlog_bug_on_msg(1, "type: %d\n", type);
332 ops = NULL; /* thanks, gcc */
336 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
337 generation, res->l_name);
338 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
341 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
343 struct inode *inode = ocfs2_lock_res_inode(lockres);
345 return OCFS2_SB(inode->i_sb);
348 static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
350 __be64 inode_blkno_be;
352 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
355 return be64_to_cpu(inode_blkno_be);
358 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
360 struct ocfs2_dentry_lock *dl = lockres->l_priv;
362 return OCFS2_SB(dl->dl_inode->i_sb);
365 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
366 u64 parent, struct inode *inode)
369 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
370 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
371 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
373 ocfs2_lock_res_init_once(lockres);
376 * Unfortunately, the standard lock naming scheme won't work
377 * here because we have two 16 byte values to use. Instead,
378 * we'll stuff the inode number as a binary value. We still
379 * want error prints to show something without garbling the
380 * display, so drop a null byte in there before the inode
381 * number. A future version of OCFS2 will likely use all
382 * binary lock names. The stringified names have been a
383 * tremendous aid in debugging, but now that the debugfs
384 * interface exists, we can mangle things there if need be.
386 * NOTE: We also drop the standard "pad" value (the total lock
387 * name size stays the same though - the last part is all
388 * zeros due to the memset in ocfs2_lock_res_init_once()
390 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
392 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
395 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
397 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
400 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
401 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
405 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
406 struct ocfs2_super *osb)
408 /* Superblock lockres doesn't come from a slab so we call init
409 * once on it manually. */
410 ocfs2_lock_res_init_once(res);
411 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
413 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
414 &ocfs2_super_lops, osb);
417 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
418 struct ocfs2_super *osb)
420 /* Rename lockres doesn't come from a slab so we call init
421 * once on it manually. */
422 ocfs2_lock_res_init_once(res);
423 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
424 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
425 &ocfs2_rename_lops, osb);
428 void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
432 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
435 ocfs2_remove_lockres_tracking(res);
437 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
438 "Lockres %s is on the blocked list\n",
440 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
441 "Lockres %s has mask waiters pending\n",
443 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
444 "Lockres %s is locked\n",
446 mlog_bug_on_msg(res->l_ro_holders,
447 "Lockres %s has %u ro holders\n",
448 res->l_name, res->l_ro_holders);
449 mlog_bug_on_msg(res->l_ex_holders,
450 "Lockres %s has %u ex holders\n",
451 res->l_name, res->l_ex_holders);
453 /* Need to clear out the lock status block for the dlm */
454 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
460 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
469 lockres->l_ex_holders++;
472 lockres->l_ro_holders++;
481 static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
490 BUG_ON(!lockres->l_ex_holders);
491 lockres->l_ex_holders--;
494 BUG_ON(!lockres->l_ro_holders);
495 lockres->l_ro_holders--;
503 /* WARNING: This function lives in a world where the only three lock
504 * levels are EX, PR, and NL. It *will* have to be adjusted when more
505 * lock types are added. */
506 static inline int ocfs2_highest_compat_lock_level(int level)
508 int new_level = LKM_EXMODE;
510 if (level == LKM_EXMODE)
511 new_level = LKM_NLMODE;
512 else if (level == LKM_PRMODE)
513 new_level = LKM_PRMODE;
517 static void lockres_set_flags(struct ocfs2_lock_res *lockres,
518 unsigned long newflags)
520 struct list_head *pos, *tmp;
521 struct ocfs2_mask_waiter *mw;
523 assert_spin_locked(&lockres->l_lock);
525 lockres->l_flags = newflags;
527 list_for_each_safe(pos, tmp, &lockres->l_mask_waiters) {
528 mw = list_entry(pos, struct ocfs2_mask_waiter, mw_item);
529 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
532 list_del_init(&mw->mw_item);
534 complete(&mw->mw_complete);
537 static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
539 lockres_set_flags(lockres, lockres->l_flags | or);
541 static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
544 lockres_set_flags(lockres, lockres->l_flags & ~clear);
547 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
551 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
552 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
553 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
554 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
556 lockres->l_level = lockres->l_requested;
557 if (lockres->l_level <=
558 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
559 lockres->l_blocking = LKM_NLMODE;
560 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
562 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
567 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
571 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
572 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
574 /* Convert from RO to EX doesn't really need anything as our
575 * information is already up to data. Convert from NL to
576 * *anything* however should mark ourselves as needing an
578 if (lockres->l_level == LKM_NLMODE &&
579 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
580 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
582 lockres->l_level = lockres->l_requested;
583 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
588 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
592 BUG_ON((!lockres->l_flags & OCFS2_LOCK_BUSY));
593 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
595 if (lockres->l_requested > LKM_NLMODE &&
596 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
597 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
598 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
600 lockres->l_level = lockres->l_requested;
601 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
602 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
607 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
610 int needs_downconvert = 0;
613 assert_spin_locked(&lockres->l_lock);
615 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
617 if (level > lockres->l_blocking) {
618 /* only schedule a downconvert if we haven't already scheduled
619 * one that goes low enough to satisfy the level we're
620 * blocking. this also catches the case where we get
622 if (ocfs2_highest_compat_lock_level(level) <
623 ocfs2_highest_compat_lock_level(lockres->l_blocking))
624 needs_downconvert = 1;
626 lockres->l_blocking = level;
629 mlog_exit(needs_downconvert);
630 return needs_downconvert;
633 static void ocfs2_blocking_ast(void *opaque, int level)
635 struct ocfs2_lock_res *lockres = opaque;
636 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
637 int needs_downconvert;
640 BUG_ON(level <= LKM_NLMODE);
642 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
643 lockres->l_name, level, lockres->l_level,
644 ocfs2_lock_type_string(lockres->l_type));
646 spin_lock_irqsave(&lockres->l_lock, flags);
647 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
648 if (needs_downconvert)
649 ocfs2_schedule_blocked_lock(osb, lockres);
650 spin_unlock_irqrestore(&lockres->l_lock, flags);
652 wake_up(&lockres->l_event);
654 ocfs2_kick_vote_thread(osb);
657 static void ocfs2_locking_ast(void *opaque)
659 struct ocfs2_lock_res *lockres = opaque;
660 struct dlm_lockstatus *lksb = &lockres->l_lksb;
663 spin_lock_irqsave(&lockres->l_lock, flags);
665 if (lksb->status != DLM_NORMAL) {
666 mlog(ML_ERROR, "lockres %s: lksb status value of %u!\n",
667 lockres->l_name, lksb->status);
668 spin_unlock_irqrestore(&lockres->l_lock, flags);
672 switch(lockres->l_action) {
673 case OCFS2_AST_ATTACH:
674 ocfs2_generic_handle_attach_action(lockres);
675 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
677 case OCFS2_AST_CONVERT:
678 ocfs2_generic_handle_convert_action(lockres);
680 case OCFS2_AST_DOWNCONVERT:
681 ocfs2_generic_handle_downconvert_action(lockres);
684 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
685 "lockres flags = 0x%lx, unlock action: %u\n",
686 lockres->l_name, lockres->l_action, lockres->l_flags,
687 lockres->l_unlock_action);
691 /* set it to something invalid so if we get called again we
693 lockres->l_action = OCFS2_AST_INVALID;
695 wake_up(&lockres->l_event);
696 spin_unlock_irqrestore(&lockres->l_lock, flags);
699 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
705 spin_lock_irqsave(&lockres->l_lock, flags);
706 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
708 lockres->l_action = OCFS2_AST_INVALID;
710 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
711 spin_unlock_irqrestore(&lockres->l_lock, flags);
713 wake_up(&lockres->l_event);
717 /* Note: If we detect another process working on the lock (i.e.,
718 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
719 * to do the right thing in that case.
721 static int ocfs2_lock_create(struct ocfs2_super *osb,
722 struct ocfs2_lock_res *lockres,
727 enum dlm_status status;
732 mlog(0, "lock %s, level = %d, flags = %d\n", lockres->l_name, level,
735 spin_lock_irqsave(&lockres->l_lock, flags);
736 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
737 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
738 spin_unlock_irqrestore(&lockres->l_lock, flags);
742 lockres->l_action = OCFS2_AST_ATTACH;
743 lockres->l_requested = level;
744 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
745 spin_unlock_irqrestore(&lockres->l_lock, flags);
747 status = dlmlock(osb->dlm,
752 OCFS2_LOCK_ID_MAX_LEN - 1,
756 if (status != DLM_NORMAL) {
757 ocfs2_log_dlm_error("dlmlock", status, lockres);
759 ocfs2_recover_from_dlm_error(lockres, 1);
762 mlog(0, "lock %s, successfull return from dlmlock\n", lockres->l_name);
769 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
775 spin_lock_irqsave(&lockres->l_lock, flags);
776 ret = lockres->l_flags & flag;
777 spin_unlock_irqrestore(&lockres->l_lock, flags);
782 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
785 wait_event(lockres->l_event,
786 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
789 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
792 wait_event(lockres->l_event,
793 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
796 /* predict what lock level we'll be dropping down to on behalf
797 * of another node, and return true if the currently wanted
798 * level will be compatible with it. */
799 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
802 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
804 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
807 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
809 INIT_LIST_HEAD(&mw->mw_item);
810 init_completion(&mw->mw_complete);
813 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
815 wait_for_completion(&mw->mw_complete);
816 /* Re-arm the completion in case we want to wait on it again */
817 INIT_COMPLETION(mw->mw_complete);
818 return mw->mw_status;
821 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
822 struct ocfs2_mask_waiter *mw,
826 BUG_ON(!list_empty(&mw->mw_item));
828 assert_spin_locked(&lockres->l_lock);
830 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
835 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
836 * if the mask still hadn't reached its goal */
837 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
838 struct ocfs2_mask_waiter *mw)
843 spin_lock_irqsave(&lockres->l_lock, flags);
844 if (!list_empty(&mw->mw_item)) {
845 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
848 list_del_init(&mw->mw_item);
849 init_completion(&mw->mw_complete);
851 spin_unlock_irqrestore(&lockres->l_lock, flags);
857 static int ocfs2_cluster_lock(struct ocfs2_super *osb,
858 struct ocfs2_lock_res *lockres,
863 struct ocfs2_mask_waiter mw;
864 enum dlm_status status;
865 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
866 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
871 ocfs2_init_mask_waiter(&mw);
876 if (catch_signals && signal_pending(current)) {
881 spin_lock_irqsave(&lockres->l_lock, flags);
883 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
884 "Cluster lock called on freeing lockres %s! flags "
885 "0x%lx\n", lockres->l_name, lockres->l_flags);
887 /* We only compare against the currently granted level
888 * here. If the lock is blocked waiting on a downconvert,
889 * we'll get caught below. */
890 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
891 level > lockres->l_level) {
892 /* is someone sitting in dlm_lock? If so, wait on
894 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
899 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
900 /* lock has not been created yet. */
901 spin_unlock_irqrestore(&lockres->l_lock, flags);
903 ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
911 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
912 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
913 /* is the lock is currently blocked on behalf of
915 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
920 if (level > lockres->l_level) {
921 if (lockres->l_action != OCFS2_AST_INVALID)
922 mlog(ML_ERROR, "lockres %s has action %u pending\n",
923 lockres->l_name, lockres->l_action);
925 lockres->l_action = OCFS2_AST_CONVERT;
926 lockres->l_requested = level;
927 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
928 spin_unlock_irqrestore(&lockres->l_lock, flags);
930 BUG_ON(level == LKM_IVMODE);
931 BUG_ON(level == LKM_NLMODE);
933 mlog(0, "lock %s, convert from %d to level = %d\n",
934 lockres->l_name, lockres->l_level, level);
936 /* call dlm_lock to upgrade lock now */
937 status = dlmlock(osb->dlm,
940 lkm_flags|LKM_CONVERT|LKM_VALBLK,
942 OCFS2_LOCK_ID_MAX_LEN - 1,
946 if (status != DLM_NORMAL) {
947 if ((lkm_flags & LKM_NOQUEUE) &&
948 (status == DLM_NOTQUEUED))
951 ocfs2_log_dlm_error("dlmlock", status,
955 ocfs2_recover_from_dlm_error(lockres, 1);
959 mlog(0, "lock %s, successfull return from dlmlock\n",
962 /* At this point we've gone inside the dlm and need to
963 * complete our work regardless. */
966 /* wait for busy to clear and carry on */
970 /* Ok, if we get here then we're good to go. */
971 ocfs2_inc_holders(lockres, level);
975 spin_unlock_irqrestore(&lockres->l_lock, flags);
978 * This is helping work around a lock inversion between the page lock
979 * and dlm locks. One path holds the page lock while calling aops
980 * which block acquiring dlm locks. The voting thread holds dlm
981 * locks while acquiring page locks while down converting data locks.
982 * This block is helping an aop path notice the inversion and back
983 * off to unlock its page lock before trying the dlm lock again.
985 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
986 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
988 if (lockres_remove_mask_waiter(lockres, &mw))
994 ret = ocfs2_wait_for_mask(&mw);
1004 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1005 struct ocfs2_lock_res *lockres,
1008 unsigned long flags;
1011 spin_lock_irqsave(&lockres->l_lock, flags);
1012 ocfs2_dec_holders(lockres, level);
1013 ocfs2_vote_on_unlock(osb, lockres);
1014 spin_unlock_irqrestore(&lockres->l_lock, flags);
1018 int ocfs2_create_new_lock(struct ocfs2_super *osb,
1019 struct ocfs2_lock_res *lockres,
1023 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1024 unsigned long flags;
1025 int lkm_flags = local ? LKM_LOCAL : 0;
1027 spin_lock_irqsave(&lockres->l_lock, flags);
1028 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1029 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1030 spin_unlock_irqrestore(&lockres->l_lock, flags);
1032 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
1035 /* Grants us an EX lock on the data and metadata resources, skipping
1036 * the normal cluster directory lookup. Use this ONLY on newly created
1037 * inodes which other nodes can't possibly see, and which haven't been
1038 * hashed in the inode hash yet. This can give us a good performance
1039 * increase as it'll skip the network broadcast normally associated
1040 * with creating a new lock resource. */
1041 int ocfs2_create_new_inode_locks(struct inode *inode)
1044 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1047 BUG_ON(!ocfs2_inode_is_new(inode));
1051 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1053 /* NOTE: That we don't increment any of the holder counts, nor
1054 * do we add anything to a journal handle. Since this is
1055 * supposed to be a new inode which the cluster doesn't know
1056 * about yet, there is no need to. As far as the LVB handling
1057 * is concerned, this is basically like acquiring an EX lock
1058 * on a resource which has an invalid one -- we'll set it
1059 * valid when we release the EX. */
1061 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
1068 * We don't want to use LKM_LOCAL on a meta data lock as they
1069 * don't use a generation in their lock names.
1071 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_meta_lockres, 1, 0);
1077 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_data_lockres, 1, 1);
1088 int ocfs2_rw_lock(struct inode *inode, int write)
1091 struct ocfs2_lock_res *lockres;
1097 mlog(0, "inode %llu take %s RW lock\n",
1098 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1099 write ? "EXMODE" : "PRMODE");
1101 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1103 level = write ? LKM_EXMODE : LKM_PRMODE;
1105 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1114 void ocfs2_rw_unlock(struct inode *inode, int write)
1116 int level = write ? LKM_EXMODE : LKM_PRMODE;
1117 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1121 mlog(0, "inode %llu drop %s RW lock\n",
1122 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1123 write ? "EXMODE" : "PRMODE");
1125 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1130 int ocfs2_data_lock_full(struct inode *inode,
1134 int status = 0, level;
1135 struct ocfs2_lock_res *lockres;
1141 mlog(0, "inode %llu take %s DATA lock\n",
1142 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1143 write ? "EXMODE" : "PRMODE");
1145 /* We'll allow faking a readonly data lock for
1147 if (ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb))) {
1155 lockres = &OCFS2_I(inode)->ip_data_lockres;
1157 level = write ? LKM_EXMODE : LKM_PRMODE;
1159 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level,
1161 if (status < 0 && status != -EAGAIN)
1169 /* see ocfs2_meta_lock_with_page() */
1170 int ocfs2_data_lock_with_page(struct inode *inode,
1176 ret = ocfs2_data_lock_full(inode, write, OCFS2_LOCK_NONBLOCK);
1177 if (ret == -EAGAIN) {
1179 if (ocfs2_data_lock(inode, write) == 0)
1180 ocfs2_data_unlock(inode, write);
1181 ret = AOP_TRUNCATED_PAGE;
1187 static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
1188 struct ocfs2_lock_res *lockres)
1194 /* If we know that another node is waiting on our lock, kick
1195 * the vote thread * pre-emptively when we reach a release
1197 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1198 switch(lockres->l_blocking) {
1200 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1204 if (!lockres->l_ex_holders)
1213 ocfs2_kick_vote_thread(osb);
1218 void ocfs2_data_unlock(struct inode *inode,
1221 int level = write ? LKM_EXMODE : LKM_PRMODE;
1222 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_data_lockres;
1226 mlog(0, "inode %llu drop %s DATA lock\n",
1227 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1228 write ? "EXMODE" : "PRMODE");
1230 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1231 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1236 #define OCFS2_SEC_BITS 34
1237 #define OCFS2_SEC_SHIFT (64 - 34)
1238 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1240 /* LVB only has room for 64 bits of time here so we pack it for
1242 static u64 ocfs2_pack_timespec(struct timespec *spec)
1245 u64 sec = spec->tv_sec;
1246 u32 nsec = spec->tv_nsec;
1248 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1253 /* Call this with the lockres locked. I am reasonably sure we don't
1254 * need ip_lock in this function as anyone who would be changing those
1255 * values is supposed to be blocked in ocfs2_meta_lock right now. */
1256 static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1258 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1259 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1260 struct ocfs2_meta_lvb *lvb;
1264 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1267 * Invalidate the LVB of a deleted inode - this way other
1268 * nodes are forced to go to disk and discover the new inode
1271 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1272 lvb->lvb_version = 0;
1276 lvb->lvb_version = OCFS2_LVB_VERSION;
1277 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1278 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1279 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1280 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1281 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1282 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1283 lvb->lvb_iatime_packed =
1284 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1285 lvb->lvb_ictime_packed =
1286 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1287 lvb->lvb_imtime_packed =
1288 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1289 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
1290 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
1293 mlog_meta_lvb(0, lockres);
1298 static void ocfs2_unpack_timespec(struct timespec *spec,
1301 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1302 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1305 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1307 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1308 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1309 struct ocfs2_meta_lvb *lvb;
1313 mlog_meta_lvb(0, lockres);
1315 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1317 /* We're safe here without the lockres lock... */
1318 spin_lock(&oi->ip_lock);
1319 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1320 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1322 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1323 ocfs2_set_inode_flags(inode);
1325 /* fast-symlinks are a special case */
1326 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1327 inode->i_blocks = 0;
1330 ocfs2_align_bytes_to_sectors(i_size_read(inode));
1332 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1333 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1334 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1335 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1336 ocfs2_unpack_timespec(&inode->i_atime,
1337 be64_to_cpu(lvb->lvb_iatime_packed));
1338 ocfs2_unpack_timespec(&inode->i_mtime,
1339 be64_to_cpu(lvb->lvb_imtime_packed));
1340 ocfs2_unpack_timespec(&inode->i_ctime,
1341 be64_to_cpu(lvb->lvb_ictime_packed));
1342 spin_unlock(&oi->ip_lock);
1347 static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1348 struct ocfs2_lock_res *lockres)
1350 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1352 if (lvb->lvb_version == OCFS2_LVB_VERSION
1353 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
1358 /* Determine whether a lock resource needs to be refreshed, and
1359 * arbitrate who gets to refresh it.
1361 * 0 means no refresh needed.
1363 * > 0 means you need to refresh this and you MUST call
1364 * ocfs2_complete_lock_res_refresh afterwards. */
1365 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1367 unsigned long flags;
1373 spin_lock_irqsave(&lockres->l_lock, flags);
1374 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1375 spin_unlock_irqrestore(&lockres->l_lock, flags);
1379 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1380 spin_unlock_irqrestore(&lockres->l_lock, flags);
1382 ocfs2_wait_on_refreshing_lock(lockres);
1386 /* Ok, I'll be the one to refresh this lock. */
1387 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1388 spin_unlock_irqrestore(&lockres->l_lock, flags);
1396 /* If status is non zero, I'll mark it as not being in refresh
1397 * anymroe, but i won't clear the needs refresh flag. */
1398 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1401 unsigned long flags;
1404 spin_lock_irqsave(&lockres->l_lock, flags);
1405 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1407 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1408 spin_unlock_irqrestore(&lockres->l_lock, flags);
1410 wake_up(&lockres->l_event);
1415 /* may or may not return a bh if it went to disk. */
1416 static int ocfs2_meta_lock_update(struct inode *inode,
1417 struct buffer_head **bh)
1420 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1421 struct ocfs2_lock_res *lockres;
1422 struct ocfs2_dinode *fe;
1426 spin_lock(&oi->ip_lock);
1427 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1428 mlog(0, "Orphaned inode %llu was deleted while we "
1429 "were waiting on a lock. ip_flags = 0x%x\n",
1430 (unsigned long long)oi->ip_blkno, oi->ip_flags);
1431 spin_unlock(&oi->ip_lock);
1435 spin_unlock(&oi->ip_lock);
1437 lockres = &oi->ip_meta_lockres;
1439 if (!ocfs2_should_refresh_lock_res(lockres))
1442 /* This will discard any caching information we might have had
1443 * for the inode metadata. */
1444 ocfs2_metadata_cache_purge(inode);
1446 /* will do nothing for inode types that don't use the extent
1447 * map (directories, bitmap files, etc) */
1448 ocfs2_extent_map_trunc(inode, 0);
1450 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
1451 mlog(0, "Trusting LVB on inode %llu\n",
1452 (unsigned long long)oi->ip_blkno);
1453 ocfs2_refresh_inode_from_lvb(inode);
1455 /* Boo, we have to go to disk. */
1456 /* read bh, cast, ocfs2_refresh_inode */
1457 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1458 bh, OCFS2_BH_CACHED, inode);
1463 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1465 /* This is a good chance to make sure we're not
1466 * locking an invalid object.
1468 * We bug on a stale inode here because we checked
1469 * above whether it was wiped from disk. The wiping
1470 * node provides a guarantee that we receive that
1471 * message and can mark the inode before dropping any
1472 * locks associated with it. */
1473 if (!OCFS2_IS_VALID_DINODE(fe)) {
1474 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1478 mlog_bug_on_msg(inode->i_generation !=
1479 le32_to_cpu(fe->i_generation),
1480 "Invalid dinode %llu disk generation: %u "
1481 "inode->i_generation: %u\n",
1482 (unsigned long long)oi->ip_blkno,
1483 le32_to_cpu(fe->i_generation),
1484 inode->i_generation);
1485 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1486 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
1487 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1488 (unsigned long long)oi->ip_blkno,
1489 (unsigned long long)le64_to_cpu(fe->i_dtime),
1490 le32_to_cpu(fe->i_flags));
1492 ocfs2_refresh_inode(inode, fe);
1497 ocfs2_complete_lock_res_refresh(lockres, status);
1503 static int ocfs2_assign_bh(struct inode *inode,
1504 struct buffer_head **ret_bh,
1505 struct buffer_head *passed_bh)
1510 /* Ok, the update went to disk for us, use the
1512 *ret_bh = passed_bh;
1518 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1519 OCFS2_I(inode)->ip_blkno,
1530 * returns < 0 error if the callback will never be called, otherwise
1531 * the result of the lock will be communicated via the callback.
1533 int ocfs2_meta_lock_full(struct inode *inode,
1534 struct ocfs2_journal_handle *handle,
1535 struct buffer_head **ret_bh,
1539 int status, level, dlm_flags, acquired;
1540 struct ocfs2_lock_res *lockres;
1541 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1542 struct buffer_head *local_bh = NULL;
1548 mlog(0, "inode %llu, take %s META lock\n",
1549 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1550 ex ? "EXMODE" : "PRMODE");
1554 /* We'll allow faking a readonly metadata lock for
1556 if (ocfs2_is_hard_readonly(osb)) {
1562 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1563 wait_event(osb->recovery_event,
1564 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1567 lockres = &OCFS2_I(inode)->ip_meta_lockres;
1568 level = ex ? LKM_EXMODE : LKM_PRMODE;
1570 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
1571 dlm_flags |= LKM_NOQUEUE;
1573 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1575 if (status != -EAGAIN && status != -EIOCBRETRY)
1580 /* Notify the error cleanup path to drop the cluster lock. */
1583 /* We wait twice because a node may have died while we were in
1584 * the lower dlm layers. The second time though, we've
1585 * committed to owning this lock so we don't allow signals to
1586 * abort the operation. */
1587 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1588 wait_event(osb->recovery_event,
1589 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1592 * We only see this flag if we're being called from
1593 * ocfs2_read_locked_inode(). It means we're locking an inode
1594 * which hasn't been populated yet, so clear the refresh flag
1595 * and let the caller handle it.
1597 if (inode->i_state & I_NEW) {
1599 ocfs2_complete_lock_res_refresh(lockres, 0);
1603 /* This is fun. The caller may want a bh back, or it may
1604 * not. ocfs2_meta_lock_update definitely wants one in, but
1605 * may or may not read one, depending on what's in the
1606 * LVB. The result of all of this is that we've *only* gone to
1607 * disk if we have to, so the complexity is worthwhile. */
1608 status = ocfs2_meta_lock_update(inode, &local_bh);
1610 if (status != -ENOENT)
1616 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
1624 status = ocfs2_handle_add_lock(handle, inode);
1631 if (ret_bh && (*ret_bh)) {
1636 ocfs2_meta_unlock(inode, ex);
1647 * This is working around a lock inversion between tasks acquiring DLM locks
1648 * while holding a page lock and the vote thread which blocks dlm lock acquiry
1649 * while acquiring page locks.
1651 * ** These _with_page variantes are only intended to be called from aop
1652 * methods that hold page locks and return a very specific *positive* error
1653 * code that aop methods pass up to the VFS -- test for errors with != 0. **
1655 * The DLM is called such that it returns -EAGAIN if it would have blocked
1656 * waiting for the vote thread. In that case we unlock our page so the vote
1657 * thread can make progress. Once we've done this we have to return
1658 * AOP_TRUNCATED_PAGE so the aop method that called us can bubble that back up
1659 * into the VFS who will then immediately retry the aop call.
1661 * We do a blocking lock and immediate unlock before returning, though, so that
1662 * the lock has a great chance of being cached on this node by the time the VFS
1663 * calls back to retry the aop. This has a potential to livelock as nodes
1664 * ping locks back and forth, but that's a risk we're willing to take to avoid
1665 * the lock inversion simply.
1667 int ocfs2_meta_lock_with_page(struct inode *inode,
1668 struct ocfs2_journal_handle *handle,
1669 struct buffer_head **ret_bh,
1675 ret = ocfs2_meta_lock_full(inode, handle, ret_bh, ex,
1676 OCFS2_LOCK_NONBLOCK);
1677 if (ret == -EAGAIN) {
1679 if (ocfs2_meta_lock(inode, handle, ret_bh, ex) == 0)
1680 ocfs2_meta_unlock(inode, ex);
1681 ret = AOP_TRUNCATED_PAGE;
1687 void ocfs2_meta_unlock(struct inode *inode,
1690 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1691 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
1695 mlog(0, "inode %llu drop %s META lock\n",
1696 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1697 ex ? "EXMODE" : "PRMODE");
1699 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1700 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1705 int ocfs2_super_lock(struct ocfs2_super *osb,
1709 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1710 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1711 struct buffer_head *bh;
1712 struct ocfs2_slot_info *si = osb->slot_info;
1716 if (ocfs2_is_hard_readonly(osb))
1719 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
1725 /* The super block lock path is really in the best position to
1726 * know when resources covered by the lock need to be
1727 * refreshed, so we do it here. Of course, making sense of
1728 * everything is up to the caller :) */
1729 status = ocfs2_should_refresh_lock_res(lockres);
1736 status = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0,
1739 ocfs2_update_slot_info(si);
1741 ocfs2_complete_lock_res_refresh(lockres, status);
1751 void ocfs2_super_unlock(struct ocfs2_super *osb,
1754 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1755 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1757 ocfs2_cluster_unlock(osb, lockres, level);
1760 int ocfs2_rename_lock(struct ocfs2_super *osb)
1763 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1765 if (ocfs2_is_hard_readonly(osb))
1768 status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
1775 void ocfs2_rename_unlock(struct ocfs2_super *osb)
1777 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1779 ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
1782 int ocfs2_dentry_lock(struct dentry *dentry, int ex)
1785 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1786 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1787 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1791 if (ocfs2_is_hard_readonly(osb))
1794 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
1801 void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
1803 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1804 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1805 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1807 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
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", OCFS2_DLM_DEBUG_STR_VERSION);
1942 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
1943 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
1945 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
1947 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
1949 seq_printf(m, "%d\t"
1960 lockres->l_unlock_action,
1961 lockres->l_ro_holders,
1962 lockres->l_ex_holders,
1963 lockres->l_requested,
1964 lockres->l_blocking);
1966 /* Dump the raw LVB */
1967 lvb = lockres->l_lksb.lvb;
1968 for(i = 0; i < DLM_LVB_LEN; i++)
1969 seq_printf(m, "0x%x\t", lvb[i]);
1972 seq_printf(m, "\n");
1976 static struct seq_operations ocfs2_dlm_seq_ops = {
1977 .start = ocfs2_dlm_seq_start,
1978 .stop = ocfs2_dlm_seq_stop,
1979 .next = ocfs2_dlm_seq_next,
1980 .show = ocfs2_dlm_seq_show,
1983 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
1985 struct seq_file *seq = (struct seq_file *) file->private_data;
1986 struct ocfs2_dlm_seq_priv *priv = seq->private;
1987 struct ocfs2_lock_res *res = &priv->p_iter_res;
1989 ocfs2_remove_lockres_tracking(res);
1990 ocfs2_put_dlm_debug(priv->p_dlm_debug);
1991 return seq_release_private(inode, file);
1994 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
1997 struct ocfs2_dlm_seq_priv *priv;
1998 struct seq_file *seq;
1999 struct ocfs2_super *osb;
2001 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2007 osb = (struct ocfs2_super *) inode->u.generic_ip;
2008 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2009 priv->p_dlm_debug = osb->osb_dlm_debug;
2010 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2012 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2019 seq = (struct seq_file *) file->private_data;
2020 seq->private = priv;
2022 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2029 static const struct file_operations ocfs2_dlm_debug_fops = {
2030 .open = ocfs2_dlm_debug_open,
2031 .release = ocfs2_dlm_debug_release,
2033 .llseek = seq_lseek,
2036 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2039 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2041 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2043 osb->osb_debug_root,
2045 &ocfs2_dlm_debug_fops);
2046 if (!dlm_debug->d_locking_state) {
2049 "Unable to create locking state debugfs file.\n");
2053 ocfs2_get_dlm_debug(dlm_debug);
2058 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2060 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2063 debugfs_remove(dlm_debug->d_locking_state);
2064 ocfs2_put_dlm_debug(dlm_debug);
2068 int ocfs2_dlm_init(struct ocfs2_super *osb)
2072 struct dlm_ctxt *dlm;
2076 status = ocfs2_dlm_init_debug(osb);
2082 /* launch vote thread */
2083 osb->vote_task = kthread_run(ocfs2_vote_thread, osb, "ocfs2vote");
2084 if (IS_ERR(osb->vote_task)) {
2085 status = PTR_ERR(osb->vote_task);
2086 osb->vote_task = NULL;
2091 /* used by the dlm code to make message headers unique, each
2092 * node in this domain must agree on this. */
2093 dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
2095 /* for now, uuid == domain */
2096 dlm = dlm_register_domain(osb->uuid_str, dlm_key);
2098 status = PTR_ERR(dlm);
2103 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2104 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2106 dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
2113 ocfs2_dlm_shutdown_debug(osb);
2115 kthread_stop(osb->vote_task);
2122 void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2126 dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
2128 ocfs2_drop_osb_locks(osb);
2130 if (osb->vote_task) {
2131 kthread_stop(osb->vote_task);
2132 osb->vote_task = NULL;
2135 ocfs2_lock_res_free(&osb->osb_super_lockres);
2136 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2138 dlm_unregister_domain(osb->dlm);
2141 ocfs2_dlm_shutdown_debug(osb);
2146 static void ocfs2_unlock_ast(void *opaque, enum dlm_status status)
2148 struct ocfs2_lock_res *lockres = opaque;
2149 unsigned long flags;
2153 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2154 lockres->l_unlock_action);
2156 spin_lock_irqsave(&lockres->l_lock, flags);
2157 /* We tried to cancel a convert request, but it was already
2158 * granted. All we want to do here is clear our unlock
2159 * state. The wake_up call done at the bottom is redundant
2160 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2161 * hurt anything anyway */
2162 if (status == DLM_CANCELGRANT &&
2163 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2164 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2166 /* We don't clear the busy flag in this case as it
2167 * should have been cleared by the ast which the dlm
2169 goto complete_unlock;
2172 if (status != DLM_NORMAL) {
2173 mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
2174 "unlock_action %d\n", status, lockres->l_name,
2175 lockres->l_unlock_action);
2176 spin_unlock_irqrestore(&lockres->l_lock, flags);
2180 switch(lockres->l_unlock_action) {
2181 case OCFS2_UNLOCK_CANCEL_CONVERT:
2182 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2183 lockres->l_action = OCFS2_AST_INVALID;
2185 case OCFS2_UNLOCK_DROP_LOCK:
2186 lockres->l_level = LKM_IVMODE;
2192 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2194 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2195 spin_unlock_irqrestore(&lockres->l_lock, flags);
2197 wake_up(&lockres->l_event);
2202 typedef void (ocfs2_pre_drop_cb_t)(struct ocfs2_lock_res *, void *);
2204 struct drop_lock_cb {
2205 ocfs2_pre_drop_cb_t *drop_func;
2209 static int ocfs2_drop_lock(struct ocfs2_super *osb,
2210 struct ocfs2_lock_res *lockres,
2211 struct drop_lock_cb *dcb)
2213 enum dlm_status status;
2214 unsigned long flags;
2216 /* We didn't get anywhere near actually using this lockres. */
2217 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2220 spin_lock_irqsave(&lockres->l_lock, flags);
2222 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2223 "lockres %s, flags 0x%lx\n",
2224 lockres->l_name, lockres->l_flags);
2226 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2227 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2228 "%u, unlock_action = %u\n",
2229 lockres->l_name, lockres->l_flags, lockres->l_action,
2230 lockres->l_unlock_action);
2232 spin_unlock_irqrestore(&lockres->l_lock, flags);
2234 /* XXX: Today we just wait on any busy
2235 * locks... Perhaps we need to cancel converts in the
2237 ocfs2_wait_on_busy_lock(lockres);
2239 spin_lock_irqsave(&lockres->l_lock, flags);
2243 dcb->drop_func(lockres, dcb->drop_data);
2245 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2246 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2248 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2249 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2251 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2252 spin_unlock_irqrestore(&lockres->l_lock, flags);
2256 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2258 /* make sure we never get here while waiting for an ast to
2260 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2262 /* is this necessary? */
2263 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2264 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2265 spin_unlock_irqrestore(&lockres->l_lock, flags);
2267 mlog(0, "lock %s\n", lockres->l_name);
2269 status = dlmunlock(osb->dlm, &lockres->l_lksb, LKM_VALBLK,
2270 ocfs2_unlock_ast, lockres);
2271 if (status != DLM_NORMAL) {
2272 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2273 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2274 dlm_print_one_lock(lockres->l_lksb.lockid);
2277 mlog(0, "lock %s, successfull return from dlmunlock\n",
2280 ocfs2_wait_on_busy_lock(lockres);
2286 /* Mark the lockres as being dropped. It will no longer be
2287 * queued if blocking, but we still may have to wait on it
2288 * being dequeued from the vote thread before we can consider
2291 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2292 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2295 struct ocfs2_mask_waiter mw;
2296 unsigned long flags;
2298 ocfs2_init_mask_waiter(&mw);
2300 spin_lock_irqsave(&lockres->l_lock, flags);
2301 lockres->l_flags |= OCFS2_LOCK_FREEING;
2302 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2303 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2304 spin_unlock_irqrestore(&lockres->l_lock, flags);
2306 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2308 status = ocfs2_wait_for_mask(&mw);
2312 spin_lock_irqsave(&lockres->l_lock, flags);
2314 spin_unlock_irqrestore(&lockres->l_lock, flags);
2317 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2318 struct ocfs2_lock_res *lockres)
2322 ocfs2_mark_lockres_freeing(lockres);
2323 ret = ocfs2_drop_lock(osb, lockres, NULL);
2328 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2330 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2331 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
2334 static void ocfs2_meta_pre_drop(struct ocfs2_lock_res *lockres, void *data)
2336 struct inode *inode = data;
2338 /* the metadata lock requires a bit more work as we have an
2339 * LVB to worry about. */
2340 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2341 lockres->l_level == LKM_EXMODE &&
2342 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2343 __ocfs2_stuff_meta_lvb(inode);
2346 int ocfs2_drop_inode_locks(struct inode *inode)
2349 struct drop_lock_cb meta_dcb = { ocfs2_meta_pre_drop, inode, };
2353 /* No need to call ocfs2_mark_lockres_freeing here -
2354 * ocfs2_clear_inode has done it for us. */
2356 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2357 &OCFS2_I(inode)->ip_data_lockres,
2364 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2365 &OCFS2_I(inode)->ip_meta_lockres,
2369 if (err < 0 && !status)
2372 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2373 &OCFS2_I(inode)->ip_rw_lockres,
2377 if (err < 0 && !status)
2384 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2387 assert_spin_locked(&lockres->l_lock);
2389 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
2391 if (lockres->l_level <= new_level) {
2392 mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
2393 lockres->l_level, new_level);
2397 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2398 lockres->l_name, new_level, lockres->l_blocking);
2400 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2401 lockres->l_requested = new_level;
2402 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2405 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2406 struct ocfs2_lock_res *lockres,
2410 int ret, dlm_flags = LKM_CONVERT;
2411 enum dlm_status status;
2416 dlm_flags |= LKM_VALBLK;
2418 status = dlmlock(osb->dlm,
2423 OCFS2_LOCK_ID_MAX_LEN - 1,
2426 ocfs2_blocking_ast);
2427 if (status != DLM_NORMAL) {
2428 ocfs2_log_dlm_error("dlmlock", status, lockres);
2430 ocfs2_recover_from_dlm_error(lockres, 1);
2440 /* returns 1 when the caller should unlock and call dlmunlock */
2441 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2442 struct ocfs2_lock_res *lockres)
2444 assert_spin_locked(&lockres->l_lock);
2447 mlog(0, "lock %s\n", lockres->l_name);
2449 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2450 /* If we're already trying to cancel a lock conversion
2451 * then just drop the spinlock and allow the caller to
2452 * requeue this lock. */
2454 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2458 /* were we in a convert when we got the bast fire? */
2459 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2460 lockres->l_action != OCFS2_AST_DOWNCONVERT);
2461 /* set things up for the unlockast to know to just
2462 * clear out the ast_action and unset busy, etc. */
2463 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2465 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2466 "lock %s, invalid flags: 0x%lx\n",
2467 lockres->l_name, lockres->l_flags);
2472 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2473 struct ocfs2_lock_res *lockres)
2476 enum dlm_status status;
2479 mlog(0, "lock %s\n", lockres->l_name);
2482 status = dlmunlock(osb->dlm,
2487 if (status != DLM_NORMAL) {
2488 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2490 ocfs2_recover_from_dlm_error(lockres, 0);
2493 mlog(0, "lock %s return from dlmunlock\n", lockres->l_name);
2499 static inline int ocfs2_can_downconvert_meta_lock(struct inode *inode,
2500 struct ocfs2_lock_res *lockres,
2507 BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
2509 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2511 mlog(0, "lockres %s currently being refreshed -- backing "
2512 "off!\n", lockres->l_name);
2513 } else if (new_level == LKM_PRMODE)
2514 ret = !lockres->l_ex_holders &&
2515 ocfs2_inode_fully_checkpointed(inode);
2516 else /* Must be NLMODE we're converting to. */
2517 ret = !lockres->l_ro_holders && !lockres->l_ex_holders &&
2518 ocfs2_inode_fully_checkpointed(inode);
2524 static int ocfs2_do_unblock_meta(struct inode *inode,
2530 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
2531 unsigned long flags;
2533 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2537 spin_lock_irqsave(&lockres->l_lock, flags);
2539 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2541 mlog(0, "l_level=%d, l_blocking=%d\n", lockres->l_level,
2542 lockres->l_blocking);
2544 BUG_ON(lockres->l_level != LKM_EXMODE &&
2545 lockres->l_level != LKM_PRMODE);
2547 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2549 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2550 spin_unlock_irqrestore(&lockres->l_lock, flags);
2552 ret = ocfs2_cancel_convert(osb, lockres);
2559 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2561 mlog(0, "l_level=%d, l_blocking=%d, new_level=%d\n",
2562 lockres->l_level, lockres->l_blocking, new_level);
2564 if (ocfs2_can_downconvert_meta_lock(inode, lockres, new_level)) {
2565 if (lockres->l_level == LKM_EXMODE)
2568 /* If the lock hasn't been refreshed yet (rare), then
2569 * our memory inode values are old and we skip
2570 * stuffing the lvb. There's no need to actually clear
2571 * out the lvb here as it's value is still valid. */
2572 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2574 __ocfs2_stuff_meta_lvb(inode);
2576 mlog(0, "lockres %s: downconverting stale lock!\n",
2579 mlog(0, "calling ocfs2_downconvert_lock with l_level=%d, "
2580 "l_blocking=%d, new_level=%d\n",
2581 lockres->l_level, lockres->l_blocking, new_level);
2583 ocfs2_prepare_downconvert(lockres, new_level);
2584 spin_unlock_irqrestore(&lockres->l_lock, flags);
2585 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
2588 if (!ocfs2_inode_fully_checkpointed(inode))
2589 ocfs2_start_checkpoint(osb);
2592 spin_unlock_irqrestore(&lockres->l_lock, flags);
2599 static int ocfs2_generic_unblock_lock(struct ocfs2_super *osb,
2600 struct ocfs2_lock_res *lockres,
2601 struct ocfs2_unblock_ctl *ctl,
2602 ocfs2_convert_worker_t *worker)
2604 unsigned long flags;
2611 spin_lock_irqsave(&lockres->l_lock, flags);
2613 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2616 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2618 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2619 spin_unlock_irqrestore(&lockres->l_lock, flags);
2621 ret = ocfs2_cancel_convert(osb, lockres);
2628 /* if we're blocking an exclusive and we have *any* holders,
2630 if ((lockres->l_blocking == LKM_EXMODE)
2631 && (lockres->l_ex_holders || lockres->l_ro_holders)) {
2632 spin_unlock_irqrestore(&lockres->l_lock, flags);
2638 /* If it's a PR we're blocking, then only
2639 * requeue if we've got any EX holders */
2640 if (lockres->l_blocking == LKM_PRMODE &&
2641 lockres->l_ex_holders) {
2642 spin_unlock_irqrestore(&lockres->l_lock, flags);
2648 /* If we get here, then we know that there are no more
2649 * incompatible holders (and anyone asking for an incompatible
2650 * lock is blocked). We can now downconvert the lock */
2654 /* Some lockres types want to do a bit of work before
2655 * downconverting a lock. Allow that here. The worker function
2656 * may sleep, so we save off a copy of what we're blocking as
2657 * it may change while we're not holding the spin lock. */
2658 blocking = lockres->l_blocking;
2659 spin_unlock_irqrestore(&lockres->l_lock, flags);
2661 ctl->unblock_action = worker(lockres, blocking);
2663 if (ctl->unblock_action == UNBLOCK_STOP_POST)
2666 spin_lock_irqsave(&lockres->l_lock, flags);
2667 if (blocking != lockres->l_blocking) {
2668 /* If this changed underneath us, then we can't drop
2675 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2677 ocfs2_prepare_downconvert(lockres, new_level);
2678 spin_unlock_irqrestore(&lockres->l_lock, flags);
2679 ret = ocfs2_downconvert_lock(osb, lockres, new_level, 0);
2685 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
2688 struct inode *inode;
2689 struct address_space *mapping;
2691 inode = ocfs2_lock_res_inode(lockres);
2692 mapping = inode->i_mapping;
2694 if (filemap_fdatawrite(mapping)) {
2695 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
2696 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2698 sync_mapping_buffers(mapping);
2699 if (blocking == LKM_EXMODE) {
2700 truncate_inode_pages(mapping, 0);
2701 unmap_mapping_range(mapping, 0, 0, 0);
2703 /* We only need to wait on the I/O if we're not also
2704 * truncating pages because truncate_inode_pages waits
2705 * for us above. We don't truncate pages if we're
2706 * blocking anything < EXMODE because we want to keep
2707 * them around in that case. */
2708 filemap_fdatawait(mapping);
2711 return UNBLOCK_CONTINUE;
2714 int ocfs2_unblock_data(struct ocfs2_lock_res *lockres,
2715 struct ocfs2_unblock_ctl *ctl)
2718 struct inode *inode;
2719 struct ocfs2_super *osb;
2723 inode = ocfs2_lock_res_inode(lockres);
2724 osb = OCFS2_SB(inode->i_sb);
2726 mlog(0, "unblock inode %llu\n",
2727 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2729 status = ocfs2_generic_unblock_lock(osb, lockres, ctl,
2730 ocfs2_data_convert_worker);
2734 mlog(0, "inode %llu, requeue = %d\n",
2735 (unsigned long long)OCFS2_I(inode)->ip_blkno, ctl->requeue);
2741 static int ocfs2_unblock_inode_lock(struct ocfs2_lock_res *lockres,
2742 struct ocfs2_unblock_ctl *ctl)
2745 struct inode *inode;
2749 mlog(0, "Unblock lockres %s\n", lockres->l_name);
2751 inode = ocfs2_lock_res_inode(lockres);
2753 status = ocfs2_generic_unblock_lock(OCFS2_SB(inode->i_sb),
2754 lockres, ctl, NULL);
2762 static int ocfs2_unblock_meta(struct ocfs2_lock_res *lockres,
2763 struct ocfs2_unblock_ctl *ctl)
2766 struct inode *inode;
2770 inode = ocfs2_lock_res_inode(lockres);
2772 mlog(0, "unblock inode %llu\n",
2773 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2775 status = ocfs2_do_unblock_meta(inode, &ctl->requeue);
2779 mlog(0, "inode %llu, requeue = %d\n",
2780 (unsigned long long)OCFS2_I(inode)->ip_blkno, ctl->requeue);
2787 * Does the final reference drop on our dentry lock. Right now this
2788 * happens in the vote thread, but we could choose to simplify the
2789 * dlmglue API and push these off to the ocfs2_wq in the future.
2791 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
2792 struct ocfs2_lock_res *lockres)
2794 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2795 ocfs2_dentry_lock_put(osb, dl);
2799 * d_delete() matching dentries before the lock downconvert.
2801 * At this point, any process waiting to destroy the
2802 * dentry_lock due to last ref count is stopped by the
2803 * OCFS2_LOCK_QUEUED flag.
2805 * We have two potential problems
2807 * 1) If we do the last reference drop on our dentry_lock (via dput)
2808 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
2809 * the downconvert to finish. Instead we take an elevated
2810 * reference and push the drop until after we've completed our
2811 * unblock processing.
2813 * 2) There might be another process with a final reference,
2814 * waiting on us to finish processing. If this is the case, we
2815 * detect it and exit out - there's no more dentries anyway.
2817 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
2820 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2821 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
2822 struct dentry *dentry;
2823 unsigned long flags;
2827 * This node is blocking another node from getting a read
2828 * lock. This happens when we've renamed within a
2829 * directory. We've forced the other nodes to d_delete(), but
2830 * we never actually dropped our lock because it's still
2831 * valid. The downconvert code will retain a PR for this node,
2832 * so there's no further work to do.
2834 if (blocking == LKM_PRMODE)
2835 return UNBLOCK_CONTINUE;
2838 * Mark this inode as potentially orphaned. The code in
2839 * ocfs2_delete_inode() will figure out whether it actually
2840 * needs to be freed or not.
2842 spin_lock(&oi->ip_lock);
2843 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
2844 spin_unlock(&oi->ip_lock);
2847 * Yuck. We need to make sure however that the check of
2848 * OCFS2_LOCK_FREEING and the extra reference are atomic with
2849 * respect to a reference decrement or the setting of that
2852 spin_lock_irqsave(&lockres->l_lock, flags);
2853 spin_lock(&dentry_attach_lock);
2854 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
2859 spin_unlock(&dentry_attach_lock);
2860 spin_unlock_irqrestore(&lockres->l_lock, flags);
2862 mlog(0, "extra_ref = %d\n", extra_ref);
2865 * We have a process waiting on us in ocfs2_dentry_iput(),
2866 * which means we can't have any more outstanding
2867 * aliases. There's no need to do any more work.
2870 return UNBLOCK_CONTINUE;
2872 spin_lock(&dentry_attach_lock);
2874 dentry = ocfs2_find_local_alias(dl->dl_inode,
2875 dl->dl_parent_blkno, 1);
2878 spin_unlock(&dentry_attach_lock);
2880 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
2881 dentry->d_name.name);
2884 * The following dcache calls may do an
2885 * iput(). Normally we don't want that from the
2886 * downconverting thread, but in this case it's ok
2887 * because the requesting node already has an
2888 * exclusive lock on the inode, so it can't be queued
2889 * for a downconvert.
2894 spin_lock(&dentry_attach_lock);
2896 spin_unlock(&dentry_attach_lock);
2899 * If we are the last holder of this dentry lock, there is no
2900 * reason to downconvert so skip straight to the unlock.
2902 if (dl->dl_count == 1)
2903 return UNBLOCK_STOP_POST;
2905 return UNBLOCK_CONTINUE_POST;
2908 static int ocfs2_unblock_dentry_lock(struct ocfs2_lock_res *lockres,
2909 struct ocfs2_unblock_ctl *ctl)
2912 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2913 struct ocfs2_super *osb = OCFS2_SB(dl->dl_inode->i_sb);
2915 mlog(0, "unblock dentry lock: %llu\n",
2916 (unsigned long long)OCFS2_I(dl->dl_inode)->ip_blkno);
2918 ret = ocfs2_generic_unblock_lock(osb,
2921 ocfs2_dentry_convert_worker);
2925 mlog(0, "requeue = %d, post = %d\n", ctl->requeue, ctl->unblock_action);
2930 /* Generic unblock function for any lockres whose private data is an
2931 * ocfs2_super pointer. */
2932 static int ocfs2_unblock_osb_lock(struct ocfs2_lock_res *lockres,
2933 struct ocfs2_unblock_ctl *ctl)
2936 struct ocfs2_super *osb;
2940 mlog(0, "Unblock lockres %s\n", lockres->l_name);
2942 osb = ocfs2_get_lockres_osb(lockres);
2944 status = ocfs2_generic_unblock_lock(osb,
2955 void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
2956 struct ocfs2_lock_res *lockres)
2959 struct ocfs2_unblock_ctl ctl = {0, 0,};
2960 unsigned long flags;
2962 /* Our reference to the lockres in this function can be
2963 * considered valid until we remove the OCFS2_LOCK_QUEUED
2969 BUG_ON(!lockres->l_ops);
2970 BUG_ON(!lockres->l_ops->unblock);
2972 mlog(0, "lockres %s blocked.\n", lockres->l_name);
2974 /* Detect whether a lock has been marked as going away while
2975 * the vote thread was processing other things. A lock can
2976 * still be marked with OCFS2_LOCK_FREEING after this check,
2977 * but short circuiting here will still save us some
2979 spin_lock_irqsave(&lockres->l_lock, flags);
2980 if (lockres->l_flags & OCFS2_LOCK_FREEING)
2982 spin_unlock_irqrestore(&lockres->l_lock, flags);
2984 status = lockres->l_ops->unblock(lockres, &ctl);
2988 spin_lock_irqsave(&lockres->l_lock, flags);
2990 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
2991 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
2993 ocfs2_schedule_blocked_lock(osb, lockres);
2995 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
2996 ctl.requeue ? "yes" : "no");
2997 spin_unlock_irqrestore(&lockres->l_lock, flags);
2999 if (ctl.unblock_action != UNBLOCK_CONTINUE
3000 && lockres->l_ops->post_unlock)
3001 lockres->l_ops->post_unlock(osb, lockres);
3006 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3007 struct ocfs2_lock_res *lockres)
3011 assert_spin_locked(&lockres->l_lock);
3013 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3014 /* Do not schedule a lock for downconvert when it's on
3015 * the way to destruction - any nodes wanting access
3016 * to the resource will get it soon. */
3017 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3018 lockres->l_name, lockres->l_flags);
3022 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3024 spin_lock(&osb->vote_task_lock);
3025 if (list_empty(&lockres->l_blocked_list)) {
3026 list_add_tail(&lockres->l_blocked_list,
3027 &osb->blocked_lock_list);
3028 osb->blocked_lock_count++;
3030 spin_unlock(&osb->vote_task_lock);
3035 /* This aids in debugging situations where a bad LVB might be involved. */
3036 void ocfs2_dump_meta_lvb_info(u64 level,
3037 const char *function,
3039 struct ocfs2_lock_res *lockres)
3041 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
3043 mlog(level, "LVB information for %s (called from %s:%u):\n",
3044 lockres->l_name, function, line);
3045 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
3046 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
3047 be32_to_cpu(lvb->lvb_igeneration));
3048 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
3049 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
3050 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
3051 be16_to_cpu(lvb->lvb_imode));
3052 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
3053 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
3054 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
3055 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
3056 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
3057 be32_to_cpu(lvb->lvb_iattr));