1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Code which implements an OCFS2 specific interface to our DLM.
8 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
26 #include <linux/types.h>
27 #include <linux/slab.h>
28 #include <linux/highmem.h>
30 #include <linux/smp_lock.h>
31 #include <linux/crc32.h>
32 #include <linux/kthread.h>
33 #include <linux/pagemap.h>
34 #include <linux/debugfs.h>
35 #include <linux/seq_file.h>
37 #include <cluster/heartbeat.h>
38 #include <cluster/nodemanager.h>
39 #include <cluster/tcp.h>
41 #include <dlm/dlmapi.h>
43 #define MLOG_MASK_PREFIX ML_DLM_GLUE
44 #include <cluster/masklog.h>
51 #include "extent_map.h"
52 #include "heartbeat.h"
60 #include "buffer_head_io.h"
62 struct ocfs2_mask_waiter {
63 struct list_head mw_item;
65 struct completion mw_complete;
66 unsigned long mw_mask;
67 unsigned long mw_goal;
70 static void ocfs2_inode_bast_func(void *opaque,
72 static void ocfs2_dentry_bast_func(void *opaque,
74 static void ocfs2_super_bast_func(void *opaque,
76 static void ocfs2_rename_bast_func(void *opaque,
80 * Return value from ocfs2_convert_worker_t functions.
82 * These control the precise actions of ocfs2_generic_unblock_lock()
83 * and ocfs2_process_blocked_lock()
86 enum ocfs2_unblock_action {
87 UNBLOCK_CONTINUE = 0, /* Continue downconvert */
88 UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire
89 * ->post_unlock callback */
90 UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire
91 * ->post_unlock() callback. */
94 struct ocfs2_unblock_ctl {
96 enum ocfs2_unblock_action unblock_action;
99 /* so far, all locks have gotten along with the same unlock ast */
100 static void ocfs2_unlock_ast_func(void *opaque,
101 enum dlm_status status);
102 static int ocfs2_unblock_meta(struct ocfs2_lock_res *lockres,
103 struct ocfs2_unblock_ctl *ctl);
104 static int ocfs2_unblock_data(struct ocfs2_lock_res *lockres,
105 struct ocfs2_unblock_ctl *ctl);
106 static int ocfs2_unblock_inode_lock(struct ocfs2_lock_res *lockres,
107 struct ocfs2_unblock_ctl *ctl);
108 static int ocfs2_unblock_dentry_lock(struct ocfs2_lock_res *lockres,
109 struct ocfs2_unblock_ctl *ctl);
110 static int ocfs2_unblock_osb_lock(struct ocfs2_lock_res *lockres,
111 struct ocfs2_unblock_ctl *ctl);
113 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
114 struct ocfs2_lock_res *lockres);
117 * OCFS2 Lock Resource Operations
119 * These fine tune the behavior of the generic dlmglue locking infrastructure.
121 struct ocfs2_lock_res_ops {
122 void (*bast)(void *, int);
123 void (*unlock_ast)(void *, enum dlm_status);
124 int (*unblock)(struct ocfs2_lock_res *, struct ocfs2_unblock_ctl *);
125 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
128 * LOCK_TYPE_* flags which describe the specific requirements
129 * of a lock type. Descriptions of each individual flag follow.
135 * Some locks want to "refresh" potentially stale data when a
136 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
137 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
138 * individual lockres l_flags member from the ast function. It is
139 * expected that the locking wrapper will clear the
140 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
142 #define LOCK_TYPE_REQUIRES_REFRESH 0x1
144 typedef int (ocfs2_convert_worker_t)(struct ocfs2_lock_res *, int);
145 static int ocfs2_generic_unblock_lock(struct ocfs2_super *osb,
146 struct ocfs2_lock_res *lockres,
147 struct ocfs2_unblock_ctl *ctl,
148 ocfs2_convert_worker_t *worker);
150 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
151 .bast = ocfs2_inode_bast_func,
152 .unlock_ast = ocfs2_unlock_ast_func,
153 .unblock = ocfs2_unblock_inode_lock,
157 static struct ocfs2_lock_res_ops ocfs2_inode_meta_lops = {
158 .bast = ocfs2_inode_bast_func,
159 .unlock_ast = ocfs2_unlock_ast_func,
160 .unblock = ocfs2_unblock_meta,
161 .flags = LOCK_TYPE_REQUIRES_REFRESH,
164 static struct ocfs2_lock_res_ops ocfs2_inode_data_lops = {
165 .bast = ocfs2_inode_bast_func,
166 .unlock_ast = ocfs2_unlock_ast_func,
167 .unblock = ocfs2_unblock_data,
171 static struct ocfs2_lock_res_ops ocfs2_super_lops = {
172 .bast = ocfs2_super_bast_func,
173 .unlock_ast = ocfs2_unlock_ast_func,
174 .unblock = ocfs2_unblock_osb_lock,
175 .flags = LOCK_TYPE_REQUIRES_REFRESH,
178 static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
179 .bast = ocfs2_rename_bast_func,
180 .unlock_ast = ocfs2_unlock_ast_func,
181 .unblock = ocfs2_unblock_osb_lock,
185 static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
186 .bast = ocfs2_dentry_bast_func,
187 .unlock_ast = ocfs2_unlock_ast_func,
188 .unblock = ocfs2_unblock_dentry_lock,
189 .post_unlock = ocfs2_dentry_post_unlock,
193 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
195 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
196 lockres->l_type == OCFS2_LOCK_TYPE_DATA ||
197 lockres->l_type == OCFS2_LOCK_TYPE_RW;
200 static inline int ocfs2_is_super_lock(struct ocfs2_lock_res *lockres)
202 return lockres->l_type == OCFS2_LOCK_TYPE_SUPER;
205 static inline int ocfs2_is_rename_lock(struct ocfs2_lock_res *lockres)
207 return lockres->l_type == OCFS2_LOCK_TYPE_RENAME;
210 static inline struct ocfs2_super *ocfs2_lock_res_super(struct ocfs2_lock_res *lockres)
212 BUG_ON(!ocfs2_is_super_lock(lockres)
213 && !ocfs2_is_rename_lock(lockres));
215 return (struct ocfs2_super *) lockres->l_priv;
218 static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
220 BUG_ON(!ocfs2_is_inode_lock(lockres));
222 return (struct inode *) lockres->l_priv;
225 static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
227 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
229 return (struct ocfs2_dentry_lock *)lockres->l_priv;
232 static int ocfs2_lock_create(struct ocfs2_super *osb,
233 struct ocfs2_lock_res *lockres,
236 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
238 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
239 struct ocfs2_lock_res *lockres,
241 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
242 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
243 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
244 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
245 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
246 struct ocfs2_lock_res *lockres);
247 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
249 #define ocfs2_log_dlm_error(_func, _stat, _lockres) do { \
250 mlog(ML_ERROR, "Dlm error \"%s\" while calling %s on " \
251 "resource %s: %s\n", dlm_errname(_stat), _func, \
252 _lockres->l_name, dlm_errmsg(_stat)); \
254 static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
255 struct ocfs2_lock_res *lockres);
256 static int ocfs2_meta_lock_update(struct inode *inode,
257 struct buffer_head **bh);
258 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
259 static inline int ocfs2_highest_compat_lock_level(int level);
260 static inline int ocfs2_can_downconvert_meta_lock(struct inode *inode,
261 struct ocfs2_lock_res *lockres,
264 static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
273 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
275 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
276 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
277 (long long)blkno, generation);
279 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
281 mlog(0, "built lock resource with name: %s\n", name);
286 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
288 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
289 struct ocfs2_dlm_debug *dlm_debug)
291 mlog(0, "Add tracking for lockres %s\n", res->l_name);
293 spin_lock(&ocfs2_dlm_tracking_lock);
294 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
295 spin_unlock(&ocfs2_dlm_tracking_lock);
298 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
300 spin_lock(&ocfs2_dlm_tracking_lock);
301 if (!list_empty(&res->l_debug_list))
302 list_del_init(&res->l_debug_list);
303 spin_unlock(&ocfs2_dlm_tracking_lock);
306 static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
307 struct ocfs2_lock_res *res,
308 enum ocfs2_lock_type type,
309 struct ocfs2_lock_res_ops *ops,
316 res->l_level = LKM_IVMODE;
317 res->l_requested = LKM_IVMODE;
318 res->l_blocking = LKM_IVMODE;
319 res->l_action = OCFS2_AST_INVALID;
320 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
322 res->l_flags = OCFS2_LOCK_INITIALIZED;
324 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
327 void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
329 /* This also clears out the lock status block */
330 memset(res, 0, sizeof(struct ocfs2_lock_res));
331 spin_lock_init(&res->l_lock);
332 init_waitqueue_head(&res->l_event);
333 INIT_LIST_HEAD(&res->l_blocked_list);
334 INIT_LIST_HEAD(&res->l_mask_waiters);
337 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
338 enum ocfs2_lock_type type,
339 unsigned int generation,
342 struct ocfs2_lock_res_ops *ops;
345 case OCFS2_LOCK_TYPE_RW:
346 ops = &ocfs2_inode_rw_lops;
348 case OCFS2_LOCK_TYPE_META:
349 ops = &ocfs2_inode_meta_lops;
351 case OCFS2_LOCK_TYPE_DATA:
352 ops = &ocfs2_inode_data_lops;
355 mlog_bug_on_msg(1, "type: %d\n", type);
356 ops = NULL; /* thanks, gcc */
360 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
361 generation, res->l_name);
362 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
365 static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
367 __be64 inode_blkno_be;
369 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
372 return be64_to_cpu(inode_blkno_be);
375 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
376 u64 parent, struct inode *inode)
379 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
380 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
381 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
383 ocfs2_lock_res_init_once(lockres);
386 * Unfortunately, the standard lock naming scheme won't work
387 * here because we have two 16 byte values to use. Instead,
388 * we'll stuff the inode number as a binary value. We still
389 * want error prints to show something without garbling the
390 * display, so drop a null byte in there before the inode
391 * number. A future version of OCFS2 will likely use all
392 * binary lock names. The stringified names have been a
393 * tremendous aid in debugging, but now that the debugfs
394 * interface exists, we can mangle things there if need be.
396 * NOTE: We also drop the standard "pad" value (the total lock
397 * name size stays the same though - the last part is all
398 * zeros due to the memset in ocfs2_lock_res_init_once()
400 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
402 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
405 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
407 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
410 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
411 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
415 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
416 struct ocfs2_super *osb)
418 /* Superblock lockres doesn't come from a slab so we call init
419 * once on it manually. */
420 ocfs2_lock_res_init_once(res);
421 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
423 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
424 &ocfs2_super_lops, osb);
427 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
428 struct ocfs2_super *osb)
430 /* Rename lockres doesn't come from a slab so we call init
431 * once on it manually. */
432 ocfs2_lock_res_init_once(res);
433 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
434 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
435 &ocfs2_rename_lops, osb);
438 void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
442 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
445 ocfs2_remove_lockres_tracking(res);
447 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
448 "Lockres %s is on the blocked list\n",
450 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
451 "Lockres %s has mask waiters pending\n",
453 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
454 "Lockres %s is locked\n",
456 mlog_bug_on_msg(res->l_ro_holders,
457 "Lockres %s has %u ro holders\n",
458 res->l_name, res->l_ro_holders);
459 mlog_bug_on_msg(res->l_ex_holders,
460 "Lockres %s has %u ex holders\n",
461 res->l_name, res->l_ex_holders);
463 /* Need to clear out the lock status block for the dlm */
464 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
470 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
479 lockres->l_ex_holders++;
482 lockres->l_ro_holders++;
491 static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
500 BUG_ON(!lockres->l_ex_holders);
501 lockres->l_ex_holders--;
504 BUG_ON(!lockres->l_ro_holders);
505 lockres->l_ro_holders--;
513 /* WARNING: This function lives in a world where the only three lock
514 * levels are EX, PR, and NL. It *will* have to be adjusted when more
515 * lock types are added. */
516 static inline int ocfs2_highest_compat_lock_level(int level)
518 int new_level = LKM_EXMODE;
520 if (level == LKM_EXMODE)
521 new_level = LKM_NLMODE;
522 else if (level == LKM_PRMODE)
523 new_level = LKM_PRMODE;
527 static void lockres_set_flags(struct ocfs2_lock_res *lockres,
528 unsigned long newflags)
530 struct list_head *pos, *tmp;
531 struct ocfs2_mask_waiter *mw;
533 assert_spin_locked(&lockres->l_lock);
535 lockres->l_flags = newflags;
537 list_for_each_safe(pos, tmp, &lockres->l_mask_waiters) {
538 mw = list_entry(pos, struct ocfs2_mask_waiter, mw_item);
539 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
542 list_del_init(&mw->mw_item);
544 complete(&mw->mw_complete);
547 static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
549 lockres_set_flags(lockres, lockres->l_flags | or);
551 static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
554 lockres_set_flags(lockres, lockres->l_flags & ~clear);
557 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
561 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
562 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
563 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
564 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
566 lockres->l_level = lockres->l_requested;
567 if (lockres->l_level <=
568 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
569 lockres->l_blocking = LKM_NLMODE;
570 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
572 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
577 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
581 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
582 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
584 /* Convert from RO to EX doesn't really need anything as our
585 * information is already up to data. Convert from NL to
586 * *anything* however should mark ourselves as needing an
588 if (lockres->l_level == LKM_NLMODE &&
589 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
590 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
592 lockres->l_level = lockres->l_requested;
593 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
598 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
602 BUG_ON((!lockres->l_flags & OCFS2_LOCK_BUSY));
603 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
605 if (lockres->l_requested > LKM_NLMODE &&
606 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
607 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
608 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
610 lockres->l_level = lockres->l_requested;
611 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
612 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
617 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
620 int needs_downconvert = 0;
623 assert_spin_locked(&lockres->l_lock);
625 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
627 if (level > lockres->l_blocking) {
628 /* only schedule a downconvert if we haven't already scheduled
629 * one that goes low enough to satisfy the level we're
630 * blocking. this also catches the case where we get
632 if (ocfs2_highest_compat_lock_level(level) <
633 ocfs2_highest_compat_lock_level(lockres->l_blocking))
634 needs_downconvert = 1;
636 lockres->l_blocking = level;
639 mlog_exit(needs_downconvert);
640 return needs_downconvert;
643 static void ocfs2_generic_bast_func(struct ocfs2_super *osb,
644 struct ocfs2_lock_res *lockres,
647 int needs_downconvert;
652 BUG_ON(level <= LKM_NLMODE);
654 spin_lock_irqsave(&lockres->l_lock, flags);
655 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
656 if (needs_downconvert)
657 ocfs2_schedule_blocked_lock(osb, lockres);
658 spin_unlock_irqrestore(&lockres->l_lock, flags);
660 wake_up(&lockres->l_event);
662 ocfs2_kick_vote_thread(osb);
667 static void ocfs2_inode_bast_func(void *opaque, int level)
669 struct ocfs2_lock_res *lockres = opaque;
671 struct ocfs2_super *osb;
675 BUG_ON(!ocfs2_is_inode_lock(lockres));
677 inode = ocfs2_lock_res_inode(lockres);
678 osb = OCFS2_SB(inode->i_sb);
680 mlog(0, "BAST fired for inode %llu, blocking %d, level %d type %s\n",
681 (unsigned long long)OCFS2_I(inode)->ip_blkno, level,
682 lockres->l_level, ocfs2_lock_type_string(lockres->l_type));
684 ocfs2_generic_bast_func(osb, lockres, level);
689 static void ocfs2_locking_ast(void *opaque)
691 struct ocfs2_lock_res *lockres = opaque;
692 struct dlm_lockstatus *lksb = &lockres->l_lksb;
695 spin_lock_irqsave(&lockres->l_lock, flags);
697 if (lksb->status != DLM_NORMAL) {
698 mlog(ML_ERROR, "lockres %s: lksb status value of %u!\n",
699 lockres->l_name, lksb->status);
700 spin_unlock_irqrestore(&lockres->l_lock, flags);
704 switch(lockres->l_action) {
705 case OCFS2_AST_ATTACH:
706 ocfs2_generic_handle_attach_action(lockres);
707 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
709 case OCFS2_AST_CONVERT:
710 ocfs2_generic_handle_convert_action(lockres);
712 case OCFS2_AST_DOWNCONVERT:
713 ocfs2_generic_handle_downconvert_action(lockres);
716 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
717 "lockres flags = 0x%lx, unlock action: %u\n",
718 lockres->l_name, lockres->l_action, lockres->l_flags,
719 lockres->l_unlock_action);
723 /* set it to something invalid so if we get called again we
725 lockres->l_action = OCFS2_AST_INVALID;
727 wake_up(&lockres->l_event);
728 spin_unlock_irqrestore(&lockres->l_lock, flags);
731 static void ocfs2_super_bast_func(void *opaque,
734 struct ocfs2_lock_res *lockres = opaque;
735 struct ocfs2_super *osb;
738 mlog(0, "Superblock BAST fired\n");
740 BUG_ON(!ocfs2_is_super_lock(lockres));
741 osb = ocfs2_lock_res_super(lockres);
742 ocfs2_generic_bast_func(osb, lockres, level);
747 static void ocfs2_rename_bast_func(void *opaque,
750 struct ocfs2_lock_res *lockres = opaque;
751 struct ocfs2_super *osb;
755 mlog(0, "Rename BAST fired\n");
757 BUG_ON(!ocfs2_is_rename_lock(lockres));
759 osb = ocfs2_lock_res_super(lockres);
760 ocfs2_generic_bast_func(osb, lockres, level);
765 static void ocfs2_dentry_bast_func(void *opaque, int level)
767 struct ocfs2_lock_res *lockres = opaque;
768 struct ocfs2_dentry_lock *dl = lockres->l_priv;
769 struct ocfs2_super *osb = OCFS2_SB(dl->dl_inode->i_sb);
771 mlog(0, "Dentry bast: level: %d, name: %s\n", level,
774 ocfs2_generic_bast_func(osb, lockres, level);
777 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
783 spin_lock_irqsave(&lockres->l_lock, flags);
784 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
786 lockres->l_action = OCFS2_AST_INVALID;
788 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
789 spin_unlock_irqrestore(&lockres->l_lock, flags);
791 wake_up(&lockres->l_event);
795 /* Note: If we detect another process working on the lock (i.e.,
796 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
797 * to do the right thing in that case.
799 static int ocfs2_lock_create(struct ocfs2_super *osb,
800 struct ocfs2_lock_res *lockres,
805 enum dlm_status status;
810 mlog(0, "lock %s, level = %d, flags = %d\n", lockres->l_name, level,
813 spin_lock_irqsave(&lockres->l_lock, flags);
814 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
815 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
816 spin_unlock_irqrestore(&lockres->l_lock, flags);
820 lockres->l_action = OCFS2_AST_ATTACH;
821 lockres->l_requested = level;
822 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
823 spin_unlock_irqrestore(&lockres->l_lock, flags);
825 status = dlmlock(osb->dlm,
830 OCFS2_LOCK_ID_MAX_LEN - 1,
833 lockres->l_ops->bast);
834 if (status != DLM_NORMAL) {
835 ocfs2_log_dlm_error("dlmlock", status, lockres);
837 ocfs2_recover_from_dlm_error(lockres, 1);
840 mlog(0, "lock %s, successfull return from dlmlock\n", lockres->l_name);
847 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
853 spin_lock_irqsave(&lockres->l_lock, flags);
854 ret = lockres->l_flags & flag;
855 spin_unlock_irqrestore(&lockres->l_lock, flags);
860 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
863 wait_event(lockres->l_event,
864 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
867 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
870 wait_event(lockres->l_event,
871 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
874 /* predict what lock level we'll be dropping down to on behalf
875 * of another node, and return true if the currently wanted
876 * level will be compatible with it. */
877 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
880 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
882 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
885 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
887 INIT_LIST_HEAD(&mw->mw_item);
888 init_completion(&mw->mw_complete);
891 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
893 wait_for_completion(&mw->mw_complete);
894 /* Re-arm the completion in case we want to wait on it again */
895 INIT_COMPLETION(mw->mw_complete);
896 return mw->mw_status;
899 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
900 struct ocfs2_mask_waiter *mw,
904 BUG_ON(!list_empty(&mw->mw_item));
906 assert_spin_locked(&lockres->l_lock);
908 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
913 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
914 * if the mask still hadn't reached its goal */
915 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
916 struct ocfs2_mask_waiter *mw)
921 spin_lock_irqsave(&lockres->l_lock, flags);
922 if (!list_empty(&mw->mw_item)) {
923 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
926 list_del_init(&mw->mw_item);
927 init_completion(&mw->mw_complete);
929 spin_unlock_irqrestore(&lockres->l_lock, flags);
935 static int ocfs2_cluster_lock(struct ocfs2_super *osb,
936 struct ocfs2_lock_res *lockres,
941 struct ocfs2_mask_waiter mw;
942 enum dlm_status status;
943 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
944 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
949 ocfs2_init_mask_waiter(&mw);
954 if (catch_signals && signal_pending(current)) {
959 spin_lock_irqsave(&lockres->l_lock, flags);
961 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
962 "Cluster lock called on freeing lockres %s! flags "
963 "0x%lx\n", lockres->l_name, lockres->l_flags);
965 /* We only compare against the currently granted level
966 * here. If the lock is blocked waiting on a downconvert,
967 * we'll get caught below. */
968 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
969 level > lockres->l_level) {
970 /* is someone sitting in dlm_lock? If so, wait on
972 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
977 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
978 /* lock has not been created yet. */
979 spin_unlock_irqrestore(&lockres->l_lock, flags);
981 ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
989 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
990 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
991 /* is the lock is currently blocked on behalf of
993 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
998 if (level > lockres->l_level) {
999 if (lockres->l_action != OCFS2_AST_INVALID)
1000 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1001 lockres->l_name, lockres->l_action);
1003 lockres->l_action = OCFS2_AST_CONVERT;
1004 lockres->l_requested = level;
1005 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1006 spin_unlock_irqrestore(&lockres->l_lock, flags);
1008 BUG_ON(level == LKM_IVMODE);
1009 BUG_ON(level == LKM_NLMODE);
1011 mlog(0, "lock %s, convert from %d to level = %d\n",
1012 lockres->l_name, lockres->l_level, level);
1014 /* call dlm_lock to upgrade lock now */
1015 status = dlmlock(osb->dlm,
1018 lkm_flags|LKM_CONVERT|LKM_VALBLK,
1020 OCFS2_LOCK_ID_MAX_LEN - 1,
1023 lockres->l_ops->bast);
1024 if (status != DLM_NORMAL) {
1025 if ((lkm_flags & LKM_NOQUEUE) &&
1026 (status == DLM_NOTQUEUED))
1029 ocfs2_log_dlm_error("dlmlock", status,
1033 ocfs2_recover_from_dlm_error(lockres, 1);
1037 mlog(0, "lock %s, successfull return from dlmlock\n",
1040 /* At this point we've gone inside the dlm and need to
1041 * complete our work regardless. */
1044 /* wait for busy to clear and carry on */
1048 /* Ok, if we get here then we're good to go. */
1049 ocfs2_inc_holders(lockres, level);
1053 spin_unlock_irqrestore(&lockres->l_lock, flags);
1056 * This is helping work around a lock inversion between the page lock
1057 * and dlm locks. One path holds the page lock while calling aops
1058 * which block acquiring dlm locks. The voting thread holds dlm
1059 * locks while acquiring page locks while down converting data locks.
1060 * This block is helping an aop path notice the inversion and back
1061 * off to unlock its page lock before trying the dlm lock again.
1063 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1064 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1066 if (lockres_remove_mask_waiter(lockres, &mw))
1072 ret = ocfs2_wait_for_mask(&mw);
1082 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1083 struct ocfs2_lock_res *lockres,
1086 unsigned long flags;
1089 spin_lock_irqsave(&lockres->l_lock, flags);
1090 ocfs2_dec_holders(lockres, level);
1091 ocfs2_vote_on_unlock(osb, lockres);
1092 spin_unlock_irqrestore(&lockres->l_lock, flags);
1096 int ocfs2_create_new_lock(struct ocfs2_super *osb,
1097 struct ocfs2_lock_res *lockres,
1101 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1102 unsigned long flags;
1103 int lkm_flags = local ? LKM_LOCAL : 0;
1105 spin_lock_irqsave(&lockres->l_lock, flags);
1106 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1107 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1108 spin_unlock_irqrestore(&lockres->l_lock, flags);
1110 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
1113 /* Grants us an EX lock on the data and metadata resources, skipping
1114 * the normal cluster directory lookup. Use this ONLY on newly created
1115 * inodes which other nodes can't possibly see, and which haven't been
1116 * hashed in the inode hash yet. This can give us a good performance
1117 * increase as it'll skip the network broadcast normally associated
1118 * with creating a new lock resource. */
1119 int ocfs2_create_new_inode_locks(struct inode *inode)
1122 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1125 BUG_ON(!ocfs2_inode_is_new(inode));
1129 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1131 /* NOTE: That we don't increment any of the holder counts, nor
1132 * do we add anything to a journal handle. Since this is
1133 * supposed to be a new inode which the cluster doesn't know
1134 * about yet, there is no need to. As far as the LVB handling
1135 * is concerned, this is basically like acquiring an EX lock
1136 * on a resource which has an invalid one -- we'll set it
1137 * valid when we release the EX. */
1139 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
1146 * We don't want to use LKM_LOCAL on a meta data lock as they
1147 * don't use a generation in their lock names.
1149 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_meta_lockres, 1, 0);
1155 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_data_lockres, 1, 1);
1166 int ocfs2_rw_lock(struct inode *inode, int write)
1169 struct ocfs2_lock_res *lockres;
1175 mlog(0, "inode %llu take %s RW lock\n",
1176 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1177 write ? "EXMODE" : "PRMODE");
1179 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1181 level = write ? LKM_EXMODE : LKM_PRMODE;
1183 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1192 void ocfs2_rw_unlock(struct inode *inode, int write)
1194 int level = write ? LKM_EXMODE : LKM_PRMODE;
1195 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1199 mlog(0, "inode %llu drop %s RW lock\n",
1200 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1201 write ? "EXMODE" : "PRMODE");
1203 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1208 int ocfs2_data_lock_full(struct inode *inode,
1212 int status = 0, level;
1213 struct ocfs2_lock_res *lockres;
1219 mlog(0, "inode %llu take %s DATA lock\n",
1220 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1221 write ? "EXMODE" : "PRMODE");
1223 /* We'll allow faking a readonly data lock for
1225 if (ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb))) {
1233 lockres = &OCFS2_I(inode)->ip_data_lockres;
1235 level = write ? LKM_EXMODE : LKM_PRMODE;
1237 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level,
1239 if (status < 0 && status != -EAGAIN)
1247 /* see ocfs2_meta_lock_with_page() */
1248 int ocfs2_data_lock_with_page(struct inode *inode,
1254 ret = ocfs2_data_lock_full(inode, write, OCFS2_LOCK_NONBLOCK);
1255 if (ret == -EAGAIN) {
1257 if (ocfs2_data_lock(inode, write) == 0)
1258 ocfs2_data_unlock(inode, write);
1259 ret = AOP_TRUNCATED_PAGE;
1265 static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
1266 struct ocfs2_lock_res *lockres)
1272 /* If we know that another node is waiting on our lock, kick
1273 * the vote thread * pre-emptively when we reach a release
1275 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1276 switch(lockres->l_blocking) {
1278 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1282 if (!lockres->l_ex_holders)
1291 ocfs2_kick_vote_thread(osb);
1296 void ocfs2_data_unlock(struct inode *inode,
1299 int level = write ? LKM_EXMODE : LKM_PRMODE;
1300 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_data_lockres;
1304 mlog(0, "inode %llu drop %s DATA lock\n",
1305 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1306 write ? "EXMODE" : "PRMODE");
1308 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1309 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1314 #define OCFS2_SEC_BITS 34
1315 #define OCFS2_SEC_SHIFT (64 - 34)
1316 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1318 /* LVB only has room for 64 bits of time here so we pack it for
1320 static u64 ocfs2_pack_timespec(struct timespec *spec)
1323 u64 sec = spec->tv_sec;
1324 u32 nsec = spec->tv_nsec;
1326 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1331 /* Call this with the lockres locked. I am reasonably sure we don't
1332 * need ip_lock in this function as anyone who would be changing those
1333 * values is supposed to be blocked in ocfs2_meta_lock right now. */
1334 static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1336 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1337 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1338 struct ocfs2_meta_lvb *lvb;
1342 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1345 * Invalidate the LVB of a deleted inode - this way other
1346 * nodes are forced to go to disk and discover the new inode
1349 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1350 lvb->lvb_version = 0;
1354 lvb->lvb_version = OCFS2_LVB_VERSION;
1355 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1356 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1357 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1358 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1359 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1360 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1361 lvb->lvb_iatime_packed =
1362 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1363 lvb->lvb_ictime_packed =
1364 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1365 lvb->lvb_imtime_packed =
1366 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1367 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
1368 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
1371 mlog_meta_lvb(0, lockres);
1376 static void ocfs2_unpack_timespec(struct timespec *spec,
1379 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1380 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1383 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1385 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1386 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1387 struct ocfs2_meta_lvb *lvb;
1391 mlog_meta_lvb(0, lockres);
1393 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1395 /* We're safe here without the lockres lock... */
1396 spin_lock(&oi->ip_lock);
1397 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1398 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1400 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1401 ocfs2_set_inode_flags(inode);
1403 /* fast-symlinks are a special case */
1404 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1405 inode->i_blocks = 0;
1408 ocfs2_align_bytes_to_sectors(i_size_read(inode));
1410 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1411 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1412 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1413 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1414 ocfs2_unpack_timespec(&inode->i_atime,
1415 be64_to_cpu(lvb->lvb_iatime_packed));
1416 ocfs2_unpack_timespec(&inode->i_mtime,
1417 be64_to_cpu(lvb->lvb_imtime_packed));
1418 ocfs2_unpack_timespec(&inode->i_ctime,
1419 be64_to_cpu(lvb->lvb_ictime_packed));
1420 spin_unlock(&oi->ip_lock);
1425 static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1426 struct ocfs2_lock_res *lockres)
1428 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1430 if (lvb->lvb_version == OCFS2_LVB_VERSION
1431 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
1436 /* Determine whether a lock resource needs to be refreshed, and
1437 * arbitrate who gets to refresh it.
1439 * 0 means no refresh needed.
1441 * > 0 means you need to refresh this and you MUST call
1442 * ocfs2_complete_lock_res_refresh afterwards. */
1443 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1445 unsigned long flags;
1451 spin_lock_irqsave(&lockres->l_lock, flags);
1452 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1453 spin_unlock_irqrestore(&lockres->l_lock, flags);
1457 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1458 spin_unlock_irqrestore(&lockres->l_lock, flags);
1460 ocfs2_wait_on_refreshing_lock(lockres);
1464 /* Ok, I'll be the one to refresh this lock. */
1465 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1466 spin_unlock_irqrestore(&lockres->l_lock, flags);
1474 /* If status is non zero, I'll mark it as not being in refresh
1475 * anymroe, but i won't clear the needs refresh flag. */
1476 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1479 unsigned long flags;
1482 spin_lock_irqsave(&lockres->l_lock, flags);
1483 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1485 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1486 spin_unlock_irqrestore(&lockres->l_lock, flags);
1488 wake_up(&lockres->l_event);
1493 /* may or may not return a bh if it went to disk. */
1494 static int ocfs2_meta_lock_update(struct inode *inode,
1495 struct buffer_head **bh)
1498 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1499 struct ocfs2_lock_res *lockres;
1500 struct ocfs2_dinode *fe;
1504 spin_lock(&oi->ip_lock);
1505 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1506 mlog(0, "Orphaned inode %llu was deleted while we "
1507 "were waiting on a lock. ip_flags = 0x%x\n",
1508 (unsigned long long)oi->ip_blkno, oi->ip_flags);
1509 spin_unlock(&oi->ip_lock);
1513 spin_unlock(&oi->ip_lock);
1515 lockres = &oi->ip_meta_lockres;
1517 if (!ocfs2_should_refresh_lock_res(lockres))
1520 /* This will discard any caching information we might have had
1521 * for the inode metadata. */
1522 ocfs2_metadata_cache_purge(inode);
1524 /* will do nothing for inode types that don't use the extent
1525 * map (directories, bitmap files, etc) */
1526 ocfs2_extent_map_trunc(inode, 0);
1528 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
1529 mlog(0, "Trusting LVB on inode %llu\n",
1530 (unsigned long long)oi->ip_blkno);
1531 ocfs2_refresh_inode_from_lvb(inode);
1533 /* Boo, we have to go to disk. */
1534 /* read bh, cast, ocfs2_refresh_inode */
1535 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1536 bh, OCFS2_BH_CACHED, inode);
1541 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1543 /* This is a good chance to make sure we're not
1544 * locking an invalid object.
1546 * We bug on a stale inode here because we checked
1547 * above whether it was wiped from disk. The wiping
1548 * node provides a guarantee that we receive that
1549 * message and can mark the inode before dropping any
1550 * locks associated with it. */
1551 if (!OCFS2_IS_VALID_DINODE(fe)) {
1552 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1556 mlog_bug_on_msg(inode->i_generation !=
1557 le32_to_cpu(fe->i_generation),
1558 "Invalid dinode %llu disk generation: %u "
1559 "inode->i_generation: %u\n",
1560 (unsigned long long)oi->ip_blkno,
1561 le32_to_cpu(fe->i_generation),
1562 inode->i_generation);
1563 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1564 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
1565 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1566 (unsigned long long)oi->ip_blkno,
1567 (unsigned long long)le64_to_cpu(fe->i_dtime),
1568 le32_to_cpu(fe->i_flags));
1570 ocfs2_refresh_inode(inode, fe);
1575 ocfs2_complete_lock_res_refresh(lockres, status);
1581 static int ocfs2_assign_bh(struct inode *inode,
1582 struct buffer_head **ret_bh,
1583 struct buffer_head *passed_bh)
1588 /* Ok, the update went to disk for us, use the
1590 *ret_bh = passed_bh;
1596 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1597 OCFS2_I(inode)->ip_blkno,
1608 * returns < 0 error if the callback will never be called, otherwise
1609 * the result of the lock will be communicated via the callback.
1611 int ocfs2_meta_lock_full(struct inode *inode,
1612 struct ocfs2_journal_handle *handle,
1613 struct buffer_head **ret_bh,
1617 int status, level, dlm_flags, acquired;
1618 struct ocfs2_lock_res *lockres;
1619 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1620 struct buffer_head *local_bh = NULL;
1626 mlog(0, "inode %llu, take %s META lock\n",
1627 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1628 ex ? "EXMODE" : "PRMODE");
1632 /* We'll allow faking a readonly metadata lock for
1634 if (ocfs2_is_hard_readonly(osb)) {
1640 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1641 wait_event(osb->recovery_event,
1642 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1645 lockres = &OCFS2_I(inode)->ip_meta_lockres;
1646 level = ex ? LKM_EXMODE : LKM_PRMODE;
1648 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
1649 dlm_flags |= LKM_NOQUEUE;
1651 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1653 if (status != -EAGAIN && status != -EIOCBRETRY)
1658 /* Notify the error cleanup path to drop the cluster lock. */
1661 /* We wait twice because a node may have died while we were in
1662 * the lower dlm layers. The second time though, we've
1663 * committed to owning this lock so we don't allow signals to
1664 * abort the operation. */
1665 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1666 wait_event(osb->recovery_event,
1667 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1670 * We only see this flag if we're being called from
1671 * ocfs2_read_locked_inode(). It means we're locking an inode
1672 * which hasn't been populated yet, so clear the refresh flag
1673 * and let the caller handle it.
1675 if (inode->i_state & I_NEW) {
1677 ocfs2_complete_lock_res_refresh(lockres, 0);
1681 /* This is fun. The caller may want a bh back, or it may
1682 * not. ocfs2_meta_lock_update definitely wants one in, but
1683 * may or may not read one, depending on what's in the
1684 * LVB. The result of all of this is that we've *only* gone to
1685 * disk if we have to, so the complexity is worthwhile. */
1686 status = ocfs2_meta_lock_update(inode, &local_bh);
1688 if (status != -ENOENT)
1694 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
1702 status = ocfs2_handle_add_lock(handle, inode);
1709 if (ret_bh && (*ret_bh)) {
1714 ocfs2_meta_unlock(inode, ex);
1725 * This is working around a lock inversion between tasks acquiring DLM locks
1726 * while holding a page lock and the vote thread which blocks dlm lock acquiry
1727 * while acquiring page locks.
1729 * ** These _with_page variantes are only intended to be called from aop
1730 * methods that hold page locks and return a very specific *positive* error
1731 * code that aop methods pass up to the VFS -- test for errors with != 0. **
1733 * The DLM is called such that it returns -EAGAIN if it would have blocked
1734 * waiting for the vote thread. In that case we unlock our page so the vote
1735 * thread can make progress. Once we've done this we have to return
1736 * AOP_TRUNCATED_PAGE so the aop method that called us can bubble that back up
1737 * into the VFS who will then immediately retry the aop call.
1739 * We do a blocking lock and immediate unlock before returning, though, so that
1740 * the lock has a great chance of being cached on this node by the time the VFS
1741 * calls back to retry the aop. This has a potential to livelock as nodes
1742 * ping locks back and forth, but that's a risk we're willing to take to avoid
1743 * the lock inversion simply.
1745 int ocfs2_meta_lock_with_page(struct inode *inode,
1746 struct ocfs2_journal_handle *handle,
1747 struct buffer_head **ret_bh,
1753 ret = ocfs2_meta_lock_full(inode, handle, ret_bh, ex,
1754 OCFS2_LOCK_NONBLOCK);
1755 if (ret == -EAGAIN) {
1757 if (ocfs2_meta_lock(inode, handle, ret_bh, ex) == 0)
1758 ocfs2_meta_unlock(inode, ex);
1759 ret = AOP_TRUNCATED_PAGE;
1765 void ocfs2_meta_unlock(struct inode *inode,
1768 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1769 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
1773 mlog(0, "inode %llu drop %s META lock\n",
1774 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1775 ex ? "EXMODE" : "PRMODE");
1777 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1778 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1783 int ocfs2_super_lock(struct ocfs2_super *osb,
1787 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1788 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1789 struct buffer_head *bh;
1790 struct ocfs2_slot_info *si = osb->slot_info;
1794 if (ocfs2_is_hard_readonly(osb))
1797 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
1803 /* The super block lock path is really in the best position to
1804 * know when resources covered by the lock need to be
1805 * refreshed, so we do it here. Of course, making sense of
1806 * everything is up to the caller :) */
1807 status = ocfs2_should_refresh_lock_res(lockres);
1814 status = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0,
1817 ocfs2_update_slot_info(si);
1819 ocfs2_complete_lock_res_refresh(lockres, status);
1829 void ocfs2_super_unlock(struct ocfs2_super *osb,
1832 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1833 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1835 ocfs2_cluster_unlock(osb, lockres, level);
1838 int ocfs2_rename_lock(struct ocfs2_super *osb)
1841 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1843 if (ocfs2_is_hard_readonly(osb))
1846 status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
1853 void ocfs2_rename_unlock(struct ocfs2_super *osb)
1855 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1857 ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
1860 int ocfs2_dentry_lock(struct dentry *dentry, int ex)
1863 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1864 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1865 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1869 if (ocfs2_is_hard_readonly(osb))
1872 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
1879 void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
1881 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1882 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1883 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1885 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
1888 /* Reference counting of the dlm debug structure. We want this because
1889 * open references on the debug inodes can live on after a mount, so
1890 * we can't rely on the ocfs2_super to always exist. */
1891 static void ocfs2_dlm_debug_free(struct kref *kref)
1893 struct ocfs2_dlm_debug *dlm_debug;
1895 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
1900 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
1903 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
1906 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
1908 kref_get(&debug->d_refcnt);
1911 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
1913 struct ocfs2_dlm_debug *dlm_debug;
1915 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
1917 mlog_errno(-ENOMEM);
1921 kref_init(&dlm_debug->d_refcnt);
1922 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
1923 dlm_debug->d_locking_state = NULL;
1928 /* Access to this is arbitrated for us via seq_file->sem. */
1929 struct ocfs2_dlm_seq_priv {
1930 struct ocfs2_dlm_debug *p_dlm_debug;
1931 struct ocfs2_lock_res p_iter_res;
1932 struct ocfs2_lock_res p_tmp_res;
1935 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
1936 struct ocfs2_dlm_seq_priv *priv)
1938 struct ocfs2_lock_res *iter, *ret = NULL;
1939 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
1941 assert_spin_locked(&ocfs2_dlm_tracking_lock);
1943 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
1944 /* discover the head of the list */
1945 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
1946 mlog(0, "End of list found, %p\n", ret);
1950 /* We track our "dummy" iteration lockres' by a NULL
1952 if (iter->l_ops != NULL) {
1961 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
1963 struct ocfs2_dlm_seq_priv *priv = m->private;
1964 struct ocfs2_lock_res *iter;
1966 spin_lock(&ocfs2_dlm_tracking_lock);
1967 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
1969 /* Since lockres' have the lifetime of their container
1970 * (which can be inodes, ocfs2_supers, etc) we want to
1971 * copy this out to a temporary lockres while still
1972 * under the spinlock. Obviously after this we can't
1973 * trust any pointers on the copy returned, but that's
1974 * ok as the information we want isn't typically held
1976 priv->p_tmp_res = *iter;
1977 iter = &priv->p_tmp_res;
1979 spin_unlock(&ocfs2_dlm_tracking_lock);
1984 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
1988 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
1990 struct ocfs2_dlm_seq_priv *priv = m->private;
1991 struct ocfs2_lock_res *iter = v;
1992 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
1994 spin_lock(&ocfs2_dlm_tracking_lock);
1995 iter = ocfs2_dlm_next_res(iter, priv);
1996 list_del_init(&dummy->l_debug_list);
1998 list_add(&dummy->l_debug_list, &iter->l_debug_list);
1999 priv->p_tmp_res = *iter;
2000 iter = &priv->p_tmp_res;
2002 spin_unlock(&ocfs2_dlm_tracking_lock);
2007 /* So that debugfs.ocfs2 can determine which format is being used */
2008 #define OCFS2_DLM_DEBUG_STR_VERSION 1
2009 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2013 struct ocfs2_lock_res *lockres = v;
2018 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2020 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2021 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2023 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2025 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2027 seq_printf(m, "%d\t"
2038 lockres->l_unlock_action,
2039 lockres->l_ro_holders,
2040 lockres->l_ex_holders,
2041 lockres->l_requested,
2042 lockres->l_blocking);
2044 /* Dump the raw LVB */
2045 lvb = lockres->l_lksb.lvb;
2046 for(i = 0; i < DLM_LVB_LEN; i++)
2047 seq_printf(m, "0x%x\t", lvb[i]);
2050 seq_printf(m, "\n");
2054 static struct seq_operations ocfs2_dlm_seq_ops = {
2055 .start = ocfs2_dlm_seq_start,
2056 .stop = ocfs2_dlm_seq_stop,
2057 .next = ocfs2_dlm_seq_next,
2058 .show = ocfs2_dlm_seq_show,
2061 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2063 struct seq_file *seq = (struct seq_file *) file->private_data;
2064 struct ocfs2_dlm_seq_priv *priv = seq->private;
2065 struct ocfs2_lock_res *res = &priv->p_iter_res;
2067 ocfs2_remove_lockres_tracking(res);
2068 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2069 return seq_release_private(inode, file);
2072 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2075 struct ocfs2_dlm_seq_priv *priv;
2076 struct seq_file *seq;
2077 struct ocfs2_super *osb;
2079 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2085 osb = (struct ocfs2_super *) inode->u.generic_ip;
2086 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2087 priv->p_dlm_debug = osb->osb_dlm_debug;
2088 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2090 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2097 seq = (struct seq_file *) file->private_data;
2098 seq->private = priv;
2100 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2107 static const struct file_operations ocfs2_dlm_debug_fops = {
2108 .open = ocfs2_dlm_debug_open,
2109 .release = ocfs2_dlm_debug_release,
2111 .llseek = seq_lseek,
2114 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2117 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2119 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2121 osb->osb_debug_root,
2123 &ocfs2_dlm_debug_fops);
2124 if (!dlm_debug->d_locking_state) {
2127 "Unable to create locking state debugfs file.\n");
2131 ocfs2_get_dlm_debug(dlm_debug);
2136 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2138 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2141 debugfs_remove(dlm_debug->d_locking_state);
2142 ocfs2_put_dlm_debug(dlm_debug);
2146 int ocfs2_dlm_init(struct ocfs2_super *osb)
2150 struct dlm_ctxt *dlm;
2154 status = ocfs2_dlm_init_debug(osb);
2160 /* launch vote thread */
2161 osb->vote_task = kthread_run(ocfs2_vote_thread, osb, "ocfs2vote");
2162 if (IS_ERR(osb->vote_task)) {
2163 status = PTR_ERR(osb->vote_task);
2164 osb->vote_task = NULL;
2169 /* used by the dlm code to make message headers unique, each
2170 * node in this domain must agree on this. */
2171 dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
2173 /* for now, uuid == domain */
2174 dlm = dlm_register_domain(osb->uuid_str, dlm_key);
2176 status = PTR_ERR(dlm);
2181 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2182 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2184 dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
2191 ocfs2_dlm_shutdown_debug(osb);
2193 kthread_stop(osb->vote_task);
2200 void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2204 dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
2206 ocfs2_drop_osb_locks(osb);
2208 if (osb->vote_task) {
2209 kthread_stop(osb->vote_task);
2210 osb->vote_task = NULL;
2213 ocfs2_lock_res_free(&osb->osb_super_lockres);
2214 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2216 dlm_unregister_domain(osb->dlm);
2219 ocfs2_dlm_shutdown_debug(osb);
2224 static void ocfs2_unlock_ast_func(void *opaque, enum dlm_status status)
2226 struct ocfs2_lock_res *lockres = opaque;
2227 unsigned long flags;
2231 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2232 lockres->l_unlock_action);
2234 spin_lock_irqsave(&lockres->l_lock, flags);
2235 /* We tried to cancel a convert request, but it was already
2236 * granted. All we want to do here is clear our unlock
2237 * state. The wake_up call done at the bottom is redundant
2238 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2239 * hurt anything anyway */
2240 if (status == DLM_CANCELGRANT &&
2241 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2242 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2244 /* We don't clear the busy flag in this case as it
2245 * should have been cleared by the ast which the dlm
2247 goto complete_unlock;
2250 if (status != DLM_NORMAL) {
2251 mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
2252 "unlock_action %d\n", status, lockres->l_name,
2253 lockres->l_unlock_action);
2254 spin_unlock_irqrestore(&lockres->l_lock, flags);
2258 switch(lockres->l_unlock_action) {
2259 case OCFS2_UNLOCK_CANCEL_CONVERT:
2260 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2261 lockres->l_action = OCFS2_AST_INVALID;
2263 case OCFS2_UNLOCK_DROP_LOCK:
2264 lockres->l_level = LKM_IVMODE;
2270 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2272 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2273 spin_unlock_irqrestore(&lockres->l_lock, flags);
2275 wake_up(&lockres->l_event);
2280 typedef void (ocfs2_pre_drop_cb_t)(struct ocfs2_lock_res *, void *);
2282 struct drop_lock_cb {
2283 ocfs2_pre_drop_cb_t *drop_func;
2287 static int ocfs2_drop_lock(struct ocfs2_super *osb,
2288 struct ocfs2_lock_res *lockres,
2289 struct drop_lock_cb *dcb)
2291 enum dlm_status status;
2292 unsigned long flags;
2294 /* We didn't get anywhere near actually using this lockres. */
2295 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2298 spin_lock_irqsave(&lockres->l_lock, flags);
2300 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2301 "lockres %s, flags 0x%lx\n",
2302 lockres->l_name, lockres->l_flags);
2304 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2305 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2306 "%u, unlock_action = %u\n",
2307 lockres->l_name, lockres->l_flags, lockres->l_action,
2308 lockres->l_unlock_action);
2310 spin_unlock_irqrestore(&lockres->l_lock, flags);
2312 /* XXX: Today we just wait on any busy
2313 * locks... Perhaps we need to cancel converts in the
2315 ocfs2_wait_on_busy_lock(lockres);
2317 spin_lock_irqsave(&lockres->l_lock, flags);
2321 dcb->drop_func(lockres, dcb->drop_data);
2323 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2324 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2326 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2327 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2329 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2330 spin_unlock_irqrestore(&lockres->l_lock, flags);
2334 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2336 /* make sure we never get here while waiting for an ast to
2338 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2340 /* is this necessary? */
2341 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2342 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2343 spin_unlock_irqrestore(&lockres->l_lock, flags);
2345 mlog(0, "lock %s\n", lockres->l_name);
2347 status = dlmunlock(osb->dlm, &lockres->l_lksb, LKM_VALBLK,
2348 lockres->l_ops->unlock_ast, lockres);
2349 if (status != DLM_NORMAL) {
2350 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2351 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2352 dlm_print_one_lock(lockres->l_lksb.lockid);
2355 mlog(0, "lock %s, successfull return from dlmunlock\n",
2358 ocfs2_wait_on_busy_lock(lockres);
2364 /* Mark the lockres as being dropped. It will no longer be
2365 * queued if blocking, but we still may have to wait on it
2366 * being dequeued from the vote thread before we can consider
2369 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2370 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2373 struct ocfs2_mask_waiter mw;
2374 unsigned long flags;
2376 ocfs2_init_mask_waiter(&mw);
2378 spin_lock_irqsave(&lockres->l_lock, flags);
2379 lockres->l_flags |= OCFS2_LOCK_FREEING;
2380 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2381 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2382 spin_unlock_irqrestore(&lockres->l_lock, flags);
2384 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2386 status = ocfs2_wait_for_mask(&mw);
2390 spin_lock_irqsave(&lockres->l_lock, flags);
2392 spin_unlock_irqrestore(&lockres->l_lock, flags);
2395 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2396 struct ocfs2_lock_res *lockres)
2400 ocfs2_mark_lockres_freeing(lockres);
2401 ret = ocfs2_drop_lock(osb, lockres, NULL);
2406 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2408 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2409 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
2412 static void ocfs2_meta_pre_drop(struct ocfs2_lock_res *lockres, void *data)
2414 struct inode *inode = data;
2416 /* the metadata lock requires a bit more work as we have an
2417 * LVB to worry about. */
2418 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2419 lockres->l_level == LKM_EXMODE &&
2420 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2421 __ocfs2_stuff_meta_lvb(inode);
2424 int ocfs2_drop_inode_locks(struct inode *inode)
2427 struct drop_lock_cb meta_dcb = { ocfs2_meta_pre_drop, inode, };
2431 /* No need to call ocfs2_mark_lockres_freeing here -
2432 * ocfs2_clear_inode has done it for us. */
2434 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2435 &OCFS2_I(inode)->ip_data_lockres,
2442 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2443 &OCFS2_I(inode)->ip_meta_lockres,
2447 if (err < 0 && !status)
2450 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2451 &OCFS2_I(inode)->ip_rw_lockres,
2455 if (err < 0 && !status)
2462 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2465 assert_spin_locked(&lockres->l_lock);
2467 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
2469 if (lockres->l_level <= new_level) {
2470 mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
2471 lockres->l_level, new_level);
2475 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2476 lockres->l_name, new_level, lockres->l_blocking);
2478 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2479 lockres->l_requested = new_level;
2480 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2483 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2484 struct ocfs2_lock_res *lockres,
2488 int ret, dlm_flags = LKM_CONVERT;
2489 enum dlm_status status;
2494 dlm_flags |= LKM_VALBLK;
2496 status = dlmlock(osb->dlm,
2501 OCFS2_LOCK_ID_MAX_LEN - 1,
2504 lockres->l_ops->bast);
2505 if (status != DLM_NORMAL) {
2506 ocfs2_log_dlm_error("dlmlock", status, lockres);
2508 ocfs2_recover_from_dlm_error(lockres, 1);
2518 /* returns 1 when the caller should unlock and call dlmunlock */
2519 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2520 struct ocfs2_lock_res *lockres)
2522 assert_spin_locked(&lockres->l_lock);
2525 mlog(0, "lock %s\n", lockres->l_name);
2527 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2528 /* If we're already trying to cancel a lock conversion
2529 * then just drop the spinlock and allow the caller to
2530 * requeue this lock. */
2532 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2536 /* were we in a convert when we got the bast fire? */
2537 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2538 lockres->l_action != OCFS2_AST_DOWNCONVERT);
2539 /* set things up for the unlockast to know to just
2540 * clear out the ast_action and unset busy, etc. */
2541 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2543 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2544 "lock %s, invalid flags: 0x%lx\n",
2545 lockres->l_name, lockres->l_flags);
2550 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2551 struct ocfs2_lock_res *lockres)
2554 enum dlm_status status;
2557 mlog(0, "lock %s\n", lockres->l_name);
2560 status = dlmunlock(osb->dlm,
2563 lockres->l_ops->unlock_ast,
2565 if (status != DLM_NORMAL) {
2566 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2568 ocfs2_recover_from_dlm_error(lockres, 0);
2571 mlog(0, "lock %s return from dlmunlock\n", lockres->l_name);
2577 static inline int ocfs2_can_downconvert_meta_lock(struct inode *inode,
2578 struct ocfs2_lock_res *lockres,
2585 BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
2587 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2589 mlog(0, "lockres %s currently being refreshed -- backing "
2590 "off!\n", lockres->l_name);
2591 } else if (new_level == LKM_PRMODE)
2592 ret = !lockres->l_ex_holders &&
2593 ocfs2_inode_fully_checkpointed(inode);
2594 else /* Must be NLMODE we're converting to. */
2595 ret = !lockres->l_ro_holders && !lockres->l_ex_holders &&
2596 ocfs2_inode_fully_checkpointed(inode);
2602 static int ocfs2_do_unblock_meta(struct inode *inode,
2608 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
2609 unsigned long flags;
2611 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2615 spin_lock_irqsave(&lockres->l_lock, flags);
2617 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2619 mlog(0, "l_level=%d, l_blocking=%d\n", lockres->l_level,
2620 lockres->l_blocking);
2622 BUG_ON(lockres->l_level != LKM_EXMODE &&
2623 lockres->l_level != LKM_PRMODE);
2625 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2627 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2628 spin_unlock_irqrestore(&lockres->l_lock, flags);
2630 ret = ocfs2_cancel_convert(osb, lockres);
2637 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2639 mlog(0, "l_level=%d, l_blocking=%d, new_level=%d\n",
2640 lockres->l_level, lockres->l_blocking, new_level);
2642 if (ocfs2_can_downconvert_meta_lock(inode, lockres, new_level)) {
2643 if (lockres->l_level == LKM_EXMODE)
2646 /* If the lock hasn't been refreshed yet (rare), then
2647 * our memory inode values are old and we skip
2648 * stuffing the lvb. There's no need to actually clear
2649 * out the lvb here as it's value is still valid. */
2650 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2652 __ocfs2_stuff_meta_lvb(inode);
2654 mlog(0, "lockres %s: downconverting stale lock!\n",
2657 mlog(0, "calling ocfs2_downconvert_lock with l_level=%d, "
2658 "l_blocking=%d, new_level=%d\n",
2659 lockres->l_level, lockres->l_blocking, new_level);
2661 ocfs2_prepare_downconvert(lockres, new_level);
2662 spin_unlock_irqrestore(&lockres->l_lock, flags);
2663 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
2666 if (!ocfs2_inode_fully_checkpointed(inode))
2667 ocfs2_start_checkpoint(osb);
2670 spin_unlock_irqrestore(&lockres->l_lock, flags);
2677 static int ocfs2_generic_unblock_lock(struct ocfs2_super *osb,
2678 struct ocfs2_lock_res *lockres,
2679 struct ocfs2_unblock_ctl *ctl,
2680 ocfs2_convert_worker_t *worker)
2682 unsigned long flags;
2689 spin_lock_irqsave(&lockres->l_lock, flags);
2691 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2694 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2696 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2697 spin_unlock_irqrestore(&lockres->l_lock, flags);
2699 ret = ocfs2_cancel_convert(osb, lockres);
2706 /* if we're blocking an exclusive and we have *any* holders,
2708 if ((lockres->l_blocking == LKM_EXMODE)
2709 && (lockres->l_ex_holders || lockres->l_ro_holders)) {
2710 spin_unlock_irqrestore(&lockres->l_lock, flags);
2716 /* If it's a PR we're blocking, then only
2717 * requeue if we've got any EX holders */
2718 if (lockres->l_blocking == LKM_PRMODE &&
2719 lockres->l_ex_holders) {
2720 spin_unlock_irqrestore(&lockres->l_lock, flags);
2726 /* If we get here, then we know that there are no more
2727 * incompatible holders (and anyone asking for an incompatible
2728 * lock is blocked). We can now downconvert the lock */
2732 /* Some lockres types want to do a bit of work before
2733 * downconverting a lock. Allow that here. The worker function
2734 * may sleep, so we save off a copy of what we're blocking as
2735 * it may change while we're not holding the spin lock. */
2736 blocking = lockres->l_blocking;
2737 spin_unlock_irqrestore(&lockres->l_lock, flags);
2739 ctl->unblock_action = worker(lockres, blocking);
2741 if (ctl->unblock_action == UNBLOCK_STOP_POST)
2744 spin_lock_irqsave(&lockres->l_lock, flags);
2745 if (blocking != lockres->l_blocking) {
2746 /* If this changed underneath us, then we can't drop
2753 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2755 ocfs2_prepare_downconvert(lockres, new_level);
2756 spin_unlock_irqrestore(&lockres->l_lock, flags);
2757 ret = ocfs2_downconvert_lock(osb, lockres, new_level, 0);
2763 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
2766 struct inode *inode;
2767 struct address_space *mapping;
2769 inode = ocfs2_lock_res_inode(lockres);
2770 mapping = inode->i_mapping;
2772 if (filemap_fdatawrite(mapping)) {
2773 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
2774 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2776 sync_mapping_buffers(mapping);
2777 if (blocking == LKM_EXMODE) {
2778 truncate_inode_pages(mapping, 0);
2779 unmap_mapping_range(mapping, 0, 0, 0);
2781 /* We only need to wait on the I/O if we're not also
2782 * truncating pages because truncate_inode_pages waits
2783 * for us above. We don't truncate pages if we're
2784 * blocking anything < EXMODE because we want to keep
2785 * them around in that case. */
2786 filemap_fdatawait(mapping);
2789 return UNBLOCK_CONTINUE;
2792 int ocfs2_unblock_data(struct ocfs2_lock_res *lockres,
2793 struct ocfs2_unblock_ctl *ctl)
2796 struct inode *inode;
2797 struct ocfs2_super *osb;
2801 inode = ocfs2_lock_res_inode(lockres);
2802 osb = OCFS2_SB(inode->i_sb);
2804 mlog(0, "unblock inode %llu\n",
2805 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2807 status = ocfs2_generic_unblock_lock(osb, lockres, ctl,
2808 ocfs2_data_convert_worker);
2812 mlog(0, "inode %llu, requeue = %d\n",
2813 (unsigned long long)OCFS2_I(inode)->ip_blkno, ctl->requeue);
2819 static int ocfs2_unblock_inode_lock(struct ocfs2_lock_res *lockres,
2820 struct ocfs2_unblock_ctl *ctl)
2823 struct inode *inode;
2827 mlog(0, "Unblock lockres %s\n", lockres->l_name);
2829 inode = ocfs2_lock_res_inode(lockres);
2831 status = ocfs2_generic_unblock_lock(OCFS2_SB(inode->i_sb),
2832 lockres, ctl, NULL);
2840 static int ocfs2_unblock_meta(struct ocfs2_lock_res *lockres,
2841 struct ocfs2_unblock_ctl *ctl)
2844 struct inode *inode;
2848 inode = ocfs2_lock_res_inode(lockres);
2850 mlog(0, "unblock inode %llu\n",
2851 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2853 status = ocfs2_do_unblock_meta(inode, &ctl->requeue);
2857 mlog(0, "inode %llu, requeue = %d\n",
2858 (unsigned long long)OCFS2_I(inode)->ip_blkno, ctl->requeue);
2865 * Does the final reference drop on our dentry lock. Right now this
2866 * happens in the vote thread, but we could choose to simplify the
2867 * dlmglue API and push these off to the ocfs2_wq in the future.
2869 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
2870 struct ocfs2_lock_res *lockres)
2872 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2873 ocfs2_dentry_lock_put(osb, dl);
2877 * d_delete() matching dentries before the lock downconvert.
2879 * At this point, any process waiting to destroy the
2880 * dentry_lock due to last ref count is stopped by the
2881 * OCFS2_LOCK_QUEUED flag.
2883 * We have two potential problems
2885 * 1) If we do the last reference drop on our dentry_lock (via dput)
2886 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
2887 * the downconvert to finish. Instead we take an elevated
2888 * reference and push the drop until after we've completed our
2889 * unblock processing.
2891 * 2) There might be another process with a final reference,
2892 * waiting on us to finish processing. If this is the case, we
2893 * detect it and exit out - there's no more dentries anyway.
2895 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
2898 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2899 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
2900 struct dentry *dentry;
2901 unsigned long flags;
2905 * This node is blocking another node from getting a read
2906 * lock. This happens when we've renamed within a
2907 * directory. We've forced the other nodes to d_delete(), but
2908 * we never actually dropped our lock because it's still
2909 * valid. The downconvert code will retain a PR for this node,
2910 * so there's no further work to do.
2912 if (blocking == LKM_PRMODE)
2913 return UNBLOCK_CONTINUE;
2916 * Mark this inode as potentially orphaned. The code in
2917 * ocfs2_delete_inode() will figure out whether it actually
2918 * needs to be freed or not.
2920 spin_lock(&oi->ip_lock);
2921 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
2922 spin_unlock(&oi->ip_lock);
2925 * Yuck. We need to make sure however that the check of
2926 * OCFS2_LOCK_FREEING and the extra reference are atomic with
2927 * respect to a reference decrement or the setting of that
2930 spin_lock_irqsave(&lockres->l_lock, flags);
2931 spin_lock(&dentry_attach_lock);
2932 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
2937 spin_unlock(&dentry_attach_lock);
2938 spin_unlock_irqrestore(&lockres->l_lock, flags);
2940 mlog(0, "extra_ref = %d\n", extra_ref);
2943 * We have a process waiting on us in ocfs2_dentry_iput(),
2944 * which means we can't have any more outstanding
2945 * aliases. There's no need to do any more work.
2948 return UNBLOCK_CONTINUE;
2950 spin_lock(&dentry_attach_lock);
2952 dentry = ocfs2_find_local_alias(dl->dl_inode,
2953 dl->dl_parent_blkno, 1);
2956 spin_unlock(&dentry_attach_lock);
2958 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
2959 dentry->d_name.name);
2962 * The following dcache calls may do an
2963 * iput(). Normally we don't want that from the
2964 * downconverting thread, but in this case it's ok
2965 * because the requesting node already has an
2966 * exclusive lock on the inode, so it can't be queued
2967 * for a downconvert.
2972 spin_lock(&dentry_attach_lock);
2974 spin_unlock(&dentry_attach_lock);
2977 * If we are the last holder of this dentry lock, there is no
2978 * reason to downconvert so skip straight to the unlock.
2980 if (dl->dl_count == 1)
2981 return UNBLOCK_STOP_POST;
2983 return UNBLOCK_CONTINUE_POST;
2986 static int ocfs2_unblock_dentry_lock(struct ocfs2_lock_res *lockres,
2987 struct ocfs2_unblock_ctl *ctl)
2990 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2991 struct ocfs2_super *osb = OCFS2_SB(dl->dl_inode->i_sb);
2993 mlog(0, "unblock dentry lock: %llu\n",
2994 (unsigned long long)OCFS2_I(dl->dl_inode)->ip_blkno);
2996 ret = ocfs2_generic_unblock_lock(osb,
2999 ocfs2_dentry_convert_worker);
3003 mlog(0, "requeue = %d, post = %d\n", ctl->requeue, ctl->unblock_action);
3008 /* Generic unblock function for any lockres whose private data is an
3009 * ocfs2_super pointer. */
3010 static int ocfs2_unblock_osb_lock(struct ocfs2_lock_res *lockres,
3011 struct ocfs2_unblock_ctl *ctl)
3014 struct ocfs2_super *osb;
3018 mlog(0, "Unblock lockres %s\n", lockres->l_name);
3020 osb = ocfs2_lock_res_super(lockres);
3022 status = ocfs2_generic_unblock_lock(osb,
3033 void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3034 struct ocfs2_lock_res *lockres)
3037 struct ocfs2_unblock_ctl ctl = {0, 0,};
3038 unsigned long flags;
3040 /* Our reference to the lockres in this function can be
3041 * considered valid until we remove the OCFS2_LOCK_QUEUED
3047 BUG_ON(!lockres->l_ops);
3048 BUG_ON(!lockres->l_ops->unblock);
3050 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3052 /* Detect whether a lock has been marked as going away while
3053 * the vote thread was processing other things. A lock can
3054 * still be marked with OCFS2_LOCK_FREEING after this check,
3055 * but short circuiting here will still save us some
3057 spin_lock_irqsave(&lockres->l_lock, flags);
3058 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3060 spin_unlock_irqrestore(&lockres->l_lock, flags);
3062 status = lockres->l_ops->unblock(lockres, &ctl);
3066 spin_lock_irqsave(&lockres->l_lock, flags);
3068 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
3069 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3071 ocfs2_schedule_blocked_lock(osb, lockres);
3073 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
3074 ctl.requeue ? "yes" : "no");
3075 spin_unlock_irqrestore(&lockres->l_lock, flags);
3077 if (ctl.unblock_action != UNBLOCK_CONTINUE
3078 && lockres->l_ops->post_unlock)
3079 lockres->l_ops->post_unlock(osb, lockres);
3084 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3085 struct ocfs2_lock_res *lockres)
3089 assert_spin_locked(&lockres->l_lock);
3091 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3092 /* Do not schedule a lock for downconvert when it's on
3093 * the way to destruction - any nodes wanting access
3094 * to the resource will get it soon. */
3095 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3096 lockres->l_name, lockres->l_flags);
3100 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3102 spin_lock(&osb->vote_task_lock);
3103 if (list_empty(&lockres->l_blocked_list)) {
3104 list_add_tail(&lockres->l_blocked_list,
3105 &osb->blocked_lock_list);
3106 osb->blocked_lock_count++;
3108 spin_unlock(&osb->vote_task_lock);
3113 /* This aids in debugging situations where a bad LVB might be involved. */
3114 void ocfs2_dump_meta_lvb_info(u64 level,
3115 const char *function,
3117 struct ocfs2_lock_res *lockres)
3119 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
3121 mlog(level, "LVB information for %s (called from %s:%u):\n",
3122 lockres->l_name, function, line);
3123 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
3124 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
3125 be32_to_cpu(lvb->lvb_igeneration));
3126 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
3127 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
3128 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
3129 be16_to_cpu(lvb->lvb_imode));
3130 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
3131 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
3132 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
3133 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
3134 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
3135 be32_to_cpu(lvb->lvb_iattr));