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/kthread.h>
31 #include <linux/pagemap.h>
32 #include <linux/debugfs.h>
33 #include <linux/seq_file.h>
35 #define MLOG_MASK_PREFIX ML_DLM_GLUE
36 #include <cluster/masklog.h>
39 #include "ocfs2_lockingver.h"
44 #include "extent_map.h"
46 #include "heartbeat.h"
49 #include "stackglue.h"
54 #include "buffer_head_io.h"
56 struct ocfs2_mask_waiter {
57 struct list_head mw_item;
59 struct completion mw_complete;
60 unsigned long mw_mask;
61 unsigned long mw_goal;
64 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
65 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
66 static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres);
69 * Return value from ->downconvert_worker functions.
71 * These control the precise actions of ocfs2_unblock_lock()
72 * and ocfs2_process_blocked_lock()
75 enum ocfs2_unblock_action {
76 UNBLOCK_CONTINUE = 0, /* Continue downconvert */
77 UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire
78 * ->post_unlock callback */
79 UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire
80 * ->post_unlock() callback. */
83 struct ocfs2_unblock_ctl {
85 enum ocfs2_unblock_action unblock_action;
88 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
90 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
92 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
95 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
98 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
99 struct ocfs2_lock_res *lockres);
102 #define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres)
104 /* This aids in debugging situations where a bad LVB might be involved. */
105 static void ocfs2_dump_meta_lvb_info(u64 level,
106 const char *function,
108 struct ocfs2_lock_res *lockres)
110 struct ocfs2_meta_lvb *lvb =
111 (struct ocfs2_meta_lvb *)ocfs2_dlm_lvb(&lockres->l_lksb);
113 mlog(level, "LVB information for %s (called from %s:%u):\n",
114 lockres->l_name, function, line);
115 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
116 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
117 be32_to_cpu(lvb->lvb_igeneration));
118 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
119 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
120 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
121 be16_to_cpu(lvb->lvb_imode));
122 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
123 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
124 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
125 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
126 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
127 be32_to_cpu(lvb->lvb_iattr));
132 * OCFS2 Lock Resource Operations
134 * These fine tune the behavior of the generic dlmglue locking infrastructure.
136 * The most basic of lock types can point ->l_priv to their respective
137 * struct ocfs2_super and allow the default actions to manage things.
139 * Right now, each lock type also needs to implement an init function,
140 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
141 * should be called when the lock is no longer needed (i.e., object
144 struct ocfs2_lock_res_ops {
146 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
147 * this callback if ->l_priv is not an ocfs2_super pointer
149 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
152 * Optionally called in the downconvert thread after a
153 * successful downconvert. The lockres will not be referenced
154 * after this callback is called, so it is safe to free
157 * The exact semantics of when this is called are controlled
158 * by ->downconvert_worker()
160 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
163 * Allow a lock type to add checks to determine whether it is
164 * safe to downconvert a lock. Return 0 to re-queue the
165 * downconvert at a later time, nonzero to continue.
167 * For most locks, the default checks that there are no
168 * incompatible holders are sufficient.
170 * Called with the lockres spinlock held.
172 int (*check_downconvert)(struct ocfs2_lock_res *, int);
175 * Allows a lock type to populate the lock value block. This
176 * is called on downconvert, and when we drop a lock.
178 * Locks that want to use this should set LOCK_TYPE_USES_LVB
179 * in the flags field.
181 * Called with the lockres spinlock held.
183 void (*set_lvb)(struct ocfs2_lock_res *);
186 * Called from the downconvert thread when it is determined
187 * that a lock will be downconverted. This is called without
188 * any locks held so the function can do work that might
189 * schedule (syncing out data, etc).
191 * This should return any one of the ocfs2_unblock_action
192 * values, depending on what it wants the thread to do.
194 int (*downconvert_worker)(struct ocfs2_lock_res *, int);
197 * LOCK_TYPE_* flags which describe the specific requirements
198 * of a lock type. Descriptions of each individual flag follow.
204 * Some locks want to "refresh" potentially stale data when a
205 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
206 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
207 * individual lockres l_flags member from the ast function. It is
208 * expected that the locking wrapper will clear the
209 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
211 #define LOCK_TYPE_REQUIRES_REFRESH 0x1
214 * Indicate that a lock type makes use of the lock value block. The
215 * ->set_lvb lock type callback must be defined.
217 #define LOCK_TYPE_USES_LVB 0x2
219 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
220 .get_osb = ocfs2_get_inode_osb,
224 static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops = {
225 .get_osb = ocfs2_get_inode_osb,
226 .check_downconvert = ocfs2_check_meta_downconvert,
227 .set_lvb = ocfs2_set_meta_lvb,
228 .downconvert_worker = ocfs2_data_convert_worker,
229 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
232 static struct ocfs2_lock_res_ops ocfs2_super_lops = {
233 .flags = LOCK_TYPE_REQUIRES_REFRESH,
236 static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
240 static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
241 .get_osb = ocfs2_get_dentry_osb,
242 .post_unlock = ocfs2_dentry_post_unlock,
243 .downconvert_worker = ocfs2_dentry_convert_worker,
247 static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
248 .get_osb = ocfs2_get_inode_osb,
252 static struct ocfs2_lock_res_ops ocfs2_flock_lops = {
253 .get_osb = ocfs2_get_file_osb,
257 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
259 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
260 lockres->l_type == OCFS2_LOCK_TYPE_RW ||
261 lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
264 static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
266 BUG_ON(!ocfs2_is_inode_lock(lockres));
268 return (struct inode *) lockres->l_priv;
271 static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
273 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
275 return (struct ocfs2_dentry_lock *)lockres->l_priv;
278 static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
280 if (lockres->l_ops->get_osb)
281 return lockres->l_ops->get_osb(lockres);
283 return (struct ocfs2_super *)lockres->l_priv;
286 static int ocfs2_lock_create(struct ocfs2_super *osb,
287 struct ocfs2_lock_res *lockres,
290 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
292 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
293 struct ocfs2_lock_res *lockres,
295 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
296 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
297 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
298 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
299 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
300 struct ocfs2_lock_res *lockres);
301 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
303 #define ocfs2_log_dlm_error(_func, _err, _lockres) do { \
304 mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \
305 _err, _func, _lockres->l_name); \
307 static int ocfs2_downconvert_thread(void *arg);
308 static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
309 struct ocfs2_lock_res *lockres);
310 static int ocfs2_inode_lock_update(struct inode *inode,
311 struct buffer_head **bh);
312 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
313 static inline int ocfs2_highest_compat_lock_level(int level);
314 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
316 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
317 struct ocfs2_lock_res *lockres,
320 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
321 struct ocfs2_lock_res *lockres);
322 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
323 struct ocfs2_lock_res *lockres);
326 static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
335 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
337 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
338 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
339 (long long)blkno, generation);
341 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
343 mlog(0, "built lock resource with name: %s\n", name);
348 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
350 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
351 struct ocfs2_dlm_debug *dlm_debug)
353 mlog(0, "Add tracking for lockres %s\n", res->l_name);
355 spin_lock(&ocfs2_dlm_tracking_lock);
356 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
357 spin_unlock(&ocfs2_dlm_tracking_lock);
360 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
362 spin_lock(&ocfs2_dlm_tracking_lock);
363 if (!list_empty(&res->l_debug_list))
364 list_del_init(&res->l_debug_list);
365 spin_unlock(&ocfs2_dlm_tracking_lock);
368 static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
369 struct ocfs2_lock_res *res,
370 enum ocfs2_lock_type type,
371 struct ocfs2_lock_res_ops *ops,
378 res->l_level = DLM_LOCK_IV;
379 res->l_requested = DLM_LOCK_IV;
380 res->l_blocking = DLM_LOCK_IV;
381 res->l_action = OCFS2_AST_INVALID;
382 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
384 res->l_flags = OCFS2_LOCK_INITIALIZED;
386 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
389 void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
391 /* This also clears out the lock status block */
392 memset(res, 0, sizeof(struct ocfs2_lock_res));
393 spin_lock_init(&res->l_lock);
394 init_waitqueue_head(&res->l_event);
395 INIT_LIST_HEAD(&res->l_blocked_list);
396 INIT_LIST_HEAD(&res->l_mask_waiters);
399 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
400 enum ocfs2_lock_type type,
401 unsigned int generation,
404 struct ocfs2_lock_res_ops *ops;
407 case OCFS2_LOCK_TYPE_RW:
408 ops = &ocfs2_inode_rw_lops;
410 case OCFS2_LOCK_TYPE_META:
411 ops = &ocfs2_inode_inode_lops;
413 case OCFS2_LOCK_TYPE_OPEN:
414 ops = &ocfs2_inode_open_lops;
417 mlog_bug_on_msg(1, "type: %d\n", type);
418 ops = NULL; /* thanks, gcc */
422 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
423 generation, res->l_name);
424 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
427 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
429 struct inode *inode = ocfs2_lock_res_inode(lockres);
431 return OCFS2_SB(inode->i_sb);
434 static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
436 struct ocfs2_file_private *fp = lockres->l_priv;
438 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
441 static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
443 __be64 inode_blkno_be;
445 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
448 return be64_to_cpu(inode_blkno_be);
451 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
453 struct ocfs2_dentry_lock *dl = lockres->l_priv;
455 return OCFS2_SB(dl->dl_inode->i_sb);
458 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
459 u64 parent, struct inode *inode)
462 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
463 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
464 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
466 ocfs2_lock_res_init_once(lockres);
469 * Unfortunately, the standard lock naming scheme won't work
470 * here because we have two 16 byte values to use. Instead,
471 * we'll stuff the inode number as a binary value. We still
472 * want error prints to show something without garbling the
473 * display, so drop a null byte in there before the inode
474 * number. A future version of OCFS2 will likely use all
475 * binary lock names. The stringified names have been a
476 * tremendous aid in debugging, but now that the debugfs
477 * interface exists, we can mangle things there if need be.
479 * NOTE: We also drop the standard "pad" value (the total lock
480 * name size stays the same though - the last part is all
481 * zeros due to the memset in ocfs2_lock_res_init_once()
483 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
485 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
488 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
490 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
493 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
494 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
498 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
499 struct ocfs2_super *osb)
501 /* Superblock lockres doesn't come from a slab so we call init
502 * once on it manually. */
503 ocfs2_lock_res_init_once(res);
504 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
506 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
507 &ocfs2_super_lops, osb);
510 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
511 struct ocfs2_super *osb)
513 /* Rename lockres doesn't come from a slab so we call init
514 * once on it manually. */
515 ocfs2_lock_res_init_once(res);
516 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
517 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
518 &ocfs2_rename_lops, osb);
521 void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
522 struct ocfs2_file_private *fp)
524 struct inode *inode = fp->fp_file->f_mapping->host;
525 struct ocfs2_inode_info *oi = OCFS2_I(inode);
527 ocfs2_lock_res_init_once(lockres);
528 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
529 inode->i_generation, lockres->l_name);
530 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
531 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
533 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
536 void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
540 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
543 ocfs2_remove_lockres_tracking(res);
545 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
546 "Lockres %s is on the blocked list\n",
548 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
549 "Lockres %s has mask waiters pending\n",
551 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
552 "Lockres %s is locked\n",
554 mlog_bug_on_msg(res->l_ro_holders,
555 "Lockres %s has %u ro holders\n",
556 res->l_name, res->l_ro_holders);
557 mlog_bug_on_msg(res->l_ex_holders,
558 "Lockres %s has %u ex holders\n",
559 res->l_name, res->l_ex_holders);
561 /* Need to clear out the lock status block for the dlm */
562 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
568 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
577 lockres->l_ex_holders++;
580 lockres->l_ro_holders++;
589 static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
598 BUG_ON(!lockres->l_ex_holders);
599 lockres->l_ex_holders--;
602 BUG_ON(!lockres->l_ro_holders);
603 lockres->l_ro_holders--;
611 /* WARNING: This function lives in a world where the only three lock
612 * levels are EX, PR, and NL. It *will* have to be adjusted when more
613 * lock types are added. */
614 static inline int ocfs2_highest_compat_lock_level(int level)
616 int new_level = DLM_LOCK_EX;
618 if (level == DLM_LOCK_EX)
619 new_level = DLM_LOCK_NL;
620 else if (level == DLM_LOCK_PR)
621 new_level = DLM_LOCK_PR;
625 static void lockres_set_flags(struct ocfs2_lock_res *lockres,
626 unsigned long newflags)
628 struct ocfs2_mask_waiter *mw, *tmp;
630 assert_spin_locked(&lockres->l_lock);
632 lockres->l_flags = newflags;
634 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
635 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
638 list_del_init(&mw->mw_item);
640 complete(&mw->mw_complete);
643 static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
645 lockres_set_flags(lockres, lockres->l_flags | or);
647 static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
650 lockres_set_flags(lockres, lockres->l_flags & ~clear);
653 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
657 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
658 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
659 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
660 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
662 lockres->l_level = lockres->l_requested;
663 if (lockres->l_level <=
664 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
665 lockres->l_blocking = DLM_LOCK_NL;
666 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
668 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
673 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
677 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
678 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
680 /* Convert from RO to EX doesn't really need anything as our
681 * information is already up to data. Convert from NL to
682 * *anything* however should mark ourselves as needing an
684 if (lockres->l_level == DLM_LOCK_NL &&
685 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
686 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
688 lockres->l_level = lockres->l_requested;
689 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
694 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
698 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
699 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
701 if (lockres->l_requested > DLM_LOCK_NL &&
702 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
703 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
704 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
706 lockres->l_level = lockres->l_requested;
707 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
708 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
713 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
716 int needs_downconvert = 0;
719 assert_spin_locked(&lockres->l_lock);
721 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
723 if (level > lockres->l_blocking) {
724 /* only schedule a downconvert if we haven't already scheduled
725 * one that goes low enough to satisfy the level we're
726 * blocking. this also catches the case where we get
728 if (ocfs2_highest_compat_lock_level(level) <
729 ocfs2_highest_compat_lock_level(lockres->l_blocking))
730 needs_downconvert = 1;
732 lockres->l_blocking = level;
735 mlog_exit(needs_downconvert);
736 return needs_downconvert;
739 static void ocfs2_blocking_ast(void *opaque, int level)
741 struct ocfs2_lock_res *lockres = opaque;
742 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
743 int needs_downconvert;
746 BUG_ON(level <= DLM_LOCK_NL);
748 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
749 lockres->l_name, level, lockres->l_level,
750 ocfs2_lock_type_string(lockres->l_type));
753 * We can skip the bast for locks which don't enable caching -
754 * they'll be dropped at the earliest possible time anyway.
756 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
759 spin_lock_irqsave(&lockres->l_lock, flags);
760 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
761 if (needs_downconvert)
762 ocfs2_schedule_blocked_lock(osb, lockres);
763 spin_unlock_irqrestore(&lockres->l_lock, flags);
765 wake_up(&lockres->l_event);
767 ocfs2_wake_downconvert_thread(osb);
770 static void ocfs2_locking_ast(void *opaque)
772 struct ocfs2_lock_res *lockres = opaque;
775 spin_lock_irqsave(&lockres->l_lock, flags);
777 if (ocfs2_dlm_lock_status(&lockres->l_lksb)) {
778 mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
780 ocfs2_dlm_lock_status(&lockres->l_lksb));
781 spin_unlock_irqrestore(&lockres->l_lock, flags);
785 switch(lockres->l_action) {
786 case OCFS2_AST_ATTACH:
787 ocfs2_generic_handle_attach_action(lockres);
788 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
790 case OCFS2_AST_CONVERT:
791 ocfs2_generic_handle_convert_action(lockres);
793 case OCFS2_AST_DOWNCONVERT:
794 ocfs2_generic_handle_downconvert_action(lockres);
797 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
798 "lockres flags = 0x%lx, unlock action: %u\n",
799 lockres->l_name, lockres->l_action, lockres->l_flags,
800 lockres->l_unlock_action);
804 /* set it to something invalid so if we get called again we
806 lockres->l_action = OCFS2_AST_INVALID;
808 wake_up(&lockres->l_event);
809 spin_unlock_irqrestore(&lockres->l_lock, flags);
812 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
818 spin_lock_irqsave(&lockres->l_lock, flags);
819 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
821 lockres->l_action = OCFS2_AST_INVALID;
823 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
824 spin_unlock_irqrestore(&lockres->l_lock, flags);
826 wake_up(&lockres->l_event);
830 /* Note: If we detect another process working on the lock (i.e.,
831 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
832 * to do the right thing in that case.
834 static int ocfs2_lock_create(struct ocfs2_super *osb,
835 struct ocfs2_lock_res *lockres,
844 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
847 spin_lock_irqsave(&lockres->l_lock, flags);
848 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
849 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
850 spin_unlock_irqrestore(&lockres->l_lock, flags);
854 lockres->l_action = OCFS2_AST_ATTACH;
855 lockres->l_requested = level;
856 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
857 spin_unlock_irqrestore(&lockres->l_lock, flags);
859 ret = ocfs2_dlm_lock(osb->cconn,
864 OCFS2_LOCK_ID_MAX_LEN - 1,
867 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
868 ocfs2_recover_from_dlm_error(lockres, 1);
871 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
878 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
884 spin_lock_irqsave(&lockres->l_lock, flags);
885 ret = lockres->l_flags & flag;
886 spin_unlock_irqrestore(&lockres->l_lock, flags);
891 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
894 wait_event(lockres->l_event,
895 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
898 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
901 wait_event(lockres->l_event,
902 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
905 /* predict what lock level we'll be dropping down to on behalf
906 * of another node, and return true if the currently wanted
907 * level will be compatible with it. */
908 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
911 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
913 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
916 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
918 INIT_LIST_HEAD(&mw->mw_item);
919 init_completion(&mw->mw_complete);
922 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
924 wait_for_completion(&mw->mw_complete);
925 /* Re-arm the completion in case we want to wait on it again */
926 INIT_COMPLETION(mw->mw_complete);
927 return mw->mw_status;
930 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
931 struct ocfs2_mask_waiter *mw,
935 BUG_ON(!list_empty(&mw->mw_item));
937 assert_spin_locked(&lockres->l_lock);
939 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
944 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
945 * if the mask still hadn't reached its goal */
946 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
947 struct ocfs2_mask_waiter *mw)
952 spin_lock_irqsave(&lockres->l_lock, flags);
953 if (!list_empty(&mw->mw_item)) {
954 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
957 list_del_init(&mw->mw_item);
958 init_completion(&mw->mw_complete);
960 spin_unlock_irqrestore(&lockres->l_lock, flags);
966 static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
967 struct ocfs2_lock_res *lockres)
971 ret = wait_for_completion_interruptible(&mw->mw_complete);
973 lockres_remove_mask_waiter(lockres, mw);
976 /* Re-arm the completion in case we want to wait on it again */
977 INIT_COMPLETION(mw->mw_complete);
981 static int ocfs2_cluster_lock(struct ocfs2_super *osb,
982 struct ocfs2_lock_res *lockres,
987 struct ocfs2_mask_waiter mw;
988 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
989 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
994 ocfs2_init_mask_waiter(&mw);
996 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
997 lkm_flags |= DLM_LKF_VALBLK;
1002 if (catch_signals && signal_pending(current)) {
1007 spin_lock_irqsave(&lockres->l_lock, flags);
1009 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1010 "Cluster lock called on freeing lockres %s! flags "
1011 "0x%lx\n", lockres->l_name, lockres->l_flags);
1013 /* We only compare against the currently granted level
1014 * here. If the lock is blocked waiting on a downconvert,
1015 * we'll get caught below. */
1016 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1017 level > lockres->l_level) {
1018 /* is someone sitting in dlm_lock? If so, wait on
1020 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1025 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1026 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1027 /* is the lock is currently blocked on behalf of
1029 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1034 if (level > lockres->l_level) {
1035 if (lockres->l_action != OCFS2_AST_INVALID)
1036 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1037 lockres->l_name, lockres->l_action);
1039 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1040 lockres->l_action = OCFS2_AST_ATTACH;
1041 lkm_flags &= ~DLM_LKF_CONVERT;
1043 lockres->l_action = OCFS2_AST_CONVERT;
1044 lkm_flags |= DLM_LKF_CONVERT;
1047 lockres->l_requested = level;
1048 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1049 spin_unlock_irqrestore(&lockres->l_lock, flags);
1051 BUG_ON(level == DLM_LOCK_IV);
1052 BUG_ON(level == DLM_LOCK_NL);
1054 mlog(0, "lock %s, convert from %d to level = %d\n",
1055 lockres->l_name, lockres->l_level, level);
1057 /* call dlm_lock to upgrade lock now */
1058 ret = ocfs2_dlm_lock(osb->cconn,
1063 OCFS2_LOCK_ID_MAX_LEN - 1,
1066 if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
1068 ocfs2_log_dlm_error("ocfs2_dlm_lock",
1071 ocfs2_recover_from_dlm_error(lockres, 1);
1075 mlog(0, "lock %s, successfull return from ocfs2_dlm_lock\n",
1078 /* At this point we've gone inside the dlm and need to
1079 * complete our work regardless. */
1082 /* wait for busy to clear and carry on */
1086 /* Ok, if we get here then we're good to go. */
1087 ocfs2_inc_holders(lockres, level);
1091 spin_unlock_irqrestore(&lockres->l_lock, flags);
1094 * This is helping work around a lock inversion between the page lock
1095 * and dlm locks. One path holds the page lock while calling aops
1096 * which block acquiring dlm locks. The voting thread holds dlm
1097 * locks while acquiring page locks while down converting data locks.
1098 * This block is helping an aop path notice the inversion and back
1099 * off to unlock its page lock before trying the dlm lock again.
1101 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1102 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1104 if (lockres_remove_mask_waiter(lockres, &mw))
1110 ret = ocfs2_wait_for_mask(&mw);
1120 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1121 struct ocfs2_lock_res *lockres,
1124 unsigned long flags;
1127 spin_lock_irqsave(&lockres->l_lock, flags);
1128 ocfs2_dec_holders(lockres, level);
1129 ocfs2_downconvert_on_unlock(osb, lockres);
1130 spin_unlock_irqrestore(&lockres->l_lock, flags);
1134 static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1135 struct ocfs2_lock_res *lockres,
1139 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1140 unsigned long flags;
1141 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
1143 spin_lock_irqsave(&lockres->l_lock, flags);
1144 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1145 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1146 spin_unlock_irqrestore(&lockres->l_lock, flags);
1148 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
1151 /* Grants us an EX lock on the data and metadata resources, skipping
1152 * the normal cluster directory lookup. Use this ONLY on newly created
1153 * inodes which other nodes can't possibly see, and which haven't been
1154 * hashed in the inode hash yet. This can give us a good performance
1155 * increase as it'll skip the network broadcast normally associated
1156 * with creating a new lock resource. */
1157 int ocfs2_create_new_inode_locks(struct inode *inode)
1160 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1163 BUG_ON(!ocfs2_inode_is_new(inode));
1167 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1169 /* NOTE: That we don't increment any of the holder counts, nor
1170 * do we add anything to a journal handle. Since this is
1171 * supposed to be a new inode which the cluster doesn't know
1172 * about yet, there is no need to. As far as the LVB handling
1173 * is concerned, this is basically like acquiring an EX lock
1174 * on a resource which has an invalid one -- we'll set it
1175 * valid when we release the EX. */
1177 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
1184 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
1185 * don't use a generation in their lock names.
1187 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
1193 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1204 int ocfs2_rw_lock(struct inode *inode, int write)
1207 struct ocfs2_lock_res *lockres;
1208 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1214 mlog(0, "inode %llu take %s RW lock\n",
1215 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1216 write ? "EXMODE" : "PRMODE");
1218 if (ocfs2_mount_local(osb))
1221 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1223 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1225 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1234 void ocfs2_rw_unlock(struct inode *inode, int write)
1236 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1237 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1238 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1242 mlog(0, "inode %llu drop %s RW lock\n",
1243 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1244 write ? "EXMODE" : "PRMODE");
1246 if (!ocfs2_mount_local(osb))
1247 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1253 * ocfs2_open_lock always get PR mode lock.
1255 int ocfs2_open_lock(struct inode *inode)
1258 struct ocfs2_lock_res *lockres;
1259 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1265 mlog(0, "inode %llu take PRMODE open lock\n",
1266 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1268 if (ocfs2_mount_local(osb))
1271 lockres = &OCFS2_I(inode)->ip_open_lockres;
1273 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1283 int ocfs2_try_open_lock(struct inode *inode, int write)
1285 int status = 0, level;
1286 struct ocfs2_lock_res *lockres;
1287 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1293 mlog(0, "inode %llu try to take %s open lock\n",
1294 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1295 write ? "EXMODE" : "PRMODE");
1297 if (ocfs2_mount_local(osb))
1300 lockres = &OCFS2_I(inode)->ip_open_lockres;
1302 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1305 * The file system may already holding a PRMODE/EXMODE open lock.
1306 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
1307 * other nodes and the -EAGAIN will indicate to the caller that
1308 * this inode is still in use.
1310 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1311 level, DLM_LKF_NOQUEUE, 0);
1319 * ocfs2_open_unlock unlock PR and EX mode open locks.
1321 void ocfs2_open_unlock(struct inode *inode)
1323 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1324 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1328 mlog(0, "inode %llu drop open lock\n",
1329 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1331 if (ocfs2_mount_local(osb))
1334 if(lockres->l_ro_holders)
1335 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1337 if(lockres->l_ex_holders)
1338 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1345 static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1349 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1350 unsigned long flags;
1351 struct ocfs2_mask_waiter mw;
1353 ocfs2_init_mask_waiter(&mw);
1356 spin_lock_irqsave(&lockres->l_lock, flags);
1357 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1358 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1360 spin_unlock_irqrestore(&lockres->l_lock, flags);
1361 ret = ocfs2_cancel_convert(osb, lockres);
1368 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1369 spin_unlock_irqrestore(&lockres->l_lock, flags);
1371 ocfs2_wait_for_mask(&mw);
1377 * We may still have gotten the lock, in which case there's no
1378 * point to restarting the syscall.
1380 if (lockres->l_level == level)
1383 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1384 lockres->l_flags, lockres->l_level, lockres->l_action);
1386 spin_unlock_irqrestore(&lockres->l_lock, flags);
1393 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1394 * flock() calls. The locking approach this requires is sufficiently
1395 * different from all other cluster lock types that we implement a
1396 * seperate path to the "low-level" dlm calls. In particular:
1398 * - No optimization of lock levels is done - we take at exactly
1399 * what's been requested.
1401 * - No lock caching is employed. We immediately downconvert to
1402 * no-lock at unlock time. This also means flock locks never go on
1403 * the blocking list).
1405 * - Since userspace can trivially deadlock itself with flock, we make
1406 * sure to allow cancellation of a misbehaving applications flock()
1409 * - Access to any flock lockres doesn't require concurrency, so we
1410 * can simplify the code by requiring the caller to guarantee
1411 * serialization of dlmglue flock calls.
1413 int ocfs2_file_lock(struct file *file, int ex, int trylock)
1415 int ret, level = ex ? LKM_EXMODE : LKM_PRMODE;
1416 unsigned int lkm_flags = trylock ? LKM_NOQUEUE : 0;
1417 unsigned long flags;
1418 struct ocfs2_file_private *fp = file->private_data;
1419 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1420 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1421 struct ocfs2_mask_waiter mw;
1423 ocfs2_init_mask_waiter(&mw);
1425 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
1426 (lockres->l_level > DLM_LOCK_NL)) {
1428 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1429 "level: %u\n", lockres->l_name, lockres->l_flags,
1434 spin_lock_irqsave(&lockres->l_lock, flags);
1435 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1436 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1437 spin_unlock_irqrestore(&lockres->l_lock, flags);
1440 * Get the lock at NLMODE to start - that way we
1441 * can cancel the upconvert request if need be.
1443 ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
1449 ret = ocfs2_wait_for_mask(&mw);
1454 spin_lock_irqsave(&lockres->l_lock, flags);
1457 lockres->l_action = OCFS2_AST_CONVERT;
1458 lkm_flags |= LKM_CONVERT;
1459 lockres->l_requested = level;
1460 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1462 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1463 spin_unlock_irqrestore(&lockres->l_lock, flags);
1465 ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
1466 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1,
1469 if (!trylock || (ret != -EAGAIN)) {
1470 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
1474 ocfs2_recover_from_dlm_error(lockres, 1);
1475 lockres_remove_mask_waiter(lockres, &mw);
1479 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1480 if (ret == -ERESTARTSYS) {
1482 * Userspace can cause deadlock itself with
1483 * flock(). Current behavior locally is to allow the
1484 * deadlock, but abort the system call if a signal is
1485 * received. We follow this example, otherwise a
1486 * poorly written program could sit in kernel until
1489 * Handling this is a bit more complicated for Ocfs2
1490 * though. We can't exit this function with an
1491 * outstanding lock request, so a cancel convert is
1492 * required. We intentionally overwrite 'ret' - if the
1493 * cancel fails and the lock was granted, it's easier
1494 * to just bubble sucess back up to the user.
1496 ret = ocfs2_flock_handle_signal(lockres, level);
1501 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1502 lockres->l_name, ex, trylock, ret);
1506 void ocfs2_file_unlock(struct file *file)
1509 unsigned long flags;
1510 struct ocfs2_file_private *fp = file->private_data;
1511 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1512 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1513 struct ocfs2_mask_waiter mw;
1515 ocfs2_init_mask_waiter(&mw);
1517 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
1520 if (lockres->l_level == LKM_NLMODE)
1523 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1524 lockres->l_name, lockres->l_flags, lockres->l_level,
1527 spin_lock_irqsave(&lockres->l_lock, flags);
1529 * Fake a blocking ast for the downconvert code.
1531 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
1532 lockres->l_blocking = DLM_LOCK_EX;
1534 ocfs2_prepare_downconvert(lockres, LKM_NLMODE);
1535 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1536 spin_unlock_irqrestore(&lockres->l_lock, flags);
1538 ret = ocfs2_downconvert_lock(osb, lockres, LKM_NLMODE, 0);
1544 ret = ocfs2_wait_for_mask(&mw);
1549 static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1550 struct ocfs2_lock_res *lockres)
1556 /* If we know that another node is waiting on our lock, kick
1557 * the downconvert thread * pre-emptively when we reach a release
1559 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1560 switch(lockres->l_blocking) {
1562 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1566 if (!lockres->l_ex_holders)
1575 ocfs2_wake_downconvert_thread(osb);
1580 #define OCFS2_SEC_BITS 34
1581 #define OCFS2_SEC_SHIFT (64 - 34)
1582 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1584 /* LVB only has room for 64 bits of time here so we pack it for
1586 static u64 ocfs2_pack_timespec(struct timespec *spec)
1589 u64 sec = spec->tv_sec;
1590 u32 nsec = spec->tv_nsec;
1592 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1597 /* Call this with the lockres locked. I am reasonably sure we don't
1598 * need ip_lock in this function as anyone who would be changing those
1599 * values is supposed to be blocked in ocfs2_inode_lock right now. */
1600 static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1602 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1603 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1604 struct ocfs2_meta_lvb *lvb;
1608 lvb = (struct ocfs2_meta_lvb *)ocfs2_dlm_lvb(&lockres->l_lksb);
1611 * Invalidate the LVB of a deleted inode - this way other
1612 * nodes are forced to go to disk and discover the new inode
1615 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1616 lvb->lvb_version = 0;
1620 lvb->lvb_version = OCFS2_LVB_VERSION;
1621 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1622 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1623 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1624 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1625 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1626 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1627 lvb->lvb_iatime_packed =
1628 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1629 lvb->lvb_ictime_packed =
1630 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1631 lvb->lvb_imtime_packed =
1632 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1633 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
1634 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
1635 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
1638 mlog_meta_lvb(0, lockres);
1643 static void ocfs2_unpack_timespec(struct timespec *spec,
1646 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1647 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1650 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1652 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1653 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1654 struct ocfs2_meta_lvb *lvb;
1658 mlog_meta_lvb(0, lockres);
1660 lvb = (struct ocfs2_meta_lvb *)ocfs2_dlm_lvb(&lockres->l_lksb);
1662 /* We're safe here without the lockres lock... */
1663 spin_lock(&oi->ip_lock);
1664 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1665 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1667 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1668 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
1669 ocfs2_set_inode_flags(inode);
1671 /* fast-symlinks are a special case */
1672 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1673 inode->i_blocks = 0;
1675 inode->i_blocks = ocfs2_inode_sector_count(inode);
1677 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1678 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1679 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1680 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1681 ocfs2_unpack_timespec(&inode->i_atime,
1682 be64_to_cpu(lvb->lvb_iatime_packed));
1683 ocfs2_unpack_timespec(&inode->i_mtime,
1684 be64_to_cpu(lvb->lvb_imtime_packed));
1685 ocfs2_unpack_timespec(&inode->i_ctime,
1686 be64_to_cpu(lvb->lvb_ictime_packed));
1687 spin_unlock(&oi->ip_lock);
1692 static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1693 struct ocfs2_lock_res *lockres)
1695 struct ocfs2_meta_lvb *lvb =
1696 (struct ocfs2_meta_lvb *)ocfs2_dlm_lvb(&lockres->l_lksb);
1698 if (lvb->lvb_version == OCFS2_LVB_VERSION
1699 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
1704 /* Determine whether a lock resource needs to be refreshed, and
1705 * arbitrate who gets to refresh it.
1707 * 0 means no refresh needed.
1709 * > 0 means you need to refresh this and you MUST call
1710 * ocfs2_complete_lock_res_refresh afterwards. */
1711 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1713 unsigned long flags;
1719 spin_lock_irqsave(&lockres->l_lock, flags);
1720 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1721 spin_unlock_irqrestore(&lockres->l_lock, flags);
1725 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1726 spin_unlock_irqrestore(&lockres->l_lock, flags);
1728 ocfs2_wait_on_refreshing_lock(lockres);
1732 /* Ok, I'll be the one to refresh this lock. */
1733 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1734 spin_unlock_irqrestore(&lockres->l_lock, flags);
1742 /* If status is non zero, I'll mark it as not being in refresh
1743 * anymroe, but i won't clear the needs refresh flag. */
1744 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1747 unsigned long flags;
1750 spin_lock_irqsave(&lockres->l_lock, flags);
1751 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1753 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1754 spin_unlock_irqrestore(&lockres->l_lock, flags);
1756 wake_up(&lockres->l_event);
1761 /* may or may not return a bh if it went to disk. */
1762 static int ocfs2_inode_lock_update(struct inode *inode,
1763 struct buffer_head **bh)
1766 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1767 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1768 struct ocfs2_dinode *fe;
1769 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1773 if (ocfs2_mount_local(osb))
1776 spin_lock(&oi->ip_lock);
1777 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1778 mlog(0, "Orphaned inode %llu was deleted while we "
1779 "were waiting on a lock. ip_flags = 0x%x\n",
1780 (unsigned long long)oi->ip_blkno, oi->ip_flags);
1781 spin_unlock(&oi->ip_lock);
1785 spin_unlock(&oi->ip_lock);
1787 if (!ocfs2_should_refresh_lock_res(lockres))
1790 /* This will discard any caching information we might have had
1791 * for the inode metadata. */
1792 ocfs2_metadata_cache_purge(inode);
1794 ocfs2_extent_map_trunc(inode, 0);
1796 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
1797 mlog(0, "Trusting LVB on inode %llu\n",
1798 (unsigned long long)oi->ip_blkno);
1799 ocfs2_refresh_inode_from_lvb(inode);
1801 /* Boo, we have to go to disk. */
1802 /* read bh, cast, ocfs2_refresh_inode */
1803 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1804 bh, OCFS2_BH_CACHED, inode);
1809 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1811 /* This is a good chance to make sure we're not
1812 * locking an invalid object.
1814 * We bug on a stale inode here because we checked
1815 * above whether it was wiped from disk. The wiping
1816 * node provides a guarantee that we receive that
1817 * message and can mark the inode before dropping any
1818 * locks associated with it. */
1819 if (!OCFS2_IS_VALID_DINODE(fe)) {
1820 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1824 mlog_bug_on_msg(inode->i_generation !=
1825 le32_to_cpu(fe->i_generation),
1826 "Invalid dinode %llu disk generation: %u "
1827 "inode->i_generation: %u\n",
1828 (unsigned long long)oi->ip_blkno,
1829 le32_to_cpu(fe->i_generation),
1830 inode->i_generation);
1831 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1832 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
1833 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1834 (unsigned long long)oi->ip_blkno,
1835 (unsigned long long)le64_to_cpu(fe->i_dtime),
1836 le32_to_cpu(fe->i_flags));
1838 ocfs2_refresh_inode(inode, fe);
1843 ocfs2_complete_lock_res_refresh(lockres, status);
1849 static int ocfs2_assign_bh(struct inode *inode,
1850 struct buffer_head **ret_bh,
1851 struct buffer_head *passed_bh)
1856 /* Ok, the update went to disk for us, use the
1858 *ret_bh = passed_bh;
1864 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1865 OCFS2_I(inode)->ip_blkno,
1876 * returns < 0 error if the callback will never be called, otherwise
1877 * the result of the lock will be communicated via the callback.
1879 int ocfs2_inode_lock_full(struct inode *inode,
1880 struct buffer_head **ret_bh,
1884 int status, level, acquired;
1886 struct ocfs2_lock_res *lockres = NULL;
1887 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1888 struct buffer_head *local_bh = NULL;
1894 mlog(0, "inode %llu, take %s META lock\n",
1895 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1896 ex ? "EXMODE" : "PRMODE");
1900 /* We'll allow faking a readonly metadata lock for
1902 if (ocfs2_is_hard_readonly(osb)) {
1908 if (ocfs2_mount_local(osb))
1911 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1912 ocfs2_wait_for_recovery(osb);
1914 lockres = &OCFS2_I(inode)->ip_inode_lockres;
1915 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1917 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
1918 dlm_flags |= DLM_LKF_NOQUEUE;
1920 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1922 if (status != -EAGAIN && status != -EIOCBRETRY)
1927 /* Notify the error cleanup path to drop the cluster lock. */
1930 /* We wait twice because a node may have died while we were in
1931 * the lower dlm layers. The second time though, we've
1932 * committed to owning this lock so we don't allow signals to
1933 * abort the operation. */
1934 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1935 ocfs2_wait_for_recovery(osb);
1939 * We only see this flag if we're being called from
1940 * ocfs2_read_locked_inode(). It means we're locking an inode
1941 * which hasn't been populated yet, so clear the refresh flag
1942 * and let the caller handle it.
1944 if (inode->i_state & I_NEW) {
1947 ocfs2_complete_lock_res_refresh(lockres, 0);
1951 /* This is fun. The caller may want a bh back, or it may
1952 * not. ocfs2_inode_lock_update definitely wants one in, but
1953 * may or may not read one, depending on what's in the
1954 * LVB. The result of all of this is that we've *only* gone to
1955 * disk if we have to, so the complexity is worthwhile. */
1956 status = ocfs2_inode_lock_update(inode, &local_bh);
1958 if (status != -ENOENT)
1964 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
1973 if (ret_bh && (*ret_bh)) {
1978 ocfs2_inode_unlock(inode, ex);
1989 * This is working around a lock inversion between tasks acquiring DLM
1990 * locks while holding a page lock and the downconvert thread which
1991 * blocks dlm lock acquiry while acquiring page locks.
1993 * ** These _with_page variantes are only intended to be called from aop
1994 * methods that hold page locks and return a very specific *positive* error
1995 * code that aop methods pass up to the VFS -- test for errors with != 0. **
1997 * The DLM is called such that it returns -EAGAIN if it would have
1998 * blocked waiting for the downconvert thread. In that case we unlock
1999 * our page so the downconvert thread can make progress. Once we've
2000 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2001 * that called us can bubble that back up into the VFS who will then
2002 * immediately retry the aop call.
2004 * We do a blocking lock and immediate unlock before returning, though, so that
2005 * the lock has a great chance of being cached on this node by the time the VFS
2006 * calls back to retry the aop. This has a potential to livelock as nodes
2007 * ping locks back and forth, but that's a risk we're willing to take to avoid
2008 * the lock inversion simply.
2010 int ocfs2_inode_lock_with_page(struct inode *inode,
2011 struct buffer_head **ret_bh,
2017 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
2018 if (ret == -EAGAIN) {
2020 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2021 ocfs2_inode_unlock(inode, ex);
2022 ret = AOP_TRUNCATED_PAGE;
2028 int ocfs2_inode_lock_atime(struct inode *inode,
2029 struct vfsmount *vfsmnt,
2035 ret = ocfs2_inode_lock(inode, NULL, 0);
2042 * If we should update atime, we will get EX lock,
2043 * otherwise we just get PR lock.
2045 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2046 struct buffer_head *bh = NULL;
2048 ocfs2_inode_unlock(inode, 0);
2049 ret = ocfs2_inode_lock(inode, &bh, 1);
2055 if (ocfs2_should_update_atime(inode, vfsmnt))
2056 ocfs2_update_inode_atime(inode, bh);
2066 void ocfs2_inode_unlock(struct inode *inode,
2069 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2070 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
2071 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2075 mlog(0, "inode %llu drop %s META lock\n",
2076 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2077 ex ? "EXMODE" : "PRMODE");
2079 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2080 !ocfs2_mount_local(osb))
2081 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
2086 int ocfs2_super_lock(struct ocfs2_super *osb,
2090 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2091 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2095 if (ocfs2_is_hard_readonly(osb))
2098 if (ocfs2_mount_local(osb))
2101 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2107 /* The super block lock path is really in the best position to
2108 * know when resources covered by the lock need to be
2109 * refreshed, so we do it here. Of course, making sense of
2110 * everything is up to the caller :) */
2111 status = ocfs2_should_refresh_lock_res(lockres);
2117 status = ocfs2_refresh_slot_info(osb);
2119 ocfs2_complete_lock_res_refresh(lockres, status);
2129 void ocfs2_super_unlock(struct ocfs2_super *osb,
2132 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2133 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2135 if (!ocfs2_mount_local(osb))
2136 ocfs2_cluster_unlock(osb, lockres, level);
2139 int ocfs2_rename_lock(struct ocfs2_super *osb)
2142 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2144 if (ocfs2_is_hard_readonly(osb))
2147 if (ocfs2_mount_local(osb))
2150 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
2157 void ocfs2_rename_unlock(struct ocfs2_super *osb)
2159 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2161 if (!ocfs2_mount_local(osb))
2162 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2165 int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2168 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2169 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2170 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2174 if (ocfs2_is_hard_readonly(osb))
2177 if (ocfs2_mount_local(osb))
2180 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2187 void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2189 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2190 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2191 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2193 if (!ocfs2_mount_local(osb))
2194 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
2197 /* Reference counting of the dlm debug structure. We want this because
2198 * open references on the debug inodes can live on after a mount, so
2199 * we can't rely on the ocfs2_super to always exist. */
2200 static void ocfs2_dlm_debug_free(struct kref *kref)
2202 struct ocfs2_dlm_debug *dlm_debug;
2204 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2209 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2212 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2215 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2217 kref_get(&debug->d_refcnt);
2220 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2222 struct ocfs2_dlm_debug *dlm_debug;
2224 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2226 mlog_errno(-ENOMEM);
2230 kref_init(&dlm_debug->d_refcnt);
2231 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2232 dlm_debug->d_locking_state = NULL;
2237 /* Access to this is arbitrated for us via seq_file->sem. */
2238 struct ocfs2_dlm_seq_priv {
2239 struct ocfs2_dlm_debug *p_dlm_debug;
2240 struct ocfs2_lock_res p_iter_res;
2241 struct ocfs2_lock_res p_tmp_res;
2244 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2245 struct ocfs2_dlm_seq_priv *priv)
2247 struct ocfs2_lock_res *iter, *ret = NULL;
2248 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2250 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2252 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2253 /* discover the head of the list */
2254 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2255 mlog(0, "End of list found, %p\n", ret);
2259 /* We track our "dummy" iteration lockres' by a NULL
2261 if (iter->l_ops != NULL) {
2270 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2272 struct ocfs2_dlm_seq_priv *priv = m->private;
2273 struct ocfs2_lock_res *iter;
2275 spin_lock(&ocfs2_dlm_tracking_lock);
2276 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2278 /* Since lockres' have the lifetime of their container
2279 * (which can be inodes, ocfs2_supers, etc) we want to
2280 * copy this out to a temporary lockres while still
2281 * under the spinlock. Obviously after this we can't
2282 * trust any pointers on the copy returned, but that's
2283 * ok as the information we want isn't typically held
2285 priv->p_tmp_res = *iter;
2286 iter = &priv->p_tmp_res;
2288 spin_unlock(&ocfs2_dlm_tracking_lock);
2293 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2297 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2299 struct ocfs2_dlm_seq_priv *priv = m->private;
2300 struct ocfs2_lock_res *iter = v;
2301 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2303 spin_lock(&ocfs2_dlm_tracking_lock);
2304 iter = ocfs2_dlm_next_res(iter, priv);
2305 list_del_init(&dummy->l_debug_list);
2307 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2308 priv->p_tmp_res = *iter;
2309 iter = &priv->p_tmp_res;
2311 spin_unlock(&ocfs2_dlm_tracking_lock);
2316 /* So that debugfs.ocfs2 can determine which format is being used */
2317 #define OCFS2_DLM_DEBUG_STR_VERSION 1
2318 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2322 struct ocfs2_lock_res *lockres = v;
2327 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2329 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2330 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2332 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2334 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2336 seq_printf(m, "%d\t"
2347 lockres->l_unlock_action,
2348 lockres->l_ro_holders,
2349 lockres->l_ex_holders,
2350 lockres->l_requested,
2351 lockres->l_blocking);
2353 /* Dump the raw LVB */
2354 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2355 for(i = 0; i < DLM_LVB_LEN; i++)
2356 seq_printf(m, "0x%x\t", lvb[i]);
2359 seq_printf(m, "\n");
2363 static const struct seq_operations ocfs2_dlm_seq_ops = {
2364 .start = ocfs2_dlm_seq_start,
2365 .stop = ocfs2_dlm_seq_stop,
2366 .next = ocfs2_dlm_seq_next,
2367 .show = ocfs2_dlm_seq_show,
2370 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2372 struct seq_file *seq = (struct seq_file *) file->private_data;
2373 struct ocfs2_dlm_seq_priv *priv = seq->private;
2374 struct ocfs2_lock_res *res = &priv->p_iter_res;
2376 ocfs2_remove_lockres_tracking(res);
2377 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2378 return seq_release_private(inode, file);
2381 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2384 struct ocfs2_dlm_seq_priv *priv;
2385 struct seq_file *seq;
2386 struct ocfs2_super *osb;
2388 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2394 osb = inode->i_private;
2395 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2396 priv->p_dlm_debug = osb->osb_dlm_debug;
2397 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2399 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2406 seq = (struct seq_file *) file->private_data;
2407 seq->private = priv;
2409 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2416 static const struct file_operations ocfs2_dlm_debug_fops = {
2417 .open = ocfs2_dlm_debug_open,
2418 .release = ocfs2_dlm_debug_release,
2420 .llseek = seq_lseek,
2423 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2426 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2428 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2430 osb->osb_debug_root,
2432 &ocfs2_dlm_debug_fops);
2433 if (!dlm_debug->d_locking_state) {
2436 "Unable to create locking state debugfs file.\n");
2440 ocfs2_get_dlm_debug(dlm_debug);
2445 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2447 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2450 debugfs_remove(dlm_debug->d_locking_state);
2451 ocfs2_put_dlm_debug(dlm_debug);
2455 int ocfs2_dlm_init(struct ocfs2_super *osb)
2458 struct ocfs2_cluster_connection *conn = NULL;
2462 if (ocfs2_mount_local(osb))
2465 status = ocfs2_dlm_init_debug(osb);
2471 /* launch downconvert thread */
2472 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2473 if (IS_ERR(osb->dc_task)) {
2474 status = PTR_ERR(osb->dc_task);
2475 osb->dc_task = NULL;
2480 /* for now, uuid == domain */
2481 status = ocfs2_cluster_connect(osb->uuid_str,
2482 strlen(osb->uuid_str),
2483 ocfs2_do_node_down, osb,
2491 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2492 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2499 ocfs2_dlm_shutdown_debug(osb);
2501 kthread_stop(osb->dc_task);
2508 void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2512 ocfs2_drop_osb_locks(osb);
2515 * Now that we have dropped all locks and ocfs2_dismount_volume()
2516 * has disabled recovery, the DLM won't be talking to us. It's
2517 * safe to tear things down before disconnecting the cluster.
2521 kthread_stop(osb->dc_task);
2522 osb->dc_task = NULL;
2525 ocfs2_lock_res_free(&osb->osb_super_lockres);
2526 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2528 ocfs2_cluster_disconnect(osb->cconn);
2531 ocfs2_dlm_shutdown_debug(osb);
2536 static void ocfs2_unlock_ast(void *opaque, int error)
2538 struct ocfs2_lock_res *lockres = opaque;
2539 unsigned long flags;
2543 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2544 lockres->l_unlock_action);
2546 spin_lock_irqsave(&lockres->l_lock, flags);
2547 /* We tried to cancel a convert request, but it was already
2548 * granted. All we want to do here is clear our unlock
2549 * state. The wake_up call done at the bottom is redundant
2550 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2551 * hurt anything anyway */
2552 if (error == -DLM_ECANCEL &&
2553 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2554 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2556 /* We don't clear the busy flag in this case as it
2557 * should have been cleared by the ast which the dlm
2559 goto complete_unlock;
2562 /* DLM_EUNLOCK is the success code for unlock */
2563 if (error != -DLM_EUNLOCK) {
2564 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
2565 "unlock_action %d\n", error, lockres->l_name,
2566 lockres->l_unlock_action);
2567 spin_unlock_irqrestore(&lockres->l_lock, flags);
2571 switch(lockres->l_unlock_action) {
2572 case OCFS2_UNLOCK_CANCEL_CONVERT:
2573 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2574 lockres->l_action = OCFS2_AST_INVALID;
2576 case OCFS2_UNLOCK_DROP_LOCK:
2577 lockres->l_level = DLM_LOCK_IV;
2583 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2585 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2586 spin_unlock_irqrestore(&lockres->l_lock, flags);
2588 wake_up(&lockres->l_event);
2593 static int ocfs2_drop_lock(struct ocfs2_super *osb,
2594 struct ocfs2_lock_res *lockres)
2597 unsigned long flags;
2600 /* We didn't get anywhere near actually using this lockres. */
2601 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2604 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
2605 lkm_flags |= DLM_LKF_VALBLK;
2607 spin_lock_irqsave(&lockres->l_lock, flags);
2609 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2610 "lockres %s, flags 0x%lx\n",
2611 lockres->l_name, lockres->l_flags);
2613 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2614 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2615 "%u, unlock_action = %u\n",
2616 lockres->l_name, lockres->l_flags, lockres->l_action,
2617 lockres->l_unlock_action);
2619 spin_unlock_irqrestore(&lockres->l_lock, flags);
2621 /* XXX: Today we just wait on any busy
2622 * locks... Perhaps we need to cancel converts in the
2624 ocfs2_wait_on_busy_lock(lockres);
2626 spin_lock_irqsave(&lockres->l_lock, flags);
2629 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2630 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2631 lockres->l_level == DLM_LOCK_EX &&
2632 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2633 lockres->l_ops->set_lvb(lockres);
2636 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2637 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2639 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2640 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2642 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2643 spin_unlock_irqrestore(&lockres->l_lock, flags);
2647 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2649 /* make sure we never get here while waiting for an ast to
2651 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2653 /* is this necessary? */
2654 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2655 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2656 spin_unlock_irqrestore(&lockres->l_lock, flags);
2658 mlog(0, "lock %s\n", lockres->l_name);
2660 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags,
2663 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
2664 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2665 /* XXX Need to abstract this */
2666 dlm_print_one_lock(lockres->l_lksb.lksb_o2dlm.lockid);
2669 mlog(0, "lock %s, successfull return from ocfs2_dlm_unlock\n",
2672 ocfs2_wait_on_busy_lock(lockres);
2678 /* Mark the lockres as being dropped. It will no longer be
2679 * queued if blocking, but we still may have to wait on it
2680 * being dequeued from the downconvert thread before we can consider
2683 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2684 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2687 struct ocfs2_mask_waiter mw;
2688 unsigned long flags;
2690 ocfs2_init_mask_waiter(&mw);
2692 spin_lock_irqsave(&lockres->l_lock, flags);
2693 lockres->l_flags |= OCFS2_LOCK_FREEING;
2694 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2695 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2696 spin_unlock_irqrestore(&lockres->l_lock, flags);
2698 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2700 status = ocfs2_wait_for_mask(&mw);
2704 spin_lock_irqsave(&lockres->l_lock, flags);
2706 spin_unlock_irqrestore(&lockres->l_lock, flags);
2709 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2710 struct ocfs2_lock_res *lockres)
2714 ocfs2_mark_lockres_freeing(lockres);
2715 ret = ocfs2_drop_lock(osb, lockres);
2720 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2722 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2723 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
2726 int ocfs2_drop_inode_locks(struct inode *inode)
2732 /* No need to call ocfs2_mark_lockres_freeing here -
2733 * ocfs2_clear_inode has done it for us. */
2735 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2736 &OCFS2_I(inode)->ip_open_lockres);
2742 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2743 &OCFS2_I(inode)->ip_inode_lockres);
2746 if (err < 0 && !status)
2749 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2750 &OCFS2_I(inode)->ip_rw_lockres);
2753 if (err < 0 && !status)
2760 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2763 assert_spin_locked(&lockres->l_lock);
2765 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
2767 if (lockres->l_level <= new_level) {
2768 mlog(ML_ERROR, "lockres->l_level (%d) <= new_level (%d)\n",
2769 lockres->l_level, new_level);
2773 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2774 lockres->l_name, new_level, lockres->l_blocking);
2776 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2777 lockres->l_requested = new_level;
2778 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2781 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2782 struct ocfs2_lock_res *lockres,
2787 u32 dlm_flags = DLM_LKF_CONVERT;
2792 dlm_flags |= DLM_LKF_VALBLK;
2794 ret = ocfs2_dlm_lock(osb->cconn,
2799 OCFS2_LOCK_ID_MAX_LEN - 1,
2802 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
2803 ocfs2_recover_from_dlm_error(lockres, 1);
2813 /* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
2814 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2815 struct ocfs2_lock_res *lockres)
2817 assert_spin_locked(&lockres->l_lock);
2820 mlog(0, "lock %s\n", lockres->l_name);
2822 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2823 /* If we're already trying to cancel a lock conversion
2824 * then just drop the spinlock and allow the caller to
2825 * requeue this lock. */
2827 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2831 /* were we in a convert when we got the bast fire? */
2832 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2833 lockres->l_action != OCFS2_AST_DOWNCONVERT);
2834 /* set things up for the unlockast to know to just
2835 * clear out the ast_action and unset busy, etc. */
2836 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2838 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2839 "lock %s, invalid flags: 0x%lx\n",
2840 lockres->l_name, lockres->l_flags);
2845 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2846 struct ocfs2_lock_res *lockres)
2851 mlog(0, "lock %s\n", lockres->l_name);
2853 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
2854 DLM_LKF_CANCEL, lockres);
2856 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
2857 ocfs2_recover_from_dlm_error(lockres, 0);
2860 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres->l_name);
2866 static int ocfs2_unblock_lock(struct ocfs2_super *osb,
2867 struct ocfs2_lock_res *lockres,
2868 struct ocfs2_unblock_ctl *ctl)
2870 unsigned long flags;
2878 spin_lock_irqsave(&lockres->l_lock, flags);
2880 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2883 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2885 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2886 spin_unlock_irqrestore(&lockres->l_lock, flags);
2888 ret = ocfs2_cancel_convert(osb, lockres);
2895 /* if we're blocking an exclusive and we have *any* holders,
2897 if ((lockres->l_blocking == DLM_LOCK_EX)
2898 && (lockres->l_ex_holders || lockres->l_ro_holders))
2901 /* If it's a PR we're blocking, then only
2902 * requeue if we've got any EX holders */
2903 if (lockres->l_blocking == DLM_LOCK_PR &&
2904 lockres->l_ex_holders)
2908 * Can we get a lock in this state if the holder counts are
2909 * zero? The meta data unblock code used to check this.
2911 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
2912 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
2915 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2917 if (lockres->l_ops->check_downconvert
2918 && !lockres->l_ops->check_downconvert(lockres, new_level))
2921 /* If we get here, then we know that there are no more
2922 * incompatible holders (and anyone asking for an incompatible
2923 * lock is blocked). We can now downconvert the lock */
2924 if (!lockres->l_ops->downconvert_worker)
2927 /* Some lockres types want to do a bit of work before
2928 * downconverting a lock. Allow that here. The worker function
2929 * may sleep, so we save off a copy of what we're blocking as
2930 * it may change while we're not holding the spin lock. */
2931 blocking = lockres->l_blocking;
2932 spin_unlock_irqrestore(&lockres->l_lock, flags);
2934 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
2936 if (ctl->unblock_action == UNBLOCK_STOP_POST)
2939 spin_lock_irqsave(&lockres->l_lock, flags);
2940 if (blocking != lockres->l_blocking) {
2941 /* If this changed underneath us, then we can't drop
2949 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2950 if (lockres->l_level == DLM_LOCK_EX)
2954 * We only set the lvb if the lock has been fully
2955 * refreshed - otherwise we risk setting stale
2956 * data. Otherwise, there's no need to actually clear
2957 * out the lvb here as it's value is still valid.
2959 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2960 lockres->l_ops->set_lvb(lockres);
2963 ocfs2_prepare_downconvert(lockres, new_level);
2964 spin_unlock_irqrestore(&lockres->l_lock, flags);
2965 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
2971 spin_unlock_irqrestore(&lockres->l_lock, flags);
2978 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
2981 struct inode *inode;
2982 struct address_space *mapping;
2984 inode = ocfs2_lock_res_inode(lockres);
2985 mapping = inode->i_mapping;
2987 if (!S_ISREG(inode->i_mode))
2991 * We need this before the filemap_fdatawrite() so that it can
2992 * transfer the dirty bit from the PTE to the
2993 * page. Unfortunately this means that even for EX->PR
2994 * downconverts, we'll lose our mappings and have to build
2997 unmap_mapping_range(mapping, 0, 0, 0);
2999 if (filemap_fdatawrite(mapping)) {
3000 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3001 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3003 sync_mapping_buffers(mapping);
3004 if (blocking == DLM_LOCK_EX) {
3005 truncate_inode_pages(mapping, 0);
3007 /* We only need to wait on the I/O if we're not also
3008 * truncating pages because truncate_inode_pages waits
3009 * for us above. We don't truncate pages if we're
3010 * blocking anything < EXMODE because we want to keep
3011 * them around in that case. */
3012 filemap_fdatawait(mapping);
3016 return UNBLOCK_CONTINUE;
3019 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3022 struct inode *inode = ocfs2_lock_res_inode(lockres);
3023 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
3025 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3026 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
3031 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
3035 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3037 struct inode *inode = ocfs2_lock_res_inode(lockres);
3039 __ocfs2_stuff_meta_lvb(inode);
3043 * Does the final reference drop on our dentry lock. Right now this
3044 * happens in the downconvert thread, but we could choose to simplify the
3045 * dlmglue API and push these off to the ocfs2_wq in the future.
3047 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3048 struct ocfs2_lock_res *lockres)
3050 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3051 ocfs2_dentry_lock_put(osb, dl);
3055 * d_delete() matching dentries before the lock downconvert.
3057 * At this point, any process waiting to destroy the
3058 * dentry_lock due to last ref count is stopped by the
3059 * OCFS2_LOCK_QUEUED flag.
3061 * We have two potential problems
3063 * 1) If we do the last reference drop on our dentry_lock (via dput)
3064 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3065 * the downconvert to finish. Instead we take an elevated
3066 * reference and push the drop until after we've completed our
3067 * unblock processing.
3069 * 2) There might be another process with a final reference,
3070 * waiting on us to finish processing. If this is the case, we
3071 * detect it and exit out - there's no more dentries anyway.
3073 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3076 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3077 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3078 struct dentry *dentry;
3079 unsigned long flags;
3083 * This node is blocking another node from getting a read
3084 * lock. This happens when we've renamed within a
3085 * directory. We've forced the other nodes to d_delete(), but
3086 * we never actually dropped our lock because it's still
3087 * valid. The downconvert code will retain a PR for this node,
3088 * so there's no further work to do.
3090 if (blocking == DLM_LOCK_PR)
3091 return UNBLOCK_CONTINUE;
3094 * Mark this inode as potentially orphaned. The code in
3095 * ocfs2_delete_inode() will figure out whether it actually
3096 * needs to be freed or not.
3098 spin_lock(&oi->ip_lock);
3099 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3100 spin_unlock(&oi->ip_lock);
3103 * Yuck. We need to make sure however that the check of
3104 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3105 * respect to a reference decrement or the setting of that
3108 spin_lock_irqsave(&lockres->l_lock, flags);
3109 spin_lock(&dentry_attach_lock);
3110 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3115 spin_unlock(&dentry_attach_lock);
3116 spin_unlock_irqrestore(&lockres->l_lock, flags);
3118 mlog(0, "extra_ref = %d\n", extra_ref);
3121 * We have a process waiting on us in ocfs2_dentry_iput(),
3122 * which means we can't have any more outstanding
3123 * aliases. There's no need to do any more work.
3126 return UNBLOCK_CONTINUE;
3128 spin_lock(&dentry_attach_lock);
3130 dentry = ocfs2_find_local_alias(dl->dl_inode,
3131 dl->dl_parent_blkno, 1);
3134 spin_unlock(&dentry_attach_lock);
3136 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3137 dentry->d_name.name);
3140 * The following dcache calls may do an
3141 * iput(). Normally we don't want that from the
3142 * downconverting thread, but in this case it's ok
3143 * because the requesting node already has an
3144 * exclusive lock on the inode, so it can't be queued
3145 * for a downconvert.
3150 spin_lock(&dentry_attach_lock);
3152 spin_unlock(&dentry_attach_lock);
3155 * If we are the last holder of this dentry lock, there is no
3156 * reason to downconvert so skip straight to the unlock.
3158 if (dl->dl_count == 1)
3159 return UNBLOCK_STOP_POST;
3161 return UNBLOCK_CONTINUE_POST;
3165 * This is the filesystem locking protocol. It provides the lock handling
3166 * hooks for the underlying DLM. It has a maximum version number.
3167 * The version number allows interoperability with systems running at
3168 * the same major number and an equal or smaller minor number.
3170 * Whenever the filesystem does new things with locks (adds or removes a
3171 * lock, orders them differently, does different things underneath a lock),
3172 * the version must be changed. The protocol is negotiated when joining
3173 * the dlm domain. A node may join the domain if its major version is
3174 * identical to all other nodes and its minor version is greater than
3175 * or equal to all other nodes. When its minor version is greater than
3176 * the other nodes, it will run at the minor version specified by the
3179 * If a locking change is made that will not be compatible with older
3180 * versions, the major number must be increased and the minor version set
3181 * to zero. If a change merely adds a behavior that can be disabled when
3182 * speaking to older versions, the minor version must be increased. If a
3183 * change adds a fully backwards compatible change (eg, LVB changes that
3184 * are just ignored by older versions), the version does not need to be
3187 static struct ocfs2_locking_protocol lproto = {
3189 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
3190 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
3192 .lp_lock_ast = ocfs2_locking_ast,
3193 .lp_blocking_ast = ocfs2_blocking_ast,
3194 .lp_unlock_ast = ocfs2_unlock_ast,
3197 /* This interface isn't the final one, hence the less-than-perfect names */
3198 void dlmglue_init_stack(void)
3200 o2cb_get_stack(&lproto);
3203 void dlmglue_exit_stack(void)
3208 static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3209 struct ocfs2_lock_res *lockres)
3212 struct ocfs2_unblock_ctl ctl = {0, 0,};
3213 unsigned long flags;
3215 /* Our reference to the lockres in this function can be
3216 * considered valid until we remove the OCFS2_LOCK_QUEUED
3222 BUG_ON(!lockres->l_ops);
3224 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3226 /* Detect whether a lock has been marked as going away while
3227 * the downconvert thread was processing other things. A lock can
3228 * still be marked with OCFS2_LOCK_FREEING after this check,
3229 * but short circuiting here will still save us some
3231 spin_lock_irqsave(&lockres->l_lock, flags);
3232 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3234 spin_unlock_irqrestore(&lockres->l_lock, flags);
3236 status = ocfs2_unblock_lock(osb, lockres, &ctl);
3240 spin_lock_irqsave(&lockres->l_lock, flags);
3242 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
3243 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3245 ocfs2_schedule_blocked_lock(osb, lockres);
3247 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
3248 ctl.requeue ? "yes" : "no");
3249 spin_unlock_irqrestore(&lockres->l_lock, flags);
3251 if (ctl.unblock_action != UNBLOCK_CONTINUE
3252 && lockres->l_ops->post_unlock)
3253 lockres->l_ops->post_unlock(osb, lockres);
3258 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3259 struct ocfs2_lock_res *lockres)
3263 assert_spin_locked(&lockres->l_lock);
3265 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3266 /* Do not schedule a lock for downconvert when it's on
3267 * the way to destruction - any nodes wanting access
3268 * to the resource will get it soon. */
3269 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3270 lockres->l_name, lockres->l_flags);
3274 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3276 spin_lock(&osb->dc_task_lock);
3277 if (list_empty(&lockres->l_blocked_list)) {
3278 list_add_tail(&lockres->l_blocked_list,
3279 &osb->blocked_lock_list);
3280 osb->blocked_lock_count++;
3282 spin_unlock(&osb->dc_task_lock);
3287 static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3289 unsigned long processed;
3290 struct ocfs2_lock_res *lockres;
3294 spin_lock(&osb->dc_task_lock);
3295 /* grab this early so we know to try again if a state change and
3296 * wake happens part-way through our work */
3297 osb->dc_work_sequence = osb->dc_wake_sequence;
3299 processed = osb->blocked_lock_count;
3301 BUG_ON(list_empty(&osb->blocked_lock_list));
3303 lockres = list_entry(osb->blocked_lock_list.next,
3304 struct ocfs2_lock_res, l_blocked_list);
3305 list_del_init(&lockres->l_blocked_list);
3306 osb->blocked_lock_count--;
3307 spin_unlock(&osb->dc_task_lock);
3312 ocfs2_process_blocked_lock(osb, lockres);
3314 spin_lock(&osb->dc_task_lock);
3316 spin_unlock(&osb->dc_task_lock);
3321 static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3325 spin_lock(&osb->dc_task_lock);
3326 if (list_empty(&osb->blocked_lock_list))
3329 spin_unlock(&osb->dc_task_lock);
3333 static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
3335 int should_wake = 0;
3337 spin_lock(&osb->dc_task_lock);
3338 if (osb->dc_work_sequence != osb->dc_wake_sequence)
3340 spin_unlock(&osb->dc_task_lock);
3345 static int ocfs2_downconvert_thread(void *arg)
3348 struct ocfs2_super *osb = arg;
3350 /* only quit once we've been asked to stop and there is no more
3352 while (!(kthread_should_stop() &&
3353 ocfs2_downconvert_thread_lists_empty(osb))) {
3355 wait_event_interruptible(osb->dc_event,
3356 ocfs2_downconvert_thread_should_wake(osb) ||
3357 kthread_should_stop());
3359 mlog(0, "downconvert_thread: awoken\n");
3361 ocfs2_downconvert_thread_do_work(osb);
3364 osb->dc_task = NULL;
3368 void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
3370 spin_lock(&osb->dc_task_lock);
3371 /* make sure the voting thread gets a swipe at whatever changes
3372 * the caller may have made to the voting state */
3373 osb->dc_wake_sequence++;
3374 spin_unlock(&osb->dc_task_lock);
3375 wake_up(&osb->dc_event);