1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Code which implements an OCFS2 specific interface to our DLM.
8 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
26 #include <linux/types.h>
27 #include <linux/slab.h>
28 #include <linux/highmem.h>
30 #include <linux/kthread.h>
31 #include <linux/pagemap.h>
32 #include <linux/debugfs.h>
33 #include <linux/seq_file.h>
34 #include <linux/time.h>
35 #include <linux/quotaops.h>
37 #define MLOG_MASK_PREFIX ML_DLM_GLUE
38 #include <cluster/masklog.h>
41 #include "ocfs2_lockingver.h"
46 #include "extent_map.h"
48 #include "heartbeat.h"
51 #include "stackglue.h"
57 #include "buffer_head_io.h"
59 struct ocfs2_mask_waiter {
60 struct list_head mw_item;
62 struct completion mw_complete;
63 unsigned long mw_mask;
64 unsigned long mw_goal;
65 #ifdef CONFIG_OCFS2_FS_STATS
66 unsigned long long mw_lock_start;
70 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
71 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
72 static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres);
73 static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres);
76 * Return value from ->downconvert_worker functions.
78 * These control the precise actions of ocfs2_unblock_lock()
79 * and ocfs2_process_blocked_lock()
82 enum ocfs2_unblock_action {
83 UNBLOCK_CONTINUE = 0, /* Continue downconvert */
84 UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire
85 * ->post_unlock callback */
86 UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire
87 * ->post_unlock() callback. */
90 struct ocfs2_unblock_ctl {
92 enum ocfs2_unblock_action unblock_action;
95 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
97 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
99 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
102 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
105 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
106 struct ocfs2_lock_res *lockres);
108 static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres);
110 #define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres)
112 /* This aids in debugging situations where a bad LVB might be involved. */
113 static void ocfs2_dump_meta_lvb_info(u64 level,
114 const char *function,
116 struct ocfs2_lock_res *lockres)
118 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
120 mlog(level, "LVB information for %s (called from %s:%u):\n",
121 lockres->l_name, function, line);
122 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
123 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
124 be32_to_cpu(lvb->lvb_igeneration));
125 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
126 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
127 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
128 be16_to_cpu(lvb->lvb_imode));
129 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
130 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
131 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
132 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
133 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
134 be32_to_cpu(lvb->lvb_iattr));
139 * OCFS2 Lock Resource Operations
141 * These fine tune the behavior of the generic dlmglue locking infrastructure.
143 * The most basic of lock types can point ->l_priv to their respective
144 * struct ocfs2_super and allow the default actions to manage things.
146 * Right now, each lock type also needs to implement an init function,
147 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
148 * should be called when the lock is no longer needed (i.e., object
151 struct ocfs2_lock_res_ops {
153 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
154 * this callback if ->l_priv is not an ocfs2_super pointer
156 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
159 * Optionally called in the downconvert thread after a
160 * successful downconvert. The lockres will not be referenced
161 * after this callback is called, so it is safe to free
164 * The exact semantics of when this is called are controlled
165 * by ->downconvert_worker()
167 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
170 * Allow a lock type to add checks to determine whether it is
171 * safe to downconvert a lock. Return 0 to re-queue the
172 * downconvert at a later time, nonzero to continue.
174 * For most locks, the default checks that there are no
175 * incompatible holders are sufficient.
177 * Called with the lockres spinlock held.
179 int (*check_downconvert)(struct ocfs2_lock_res *, int);
182 * Allows a lock type to populate the lock value block. This
183 * is called on downconvert, and when we drop a lock.
185 * Locks that want to use this should set LOCK_TYPE_USES_LVB
186 * in the flags field.
188 * Called with the lockres spinlock held.
190 void (*set_lvb)(struct ocfs2_lock_res *);
193 * Called from the downconvert thread when it is determined
194 * that a lock will be downconverted. This is called without
195 * any locks held so the function can do work that might
196 * schedule (syncing out data, etc).
198 * This should return any one of the ocfs2_unblock_action
199 * values, depending on what it wants the thread to do.
201 int (*downconvert_worker)(struct ocfs2_lock_res *, int);
204 * LOCK_TYPE_* flags which describe the specific requirements
205 * of a lock type. Descriptions of each individual flag follow.
211 * Some locks want to "refresh" potentially stale data when a
212 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
213 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
214 * individual lockres l_flags member from the ast function. It is
215 * expected that the locking wrapper will clear the
216 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
218 #define LOCK_TYPE_REQUIRES_REFRESH 0x1
221 * Indicate that a lock type makes use of the lock value block. The
222 * ->set_lvb lock type callback must be defined.
224 #define LOCK_TYPE_USES_LVB 0x2
226 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
227 .get_osb = ocfs2_get_inode_osb,
231 static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops = {
232 .get_osb = ocfs2_get_inode_osb,
233 .check_downconvert = ocfs2_check_meta_downconvert,
234 .set_lvb = ocfs2_set_meta_lvb,
235 .downconvert_worker = ocfs2_data_convert_worker,
236 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
239 static struct ocfs2_lock_res_ops ocfs2_super_lops = {
240 .flags = LOCK_TYPE_REQUIRES_REFRESH,
243 static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
247 static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
248 .get_osb = ocfs2_get_dentry_osb,
249 .post_unlock = ocfs2_dentry_post_unlock,
250 .downconvert_worker = ocfs2_dentry_convert_worker,
254 static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
255 .get_osb = ocfs2_get_inode_osb,
259 static struct ocfs2_lock_res_ops ocfs2_flock_lops = {
260 .get_osb = ocfs2_get_file_osb,
264 static struct ocfs2_lock_res_ops ocfs2_qinfo_lops = {
265 .set_lvb = ocfs2_set_qinfo_lvb,
266 .get_osb = ocfs2_get_qinfo_osb,
267 .flags = LOCK_TYPE_REQUIRES_REFRESH | LOCK_TYPE_USES_LVB,
270 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
272 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
273 lockres->l_type == OCFS2_LOCK_TYPE_RW ||
274 lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
277 static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
279 BUG_ON(!ocfs2_is_inode_lock(lockres));
281 return (struct inode *) lockres->l_priv;
284 static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
286 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
288 return (struct ocfs2_dentry_lock *)lockres->l_priv;
291 static inline struct ocfs2_mem_dqinfo *ocfs2_lock_res_qinfo(struct ocfs2_lock_res *lockres)
293 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_QINFO);
295 return (struct ocfs2_mem_dqinfo *)lockres->l_priv;
298 static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
300 if (lockres->l_ops->get_osb)
301 return lockres->l_ops->get_osb(lockres);
303 return (struct ocfs2_super *)lockres->l_priv;
306 static int ocfs2_lock_create(struct ocfs2_super *osb,
307 struct ocfs2_lock_res *lockres,
310 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
312 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
313 struct ocfs2_lock_res *lockres,
315 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
316 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
317 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
318 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
319 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
320 struct ocfs2_lock_res *lockres);
321 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
323 #define ocfs2_log_dlm_error(_func, _err, _lockres) do { \
324 mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \
325 _err, _func, _lockres->l_name); \
327 static int ocfs2_downconvert_thread(void *arg);
328 static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
329 struct ocfs2_lock_res *lockres);
330 static int ocfs2_inode_lock_update(struct inode *inode,
331 struct buffer_head **bh);
332 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
333 static inline int ocfs2_highest_compat_lock_level(int level);
334 static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
336 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
337 struct ocfs2_lock_res *lockres,
340 unsigned int generation);
341 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
342 struct ocfs2_lock_res *lockres);
343 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
344 struct ocfs2_lock_res *lockres);
347 static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
356 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
358 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
359 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
360 (long long)blkno, generation);
362 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
364 mlog(0, "built lock resource with name: %s\n", name);
369 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
371 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
372 struct ocfs2_dlm_debug *dlm_debug)
374 mlog(0, "Add tracking for lockres %s\n", res->l_name);
376 spin_lock(&ocfs2_dlm_tracking_lock);
377 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
378 spin_unlock(&ocfs2_dlm_tracking_lock);
381 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
383 spin_lock(&ocfs2_dlm_tracking_lock);
384 if (!list_empty(&res->l_debug_list))
385 list_del_init(&res->l_debug_list);
386 spin_unlock(&ocfs2_dlm_tracking_lock);
389 #ifdef CONFIG_OCFS2_FS_STATS
390 static void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
392 res->l_lock_num_prmode = 0;
393 res->l_lock_num_prmode_failed = 0;
394 res->l_lock_total_prmode = 0;
395 res->l_lock_max_prmode = 0;
396 res->l_lock_num_exmode = 0;
397 res->l_lock_num_exmode_failed = 0;
398 res->l_lock_total_exmode = 0;
399 res->l_lock_max_exmode = 0;
400 res->l_lock_refresh = 0;
403 static void ocfs2_update_lock_stats(struct ocfs2_lock_res *res, int level,
404 struct ocfs2_mask_waiter *mw, int ret)
406 unsigned long long *num, *sum;
407 unsigned int *max, *failed;
408 struct timespec ts = current_kernel_time();
409 unsigned long long time = timespec_to_ns(&ts) - mw->mw_lock_start;
411 if (level == LKM_PRMODE) {
412 num = &res->l_lock_num_prmode;
413 sum = &res->l_lock_total_prmode;
414 max = &res->l_lock_max_prmode;
415 failed = &res->l_lock_num_prmode_failed;
416 } else if (level == LKM_EXMODE) {
417 num = &res->l_lock_num_exmode;
418 sum = &res->l_lock_total_exmode;
419 max = &res->l_lock_max_exmode;
420 failed = &res->l_lock_num_exmode_failed;
432 static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
434 lockres->l_lock_refresh++;
437 static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
439 struct timespec ts = current_kernel_time();
440 mw->mw_lock_start = timespec_to_ns(&ts);
443 static inline void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
446 static inline void ocfs2_update_lock_stats(struct ocfs2_lock_res *res,
447 int level, struct ocfs2_mask_waiter *mw, int ret)
450 static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
453 static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
458 static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
459 struct ocfs2_lock_res *res,
460 enum ocfs2_lock_type type,
461 struct ocfs2_lock_res_ops *ops,
468 res->l_level = DLM_LOCK_IV;
469 res->l_requested = DLM_LOCK_IV;
470 res->l_blocking = DLM_LOCK_IV;
471 res->l_action = OCFS2_AST_INVALID;
472 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
474 res->l_flags = OCFS2_LOCK_INITIALIZED;
476 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
478 ocfs2_init_lock_stats(res);
481 void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
483 /* This also clears out the lock status block */
484 memset(res, 0, sizeof(struct ocfs2_lock_res));
485 spin_lock_init(&res->l_lock);
486 init_waitqueue_head(&res->l_event);
487 INIT_LIST_HEAD(&res->l_blocked_list);
488 INIT_LIST_HEAD(&res->l_mask_waiters);
491 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
492 enum ocfs2_lock_type type,
493 unsigned int generation,
496 struct ocfs2_lock_res_ops *ops;
499 case OCFS2_LOCK_TYPE_RW:
500 ops = &ocfs2_inode_rw_lops;
502 case OCFS2_LOCK_TYPE_META:
503 ops = &ocfs2_inode_inode_lops;
505 case OCFS2_LOCK_TYPE_OPEN:
506 ops = &ocfs2_inode_open_lops;
509 mlog_bug_on_msg(1, "type: %d\n", type);
510 ops = NULL; /* thanks, gcc */
514 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
515 generation, res->l_name);
516 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
519 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
521 struct inode *inode = ocfs2_lock_res_inode(lockres);
523 return OCFS2_SB(inode->i_sb);
526 static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres)
528 struct ocfs2_mem_dqinfo *info = lockres->l_priv;
530 return OCFS2_SB(info->dqi_gi.dqi_sb);
533 static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
535 struct ocfs2_file_private *fp = lockres->l_priv;
537 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
540 static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
542 __be64 inode_blkno_be;
544 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
547 return be64_to_cpu(inode_blkno_be);
550 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
552 struct ocfs2_dentry_lock *dl = lockres->l_priv;
554 return OCFS2_SB(dl->dl_inode->i_sb);
557 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
558 u64 parent, struct inode *inode)
561 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
562 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
563 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
565 ocfs2_lock_res_init_once(lockres);
568 * Unfortunately, the standard lock naming scheme won't work
569 * here because we have two 16 byte values to use. Instead,
570 * we'll stuff the inode number as a binary value. We still
571 * want error prints to show something without garbling the
572 * display, so drop a null byte in there before the inode
573 * number. A future version of OCFS2 will likely use all
574 * binary lock names. The stringified names have been a
575 * tremendous aid in debugging, but now that the debugfs
576 * interface exists, we can mangle things there if need be.
578 * NOTE: We also drop the standard "pad" value (the total lock
579 * name size stays the same though - the last part is all
580 * zeros due to the memset in ocfs2_lock_res_init_once()
582 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
584 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
587 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
589 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
592 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
593 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
597 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
598 struct ocfs2_super *osb)
600 /* Superblock lockres doesn't come from a slab so we call init
601 * once on it manually. */
602 ocfs2_lock_res_init_once(res);
603 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
605 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
606 &ocfs2_super_lops, osb);
609 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
610 struct ocfs2_super *osb)
612 /* Rename lockres doesn't come from a slab so we call init
613 * once on it manually. */
614 ocfs2_lock_res_init_once(res);
615 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
616 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
617 &ocfs2_rename_lops, osb);
620 void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
621 struct ocfs2_file_private *fp)
623 struct inode *inode = fp->fp_file->f_mapping->host;
624 struct ocfs2_inode_info *oi = OCFS2_I(inode);
626 ocfs2_lock_res_init_once(lockres);
627 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
628 inode->i_generation, lockres->l_name);
629 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
630 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
632 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
635 void ocfs2_qinfo_lock_res_init(struct ocfs2_lock_res *lockres,
636 struct ocfs2_mem_dqinfo *info)
638 ocfs2_lock_res_init_once(lockres);
639 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_QINFO, info->dqi_gi.dqi_type,
641 ocfs2_lock_res_init_common(OCFS2_SB(info->dqi_gi.dqi_sb), lockres,
642 OCFS2_LOCK_TYPE_QINFO, &ocfs2_qinfo_lops,
646 void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
650 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
653 ocfs2_remove_lockres_tracking(res);
655 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
656 "Lockres %s is on the blocked list\n",
658 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
659 "Lockres %s has mask waiters pending\n",
661 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
662 "Lockres %s is locked\n",
664 mlog_bug_on_msg(res->l_ro_holders,
665 "Lockres %s has %u ro holders\n",
666 res->l_name, res->l_ro_holders);
667 mlog_bug_on_msg(res->l_ex_holders,
668 "Lockres %s has %u ex holders\n",
669 res->l_name, res->l_ex_holders);
671 /* Need to clear out the lock status block for the dlm */
672 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
678 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
687 lockres->l_ex_holders++;
690 lockres->l_ro_holders++;
699 static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
708 BUG_ON(!lockres->l_ex_holders);
709 lockres->l_ex_holders--;
712 BUG_ON(!lockres->l_ro_holders);
713 lockres->l_ro_holders--;
721 /* WARNING: This function lives in a world where the only three lock
722 * levels are EX, PR, and NL. It *will* have to be adjusted when more
723 * lock types are added. */
724 static inline int ocfs2_highest_compat_lock_level(int level)
726 int new_level = DLM_LOCK_EX;
728 if (level == DLM_LOCK_EX)
729 new_level = DLM_LOCK_NL;
730 else if (level == DLM_LOCK_PR)
731 new_level = DLM_LOCK_PR;
735 static void lockres_set_flags(struct ocfs2_lock_res *lockres,
736 unsigned long newflags)
738 struct ocfs2_mask_waiter *mw, *tmp;
740 assert_spin_locked(&lockres->l_lock);
742 lockres->l_flags = newflags;
744 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
745 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
748 list_del_init(&mw->mw_item);
750 complete(&mw->mw_complete);
753 static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
755 lockres_set_flags(lockres, lockres->l_flags | or);
757 static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
760 lockres_set_flags(lockres, lockres->l_flags & ~clear);
763 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
767 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
768 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
769 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
770 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
772 lockres->l_level = lockres->l_requested;
773 if (lockres->l_level <=
774 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
775 lockres->l_blocking = DLM_LOCK_NL;
776 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
778 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
783 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
787 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
788 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
790 /* Convert from RO to EX doesn't really need anything as our
791 * information is already up to data. Convert from NL to
792 * *anything* however should mark ourselves as needing an
794 if (lockres->l_level == DLM_LOCK_NL &&
795 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
796 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
798 lockres->l_level = lockres->l_requested;
799 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
804 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
808 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
809 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
811 if (lockres->l_requested > DLM_LOCK_NL &&
812 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
813 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
814 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
816 lockres->l_level = lockres->l_requested;
817 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
818 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
823 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
826 int needs_downconvert = 0;
829 assert_spin_locked(&lockres->l_lock);
831 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
833 if (level > lockres->l_blocking) {
834 /* only schedule a downconvert if we haven't already scheduled
835 * one that goes low enough to satisfy the level we're
836 * blocking. this also catches the case where we get
838 if (ocfs2_highest_compat_lock_level(level) <
839 ocfs2_highest_compat_lock_level(lockres->l_blocking))
840 needs_downconvert = 1;
842 lockres->l_blocking = level;
845 mlog_exit(needs_downconvert);
846 return needs_downconvert;
850 * OCFS2_LOCK_PENDING and l_pending_gen.
852 * Why does OCFS2_LOCK_PENDING exist? To close a race between setting
853 * OCFS2_LOCK_BUSY and calling ocfs2_dlm_lock(). See ocfs2_unblock_lock()
854 * for more details on the race.
856 * OCFS2_LOCK_PENDING closes the race quite nicely. However, it introduces
857 * a race on itself. In o2dlm, we can get the ast before ocfs2_dlm_lock()
858 * returns. The ast clears OCFS2_LOCK_BUSY, and must therefore clear
859 * OCFS2_LOCK_PENDING at the same time. When ocfs2_dlm_lock() returns,
860 * the caller is going to try to clear PENDING again. If nothing else is
861 * happening, __lockres_clear_pending() sees PENDING is unset and does
864 * But what if another path (eg downconvert thread) has just started a
865 * new locking action? The other path has re-set PENDING. Our path
866 * cannot clear PENDING, because that will re-open the original race
872 * ocfs2_cluster_lock()
877 * ocfs2_locking_ast() ocfs2_downconvert_thread()
878 * clear PENDING ocfs2_unblock_lock()
881 * ocfs2_prepare_downconvert()
891 * So as you can see, we now have a window where l_lock is not held,
892 * PENDING is not set, and ocfs2_dlm_lock() has not been called.
894 * The core problem is that ocfs2_cluster_lock() has cleared the PENDING
895 * set by ocfs2_prepare_downconvert(). That wasn't nice.
897 * To solve this we introduce l_pending_gen. A call to
898 * lockres_clear_pending() will only do so when it is passed a generation
899 * number that matches the lockres. lockres_set_pending() will return the
900 * current generation number. When ocfs2_cluster_lock() goes to clear
901 * PENDING, it passes the generation it got from set_pending(). In our
902 * example above, the generation numbers will *not* match. Thus,
903 * ocfs2_cluster_lock() will not clear the PENDING set by
904 * ocfs2_prepare_downconvert().
907 /* Unlocked version for ocfs2_locking_ast() */
908 static void __lockres_clear_pending(struct ocfs2_lock_res *lockres,
909 unsigned int generation,
910 struct ocfs2_super *osb)
912 assert_spin_locked(&lockres->l_lock);
915 * The ast and locking functions can race us here. The winner
916 * will clear pending, the loser will not.
918 if (!(lockres->l_flags & OCFS2_LOCK_PENDING) ||
919 (lockres->l_pending_gen != generation))
922 lockres_clear_flags(lockres, OCFS2_LOCK_PENDING);
923 lockres->l_pending_gen++;
926 * The downconvert thread may have skipped us because we
927 * were PENDING. Wake it up.
929 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
930 ocfs2_wake_downconvert_thread(osb);
933 /* Locked version for callers of ocfs2_dlm_lock() */
934 static void lockres_clear_pending(struct ocfs2_lock_res *lockres,
935 unsigned int generation,
936 struct ocfs2_super *osb)
940 spin_lock_irqsave(&lockres->l_lock, flags);
941 __lockres_clear_pending(lockres, generation, osb);
942 spin_unlock_irqrestore(&lockres->l_lock, flags);
945 static unsigned int lockres_set_pending(struct ocfs2_lock_res *lockres)
947 assert_spin_locked(&lockres->l_lock);
948 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
950 lockres_or_flags(lockres, OCFS2_LOCK_PENDING);
952 return lockres->l_pending_gen;
956 static void ocfs2_blocking_ast(void *opaque, int level)
958 struct ocfs2_lock_res *lockres = opaque;
959 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
960 int needs_downconvert;
963 BUG_ON(level <= DLM_LOCK_NL);
965 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
966 lockres->l_name, level, lockres->l_level,
967 ocfs2_lock_type_string(lockres->l_type));
970 * We can skip the bast for locks which don't enable caching -
971 * they'll be dropped at the earliest possible time anyway.
973 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
976 spin_lock_irqsave(&lockres->l_lock, flags);
977 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
978 if (needs_downconvert)
979 ocfs2_schedule_blocked_lock(osb, lockres);
980 spin_unlock_irqrestore(&lockres->l_lock, flags);
982 wake_up(&lockres->l_event);
984 ocfs2_wake_downconvert_thread(osb);
987 static void ocfs2_locking_ast(void *opaque)
989 struct ocfs2_lock_res *lockres = opaque;
990 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
994 spin_lock_irqsave(&lockres->l_lock, flags);
996 status = ocfs2_dlm_lock_status(&lockres->l_lksb);
998 if (status == -EAGAIN) {
999 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1004 mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
1005 lockres->l_name, status);
1006 spin_unlock_irqrestore(&lockres->l_lock, flags);
1010 switch(lockres->l_action) {
1011 case OCFS2_AST_ATTACH:
1012 ocfs2_generic_handle_attach_action(lockres);
1013 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
1015 case OCFS2_AST_CONVERT:
1016 ocfs2_generic_handle_convert_action(lockres);
1018 case OCFS2_AST_DOWNCONVERT:
1019 ocfs2_generic_handle_downconvert_action(lockres);
1022 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
1023 "lockres flags = 0x%lx, unlock action: %u\n",
1024 lockres->l_name, lockres->l_action, lockres->l_flags,
1025 lockres->l_unlock_action);
1029 /* set it to something invalid so if we get called again we
1031 lockres->l_action = OCFS2_AST_INVALID;
1033 /* Did we try to cancel this lock? Clear that state */
1034 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT)
1035 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1038 * We may have beaten the locking functions here. We certainly
1039 * know that dlm_lock() has been called :-)
1040 * Because we can't have two lock calls in flight at once, we
1041 * can use lockres->l_pending_gen.
1043 __lockres_clear_pending(lockres, lockres->l_pending_gen, osb);
1045 wake_up(&lockres->l_event);
1046 spin_unlock_irqrestore(&lockres->l_lock, flags);
1049 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
1052 unsigned long flags;
1055 spin_lock_irqsave(&lockres->l_lock, flags);
1056 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1058 lockres->l_action = OCFS2_AST_INVALID;
1060 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1061 spin_unlock_irqrestore(&lockres->l_lock, flags);
1063 wake_up(&lockres->l_event);
1067 /* Note: If we detect another process working on the lock (i.e.,
1068 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
1069 * to do the right thing in that case.
1071 static int ocfs2_lock_create(struct ocfs2_super *osb,
1072 struct ocfs2_lock_res *lockres,
1077 unsigned long flags;
1082 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
1085 spin_lock_irqsave(&lockres->l_lock, flags);
1086 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
1087 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
1088 spin_unlock_irqrestore(&lockres->l_lock, flags);
1092 lockres->l_action = OCFS2_AST_ATTACH;
1093 lockres->l_requested = level;
1094 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1095 gen = lockres_set_pending(lockres);
1096 spin_unlock_irqrestore(&lockres->l_lock, flags);
1098 ret = ocfs2_dlm_lock(osb->cconn,
1103 OCFS2_LOCK_ID_MAX_LEN - 1,
1105 lockres_clear_pending(lockres, gen, osb);
1107 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
1108 ocfs2_recover_from_dlm_error(lockres, 1);
1111 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
1118 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
1121 unsigned long flags;
1124 spin_lock_irqsave(&lockres->l_lock, flags);
1125 ret = lockres->l_flags & flag;
1126 spin_unlock_irqrestore(&lockres->l_lock, flags);
1131 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
1134 wait_event(lockres->l_event,
1135 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
1138 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
1141 wait_event(lockres->l_event,
1142 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
1145 /* predict what lock level we'll be dropping down to on behalf
1146 * of another node, and return true if the currently wanted
1147 * level will be compatible with it. */
1148 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
1151 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
1153 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
1156 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
1158 INIT_LIST_HEAD(&mw->mw_item);
1159 init_completion(&mw->mw_complete);
1160 ocfs2_init_start_time(mw);
1163 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
1165 wait_for_completion(&mw->mw_complete);
1166 /* Re-arm the completion in case we want to wait on it again */
1167 INIT_COMPLETION(mw->mw_complete);
1168 return mw->mw_status;
1171 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
1172 struct ocfs2_mask_waiter *mw,
1176 BUG_ON(!list_empty(&mw->mw_item));
1178 assert_spin_locked(&lockres->l_lock);
1180 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
1185 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
1186 * if the mask still hadn't reached its goal */
1187 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
1188 struct ocfs2_mask_waiter *mw)
1190 unsigned long flags;
1193 spin_lock_irqsave(&lockres->l_lock, flags);
1194 if (!list_empty(&mw->mw_item)) {
1195 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
1198 list_del_init(&mw->mw_item);
1199 init_completion(&mw->mw_complete);
1201 spin_unlock_irqrestore(&lockres->l_lock, flags);
1207 static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
1208 struct ocfs2_lock_res *lockres)
1212 ret = wait_for_completion_interruptible(&mw->mw_complete);
1214 lockres_remove_mask_waiter(lockres, mw);
1216 ret = mw->mw_status;
1217 /* Re-arm the completion in case we want to wait on it again */
1218 INIT_COMPLETION(mw->mw_complete);
1222 static int ocfs2_cluster_lock(struct ocfs2_super *osb,
1223 struct ocfs2_lock_res *lockres,
1228 struct ocfs2_mask_waiter mw;
1229 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
1230 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1231 unsigned long flags;
1233 int noqueue_attempted = 0;
1237 ocfs2_init_mask_waiter(&mw);
1239 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
1240 lkm_flags |= DLM_LKF_VALBLK;
1245 if (catch_signals && signal_pending(current)) {
1250 spin_lock_irqsave(&lockres->l_lock, flags);
1252 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1253 "Cluster lock called on freeing lockres %s! flags "
1254 "0x%lx\n", lockres->l_name, lockres->l_flags);
1256 /* We only compare against the currently granted level
1257 * here. If the lock is blocked waiting on a downconvert,
1258 * we'll get caught below. */
1259 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1260 level > lockres->l_level) {
1261 /* is someone sitting in dlm_lock? If so, wait on
1263 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1268 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1269 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1270 /* is the lock is currently blocked on behalf of
1272 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1277 if (level > lockres->l_level) {
1278 if (noqueue_attempted > 0) {
1282 if (lkm_flags & DLM_LKF_NOQUEUE)
1283 noqueue_attempted = 1;
1285 if (lockres->l_action != OCFS2_AST_INVALID)
1286 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1287 lockres->l_name, lockres->l_action);
1289 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1290 lockres->l_action = OCFS2_AST_ATTACH;
1291 lkm_flags &= ~DLM_LKF_CONVERT;
1293 lockres->l_action = OCFS2_AST_CONVERT;
1294 lkm_flags |= DLM_LKF_CONVERT;
1297 lockres->l_requested = level;
1298 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1299 gen = lockres_set_pending(lockres);
1300 spin_unlock_irqrestore(&lockres->l_lock, flags);
1302 BUG_ON(level == DLM_LOCK_IV);
1303 BUG_ON(level == DLM_LOCK_NL);
1305 mlog(0, "lock %s, convert from %d to level = %d\n",
1306 lockres->l_name, lockres->l_level, level);
1308 /* call dlm_lock to upgrade lock now */
1309 ret = ocfs2_dlm_lock(osb->cconn,
1314 OCFS2_LOCK_ID_MAX_LEN - 1,
1316 lockres_clear_pending(lockres, gen, osb);
1318 if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
1320 ocfs2_log_dlm_error("ocfs2_dlm_lock",
1323 ocfs2_recover_from_dlm_error(lockres, 1);
1327 mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n",
1330 /* At this point we've gone inside the dlm and need to
1331 * complete our work regardless. */
1334 /* wait for busy to clear and carry on */
1338 /* Ok, if we get here then we're good to go. */
1339 ocfs2_inc_holders(lockres, level);
1343 spin_unlock_irqrestore(&lockres->l_lock, flags);
1346 * This is helping work around a lock inversion between the page lock
1347 * and dlm locks. One path holds the page lock while calling aops
1348 * which block acquiring dlm locks. The voting thread holds dlm
1349 * locks while acquiring page locks while down converting data locks.
1350 * This block is helping an aop path notice the inversion and back
1351 * off to unlock its page lock before trying the dlm lock again.
1353 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1354 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1356 if (lockres_remove_mask_waiter(lockres, &mw))
1362 ret = ocfs2_wait_for_mask(&mw);
1367 ocfs2_update_lock_stats(lockres, level, &mw, ret);
1373 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1374 struct ocfs2_lock_res *lockres,
1377 unsigned long flags;
1380 spin_lock_irqsave(&lockres->l_lock, flags);
1381 ocfs2_dec_holders(lockres, level);
1382 ocfs2_downconvert_on_unlock(osb, lockres);
1383 spin_unlock_irqrestore(&lockres->l_lock, flags);
1387 static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1388 struct ocfs2_lock_res *lockres,
1392 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1393 unsigned long flags;
1394 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
1396 spin_lock_irqsave(&lockres->l_lock, flags);
1397 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1398 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1399 spin_unlock_irqrestore(&lockres->l_lock, flags);
1401 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
1404 /* Grants us an EX lock on the data and metadata resources, skipping
1405 * the normal cluster directory lookup. Use this ONLY on newly created
1406 * inodes which other nodes can't possibly see, and which haven't been
1407 * hashed in the inode hash yet. This can give us a good performance
1408 * increase as it'll skip the network broadcast normally associated
1409 * with creating a new lock resource. */
1410 int ocfs2_create_new_inode_locks(struct inode *inode)
1413 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1416 BUG_ON(!ocfs2_inode_is_new(inode));
1420 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1422 /* NOTE: That we don't increment any of the holder counts, nor
1423 * do we add anything to a journal handle. Since this is
1424 * supposed to be a new inode which the cluster doesn't know
1425 * about yet, there is no need to. As far as the LVB handling
1426 * is concerned, this is basically like acquiring an EX lock
1427 * on a resource which has an invalid one -- we'll set it
1428 * valid when we release the EX. */
1430 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
1437 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
1438 * don't use a generation in their lock names.
1440 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
1446 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1457 int ocfs2_rw_lock(struct inode *inode, int write)
1460 struct ocfs2_lock_res *lockres;
1461 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1467 mlog(0, "inode %llu take %s RW lock\n",
1468 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1469 write ? "EXMODE" : "PRMODE");
1471 if (ocfs2_mount_local(osb))
1474 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1476 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1478 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1487 void ocfs2_rw_unlock(struct inode *inode, int write)
1489 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1490 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1491 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1495 mlog(0, "inode %llu drop %s RW lock\n",
1496 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1497 write ? "EXMODE" : "PRMODE");
1499 if (!ocfs2_mount_local(osb))
1500 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1506 * ocfs2_open_lock always get PR mode lock.
1508 int ocfs2_open_lock(struct inode *inode)
1511 struct ocfs2_lock_res *lockres;
1512 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1518 mlog(0, "inode %llu take PRMODE open lock\n",
1519 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1521 if (ocfs2_mount_local(osb))
1524 lockres = &OCFS2_I(inode)->ip_open_lockres;
1526 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1536 int ocfs2_try_open_lock(struct inode *inode, int write)
1538 int status = 0, level;
1539 struct ocfs2_lock_res *lockres;
1540 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1546 mlog(0, "inode %llu try to take %s open lock\n",
1547 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1548 write ? "EXMODE" : "PRMODE");
1550 if (ocfs2_mount_local(osb))
1553 lockres = &OCFS2_I(inode)->ip_open_lockres;
1555 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1558 * The file system may already holding a PRMODE/EXMODE open lock.
1559 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
1560 * other nodes and the -EAGAIN will indicate to the caller that
1561 * this inode is still in use.
1563 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1564 level, DLM_LKF_NOQUEUE, 0);
1572 * ocfs2_open_unlock unlock PR and EX mode open locks.
1574 void ocfs2_open_unlock(struct inode *inode)
1576 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1577 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1581 mlog(0, "inode %llu drop open lock\n",
1582 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1584 if (ocfs2_mount_local(osb))
1587 if(lockres->l_ro_holders)
1588 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1590 if(lockres->l_ex_holders)
1591 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1598 static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1602 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1603 unsigned long flags;
1604 struct ocfs2_mask_waiter mw;
1606 ocfs2_init_mask_waiter(&mw);
1609 spin_lock_irqsave(&lockres->l_lock, flags);
1610 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1611 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1613 spin_unlock_irqrestore(&lockres->l_lock, flags);
1614 ret = ocfs2_cancel_convert(osb, lockres);
1621 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1622 spin_unlock_irqrestore(&lockres->l_lock, flags);
1624 ocfs2_wait_for_mask(&mw);
1630 * We may still have gotten the lock, in which case there's no
1631 * point to restarting the syscall.
1633 if (lockres->l_level == level)
1636 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1637 lockres->l_flags, lockres->l_level, lockres->l_action);
1639 spin_unlock_irqrestore(&lockres->l_lock, flags);
1646 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1647 * flock() calls. The locking approach this requires is sufficiently
1648 * different from all other cluster lock types that we implement a
1649 * seperate path to the "low-level" dlm calls. In particular:
1651 * - No optimization of lock levels is done - we take at exactly
1652 * what's been requested.
1654 * - No lock caching is employed. We immediately downconvert to
1655 * no-lock at unlock time. This also means flock locks never go on
1656 * the blocking list).
1658 * - Since userspace can trivially deadlock itself with flock, we make
1659 * sure to allow cancellation of a misbehaving applications flock()
1662 * - Access to any flock lockres doesn't require concurrency, so we
1663 * can simplify the code by requiring the caller to guarantee
1664 * serialization of dlmglue flock calls.
1666 int ocfs2_file_lock(struct file *file, int ex, int trylock)
1668 int ret, level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1669 unsigned int lkm_flags = trylock ? DLM_LKF_NOQUEUE : 0;
1670 unsigned long flags;
1671 struct ocfs2_file_private *fp = file->private_data;
1672 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1673 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1674 struct ocfs2_mask_waiter mw;
1676 ocfs2_init_mask_waiter(&mw);
1678 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
1679 (lockres->l_level > DLM_LOCK_NL)) {
1681 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1682 "level: %u\n", lockres->l_name, lockres->l_flags,
1687 spin_lock_irqsave(&lockres->l_lock, flags);
1688 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1689 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1690 spin_unlock_irqrestore(&lockres->l_lock, flags);
1693 * Get the lock at NLMODE to start - that way we
1694 * can cancel the upconvert request if need be.
1696 ret = ocfs2_lock_create(osb, lockres, DLM_LOCK_NL, 0);
1702 ret = ocfs2_wait_for_mask(&mw);
1707 spin_lock_irqsave(&lockres->l_lock, flags);
1710 lockres->l_action = OCFS2_AST_CONVERT;
1711 lkm_flags |= DLM_LKF_CONVERT;
1712 lockres->l_requested = level;
1713 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1715 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1716 spin_unlock_irqrestore(&lockres->l_lock, flags);
1718 ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
1719 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1,
1722 if (!trylock || (ret != -EAGAIN)) {
1723 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
1727 ocfs2_recover_from_dlm_error(lockres, 1);
1728 lockres_remove_mask_waiter(lockres, &mw);
1732 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1733 if (ret == -ERESTARTSYS) {
1735 * Userspace can cause deadlock itself with
1736 * flock(). Current behavior locally is to allow the
1737 * deadlock, but abort the system call if a signal is
1738 * received. We follow this example, otherwise a
1739 * poorly written program could sit in kernel until
1742 * Handling this is a bit more complicated for Ocfs2
1743 * though. We can't exit this function with an
1744 * outstanding lock request, so a cancel convert is
1745 * required. We intentionally overwrite 'ret' - if the
1746 * cancel fails and the lock was granted, it's easier
1747 * to just bubble sucess back up to the user.
1749 ret = ocfs2_flock_handle_signal(lockres, level);
1750 } else if (!ret && (level > lockres->l_level)) {
1751 /* Trylock failed asynchronously */
1758 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1759 lockres->l_name, ex, trylock, ret);
1763 void ocfs2_file_unlock(struct file *file)
1767 unsigned long flags;
1768 struct ocfs2_file_private *fp = file->private_data;
1769 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1770 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1771 struct ocfs2_mask_waiter mw;
1773 ocfs2_init_mask_waiter(&mw);
1775 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
1778 if (lockres->l_level == DLM_LOCK_NL)
1781 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1782 lockres->l_name, lockres->l_flags, lockres->l_level,
1785 spin_lock_irqsave(&lockres->l_lock, flags);
1787 * Fake a blocking ast for the downconvert code.
1789 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
1790 lockres->l_blocking = DLM_LOCK_EX;
1792 gen = ocfs2_prepare_downconvert(lockres, DLM_LOCK_NL);
1793 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1794 spin_unlock_irqrestore(&lockres->l_lock, flags);
1796 ret = ocfs2_downconvert_lock(osb, lockres, DLM_LOCK_NL, 0, gen);
1802 ret = ocfs2_wait_for_mask(&mw);
1807 static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1808 struct ocfs2_lock_res *lockres)
1814 /* If we know that another node is waiting on our lock, kick
1815 * the downconvert thread * pre-emptively when we reach a release
1817 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1818 switch(lockres->l_blocking) {
1820 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1824 if (!lockres->l_ex_holders)
1833 ocfs2_wake_downconvert_thread(osb);
1838 #define OCFS2_SEC_BITS 34
1839 #define OCFS2_SEC_SHIFT (64 - 34)
1840 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1842 /* LVB only has room for 64 bits of time here so we pack it for
1844 static u64 ocfs2_pack_timespec(struct timespec *spec)
1847 u64 sec = spec->tv_sec;
1848 u32 nsec = spec->tv_nsec;
1850 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1855 /* Call this with the lockres locked. I am reasonably sure we don't
1856 * need ip_lock in this function as anyone who would be changing those
1857 * values is supposed to be blocked in ocfs2_inode_lock right now. */
1858 static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1860 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1861 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1862 struct ocfs2_meta_lvb *lvb;
1866 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
1869 * Invalidate the LVB of a deleted inode - this way other
1870 * nodes are forced to go to disk and discover the new inode
1873 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1874 lvb->lvb_version = 0;
1878 lvb->lvb_version = OCFS2_LVB_VERSION;
1879 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1880 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1881 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1882 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1883 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1884 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1885 lvb->lvb_iatime_packed =
1886 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1887 lvb->lvb_ictime_packed =
1888 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1889 lvb->lvb_imtime_packed =
1890 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1891 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
1892 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
1893 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
1896 mlog_meta_lvb(0, lockres);
1901 static void ocfs2_unpack_timespec(struct timespec *spec,
1904 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1905 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1908 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1910 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1911 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1912 struct ocfs2_meta_lvb *lvb;
1916 mlog_meta_lvb(0, lockres);
1918 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
1920 /* We're safe here without the lockres lock... */
1921 spin_lock(&oi->ip_lock);
1922 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1923 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1925 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1926 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
1927 ocfs2_set_inode_flags(inode);
1929 /* fast-symlinks are a special case */
1930 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1931 inode->i_blocks = 0;
1933 inode->i_blocks = ocfs2_inode_sector_count(inode);
1935 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1936 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1937 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1938 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1939 ocfs2_unpack_timespec(&inode->i_atime,
1940 be64_to_cpu(lvb->lvb_iatime_packed));
1941 ocfs2_unpack_timespec(&inode->i_mtime,
1942 be64_to_cpu(lvb->lvb_imtime_packed));
1943 ocfs2_unpack_timespec(&inode->i_ctime,
1944 be64_to_cpu(lvb->lvb_ictime_packed));
1945 spin_unlock(&oi->ip_lock);
1950 static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1951 struct ocfs2_lock_res *lockres)
1953 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
1955 if (lvb->lvb_version == OCFS2_LVB_VERSION
1956 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
1961 /* Determine whether a lock resource needs to be refreshed, and
1962 * arbitrate who gets to refresh it.
1964 * 0 means no refresh needed.
1966 * > 0 means you need to refresh this and you MUST call
1967 * ocfs2_complete_lock_res_refresh afterwards. */
1968 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1970 unsigned long flags;
1976 spin_lock_irqsave(&lockres->l_lock, flags);
1977 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1978 spin_unlock_irqrestore(&lockres->l_lock, flags);
1982 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1983 spin_unlock_irqrestore(&lockres->l_lock, flags);
1985 ocfs2_wait_on_refreshing_lock(lockres);
1989 /* Ok, I'll be the one to refresh this lock. */
1990 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1991 spin_unlock_irqrestore(&lockres->l_lock, flags);
1999 /* If status is non zero, I'll mark it as not being in refresh
2000 * anymroe, but i won't clear the needs refresh flag. */
2001 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
2004 unsigned long flags;
2007 spin_lock_irqsave(&lockres->l_lock, flags);
2008 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
2010 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
2011 spin_unlock_irqrestore(&lockres->l_lock, flags);
2013 wake_up(&lockres->l_event);
2018 /* may or may not return a bh if it went to disk. */
2019 static int ocfs2_inode_lock_update(struct inode *inode,
2020 struct buffer_head **bh)
2023 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2024 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
2025 struct ocfs2_dinode *fe;
2026 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2030 if (ocfs2_mount_local(osb))
2033 spin_lock(&oi->ip_lock);
2034 if (oi->ip_flags & OCFS2_INODE_DELETED) {
2035 mlog(0, "Orphaned inode %llu was deleted while we "
2036 "were waiting on a lock. ip_flags = 0x%x\n",
2037 (unsigned long long)oi->ip_blkno, oi->ip_flags);
2038 spin_unlock(&oi->ip_lock);
2042 spin_unlock(&oi->ip_lock);
2044 if (!ocfs2_should_refresh_lock_res(lockres))
2047 /* This will discard any caching information we might have had
2048 * for the inode metadata. */
2049 ocfs2_metadata_cache_purge(inode);
2051 ocfs2_extent_map_trunc(inode, 0);
2053 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
2054 mlog(0, "Trusting LVB on inode %llu\n",
2055 (unsigned long long)oi->ip_blkno);
2056 ocfs2_refresh_inode_from_lvb(inode);
2058 /* Boo, we have to go to disk. */
2059 /* read bh, cast, ocfs2_refresh_inode */
2060 status = ocfs2_read_inode_block(inode, bh);
2065 fe = (struct ocfs2_dinode *) (*bh)->b_data;
2067 /* This is a good chance to make sure we're not
2068 * locking an invalid object. ocfs2_read_inode_block()
2069 * already checked that the inode block is sane.
2071 * We bug on a stale inode here because we checked
2072 * above whether it was wiped from disk. The wiping
2073 * node provides a guarantee that we receive that
2074 * message and can mark the inode before dropping any
2075 * locks associated with it. */
2076 mlog_bug_on_msg(inode->i_generation !=
2077 le32_to_cpu(fe->i_generation),
2078 "Invalid dinode %llu disk generation: %u "
2079 "inode->i_generation: %u\n",
2080 (unsigned long long)oi->ip_blkno,
2081 le32_to_cpu(fe->i_generation),
2082 inode->i_generation);
2083 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
2084 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
2085 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
2086 (unsigned long long)oi->ip_blkno,
2087 (unsigned long long)le64_to_cpu(fe->i_dtime),
2088 le32_to_cpu(fe->i_flags));
2090 ocfs2_refresh_inode(inode, fe);
2091 ocfs2_track_lock_refresh(lockres);
2096 ocfs2_complete_lock_res_refresh(lockres, status);
2102 static int ocfs2_assign_bh(struct inode *inode,
2103 struct buffer_head **ret_bh,
2104 struct buffer_head *passed_bh)
2109 /* Ok, the update went to disk for us, use the
2111 *ret_bh = passed_bh;
2117 status = ocfs2_read_inode_block(inode, ret_bh);
2125 * returns < 0 error if the callback will never be called, otherwise
2126 * the result of the lock will be communicated via the callback.
2128 int ocfs2_inode_lock_full(struct inode *inode,
2129 struct buffer_head **ret_bh,
2133 int status, level, acquired;
2135 struct ocfs2_lock_res *lockres = NULL;
2136 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2137 struct buffer_head *local_bh = NULL;
2143 mlog(0, "inode %llu, take %s META lock\n",
2144 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2145 ex ? "EXMODE" : "PRMODE");
2149 /* We'll allow faking a readonly metadata lock for
2151 if (ocfs2_is_hard_readonly(osb)) {
2157 if (ocfs2_mount_local(osb))
2160 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
2161 ocfs2_wait_for_recovery(osb);
2163 lockres = &OCFS2_I(inode)->ip_inode_lockres;
2164 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2166 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
2167 dlm_flags |= DLM_LKF_NOQUEUE;
2169 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
2171 if (status != -EAGAIN && status != -EIOCBRETRY)
2176 /* Notify the error cleanup path to drop the cluster lock. */
2179 /* We wait twice because a node may have died while we were in
2180 * the lower dlm layers. The second time though, we've
2181 * committed to owning this lock so we don't allow signals to
2182 * abort the operation. */
2183 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
2184 ocfs2_wait_for_recovery(osb);
2188 * We only see this flag if we're being called from
2189 * ocfs2_read_locked_inode(). It means we're locking an inode
2190 * which hasn't been populated yet, so clear the refresh flag
2191 * and let the caller handle it.
2193 if (inode->i_state & I_NEW) {
2196 ocfs2_complete_lock_res_refresh(lockres, 0);
2200 /* This is fun. The caller may want a bh back, or it may
2201 * not. ocfs2_inode_lock_update definitely wants one in, but
2202 * may or may not read one, depending on what's in the
2203 * LVB. The result of all of this is that we've *only* gone to
2204 * disk if we have to, so the complexity is worthwhile. */
2205 status = ocfs2_inode_lock_update(inode, &local_bh);
2207 if (status != -ENOENT)
2213 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
2222 if (ret_bh && (*ret_bh)) {
2227 ocfs2_inode_unlock(inode, ex);
2238 * This is working around a lock inversion between tasks acquiring DLM
2239 * locks while holding a page lock and the downconvert thread which
2240 * blocks dlm lock acquiry while acquiring page locks.
2242 * ** These _with_page variantes are only intended to be called from aop
2243 * methods that hold page locks and return a very specific *positive* error
2244 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2246 * The DLM is called such that it returns -EAGAIN if it would have
2247 * blocked waiting for the downconvert thread. In that case we unlock
2248 * our page so the downconvert thread can make progress. Once we've
2249 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2250 * that called us can bubble that back up into the VFS who will then
2251 * immediately retry the aop call.
2253 * We do a blocking lock and immediate unlock before returning, though, so that
2254 * the lock has a great chance of being cached on this node by the time the VFS
2255 * calls back to retry the aop. This has a potential to livelock as nodes
2256 * ping locks back and forth, but that's a risk we're willing to take to avoid
2257 * the lock inversion simply.
2259 int ocfs2_inode_lock_with_page(struct inode *inode,
2260 struct buffer_head **ret_bh,
2266 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
2267 if (ret == -EAGAIN) {
2269 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2270 ocfs2_inode_unlock(inode, ex);
2271 ret = AOP_TRUNCATED_PAGE;
2277 int ocfs2_inode_lock_atime(struct inode *inode,
2278 struct vfsmount *vfsmnt,
2284 ret = ocfs2_inode_lock(inode, NULL, 0);
2291 * If we should update atime, we will get EX lock,
2292 * otherwise we just get PR lock.
2294 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2295 struct buffer_head *bh = NULL;
2297 ocfs2_inode_unlock(inode, 0);
2298 ret = ocfs2_inode_lock(inode, &bh, 1);
2304 if (ocfs2_should_update_atime(inode, vfsmnt))
2305 ocfs2_update_inode_atime(inode, bh);
2315 void ocfs2_inode_unlock(struct inode *inode,
2318 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2319 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
2320 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2324 mlog(0, "inode %llu drop %s META lock\n",
2325 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2326 ex ? "EXMODE" : "PRMODE");
2328 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2329 !ocfs2_mount_local(osb))
2330 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
2335 int ocfs2_super_lock(struct ocfs2_super *osb,
2339 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2340 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2344 if (ocfs2_is_hard_readonly(osb))
2347 if (ocfs2_mount_local(osb))
2350 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2356 /* The super block lock path is really in the best position to
2357 * know when resources covered by the lock need to be
2358 * refreshed, so we do it here. Of course, making sense of
2359 * everything is up to the caller :) */
2360 status = ocfs2_should_refresh_lock_res(lockres);
2366 status = ocfs2_refresh_slot_info(osb);
2368 ocfs2_complete_lock_res_refresh(lockres, status);
2372 ocfs2_track_lock_refresh(lockres);
2379 void ocfs2_super_unlock(struct ocfs2_super *osb,
2382 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2383 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2385 if (!ocfs2_mount_local(osb))
2386 ocfs2_cluster_unlock(osb, lockres, level);
2389 int ocfs2_rename_lock(struct ocfs2_super *osb)
2392 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2394 if (ocfs2_is_hard_readonly(osb))
2397 if (ocfs2_mount_local(osb))
2400 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
2407 void ocfs2_rename_unlock(struct ocfs2_super *osb)
2409 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2411 if (!ocfs2_mount_local(osb))
2412 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2415 int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2418 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2419 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2420 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2424 if (ocfs2_is_hard_readonly(osb))
2427 if (ocfs2_mount_local(osb))
2430 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2437 void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2439 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2440 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2441 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2443 if (!ocfs2_mount_local(osb))
2444 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
2447 /* Reference counting of the dlm debug structure. We want this because
2448 * open references on the debug inodes can live on after a mount, so
2449 * we can't rely on the ocfs2_super to always exist. */
2450 static void ocfs2_dlm_debug_free(struct kref *kref)
2452 struct ocfs2_dlm_debug *dlm_debug;
2454 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2459 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2462 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2465 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2467 kref_get(&debug->d_refcnt);
2470 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2472 struct ocfs2_dlm_debug *dlm_debug;
2474 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2476 mlog_errno(-ENOMEM);
2480 kref_init(&dlm_debug->d_refcnt);
2481 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2482 dlm_debug->d_locking_state = NULL;
2487 /* Access to this is arbitrated for us via seq_file->sem. */
2488 struct ocfs2_dlm_seq_priv {
2489 struct ocfs2_dlm_debug *p_dlm_debug;
2490 struct ocfs2_lock_res p_iter_res;
2491 struct ocfs2_lock_res p_tmp_res;
2494 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2495 struct ocfs2_dlm_seq_priv *priv)
2497 struct ocfs2_lock_res *iter, *ret = NULL;
2498 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2500 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2502 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2503 /* discover the head of the list */
2504 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2505 mlog(0, "End of list found, %p\n", ret);
2509 /* We track our "dummy" iteration lockres' by a NULL
2511 if (iter->l_ops != NULL) {
2520 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2522 struct ocfs2_dlm_seq_priv *priv = m->private;
2523 struct ocfs2_lock_res *iter;
2525 spin_lock(&ocfs2_dlm_tracking_lock);
2526 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2528 /* Since lockres' have the lifetime of their container
2529 * (which can be inodes, ocfs2_supers, etc) we want to
2530 * copy this out to a temporary lockres while still
2531 * under the spinlock. Obviously after this we can't
2532 * trust any pointers on the copy returned, but that's
2533 * ok as the information we want isn't typically held
2535 priv->p_tmp_res = *iter;
2536 iter = &priv->p_tmp_res;
2538 spin_unlock(&ocfs2_dlm_tracking_lock);
2543 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2547 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2549 struct ocfs2_dlm_seq_priv *priv = m->private;
2550 struct ocfs2_lock_res *iter = v;
2551 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2553 spin_lock(&ocfs2_dlm_tracking_lock);
2554 iter = ocfs2_dlm_next_res(iter, priv);
2555 list_del_init(&dummy->l_debug_list);
2557 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2558 priv->p_tmp_res = *iter;
2559 iter = &priv->p_tmp_res;
2561 spin_unlock(&ocfs2_dlm_tracking_lock);
2566 /* So that debugfs.ocfs2 can determine which format is being used */
2567 #define OCFS2_DLM_DEBUG_STR_VERSION 2
2568 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2572 struct ocfs2_lock_res *lockres = v;
2577 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2579 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2580 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2582 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2584 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2586 seq_printf(m, "%d\t"
2597 lockres->l_unlock_action,
2598 lockres->l_ro_holders,
2599 lockres->l_ex_holders,
2600 lockres->l_requested,
2601 lockres->l_blocking);
2603 /* Dump the raw LVB */
2604 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2605 for(i = 0; i < DLM_LVB_LEN; i++)
2606 seq_printf(m, "0x%x\t", lvb[i]);
2608 #ifdef CONFIG_OCFS2_FS_STATS
2609 # define lock_num_prmode(_l) (_l)->l_lock_num_prmode
2610 # define lock_num_exmode(_l) (_l)->l_lock_num_exmode
2611 # define lock_num_prmode_failed(_l) (_l)->l_lock_num_prmode_failed
2612 # define lock_num_exmode_failed(_l) (_l)->l_lock_num_exmode_failed
2613 # define lock_total_prmode(_l) (_l)->l_lock_total_prmode
2614 # define lock_total_exmode(_l) (_l)->l_lock_total_exmode
2615 # define lock_max_prmode(_l) (_l)->l_lock_max_prmode
2616 # define lock_max_exmode(_l) (_l)->l_lock_max_exmode
2617 # define lock_refresh(_l) (_l)->l_lock_refresh
2619 # define lock_num_prmode(_l) (0ULL)
2620 # define lock_num_exmode(_l) (0ULL)
2621 # define lock_num_prmode_failed(_l) (0)
2622 # define lock_num_exmode_failed(_l) (0)
2623 # define lock_total_prmode(_l) (0ULL)
2624 # define lock_total_exmode(_l) (0ULL)
2625 # define lock_max_prmode(_l) (0)
2626 # define lock_max_exmode(_l) (0)
2627 # define lock_refresh(_l) (0)
2629 /* The following seq_print was added in version 2 of this output */
2630 seq_printf(m, "%llu\t"
2639 lock_num_prmode(lockres),
2640 lock_num_exmode(lockres),
2641 lock_num_prmode_failed(lockres),
2642 lock_num_exmode_failed(lockres),
2643 lock_total_prmode(lockres),
2644 lock_total_exmode(lockres),
2645 lock_max_prmode(lockres),
2646 lock_max_exmode(lockres),
2647 lock_refresh(lockres));
2650 seq_printf(m, "\n");
2654 static const struct seq_operations ocfs2_dlm_seq_ops = {
2655 .start = ocfs2_dlm_seq_start,
2656 .stop = ocfs2_dlm_seq_stop,
2657 .next = ocfs2_dlm_seq_next,
2658 .show = ocfs2_dlm_seq_show,
2661 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2663 struct seq_file *seq = (struct seq_file *) file->private_data;
2664 struct ocfs2_dlm_seq_priv *priv = seq->private;
2665 struct ocfs2_lock_res *res = &priv->p_iter_res;
2667 ocfs2_remove_lockres_tracking(res);
2668 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2669 return seq_release_private(inode, file);
2672 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2675 struct ocfs2_dlm_seq_priv *priv;
2676 struct seq_file *seq;
2677 struct ocfs2_super *osb;
2679 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2685 osb = inode->i_private;
2686 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2687 priv->p_dlm_debug = osb->osb_dlm_debug;
2688 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2690 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2697 seq = (struct seq_file *) file->private_data;
2698 seq->private = priv;
2700 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2707 static const struct file_operations ocfs2_dlm_debug_fops = {
2708 .open = ocfs2_dlm_debug_open,
2709 .release = ocfs2_dlm_debug_release,
2711 .llseek = seq_lseek,
2714 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2717 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2719 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2721 osb->osb_debug_root,
2723 &ocfs2_dlm_debug_fops);
2724 if (!dlm_debug->d_locking_state) {
2727 "Unable to create locking state debugfs file.\n");
2731 ocfs2_get_dlm_debug(dlm_debug);
2736 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2738 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2741 debugfs_remove(dlm_debug->d_locking_state);
2742 ocfs2_put_dlm_debug(dlm_debug);
2746 int ocfs2_dlm_init(struct ocfs2_super *osb)
2749 struct ocfs2_cluster_connection *conn = NULL;
2753 if (ocfs2_mount_local(osb)) {
2758 status = ocfs2_dlm_init_debug(osb);
2764 /* launch downconvert thread */
2765 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2766 if (IS_ERR(osb->dc_task)) {
2767 status = PTR_ERR(osb->dc_task);
2768 osb->dc_task = NULL;
2773 /* for now, uuid == domain */
2774 status = ocfs2_cluster_connect(osb->osb_cluster_stack,
2776 strlen(osb->uuid_str),
2777 ocfs2_do_node_down, osb,
2784 status = ocfs2_cluster_this_node(&osb->node_num);
2788 "could not find this host's node number\n");
2789 ocfs2_cluster_disconnect(conn, 0);
2794 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2795 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2802 ocfs2_dlm_shutdown_debug(osb);
2804 kthread_stop(osb->dc_task);
2811 void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
2816 ocfs2_drop_osb_locks(osb);
2819 * Now that we have dropped all locks and ocfs2_dismount_volume()
2820 * has disabled recovery, the DLM won't be talking to us. It's
2821 * safe to tear things down before disconnecting the cluster.
2825 kthread_stop(osb->dc_task);
2826 osb->dc_task = NULL;
2829 ocfs2_lock_res_free(&osb->osb_super_lockres);
2830 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2832 ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
2835 ocfs2_dlm_shutdown_debug(osb);
2840 static void ocfs2_unlock_ast(void *opaque, int error)
2842 struct ocfs2_lock_res *lockres = opaque;
2843 unsigned long flags;
2847 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2848 lockres->l_unlock_action);
2850 spin_lock_irqsave(&lockres->l_lock, flags);
2852 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
2853 "unlock_action %d\n", error, lockres->l_name,
2854 lockres->l_unlock_action);
2855 spin_unlock_irqrestore(&lockres->l_lock, flags);
2859 switch(lockres->l_unlock_action) {
2860 case OCFS2_UNLOCK_CANCEL_CONVERT:
2861 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2862 lockres->l_action = OCFS2_AST_INVALID;
2863 /* Downconvert thread may have requeued this lock, we
2864 * need to wake it. */
2865 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2866 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres));
2868 case OCFS2_UNLOCK_DROP_LOCK:
2869 lockres->l_level = DLM_LOCK_IV;
2875 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2876 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2877 wake_up(&lockres->l_event);
2878 spin_unlock_irqrestore(&lockres->l_lock, flags);
2883 static int ocfs2_drop_lock(struct ocfs2_super *osb,
2884 struct ocfs2_lock_res *lockres)
2887 unsigned long flags;
2890 /* We didn't get anywhere near actually using this lockres. */
2891 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2894 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
2895 lkm_flags |= DLM_LKF_VALBLK;
2897 spin_lock_irqsave(&lockres->l_lock, flags);
2899 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2900 "lockres %s, flags 0x%lx\n",
2901 lockres->l_name, lockres->l_flags);
2903 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2904 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2905 "%u, unlock_action = %u\n",
2906 lockres->l_name, lockres->l_flags, lockres->l_action,
2907 lockres->l_unlock_action);
2909 spin_unlock_irqrestore(&lockres->l_lock, flags);
2911 /* XXX: Today we just wait on any busy
2912 * locks... Perhaps we need to cancel converts in the
2914 ocfs2_wait_on_busy_lock(lockres);
2916 spin_lock_irqsave(&lockres->l_lock, flags);
2919 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2920 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2921 lockres->l_level == DLM_LOCK_EX &&
2922 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2923 lockres->l_ops->set_lvb(lockres);
2926 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2927 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2929 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2930 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2932 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2933 spin_unlock_irqrestore(&lockres->l_lock, flags);
2937 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2939 /* make sure we never get here while waiting for an ast to
2941 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2943 /* is this necessary? */
2944 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2945 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2946 spin_unlock_irqrestore(&lockres->l_lock, flags);
2948 mlog(0, "lock %s\n", lockres->l_name);
2950 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags,
2953 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
2954 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2955 ocfs2_dlm_dump_lksb(&lockres->l_lksb);
2958 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
2961 ocfs2_wait_on_busy_lock(lockres);
2967 /* Mark the lockres as being dropped. It will no longer be
2968 * queued if blocking, but we still may have to wait on it
2969 * being dequeued from the downconvert thread before we can consider
2972 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2973 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2976 struct ocfs2_mask_waiter mw;
2977 unsigned long flags;
2979 ocfs2_init_mask_waiter(&mw);
2981 spin_lock_irqsave(&lockres->l_lock, flags);
2982 lockres->l_flags |= OCFS2_LOCK_FREEING;
2983 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2984 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2985 spin_unlock_irqrestore(&lockres->l_lock, flags);
2987 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2989 status = ocfs2_wait_for_mask(&mw);
2993 spin_lock_irqsave(&lockres->l_lock, flags);
2995 spin_unlock_irqrestore(&lockres->l_lock, flags);
2998 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2999 struct ocfs2_lock_res *lockres)
3003 ocfs2_mark_lockres_freeing(lockres);
3004 ret = ocfs2_drop_lock(osb, lockres);
3009 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
3011 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
3012 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
3015 int ocfs2_drop_inode_locks(struct inode *inode)
3021 /* No need to call ocfs2_mark_lockres_freeing here -
3022 * ocfs2_clear_inode has done it for us. */
3024 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
3025 &OCFS2_I(inode)->ip_open_lockres);
3031 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
3032 &OCFS2_I(inode)->ip_inode_lockres);
3035 if (err < 0 && !status)
3038 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
3039 &OCFS2_I(inode)->ip_rw_lockres);
3042 if (err < 0 && !status)
3049 static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
3052 assert_spin_locked(&lockres->l_lock);
3054 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
3056 if (lockres->l_level <= new_level) {
3057 mlog(ML_ERROR, "lockres->l_level (%d) <= new_level (%d)\n",
3058 lockres->l_level, new_level);
3062 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
3063 lockres->l_name, new_level, lockres->l_blocking);
3065 lockres->l_action = OCFS2_AST_DOWNCONVERT;
3066 lockres->l_requested = new_level;
3067 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3068 return lockres_set_pending(lockres);
3071 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
3072 struct ocfs2_lock_res *lockres,
3075 unsigned int generation)
3078 u32 dlm_flags = DLM_LKF_CONVERT;
3083 dlm_flags |= DLM_LKF_VALBLK;
3085 ret = ocfs2_dlm_lock(osb->cconn,
3090 OCFS2_LOCK_ID_MAX_LEN - 1,
3092 lockres_clear_pending(lockres, generation, osb);
3094 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
3095 ocfs2_recover_from_dlm_error(lockres, 1);
3105 /* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
3106 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
3107 struct ocfs2_lock_res *lockres)
3109 assert_spin_locked(&lockres->l_lock);
3112 mlog(0, "lock %s\n", lockres->l_name);
3114 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
3115 /* If we're already trying to cancel a lock conversion
3116 * then just drop the spinlock and allow the caller to
3117 * requeue this lock. */
3119 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
3123 /* were we in a convert when we got the bast fire? */
3124 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
3125 lockres->l_action != OCFS2_AST_DOWNCONVERT);
3126 /* set things up for the unlockast to know to just
3127 * clear out the ast_action and unset busy, etc. */
3128 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
3130 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
3131 "lock %s, invalid flags: 0x%lx\n",
3132 lockres->l_name, lockres->l_flags);
3137 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
3138 struct ocfs2_lock_res *lockres)
3143 mlog(0, "lock %s\n", lockres->l_name);
3145 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
3146 DLM_LKF_CANCEL, lockres);
3148 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
3149 ocfs2_recover_from_dlm_error(lockres, 0);
3152 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres->l_name);
3158 static int ocfs2_unblock_lock(struct ocfs2_super *osb,
3159 struct ocfs2_lock_res *lockres,
3160 struct ocfs2_unblock_ctl *ctl)
3162 unsigned long flags;
3171 spin_lock_irqsave(&lockres->l_lock, flags);
3173 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
3176 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
3178 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3179 * exists entirely for one reason - another thread has set
3180 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3182 * If we do ocfs2_cancel_convert() before the other thread
3183 * calls dlm_lock(), our cancel will do nothing. We will
3184 * get no ast, and we will have no way of knowing the
3185 * cancel failed. Meanwhile, the other thread will call
3186 * into dlm_lock() and wait...forever.
3188 * Why forever? Because another node has asked for the
3189 * lock first; that's why we're here in unblock_lock().
3191 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3192 * set, we just requeue the unblock. Only when the other
3193 * thread has called dlm_lock() and cleared PENDING will
3194 * we then cancel their request.
3196 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3197 * at the same time they set OCFS2_DLM_BUSY. They must
3198 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3200 if (lockres->l_flags & OCFS2_LOCK_PENDING)
3204 ret = ocfs2_prepare_cancel_convert(osb, lockres);
3205 spin_unlock_irqrestore(&lockres->l_lock, flags);
3207 ret = ocfs2_cancel_convert(osb, lockres);
3214 /* if we're blocking an exclusive and we have *any* holders,
3216 if ((lockres->l_blocking == DLM_LOCK_EX)
3217 && (lockres->l_ex_holders || lockres->l_ro_holders))
3220 /* If it's a PR we're blocking, then only
3221 * requeue if we've got any EX holders */
3222 if (lockres->l_blocking == DLM_LOCK_PR &&
3223 lockres->l_ex_holders)
3227 * Can we get a lock in this state if the holder counts are
3228 * zero? The meta data unblock code used to check this.
3230 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
3231 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
3234 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
3236 if (lockres->l_ops->check_downconvert
3237 && !lockres->l_ops->check_downconvert(lockres, new_level))
3240 /* If we get here, then we know that there are no more
3241 * incompatible holders (and anyone asking for an incompatible
3242 * lock is blocked). We can now downconvert the lock */
3243 if (!lockres->l_ops->downconvert_worker)
3246 /* Some lockres types want to do a bit of work before
3247 * downconverting a lock. Allow that here. The worker function
3248 * may sleep, so we save off a copy of what we're blocking as
3249 * it may change while we're not holding the spin lock. */
3250 blocking = lockres->l_blocking;
3251 spin_unlock_irqrestore(&lockres->l_lock, flags);
3253 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
3255 if (ctl->unblock_action == UNBLOCK_STOP_POST)
3258 spin_lock_irqsave(&lockres->l_lock, flags);
3259 if (blocking != lockres->l_blocking) {
3260 /* If this changed underneath us, then we can't drop
3268 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3269 if (lockres->l_level == DLM_LOCK_EX)
3273 * We only set the lvb if the lock has been fully
3274 * refreshed - otherwise we risk setting stale
3275 * data. Otherwise, there's no need to actually clear
3276 * out the lvb here as it's value is still valid.
3278 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3279 lockres->l_ops->set_lvb(lockres);
3282 gen = ocfs2_prepare_downconvert(lockres, new_level);
3283 spin_unlock_irqrestore(&lockres->l_lock, flags);
3284 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
3292 spin_unlock_irqrestore(&lockres->l_lock, flags);
3299 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3302 struct inode *inode;
3303 struct address_space *mapping;
3305 inode = ocfs2_lock_res_inode(lockres);
3306 mapping = inode->i_mapping;
3308 if (!S_ISREG(inode->i_mode))
3312 * We need this before the filemap_fdatawrite() so that it can
3313 * transfer the dirty bit from the PTE to the
3314 * page. Unfortunately this means that even for EX->PR
3315 * downconverts, we'll lose our mappings and have to build
3318 unmap_mapping_range(mapping, 0, 0, 0);
3320 if (filemap_fdatawrite(mapping)) {
3321 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3322 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3324 sync_mapping_buffers(mapping);
3325 if (blocking == DLM_LOCK_EX) {
3326 truncate_inode_pages(mapping, 0);
3328 /* We only need to wait on the I/O if we're not also
3329 * truncating pages because truncate_inode_pages waits
3330 * for us above. We don't truncate pages if we're
3331 * blocking anything < EXMODE because we want to keep
3332 * them around in that case. */
3333 filemap_fdatawait(mapping);
3337 return UNBLOCK_CONTINUE;
3340 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3343 struct inode *inode = ocfs2_lock_res_inode(lockres);
3344 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
3346 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3347 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
3352 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
3356 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3358 struct inode *inode = ocfs2_lock_res_inode(lockres);
3360 __ocfs2_stuff_meta_lvb(inode);
3364 * Does the final reference drop on our dentry lock. Right now this
3365 * happens in the downconvert thread, but we could choose to simplify the
3366 * dlmglue API and push these off to the ocfs2_wq in the future.
3368 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3369 struct ocfs2_lock_res *lockres)
3371 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3372 ocfs2_dentry_lock_put(osb, dl);
3376 * d_delete() matching dentries before the lock downconvert.
3378 * At this point, any process waiting to destroy the
3379 * dentry_lock due to last ref count is stopped by the
3380 * OCFS2_LOCK_QUEUED flag.
3382 * We have two potential problems
3384 * 1) If we do the last reference drop on our dentry_lock (via dput)
3385 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3386 * the downconvert to finish. Instead we take an elevated
3387 * reference and push the drop until after we've completed our
3388 * unblock processing.
3390 * 2) There might be another process with a final reference,
3391 * waiting on us to finish processing. If this is the case, we
3392 * detect it and exit out - there's no more dentries anyway.
3394 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3397 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3398 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3399 struct dentry *dentry;
3400 unsigned long flags;
3404 * This node is blocking another node from getting a read
3405 * lock. This happens when we've renamed within a
3406 * directory. We've forced the other nodes to d_delete(), but
3407 * we never actually dropped our lock because it's still
3408 * valid. The downconvert code will retain a PR for this node,
3409 * so there's no further work to do.
3411 if (blocking == DLM_LOCK_PR)
3412 return UNBLOCK_CONTINUE;
3415 * Mark this inode as potentially orphaned. The code in
3416 * ocfs2_delete_inode() will figure out whether it actually
3417 * needs to be freed or not.
3419 spin_lock(&oi->ip_lock);
3420 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3421 spin_unlock(&oi->ip_lock);
3424 * Yuck. We need to make sure however that the check of
3425 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3426 * respect to a reference decrement or the setting of that
3429 spin_lock_irqsave(&lockres->l_lock, flags);
3430 spin_lock(&dentry_attach_lock);
3431 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3436 spin_unlock(&dentry_attach_lock);
3437 spin_unlock_irqrestore(&lockres->l_lock, flags);
3439 mlog(0, "extra_ref = %d\n", extra_ref);
3442 * We have a process waiting on us in ocfs2_dentry_iput(),
3443 * which means we can't have any more outstanding
3444 * aliases. There's no need to do any more work.
3447 return UNBLOCK_CONTINUE;
3449 spin_lock(&dentry_attach_lock);
3451 dentry = ocfs2_find_local_alias(dl->dl_inode,
3452 dl->dl_parent_blkno, 1);
3455 spin_unlock(&dentry_attach_lock);
3457 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3458 dentry->d_name.name);
3461 * The following dcache calls may do an
3462 * iput(). Normally we don't want that from the
3463 * downconverting thread, but in this case it's ok
3464 * because the requesting node already has an
3465 * exclusive lock on the inode, so it can't be queued
3466 * for a downconvert.
3471 spin_lock(&dentry_attach_lock);
3473 spin_unlock(&dentry_attach_lock);
3476 * If we are the last holder of this dentry lock, there is no
3477 * reason to downconvert so skip straight to the unlock.
3479 if (dl->dl_count == 1)
3480 return UNBLOCK_STOP_POST;
3482 return UNBLOCK_CONTINUE_POST;
3485 static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres)
3487 struct ocfs2_qinfo_lvb *lvb;
3488 struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres);
3489 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3490 oinfo->dqi_gi.dqi_type);
3494 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
3495 lvb->lvb_version = OCFS2_QINFO_LVB_VERSION;
3496 lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace);
3497 lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace);
3498 lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms);
3499 lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks);
3500 lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk);
3501 lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry);
3506 void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3508 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3509 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3510 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3513 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
3514 ocfs2_cluster_unlock(osb, lockres, level);
3518 static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo)
3520 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3521 oinfo->dqi_gi.dqi_type);
3522 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3523 struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
3524 struct buffer_head *bh = NULL;
3525 struct ocfs2_global_disk_dqinfo *gdinfo;
3528 if (lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) {
3529 info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace);
3530 info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace);
3531 oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms);
3532 oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks);
3533 oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk);
3534 oinfo->dqi_gi.dqi_free_entry =
3535 be32_to_cpu(lvb->lvb_free_entry);
3537 status = ocfs2_read_quota_block(oinfo->dqi_gqinode, 0, &bh);
3542 gdinfo = (struct ocfs2_global_disk_dqinfo *)
3543 (bh->b_data + OCFS2_GLOBAL_INFO_OFF);
3544 info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace);
3545 info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace);
3546 oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms);
3547 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks);
3548 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk);
3549 oinfo->dqi_gi.dqi_free_entry =
3550 le32_to_cpu(gdinfo->dqi_free_entry);
3552 ocfs2_track_lock_refresh(lockres);
3559 /* Lock quota info, this function expects at least shared lock on the quota file
3560 * so that we can safely refresh quota info from disk. */
3561 int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3563 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3564 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3565 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3570 /* On RO devices, locking really isn't needed... */
3571 if (ocfs2_is_hard_readonly(osb)) {
3576 if (ocfs2_mount_local(osb))
3579 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
3584 if (!ocfs2_should_refresh_lock_res(lockres))
3586 /* OK, we have the lock but we need to refresh the quota info */
3587 status = ocfs2_refresh_qinfo(oinfo);
3589 ocfs2_qinfo_unlock(oinfo, ex);
3590 ocfs2_complete_lock_res_refresh(lockres, status);
3597 * This is the filesystem locking protocol. It provides the lock handling
3598 * hooks for the underlying DLM. It has a maximum version number.
3599 * The version number allows interoperability with systems running at
3600 * the same major number and an equal or smaller minor number.
3602 * Whenever the filesystem does new things with locks (adds or removes a
3603 * lock, orders them differently, does different things underneath a lock),
3604 * the version must be changed. The protocol is negotiated when joining
3605 * the dlm domain. A node may join the domain if its major version is
3606 * identical to all other nodes and its minor version is greater than
3607 * or equal to all other nodes. When its minor version is greater than
3608 * the other nodes, it will run at the minor version specified by the
3611 * If a locking change is made that will not be compatible with older
3612 * versions, the major number must be increased and the minor version set
3613 * to zero. If a change merely adds a behavior that can be disabled when
3614 * speaking to older versions, the minor version must be increased. If a
3615 * change adds a fully backwards compatible change (eg, LVB changes that
3616 * are just ignored by older versions), the version does not need to be
3619 static struct ocfs2_locking_protocol lproto = {
3621 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
3622 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
3624 .lp_lock_ast = ocfs2_locking_ast,
3625 .lp_blocking_ast = ocfs2_blocking_ast,
3626 .lp_unlock_ast = ocfs2_unlock_ast,
3629 void ocfs2_set_locking_protocol(void)
3631 ocfs2_stack_glue_set_locking_protocol(&lproto);
3635 static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3636 struct ocfs2_lock_res *lockres)
3639 struct ocfs2_unblock_ctl ctl = {0, 0,};
3640 unsigned long flags;
3642 /* Our reference to the lockres in this function can be
3643 * considered valid until we remove the OCFS2_LOCK_QUEUED
3649 BUG_ON(!lockres->l_ops);
3651 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3653 /* Detect whether a lock has been marked as going away while
3654 * the downconvert thread was processing other things. A lock can
3655 * still be marked with OCFS2_LOCK_FREEING after this check,
3656 * but short circuiting here will still save us some
3658 spin_lock_irqsave(&lockres->l_lock, flags);
3659 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3661 spin_unlock_irqrestore(&lockres->l_lock, flags);
3663 status = ocfs2_unblock_lock(osb, lockres, &ctl);
3667 spin_lock_irqsave(&lockres->l_lock, flags);
3669 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
3670 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3672 ocfs2_schedule_blocked_lock(osb, lockres);
3674 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
3675 ctl.requeue ? "yes" : "no");
3676 spin_unlock_irqrestore(&lockres->l_lock, flags);
3678 if (ctl.unblock_action != UNBLOCK_CONTINUE
3679 && lockres->l_ops->post_unlock)
3680 lockres->l_ops->post_unlock(osb, lockres);
3685 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3686 struct ocfs2_lock_res *lockres)
3690 assert_spin_locked(&lockres->l_lock);
3692 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3693 /* Do not schedule a lock for downconvert when it's on
3694 * the way to destruction - any nodes wanting access
3695 * to the resource will get it soon. */
3696 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3697 lockres->l_name, lockres->l_flags);
3701 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3703 spin_lock(&osb->dc_task_lock);
3704 if (list_empty(&lockres->l_blocked_list)) {
3705 list_add_tail(&lockres->l_blocked_list,
3706 &osb->blocked_lock_list);
3707 osb->blocked_lock_count++;
3709 spin_unlock(&osb->dc_task_lock);
3714 static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3716 unsigned long processed;
3717 struct ocfs2_lock_res *lockres;
3721 spin_lock(&osb->dc_task_lock);
3722 /* grab this early so we know to try again if a state change and
3723 * wake happens part-way through our work */
3724 osb->dc_work_sequence = osb->dc_wake_sequence;
3726 processed = osb->blocked_lock_count;
3728 BUG_ON(list_empty(&osb->blocked_lock_list));
3730 lockres = list_entry(osb->blocked_lock_list.next,
3731 struct ocfs2_lock_res, l_blocked_list);
3732 list_del_init(&lockres->l_blocked_list);
3733 osb->blocked_lock_count--;
3734 spin_unlock(&osb->dc_task_lock);
3739 ocfs2_process_blocked_lock(osb, lockres);
3741 spin_lock(&osb->dc_task_lock);
3743 spin_unlock(&osb->dc_task_lock);
3748 static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3752 spin_lock(&osb->dc_task_lock);
3753 if (list_empty(&osb->blocked_lock_list))
3756 spin_unlock(&osb->dc_task_lock);
3760 static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
3762 int should_wake = 0;
3764 spin_lock(&osb->dc_task_lock);
3765 if (osb->dc_work_sequence != osb->dc_wake_sequence)
3767 spin_unlock(&osb->dc_task_lock);
3772 static int ocfs2_downconvert_thread(void *arg)
3775 struct ocfs2_super *osb = arg;
3777 /* only quit once we've been asked to stop and there is no more
3779 while (!(kthread_should_stop() &&
3780 ocfs2_downconvert_thread_lists_empty(osb))) {
3782 wait_event_interruptible(osb->dc_event,
3783 ocfs2_downconvert_thread_should_wake(osb) ||
3784 kthread_should_stop());
3786 mlog(0, "downconvert_thread: awoken\n");
3788 ocfs2_downconvert_thread_do_work(osb);
3791 osb->dc_task = NULL;
3795 void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
3797 spin_lock(&osb->dc_task_lock);
3798 /* make sure the voting thread gets a swipe at whatever changes
3799 * the caller may have made to the voting state */
3800 osb->dc_wake_sequence++;
3801 spin_unlock(&osb->dc_task_lock);
3802 wake_up(&osb->dc_event);