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/crc32.h>
31 #include <linux/kthread.h>
32 #include <linux/pagemap.h>
33 #include <linux/debugfs.h>
34 #include <linux/seq_file.h>
36 #include <cluster/heartbeat.h>
37 #include <cluster/nodemanager.h>
38 #include <cluster/tcp.h>
40 #include <dlm/dlmapi.h>
42 #define MLOG_MASK_PREFIX ML_DLM_GLUE
43 #include <cluster/masklog.h>
46 #include "ocfs2_lockingver.h"
51 #include "extent_map.h"
53 #include "heartbeat.h"
56 #include "stackglue.h"
61 #include "buffer_head_io.h"
63 struct ocfs2_mask_waiter {
64 struct list_head mw_item;
66 struct completion mw_complete;
67 unsigned long mw_mask;
68 unsigned long mw_goal;
71 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
72 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
73 static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres);
76 * Return value from ->downconvert_worker functions.
78 * These control the precise actions of ocfs2_unblock_lock()
79 * and ocfs2_process_blocked_lock()
82 enum ocfs2_unblock_action {
83 UNBLOCK_CONTINUE = 0, /* Continue downconvert */
84 UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire
85 * ->post_unlock callback */
86 UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire
87 * ->post_unlock() callback. */
90 struct ocfs2_unblock_ctl {
92 enum ocfs2_unblock_action unblock_action;
95 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
97 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
99 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
102 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
105 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
106 struct ocfs2_lock_res *lockres);
109 #define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres)
111 /* This aids in debugging situations where a bad LVB might be involved. */
112 static void ocfs2_dump_meta_lvb_info(u64 level,
113 const char *function,
115 struct ocfs2_lock_res *lockres)
117 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
119 mlog(level, "LVB information for %s (called from %s:%u):\n",
120 lockres->l_name, function, line);
121 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
122 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
123 be32_to_cpu(lvb->lvb_igeneration));
124 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
125 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
126 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
127 be16_to_cpu(lvb->lvb_imode));
128 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
129 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
130 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
131 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
132 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
133 be32_to_cpu(lvb->lvb_iattr));
138 * OCFS2 Lock Resource Operations
140 * These fine tune the behavior of the generic dlmglue locking infrastructure.
142 * The most basic of lock types can point ->l_priv to their respective
143 * struct ocfs2_super and allow the default actions to manage things.
145 * Right now, each lock type also needs to implement an init function,
146 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
147 * should be called when the lock is no longer needed (i.e., object
150 struct ocfs2_lock_res_ops {
152 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
153 * this callback if ->l_priv is not an ocfs2_super pointer
155 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
158 * Optionally called in the downconvert thread after a
159 * successful downconvert. The lockres will not be referenced
160 * after this callback is called, so it is safe to free
163 * The exact semantics of when this is called are controlled
164 * by ->downconvert_worker()
166 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
169 * Allow a lock type to add checks to determine whether it is
170 * safe to downconvert a lock. Return 0 to re-queue the
171 * downconvert at a later time, nonzero to continue.
173 * For most locks, the default checks that there are no
174 * incompatible holders are sufficient.
176 * Called with the lockres spinlock held.
178 int (*check_downconvert)(struct ocfs2_lock_res *, int);
181 * Allows a lock type to populate the lock value block. This
182 * is called on downconvert, and when we drop a lock.
184 * Locks that want to use this should set LOCK_TYPE_USES_LVB
185 * in the flags field.
187 * Called with the lockres spinlock held.
189 void (*set_lvb)(struct ocfs2_lock_res *);
192 * Called from the downconvert thread when it is determined
193 * that a lock will be downconverted. This is called without
194 * any locks held so the function can do work that might
195 * schedule (syncing out data, etc).
197 * This should return any one of the ocfs2_unblock_action
198 * values, depending on what it wants the thread to do.
200 int (*downconvert_worker)(struct ocfs2_lock_res *, int);
203 * LOCK_TYPE_* flags which describe the specific requirements
204 * of a lock type. Descriptions of each individual flag follow.
210 * Some locks want to "refresh" potentially stale data when a
211 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
212 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
213 * individual lockres l_flags member from the ast function. It is
214 * expected that the locking wrapper will clear the
215 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
217 #define LOCK_TYPE_REQUIRES_REFRESH 0x1
220 * Indicate that a lock type makes use of the lock value block. The
221 * ->set_lvb lock type callback must be defined.
223 #define LOCK_TYPE_USES_LVB 0x2
225 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
226 .get_osb = ocfs2_get_inode_osb,
230 static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops = {
231 .get_osb = ocfs2_get_inode_osb,
232 .check_downconvert = ocfs2_check_meta_downconvert,
233 .set_lvb = ocfs2_set_meta_lvb,
234 .downconvert_worker = ocfs2_data_convert_worker,
235 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
238 static struct ocfs2_lock_res_ops ocfs2_super_lops = {
239 .flags = LOCK_TYPE_REQUIRES_REFRESH,
242 static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
246 static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
247 .get_osb = ocfs2_get_dentry_osb,
248 .post_unlock = ocfs2_dentry_post_unlock,
249 .downconvert_worker = ocfs2_dentry_convert_worker,
253 static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
254 .get_osb = ocfs2_get_inode_osb,
258 static struct ocfs2_lock_res_ops ocfs2_flock_lops = {
259 .get_osb = ocfs2_get_file_osb,
264 * This is the filesystem locking protocol version.
266 * Whenever the filesystem does new things with locks (adds or removes a
267 * lock, orders them differently, does different things underneath a lock),
268 * the version must be changed. The protocol is negotiated when joining
269 * the dlm domain. A node may join the domain if its major version is
270 * identical to all other nodes and its minor version is greater than
271 * or equal to all other nodes. When its minor version is greater than
272 * the other nodes, it will run at the minor version specified by the
275 * If a locking change is made that will not be compatible with older
276 * versions, the major number must be increased and the minor version set
277 * to zero. If a change merely adds a behavior that can be disabled when
278 * speaking to older versions, the minor version must be increased. If a
279 * change adds a fully backwards compatible change (eg, LVB changes that
280 * are just ignored by older versions), the version does not need to be
283 const struct dlm_protocol_version ocfs2_locking_protocol = {
284 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
285 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
288 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
290 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
291 lockres->l_type == OCFS2_LOCK_TYPE_RW ||
292 lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
295 static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
297 BUG_ON(!ocfs2_is_inode_lock(lockres));
299 return (struct inode *) lockres->l_priv;
302 static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
304 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
306 return (struct ocfs2_dentry_lock *)lockres->l_priv;
309 static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
311 if (lockres->l_ops->get_osb)
312 return lockres->l_ops->get_osb(lockres);
314 return (struct ocfs2_super *)lockres->l_priv;
317 static int ocfs2_lock_create(struct ocfs2_super *osb,
318 struct ocfs2_lock_res *lockres,
321 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
323 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
324 struct ocfs2_lock_res *lockres,
326 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
327 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
328 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
329 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
330 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
331 struct ocfs2_lock_res *lockres);
332 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
334 #define ocfs2_log_dlm_error(_func, _stat, _lockres) do { \
335 mlog(ML_ERROR, "Dlm error \"%s\" while calling %s on " \
336 "resource %s: %s\n", dlm_errname(_stat), _func, \
337 _lockres->l_name, dlm_errmsg(_stat)); \
339 static int ocfs2_downconvert_thread(void *arg);
340 static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
341 struct ocfs2_lock_res *lockres);
342 static int ocfs2_inode_lock_update(struct inode *inode,
343 struct buffer_head **bh);
344 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
345 static inline int ocfs2_highest_compat_lock_level(int level);
346 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
348 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
349 struct ocfs2_lock_res *lockres,
352 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
353 struct ocfs2_lock_res *lockres);
354 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
355 struct ocfs2_lock_res *lockres);
358 static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
367 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
369 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
370 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
371 (long long)blkno, generation);
373 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
375 mlog(0, "built lock resource with name: %s\n", name);
380 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
382 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
383 struct ocfs2_dlm_debug *dlm_debug)
385 mlog(0, "Add tracking for lockres %s\n", res->l_name);
387 spin_lock(&ocfs2_dlm_tracking_lock);
388 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
389 spin_unlock(&ocfs2_dlm_tracking_lock);
392 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
394 spin_lock(&ocfs2_dlm_tracking_lock);
395 if (!list_empty(&res->l_debug_list))
396 list_del_init(&res->l_debug_list);
397 spin_unlock(&ocfs2_dlm_tracking_lock);
400 static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
401 struct ocfs2_lock_res *res,
402 enum ocfs2_lock_type type,
403 struct ocfs2_lock_res_ops *ops,
410 res->l_level = LKM_IVMODE;
411 res->l_requested = LKM_IVMODE;
412 res->l_blocking = LKM_IVMODE;
413 res->l_action = OCFS2_AST_INVALID;
414 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
416 res->l_flags = OCFS2_LOCK_INITIALIZED;
418 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
421 void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
423 /* This also clears out the lock status block */
424 memset(res, 0, sizeof(struct ocfs2_lock_res));
425 spin_lock_init(&res->l_lock);
426 init_waitqueue_head(&res->l_event);
427 INIT_LIST_HEAD(&res->l_blocked_list);
428 INIT_LIST_HEAD(&res->l_mask_waiters);
431 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
432 enum ocfs2_lock_type type,
433 unsigned int generation,
436 struct ocfs2_lock_res_ops *ops;
439 case OCFS2_LOCK_TYPE_RW:
440 ops = &ocfs2_inode_rw_lops;
442 case OCFS2_LOCK_TYPE_META:
443 ops = &ocfs2_inode_inode_lops;
445 case OCFS2_LOCK_TYPE_OPEN:
446 ops = &ocfs2_inode_open_lops;
449 mlog_bug_on_msg(1, "type: %d\n", type);
450 ops = NULL; /* thanks, gcc */
454 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
455 generation, res->l_name);
456 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
459 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
461 struct inode *inode = ocfs2_lock_res_inode(lockres);
463 return OCFS2_SB(inode->i_sb);
466 static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
468 struct ocfs2_file_private *fp = lockres->l_priv;
470 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
473 static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
475 __be64 inode_blkno_be;
477 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
480 return be64_to_cpu(inode_blkno_be);
483 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
485 struct ocfs2_dentry_lock *dl = lockres->l_priv;
487 return OCFS2_SB(dl->dl_inode->i_sb);
490 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
491 u64 parent, struct inode *inode)
494 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
495 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
496 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
498 ocfs2_lock_res_init_once(lockres);
501 * Unfortunately, the standard lock naming scheme won't work
502 * here because we have two 16 byte values to use. Instead,
503 * we'll stuff the inode number as a binary value. We still
504 * want error prints to show something without garbling the
505 * display, so drop a null byte in there before the inode
506 * number. A future version of OCFS2 will likely use all
507 * binary lock names. The stringified names have been a
508 * tremendous aid in debugging, but now that the debugfs
509 * interface exists, we can mangle things there if need be.
511 * NOTE: We also drop the standard "pad" value (the total lock
512 * name size stays the same though - the last part is all
513 * zeros due to the memset in ocfs2_lock_res_init_once()
515 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
517 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
520 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
522 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
525 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
526 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
530 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
531 struct ocfs2_super *osb)
533 /* Superblock lockres doesn't come from a slab so we call init
534 * once on it manually. */
535 ocfs2_lock_res_init_once(res);
536 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
538 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
539 &ocfs2_super_lops, osb);
542 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
543 struct ocfs2_super *osb)
545 /* Rename lockres doesn't come from a slab so we call init
546 * once on it manually. */
547 ocfs2_lock_res_init_once(res);
548 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
549 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
550 &ocfs2_rename_lops, osb);
553 void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
554 struct ocfs2_file_private *fp)
556 struct inode *inode = fp->fp_file->f_mapping->host;
557 struct ocfs2_inode_info *oi = OCFS2_I(inode);
559 ocfs2_lock_res_init_once(lockres);
560 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
561 inode->i_generation, lockres->l_name);
562 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
563 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
565 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
568 void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
572 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
575 ocfs2_remove_lockres_tracking(res);
577 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
578 "Lockres %s is on the blocked list\n",
580 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
581 "Lockres %s has mask waiters pending\n",
583 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
584 "Lockres %s is locked\n",
586 mlog_bug_on_msg(res->l_ro_holders,
587 "Lockres %s has %u ro holders\n",
588 res->l_name, res->l_ro_holders);
589 mlog_bug_on_msg(res->l_ex_holders,
590 "Lockres %s has %u ex holders\n",
591 res->l_name, res->l_ex_holders);
593 /* Need to clear out the lock status block for the dlm */
594 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
600 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
609 lockres->l_ex_holders++;
612 lockres->l_ro_holders++;
621 static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
630 BUG_ON(!lockres->l_ex_holders);
631 lockres->l_ex_holders--;
634 BUG_ON(!lockres->l_ro_holders);
635 lockres->l_ro_holders--;
643 /* WARNING: This function lives in a world where the only three lock
644 * levels are EX, PR, and NL. It *will* have to be adjusted when more
645 * lock types are added. */
646 static inline int ocfs2_highest_compat_lock_level(int level)
648 int new_level = LKM_EXMODE;
650 if (level == LKM_EXMODE)
651 new_level = LKM_NLMODE;
652 else if (level == LKM_PRMODE)
653 new_level = LKM_PRMODE;
657 static void lockres_set_flags(struct ocfs2_lock_res *lockres,
658 unsigned long newflags)
660 struct ocfs2_mask_waiter *mw, *tmp;
662 assert_spin_locked(&lockres->l_lock);
664 lockres->l_flags = newflags;
666 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
667 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
670 list_del_init(&mw->mw_item);
672 complete(&mw->mw_complete);
675 static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
677 lockres_set_flags(lockres, lockres->l_flags | or);
679 static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
682 lockres_set_flags(lockres, lockres->l_flags & ~clear);
685 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
689 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
690 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
691 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
692 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
694 lockres->l_level = lockres->l_requested;
695 if (lockres->l_level <=
696 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
697 lockres->l_blocking = LKM_NLMODE;
698 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
700 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
705 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
709 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
710 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
712 /* Convert from RO to EX doesn't really need anything as our
713 * information is already up to data. Convert from NL to
714 * *anything* however should mark ourselves as needing an
716 if (lockres->l_level == LKM_NLMODE &&
717 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
718 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
720 lockres->l_level = lockres->l_requested;
721 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
726 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
730 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
731 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
733 if (lockres->l_requested > LKM_NLMODE &&
734 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
735 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
736 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
738 lockres->l_level = lockres->l_requested;
739 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
740 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
745 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
748 int needs_downconvert = 0;
751 assert_spin_locked(&lockres->l_lock);
753 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
755 if (level > lockres->l_blocking) {
756 /* only schedule a downconvert if we haven't already scheduled
757 * one that goes low enough to satisfy the level we're
758 * blocking. this also catches the case where we get
760 if (ocfs2_highest_compat_lock_level(level) <
761 ocfs2_highest_compat_lock_level(lockres->l_blocking))
762 needs_downconvert = 1;
764 lockres->l_blocking = level;
767 mlog_exit(needs_downconvert);
768 return needs_downconvert;
771 static void ocfs2_blocking_ast(void *opaque, int level)
773 struct ocfs2_lock_res *lockres = opaque;
774 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
775 int needs_downconvert;
778 BUG_ON(level <= LKM_NLMODE);
780 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
781 lockres->l_name, level, lockres->l_level,
782 ocfs2_lock_type_string(lockres->l_type));
785 * We can skip the bast for locks which don't enable caching -
786 * they'll be dropped at the earliest possible time anyway.
788 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
791 spin_lock_irqsave(&lockres->l_lock, flags);
792 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
793 if (needs_downconvert)
794 ocfs2_schedule_blocked_lock(osb, lockres);
795 spin_unlock_irqrestore(&lockres->l_lock, flags);
797 wake_up(&lockres->l_event);
799 ocfs2_wake_downconvert_thread(osb);
802 static void ocfs2_locking_ast(void *opaque)
804 struct ocfs2_lock_res *lockres = opaque;
805 struct dlm_lockstatus *lksb = &lockres->l_lksb;
808 spin_lock_irqsave(&lockres->l_lock, flags);
810 if (lksb->status != DLM_NORMAL) {
811 mlog(ML_ERROR, "lockres %s: lksb status value of %u!\n",
812 lockres->l_name, lksb->status);
813 spin_unlock_irqrestore(&lockres->l_lock, flags);
817 switch(lockres->l_action) {
818 case OCFS2_AST_ATTACH:
819 ocfs2_generic_handle_attach_action(lockres);
820 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
822 case OCFS2_AST_CONVERT:
823 ocfs2_generic_handle_convert_action(lockres);
825 case OCFS2_AST_DOWNCONVERT:
826 ocfs2_generic_handle_downconvert_action(lockres);
829 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
830 "lockres flags = 0x%lx, unlock action: %u\n",
831 lockres->l_name, lockres->l_action, lockres->l_flags,
832 lockres->l_unlock_action);
836 /* set it to something invalid so if we get called again we
838 lockres->l_action = OCFS2_AST_INVALID;
840 wake_up(&lockres->l_event);
841 spin_unlock_irqrestore(&lockres->l_lock, flags);
844 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
850 spin_lock_irqsave(&lockres->l_lock, flags);
851 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
853 lockres->l_action = OCFS2_AST_INVALID;
855 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
856 spin_unlock_irqrestore(&lockres->l_lock, flags);
858 wake_up(&lockres->l_event);
862 /* Note: If we detect another process working on the lock (i.e.,
863 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
864 * to do the right thing in that case.
866 static int ocfs2_lock_create(struct ocfs2_super *osb,
867 struct ocfs2_lock_res *lockres,
872 enum dlm_status status = DLM_NORMAL;
877 mlog(0, "lock %s, level = %d, flags = %d\n", lockres->l_name, level,
880 spin_lock_irqsave(&lockres->l_lock, flags);
881 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
882 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
883 spin_unlock_irqrestore(&lockres->l_lock, flags);
887 lockres->l_action = OCFS2_AST_ATTACH;
888 lockres->l_requested = level;
889 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
890 spin_unlock_irqrestore(&lockres->l_lock, flags);
892 status = ocfs2_dlm_lock(osb->dlm,
897 OCFS2_LOCK_ID_MAX_LEN - 1,
899 if (status != DLM_NORMAL) {
900 ocfs2_log_dlm_error("ocfs2_dlm_lock", status, lockres);
902 ocfs2_recover_from_dlm_error(lockres, 1);
905 mlog(0, "lock %s, successfull return from ocfs2_dlm_lock\n",
913 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
919 spin_lock_irqsave(&lockres->l_lock, flags);
920 ret = lockres->l_flags & flag;
921 spin_unlock_irqrestore(&lockres->l_lock, flags);
926 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
929 wait_event(lockres->l_event,
930 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
933 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
936 wait_event(lockres->l_event,
937 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
940 /* predict what lock level we'll be dropping down to on behalf
941 * of another node, and return true if the currently wanted
942 * level will be compatible with it. */
943 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
946 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
948 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
951 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
953 INIT_LIST_HEAD(&mw->mw_item);
954 init_completion(&mw->mw_complete);
957 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
959 wait_for_completion(&mw->mw_complete);
960 /* Re-arm the completion in case we want to wait on it again */
961 INIT_COMPLETION(mw->mw_complete);
962 return mw->mw_status;
965 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
966 struct ocfs2_mask_waiter *mw,
970 BUG_ON(!list_empty(&mw->mw_item));
972 assert_spin_locked(&lockres->l_lock);
974 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
979 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
980 * if the mask still hadn't reached its goal */
981 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
982 struct ocfs2_mask_waiter *mw)
987 spin_lock_irqsave(&lockres->l_lock, flags);
988 if (!list_empty(&mw->mw_item)) {
989 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
992 list_del_init(&mw->mw_item);
993 init_completion(&mw->mw_complete);
995 spin_unlock_irqrestore(&lockres->l_lock, flags);
1001 static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
1002 struct ocfs2_lock_res *lockres)
1006 ret = wait_for_completion_interruptible(&mw->mw_complete);
1008 lockres_remove_mask_waiter(lockres, mw);
1010 ret = mw->mw_status;
1011 /* Re-arm the completion in case we want to wait on it again */
1012 INIT_COMPLETION(mw->mw_complete);
1016 static int ocfs2_cluster_lock(struct ocfs2_super *osb,
1017 struct ocfs2_lock_res *lockres,
1022 struct ocfs2_mask_waiter mw;
1023 enum dlm_status status;
1024 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
1025 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1026 unsigned long flags;
1030 ocfs2_init_mask_waiter(&mw);
1032 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
1033 lkm_flags |= LKM_VALBLK;
1038 if (catch_signals && signal_pending(current)) {
1043 spin_lock_irqsave(&lockres->l_lock, flags);
1045 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1046 "Cluster lock called on freeing lockres %s! flags "
1047 "0x%lx\n", lockres->l_name, lockres->l_flags);
1049 /* We only compare against the currently granted level
1050 * here. If the lock is blocked waiting on a downconvert,
1051 * we'll get caught below. */
1052 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1053 level > lockres->l_level) {
1054 /* is someone sitting in dlm_lock? If so, wait on
1056 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1061 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1062 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1063 /* is the lock is currently blocked on behalf of
1065 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1070 if (level > lockres->l_level) {
1071 if (lockres->l_action != OCFS2_AST_INVALID)
1072 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1073 lockres->l_name, lockres->l_action);
1075 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1076 lockres->l_action = OCFS2_AST_ATTACH;
1077 lkm_flags &= ~LKM_CONVERT;
1079 lockres->l_action = OCFS2_AST_CONVERT;
1080 lkm_flags |= LKM_CONVERT;
1083 lockres->l_requested = level;
1084 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1085 spin_unlock_irqrestore(&lockres->l_lock, flags);
1087 BUG_ON(level == LKM_IVMODE);
1088 BUG_ON(level == LKM_NLMODE);
1090 mlog(0, "lock %s, convert from %d to level = %d\n",
1091 lockres->l_name, lockres->l_level, level);
1093 /* call dlm_lock to upgrade lock now */
1094 status = ocfs2_dlm_lock(osb->dlm,
1099 OCFS2_LOCK_ID_MAX_LEN - 1,
1101 if (status != DLM_NORMAL) {
1102 if ((lkm_flags & LKM_NOQUEUE) &&
1103 (status == DLM_NOTQUEUED))
1106 ocfs2_log_dlm_error("ocfs2_dlm_lock",
1110 ocfs2_recover_from_dlm_error(lockres, 1);
1114 mlog(0, "lock %s, successfull return from ocfs2_dlm_lock\n",
1117 /* At this point we've gone inside the dlm and need to
1118 * complete our work regardless. */
1121 /* wait for busy to clear and carry on */
1125 /* Ok, if we get here then we're good to go. */
1126 ocfs2_inc_holders(lockres, level);
1130 spin_unlock_irqrestore(&lockres->l_lock, flags);
1133 * This is helping work around a lock inversion between the page lock
1134 * and dlm locks. One path holds the page lock while calling aops
1135 * which block acquiring dlm locks. The voting thread holds dlm
1136 * locks while acquiring page locks while down converting data locks.
1137 * This block is helping an aop path notice the inversion and back
1138 * off to unlock its page lock before trying the dlm lock again.
1140 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1141 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1143 if (lockres_remove_mask_waiter(lockres, &mw))
1149 ret = ocfs2_wait_for_mask(&mw);
1159 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1160 struct ocfs2_lock_res *lockres,
1163 unsigned long flags;
1166 spin_lock_irqsave(&lockres->l_lock, flags);
1167 ocfs2_dec_holders(lockres, level);
1168 ocfs2_downconvert_on_unlock(osb, lockres);
1169 spin_unlock_irqrestore(&lockres->l_lock, flags);
1173 static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1174 struct ocfs2_lock_res *lockres,
1178 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1179 unsigned long flags;
1180 int lkm_flags = local ? LKM_LOCAL : 0;
1182 spin_lock_irqsave(&lockres->l_lock, flags);
1183 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1184 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1185 spin_unlock_irqrestore(&lockres->l_lock, flags);
1187 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
1190 /* Grants us an EX lock on the data and metadata resources, skipping
1191 * the normal cluster directory lookup. Use this ONLY on newly created
1192 * inodes which other nodes can't possibly see, and which haven't been
1193 * hashed in the inode hash yet. This can give us a good performance
1194 * increase as it'll skip the network broadcast normally associated
1195 * with creating a new lock resource. */
1196 int ocfs2_create_new_inode_locks(struct inode *inode)
1199 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1202 BUG_ON(!ocfs2_inode_is_new(inode));
1206 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1208 /* NOTE: That we don't increment any of the holder counts, nor
1209 * do we add anything to a journal handle. Since this is
1210 * supposed to be a new inode which the cluster doesn't know
1211 * about yet, there is no need to. As far as the LVB handling
1212 * is concerned, this is basically like acquiring an EX lock
1213 * on a resource which has an invalid one -- we'll set it
1214 * valid when we release the EX. */
1216 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
1223 * We don't want to use LKM_LOCAL on a meta data lock as they
1224 * don't use a generation in their lock names.
1226 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
1232 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1243 int ocfs2_rw_lock(struct inode *inode, int write)
1246 struct ocfs2_lock_res *lockres;
1247 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1253 mlog(0, "inode %llu take %s RW lock\n",
1254 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1255 write ? "EXMODE" : "PRMODE");
1257 if (ocfs2_mount_local(osb))
1260 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1262 level = write ? LKM_EXMODE : LKM_PRMODE;
1264 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1273 void ocfs2_rw_unlock(struct inode *inode, int write)
1275 int level = write ? LKM_EXMODE : LKM_PRMODE;
1276 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1277 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1281 mlog(0, "inode %llu drop %s RW lock\n",
1282 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1283 write ? "EXMODE" : "PRMODE");
1285 if (!ocfs2_mount_local(osb))
1286 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1292 * ocfs2_open_lock always get PR mode lock.
1294 int ocfs2_open_lock(struct inode *inode)
1297 struct ocfs2_lock_res *lockres;
1298 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1304 mlog(0, "inode %llu take PRMODE open lock\n",
1305 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1307 if (ocfs2_mount_local(osb))
1310 lockres = &OCFS2_I(inode)->ip_open_lockres;
1312 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1322 int ocfs2_try_open_lock(struct inode *inode, int write)
1324 int status = 0, level;
1325 struct ocfs2_lock_res *lockres;
1326 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1332 mlog(0, "inode %llu try to take %s open lock\n",
1333 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1334 write ? "EXMODE" : "PRMODE");
1336 if (ocfs2_mount_local(osb))
1339 lockres = &OCFS2_I(inode)->ip_open_lockres;
1341 level = write ? LKM_EXMODE : LKM_PRMODE;
1344 * The file system may already holding a PRMODE/EXMODE open lock.
1345 * Since we pass LKM_NOQUEUE, the request won't block waiting on
1346 * other nodes and the -EAGAIN will indicate to the caller that
1347 * this inode is still in use.
1349 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1350 level, LKM_NOQUEUE, 0);
1358 * ocfs2_open_unlock unlock PR and EX mode open locks.
1360 void ocfs2_open_unlock(struct inode *inode)
1362 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1363 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1367 mlog(0, "inode %llu drop open lock\n",
1368 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1370 if (ocfs2_mount_local(osb))
1373 if(lockres->l_ro_holders)
1374 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1376 if(lockres->l_ex_holders)
1377 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1384 static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1388 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1389 unsigned long flags;
1390 struct ocfs2_mask_waiter mw;
1392 ocfs2_init_mask_waiter(&mw);
1395 spin_lock_irqsave(&lockres->l_lock, flags);
1396 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1397 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1399 spin_unlock_irqrestore(&lockres->l_lock, flags);
1400 ret = ocfs2_cancel_convert(osb, lockres);
1407 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1408 spin_unlock_irqrestore(&lockres->l_lock, flags);
1410 ocfs2_wait_for_mask(&mw);
1416 * We may still have gotten the lock, in which case there's no
1417 * point to restarting the syscall.
1419 if (lockres->l_level == level)
1422 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1423 lockres->l_flags, lockres->l_level, lockres->l_action);
1425 spin_unlock_irqrestore(&lockres->l_lock, flags);
1432 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1433 * flock() calls. The locking approach this requires is sufficiently
1434 * different from all other cluster lock types that we implement a
1435 * seperate path to the "low-level" dlm calls. In particular:
1437 * - No optimization of lock levels is done - we take at exactly
1438 * what's been requested.
1440 * - No lock caching is employed. We immediately downconvert to
1441 * no-lock at unlock time. This also means flock locks never go on
1442 * the blocking list).
1444 * - Since userspace can trivially deadlock itself with flock, we make
1445 * sure to allow cancellation of a misbehaving applications flock()
1448 * - Access to any flock lockres doesn't require concurrency, so we
1449 * can simplify the code by requiring the caller to guarantee
1450 * serialization of dlmglue flock calls.
1452 int ocfs2_file_lock(struct file *file, int ex, int trylock)
1454 int ret, level = ex ? LKM_EXMODE : LKM_PRMODE;
1455 unsigned int lkm_flags = trylock ? LKM_NOQUEUE : 0;
1456 unsigned long flags;
1457 struct ocfs2_file_private *fp = file->private_data;
1458 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1459 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1460 struct ocfs2_mask_waiter mw;
1462 ocfs2_init_mask_waiter(&mw);
1464 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
1465 (lockres->l_level > LKM_NLMODE)) {
1467 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1468 "level: %u\n", lockres->l_name, lockres->l_flags,
1473 spin_lock_irqsave(&lockres->l_lock, flags);
1474 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1475 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1476 spin_unlock_irqrestore(&lockres->l_lock, flags);
1479 * Get the lock at NLMODE to start - that way we
1480 * can cancel the upconvert request if need be.
1482 ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
1488 ret = ocfs2_wait_for_mask(&mw);
1493 spin_lock_irqsave(&lockres->l_lock, flags);
1496 lockres->l_action = OCFS2_AST_CONVERT;
1497 lkm_flags |= LKM_CONVERT;
1498 lockres->l_requested = level;
1499 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1501 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1502 spin_unlock_irqrestore(&lockres->l_lock, flags);
1504 ret = ocfs2_dlm_lock(osb->dlm, level, &lockres->l_lksb, lkm_flags,
1505 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1,
1507 if (ret != DLM_NORMAL) {
1508 if (trylock && ret == DLM_NOTQUEUED)
1511 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
1515 ocfs2_recover_from_dlm_error(lockres, 1);
1516 lockres_remove_mask_waiter(lockres, &mw);
1520 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1521 if (ret == -ERESTARTSYS) {
1523 * Userspace can cause deadlock itself with
1524 * flock(). Current behavior locally is to allow the
1525 * deadlock, but abort the system call if a signal is
1526 * received. We follow this example, otherwise a
1527 * poorly written program could sit in kernel until
1530 * Handling this is a bit more complicated for Ocfs2
1531 * though. We can't exit this function with an
1532 * outstanding lock request, so a cancel convert is
1533 * required. We intentionally overwrite 'ret' - if the
1534 * cancel fails and the lock was granted, it's easier
1535 * to just bubble sucess back up to the user.
1537 ret = ocfs2_flock_handle_signal(lockres, level);
1542 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1543 lockres->l_name, ex, trylock, ret);
1547 void ocfs2_file_unlock(struct file *file)
1550 unsigned long flags;
1551 struct ocfs2_file_private *fp = file->private_data;
1552 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1553 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1554 struct ocfs2_mask_waiter mw;
1556 ocfs2_init_mask_waiter(&mw);
1558 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
1561 if (lockres->l_level == LKM_NLMODE)
1564 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1565 lockres->l_name, lockres->l_flags, lockres->l_level,
1568 spin_lock_irqsave(&lockres->l_lock, flags);
1570 * Fake a blocking ast for the downconvert code.
1572 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
1573 lockres->l_blocking = LKM_EXMODE;
1575 ocfs2_prepare_downconvert(lockres, LKM_NLMODE);
1576 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1577 spin_unlock_irqrestore(&lockres->l_lock, flags);
1579 ret = ocfs2_downconvert_lock(osb, lockres, LKM_NLMODE, 0);
1585 ret = ocfs2_wait_for_mask(&mw);
1590 static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1591 struct ocfs2_lock_res *lockres)
1597 /* If we know that another node is waiting on our lock, kick
1598 * the downconvert thread * pre-emptively when we reach a release
1600 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1601 switch(lockres->l_blocking) {
1603 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1607 if (!lockres->l_ex_holders)
1616 ocfs2_wake_downconvert_thread(osb);
1621 #define OCFS2_SEC_BITS 34
1622 #define OCFS2_SEC_SHIFT (64 - 34)
1623 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1625 /* LVB only has room for 64 bits of time here so we pack it for
1627 static u64 ocfs2_pack_timespec(struct timespec *spec)
1630 u64 sec = spec->tv_sec;
1631 u32 nsec = spec->tv_nsec;
1633 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1638 /* Call this with the lockres locked. I am reasonably sure we don't
1639 * need ip_lock in this function as anyone who would be changing those
1640 * values is supposed to be blocked in ocfs2_inode_lock right now. */
1641 static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1643 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1644 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1645 struct ocfs2_meta_lvb *lvb;
1649 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1652 * Invalidate the LVB of a deleted inode - this way other
1653 * nodes are forced to go to disk and discover the new inode
1656 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1657 lvb->lvb_version = 0;
1661 lvb->lvb_version = OCFS2_LVB_VERSION;
1662 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1663 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1664 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1665 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1666 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1667 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1668 lvb->lvb_iatime_packed =
1669 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1670 lvb->lvb_ictime_packed =
1671 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1672 lvb->lvb_imtime_packed =
1673 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1674 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
1675 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
1676 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
1679 mlog_meta_lvb(0, lockres);
1684 static void ocfs2_unpack_timespec(struct timespec *spec,
1687 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1688 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1691 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1693 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1694 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1695 struct ocfs2_meta_lvb *lvb;
1699 mlog_meta_lvb(0, lockres);
1701 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1703 /* We're safe here without the lockres lock... */
1704 spin_lock(&oi->ip_lock);
1705 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1706 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1708 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1709 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
1710 ocfs2_set_inode_flags(inode);
1712 /* fast-symlinks are a special case */
1713 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1714 inode->i_blocks = 0;
1716 inode->i_blocks = ocfs2_inode_sector_count(inode);
1718 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1719 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1720 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1721 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1722 ocfs2_unpack_timespec(&inode->i_atime,
1723 be64_to_cpu(lvb->lvb_iatime_packed));
1724 ocfs2_unpack_timespec(&inode->i_mtime,
1725 be64_to_cpu(lvb->lvb_imtime_packed));
1726 ocfs2_unpack_timespec(&inode->i_ctime,
1727 be64_to_cpu(lvb->lvb_ictime_packed));
1728 spin_unlock(&oi->ip_lock);
1733 static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1734 struct ocfs2_lock_res *lockres)
1736 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1738 if (lvb->lvb_version == OCFS2_LVB_VERSION
1739 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
1744 /* Determine whether a lock resource needs to be refreshed, and
1745 * arbitrate who gets to refresh it.
1747 * 0 means no refresh needed.
1749 * > 0 means you need to refresh this and you MUST call
1750 * ocfs2_complete_lock_res_refresh afterwards. */
1751 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1753 unsigned long flags;
1759 spin_lock_irqsave(&lockres->l_lock, flags);
1760 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1761 spin_unlock_irqrestore(&lockres->l_lock, flags);
1765 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1766 spin_unlock_irqrestore(&lockres->l_lock, flags);
1768 ocfs2_wait_on_refreshing_lock(lockres);
1772 /* Ok, I'll be the one to refresh this lock. */
1773 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1774 spin_unlock_irqrestore(&lockres->l_lock, flags);
1782 /* If status is non zero, I'll mark it as not being in refresh
1783 * anymroe, but i won't clear the needs refresh flag. */
1784 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1787 unsigned long flags;
1790 spin_lock_irqsave(&lockres->l_lock, flags);
1791 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1793 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1794 spin_unlock_irqrestore(&lockres->l_lock, flags);
1796 wake_up(&lockres->l_event);
1801 /* may or may not return a bh if it went to disk. */
1802 static int ocfs2_inode_lock_update(struct inode *inode,
1803 struct buffer_head **bh)
1806 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1807 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1808 struct ocfs2_dinode *fe;
1809 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1813 if (ocfs2_mount_local(osb))
1816 spin_lock(&oi->ip_lock);
1817 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1818 mlog(0, "Orphaned inode %llu was deleted while we "
1819 "were waiting on a lock. ip_flags = 0x%x\n",
1820 (unsigned long long)oi->ip_blkno, oi->ip_flags);
1821 spin_unlock(&oi->ip_lock);
1825 spin_unlock(&oi->ip_lock);
1827 if (!ocfs2_should_refresh_lock_res(lockres))
1830 /* This will discard any caching information we might have had
1831 * for the inode metadata. */
1832 ocfs2_metadata_cache_purge(inode);
1834 ocfs2_extent_map_trunc(inode, 0);
1836 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
1837 mlog(0, "Trusting LVB on inode %llu\n",
1838 (unsigned long long)oi->ip_blkno);
1839 ocfs2_refresh_inode_from_lvb(inode);
1841 /* Boo, we have to go to disk. */
1842 /* read bh, cast, ocfs2_refresh_inode */
1843 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1844 bh, OCFS2_BH_CACHED, inode);
1849 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1851 /* This is a good chance to make sure we're not
1852 * locking an invalid object.
1854 * We bug on a stale inode here because we checked
1855 * above whether it was wiped from disk. The wiping
1856 * node provides a guarantee that we receive that
1857 * message and can mark the inode before dropping any
1858 * locks associated with it. */
1859 if (!OCFS2_IS_VALID_DINODE(fe)) {
1860 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1864 mlog_bug_on_msg(inode->i_generation !=
1865 le32_to_cpu(fe->i_generation),
1866 "Invalid dinode %llu disk generation: %u "
1867 "inode->i_generation: %u\n",
1868 (unsigned long long)oi->ip_blkno,
1869 le32_to_cpu(fe->i_generation),
1870 inode->i_generation);
1871 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1872 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
1873 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1874 (unsigned long long)oi->ip_blkno,
1875 (unsigned long long)le64_to_cpu(fe->i_dtime),
1876 le32_to_cpu(fe->i_flags));
1878 ocfs2_refresh_inode(inode, fe);
1883 ocfs2_complete_lock_res_refresh(lockres, status);
1889 static int ocfs2_assign_bh(struct inode *inode,
1890 struct buffer_head **ret_bh,
1891 struct buffer_head *passed_bh)
1896 /* Ok, the update went to disk for us, use the
1898 *ret_bh = passed_bh;
1904 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1905 OCFS2_I(inode)->ip_blkno,
1916 * returns < 0 error if the callback will never be called, otherwise
1917 * the result of the lock will be communicated via the callback.
1919 int ocfs2_inode_lock_full(struct inode *inode,
1920 struct buffer_head **ret_bh,
1924 int status, level, dlm_flags, acquired;
1925 struct ocfs2_lock_res *lockres = NULL;
1926 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1927 struct buffer_head *local_bh = NULL;
1933 mlog(0, "inode %llu, take %s META lock\n",
1934 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1935 ex ? "EXMODE" : "PRMODE");
1939 /* We'll allow faking a readonly metadata lock for
1941 if (ocfs2_is_hard_readonly(osb)) {
1947 if (ocfs2_mount_local(osb))
1950 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1951 ocfs2_wait_for_recovery(osb);
1953 lockres = &OCFS2_I(inode)->ip_inode_lockres;
1954 level = ex ? LKM_EXMODE : LKM_PRMODE;
1956 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
1957 dlm_flags |= LKM_NOQUEUE;
1959 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1961 if (status != -EAGAIN && status != -EIOCBRETRY)
1966 /* Notify the error cleanup path to drop the cluster lock. */
1969 /* We wait twice because a node may have died while we were in
1970 * the lower dlm layers. The second time though, we've
1971 * committed to owning this lock so we don't allow signals to
1972 * abort the operation. */
1973 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1974 ocfs2_wait_for_recovery(osb);
1978 * We only see this flag if we're being called from
1979 * ocfs2_read_locked_inode(). It means we're locking an inode
1980 * which hasn't been populated yet, so clear the refresh flag
1981 * and let the caller handle it.
1983 if (inode->i_state & I_NEW) {
1986 ocfs2_complete_lock_res_refresh(lockres, 0);
1990 /* This is fun. The caller may want a bh back, or it may
1991 * not. ocfs2_inode_lock_update definitely wants one in, but
1992 * may or may not read one, depending on what's in the
1993 * LVB. The result of all of this is that we've *only* gone to
1994 * disk if we have to, so the complexity is worthwhile. */
1995 status = ocfs2_inode_lock_update(inode, &local_bh);
1997 if (status != -ENOENT)
2003 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
2012 if (ret_bh && (*ret_bh)) {
2017 ocfs2_inode_unlock(inode, ex);
2028 * This is working around a lock inversion between tasks acquiring DLM
2029 * locks while holding a page lock and the downconvert thread which
2030 * blocks dlm lock acquiry while acquiring page locks.
2032 * ** These _with_page variantes are only intended to be called from aop
2033 * methods that hold page locks and return a very specific *positive* error
2034 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2036 * The DLM is called such that it returns -EAGAIN if it would have
2037 * blocked waiting for the downconvert thread. In that case we unlock
2038 * our page so the downconvert thread can make progress. Once we've
2039 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2040 * that called us can bubble that back up into the VFS who will then
2041 * immediately retry the aop call.
2043 * We do a blocking lock and immediate unlock before returning, though, so that
2044 * the lock has a great chance of being cached on this node by the time the VFS
2045 * calls back to retry the aop. This has a potential to livelock as nodes
2046 * ping locks back and forth, but that's a risk we're willing to take to avoid
2047 * the lock inversion simply.
2049 int ocfs2_inode_lock_with_page(struct inode *inode,
2050 struct buffer_head **ret_bh,
2056 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
2057 if (ret == -EAGAIN) {
2059 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2060 ocfs2_inode_unlock(inode, ex);
2061 ret = AOP_TRUNCATED_PAGE;
2067 int ocfs2_inode_lock_atime(struct inode *inode,
2068 struct vfsmount *vfsmnt,
2074 ret = ocfs2_inode_lock(inode, NULL, 0);
2081 * If we should update atime, we will get EX lock,
2082 * otherwise we just get PR lock.
2084 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2085 struct buffer_head *bh = NULL;
2087 ocfs2_inode_unlock(inode, 0);
2088 ret = ocfs2_inode_lock(inode, &bh, 1);
2094 if (ocfs2_should_update_atime(inode, vfsmnt))
2095 ocfs2_update_inode_atime(inode, bh);
2105 void ocfs2_inode_unlock(struct inode *inode,
2108 int level = ex ? LKM_EXMODE : LKM_PRMODE;
2109 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
2110 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2114 mlog(0, "inode %llu drop %s META lock\n",
2115 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2116 ex ? "EXMODE" : "PRMODE");
2118 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2119 !ocfs2_mount_local(osb))
2120 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
2125 int ocfs2_super_lock(struct ocfs2_super *osb,
2129 int level = ex ? LKM_EXMODE : LKM_PRMODE;
2130 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2134 if (ocfs2_is_hard_readonly(osb))
2137 if (ocfs2_mount_local(osb))
2140 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2146 /* The super block lock path is really in the best position to
2147 * know when resources covered by the lock need to be
2148 * refreshed, so we do it here. Of course, making sense of
2149 * everything is up to the caller :) */
2150 status = ocfs2_should_refresh_lock_res(lockres);
2156 status = ocfs2_refresh_slot_info(osb);
2158 ocfs2_complete_lock_res_refresh(lockres, status);
2168 void ocfs2_super_unlock(struct ocfs2_super *osb,
2171 int level = ex ? LKM_EXMODE : LKM_PRMODE;
2172 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2174 if (!ocfs2_mount_local(osb))
2175 ocfs2_cluster_unlock(osb, lockres, level);
2178 int ocfs2_rename_lock(struct ocfs2_super *osb)
2181 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2183 if (ocfs2_is_hard_readonly(osb))
2186 if (ocfs2_mount_local(osb))
2189 status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
2196 void ocfs2_rename_unlock(struct ocfs2_super *osb)
2198 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2200 if (!ocfs2_mount_local(osb))
2201 ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
2204 int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2207 int level = ex ? LKM_EXMODE : LKM_PRMODE;
2208 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2209 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2213 if (ocfs2_is_hard_readonly(osb))
2216 if (ocfs2_mount_local(osb))
2219 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2226 void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2228 int level = ex ? LKM_EXMODE : LKM_PRMODE;
2229 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2230 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2232 if (!ocfs2_mount_local(osb))
2233 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
2236 /* Reference counting of the dlm debug structure. We want this because
2237 * open references on the debug inodes can live on after a mount, so
2238 * we can't rely on the ocfs2_super to always exist. */
2239 static void ocfs2_dlm_debug_free(struct kref *kref)
2241 struct ocfs2_dlm_debug *dlm_debug;
2243 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2248 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2251 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2254 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2256 kref_get(&debug->d_refcnt);
2259 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2261 struct ocfs2_dlm_debug *dlm_debug;
2263 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2265 mlog_errno(-ENOMEM);
2269 kref_init(&dlm_debug->d_refcnt);
2270 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2271 dlm_debug->d_locking_state = NULL;
2276 /* Access to this is arbitrated for us via seq_file->sem. */
2277 struct ocfs2_dlm_seq_priv {
2278 struct ocfs2_dlm_debug *p_dlm_debug;
2279 struct ocfs2_lock_res p_iter_res;
2280 struct ocfs2_lock_res p_tmp_res;
2283 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2284 struct ocfs2_dlm_seq_priv *priv)
2286 struct ocfs2_lock_res *iter, *ret = NULL;
2287 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2289 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2291 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2292 /* discover the head of the list */
2293 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2294 mlog(0, "End of list found, %p\n", ret);
2298 /* We track our "dummy" iteration lockres' by a NULL
2300 if (iter->l_ops != NULL) {
2309 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2311 struct ocfs2_dlm_seq_priv *priv = m->private;
2312 struct ocfs2_lock_res *iter;
2314 spin_lock(&ocfs2_dlm_tracking_lock);
2315 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2317 /* Since lockres' have the lifetime of their container
2318 * (which can be inodes, ocfs2_supers, etc) we want to
2319 * copy this out to a temporary lockres while still
2320 * under the spinlock. Obviously after this we can't
2321 * trust any pointers on the copy returned, but that's
2322 * ok as the information we want isn't typically held
2324 priv->p_tmp_res = *iter;
2325 iter = &priv->p_tmp_res;
2327 spin_unlock(&ocfs2_dlm_tracking_lock);
2332 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2336 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2338 struct ocfs2_dlm_seq_priv *priv = m->private;
2339 struct ocfs2_lock_res *iter = v;
2340 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2342 spin_lock(&ocfs2_dlm_tracking_lock);
2343 iter = ocfs2_dlm_next_res(iter, priv);
2344 list_del_init(&dummy->l_debug_list);
2346 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2347 priv->p_tmp_res = *iter;
2348 iter = &priv->p_tmp_res;
2350 spin_unlock(&ocfs2_dlm_tracking_lock);
2355 /* So that debugfs.ocfs2 can determine which format is being used */
2356 #define OCFS2_DLM_DEBUG_STR_VERSION 1
2357 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2361 struct ocfs2_lock_res *lockres = v;
2366 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2368 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2369 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2371 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2373 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2375 seq_printf(m, "%d\t"
2386 lockres->l_unlock_action,
2387 lockres->l_ro_holders,
2388 lockres->l_ex_holders,
2389 lockres->l_requested,
2390 lockres->l_blocking);
2392 /* Dump the raw LVB */
2393 lvb = lockres->l_lksb.lvb;
2394 for(i = 0; i < DLM_LVB_LEN; i++)
2395 seq_printf(m, "0x%x\t", lvb[i]);
2398 seq_printf(m, "\n");
2402 static const struct seq_operations ocfs2_dlm_seq_ops = {
2403 .start = ocfs2_dlm_seq_start,
2404 .stop = ocfs2_dlm_seq_stop,
2405 .next = ocfs2_dlm_seq_next,
2406 .show = ocfs2_dlm_seq_show,
2409 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2411 struct seq_file *seq = (struct seq_file *) file->private_data;
2412 struct ocfs2_dlm_seq_priv *priv = seq->private;
2413 struct ocfs2_lock_res *res = &priv->p_iter_res;
2415 ocfs2_remove_lockres_tracking(res);
2416 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2417 return seq_release_private(inode, file);
2420 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2423 struct ocfs2_dlm_seq_priv *priv;
2424 struct seq_file *seq;
2425 struct ocfs2_super *osb;
2427 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2433 osb = inode->i_private;
2434 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2435 priv->p_dlm_debug = osb->osb_dlm_debug;
2436 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2438 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2445 seq = (struct seq_file *) file->private_data;
2446 seq->private = priv;
2448 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2455 static const struct file_operations ocfs2_dlm_debug_fops = {
2456 .open = ocfs2_dlm_debug_open,
2457 .release = ocfs2_dlm_debug_release,
2459 .llseek = seq_lseek,
2462 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2465 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2467 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2469 osb->osb_debug_root,
2471 &ocfs2_dlm_debug_fops);
2472 if (!dlm_debug->d_locking_state) {
2475 "Unable to create locking state debugfs file.\n");
2479 ocfs2_get_dlm_debug(dlm_debug);
2484 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2486 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2489 debugfs_remove(dlm_debug->d_locking_state);
2490 ocfs2_put_dlm_debug(dlm_debug);
2494 int ocfs2_dlm_init(struct ocfs2_super *osb)
2498 struct dlm_ctxt *dlm = NULL;
2502 if (ocfs2_mount_local(osb))
2505 status = ocfs2_dlm_init_debug(osb);
2511 /* launch downconvert thread */
2512 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2513 if (IS_ERR(osb->dc_task)) {
2514 status = PTR_ERR(osb->dc_task);
2515 osb->dc_task = NULL;
2520 /* used by the dlm code to make message headers unique, each
2521 * node in this domain must agree on this. */
2522 dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
2524 /* for now, uuid == domain */
2525 dlm = dlm_register_domain(osb->uuid_str, dlm_key,
2526 &osb->osb_locking_proto);
2528 status = PTR_ERR(dlm);
2533 dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
2536 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2537 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2544 ocfs2_dlm_shutdown_debug(osb);
2546 kthread_stop(osb->dc_task);
2553 void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2557 dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
2559 ocfs2_drop_osb_locks(osb);
2562 kthread_stop(osb->dc_task);
2563 osb->dc_task = NULL;
2566 ocfs2_lock_res_free(&osb->osb_super_lockres);
2567 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2569 dlm_unregister_domain(osb->dlm);
2572 ocfs2_dlm_shutdown_debug(osb);
2577 static void ocfs2_unlock_ast(void *opaque, enum dlm_status status)
2579 struct ocfs2_lock_res *lockres = opaque;
2580 unsigned long flags;
2584 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2585 lockres->l_unlock_action);
2587 spin_lock_irqsave(&lockres->l_lock, flags);
2588 /* We tried to cancel a convert request, but it was already
2589 * granted. All we want to do here is clear our unlock
2590 * state. The wake_up call done at the bottom is redundant
2591 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2592 * hurt anything anyway */
2593 if (status == DLM_CANCELGRANT &&
2594 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2595 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2597 /* We don't clear the busy flag in this case as it
2598 * should have been cleared by the ast which the dlm
2600 goto complete_unlock;
2603 if (status != DLM_NORMAL) {
2604 mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
2605 "unlock_action %d\n", status, lockres->l_name,
2606 lockres->l_unlock_action);
2607 spin_unlock_irqrestore(&lockres->l_lock, flags);
2611 switch(lockres->l_unlock_action) {
2612 case OCFS2_UNLOCK_CANCEL_CONVERT:
2613 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2614 lockres->l_action = OCFS2_AST_INVALID;
2616 case OCFS2_UNLOCK_DROP_LOCK:
2617 lockres->l_level = LKM_IVMODE;
2623 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2625 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2626 spin_unlock_irqrestore(&lockres->l_lock, flags);
2628 wake_up(&lockres->l_event);
2633 static int ocfs2_drop_lock(struct ocfs2_super *osb,
2634 struct ocfs2_lock_res *lockres)
2636 enum dlm_status status;
2637 unsigned long flags;
2640 /* We didn't get anywhere near actually using this lockres. */
2641 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2644 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
2645 lkm_flags |= LKM_VALBLK;
2647 spin_lock_irqsave(&lockres->l_lock, flags);
2649 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2650 "lockres %s, flags 0x%lx\n",
2651 lockres->l_name, lockres->l_flags);
2653 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2654 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2655 "%u, unlock_action = %u\n",
2656 lockres->l_name, lockres->l_flags, lockres->l_action,
2657 lockres->l_unlock_action);
2659 spin_unlock_irqrestore(&lockres->l_lock, flags);
2661 /* XXX: Today we just wait on any busy
2662 * locks... Perhaps we need to cancel converts in the
2664 ocfs2_wait_on_busy_lock(lockres);
2666 spin_lock_irqsave(&lockres->l_lock, flags);
2669 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2670 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2671 lockres->l_level == LKM_EXMODE &&
2672 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2673 lockres->l_ops->set_lvb(lockres);
2676 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2677 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2679 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2680 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2682 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2683 spin_unlock_irqrestore(&lockres->l_lock, flags);
2687 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2689 /* make sure we never get here while waiting for an ast to
2691 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2693 /* is this necessary? */
2694 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2695 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2696 spin_unlock_irqrestore(&lockres->l_lock, flags);
2698 mlog(0, "lock %s\n", lockres->l_name);
2700 status = ocfs2_dlm_unlock(osb->dlm, &lockres->l_lksb, lkm_flags,
2702 if (status != DLM_NORMAL) {
2703 ocfs2_log_dlm_error("ocfs2_dlm_unlock", status, lockres);
2704 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2705 dlm_print_one_lock(lockres->l_lksb.lockid);
2708 mlog(0, "lock %s, successfull return from ocfs2_dlm_unlock\n",
2711 ocfs2_wait_on_busy_lock(lockres);
2717 /* Mark the lockres as being dropped. It will no longer be
2718 * queued if blocking, but we still may have to wait on it
2719 * being dequeued from the downconvert thread before we can consider
2722 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2723 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2726 struct ocfs2_mask_waiter mw;
2727 unsigned long flags;
2729 ocfs2_init_mask_waiter(&mw);
2731 spin_lock_irqsave(&lockres->l_lock, flags);
2732 lockres->l_flags |= OCFS2_LOCK_FREEING;
2733 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2734 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2735 spin_unlock_irqrestore(&lockres->l_lock, flags);
2737 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2739 status = ocfs2_wait_for_mask(&mw);
2743 spin_lock_irqsave(&lockres->l_lock, flags);
2745 spin_unlock_irqrestore(&lockres->l_lock, flags);
2748 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2749 struct ocfs2_lock_res *lockres)
2753 ocfs2_mark_lockres_freeing(lockres);
2754 ret = ocfs2_drop_lock(osb, lockres);
2759 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2761 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2762 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
2765 int ocfs2_drop_inode_locks(struct inode *inode)
2771 /* No need to call ocfs2_mark_lockres_freeing here -
2772 * ocfs2_clear_inode has done it for us. */
2774 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2775 &OCFS2_I(inode)->ip_open_lockres);
2781 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2782 &OCFS2_I(inode)->ip_inode_lockres);
2785 if (err < 0 && !status)
2788 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2789 &OCFS2_I(inode)->ip_rw_lockres);
2792 if (err < 0 && !status)
2799 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2802 assert_spin_locked(&lockres->l_lock);
2804 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
2806 if (lockres->l_level <= new_level) {
2807 mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
2808 lockres->l_level, new_level);
2812 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2813 lockres->l_name, new_level, lockres->l_blocking);
2815 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2816 lockres->l_requested = new_level;
2817 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2820 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2821 struct ocfs2_lock_res *lockres,
2825 int ret, dlm_flags = LKM_CONVERT;
2826 enum dlm_status status;
2831 dlm_flags |= LKM_VALBLK;
2833 status = ocfs2_dlm_lock(osb->dlm,
2838 OCFS2_LOCK_ID_MAX_LEN - 1,
2840 if (status != DLM_NORMAL) {
2841 ocfs2_log_dlm_error("ocfs2_dlm_lock", status, lockres);
2843 ocfs2_recover_from_dlm_error(lockres, 1);
2853 /* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
2854 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2855 struct ocfs2_lock_res *lockres)
2857 assert_spin_locked(&lockres->l_lock);
2860 mlog(0, "lock %s\n", lockres->l_name);
2862 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2863 /* If we're already trying to cancel a lock conversion
2864 * then just drop the spinlock and allow the caller to
2865 * requeue this lock. */
2867 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2871 /* were we in a convert when we got the bast fire? */
2872 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2873 lockres->l_action != OCFS2_AST_DOWNCONVERT);
2874 /* set things up for the unlockast to know to just
2875 * clear out the ast_action and unset busy, etc. */
2876 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2878 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2879 "lock %s, invalid flags: 0x%lx\n",
2880 lockres->l_name, lockres->l_flags);
2885 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2886 struct ocfs2_lock_res *lockres)
2889 enum dlm_status status;
2892 mlog(0, "lock %s\n", lockres->l_name);
2895 status = ocfs2_dlm_unlock(osb->dlm,
2899 if (status != DLM_NORMAL) {
2900 ocfs2_log_dlm_error("ocfs2_dlm_unlock", status, lockres);
2902 ocfs2_recover_from_dlm_error(lockres, 0);
2905 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres->l_name);
2911 static int ocfs2_unblock_lock(struct ocfs2_super *osb,
2912 struct ocfs2_lock_res *lockres,
2913 struct ocfs2_unblock_ctl *ctl)
2915 unsigned long flags;
2923 spin_lock_irqsave(&lockres->l_lock, flags);
2925 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2928 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2930 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2931 spin_unlock_irqrestore(&lockres->l_lock, flags);
2933 ret = ocfs2_cancel_convert(osb, lockres);
2940 /* if we're blocking an exclusive and we have *any* holders,
2942 if ((lockres->l_blocking == LKM_EXMODE)
2943 && (lockres->l_ex_holders || lockres->l_ro_holders))
2946 /* If it's a PR we're blocking, then only
2947 * requeue if we've got any EX holders */
2948 if (lockres->l_blocking == LKM_PRMODE &&
2949 lockres->l_ex_holders)
2953 * Can we get a lock in this state if the holder counts are
2954 * zero? The meta data unblock code used to check this.
2956 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
2957 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
2960 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2962 if (lockres->l_ops->check_downconvert
2963 && !lockres->l_ops->check_downconvert(lockres, new_level))
2966 /* If we get here, then we know that there are no more
2967 * incompatible holders (and anyone asking for an incompatible
2968 * lock is blocked). We can now downconvert the lock */
2969 if (!lockres->l_ops->downconvert_worker)
2972 /* Some lockres types want to do a bit of work before
2973 * downconverting a lock. Allow that here. The worker function
2974 * may sleep, so we save off a copy of what we're blocking as
2975 * it may change while we're not holding the spin lock. */
2976 blocking = lockres->l_blocking;
2977 spin_unlock_irqrestore(&lockres->l_lock, flags);
2979 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
2981 if (ctl->unblock_action == UNBLOCK_STOP_POST)
2984 spin_lock_irqsave(&lockres->l_lock, flags);
2985 if (blocking != lockres->l_blocking) {
2986 /* If this changed underneath us, then we can't drop
2994 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2995 if (lockres->l_level == LKM_EXMODE)
2999 * We only set the lvb if the lock has been fully
3000 * refreshed - otherwise we risk setting stale
3001 * data. Otherwise, there's no need to actually clear
3002 * out the lvb here as it's value is still valid.
3004 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3005 lockres->l_ops->set_lvb(lockres);
3008 ocfs2_prepare_downconvert(lockres, new_level);
3009 spin_unlock_irqrestore(&lockres->l_lock, flags);
3010 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
3016 spin_unlock_irqrestore(&lockres->l_lock, flags);
3023 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3026 struct inode *inode;
3027 struct address_space *mapping;
3029 inode = ocfs2_lock_res_inode(lockres);
3030 mapping = inode->i_mapping;
3032 if (!S_ISREG(inode->i_mode))
3036 * We need this before the filemap_fdatawrite() so that it can
3037 * transfer the dirty bit from the PTE to the
3038 * page. Unfortunately this means that even for EX->PR
3039 * downconverts, we'll lose our mappings and have to build
3042 unmap_mapping_range(mapping, 0, 0, 0);
3044 if (filemap_fdatawrite(mapping)) {
3045 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3046 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3048 sync_mapping_buffers(mapping);
3049 if (blocking == LKM_EXMODE) {
3050 truncate_inode_pages(mapping, 0);
3052 /* We only need to wait on the I/O if we're not also
3053 * truncating pages because truncate_inode_pages waits
3054 * for us above. We don't truncate pages if we're
3055 * blocking anything < EXMODE because we want to keep
3056 * them around in that case. */
3057 filemap_fdatawait(mapping);
3061 return UNBLOCK_CONTINUE;
3064 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3067 struct inode *inode = ocfs2_lock_res_inode(lockres);
3068 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
3070 BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
3071 BUG_ON(lockres->l_level != LKM_EXMODE && !checkpointed);
3076 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
3080 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3082 struct inode *inode = ocfs2_lock_res_inode(lockres);
3084 __ocfs2_stuff_meta_lvb(inode);
3088 * Does the final reference drop on our dentry lock. Right now this
3089 * happens in the downconvert thread, but we could choose to simplify the
3090 * dlmglue API and push these off to the ocfs2_wq in the future.
3092 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3093 struct ocfs2_lock_res *lockres)
3095 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3096 ocfs2_dentry_lock_put(osb, dl);
3100 * d_delete() matching dentries before the lock downconvert.
3102 * At this point, any process waiting to destroy the
3103 * dentry_lock due to last ref count is stopped by the
3104 * OCFS2_LOCK_QUEUED flag.
3106 * We have two potential problems
3108 * 1) If we do the last reference drop on our dentry_lock (via dput)
3109 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3110 * the downconvert to finish. Instead we take an elevated
3111 * reference and push the drop until after we've completed our
3112 * unblock processing.
3114 * 2) There might be another process with a final reference,
3115 * waiting on us to finish processing. If this is the case, we
3116 * detect it and exit out - there's no more dentries anyway.
3118 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3121 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3122 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3123 struct dentry *dentry;
3124 unsigned long flags;
3128 * This node is blocking another node from getting a read
3129 * lock. This happens when we've renamed within a
3130 * directory. We've forced the other nodes to d_delete(), but
3131 * we never actually dropped our lock because it's still
3132 * valid. The downconvert code will retain a PR for this node,
3133 * so there's no further work to do.
3135 if (blocking == LKM_PRMODE)
3136 return UNBLOCK_CONTINUE;
3139 * Mark this inode as potentially orphaned. The code in
3140 * ocfs2_delete_inode() will figure out whether it actually
3141 * needs to be freed or not.
3143 spin_lock(&oi->ip_lock);
3144 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3145 spin_unlock(&oi->ip_lock);
3148 * Yuck. We need to make sure however that the check of
3149 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3150 * respect to a reference decrement or the setting of that
3153 spin_lock_irqsave(&lockres->l_lock, flags);
3154 spin_lock(&dentry_attach_lock);
3155 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3160 spin_unlock(&dentry_attach_lock);
3161 spin_unlock_irqrestore(&lockres->l_lock, flags);
3163 mlog(0, "extra_ref = %d\n", extra_ref);
3166 * We have a process waiting on us in ocfs2_dentry_iput(),
3167 * which means we can't have any more outstanding
3168 * aliases. There's no need to do any more work.
3171 return UNBLOCK_CONTINUE;
3173 spin_lock(&dentry_attach_lock);
3175 dentry = ocfs2_find_local_alias(dl->dl_inode,
3176 dl->dl_parent_blkno, 1);
3179 spin_unlock(&dentry_attach_lock);
3181 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3182 dentry->d_name.name);
3185 * The following dcache calls may do an
3186 * iput(). Normally we don't want that from the
3187 * downconverting thread, but in this case it's ok
3188 * because the requesting node already has an
3189 * exclusive lock on the inode, so it can't be queued
3190 * for a downconvert.
3195 spin_lock(&dentry_attach_lock);
3197 spin_unlock(&dentry_attach_lock);
3200 * If we are the last holder of this dentry lock, there is no
3201 * reason to downconvert so skip straight to the unlock.
3203 if (dl->dl_count == 1)
3204 return UNBLOCK_STOP_POST;
3206 return UNBLOCK_CONTINUE_POST;
3209 static struct ocfs2_locking_protocol lproto = {
3210 .lp_lock_ast = ocfs2_locking_ast,
3211 .lp_blocking_ast = ocfs2_blocking_ast,
3212 .lp_unlock_ast = ocfs2_unlock_ast,
3215 /* This interface isn't the final one, hence the less-than-perfect names */
3216 void dlmglue_init_stack(void)
3218 o2cb_get_stack(&lproto);
3221 void dlmglue_exit_stack(void)
3226 static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3227 struct ocfs2_lock_res *lockres)
3230 struct ocfs2_unblock_ctl ctl = {0, 0,};
3231 unsigned long flags;
3233 /* Our reference to the lockres in this function can be
3234 * considered valid until we remove the OCFS2_LOCK_QUEUED
3240 BUG_ON(!lockres->l_ops);
3242 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3244 /* Detect whether a lock has been marked as going away while
3245 * the downconvert thread was processing other things. A lock can
3246 * still be marked with OCFS2_LOCK_FREEING after this check,
3247 * but short circuiting here will still save us some
3249 spin_lock_irqsave(&lockres->l_lock, flags);
3250 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3252 spin_unlock_irqrestore(&lockres->l_lock, flags);
3254 status = ocfs2_unblock_lock(osb, lockres, &ctl);
3258 spin_lock_irqsave(&lockres->l_lock, flags);
3260 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
3261 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3263 ocfs2_schedule_blocked_lock(osb, lockres);
3265 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
3266 ctl.requeue ? "yes" : "no");
3267 spin_unlock_irqrestore(&lockres->l_lock, flags);
3269 if (ctl.unblock_action != UNBLOCK_CONTINUE
3270 && lockres->l_ops->post_unlock)
3271 lockres->l_ops->post_unlock(osb, lockres);
3276 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3277 struct ocfs2_lock_res *lockres)
3281 assert_spin_locked(&lockres->l_lock);
3283 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3284 /* Do not schedule a lock for downconvert when it's on
3285 * the way to destruction - any nodes wanting access
3286 * to the resource will get it soon. */
3287 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3288 lockres->l_name, lockres->l_flags);
3292 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3294 spin_lock(&osb->dc_task_lock);
3295 if (list_empty(&lockres->l_blocked_list)) {
3296 list_add_tail(&lockres->l_blocked_list,
3297 &osb->blocked_lock_list);
3298 osb->blocked_lock_count++;
3300 spin_unlock(&osb->dc_task_lock);
3305 static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3307 unsigned long processed;
3308 struct ocfs2_lock_res *lockres;
3312 spin_lock(&osb->dc_task_lock);
3313 /* grab this early so we know to try again if a state change and
3314 * wake happens part-way through our work */
3315 osb->dc_work_sequence = osb->dc_wake_sequence;
3317 processed = osb->blocked_lock_count;
3319 BUG_ON(list_empty(&osb->blocked_lock_list));
3321 lockres = list_entry(osb->blocked_lock_list.next,
3322 struct ocfs2_lock_res, l_blocked_list);
3323 list_del_init(&lockres->l_blocked_list);
3324 osb->blocked_lock_count--;
3325 spin_unlock(&osb->dc_task_lock);
3330 ocfs2_process_blocked_lock(osb, lockres);
3332 spin_lock(&osb->dc_task_lock);
3334 spin_unlock(&osb->dc_task_lock);
3339 static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3343 spin_lock(&osb->dc_task_lock);
3344 if (list_empty(&osb->blocked_lock_list))
3347 spin_unlock(&osb->dc_task_lock);
3351 static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
3353 int should_wake = 0;
3355 spin_lock(&osb->dc_task_lock);
3356 if (osb->dc_work_sequence != osb->dc_wake_sequence)
3358 spin_unlock(&osb->dc_task_lock);
3363 static int ocfs2_downconvert_thread(void *arg)
3366 struct ocfs2_super *osb = arg;
3368 /* only quit once we've been asked to stop and there is no more
3370 while (!(kthread_should_stop() &&
3371 ocfs2_downconvert_thread_lists_empty(osb))) {
3373 wait_event_interruptible(osb->dc_event,
3374 ocfs2_downconvert_thread_should_wake(osb) ||
3375 kthread_should_stop());
3377 mlog(0, "downconvert_thread: awoken\n");
3379 ocfs2_downconvert_thread_do_work(osb);
3382 osb->dc_task = NULL;
3386 void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
3388 spin_lock(&osb->dc_task_lock);
3389 /* make sure the voting thread gets a swipe at whatever changes
3390 * the caller may have made to the voting state */
3391 osb->dc_wake_sequence++;
3392 spin_unlock(&osb->dc_task_lock);
3393 wake_up(&osb->dc_event);