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 #define MLOG_MASK_PREFIX ML_DLM_GLUE
41 #include <cluster/masklog.h>
44 #include "ocfs2_lockingver.h"
49 #include "extent_map.h"
51 #include "heartbeat.h"
54 #include "stackglue.h"
59 #include "buffer_head_io.h"
61 struct ocfs2_mask_waiter {
62 struct list_head mw_item;
64 struct completion mw_complete;
65 unsigned long mw_mask;
66 unsigned long mw_goal;
69 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
70 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
71 static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres);
74 * Return value from ->downconvert_worker functions.
76 * These control the precise actions of ocfs2_unblock_lock()
77 * and ocfs2_process_blocked_lock()
80 enum ocfs2_unblock_action {
81 UNBLOCK_CONTINUE = 0, /* Continue downconvert */
82 UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire
83 * ->post_unlock callback */
84 UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire
85 * ->post_unlock() callback. */
88 struct ocfs2_unblock_ctl {
90 enum ocfs2_unblock_action unblock_action;
93 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
95 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
97 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
100 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
103 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
104 struct ocfs2_lock_res *lockres);
107 #define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres)
109 /* This aids in debugging situations where a bad LVB might be involved. */
110 static void ocfs2_dump_meta_lvb_info(u64 level,
111 const char *function,
113 struct ocfs2_lock_res *lockres)
115 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
117 mlog(level, "LVB information for %s (called from %s:%u):\n",
118 lockres->l_name, function, line);
119 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
120 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
121 be32_to_cpu(lvb->lvb_igeneration));
122 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
123 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
124 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
125 be16_to_cpu(lvb->lvb_imode));
126 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
127 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
128 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
129 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
130 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
131 be32_to_cpu(lvb->lvb_iattr));
136 * OCFS2 Lock Resource Operations
138 * These fine tune the behavior of the generic dlmglue locking infrastructure.
140 * The most basic of lock types can point ->l_priv to their respective
141 * struct ocfs2_super and allow the default actions to manage things.
143 * Right now, each lock type also needs to implement an init function,
144 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
145 * should be called when the lock is no longer needed (i.e., object
148 struct ocfs2_lock_res_ops {
150 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
151 * this callback if ->l_priv is not an ocfs2_super pointer
153 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
156 * Optionally called in the downconvert thread after a
157 * successful downconvert. The lockres will not be referenced
158 * after this callback is called, so it is safe to free
161 * The exact semantics of when this is called are controlled
162 * by ->downconvert_worker()
164 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
167 * Allow a lock type to add checks to determine whether it is
168 * safe to downconvert a lock. Return 0 to re-queue the
169 * downconvert at a later time, nonzero to continue.
171 * For most locks, the default checks that there are no
172 * incompatible holders are sufficient.
174 * Called with the lockres spinlock held.
176 int (*check_downconvert)(struct ocfs2_lock_res *, int);
179 * Allows a lock type to populate the lock value block. This
180 * is called on downconvert, and when we drop a lock.
182 * Locks that want to use this should set LOCK_TYPE_USES_LVB
183 * in the flags field.
185 * Called with the lockres spinlock held.
187 void (*set_lvb)(struct ocfs2_lock_res *);
190 * Called from the downconvert thread when it is determined
191 * that a lock will be downconverted. This is called without
192 * any locks held so the function can do work that might
193 * schedule (syncing out data, etc).
195 * This should return any one of the ocfs2_unblock_action
196 * values, depending on what it wants the thread to do.
198 int (*downconvert_worker)(struct ocfs2_lock_res *, int);
201 * LOCK_TYPE_* flags which describe the specific requirements
202 * of a lock type. Descriptions of each individual flag follow.
208 * Some locks want to "refresh" potentially stale data when a
209 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
210 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
211 * individual lockres l_flags member from the ast function. It is
212 * expected that the locking wrapper will clear the
213 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
215 #define LOCK_TYPE_REQUIRES_REFRESH 0x1
218 * Indicate that a lock type makes use of the lock value block. The
219 * ->set_lvb lock type callback must be defined.
221 #define LOCK_TYPE_USES_LVB 0x2
223 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
224 .get_osb = ocfs2_get_inode_osb,
228 static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops = {
229 .get_osb = ocfs2_get_inode_osb,
230 .check_downconvert = ocfs2_check_meta_downconvert,
231 .set_lvb = ocfs2_set_meta_lvb,
232 .downconvert_worker = ocfs2_data_convert_worker,
233 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
236 static struct ocfs2_lock_res_ops ocfs2_super_lops = {
237 .flags = LOCK_TYPE_REQUIRES_REFRESH,
240 static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
244 static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
245 .get_osb = ocfs2_get_dentry_osb,
246 .post_unlock = ocfs2_dentry_post_unlock,
247 .downconvert_worker = ocfs2_dentry_convert_worker,
251 static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
252 .get_osb = ocfs2_get_inode_osb,
256 static struct ocfs2_lock_res_ops ocfs2_flock_lops = {
257 .get_osb = ocfs2_get_file_osb,
262 * This is the filesystem locking protocol version.
264 * Whenever the filesystem does new things with locks (adds or removes a
265 * lock, orders them differently, does different things underneath a lock),
266 * the version must be changed. The protocol is negotiated when joining
267 * the dlm domain. A node may join the domain if its major version is
268 * identical to all other nodes and its minor version is greater than
269 * or equal to all other nodes. When its minor version is greater than
270 * the other nodes, it will run at the minor version specified by the
273 * If a locking change is made that will not be compatible with older
274 * versions, the major number must be increased and the minor version set
275 * to zero. If a change merely adds a behavior that can be disabled when
276 * speaking to older versions, the minor version must be increased. If a
277 * change adds a fully backwards compatible change (eg, LVB changes that
278 * are just ignored by older versions), the version does not need to be
281 const struct dlm_protocol_version ocfs2_locking_protocol = {
282 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
283 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
286 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
288 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
289 lockres->l_type == OCFS2_LOCK_TYPE_RW ||
290 lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
293 static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
295 BUG_ON(!ocfs2_is_inode_lock(lockres));
297 return (struct inode *) lockres->l_priv;
300 static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
302 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
304 return (struct ocfs2_dentry_lock *)lockres->l_priv;
307 static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
309 if (lockres->l_ops->get_osb)
310 return lockres->l_ops->get_osb(lockres);
312 return (struct ocfs2_super *)lockres->l_priv;
315 static int ocfs2_lock_create(struct ocfs2_super *osb,
316 struct ocfs2_lock_res *lockres,
319 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
321 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
322 struct ocfs2_lock_res *lockres,
324 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
325 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
326 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
327 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
328 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
329 struct ocfs2_lock_res *lockres);
330 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
332 #define ocfs2_log_dlm_error(_func, _stat, _lockres) do { \
333 mlog(ML_ERROR, "Dlm error \"%s\" while calling %s on " \
334 "resource %s: %s\n", dlm_errname(_stat), _func, \
335 _lockres->l_name, dlm_errmsg(_stat)); \
337 static int ocfs2_downconvert_thread(void *arg);
338 static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
339 struct ocfs2_lock_res *lockres);
340 static int ocfs2_inode_lock_update(struct inode *inode,
341 struct buffer_head **bh);
342 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
343 static inline int ocfs2_highest_compat_lock_level(int level);
344 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
346 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
347 struct ocfs2_lock_res *lockres,
350 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
351 struct ocfs2_lock_res *lockres);
352 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
353 struct ocfs2_lock_res *lockres);
356 static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
365 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
367 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
368 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
369 (long long)blkno, generation);
371 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
373 mlog(0, "built lock resource with name: %s\n", name);
378 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
380 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
381 struct ocfs2_dlm_debug *dlm_debug)
383 mlog(0, "Add tracking for lockres %s\n", res->l_name);
385 spin_lock(&ocfs2_dlm_tracking_lock);
386 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
387 spin_unlock(&ocfs2_dlm_tracking_lock);
390 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
392 spin_lock(&ocfs2_dlm_tracking_lock);
393 if (!list_empty(&res->l_debug_list))
394 list_del_init(&res->l_debug_list);
395 spin_unlock(&ocfs2_dlm_tracking_lock);
398 static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
399 struct ocfs2_lock_res *res,
400 enum ocfs2_lock_type type,
401 struct ocfs2_lock_res_ops *ops,
408 res->l_level = DLM_LOCK_IV;
409 res->l_requested = DLM_LOCK_IV;
410 res->l_blocking = DLM_LOCK_IV;
411 res->l_action = OCFS2_AST_INVALID;
412 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
414 res->l_flags = OCFS2_LOCK_INITIALIZED;
416 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
419 void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
421 /* This also clears out the lock status block */
422 memset(res, 0, sizeof(struct ocfs2_lock_res));
423 spin_lock_init(&res->l_lock);
424 init_waitqueue_head(&res->l_event);
425 INIT_LIST_HEAD(&res->l_blocked_list);
426 INIT_LIST_HEAD(&res->l_mask_waiters);
429 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
430 enum ocfs2_lock_type type,
431 unsigned int generation,
434 struct ocfs2_lock_res_ops *ops;
437 case OCFS2_LOCK_TYPE_RW:
438 ops = &ocfs2_inode_rw_lops;
440 case OCFS2_LOCK_TYPE_META:
441 ops = &ocfs2_inode_inode_lops;
443 case OCFS2_LOCK_TYPE_OPEN:
444 ops = &ocfs2_inode_open_lops;
447 mlog_bug_on_msg(1, "type: %d\n", type);
448 ops = NULL; /* thanks, gcc */
452 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
453 generation, res->l_name);
454 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
457 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
459 struct inode *inode = ocfs2_lock_res_inode(lockres);
461 return OCFS2_SB(inode->i_sb);
464 static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
466 struct ocfs2_file_private *fp = lockres->l_priv;
468 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
471 static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
473 __be64 inode_blkno_be;
475 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
478 return be64_to_cpu(inode_blkno_be);
481 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
483 struct ocfs2_dentry_lock *dl = lockres->l_priv;
485 return OCFS2_SB(dl->dl_inode->i_sb);
488 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
489 u64 parent, struct inode *inode)
492 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
493 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
494 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
496 ocfs2_lock_res_init_once(lockres);
499 * Unfortunately, the standard lock naming scheme won't work
500 * here because we have two 16 byte values to use. Instead,
501 * we'll stuff the inode number as a binary value. We still
502 * want error prints to show something without garbling the
503 * display, so drop a null byte in there before the inode
504 * number. A future version of OCFS2 will likely use all
505 * binary lock names. The stringified names have been a
506 * tremendous aid in debugging, but now that the debugfs
507 * interface exists, we can mangle things there if need be.
509 * NOTE: We also drop the standard "pad" value (the total lock
510 * name size stays the same though - the last part is all
511 * zeros due to the memset in ocfs2_lock_res_init_once()
513 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
515 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
518 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
520 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
523 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
524 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
528 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
529 struct ocfs2_super *osb)
531 /* Superblock lockres doesn't come from a slab so we call init
532 * once on it manually. */
533 ocfs2_lock_res_init_once(res);
534 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
536 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
537 &ocfs2_super_lops, osb);
540 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
541 struct ocfs2_super *osb)
543 /* Rename lockres doesn't come from a slab so we call init
544 * once on it manually. */
545 ocfs2_lock_res_init_once(res);
546 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
547 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
548 &ocfs2_rename_lops, osb);
551 void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
552 struct ocfs2_file_private *fp)
554 struct inode *inode = fp->fp_file->f_mapping->host;
555 struct ocfs2_inode_info *oi = OCFS2_I(inode);
557 ocfs2_lock_res_init_once(lockres);
558 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
559 inode->i_generation, lockres->l_name);
560 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
561 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
563 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
566 void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
570 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
573 ocfs2_remove_lockres_tracking(res);
575 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
576 "Lockres %s is on the blocked list\n",
578 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
579 "Lockres %s has mask waiters pending\n",
581 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
582 "Lockres %s is locked\n",
584 mlog_bug_on_msg(res->l_ro_holders,
585 "Lockres %s has %u ro holders\n",
586 res->l_name, res->l_ro_holders);
587 mlog_bug_on_msg(res->l_ex_holders,
588 "Lockres %s has %u ex holders\n",
589 res->l_name, res->l_ex_holders);
591 /* Need to clear out the lock status block for the dlm */
592 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
598 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
607 lockres->l_ex_holders++;
610 lockres->l_ro_holders++;
619 static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
628 BUG_ON(!lockres->l_ex_holders);
629 lockres->l_ex_holders--;
632 BUG_ON(!lockres->l_ro_holders);
633 lockres->l_ro_holders--;
641 /* WARNING: This function lives in a world where the only three lock
642 * levels are EX, PR, and NL. It *will* have to be adjusted when more
643 * lock types are added. */
644 static inline int ocfs2_highest_compat_lock_level(int level)
646 int new_level = DLM_LOCK_EX;
648 if (level == DLM_LOCK_EX)
649 new_level = DLM_LOCK_NL;
650 else if (level == DLM_LOCK_PR)
651 new_level = DLM_LOCK_PR;
655 static void lockres_set_flags(struct ocfs2_lock_res *lockres,
656 unsigned long newflags)
658 struct ocfs2_mask_waiter *mw, *tmp;
660 assert_spin_locked(&lockres->l_lock);
662 lockres->l_flags = newflags;
664 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
665 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
668 list_del_init(&mw->mw_item);
670 complete(&mw->mw_complete);
673 static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
675 lockres_set_flags(lockres, lockres->l_flags | or);
677 static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
680 lockres_set_flags(lockres, lockres->l_flags & ~clear);
683 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
687 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
688 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
689 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
690 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
692 lockres->l_level = lockres->l_requested;
693 if (lockres->l_level <=
694 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
695 lockres->l_blocking = DLM_LOCK_NL;
696 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
698 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
703 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
707 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
708 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
710 /* Convert from RO to EX doesn't really need anything as our
711 * information is already up to data. Convert from NL to
712 * *anything* however should mark ourselves as needing an
714 if (lockres->l_level == DLM_LOCK_NL &&
715 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
716 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
718 lockres->l_level = lockres->l_requested;
719 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
724 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
728 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
729 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
731 if (lockres->l_requested > DLM_LOCK_NL &&
732 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
733 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
734 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
736 lockres->l_level = lockres->l_requested;
737 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
738 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
743 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
746 int needs_downconvert = 0;
749 assert_spin_locked(&lockres->l_lock);
751 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
753 if (level > lockres->l_blocking) {
754 /* only schedule a downconvert if we haven't already scheduled
755 * one that goes low enough to satisfy the level we're
756 * blocking. this also catches the case where we get
758 if (ocfs2_highest_compat_lock_level(level) <
759 ocfs2_highest_compat_lock_level(lockres->l_blocking))
760 needs_downconvert = 1;
762 lockres->l_blocking = level;
765 mlog_exit(needs_downconvert);
766 return needs_downconvert;
769 static void ocfs2_blocking_ast(void *opaque, int level)
771 struct ocfs2_lock_res *lockres = opaque;
772 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
773 int needs_downconvert;
776 BUG_ON(level <= DLM_LOCK_NL);
778 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
779 lockres->l_name, level, lockres->l_level,
780 ocfs2_lock_type_string(lockres->l_type));
783 * We can skip the bast for locks which don't enable caching -
784 * they'll be dropped at the earliest possible time anyway.
786 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
789 spin_lock_irqsave(&lockres->l_lock, flags);
790 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
791 if (needs_downconvert)
792 ocfs2_schedule_blocked_lock(osb, lockres);
793 spin_unlock_irqrestore(&lockres->l_lock, flags);
795 wake_up(&lockres->l_event);
797 ocfs2_wake_downconvert_thread(osb);
800 static void ocfs2_locking_ast(void *opaque)
802 struct ocfs2_lock_res *lockres = opaque;
803 struct dlm_lockstatus *lksb = &lockres->l_lksb;
806 spin_lock_irqsave(&lockres->l_lock, flags);
808 if (lksb->status != DLM_NORMAL) {
809 mlog(ML_ERROR, "lockres %s: lksb status value of %u!\n",
810 lockres->l_name, lksb->status);
811 spin_unlock_irqrestore(&lockres->l_lock, flags);
815 switch(lockres->l_action) {
816 case OCFS2_AST_ATTACH:
817 ocfs2_generic_handle_attach_action(lockres);
818 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
820 case OCFS2_AST_CONVERT:
821 ocfs2_generic_handle_convert_action(lockres);
823 case OCFS2_AST_DOWNCONVERT:
824 ocfs2_generic_handle_downconvert_action(lockres);
827 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
828 "lockres flags = 0x%lx, unlock action: %u\n",
829 lockres->l_name, lockres->l_action, lockres->l_flags,
830 lockres->l_unlock_action);
834 /* set it to something invalid so if we get called again we
836 lockres->l_action = OCFS2_AST_INVALID;
838 wake_up(&lockres->l_event);
839 spin_unlock_irqrestore(&lockres->l_lock, flags);
842 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
848 spin_lock_irqsave(&lockres->l_lock, flags);
849 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
851 lockres->l_action = OCFS2_AST_INVALID;
853 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
854 spin_unlock_irqrestore(&lockres->l_lock, flags);
856 wake_up(&lockres->l_event);
860 /* Note: If we detect another process working on the lock (i.e.,
861 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
862 * to do the right thing in that case.
864 static int ocfs2_lock_create(struct ocfs2_super *osb,
865 struct ocfs2_lock_res *lockres,
870 enum dlm_status status = DLM_NORMAL;
875 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
878 spin_lock_irqsave(&lockres->l_lock, flags);
879 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
880 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
881 spin_unlock_irqrestore(&lockres->l_lock, flags);
885 lockres->l_action = OCFS2_AST_ATTACH;
886 lockres->l_requested = level;
887 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
888 spin_unlock_irqrestore(&lockres->l_lock, flags);
890 status = ocfs2_dlm_lock(osb->dlm,
895 OCFS2_LOCK_ID_MAX_LEN - 1,
897 if (status != DLM_NORMAL) {
898 ocfs2_log_dlm_error("ocfs2_dlm_lock", status, lockres);
900 ocfs2_recover_from_dlm_error(lockres, 1);
903 mlog(0, "lock %s, successfull return from ocfs2_dlm_lock\n",
911 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
917 spin_lock_irqsave(&lockres->l_lock, flags);
918 ret = lockres->l_flags & flag;
919 spin_unlock_irqrestore(&lockres->l_lock, flags);
924 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
927 wait_event(lockres->l_event,
928 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
931 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
934 wait_event(lockres->l_event,
935 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
938 /* predict what lock level we'll be dropping down to on behalf
939 * of another node, and return true if the currently wanted
940 * level will be compatible with it. */
941 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
944 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
946 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
949 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
951 INIT_LIST_HEAD(&mw->mw_item);
952 init_completion(&mw->mw_complete);
955 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
957 wait_for_completion(&mw->mw_complete);
958 /* Re-arm the completion in case we want to wait on it again */
959 INIT_COMPLETION(mw->mw_complete);
960 return mw->mw_status;
963 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
964 struct ocfs2_mask_waiter *mw,
968 BUG_ON(!list_empty(&mw->mw_item));
970 assert_spin_locked(&lockres->l_lock);
972 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
977 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
978 * if the mask still hadn't reached its goal */
979 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
980 struct ocfs2_mask_waiter *mw)
985 spin_lock_irqsave(&lockres->l_lock, flags);
986 if (!list_empty(&mw->mw_item)) {
987 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
990 list_del_init(&mw->mw_item);
991 init_completion(&mw->mw_complete);
993 spin_unlock_irqrestore(&lockres->l_lock, flags);
999 static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
1000 struct ocfs2_lock_res *lockres)
1004 ret = wait_for_completion_interruptible(&mw->mw_complete);
1006 lockres_remove_mask_waiter(lockres, mw);
1008 ret = mw->mw_status;
1009 /* Re-arm the completion in case we want to wait on it again */
1010 INIT_COMPLETION(mw->mw_complete);
1014 static int ocfs2_cluster_lock(struct ocfs2_super *osb,
1015 struct ocfs2_lock_res *lockres,
1020 struct ocfs2_mask_waiter mw;
1021 enum dlm_status status;
1022 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
1023 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1024 unsigned long flags;
1028 ocfs2_init_mask_waiter(&mw);
1030 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
1031 lkm_flags |= DLM_LKF_VALBLK;
1036 if (catch_signals && signal_pending(current)) {
1041 spin_lock_irqsave(&lockres->l_lock, flags);
1043 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1044 "Cluster lock called on freeing lockres %s! flags "
1045 "0x%lx\n", lockres->l_name, lockres->l_flags);
1047 /* We only compare against the currently granted level
1048 * here. If the lock is blocked waiting on a downconvert,
1049 * we'll get caught below. */
1050 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1051 level > lockres->l_level) {
1052 /* is someone sitting in dlm_lock? If so, wait on
1054 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1059 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1060 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1061 /* is the lock is currently blocked on behalf of
1063 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1068 if (level > lockres->l_level) {
1069 if (lockres->l_action != OCFS2_AST_INVALID)
1070 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1071 lockres->l_name, lockres->l_action);
1073 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1074 lockres->l_action = OCFS2_AST_ATTACH;
1075 lkm_flags &= ~DLM_LKF_CONVERT;
1077 lockres->l_action = OCFS2_AST_CONVERT;
1078 lkm_flags |= DLM_LKF_CONVERT;
1081 lockres->l_requested = level;
1082 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1083 spin_unlock_irqrestore(&lockres->l_lock, flags);
1085 BUG_ON(level == DLM_LOCK_IV);
1086 BUG_ON(level == DLM_LOCK_NL);
1088 mlog(0, "lock %s, convert from %d to level = %d\n",
1089 lockres->l_name, lockres->l_level, level);
1091 /* call dlm_lock to upgrade lock now */
1092 status = ocfs2_dlm_lock(osb->dlm,
1097 OCFS2_LOCK_ID_MAX_LEN - 1,
1099 if (status != DLM_NORMAL) {
1100 if ((lkm_flags & DLM_LKF_NOQUEUE) &&
1101 (status == DLM_NOTQUEUED))
1104 ocfs2_log_dlm_error("ocfs2_dlm_lock",
1108 ocfs2_recover_from_dlm_error(lockres, 1);
1112 mlog(0, "lock %s, successfull return from ocfs2_dlm_lock\n",
1115 /* At this point we've gone inside the dlm and need to
1116 * complete our work regardless. */
1119 /* wait for busy to clear and carry on */
1123 /* Ok, if we get here then we're good to go. */
1124 ocfs2_inc_holders(lockres, level);
1128 spin_unlock_irqrestore(&lockres->l_lock, flags);
1131 * This is helping work around a lock inversion between the page lock
1132 * and dlm locks. One path holds the page lock while calling aops
1133 * which block acquiring dlm locks. The voting thread holds dlm
1134 * locks while acquiring page locks while down converting data locks.
1135 * This block is helping an aop path notice the inversion and back
1136 * off to unlock its page lock before trying the dlm lock again.
1138 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1139 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1141 if (lockres_remove_mask_waiter(lockres, &mw))
1147 ret = ocfs2_wait_for_mask(&mw);
1157 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1158 struct ocfs2_lock_res *lockres,
1161 unsigned long flags;
1164 spin_lock_irqsave(&lockres->l_lock, flags);
1165 ocfs2_dec_holders(lockres, level);
1166 ocfs2_downconvert_on_unlock(osb, lockres);
1167 spin_unlock_irqrestore(&lockres->l_lock, flags);
1171 static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1172 struct ocfs2_lock_res *lockres,
1176 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1177 unsigned long flags;
1178 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
1180 spin_lock_irqsave(&lockres->l_lock, flags);
1181 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1182 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1183 spin_unlock_irqrestore(&lockres->l_lock, flags);
1185 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
1188 /* Grants us an EX lock on the data and metadata resources, skipping
1189 * the normal cluster directory lookup. Use this ONLY on newly created
1190 * inodes which other nodes can't possibly see, and which haven't been
1191 * hashed in the inode hash yet. This can give us a good performance
1192 * increase as it'll skip the network broadcast normally associated
1193 * with creating a new lock resource. */
1194 int ocfs2_create_new_inode_locks(struct inode *inode)
1197 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1200 BUG_ON(!ocfs2_inode_is_new(inode));
1204 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1206 /* NOTE: That we don't increment any of the holder counts, nor
1207 * do we add anything to a journal handle. Since this is
1208 * supposed to be a new inode which the cluster doesn't know
1209 * about yet, there is no need to. As far as the LVB handling
1210 * is concerned, this is basically like acquiring an EX lock
1211 * on a resource which has an invalid one -- we'll set it
1212 * valid when we release the EX. */
1214 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
1221 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
1222 * don't use a generation in their lock names.
1224 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
1230 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1241 int ocfs2_rw_lock(struct inode *inode, int write)
1244 struct ocfs2_lock_res *lockres;
1245 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1251 mlog(0, "inode %llu take %s RW lock\n",
1252 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1253 write ? "EXMODE" : "PRMODE");
1255 if (ocfs2_mount_local(osb))
1258 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1260 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1262 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1271 void ocfs2_rw_unlock(struct inode *inode, int write)
1273 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1274 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1275 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1279 mlog(0, "inode %llu drop %s RW lock\n",
1280 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1281 write ? "EXMODE" : "PRMODE");
1283 if (!ocfs2_mount_local(osb))
1284 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1290 * ocfs2_open_lock always get PR mode lock.
1292 int ocfs2_open_lock(struct inode *inode)
1295 struct ocfs2_lock_res *lockres;
1296 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1302 mlog(0, "inode %llu take PRMODE open lock\n",
1303 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1305 if (ocfs2_mount_local(osb))
1308 lockres = &OCFS2_I(inode)->ip_open_lockres;
1310 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1320 int ocfs2_try_open_lock(struct inode *inode, int write)
1322 int status = 0, level;
1323 struct ocfs2_lock_res *lockres;
1324 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1330 mlog(0, "inode %llu try to take %s open lock\n",
1331 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1332 write ? "EXMODE" : "PRMODE");
1334 if (ocfs2_mount_local(osb))
1337 lockres = &OCFS2_I(inode)->ip_open_lockres;
1339 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1342 * The file system may already holding a PRMODE/EXMODE open lock.
1343 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
1344 * other nodes and the -EAGAIN will indicate to the caller that
1345 * this inode is still in use.
1347 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1348 level, DLM_LKF_NOQUEUE, 0);
1356 * ocfs2_open_unlock unlock PR and EX mode open locks.
1358 void ocfs2_open_unlock(struct inode *inode)
1360 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1361 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1365 mlog(0, "inode %llu drop open lock\n",
1366 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1368 if (ocfs2_mount_local(osb))
1371 if(lockres->l_ro_holders)
1372 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1374 if(lockres->l_ex_holders)
1375 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1382 static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1386 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1387 unsigned long flags;
1388 struct ocfs2_mask_waiter mw;
1390 ocfs2_init_mask_waiter(&mw);
1393 spin_lock_irqsave(&lockres->l_lock, flags);
1394 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1395 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1397 spin_unlock_irqrestore(&lockres->l_lock, flags);
1398 ret = ocfs2_cancel_convert(osb, lockres);
1405 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1406 spin_unlock_irqrestore(&lockres->l_lock, flags);
1408 ocfs2_wait_for_mask(&mw);
1414 * We may still have gotten the lock, in which case there's no
1415 * point to restarting the syscall.
1417 if (lockres->l_level == level)
1420 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1421 lockres->l_flags, lockres->l_level, lockres->l_action);
1423 spin_unlock_irqrestore(&lockres->l_lock, flags);
1430 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1431 * flock() calls. The locking approach this requires is sufficiently
1432 * different from all other cluster lock types that we implement a
1433 * seperate path to the "low-level" dlm calls. In particular:
1435 * - No optimization of lock levels is done - we take at exactly
1436 * what's been requested.
1438 * - No lock caching is employed. We immediately downconvert to
1439 * no-lock at unlock time. This also means flock locks never go on
1440 * the blocking list).
1442 * - Since userspace can trivially deadlock itself with flock, we make
1443 * sure to allow cancellation of a misbehaving applications flock()
1446 * - Access to any flock lockres doesn't require concurrency, so we
1447 * can simplify the code by requiring the caller to guarantee
1448 * serialization of dlmglue flock calls.
1450 int ocfs2_file_lock(struct file *file, int ex, int trylock)
1452 int ret, level = ex ? LKM_EXMODE : LKM_PRMODE;
1453 unsigned int lkm_flags = trylock ? LKM_NOQUEUE : 0;
1454 unsigned long flags;
1455 struct ocfs2_file_private *fp = file->private_data;
1456 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1457 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1458 struct ocfs2_mask_waiter mw;
1460 ocfs2_init_mask_waiter(&mw);
1462 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
1463 (lockres->l_level > DLM_LOCK_NL)) {
1465 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1466 "level: %u\n", lockres->l_name, lockres->l_flags,
1471 spin_lock_irqsave(&lockres->l_lock, flags);
1472 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1473 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1474 spin_unlock_irqrestore(&lockres->l_lock, flags);
1477 * Get the lock at NLMODE to start - that way we
1478 * can cancel the upconvert request if need be.
1480 ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
1486 ret = ocfs2_wait_for_mask(&mw);
1491 spin_lock_irqsave(&lockres->l_lock, flags);
1494 lockres->l_action = OCFS2_AST_CONVERT;
1495 lkm_flags |= LKM_CONVERT;
1496 lockres->l_requested = level;
1497 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1499 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1500 spin_unlock_irqrestore(&lockres->l_lock, flags);
1502 ret = ocfs2_dlm_lock(osb->dlm, level, &lockres->l_lksb, lkm_flags,
1503 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1,
1505 if (ret != DLM_NORMAL) {
1506 if (trylock && ret == DLM_NOTQUEUED)
1509 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
1513 ocfs2_recover_from_dlm_error(lockres, 1);
1514 lockres_remove_mask_waiter(lockres, &mw);
1518 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1519 if (ret == -ERESTARTSYS) {
1521 * Userspace can cause deadlock itself with
1522 * flock(). Current behavior locally is to allow the
1523 * deadlock, but abort the system call if a signal is
1524 * received. We follow this example, otherwise a
1525 * poorly written program could sit in kernel until
1528 * Handling this is a bit more complicated for Ocfs2
1529 * though. We can't exit this function with an
1530 * outstanding lock request, so a cancel convert is
1531 * required. We intentionally overwrite 'ret' - if the
1532 * cancel fails and the lock was granted, it's easier
1533 * to just bubble sucess back up to the user.
1535 ret = ocfs2_flock_handle_signal(lockres, level);
1540 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1541 lockres->l_name, ex, trylock, ret);
1545 void ocfs2_file_unlock(struct file *file)
1548 unsigned long flags;
1549 struct ocfs2_file_private *fp = file->private_data;
1550 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1551 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1552 struct ocfs2_mask_waiter mw;
1554 ocfs2_init_mask_waiter(&mw);
1556 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
1559 if (lockres->l_level == LKM_NLMODE)
1562 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1563 lockres->l_name, lockres->l_flags, lockres->l_level,
1566 spin_lock_irqsave(&lockres->l_lock, flags);
1568 * Fake a blocking ast for the downconvert code.
1570 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
1571 lockres->l_blocking = DLM_LOCK_EX;
1573 ocfs2_prepare_downconvert(lockres, LKM_NLMODE);
1574 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1575 spin_unlock_irqrestore(&lockres->l_lock, flags);
1577 ret = ocfs2_downconvert_lock(osb, lockres, LKM_NLMODE, 0);
1583 ret = ocfs2_wait_for_mask(&mw);
1588 static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1589 struct ocfs2_lock_res *lockres)
1595 /* If we know that another node is waiting on our lock, kick
1596 * the downconvert thread * pre-emptively when we reach a release
1598 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1599 switch(lockres->l_blocking) {
1601 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1605 if (!lockres->l_ex_holders)
1614 ocfs2_wake_downconvert_thread(osb);
1619 #define OCFS2_SEC_BITS 34
1620 #define OCFS2_SEC_SHIFT (64 - 34)
1621 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1623 /* LVB only has room for 64 bits of time here so we pack it for
1625 static u64 ocfs2_pack_timespec(struct timespec *spec)
1628 u64 sec = spec->tv_sec;
1629 u32 nsec = spec->tv_nsec;
1631 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1636 /* Call this with the lockres locked. I am reasonably sure we don't
1637 * need ip_lock in this function as anyone who would be changing those
1638 * values is supposed to be blocked in ocfs2_inode_lock right now. */
1639 static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1641 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1642 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1643 struct ocfs2_meta_lvb *lvb;
1647 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1650 * Invalidate the LVB of a deleted inode - this way other
1651 * nodes are forced to go to disk and discover the new inode
1654 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1655 lvb->lvb_version = 0;
1659 lvb->lvb_version = OCFS2_LVB_VERSION;
1660 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1661 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1662 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1663 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1664 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1665 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1666 lvb->lvb_iatime_packed =
1667 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1668 lvb->lvb_ictime_packed =
1669 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1670 lvb->lvb_imtime_packed =
1671 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1672 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
1673 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
1674 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
1677 mlog_meta_lvb(0, lockres);
1682 static void ocfs2_unpack_timespec(struct timespec *spec,
1685 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1686 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1689 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1691 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1692 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1693 struct ocfs2_meta_lvb *lvb;
1697 mlog_meta_lvb(0, lockres);
1699 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1701 /* We're safe here without the lockres lock... */
1702 spin_lock(&oi->ip_lock);
1703 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1704 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1706 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1707 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
1708 ocfs2_set_inode_flags(inode);
1710 /* fast-symlinks are a special case */
1711 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1712 inode->i_blocks = 0;
1714 inode->i_blocks = ocfs2_inode_sector_count(inode);
1716 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1717 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1718 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1719 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1720 ocfs2_unpack_timespec(&inode->i_atime,
1721 be64_to_cpu(lvb->lvb_iatime_packed));
1722 ocfs2_unpack_timespec(&inode->i_mtime,
1723 be64_to_cpu(lvb->lvb_imtime_packed));
1724 ocfs2_unpack_timespec(&inode->i_ctime,
1725 be64_to_cpu(lvb->lvb_ictime_packed));
1726 spin_unlock(&oi->ip_lock);
1731 static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1732 struct ocfs2_lock_res *lockres)
1734 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1736 if (lvb->lvb_version == OCFS2_LVB_VERSION
1737 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
1742 /* Determine whether a lock resource needs to be refreshed, and
1743 * arbitrate who gets to refresh it.
1745 * 0 means no refresh needed.
1747 * > 0 means you need to refresh this and you MUST call
1748 * ocfs2_complete_lock_res_refresh afterwards. */
1749 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1751 unsigned long flags;
1757 spin_lock_irqsave(&lockres->l_lock, flags);
1758 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1759 spin_unlock_irqrestore(&lockres->l_lock, flags);
1763 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1764 spin_unlock_irqrestore(&lockres->l_lock, flags);
1766 ocfs2_wait_on_refreshing_lock(lockres);
1770 /* Ok, I'll be the one to refresh this lock. */
1771 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1772 spin_unlock_irqrestore(&lockres->l_lock, flags);
1780 /* If status is non zero, I'll mark it as not being in refresh
1781 * anymroe, but i won't clear the needs refresh flag. */
1782 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1785 unsigned long flags;
1788 spin_lock_irqsave(&lockres->l_lock, flags);
1789 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1791 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1792 spin_unlock_irqrestore(&lockres->l_lock, flags);
1794 wake_up(&lockres->l_event);
1799 /* may or may not return a bh if it went to disk. */
1800 static int ocfs2_inode_lock_update(struct inode *inode,
1801 struct buffer_head **bh)
1804 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1805 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1806 struct ocfs2_dinode *fe;
1807 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1811 if (ocfs2_mount_local(osb))
1814 spin_lock(&oi->ip_lock);
1815 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1816 mlog(0, "Orphaned inode %llu was deleted while we "
1817 "were waiting on a lock. ip_flags = 0x%x\n",
1818 (unsigned long long)oi->ip_blkno, oi->ip_flags);
1819 spin_unlock(&oi->ip_lock);
1823 spin_unlock(&oi->ip_lock);
1825 if (!ocfs2_should_refresh_lock_res(lockres))
1828 /* This will discard any caching information we might have had
1829 * for the inode metadata. */
1830 ocfs2_metadata_cache_purge(inode);
1832 ocfs2_extent_map_trunc(inode, 0);
1834 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
1835 mlog(0, "Trusting LVB on inode %llu\n",
1836 (unsigned long long)oi->ip_blkno);
1837 ocfs2_refresh_inode_from_lvb(inode);
1839 /* Boo, we have to go to disk. */
1840 /* read bh, cast, ocfs2_refresh_inode */
1841 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1842 bh, OCFS2_BH_CACHED, inode);
1847 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1849 /* This is a good chance to make sure we're not
1850 * locking an invalid object.
1852 * We bug on a stale inode here because we checked
1853 * above whether it was wiped from disk. The wiping
1854 * node provides a guarantee that we receive that
1855 * message and can mark the inode before dropping any
1856 * locks associated with it. */
1857 if (!OCFS2_IS_VALID_DINODE(fe)) {
1858 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1862 mlog_bug_on_msg(inode->i_generation !=
1863 le32_to_cpu(fe->i_generation),
1864 "Invalid dinode %llu disk generation: %u "
1865 "inode->i_generation: %u\n",
1866 (unsigned long long)oi->ip_blkno,
1867 le32_to_cpu(fe->i_generation),
1868 inode->i_generation);
1869 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1870 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
1871 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1872 (unsigned long long)oi->ip_blkno,
1873 (unsigned long long)le64_to_cpu(fe->i_dtime),
1874 le32_to_cpu(fe->i_flags));
1876 ocfs2_refresh_inode(inode, fe);
1881 ocfs2_complete_lock_res_refresh(lockres, status);
1887 static int ocfs2_assign_bh(struct inode *inode,
1888 struct buffer_head **ret_bh,
1889 struct buffer_head *passed_bh)
1894 /* Ok, the update went to disk for us, use the
1896 *ret_bh = passed_bh;
1902 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1903 OCFS2_I(inode)->ip_blkno,
1914 * returns < 0 error if the callback will never be called, otherwise
1915 * the result of the lock will be communicated via the callback.
1917 int ocfs2_inode_lock_full(struct inode *inode,
1918 struct buffer_head **ret_bh,
1922 int status, level, acquired;
1924 struct ocfs2_lock_res *lockres = NULL;
1925 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1926 struct buffer_head *local_bh = NULL;
1932 mlog(0, "inode %llu, take %s META lock\n",
1933 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1934 ex ? "EXMODE" : "PRMODE");
1938 /* We'll allow faking a readonly metadata lock for
1940 if (ocfs2_is_hard_readonly(osb)) {
1946 if (ocfs2_mount_local(osb))
1949 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1950 ocfs2_wait_for_recovery(osb);
1952 lockres = &OCFS2_I(inode)->ip_inode_lockres;
1953 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1955 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
1956 dlm_flags |= DLM_LKF_NOQUEUE;
1958 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1960 if (status != -EAGAIN && status != -EIOCBRETRY)
1965 /* Notify the error cleanup path to drop the cluster lock. */
1968 /* We wait twice because a node may have died while we were in
1969 * the lower dlm layers. The second time though, we've
1970 * committed to owning this lock so we don't allow signals to
1971 * abort the operation. */
1972 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1973 ocfs2_wait_for_recovery(osb);
1977 * We only see this flag if we're being called from
1978 * ocfs2_read_locked_inode(). It means we're locking an inode
1979 * which hasn't been populated yet, so clear the refresh flag
1980 * and let the caller handle it.
1982 if (inode->i_state & I_NEW) {
1985 ocfs2_complete_lock_res_refresh(lockres, 0);
1989 /* This is fun. The caller may want a bh back, or it may
1990 * not. ocfs2_inode_lock_update definitely wants one in, but
1991 * may or may not read one, depending on what's in the
1992 * LVB. The result of all of this is that we've *only* gone to
1993 * disk if we have to, so the complexity is worthwhile. */
1994 status = ocfs2_inode_lock_update(inode, &local_bh);
1996 if (status != -ENOENT)
2002 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
2011 if (ret_bh && (*ret_bh)) {
2016 ocfs2_inode_unlock(inode, ex);
2027 * This is working around a lock inversion between tasks acquiring DLM
2028 * locks while holding a page lock and the downconvert thread which
2029 * blocks dlm lock acquiry while acquiring page locks.
2031 * ** These _with_page variantes are only intended to be called from aop
2032 * methods that hold page locks and return a very specific *positive* error
2033 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2035 * The DLM is called such that it returns -EAGAIN if it would have
2036 * blocked waiting for the downconvert thread. In that case we unlock
2037 * our page so the downconvert thread can make progress. Once we've
2038 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2039 * that called us can bubble that back up into the VFS who will then
2040 * immediately retry the aop call.
2042 * We do a blocking lock and immediate unlock before returning, though, so that
2043 * the lock has a great chance of being cached on this node by the time the VFS
2044 * calls back to retry the aop. This has a potential to livelock as nodes
2045 * ping locks back and forth, but that's a risk we're willing to take to avoid
2046 * the lock inversion simply.
2048 int ocfs2_inode_lock_with_page(struct inode *inode,
2049 struct buffer_head **ret_bh,
2055 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
2056 if (ret == -EAGAIN) {
2058 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2059 ocfs2_inode_unlock(inode, ex);
2060 ret = AOP_TRUNCATED_PAGE;
2066 int ocfs2_inode_lock_atime(struct inode *inode,
2067 struct vfsmount *vfsmnt,
2073 ret = ocfs2_inode_lock(inode, NULL, 0);
2080 * If we should update atime, we will get EX lock,
2081 * otherwise we just get PR lock.
2083 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2084 struct buffer_head *bh = NULL;
2086 ocfs2_inode_unlock(inode, 0);
2087 ret = ocfs2_inode_lock(inode, &bh, 1);
2093 if (ocfs2_should_update_atime(inode, vfsmnt))
2094 ocfs2_update_inode_atime(inode, bh);
2104 void ocfs2_inode_unlock(struct inode *inode,
2107 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2108 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
2109 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2113 mlog(0, "inode %llu drop %s META lock\n",
2114 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2115 ex ? "EXMODE" : "PRMODE");
2117 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2118 !ocfs2_mount_local(osb))
2119 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
2124 int ocfs2_super_lock(struct ocfs2_super *osb,
2128 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2129 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2133 if (ocfs2_is_hard_readonly(osb))
2136 if (ocfs2_mount_local(osb))
2139 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2145 /* The super block lock path is really in the best position to
2146 * know when resources covered by the lock need to be
2147 * refreshed, so we do it here. Of course, making sense of
2148 * everything is up to the caller :) */
2149 status = ocfs2_should_refresh_lock_res(lockres);
2155 status = ocfs2_refresh_slot_info(osb);
2157 ocfs2_complete_lock_res_refresh(lockres, status);
2167 void ocfs2_super_unlock(struct ocfs2_super *osb,
2170 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2171 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2173 if (!ocfs2_mount_local(osb))
2174 ocfs2_cluster_unlock(osb, lockres, level);
2177 int ocfs2_rename_lock(struct ocfs2_super *osb)
2180 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2182 if (ocfs2_is_hard_readonly(osb))
2185 if (ocfs2_mount_local(osb))
2188 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
2195 void ocfs2_rename_unlock(struct ocfs2_super *osb)
2197 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2199 if (!ocfs2_mount_local(osb))
2200 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2203 int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2206 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2207 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2208 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2212 if (ocfs2_is_hard_readonly(osb))
2215 if (ocfs2_mount_local(osb))
2218 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2225 void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2227 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2228 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2229 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2231 if (!ocfs2_mount_local(osb))
2232 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
2235 /* Reference counting of the dlm debug structure. We want this because
2236 * open references on the debug inodes can live on after a mount, so
2237 * we can't rely on the ocfs2_super to always exist. */
2238 static void ocfs2_dlm_debug_free(struct kref *kref)
2240 struct ocfs2_dlm_debug *dlm_debug;
2242 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2247 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2250 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2253 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2255 kref_get(&debug->d_refcnt);
2258 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2260 struct ocfs2_dlm_debug *dlm_debug;
2262 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2264 mlog_errno(-ENOMEM);
2268 kref_init(&dlm_debug->d_refcnt);
2269 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2270 dlm_debug->d_locking_state = NULL;
2275 /* Access to this is arbitrated for us via seq_file->sem. */
2276 struct ocfs2_dlm_seq_priv {
2277 struct ocfs2_dlm_debug *p_dlm_debug;
2278 struct ocfs2_lock_res p_iter_res;
2279 struct ocfs2_lock_res p_tmp_res;
2282 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2283 struct ocfs2_dlm_seq_priv *priv)
2285 struct ocfs2_lock_res *iter, *ret = NULL;
2286 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2288 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2290 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2291 /* discover the head of the list */
2292 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2293 mlog(0, "End of list found, %p\n", ret);
2297 /* We track our "dummy" iteration lockres' by a NULL
2299 if (iter->l_ops != NULL) {
2308 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2310 struct ocfs2_dlm_seq_priv *priv = m->private;
2311 struct ocfs2_lock_res *iter;
2313 spin_lock(&ocfs2_dlm_tracking_lock);
2314 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2316 /* Since lockres' have the lifetime of their container
2317 * (which can be inodes, ocfs2_supers, etc) we want to
2318 * copy this out to a temporary lockres while still
2319 * under the spinlock. Obviously after this we can't
2320 * trust any pointers on the copy returned, but that's
2321 * ok as the information we want isn't typically held
2323 priv->p_tmp_res = *iter;
2324 iter = &priv->p_tmp_res;
2326 spin_unlock(&ocfs2_dlm_tracking_lock);
2331 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2335 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2337 struct ocfs2_dlm_seq_priv *priv = m->private;
2338 struct ocfs2_lock_res *iter = v;
2339 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2341 spin_lock(&ocfs2_dlm_tracking_lock);
2342 iter = ocfs2_dlm_next_res(iter, priv);
2343 list_del_init(&dummy->l_debug_list);
2345 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2346 priv->p_tmp_res = *iter;
2347 iter = &priv->p_tmp_res;
2349 spin_unlock(&ocfs2_dlm_tracking_lock);
2354 /* So that debugfs.ocfs2 can determine which format is being used */
2355 #define OCFS2_DLM_DEBUG_STR_VERSION 1
2356 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2360 struct ocfs2_lock_res *lockres = v;
2365 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2367 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2368 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2370 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2372 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2374 seq_printf(m, "%d\t"
2385 lockres->l_unlock_action,
2386 lockres->l_ro_holders,
2387 lockres->l_ex_holders,
2388 lockres->l_requested,
2389 lockres->l_blocking);
2391 /* Dump the raw LVB */
2392 lvb = lockres->l_lksb.lvb;
2393 for(i = 0; i < DLM_LVB_LEN; i++)
2394 seq_printf(m, "0x%x\t", lvb[i]);
2397 seq_printf(m, "\n");
2401 static const struct seq_operations ocfs2_dlm_seq_ops = {
2402 .start = ocfs2_dlm_seq_start,
2403 .stop = ocfs2_dlm_seq_stop,
2404 .next = ocfs2_dlm_seq_next,
2405 .show = ocfs2_dlm_seq_show,
2408 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2410 struct seq_file *seq = (struct seq_file *) file->private_data;
2411 struct ocfs2_dlm_seq_priv *priv = seq->private;
2412 struct ocfs2_lock_res *res = &priv->p_iter_res;
2414 ocfs2_remove_lockres_tracking(res);
2415 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2416 return seq_release_private(inode, file);
2419 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2422 struct ocfs2_dlm_seq_priv *priv;
2423 struct seq_file *seq;
2424 struct ocfs2_super *osb;
2426 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2432 osb = inode->i_private;
2433 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2434 priv->p_dlm_debug = osb->osb_dlm_debug;
2435 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2437 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2444 seq = (struct seq_file *) file->private_data;
2445 seq->private = priv;
2447 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2454 static const struct file_operations ocfs2_dlm_debug_fops = {
2455 .open = ocfs2_dlm_debug_open,
2456 .release = ocfs2_dlm_debug_release,
2458 .llseek = seq_lseek,
2461 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2464 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2466 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2468 osb->osb_debug_root,
2470 &ocfs2_dlm_debug_fops);
2471 if (!dlm_debug->d_locking_state) {
2474 "Unable to create locking state debugfs file.\n");
2478 ocfs2_get_dlm_debug(dlm_debug);
2483 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2485 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2488 debugfs_remove(dlm_debug->d_locking_state);
2489 ocfs2_put_dlm_debug(dlm_debug);
2493 int ocfs2_dlm_init(struct ocfs2_super *osb)
2497 struct dlm_ctxt *dlm = NULL;
2501 if (ocfs2_mount_local(osb))
2504 status = ocfs2_dlm_init_debug(osb);
2510 /* launch downconvert thread */
2511 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2512 if (IS_ERR(osb->dc_task)) {
2513 status = PTR_ERR(osb->dc_task);
2514 osb->dc_task = NULL;
2519 /* used by the dlm code to make message headers unique, each
2520 * node in this domain must agree on this. */
2521 dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
2523 /* for now, uuid == domain */
2524 dlm = dlm_register_domain(osb->uuid_str, dlm_key,
2525 &osb->osb_locking_proto);
2527 status = PTR_ERR(dlm);
2532 dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
2535 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2536 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2543 ocfs2_dlm_shutdown_debug(osb);
2545 kthread_stop(osb->dc_task);
2552 void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2556 dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
2558 ocfs2_drop_osb_locks(osb);
2561 kthread_stop(osb->dc_task);
2562 osb->dc_task = NULL;
2565 ocfs2_lock_res_free(&osb->osb_super_lockres);
2566 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2568 dlm_unregister_domain(osb->dlm);
2571 ocfs2_dlm_shutdown_debug(osb);
2576 static void ocfs2_unlock_ast(void *opaque, enum dlm_status status)
2578 struct ocfs2_lock_res *lockres = opaque;
2579 unsigned long flags;
2583 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2584 lockres->l_unlock_action);
2586 spin_lock_irqsave(&lockres->l_lock, flags);
2587 /* We tried to cancel a convert request, but it was already
2588 * granted. All we want to do here is clear our unlock
2589 * state. The wake_up call done at the bottom is redundant
2590 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2591 * hurt anything anyway */
2592 if (status == DLM_CANCELGRANT &&
2593 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2594 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2596 /* We don't clear the busy flag in this case as it
2597 * should have been cleared by the ast which the dlm
2599 goto complete_unlock;
2602 if (status != DLM_NORMAL) {
2603 mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
2604 "unlock_action %d\n", status, lockres->l_name,
2605 lockres->l_unlock_action);
2606 spin_unlock_irqrestore(&lockres->l_lock, flags);
2610 switch(lockres->l_unlock_action) {
2611 case OCFS2_UNLOCK_CANCEL_CONVERT:
2612 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2613 lockres->l_action = OCFS2_AST_INVALID;
2615 case OCFS2_UNLOCK_DROP_LOCK:
2616 lockres->l_level = DLM_LOCK_IV;
2622 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2624 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2625 spin_unlock_irqrestore(&lockres->l_lock, flags);
2627 wake_up(&lockres->l_event);
2632 static int ocfs2_drop_lock(struct ocfs2_super *osb,
2633 struct ocfs2_lock_res *lockres)
2635 enum dlm_status status;
2636 unsigned long flags;
2639 /* We didn't get anywhere near actually using this lockres. */
2640 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2643 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
2644 lkm_flags |= DLM_LKF_VALBLK;
2646 spin_lock_irqsave(&lockres->l_lock, flags);
2648 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2649 "lockres %s, flags 0x%lx\n",
2650 lockres->l_name, lockres->l_flags);
2652 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2653 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2654 "%u, unlock_action = %u\n",
2655 lockres->l_name, lockres->l_flags, lockres->l_action,
2656 lockres->l_unlock_action);
2658 spin_unlock_irqrestore(&lockres->l_lock, flags);
2660 /* XXX: Today we just wait on any busy
2661 * locks... Perhaps we need to cancel converts in the
2663 ocfs2_wait_on_busy_lock(lockres);
2665 spin_lock_irqsave(&lockres->l_lock, flags);
2668 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2669 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2670 lockres->l_level == DLM_LOCK_EX &&
2671 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2672 lockres->l_ops->set_lvb(lockres);
2675 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2676 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2678 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2679 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2681 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2682 spin_unlock_irqrestore(&lockres->l_lock, flags);
2686 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2688 /* make sure we never get here while waiting for an ast to
2690 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2692 /* is this necessary? */
2693 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2694 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2695 spin_unlock_irqrestore(&lockres->l_lock, flags);
2697 mlog(0, "lock %s\n", lockres->l_name);
2699 status = ocfs2_dlm_unlock(osb->dlm, &lockres->l_lksb, lkm_flags,
2701 if (status != DLM_NORMAL) {
2702 ocfs2_log_dlm_error("ocfs2_dlm_unlock", status, lockres);
2703 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2704 dlm_print_one_lock(lockres->l_lksb.lockid);
2707 mlog(0, "lock %s, successfull return from ocfs2_dlm_unlock\n",
2710 ocfs2_wait_on_busy_lock(lockres);
2716 /* Mark the lockres as being dropped. It will no longer be
2717 * queued if blocking, but we still may have to wait on it
2718 * being dequeued from the downconvert thread before we can consider
2721 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2722 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2725 struct ocfs2_mask_waiter mw;
2726 unsigned long flags;
2728 ocfs2_init_mask_waiter(&mw);
2730 spin_lock_irqsave(&lockres->l_lock, flags);
2731 lockres->l_flags |= OCFS2_LOCK_FREEING;
2732 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2733 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2734 spin_unlock_irqrestore(&lockres->l_lock, flags);
2736 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2738 status = ocfs2_wait_for_mask(&mw);
2742 spin_lock_irqsave(&lockres->l_lock, flags);
2744 spin_unlock_irqrestore(&lockres->l_lock, flags);
2747 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2748 struct ocfs2_lock_res *lockres)
2752 ocfs2_mark_lockres_freeing(lockres);
2753 ret = ocfs2_drop_lock(osb, lockres);
2758 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2760 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2761 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
2764 int ocfs2_drop_inode_locks(struct inode *inode)
2770 /* No need to call ocfs2_mark_lockres_freeing here -
2771 * ocfs2_clear_inode has done it for us. */
2773 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2774 &OCFS2_I(inode)->ip_open_lockres);
2780 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2781 &OCFS2_I(inode)->ip_inode_lockres);
2784 if (err < 0 && !status)
2787 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2788 &OCFS2_I(inode)->ip_rw_lockres);
2791 if (err < 0 && !status)
2798 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2801 assert_spin_locked(&lockres->l_lock);
2803 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
2805 if (lockres->l_level <= new_level) {
2806 mlog(ML_ERROR, "lockres->l_level (%d) <= new_level (%d)\n",
2807 lockres->l_level, new_level);
2811 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2812 lockres->l_name, new_level, lockres->l_blocking);
2814 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2815 lockres->l_requested = new_level;
2816 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2819 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2820 struct ocfs2_lock_res *lockres,
2825 u32 dlm_flags = DLM_LKF_CONVERT;
2826 enum dlm_status status;
2831 dlm_flags |= DLM_LKF_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 == DLM_LOCK_EX)
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 == DLM_LOCK_PR &&
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 == DLM_LOCK_EX)
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 == DLM_LOCK_EX) {
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 != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3071 BUG_ON(lockres->l_level != DLM_LOCK_EX && !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 == DLM_LOCK_PR)
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);