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, _err, _lockres) do { \
333 mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \
334 _err, _func, _lockres->l_name); \
336 static int ocfs2_downconvert_thread(void *arg);
337 static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
338 struct ocfs2_lock_res *lockres);
339 static int ocfs2_inode_lock_update(struct inode *inode,
340 struct buffer_head **bh);
341 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
342 static inline int ocfs2_highest_compat_lock_level(int level);
343 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
345 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
346 struct ocfs2_lock_res *lockres,
349 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
350 struct ocfs2_lock_res *lockres);
351 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
352 struct ocfs2_lock_res *lockres);
355 static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
364 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
366 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
367 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
368 (long long)blkno, generation);
370 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
372 mlog(0, "built lock resource with name: %s\n", name);
377 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
379 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
380 struct ocfs2_dlm_debug *dlm_debug)
382 mlog(0, "Add tracking for lockres %s\n", res->l_name);
384 spin_lock(&ocfs2_dlm_tracking_lock);
385 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
386 spin_unlock(&ocfs2_dlm_tracking_lock);
389 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
391 spin_lock(&ocfs2_dlm_tracking_lock);
392 if (!list_empty(&res->l_debug_list))
393 list_del_init(&res->l_debug_list);
394 spin_unlock(&ocfs2_dlm_tracking_lock);
397 static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
398 struct ocfs2_lock_res *res,
399 enum ocfs2_lock_type type,
400 struct ocfs2_lock_res_ops *ops,
407 res->l_level = DLM_LOCK_IV;
408 res->l_requested = DLM_LOCK_IV;
409 res->l_blocking = DLM_LOCK_IV;
410 res->l_action = OCFS2_AST_INVALID;
411 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
413 res->l_flags = OCFS2_LOCK_INITIALIZED;
415 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
418 void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
420 /* This also clears out the lock status block */
421 memset(res, 0, sizeof(struct ocfs2_lock_res));
422 spin_lock_init(&res->l_lock);
423 init_waitqueue_head(&res->l_event);
424 INIT_LIST_HEAD(&res->l_blocked_list);
425 INIT_LIST_HEAD(&res->l_mask_waiters);
428 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
429 enum ocfs2_lock_type type,
430 unsigned int generation,
433 struct ocfs2_lock_res_ops *ops;
436 case OCFS2_LOCK_TYPE_RW:
437 ops = &ocfs2_inode_rw_lops;
439 case OCFS2_LOCK_TYPE_META:
440 ops = &ocfs2_inode_inode_lops;
442 case OCFS2_LOCK_TYPE_OPEN:
443 ops = &ocfs2_inode_open_lops;
446 mlog_bug_on_msg(1, "type: %d\n", type);
447 ops = NULL; /* thanks, gcc */
451 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
452 generation, res->l_name);
453 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
456 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
458 struct inode *inode = ocfs2_lock_res_inode(lockres);
460 return OCFS2_SB(inode->i_sb);
463 static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
465 struct ocfs2_file_private *fp = lockres->l_priv;
467 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
470 static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
472 __be64 inode_blkno_be;
474 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
477 return be64_to_cpu(inode_blkno_be);
480 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
482 struct ocfs2_dentry_lock *dl = lockres->l_priv;
484 return OCFS2_SB(dl->dl_inode->i_sb);
487 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
488 u64 parent, struct inode *inode)
491 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
492 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
493 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
495 ocfs2_lock_res_init_once(lockres);
498 * Unfortunately, the standard lock naming scheme won't work
499 * here because we have two 16 byte values to use. Instead,
500 * we'll stuff the inode number as a binary value. We still
501 * want error prints to show something without garbling the
502 * display, so drop a null byte in there before the inode
503 * number. A future version of OCFS2 will likely use all
504 * binary lock names. The stringified names have been a
505 * tremendous aid in debugging, but now that the debugfs
506 * interface exists, we can mangle things there if need be.
508 * NOTE: We also drop the standard "pad" value (the total lock
509 * name size stays the same though - the last part is all
510 * zeros due to the memset in ocfs2_lock_res_init_once()
512 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
514 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
517 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
519 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
522 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
523 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
527 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
528 struct ocfs2_super *osb)
530 /* Superblock lockres doesn't come from a slab so we call init
531 * once on it manually. */
532 ocfs2_lock_res_init_once(res);
533 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
535 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
536 &ocfs2_super_lops, osb);
539 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
540 struct ocfs2_super *osb)
542 /* Rename lockres doesn't come from a slab so we call init
543 * once on it manually. */
544 ocfs2_lock_res_init_once(res);
545 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
546 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
547 &ocfs2_rename_lops, osb);
550 void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
551 struct ocfs2_file_private *fp)
553 struct inode *inode = fp->fp_file->f_mapping->host;
554 struct ocfs2_inode_info *oi = OCFS2_I(inode);
556 ocfs2_lock_res_init_once(lockres);
557 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
558 inode->i_generation, lockres->l_name);
559 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
560 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
562 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
565 void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
569 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
572 ocfs2_remove_lockres_tracking(res);
574 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
575 "Lockres %s is on the blocked list\n",
577 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
578 "Lockres %s has mask waiters pending\n",
580 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
581 "Lockres %s is locked\n",
583 mlog_bug_on_msg(res->l_ro_holders,
584 "Lockres %s has %u ro holders\n",
585 res->l_name, res->l_ro_holders);
586 mlog_bug_on_msg(res->l_ex_holders,
587 "Lockres %s has %u ex holders\n",
588 res->l_name, res->l_ex_holders);
590 /* Need to clear out the lock status block for the dlm */
591 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
597 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
606 lockres->l_ex_holders++;
609 lockres->l_ro_holders++;
618 static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
627 BUG_ON(!lockres->l_ex_holders);
628 lockres->l_ex_holders--;
631 BUG_ON(!lockres->l_ro_holders);
632 lockres->l_ro_holders--;
640 /* WARNING: This function lives in a world where the only three lock
641 * levels are EX, PR, and NL. It *will* have to be adjusted when more
642 * lock types are added. */
643 static inline int ocfs2_highest_compat_lock_level(int level)
645 int new_level = DLM_LOCK_EX;
647 if (level == DLM_LOCK_EX)
648 new_level = DLM_LOCK_NL;
649 else if (level == DLM_LOCK_PR)
650 new_level = DLM_LOCK_PR;
654 static void lockres_set_flags(struct ocfs2_lock_res *lockres,
655 unsigned long newflags)
657 struct ocfs2_mask_waiter *mw, *tmp;
659 assert_spin_locked(&lockres->l_lock);
661 lockres->l_flags = newflags;
663 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
664 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
667 list_del_init(&mw->mw_item);
669 complete(&mw->mw_complete);
672 static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
674 lockres_set_flags(lockres, lockres->l_flags | or);
676 static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
679 lockres_set_flags(lockres, lockres->l_flags & ~clear);
682 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
686 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
687 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
688 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
689 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
691 lockres->l_level = lockres->l_requested;
692 if (lockres->l_level <=
693 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
694 lockres->l_blocking = DLM_LOCK_NL;
695 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
697 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
702 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
706 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
707 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
709 /* Convert from RO to EX doesn't really need anything as our
710 * information is already up to data. Convert from NL to
711 * *anything* however should mark ourselves as needing an
713 if (lockres->l_level == DLM_LOCK_NL &&
714 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
715 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
717 lockres->l_level = lockres->l_requested;
718 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
723 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
727 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
728 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
730 if (lockres->l_requested > DLM_LOCK_NL &&
731 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
732 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
733 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
735 lockres->l_level = lockres->l_requested;
736 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
737 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
742 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
745 int needs_downconvert = 0;
748 assert_spin_locked(&lockres->l_lock);
750 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
752 if (level > lockres->l_blocking) {
753 /* only schedule a downconvert if we haven't already scheduled
754 * one that goes low enough to satisfy the level we're
755 * blocking. this also catches the case where we get
757 if (ocfs2_highest_compat_lock_level(level) <
758 ocfs2_highest_compat_lock_level(lockres->l_blocking))
759 needs_downconvert = 1;
761 lockres->l_blocking = level;
764 mlog_exit(needs_downconvert);
765 return needs_downconvert;
768 static void ocfs2_blocking_ast(void *opaque, int level)
770 struct ocfs2_lock_res *lockres = opaque;
771 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
772 int needs_downconvert;
775 BUG_ON(level <= DLM_LOCK_NL);
777 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
778 lockres->l_name, level, lockres->l_level,
779 ocfs2_lock_type_string(lockres->l_type));
782 * We can skip the bast for locks which don't enable caching -
783 * they'll be dropped at the earliest possible time anyway.
785 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
788 spin_lock_irqsave(&lockres->l_lock, flags);
789 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
790 if (needs_downconvert)
791 ocfs2_schedule_blocked_lock(osb, lockres);
792 spin_unlock_irqrestore(&lockres->l_lock, flags);
794 wake_up(&lockres->l_event);
796 ocfs2_wake_downconvert_thread(osb);
799 static void ocfs2_locking_ast(void *opaque)
801 struct ocfs2_lock_res *lockres = opaque;
802 struct dlm_lockstatus *lksb = &lockres->l_lksb;
805 spin_lock_irqsave(&lockres->l_lock, flags);
807 if (lksb->status != DLM_NORMAL) {
808 mlog(ML_ERROR, "lockres %s: lksb status value of %u!\n",
809 lockres->l_name, lksb->status);
810 spin_unlock_irqrestore(&lockres->l_lock, flags);
814 switch(lockres->l_action) {
815 case OCFS2_AST_ATTACH:
816 ocfs2_generic_handle_attach_action(lockres);
817 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
819 case OCFS2_AST_CONVERT:
820 ocfs2_generic_handle_convert_action(lockres);
822 case OCFS2_AST_DOWNCONVERT:
823 ocfs2_generic_handle_downconvert_action(lockres);
826 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
827 "lockres flags = 0x%lx, unlock action: %u\n",
828 lockres->l_name, lockres->l_action, lockres->l_flags,
829 lockres->l_unlock_action);
833 /* set it to something invalid so if we get called again we
835 lockres->l_action = OCFS2_AST_INVALID;
837 wake_up(&lockres->l_event);
838 spin_unlock_irqrestore(&lockres->l_lock, flags);
841 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
847 spin_lock_irqsave(&lockres->l_lock, flags);
848 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
850 lockres->l_action = OCFS2_AST_INVALID;
852 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
853 spin_unlock_irqrestore(&lockres->l_lock, flags);
855 wake_up(&lockres->l_event);
859 /* Note: If we detect another process working on the lock (i.e.,
860 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
861 * to do the right thing in that case.
863 static int ocfs2_lock_create(struct ocfs2_super *osb,
864 struct ocfs2_lock_res *lockres,
873 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
876 spin_lock_irqsave(&lockres->l_lock, flags);
877 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
878 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
879 spin_unlock_irqrestore(&lockres->l_lock, flags);
883 lockres->l_action = OCFS2_AST_ATTACH;
884 lockres->l_requested = level;
885 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
886 spin_unlock_irqrestore(&lockres->l_lock, flags);
888 ret = ocfs2_dlm_lock(osb->dlm,
893 OCFS2_LOCK_ID_MAX_LEN - 1,
896 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
897 ocfs2_recover_from_dlm_error(lockres, 1);
900 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
907 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
913 spin_lock_irqsave(&lockres->l_lock, flags);
914 ret = lockres->l_flags & flag;
915 spin_unlock_irqrestore(&lockres->l_lock, flags);
920 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
923 wait_event(lockres->l_event,
924 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
927 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
930 wait_event(lockres->l_event,
931 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
934 /* predict what lock level we'll be dropping down to on behalf
935 * of another node, and return true if the currently wanted
936 * level will be compatible with it. */
937 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
940 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
942 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
945 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
947 INIT_LIST_HEAD(&mw->mw_item);
948 init_completion(&mw->mw_complete);
951 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
953 wait_for_completion(&mw->mw_complete);
954 /* Re-arm the completion in case we want to wait on it again */
955 INIT_COMPLETION(mw->mw_complete);
956 return mw->mw_status;
959 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
960 struct ocfs2_mask_waiter *mw,
964 BUG_ON(!list_empty(&mw->mw_item));
966 assert_spin_locked(&lockres->l_lock);
968 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
973 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
974 * if the mask still hadn't reached its goal */
975 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
976 struct ocfs2_mask_waiter *mw)
981 spin_lock_irqsave(&lockres->l_lock, flags);
982 if (!list_empty(&mw->mw_item)) {
983 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
986 list_del_init(&mw->mw_item);
987 init_completion(&mw->mw_complete);
989 spin_unlock_irqrestore(&lockres->l_lock, flags);
995 static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
996 struct ocfs2_lock_res *lockres)
1000 ret = wait_for_completion_interruptible(&mw->mw_complete);
1002 lockres_remove_mask_waiter(lockres, mw);
1004 ret = mw->mw_status;
1005 /* Re-arm the completion in case we want to wait on it again */
1006 INIT_COMPLETION(mw->mw_complete);
1010 static int ocfs2_cluster_lock(struct ocfs2_super *osb,
1011 struct ocfs2_lock_res *lockres,
1016 struct ocfs2_mask_waiter mw;
1017 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
1018 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1019 unsigned long flags;
1023 ocfs2_init_mask_waiter(&mw);
1025 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
1026 lkm_flags |= DLM_LKF_VALBLK;
1031 if (catch_signals && signal_pending(current)) {
1036 spin_lock_irqsave(&lockres->l_lock, flags);
1038 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1039 "Cluster lock called on freeing lockres %s! flags "
1040 "0x%lx\n", lockres->l_name, lockres->l_flags);
1042 /* We only compare against the currently granted level
1043 * here. If the lock is blocked waiting on a downconvert,
1044 * we'll get caught below. */
1045 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1046 level > lockres->l_level) {
1047 /* is someone sitting in dlm_lock? If so, wait on
1049 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1054 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1055 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1056 /* is the lock is currently blocked on behalf of
1058 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1063 if (level > lockres->l_level) {
1064 if (lockres->l_action != OCFS2_AST_INVALID)
1065 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1066 lockres->l_name, lockres->l_action);
1068 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1069 lockres->l_action = OCFS2_AST_ATTACH;
1070 lkm_flags &= ~DLM_LKF_CONVERT;
1072 lockres->l_action = OCFS2_AST_CONVERT;
1073 lkm_flags |= DLM_LKF_CONVERT;
1076 lockres->l_requested = level;
1077 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1078 spin_unlock_irqrestore(&lockres->l_lock, flags);
1080 BUG_ON(level == DLM_LOCK_IV);
1081 BUG_ON(level == DLM_LOCK_NL);
1083 mlog(0, "lock %s, convert from %d to level = %d\n",
1084 lockres->l_name, lockres->l_level, level);
1086 /* call dlm_lock to upgrade lock now */
1087 ret = ocfs2_dlm_lock(osb->dlm,
1092 OCFS2_LOCK_ID_MAX_LEN - 1,
1095 if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
1097 ocfs2_log_dlm_error("ocfs2_dlm_lock",
1100 ocfs2_recover_from_dlm_error(lockres, 1);
1104 mlog(0, "lock %s, successfull return from ocfs2_dlm_lock\n",
1107 /* At this point we've gone inside the dlm and need to
1108 * complete our work regardless. */
1111 /* wait for busy to clear and carry on */
1115 /* Ok, if we get here then we're good to go. */
1116 ocfs2_inc_holders(lockres, level);
1120 spin_unlock_irqrestore(&lockres->l_lock, flags);
1123 * This is helping work around a lock inversion between the page lock
1124 * and dlm locks. One path holds the page lock while calling aops
1125 * which block acquiring dlm locks. The voting thread holds dlm
1126 * locks while acquiring page locks while down converting data locks.
1127 * This block is helping an aop path notice the inversion and back
1128 * off to unlock its page lock before trying the dlm lock again.
1130 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1131 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1133 if (lockres_remove_mask_waiter(lockres, &mw))
1139 ret = ocfs2_wait_for_mask(&mw);
1149 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1150 struct ocfs2_lock_res *lockres,
1153 unsigned long flags;
1156 spin_lock_irqsave(&lockres->l_lock, flags);
1157 ocfs2_dec_holders(lockres, level);
1158 ocfs2_downconvert_on_unlock(osb, lockres);
1159 spin_unlock_irqrestore(&lockres->l_lock, flags);
1163 static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1164 struct ocfs2_lock_res *lockres,
1168 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1169 unsigned long flags;
1170 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
1172 spin_lock_irqsave(&lockres->l_lock, flags);
1173 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1174 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1175 spin_unlock_irqrestore(&lockres->l_lock, flags);
1177 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
1180 /* Grants us an EX lock on the data and metadata resources, skipping
1181 * the normal cluster directory lookup. Use this ONLY on newly created
1182 * inodes which other nodes can't possibly see, and which haven't been
1183 * hashed in the inode hash yet. This can give us a good performance
1184 * increase as it'll skip the network broadcast normally associated
1185 * with creating a new lock resource. */
1186 int ocfs2_create_new_inode_locks(struct inode *inode)
1189 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1192 BUG_ON(!ocfs2_inode_is_new(inode));
1196 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1198 /* NOTE: That we don't increment any of the holder counts, nor
1199 * do we add anything to a journal handle. Since this is
1200 * supposed to be a new inode which the cluster doesn't know
1201 * about yet, there is no need to. As far as the LVB handling
1202 * is concerned, this is basically like acquiring an EX lock
1203 * on a resource which has an invalid one -- we'll set it
1204 * valid when we release the EX. */
1206 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
1213 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
1214 * don't use a generation in their lock names.
1216 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
1222 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1233 int ocfs2_rw_lock(struct inode *inode, int write)
1236 struct ocfs2_lock_res *lockres;
1237 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1243 mlog(0, "inode %llu take %s RW lock\n",
1244 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1245 write ? "EXMODE" : "PRMODE");
1247 if (ocfs2_mount_local(osb))
1250 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1252 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1254 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1263 void ocfs2_rw_unlock(struct inode *inode, int write)
1265 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1266 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1267 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1271 mlog(0, "inode %llu drop %s RW lock\n",
1272 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1273 write ? "EXMODE" : "PRMODE");
1275 if (!ocfs2_mount_local(osb))
1276 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1282 * ocfs2_open_lock always get PR mode lock.
1284 int ocfs2_open_lock(struct inode *inode)
1287 struct ocfs2_lock_res *lockres;
1288 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1294 mlog(0, "inode %llu take PRMODE open lock\n",
1295 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1297 if (ocfs2_mount_local(osb))
1300 lockres = &OCFS2_I(inode)->ip_open_lockres;
1302 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1312 int ocfs2_try_open_lock(struct inode *inode, int write)
1314 int status = 0, level;
1315 struct ocfs2_lock_res *lockres;
1316 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1322 mlog(0, "inode %llu try to take %s open lock\n",
1323 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1324 write ? "EXMODE" : "PRMODE");
1326 if (ocfs2_mount_local(osb))
1329 lockres = &OCFS2_I(inode)->ip_open_lockres;
1331 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1334 * The file system may already holding a PRMODE/EXMODE open lock.
1335 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
1336 * other nodes and the -EAGAIN will indicate to the caller that
1337 * this inode is still in use.
1339 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1340 level, DLM_LKF_NOQUEUE, 0);
1348 * ocfs2_open_unlock unlock PR and EX mode open locks.
1350 void ocfs2_open_unlock(struct inode *inode)
1352 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1353 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1357 mlog(0, "inode %llu drop open lock\n",
1358 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1360 if (ocfs2_mount_local(osb))
1363 if(lockres->l_ro_holders)
1364 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1366 if(lockres->l_ex_holders)
1367 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1374 static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1378 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1379 unsigned long flags;
1380 struct ocfs2_mask_waiter mw;
1382 ocfs2_init_mask_waiter(&mw);
1385 spin_lock_irqsave(&lockres->l_lock, flags);
1386 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1387 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1389 spin_unlock_irqrestore(&lockres->l_lock, flags);
1390 ret = ocfs2_cancel_convert(osb, lockres);
1397 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1398 spin_unlock_irqrestore(&lockres->l_lock, flags);
1400 ocfs2_wait_for_mask(&mw);
1406 * We may still have gotten the lock, in which case there's no
1407 * point to restarting the syscall.
1409 if (lockres->l_level == level)
1412 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1413 lockres->l_flags, lockres->l_level, lockres->l_action);
1415 spin_unlock_irqrestore(&lockres->l_lock, flags);
1422 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1423 * flock() calls. The locking approach this requires is sufficiently
1424 * different from all other cluster lock types that we implement a
1425 * seperate path to the "low-level" dlm calls. In particular:
1427 * - No optimization of lock levels is done - we take at exactly
1428 * what's been requested.
1430 * - No lock caching is employed. We immediately downconvert to
1431 * no-lock at unlock time. This also means flock locks never go on
1432 * the blocking list).
1434 * - Since userspace can trivially deadlock itself with flock, we make
1435 * sure to allow cancellation of a misbehaving applications flock()
1438 * - Access to any flock lockres doesn't require concurrency, so we
1439 * can simplify the code by requiring the caller to guarantee
1440 * serialization of dlmglue flock calls.
1442 int ocfs2_file_lock(struct file *file, int ex, int trylock)
1444 int ret, level = ex ? LKM_EXMODE : LKM_PRMODE;
1445 unsigned int lkm_flags = trylock ? LKM_NOQUEUE : 0;
1446 unsigned long flags;
1447 struct ocfs2_file_private *fp = file->private_data;
1448 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1449 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1450 struct ocfs2_mask_waiter mw;
1452 ocfs2_init_mask_waiter(&mw);
1454 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
1455 (lockres->l_level > DLM_LOCK_NL)) {
1457 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1458 "level: %u\n", lockres->l_name, lockres->l_flags,
1463 spin_lock_irqsave(&lockres->l_lock, flags);
1464 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1465 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1466 spin_unlock_irqrestore(&lockres->l_lock, flags);
1469 * Get the lock at NLMODE to start - that way we
1470 * can cancel the upconvert request if need be.
1472 ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
1478 ret = ocfs2_wait_for_mask(&mw);
1483 spin_lock_irqsave(&lockres->l_lock, flags);
1486 lockres->l_action = OCFS2_AST_CONVERT;
1487 lkm_flags |= LKM_CONVERT;
1488 lockres->l_requested = level;
1489 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1491 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1492 spin_unlock_irqrestore(&lockres->l_lock, flags);
1494 ret = ocfs2_dlm_lock(osb->dlm, level, &lockres->l_lksb, lkm_flags,
1495 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1,
1498 if (!trylock || (ret != -EAGAIN)) {
1499 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
1503 ocfs2_recover_from_dlm_error(lockres, 1);
1504 lockres_remove_mask_waiter(lockres, &mw);
1508 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1509 if (ret == -ERESTARTSYS) {
1511 * Userspace can cause deadlock itself with
1512 * flock(). Current behavior locally is to allow the
1513 * deadlock, but abort the system call if a signal is
1514 * received. We follow this example, otherwise a
1515 * poorly written program could sit in kernel until
1518 * Handling this is a bit more complicated for Ocfs2
1519 * though. We can't exit this function with an
1520 * outstanding lock request, so a cancel convert is
1521 * required. We intentionally overwrite 'ret' - if the
1522 * cancel fails and the lock was granted, it's easier
1523 * to just bubble sucess back up to the user.
1525 ret = ocfs2_flock_handle_signal(lockres, level);
1530 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1531 lockres->l_name, ex, trylock, ret);
1535 void ocfs2_file_unlock(struct file *file)
1538 unsigned long flags;
1539 struct ocfs2_file_private *fp = file->private_data;
1540 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1541 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1542 struct ocfs2_mask_waiter mw;
1544 ocfs2_init_mask_waiter(&mw);
1546 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
1549 if (lockres->l_level == LKM_NLMODE)
1552 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1553 lockres->l_name, lockres->l_flags, lockres->l_level,
1556 spin_lock_irqsave(&lockres->l_lock, flags);
1558 * Fake a blocking ast for the downconvert code.
1560 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
1561 lockres->l_blocking = DLM_LOCK_EX;
1563 ocfs2_prepare_downconvert(lockres, LKM_NLMODE);
1564 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1565 spin_unlock_irqrestore(&lockres->l_lock, flags);
1567 ret = ocfs2_downconvert_lock(osb, lockres, LKM_NLMODE, 0);
1573 ret = ocfs2_wait_for_mask(&mw);
1578 static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1579 struct ocfs2_lock_res *lockres)
1585 /* If we know that another node is waiting on our lock, kick
1586 * the downconvert thread * pre-emptively when we reach a release
1588 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1589 switch(lockres->l_blocking) {
1591 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1595 if (!lockres->l_ex_holders)
1604 ocfs2_wake_downconvert_thread(osb);
1609 #define OCFS2_SEC_BITS 34
1610 #define OCFS2_SEC_SHIFT (64 - 34)
1611 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1613 /* LVB only has room for 64 bits of time here so we pack it for
1615 static u64 ocfs2_pack_timespec(struct timespec *spec)
1618 u64 sec = spec->tv_sec;
1619 u32 nsec = spec->tv_nsec;
1621 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1626 /* Call this with the lockres locked. I am reasonably sure we don't
1627 * need ip_lock in this function as anyone who would be changing those
1628 * values is supposed to be blocked in ocfs2_inode_lock right now. */
1629 static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1631 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1632 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1633 struct ocfs2_meta_lvb *lvb;
1637 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1640 * Invalidate the LVB of a deleted inode - this way other
1641 * nodes are forced to go to disk and discover the new inode
1644 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1645 lvb->lvb_version = 0;
1649 lvb->lvb_version = OCFS2_LVB_VERSION;
1650 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1651 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1652 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1653 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1654 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1655 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1656 lvb->lvb_iatime_packed =
1657 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1658 lvb->lvb_ictime_packed =
1659 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1660 lvb->lvb_imtime_packed =
1661 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1662 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
1663 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
1664 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
1667 mlog_meta_lvb(0, lockres);
1672 static void ocfs2_unpack_timespec(struct timespec *spec,
1675 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1676 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1679 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1681 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1682 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1683 struct ocfs2_meta_lvb *lvb;
1687 mlog_meta_lvb(0, lockres);
1689 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1691 /* We're safe here without the lockres lock... */
1692 spin_lock(&oi->ip_lock);
1693 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1694 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1696 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1697 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
1698 ocfs2_set_inode_flags(inode);
1700 /* fast-symlinks are a special case */
1701 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1702 inode->i_blocks = 0;
1704 inode->i_blocks = ocfs2_inode_sector_count(inode);
1706 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1707 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1708 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1709 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1710 ocfs2_unpack_timespec(&inode->i_atime,
1711 be64_to_cpu(lvb->lvb_iatime_packed));
1712 ocfs2_unpack_timespec(&inode->i_mtime,
1713 be64_to_cpu(lvb->lvb_imtime_packed));
1714 ocfs2_unpack_timespec(&inode->i_ctime,
1715 be64_to_cpu(lvb->lvb_ictime_packed));
1716 spin_unlock(&oi->ip_lock);
1721 static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1722 struct ocfs2_lock_res *lockres)
1724 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1726 if (lvb->lvb_version == OCFS2_LVB_VERSION
1727 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
1732 /* Determine whether a lock resource needs to be refreshed, and
1733 * arbitrate who gets to refresh it.
1735 * 0 means no refresh needed.
1737 * > 0 means you need to refresh this and you MUST call
1738 * ocfs2_complete_lock_res_refresh afterwards. */
1739 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1741 unsigned long flags;
1747 spin_lock_irqsave(&lockres->l_lock, flags);
1748 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1749 spin_unlock_irqrestore(&lockres->l_lock, flags);
1753 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1754 spin_unlock_irqrestore(&lockres->l_lock, flags);
1756 ocfs2_wait_on_refreshing_lock(lockres);
1760 /* Ok, I'll be the one to refresh this lock. */
1761 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1762 spin_unlock_irqrestore(&lockres->l_lock, flags);
1770 /* If status is non zero, I'll mark it as not being in refresh
1771 * anymroe, but i won't clear the needs refresh flag. */
1772 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1775 unsigned long flags;
1778 spin_lock_irqsave(&lockres->l_lock, flags);
1779 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1781 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1782 spin_unlock_irqrestore(&lockres->l_lock, flags);
1784 wake_up(&lockres->l_event);
1789 /* may or may not return a bh if it went to disk. */
1790 static int ocfs2_inode_lock_update(struct inode *inode,
1791 struct buffer_head **bh)
1794 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1795 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1796 struct ocfs2_dinode *fe;
1797 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1801 if (ocfs2_mount_local(osb))
1804 spin_lock(&oi->ip_lock);
1805 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1806 mlog(0, "Orphaned inode %llu was deleted while we "
1807 "were waiting on a lock. ip_flags = 0x%x\n",
1808 (unsigned long long)oi->ip_blkno, oi->ip_flags);
1809 spin_unlock(&oi->ip_lock);
1813 spin_unlock(&oi->ip_lock);
1815 if (!ocfs2_should_refresh_lock_res(lockres))
1818 /* This will discard any caching information we might have had
1819 * for the inode metadata. */
1820 ocfs2_metadata_cache_purge(inode);
1822 ocfs2_extent_map_trunc(inode, 0);
1824 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
1825 mlog(0, "Trusting LVB on inode %llu\n",
1826 (unsigned long long)oi->ip_blkno);
1827 ocfs2_refresh_inode_from_lvb(inode);
1829 /* Boo, we have to go to disk. */
1830 /* read bh, cast, ocfs2_refresh_inode */
1831 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1832 bh, OCFS2_BH_CACHED, inode);
1837 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1839 /* This is a good chance to make sure we're not
1840 * locking an invalid object.
1842 * We bug on a stale inode here because we checked
1843 * above whether it was wiped from disk. The wiping
1844 * node provides a guarantee that we receive that
1845 * message and can mark the inode before dropping any
1846 * locks associated with it. */
1847 if (!OCFS2_IS_VALID_DINODE(fe)) {
1848 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1852 mlog_bug_on_msg(inode->i_generation !=
1853 le32_to_cpu(fe->i_generation),
1854 "Invalid dinode %llu disk generation: %u "
1855 "inode->i_generation: %u\n",
1856 (unsigned long long)oi->ip_blkno,
1857 le32_to_cpu(fe->i_generation),
1858 inode->i_generation);
1859 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1860 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
1861 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1862 (unsigned long long)oi->ip_blkno,
1863 (unsigned long long)le64_to_cpu(fe->i_dtime),
1864 le32_to_cpu(fe->i_flags));
1866 ocfs2_refresh_inode(inode, fe);
1871 ocfs2_complete_lock_res_refresh(lockres, status);
1877 static int ocfs2_assign_bh(struct inode *inode,
1878 struct buffer_head **ret_bh,
1879 struct buffer_head *passed_bh)
1884 /* Ok, the update went to disk for us, use the
1886 *ret_bh = passed_bh;
1892 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1893 OCFS2_I(inode)->ip_blkno,
1904 * returns < 0 error if the callback will never be called, otherwise
1905 * the result of the lock will be communicated via the callback.
1907 int ocfs2_inode_lock_full(struct inode *inode,
1908 struct buffer_head **ret_bh,
1912 int status, level, acquired;
1914 struct ocfs2_lock_res *lockres = NULL;
1915 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1916 struct buffer_head *local_bh = NULL;
1922 mlog(0, "inode %llu, take %s META lock\n",
1923 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1924 ex ? "EXMODE" : "PRMODE");
1928 /* We'll allow faking a readonly metadata lock for
1930 if (ocfs2_is_hard_readonly(osb)) {
1936 if (ocfs2_mount_local(osb))
1939 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1940 ocfs2_wait_for_recovery(osb);
1942 lockres = &OCFS2_I(inode)->ip_inode_lockres;
1943 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1945 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
1946 dlm_flags |= DLM_LKF_NOQUEUE;
1948 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1950 if (status != -EAGAIN && status != -EIOCBRETRY)
1955 /* Notify the error cleanup path to drop the cluster lock. */
1958 /* We wait twice because a node may have died while we were in
1959 * the lower dlm layers. The second time though, we've
1960 * committed to owning this lock so we don't allow signals to
1961 * abort the operation. */
1962 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1963 ocfs2_wait_for_recovery(osb);
1967 * We only see this flag if we're being called from
1968 * ocfs2_read_locked_inode(). It means we're locking an inode
1969 * which hasn't been populated yet, so clear the refresh flag
1970 * and let the caller handle it.
1972 if (inode->i_state & I_NEW) {
1975 ocfs2_complete_lock_res_refresh(lockres, 0);
1979 /* This is fun. The caller may want a bh back, or it may
1980 * not. ocfs2_inode_lock_update definitely wants one in, but
1981 * may or may not read one, depending on what's in the
1982 * LVB. The result of all of this is that we've *only* gone to
1983 * disk if we have to, so the complexity is worthwhile. */
1984 status = ocfs2_inode_lock_update(inode, &local_bh);
1986 if (status != -ENOENT)
1992 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
2001 if (ret_bh && (*ret_bh)) {
2006 ocfs2_inode_unlock(inode, ex);
2017 * This is working around a lock inversion between tasks acquiring DLM
2018 * locks while holding a page lock and the downconvert thread which
2019 * blocks dlm lock acquiry while acquiring page locks.
2021 * ** These _with_page variantes are only intended to be called from aop
2022 * methods that hold page locks and return a very specific *positive* error
2023 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2025 * The DLM is called such that it returns -EAGAIN if it would have
2026 * blocked waiting for the downconvert thread. In that case we unlock
2027 * our page so the downconvert thread can make progress. Once we've
2028 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2029 * that called us can bubble that back up into the VFS who will then
2030 * immediately retry the aop call.
2032 * We do a blocking lock and immediate unlock before returning, though, so that
2033 * the lock has a great chance of being cached on this node by the time the VFS
2034 * calls back to retry the aop. This has a potential to livelock as nodes
2035 * ping locks back and forth, but that's a risk we're willing to take to avoid
2036 * the lock inversion simply.
2038 int ocfs2_inode_lock_with_page(struct inode *inode,
2039 struct buffer_head **ret_bh,
2045 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
2046 if (ret == -EAGAIN) {
2048 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2049 ocfs2_inode_unlock(inode, ex);
2050 ret = AOP_TRUNCATED_PAGE;
2056 int ocfs2_inode_lock_atime(struct inode *inode,
2057 struct vfsmount *vfsmnt,
2063 ret = ocfs2_inode_lock(inode, NULL, 0);
2070 * If we should update atime, we will get EX lock,
2071 * otherwise we just get PR lock.
2073 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2074 struct buffer_head *bh = NULL;
2076 ocfs2_inode_unlock(inode, 0);
2077 ret = ocfs2_inode_lock(inode, &bh, 1);
2083 if (ocfs2_should_update_atime(inode, vfsmnt))
2084 ocfs2_update_inode_atime(inode, bh);
2094 void ocfs2_inode_unlock(struct inode *inode,
2097 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2098 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
2099 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2103 mlog(0, "inode %llu drop %s META lock\n",
2104 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2105 ex ? "EXMODE" : "PRMODE");
2107 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2108 !ocfs2_mount_local(osb))
2109 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
2114 int ocfs2_super_lock(struct ocfs2_super *osb,
2118 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2119 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2123 if (ocfs2_is_hard_readonly(osb))
2126 if (ocfs2_mount_local(osb))
2129 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2135 /* The super block lock path is really in the best position to
2136 * know when resources covered by the lock need to be
2137 * refreshed, so we do it here. Of course, making sense of
2138 * everything is up to the caller :) */
2139 status = ocfs2_should_refresh_lock_res(lockres);
2145 status = ocfs2_refresh_slot_info(osb);
2147 ocfs2_complete_lock_res_refresh(lockres, status);
2157 void ocfs2_super_unlock(struct ocfs2_super *osb,
2160 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2161 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2163 if (!ocfs2_mount_local(osb))
2164 ocfs2_cluster_unlock(osb, lockres, level);
2167 int ocfs2_rename_lock(struct ocfs2_super *osb)
2170 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2172 if (ocfs2_is_hard_readonly(osb))
2175 if (ocfs2_mount_local(osb))
2178 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
2185 void ocfs2_rename_unlock(struct ocfs2_super *osb)
2187 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2189 if (!ocfs2_mount_local(osb))
2190 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2193 int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2196 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2197 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2198 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2202 if (ocfs2_is_hard_readonly(osb))
2205 if (ocfs2_mount_local(osb))
2208 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2215 void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2217 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2218 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2219 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2221 if (!ocfs2_mount_local(osb))
2222 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
2225 /* Reference counting of the dlm debug structure. We want this because
2226 * open references on the debug inodes can live on after a mount, so
2227 * we can't rely on the ocfs2_super to always exist. */
2228 static void ocfs2_dlm_debug_free(struct kref *kref)
2230 struct ocfs2_dlm_debug *dlm_debug;
2232 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2237 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2240 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2243 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2245 kref_get(&debug->d_refcnt);
2248 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2250 struct ocfs2_dlm_debug *dlm_debug;
2252 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2254 mlog_errno(-ENOMEM);
2258 kref_init(&dlm_debug->d_refcnt);
2259 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2260 dlm_debug->d_locking_state = NULL;
2265 /* Access to this is arbitrated for us via seq_file->sem. */
2266 struct ocfs2_dlm_seq_priv {
2267 struct ocfs2_dlm_debug *p_dlm_debug;
2268 struct ocfs2_lock_res p_iter_res;
2269 struct ocfs2_lock_res p_tmp_res;
2272 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2273 struct ocfs2_dlm_seq_priv *priv)
2275 struct ocfs2_lock_res *iter, *ret = NULL;
2276 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2278 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2280 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2281 /* discover the head of the list */
2282 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2283 mlog(0, "End of list found, %p\n", ret);
2287 /* We track our "dummy" iteration lockres' by a NULL
2289 if (iter->l_ops != NULL) {
2298 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2300 struct ocfs2_dlm_seq_priv *priv = m->private;
2301 struct ocfs2_lock_res *iter;
2303 spin_lock(&ocfs2_dlm_tracking_lock);
2304 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2306 /* Since lockres' have the lifetime of their container
2307 * (which can be inodes, ocfs2_supers, etc) we want to
2308 * copy this out to a temporary lockres while still
2309 * under the spinlock. Obviously after this we can't
2310 * trust any pointers on the copy returned, but that's
2311 * ok as the information we want isn't typically held
2313 priv->p_tmp_res = *iter;
2314 iter = &priv->p_tmp_res;
2316 spin_unlock(&ocfs2_dlm_tracking_lock);
2321 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2325 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2327 struct ocfs2_dlm_seq_priv *priv = m->private;
2328 struct ocfs2_lock_res *iter = v;
2329 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2331 spin_lock(&ocfs2_dlm_tracking_lock);
2332 iter = ocfs2_dlm_next_res(iter, priv);
2333 list_del_init(&dummy->l_debug_list);
2335 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2336 priv->p_tmp_res = *iter;
2337 iter = &priv->p_tmp_res;
2339 spin_unlock(&ocfs2_dlm_tracking_lock);
2344 /* So that debugfs.ocfs2 can determine which format is being used */
2345 #define OCFS2_DLM_DEBUG_STR_VERSION 1
2346 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2350 struct ocfs2_lock_res *lockres = v;
2355 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2357 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2358 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2360 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2362 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2364 seq_printf(m, "%d\t"
2375 lockres->l_unlock_action,
2376 lockres->l_ro_holders,
2377 lockres->l_ex_holders,
2378 lockres->l_requested,
2379 lockres->l_blocking);
2381 /* Dump the raw LVB */
2382 lvb = lockres->l_lksb.lvb;
2383 for(i = 0; i < DLM_LVB_LEN; i++)
2384 seq_printf(m, "0x%x\t", lvb[i]);
2387 seq_printf(m, "\n");
2391 static const struct seq_operations ocfs2_dlm_seq_ops = {
2392 .start = ocfs2_dlm_seq_start,
2393 .stop = ocfs2_dlm_seq_stop,
2394 .next = ocfs2_dlm_seq_next,
2395 .show = ocfs2_dlm_seq_show,
2398 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2400 struct seq_file *seq = (struct seq_file *) file->private_data;
2401 struct ocfs2_dlm_seq_priv *priv = seq->private;
2402 struct ocfs2_lock_res *res = &priv->p_iter_res;
2404 ocfs2_remove_lockres_tracking(res);
2405 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2406 return seq_release_private(inode, file);
2409 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2412 struct ocfs2_dlm_seq_priv *priv;
2413 struct seq_file *seq;
2414 struct ocfs2_super *osb;
2416 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2422 osb = inode->i_private;
2423 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2424 priv->p_dlm_debug = osb->osb_dlm_debug;
2425 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2427 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2434 seq = (struct seq_file *) file->private_data;
2435 seq->private = priv;
2437 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2444 static const struct file_operations ocfs2_dlm_debug_fops = {
2445 .open = ocfs2_dlm_debug_open,
2446 .release = ocfs2_dlm_debug_release,
2448 .llseek = seq_lseek,
2451 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2454 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2456 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2458 osb->osb_debug_root,
2460 &ocfs2_dlm_debug_fops);
2461 if (!dlm_debug->d_locking_state) {
2464 "Unable to create locking state debugfs file.\n");
2468 ocfs2_get_dlm_debug(dlm_debug);
2473 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2475 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2478 debugfs_remove(dlm_debug->d_locking_state);
2479 ocfs2_put_dlm_debug(dlm_debug);
2483 int ocfs2_dlm_init(struct ocfs2_super *osb)
2487 struct dlm_ctxt *dlm = NULL;
2491 if (ocfs2_mount_local(osb))
2494 status = ocfs2_dlm_init_debug(osb);
2500 /* launch downconvert thread */
2501 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2502 if (IS_ERR(osb->dc_task)) {
2503 status = PTR_ERR(osb->dc_task);
2504 osb->dc_task = NULL;
2509 /* used by the dlm code to make message headers unique, each
2510 * node in this domain must agree on this. */
2511 dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
2513 /* for now, uuid == domain */
2514 dlm = dlm_register_domain(osb->uuid_str, dlm_key,
2515 &osb->osb_locking_proto);
2517 status = PTR_ERR(dlm);
2522 dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
2525 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2526 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2533 ocfs2_dlm_shutdown_debug(osb);
2535 kthread_stop(osb->dc_task);
2542 void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2546 dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
2548 ocfs2_drop_osb_locks(osb);
2551 kthread_stop(osb->dc_task);
2552 osb->dc_task = NULL;
2555 ocfs2_lock_res_free(&osb->osb_super_lockres);
2556 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2558 dlm_unregister_domain(osb->dlm);
2561 ocfs2_dlm_shutdown_debug(osb);
2566 static void ocfs2_unlock_ast(void *opaque, int error)
2568 struct ocfs2_lock_res *lockres = opaque;
2569 unsigned long flags;
2573 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2574 lockres->l_unlock_action);
2576 spin_lock_irqsave(&lockres->l_lock, flags);
2577 /* We tried to cancel a convert request, but it was already
2578 * granted. All we want to do here is clear our unlock
2579 * state. The wake_up call done at the bottom is redundant
2580 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2581 * hurt anything anyway */
2582 if (error == -DLM_ECANCEL &&
2583 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2584 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2586 /* We don't clear the busy flag in this case as it
2587 * should have been cleared by the ast which the dlm
2589 goto complete_unlock;
2592 /* DLM_EUNLOCK is the success code for unlock */
2593 if (error != -DLM_EUNLOCK) {
2594 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
2595 "unlock_action %d\n", error, lockres->l_name,
2596 lockres->l_unlock_action);
2597 spin_unlock_irqrestore(&lockres->l_lock, flags);
2601 switch(lockres->l_unlock_action) {
2602 case OCFS2_UNLOCK_CANCEL_CONVERT:
2603 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2604 lockres->l_action = OCFS2_AST_INVALID;
2606 case OCFS2_UNLOCK_DROP_LOCK:
2607 lockres->l_level = DLM_LOCK_IV;
2613 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2615 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2616 spin_unlock_irqrestore(&lockres->l_lock, flags);
2618 wake_up(&lockres->l_event);
2623 static int ocfs2_drop_lock(struct ocfs2_super *osb,
2624 struct ocfs2_lock_res *lockres)
2627 unsigned long flags;
2630 /* We didn't get anywhere near actually using this lockres. */
2631 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2634 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
2635 lkm_flags |= DLM_LKF_VALBLK;
2637 spin_lock_irqsave(&lockres->l_lock, flags);
2639 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2640 "lockres %s, flags 0x%lx\n",
2641 lockres->l_name, lockres->l_flags);
2643 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2644 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2645 "%u, unlock_action = %u\n",
2646 lockres->l_name, lockres->l_flags, lockres->l_action,
2647 lockres->l_unlock_action);
2649 spin_unlock_irqrestore(&lockres->l_lock, flags);
2651 /* XXX: Today we just wait on any busy
2652 * locks... Perhaps we need to cancel converts in the
2654 ocfs2_wait_on_busy_lock(lockres);
2656 spin_lock_irqsave(&lockres->l_lock, flags);
2659 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2660 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2661 lockres->l_level == DLM_LOCK_EX &&
2662 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2663 lockres->l_ops->set_lvb(lockres);
2666 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2667 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2669 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2670 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2672 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2673 spin_unlock_irqrestore(&lockres->l_lock, flags);
2677 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2679 /* make sure we never get here while waiting for an ast to
2681 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2683 /* is this necessary? */
2684 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2685 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2686 spin_unlock_irqrestore(&lockres->l_lock, flags);
2688 mlog(0, "lock %s\n", lockres->l_name);
2690 ret = ocfs2_dlm_unlock(osb->dlm, &lockres->l_lksb, lkm_flags,
2693 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
2694 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2695 dlm_print_one_lock(lockres->l_lksb.lockid);
2698 mlog(0, "lock %s, successfull return from ocfs2_dlm_unlock\n",
2701 ocfs2_wait_on_busy_lock(lockres);
2707 /* Mark the lockres as being dropped. It will no longer be
2708 * queued if blocking, but we still may have to wait on it
2709 * being dequeued from the downconvert thread before we can consider
2712 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2713 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2716 struct ocfs2_mask_waiter mw;
2717 unsigned long flags;
2719 ocfs2_init_mask_waiter(&mw);
2721 spin_lock_irqsave(&lockres->l_lock, flags);
2722 lockres->l_flags |= OCFS2_LOCK_FREEING;
2723 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2724 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2725 spin_unlock_irqrestore(&lockres->l_lock, flags);
2727 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2729 status = ocfs2_wait_for_mask(&mw);
2733 spin_lock_irqsave(&lockres->l_lock, flags);
2735 spin_unlock_irqrestore(&lockres->l_lock, flags);
2738 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2739 struct ocfs2_lock_res *lockres)
2743 ocfs2_mark_lockres_freeing(lockres);
2744 ret = ocfs2_drop_lock(osb, lockres);
2749 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2751 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2752 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
2755 int ocfs2_drop_inode_locks(struct inode *inode)
2761 /* No need to call ocfs2_mark_lockres_freeing here -
2762 * ocfs2_clear_inode has done it for us. */
2764 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2765 &OCFS2_I(inode)->ip_open_lockres);
2771 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2772 &OCFS2_I(inode)->ip_inode_lockres);
2775 if (err < 0 && !status)
2778 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2779 &OCFS2_I(inode)->ip_rw_lockres);
2782 if (err < 0 && !status)
2789 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2792 assert_spin_locked(&lockres->l_lock);
2794 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
2796 if (lockres->l_level <= new_level) {
2797 mlog(ML_ERROR, "lockres->l_level (%d) <= new_level (%d)\n",
2798 lockres->l_level, new_level);
2802 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2803 lockres->l_name, new_level, lockres->l_blocking);
2805 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2806 lockres->l_requested = new_level;
2807 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2810 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2811 struct ocfs2_lock_res *lockres,
2816 u32 dlm_flags = DLM_LKF_CONVERT;
2821 dlm_flags |= DLM_LKF_VALBLK;
2823 ret = ocfs2_dlm_lock(osb->dlm,
2828 OCFS2_LOCK_ID_MAX_LEN - 1,
2831 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
2832 ocfs2_recover_from_dlm_error(lockres, 1);
2842 /* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
2843 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2844 struct ocfs2_lock_res *lockres)
2846 assert_spin_locked(&lockres->l_lock);
2849 mlog(0, "lock %s\n", lockres->l_name);
2851 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2852 /* If we're already trying to cancel a lock conversion
2853 * then just drop the spinlock and allow the caller to
2854 * requeue this lock. */
2856 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2860 /* were we in a convert when we got the bast fire? */
2861 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2862 lockres->l_action != OCFS2_AST_DOWNCONVERT);
2863 /* set things up for the unlockast to know to just
2864 * clear out the ast_action and unset busy, etc. */
2865 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2867 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2868 "lock %s, invalid flags: 0x%lx\n",
2869 lockres->l_name, lockres->l_flags);
2874 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2875 struct ocfs2_lock_res *lockres)
2880 mlog(0, "lock %s\n", lockres->l_name);
2882 ret = ocfs2_dlm_unlock(osb->dlm, &lockres->l_lksb,
2883 DLM_LKF_CANCEL, lockres);
2885 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
2886 ocfs2_recover_from_dlm_error(lockres, 0);
2889 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres->l_name);
2895 static int ocfs2_unblock_lock(struct ocfs2_super *osb,
2896 struct ocfs2_lock_res *lockres,
2897 struct ocfs2_unblock_ctl *ctl)
2899 unsigned long flags;
2907 spin_lock_irqsave(&lockres->l_lock, flags);
2909 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2912 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2914 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2915 spin_unlock_irqrestore(&lockres->l_lock, flags);
2917 ret = ocfs2_cancel_convert(osb, lockres);
2924 /* if we're blocking an exclusive and we have *any* holders,
2926 if ((lockres->l_blocking == DLM_LOCK_EX)
2927 && (lockres->l_ex_holders || lockres->l_ro_holders))
2930 /* If it's a PR we're blocking, then only
2931 * requeue if we've got any EX holders */
2932 if (lockres->l_blocking == DLM_LOCK_PR &&
2933 lockres->l_ex_holders)
2937 * Can we get a lock in this state if the holder counts are
2938 * zero? The meta data unblock code used to check this.
2940 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
2941 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
2944 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2946 if (lockres->l_ops->check_downconvert
2947 && !lockres->l_ops->check_downconvert(lockres, new_level))
2950 /* If we get here, then we know that there are no more
2951 * incompatible holders (and anyone asking for an incompatible
2952 * lock is blocked). We can now downconvert the lock */
2953 if (!lockres->l_ops->downconvert_worker)
2956 /* Some lockres types want to do a bit of work before
2957 * downconverting a lock. Allow that here. The worker function
2958 * may sleep, so we save off a copy of what we're blocking as
2959 * it may change while we're not holding the spin lock. */
2960 blocking = lockres->l_blocking;
2961 spin_unlock_irqrestore(&lockres->l_lock, flags);
2963 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
2965 if (ctl->unblock_action == UNBLOCK_STOP_POST)
2968 spin_lock_irqsave(&lockres->l_lock, flags);
2969 if (blocking != lockres->l_blocking) {
2970 /* If this changed underneath us, then we can't drop
2978 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2979 if (lockres->l_level == DLM_LOCK_EX)
2983 * We only set the lvb if the lock has been fully
2984 * refreshed - otherwise we risk setting stale
2985 * data. Otherwise, there's no need to actually clear
2986 * out the lvb here as it's value is still valid.
2988 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2989 lockres->l_ops->set_lvb(lockres);
2992 ocfs2_prepare_downconvert(lockres, new_level);
2993 spin_unlock_irqrestore(&lockres->l_lock, flags);
2994 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
3000 spin_unlock_irqrestore(&lockres->l_lock, flags);
3007 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3010 struct inode *inode;
3011 struct address_space *mapping;
3013 inode = ocfs2_lock_res_inode(lockres);
3014 mapping = inode->i_mapping;
3016 if (!S_ISREG(inode->i_mode))
3020 * We need this before the filemap_fdatawrite() so that it can
3021 * transfer the dirty bit from the PTE to the
3022 * page. Unfortunately this means that even for EX->PR
3023 * downconverts, we'll lose our mappings and have to build
3026 unmap_mapping_range(mapping, 0, 0, 0);
3028 if (filemap_fdatawrite(mapping)) {
3029 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3030 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3032 sync_mapping_buffers(mapping);
3033 if (blocking == DLM_LOCK_EX) {
3034 truncate_inode_pages(mapping, 0);
3036 /* We only need to wait on the I/O if we're not also
3037 * truncating pages because truncate_inode_pages waits
3038 * for us above. We don't truncate pages if we're
3039 * blocking anything < EXMODE because we want to keep
3040 * them around in that case. */
3041 filemap_fdatawait(mapping);
3045 return UNBLOCK_CONTINUE;
3048 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3051 struct inode *inode = ocfs2_lock_res_inode(lockres);
3052 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
3054 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3055 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
3060 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
3064 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3066 struct inode *inode = ocfs2_lock_res_inode(lockres);
3068 __ocfs2_stuff_meta_lvb(inode);
3072 * Does the final reference drop on our dentry lock. Right now this
3073 * happens in the downconvert thread, but we could choose to simplify the
3074 * dlmglue API and push these off to the ocfs2_wq in the future.
3076 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3077 struct ocfs2_lock_res *lockres)
3079 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3080 ocfs2_dentry_lock_put(osb, dl);
3084 * d_delete() matching dentries before the lock downconvert.
3086 * At this point, any process waiting to destroy the
3087 * dentry_lock due to last ref count is stopped by the
3088 * OCFS2_LOCK_QUEUED flag.
3090 * We have two potential problems
3092 * 1) If we do the last reference drop on our dentry_lock (via dput)
3093 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3094 * the downconvert to finish. Instead we take an elevated
3095 * reference and push the drop until after we've completed our
3096 * unblock processing.
3098 * 2) There might be another process with a final reference,
3099 * waiting on us to finish processing. If this is the case, we
3100 * detect it and exit out - there's no more dentries anyway.
3102 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3105 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3106 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3107 struct dentry *dentry;
3108 unsigned long flags;
3112 * This node is blocking another node from getting a read
3113 * lock. This happens when we've renamed within a
3114 * directory. We've forced the other nodes to d_delete(), but
3115 * we never actually dropped our lock because it's still
3116 * valid. The downconvert code will retain a PR for this node,
3117 * so there's no further work to do.
3119 if (blocking == DLM_LOCK_PR)
3120 return UNBLOCK_CONTINUE;
3123 * Mark this inode as potentially orphaned. The code in
3124 * ocfs2_delete_inode() will figure out whether it actually
3125 * needs to be freed or not.
3127 spin_lock(&oi->ip_lock);
3128 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3129 spin_unlock(&oi->ip_lock);
3132 * Yuck. We need to make sure however that the check of
3133 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3134 * respect to a reference decrement or the setting of that
3137 spin_lock_irqsave(&lockres->l_lock, flags);
3138 spin_lock(&dentry_attach_lock);
3139 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3144 spin_unlock(&dentry_attach_lock);
3145 spin_unlock_irqrestore(&lockres->l_lock, flags);
3147 mlog(0, "extra_ref = %d\n", extra_ref);
3150 * We have a process waiting on us in ocfs2_dentry_iput(),
3151 * which means we can't have any more outstanding
3152 * aliases. There's no need to do any more work.
3155 return UNBLOCK_CONTINUE;
3157 spin_lock(&dentry_attach_lock);
3159 dentry = ocfs2_find_local_alias(dl->dl_inode,
3160 dl->dl_parent_blkno, 1);
3163 spin_unlock(&dentry_attach_lock);
3165 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3166 dentry->d_name.name);
3169 * The following dcache calls may do an
3170 * iput(). Normally we don't want that from the
3171 * downconverting thread, but in this case it's ok
3172 * because the requesting node already has an
3173 * exclusive lock on the inode, so it can't be queued
3174 * for a downconvert.
3179 spin_lock(&dentry_attach_lock);
3181 spin_unlock(&dentry_attach_lock);
3184 * If we are the last holder of this dentry lock, there is no
3185 * reason to downconvert so skip straight to the unlock.
3187 if (dl->dl_count == 1)
3188 return UNBLOCK_STOP_POST;
3190 return UNBLOCK_CONTINUE_POST;
3193 static struct ocfs2_locking_protocol lproto = {
3194 .lp_lock_ast = ocfs2_locking_ast,
3195 .lp_blocking_ast = ocfs2_blocking_ast,
3196 .lp_unlock_ast = ocfs2_unlock_ast,
3199 /* This interface isn't the final one, hence the less-than-perfect names */
3200 void dlmglue_init_stack(void)
3202 o2cb_get_stack(&lproto);
3205 void dlmglue_exit_stack(void)
3210 static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3211 struct ocfs2_lock_res *lockres)
3214 struct ocfs2_unblock_ctl ctl = {0, 0,};
3215 unsigned long flags;
3217 /* Our reference to the lockres in this function can be
3218 * considered valid until we remove the OCFS2_LOCK_QUEUED
3224 BUG_ON(!lockres->l_ops);
3226 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3228 /* Detect whether a lock has been marked as going away while
3229 * the downconvert thread was processing other things. A lock can
3230 * still be marked with OCFS2_LOCK_FREEING after this check,
3231 * but short circuiting here will still save us some
3233 spin_lock_irqsave(&lockres->l_lock, flags);
3234 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3236 spin_unlock_irqrestore(&lockres->l_lock, flags);
3238 status = ocfs2_unblock_lock(osb, lockres, &ctl);
3242 spin_lock_irqsave(&lockres->l_lock, flags);
3244 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
3245 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3247 ocfs2_schedule_blocked_lock(osb, lockres);
3249 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
3250 ctl.requeue ? "yes" : "no");
3251 spin_unlock_irqrestore(&lockres->l_lock, flags);
3253 if (ctl.unblock_action != UNBLOCK_CONTINUE
3254 && lockres->l_ops->post_unlock)
3255 lockres->l_ops->post_unlock(osb, lockres);
3260 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3261 struct ocfs2_lock_res *lockres)
3265 assert_spin_locked(&lockres->l_lock);
3267 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3268 /* Do not schedule a lock for downconvert when it's on
3269 * the way to destruction - any nodes wanting access
3270 * to the resource will get it soon. */
3271 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3272 lockres->l_name, lockres->l_flags);
3276 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3278 spin_lock(&osb->dc_task_lock);
3279 if (list_empty(&lockres->l_blocked_list)) {
3280 list_add_tail(&lockres->l_blocked_list,
3281 &osb->blocked_lock_list);
3282 osb->blocked_lock_count++;
3284 spin_unlock(&osb->dc_task_lock);
3289 static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3291 unsigned long processed;
3292 struct ocfs2_lock_res *lockres;
3296 spin_lock(&osb->dc_task_lock);
3297 /* grab this early so we know to try again if a state change and
3298 * wake happens part-way through our work */
3299 osb->dc_work_sequence = osb->dc_wake_sequence;
3301 processed = osb->blocked_lock_count;
3303 BUG_ON(list_empty(&osb->blocked_lock_list));
3305 lockres = list_entry(osb->blocked_lock_list.next,
3306 struct ocfs2_lock_res, l_blocked_list);
3307 list_del_init(&lockres->l_blocked_list);
3308 osb->blocked_lock_count--;
3309 spin_unlock(&osb->dc_task_lock);
3314 ocfs2_process_blocked_lock(osb, lockres);
3316 spin_lock(&osb->dc_task_lock);
3318 spin_unlock(&osb->dc_task_lock);
3323 static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3327 spin_lock(&osb->dc_task_lock);
3328 if (list_empty(&osb->blocked_lock_list))
3331 spin_unlock(&osb->dc_task_lock);
3335 static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
3337 int should_wake = 0;
3339 spin_lock(&osb->dc_task_lock);
3340 if (osb->dc_work_sequence != osb->dc_wake_sequence)
3342 spin_unlock(&osb->dc_task_lock);
3347 static int ocfs2_downconvert_thread(void *arg)
3350 struct ocfs2_super *osb = arg;
3352 /* only quit once we've been asked to stop and there is no more
3354 while (!(kthread_should_stop() &&
3355 ocfs2_downconvert_thread_lists_empty(osb))) {
3357 wait_event_interruptible(osb->dc_event,
3358 ocfs2_downconvert_thread_should_wake(osb) ||
3359 kthread_should_stop());
3361 mlog(0, "downconvert_thread: awoken\n");
3363 ocfs2_downconvert_thread_do_work(osb);
3366 osb->dc_task = NULL;
3370 void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
3372 spin_lock(&osb->dc_task_lock);
3373 /* make sure the voting thread gets a swipe at whatever changes
3374 * the caller may have made to the voting state */
3375 osb->dc_wake_sequence++;
3376 spin_unlock(&osb->dc_task_lock);
3377 wake_up(&osb->dc_event);