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 /* Lockdep class keys */
96 struct lock_class_key lockdep_keys[OCFS2_NUM_LOCK_TYPES];
98 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
100 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
102 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
105 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
108 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
109 struct ocfs2_lock_res *lockres);
111 static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres);
113 #define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres)
115 /* This aids in debugging situations where a bad LVB might be involved. */
116 static void ocfs2_dump_meta_lvb_info(u64 level,
117 const char *function,
119 struct ocfs2_lock_res *lockres)
121 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
123 mlog(level, "LVB information for %s (called from %s:%u):\n",
124 lockres->l_name, function, line);
125 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
126 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
127 be32_to_cpu(lvb->lvb_igeneration));
128 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
129 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
130 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
131 be16_to_cpu(lvb->lvb_imode));
132 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
133 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
134 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
135 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
136 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
137 be32_to_cpu(lvb->lvb_iattr));
142 * OCFS2 Lock Resource Operations
144 * These fine tune the behavior of the generic dlmglue locking infrastructure.
146 * The most basic of lock types can point ->l_priv to their respective
147 * struct ocfs2_super and allow the default actions to manage things.
149 * Right now, each lock type also needs to implement an init function,
150 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
151 * should be called when the lock is no longer needed (i.e., object
154 struct ocfs2_lock_res_ops {
156 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
157 * this callback if ->l_priv is not an ocfs2_super pointer
159 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
162 * Optionally called in the downconvert thread after a
163 * successful downconvert. The lockres will not be referenced
164 * after this callback is called, so it is safe to free
167 * The exact semantics of when this is called are controlled
168 * by ->downconvert_worker()
170 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
173 * Allow a lock type to add checks to determine whether it is
174 * safe to downconvert a lock. Return 0 to re-queue the
175 * downconvert at a later time, nonzero to continue.
177 * For most locks, the default checks that there are no
178 * incompatible holders are sufficient.
180 * Called with the lockres spinlock held.
182 int (*check_downconvert)(struct ocfs2_lock_res *, int);
185 * Allows a lock type to populate the lock value block. This
186 * is called on downconvert, and when we drop a lock.
188 * Locks that want to use this should set LOCK_TYPE_USES_LVB
189 * in the flags field.
191 * Called with the lockres spinlock held.
193 void (*set_lvb)(struct ocfs2_lock_res *);
196 * Called from the downconvert thread when it is determined
197 * that a lock will be downconverted. This is called without
198 * any locks held so the function can do work that might
199 * schedule (syncing out data, etc).
201 * This should return any one of the ocfs2_unblock_action
202 * values, depending on what it wants the thread to do.
204 int (*downconvert_worker)(struct ocfs2_lock_res *, int);
207 * LOCK_TYPE_* flags which describe the specific requirements
208 * of a lock type. Descriptions of each individual flag follow.
214 * Some locks want to "refresh" potentially stale data when a
215 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
216 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
217 * individual lockres l_flags member from the ast function. It is
218 * expected that the locking wrapper will clear the
219 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
221 #define LOCK_TYPE_REQUIRES_REFRESH 0x1
224 * Indicate that a lock type makes use of the lock value block. The
225 * ->set_lvb lock type callback must be defined.
227 #define LOCK_TYPE_USES_LVB 0x2
229 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
230 .get_osb = ocfs2_get_inode_osb,
234 static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops = {
235 .get_osb = ocfs2_get_inode_osb,
236 .check_downconvert = ocfs2_check_meta_downconvert,
237 .set_lvb = ocfs2_set_meta_lvb,
238 .downconvert_worker = ocfs2_data_convert_worker,
239 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
242 static struct ocfs2_lock_res_ops ocfs2_super_lops = {
243 .flags = LOCK_TYPE_REQUIRES_REFRESH,
246 static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
250 static struct ocfs2_lock_res_ops ocfs2_nfs_sync_lops = {
254 static struct ocfs2_lock_res_ops ocfs2_orphan_scan_lops = {
255 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
258 static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
259 .get_osb = ocfs2_get_dentry_osb,
260 .post_unlock = ocfs2_dentry_post_unlock,
261 .downconvert_worker = ocfs2_dentry_convert_worker,
265 static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
266 .get_osb = ocfs2_get_inode_osb,
270 static struct ocfs2_lock_res_ops ocfs2_flock_lops = {
271 .get_osb = ocfs2_get_file_osb,
275 static struct ocfs2_lock_res_ops ocfs2_qinfo_lops = {
276 .set_lvb = ocfs2_set_qinfo_lvb,
277 .get_osb = ocfs2_get_qinfo_osb,
278 .flags = LOCK_TYPE_REQUIRES_REFRESH | LOCK_TYPE_USES_LVB,
281 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
283 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
284 lockres->l_type == OCFS2_LOCK_TYPE_RW ||
285 lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
288 static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
290 BUG_ON(!ocfs2_is_inode_lock(lockres));
292 return (struct inode *) lockres->l_priv;
295 static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
297 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
299 return (struct ocfs2_dentry_lock *)lockres->l_priv;
302 static inline struct ocfs2_mem_dqinfo *ocfs2_lock_res_qinfo(struct ocfs2_lock_res *lockres)
304 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_QINFO);
306 return (struct ocfs2_mem_dqinfo *)lockres->l_priv;
309 static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
311 if (lockres->l_ops->get_osb)
312 return lockres->l_ops->get_osb(lockres);
314 return (struct ocfs2_super *)lockres->l_priv;
317 static int ocfs2_lock_create(struct ocfs2_super *osb,
318 struct ocfs2_lock_res *lockres,
321 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
323 static void __ocfs2_cluster_unlock(struct ocfs2_super *osb,
324 struct ocfs2_lock_res *lockres,
325 int level, unsigned long caller_ip);
326 static inline void ocfs2_cluster_unlock(struct ocfs2_super *osb,
327 struct ocfs2_lock_res *lockres,
330 __ocfs2_cluster_unlock(osb, lockres, level, _RET_IP_);
333 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
334 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
335 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
336 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
337 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
338 struct ocfs2_lock_res *lockres);
339 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
341 #define ocfs2_log_dlm_error(_func, _err, _lockres) do { \
342 if ((_lockres)->l_type != OCFS2_LOCK_TYPE_DENTRY) \
343 mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \
344 _err, _func, _lockres->l_name); \
346 mlog(ML_ERROR, "DLM error %d while calling %s on resource %.*s%08x\n", \
347 _err, _func, OCFS2_DENTRY_LOCK_INO_START - 1, (_lockres)->l_name, \
348 (unsigned int)ocfs2_get_dentry_lock_ino(_lockres)); \
350 static int ocfs2_downconvert_thread(void *arg);
351 static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
352 struct ocfs2_lock_res *lockres);
353 static int ocfs2_inode_lock_update(struct inode *inode,
354 struct buffer_head **bh);
355 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
356 static inline int ocfs2_highest_compat_lock_level(int level);
357 static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
359 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
360 struct ocfs2_lock_res *lockres,
363 unsigned int generation);
364 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
365 struct ocfs2_lock_res *lockres);
366 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
367 struct ocfs2_lock_res *lockres);
370 static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
379 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
381 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
382 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
383 (long long)blkno, generation);
385 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
387 mlog(0, "built lock resource with name: %s\n", name);
392 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
394 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
395 struct ocfs2_dlm_debug *dlm_debug)
397 mlog(0, "Add tracking for lockres %s\n", res->l_name);
399 spin_lock(&ocfs2_dlm_tracking_lock);
400 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
401 spin_unlock(&ocfs2_dlm_tracking_lock);
404 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
406 spin_lock(&ocfs2_dlm_tracking_lock);
407 if (!list_empty(&res->l_debug_list))
408 list_del_init(&res->l_debug_list);
409 spin_unlock(&ocfs2_dlm_tracking_lock);
412 #ifdef CONFIG_OCFS2_FS_STATS
413 static void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
415 res->l_lock_num_prmode = 0;
416 res->l_lock_num_prmode_failed = 0;
417 res->l_lock_total_prmode = 0;
418 res->l_lock_max_prmode = 0;
419 res->l_lock_num_exmode = 0;
420 res->l_lock_num_exmode_failed = 0;
421 res->l_lock_total_exmode = 0;
422 res->l_lock_max_exmode = 0;
423 res->l_lock_refresh = 0;
426 static void ocfs2_update_lock_stats(struct ocfs2_lock_res *res, int level,
427 struct ocfs2_mask_waiter *mw, int ret)
429 unsigned long long *num, *sum;
430 unsigned int *max, *failed;
431 struct timespec ts = current_kernel_time();
432 unsigned long long time = timespec_to_ns(&ts) - mw->mw_lock_start;
434 if (level == LKM_PRMODE) {
435 num = &res->l_lock_num_prmode;
436 sum = &res->l_lock_total_prmode;
437 max = &res->l_lock_max_prmode;
438 failed = &res->l_lock_num_prmode_failed;
439 } else if (level == LKM_EXMODE) {
440 num = &res->l_lock_num_exmode;
441 sum = &res->l_lock_total_exmode;
442 max = &res->l_lock_max_exmode;
443 failed = &res->l_lock_num_exmode_failed;
455 static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
457 lockres->l_lock_refresh++;
460 static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
462 struct timespec ts = current_kernel_time();
463 mw->mw_lock_start = timespec_to_ns(&ts);
466 static inline void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
469 static inline void ocfs2_update_lock_stats(struct ocfs2_lock_res *res,
470 int level, struct ocfs2_mask_waiter *mw, int ret)
473 static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
476 static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
481 static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
482 struct ocfs2_lock_res *res,
483 enum ocfs2_lock_type type,
484 struct ocfs2_lock_res_ops *ops,
491 res->l_level = DLM_LOCK_IV;
492 res->l_requested = DLM_LOCK_IV;
493 res->l_blocking = DLM_LOCK_IV;
494 res->l_action = OCFS2_AST_INVALID;
495 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
497 res->l_flags = OCFS2_LOCK_INITIALIZED;
499 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
501 ocfs2_init_lock_stats(res);
502 #ifdef CONFIG_DEBUG_LOCK_ALLOC
503 if (type != OCFS2_LOCK_TYPE_OPEN)
504 lockdep_init_map(&res->l_lockdep_map, ocfs2_lock_type_strings[type],
505 &lockdep_keys[type], 0);
507 res->l_lockdep_map.key = NULL;
511 void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
513 /* This also clears out the lock status block */
514 memset(res, 0, sizeof(struct ocfs2_lock_res));
515 spin_lock_init(&res->l_lock);
516 init_waitqueue_head(&res->l_event);
517 INIT_LIST_HEAD(&res->l_blocked_list);
518 INIT_LIST_HEAD(&res->l_mask_waiters);
521 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
522 enum ocfs2_lock_type type,
523 unsigned int generation,
526 struct ocfs2_lock_res_ops *ops;
529 case OCFS2_LOCK_TYPE_RW:
530 ops = &ocfs2_inode_rw_lops;
532 case OCFS2_LOCK_TYPE_META:
533 ops = &ocfs2_inode_inode_lops;
535 case OCFS2_LOCK_TYPE_OPEN:
536 ops = &ocfs2_inode_open_lops;
539 mlog_bug_on_msg(1, "type: %d\n", type);
540 ops = NULL; /* thanks, gcc */
544 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
545 generation, res->l_name);
546 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
549 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
551 struct inode *inode = ocfs2_lock_res_inode(lockres);
553 return OCFS2_SB(inode->i_sb);
556 static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres)
558 struct ocfs2_mem_dqinfo *info = lockres->l_priv;
560 return OCFS2_SB(info->dqi_gi.dqi_sb);
563 static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
565 struct ocfs2_file_private *fp = lockres->l_priv;
567 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
570 static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
572 __be64 inode_blkno_be;
574 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
577 return be64_to_cpu(inode_blkno_be);
580 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
582 struct ocfs2_dentry_lock *dl = lockres->l_priv;
584 return OCFS2_SB(dl->dl_inode->i_sb);
587 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
588 u64 parent, struct inode *inode)
591 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
592 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
593 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
595 ocfs2_lock_res_init_once(lockres);
598 * Unfortunately, the standard lock naming scheme won't work
599 * here because we have two 16 byte values to use. Instead,
600 * we'll stuff the inode number as a binary value. We still
601 * want error prints to show something without garbling the
602 * display, so drop a null byte in there before the inode
603 * number. A future version of OCFS2 will likely use all
604 * binary lock names. The stringified names have been a
605 * tremendous aid in debugging, but now that the debugfs
606 * interface exists, we can mangle things there if need be.
608 * NOTE: We also drop the standard "pad" value (the total lock
609 * name size stays the same though - the last part is all
610 * zeros due to the memset in ocfs2_lock_res_init_once()
612 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
614 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
617 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
619 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
622 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
623 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
627 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
628 struct ocfs2_super *osb)
630 /* Superblock lockres doesn't come from a slab so we call init
631 * once on it manually. */
632 ocfs2_lock_res_init_once(res);
633 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
635 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
636 &ocfs2_super_lops, osb);
639 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
640 struct ocfs2_super *osb)
642 /* Rename lockres doesn't come from a slab so we call init
643 * once on it manually. */
644 ocfs2_lock_res_init_once(res);
645 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
646 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
647 &ocfs2_rename_lops, osb);
650 static void ocfs2_nfs_sync_lock_res_init(struct ocfs2_lock_res *res,
651 struct ocfs2_super *osb)
653 /* nfs_sync lockres doesn't come from a slab so we call init
654 * once on it manually. */
655 ocfs2_lock_res_init_once(res);
656 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_NFS_SYNC, 0, 0, res->l_name);
657 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_NFS_SYNC,
658 &ocfs2_nfs_sync_lops, osb);
661 static void ocfs2_orphan_scan_lock_res_init(struct ocfs2_lock_res *res,
662 struct ocfs2_super *osb)
664 ocfs2_lock_res_init_once(res);
665 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_ORPHAN_SCAN, 0, 0, res->l_name);
666 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_ORPHAN_SCAN,
667 &ocfs2_orphan_scan_lops, osb);
670 void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
671 struct ocfs2_file_private *fp)
673 struct inode *inode = fp->fp_file->f_mapping->host;
674 struct ocfs2_inode_info *oi = OCFS2_I(inode);
676 ocfs2_lock_res_init_once(lockres);
677 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
678 inode->i_generation, lockres->l_name);
679 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
680 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
682 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
685 void ocfs2_qinfo_lock_res_init(struct ocfs2_lock_res *lockres,
686 struct ocfs2_mem_dqinfo *info)
688 ocfs2_lock_res_init_once(lockres);
689 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_QINFO, info->dqi_gi.dqi_type,
691 ocfs2_lock_res_init_common(OCFS2_SB(info->dqi_gi.dqi_sb), lockres,
692 OCFS2_LOCK_TYPE_QINFO, &ocfs2_qinfo_lops,
696 void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
700 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
703 ocfs2_remove_lockres_tracking(res);
705 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
706 "Lockres %s is on the blocked list\n",
708 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
709 "Lockres %s has mask waiters pending\n",
711 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
712 "Lockres %s is locked\n",
714 mlog_bug_on_msg(res->l_ro_holders,
715 "Lockres %s has %u ro holders\n",
716 res->l_name, res->l_ro_holders);
717 mlog_bug_on_msg(res->l_ex_holders,
718 "Lockres %s has %u ex holders\n",
719 res->l_name, res->l_ex_holders);
721 /* Need to clear out the lock status block for the dlm */
722 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
728 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
737 lockres->l_ex_holders++;
740 lockres->l_ro_holders++;
749 static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
758 BUG_ON(!lockres->l_ex_holders);
759 lockres->l_ex_holders--;
762 BUG_ON(!lockres->l_ro_holders);
763 lockres->l_ro_holders--;
771 /* WARNING: This function lives in a world where the only three lock
772 * levels are EX, PR, and NL. It *will* have to be adjusted when more
773 * lock types are added. */
774 static inline int ocfs2_highest_compat_lock_level(int level)
776 int new_level = DLM_LOCK_EX;
778 if (level == DLM_LOCK_EX)
779 new_level = DLM_LOCK_NL;
780 else if (level == DLM_LOCK_PR)
781 new_level = DLM_LOCK_PR;
785 static void lockres_set_flags(struct ocfs2_lock_res *lockres,
786 unsigned long newflags)
788 struct ocfs2_mask_waiter *mw, *tmp;
790 assert_spin_locked(&lockres->l_lock);
792 lockres->l_flags = newflags;
794 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
795 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
798 list_del_init(&mw->mw_item);
800 complete(&mw->mw_complete);
803 static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
805 lockres_set_flags(lockres, lockres->l_flags | or);
807 static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
810 lockres_set_flags(lockres, lockres->l_flags & ~clear);
813 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
817 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
818 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
819 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
820 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
822 lockres->l_level = lockres->l_requested;
823 if (lockres->l_level <=
824 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
825 lockres->l_blocking = DLM_LOCK_NL;
826 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
828 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
833 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
837 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
838 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
840 /* Convert from RO to EX doesn't really need anything as our
841 * information is already up to data. Convert from NL to
842 * *anything* however should mark ourselves as needing an
844 if (lockres->l_level == DLM_LOCK_NL &&
845 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
846 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
848 lockres->l_level = lockres->l_requested;
849 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
854 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
858 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
859 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
861 if (lockres->l_requested > DLM_LOCK_NL &&
862 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
863 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
864 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
866 lockres->l_level = lockres->l_requested;
867 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
868 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
873 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
876 int needs_downconvert = 0;
879 assert_spin_locked(&lockres->l_lock);
881 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
883 if (level > lockres->l_blocking) {
884 /* only schedule a downconvert if we haven't already scheduled
885 * one that goes low enough to satisfy the level we're
886 * blocking. this also catches the case where we get
888 if (ocfs2_highest_compat_lock_level(level) <
889 ocfs2_highest_compat_lock_level(lockres->l_blocking))
890 needs_downconvert = 1;
892 lockres->l_blocking = level;
895 mlog_exit(needs_downconvert);
896 return needs_downconvert;
900 * OCFS2_LOCK_PENDING and l_pending_gen.
902 * Why does OCFS2_LOCK_PENDING exist? To close a race between setting
903 * OCFS2_LOCK_BUSY and calling ocfs2_dlm_lock(). See ocfs2_unblock_lock()
904 * for more details on the race.
906 * OCFS2_LOCK_PENDING closes the race quite nicely. However, it introduces
907 * a race on itself. In o2dlm, we can get the ast before ocfs2_dlm_lock()
908 * returns. The ast clears OCFS2_LOCK_BUSY, and must therefore clear
909 * OCFS2_LOCK_PENDING at the same time. When ocfs2_dlm_lock() returns,
910 * the caller is going to try to clear PENDING again. If nothing else is
911 * happening, __lockres_clear_pending() sees PENDING is unset and does
914 * But what if another path (eg downconvert thread) has just started a
915 * new locking action? The other path has re-set PENDING. Our path
916 * cannot clear PENDING, because that will re-open the original race
922 * ocfs2_cluster_lock()
927 * ocfs2_locking_ast() ocfs2_downconvert_thread()
928 * clear PENDING ocfs2_unblock_lock()
931 * ocfs2_prepare_downconvert()
941 * So as you can see, we now have a window where l_lock is not held,
942 * PENDING is not set, and ocfs2_dlm_lock() has not been called.
944 * The core problem is that ocfs2_cluster_lock() has cleared the PENDING
945 * set by ocfs2_prepare_downconvert(). That wasn't nice.
947 * To solve this we introduce l_pending_gen. A call to
948 * lockres_clear_pending() will only do so when it is passed a generation
949 * number that matches the lockres. lockres_set_pending() will return the
950 * current generation number. When ocfs2_cluster_lock() goes to clear
951 * PENDING, it passes the generation it got from set_pending(). In our
952 * example above, the generation numbers will *not* match. Thus,
953 * ocfs2_cluster_lock() will not clear the PENDING set by
954 * ocfs2_prepare_downconvert().
957 /* Unlocked version for ocfs2_locking_ast() */
958 static void __lockres_clear_pending(struct ocfs2_lock_res *lockres,
959 unsigned int generation,
960 struct ocfs2_super *osb)
962 assert_spin_locked(&lockres->l_lock);
965 * The ast and locking functions can race us here. The winner
966 * will clear pending, the loser will not.
968 if (!(lockres->l_flags & OCFS2_LOCK_PENDING) ||
969 (lockres->l_pending_gen != generation))
972 lockres_clear_flags(lockres, OCFS2_LOCK_PENDING);
973 lockres->l_pending_gen++;
976 * The downconvert thread may have skipped us because we
977 * were PENDING. Wake it up.
979 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
980 ocfs2_wake_downconvert_thread(osb);
983 /* Locked version for callers of ocfs2_dlm_lock() */
984 static void lockres_clear_pending(struct ocfs2_lock_res *lockres,
985 unsigned int generation,
986 struct ocfs2_super *osb)
990 spin_lock_irqsave(&lockres->l_lock, flags);
991 __lockres_clear_pending(lockres, generation, osb);
992 spin_unlock_irqrestore(&lockres->l_lock, flags);
995 static unsigned int lockres_set_pending(struct ocfs2_lock_res *lockres)
997 assert_spin_locked(&lockres->l_lock);
998 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
1000 lockres_or_flags(lockres, OCFS2_LOCK_PENDING);
1002 return lockres->l_pending_gen;
1006 static void ocfs2_blocking_ast(void *opaque, int level)
1008 struct ocfs2_lock_res *lockres = opaque;
1009 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1010 int needs_downconvert;
1011 unsigned long flags;
1013 BUG_ON(level <= DLM_LOCK_NL);
1015 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
1016 lockres->l_name, level, lockres->l_level,
1017 ocfs2_lock_type_string(lockres->l_type));
1020 * We can skip the bast for locks which don't enable caching -
1021 * they'll be dropped at the earliest possible time anyway.
1023 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
1026 spin_lock_irqsave(&lockres->l_lock, flags);
1027 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
1028 if (needs_downconvert)
1029 ocfs2_schedule_blocked_lock(osb, lockres);
1030 spin_unlock_irqrestore(&lockres->l_lock, flags);
1032 wake_up(&lockres->l_event);
1034 ocfs2_wake_downconvert_thread(osb);
1037 static void ocfs2_locking_ast(void *opaque)
1039 struct ocfs2_lock_res *lockres = opaque;
1040 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1041 unsigned long flags;
1044 spin_lock_irqsave(&lockres->l_lock, flags);
1046 status = ocfs2_dlm_lock_status(&lockres->l_lksb);
1048 if (status == -EAGAIN) {
1049 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1054 mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
1055 lockres->l_name, status);
1056 spin_unlock_irqrestore(&lockres->l_lock, flags);
1060 switch(lockres->l_action) {
1061 case OCFS2_AST_ATTACH:
1062 ocfs2_generic_handle_attach_action(lockres);
1063 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
1065 case OCFS2_AST_CONVERT:
1066 ocfs2_generic_handle_convert_action(lockres);
1068 case OCFS2_AST_DOWNCONVERT:
1069 ocfs2_generic_handle_downconvert_action(lockres);
1072 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
1073 "lockres flags = 0x%lx, unlock action: %u\n",
1074 lockres->l_name, lockres->l_action, lockres->l_flags,
1075 lockres->l_unlock_action);
1079 /* set it to something invalid so if we get called again we
1081 lockres->l_action = OCFS2_AST_INVALID;
1083 /* Did we try to cancel this lock? Clear that state */
1084 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT)
1085 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1088 * We may have beaten the locking functions here. We certainly
1089 * know that dlm_lock() has been called :-)
1090 * Because we can't have two lock calls in flight at once, we
1091 * can use lockres->l_pending_gen.
1093 __lockres_clear_pending(lockres, lockres->l_pending_gen, osb);
1095 wake_up(&lockres->l_event);
1096 spin_unlock_irqrestore(&lockres->l_lock, flags);
1099 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
1102 unsigned long flags;
1105 spin_lock_irqsave(&lockres->l_lock, flags);
1106 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1108 lockres->l_action = OCFS2_AST_INVALID;
1110 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1111 spin_unlock_irqrestore(&lockres->l_lock, flags);
1113 wake_up(&lockres->l_event);
1117 /* Note: If we detect another process working on the lock (i.e.,
1118 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
1119 * to do the right thing in that case.
1121 static int ocfs2_lock_create(struct ocfs2_super *osb,
1122 struct ocfs2_lock_res *lockres,
1127 unsigned long flags;
1132 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
1135 spin_lock_irqsave(&lockres->l_lock, flags);
1136 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
1137 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
1138 spin_unlock_irqrestore(&lockres->l_lock, flags);
1142 lockres->l_action = OCFS2_AST_ATTACH;
1143 lockres->l_requested = level;
1144 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1145 gen = lockres_set_pending(lockres);
1146 spin_unlock_irqrestore(&lockres->l_lock, flags);
1148 ret = ocfs2_dlm_lock(osb->cconn,
1153 OCFS2_LOCK_ID_MAX_LEN - 1,
1155 lockres_clear_pending(lockres, gen, osb);
1157 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
1158 ocfs2_recover_from_dlm_error(lockres, 1);
1161 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
1168 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
1171 unsigned long flags;
1174 spin_lock_irqsave(&lockres->l_lock, flags);
1175 ret = lockres->l_flags & flag;
1176 spin_unlock_irqrestore(&lockres->l_lock, flags);
1181 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
1184 wait_event(lockres->l_event,
1185 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
1188 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
1191 wait_event(lockres->l_event,
1192 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
1195 /* predict what lock level we'll be dropping down to on behalf
1196 * of another node, and return true if the currently wanted
1197 * level will be compatible with it. */
1198 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
1201 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
1203 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
1206 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
1208 INIT_LIST_HEAD(&mw->mw_item);
1209 init_completion(&mw->mw_complete);
1210 ocfs2_init_start_time(mw);
1213 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
1215 wait_for_completion(&mw->mw_complete);
1216 /* Re-arm the completion in case we want to wait on it again */
1217 INIT_COMPLETION(mw->mw_complete);
1218 return mw->mw_status;
1221 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
1222 struct ocfs2_mask_waiter *mw,
1226 BUG_ON(!list_empty(&mw->mw_item));
1228 assert_spin_locked(&lockres->l_lock);
1230 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
1235 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
1236 * if the mask still hadn't reached its goal */
1237 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
1238 struct ocfs2_mask_waiter *mw)
1240 unsigned long flags;
1243 spin_lock_irqsave(&lockres->l_lock, flags);
1244 if (!list_empty(&mw->mw_item)) {
1245 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
1248 list_del_init(&mw->mw_item);
1249 init_completion(&mw->mw_complete);
1251 spin_unlock_irqrestore(&lockres->l_lock, flags);
1257 static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
1258 struct ocfs2_lock_res *lockres)
1262 ret = wait_for_completion_interruptible(&mw->mw_complete);
1264 lockres_remove_mask_waiter(lockres, mw);
1266 ret = mw->mw_status;
1267 /* Re-arm the completion in case we want to wait on it again */
1268 INIT_COMPLETION(mw->mw_complete);
1272 static int __ocfs2_cluster_lock(struct ocfs2_super *osb,
1273 struct ocfs2_lock_res *lockres,
1278 unsigned long caller_ip)
1280 struct ocfs2_mask_waiter mw;
1281 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
1282 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1283 unsigned long flags;
1285 int noqueue_attempted = 0;
1289 ocfs2_init_mask_waiter(&mw);
1291 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
1292 lkm_flags |= DLM_LKF_VALBLK;
1297 if (catch_signals && signal_pending(current)) {
1302 spin_lock_irqsave(&lockres->l_lock, flags);
1304 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1305 "Cluster lock called on freeing lockres %s! flags "
1306 "0x%lx\n", lockres->l_name, lockres->l_flags);
1308 /* We only compare against the currently granted level
1309 * here. If the lock is blocked waiting on a downconvert,
1310 * we'll get caught below. */
1311 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1312 level > lockres->l_level) {
1313 /* is someone sitting in dlm_lock? If so, wait on
1315 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1320 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1321 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1322 /* is the lock is currently blocked on behalf of
1324 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1329 if (level > lockres->l_level) {
1330 if (noqueue_attempted > 0) {
1334 if (lkm_flags & DLM_LKF_NOQUEUE)
1335 noqueue_attempted = 1;
1337 if (lockres->l_action != OCFS2_AST_INVALID)
1338 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1339 lockres->l_name, lockres->l_action);
1341 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1342 lockres->l_action = OCFS2_AST_ATTACH;
1343 lkm_flags &= ~DLM_LKF_CONVERT;
1345 lockres->l_action = OCFS2_AST_CONVERT;
1346 lkm_flags |= DLM_LKF_CONVERT;
1349 lockres->l_requested = level;
1350 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1351 gen = lockres_set_pending(lockres);
1352 spin_unlock_irqrestore(&lockres->l_lock, flags);
1354 BUG_ON(level == DLM_LOCK_IV);
1355 BUG_ON(level == DLM_LOCK_NL);
1357 mlog(0, "lock %s, convert from %d to level = %d\n",
1358 lockres->l_name, lockres->l_level, level);
1360 /* call dlm_lock to upgrade lock now */
1361 ret = ocfs2_dlm_lock(osb->cconn,
1366 OCFS2_LOCK_ID_MAX_LEN - 1,
1368 lockres_clear_pending(lockres, gen, osb);
1370 if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
1372 ocfs2_log_dlm_error("ocfs2_dlm_lock",
1375 ocfs2_recover_from_dlm_error(lockres, 1);
1379 mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n",
1382 /* At this point we've gone inside the dlm and need to
1383 * complete our work regardless. */
1386 /* wait for busy to clear and carry on */
1390 /* Ok, if we get here then we're good to go. */
1391 ocfs2_inc_holders(lockres, level);
1395 spin_unlock_irqrestore(&lockres->l_lock, flags);
1398 * This is helping work around a lock inversion between the page lock
1399 * and dlm locks. One path holds the page lock while calling aops
1400 * which block acquiring dlm locks. The voting thread holds dlm
1401 * locks while acquiring page locks while down converting data locks.
1402 * This block is helping an aop path notice the inversion and back
1403 * off to unlock its page lock before trying the dlm lock again.
1405 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1406 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1408 if (lockres_remove_mask_waiter(lockres, &mw))
1414 ret = ocfs2_wait_for_mask(&mw);
1419 ocfs2_update_lock_stats(lockres, level, &mw, ret);
1421 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1422 if (!ret && lockres->l_lockdep_map.key != NULL) {
1423 if (level == DLM_LOCK_PR)
1424 rwsem_acquire_read(&lockres->l_lockdep_map, l_subclass,
1425 !!(arg_flags & OCFS2_META_LOCK_NOQUEUE),
1428 rwsem_acquire(&lockres->l_lockdep_map, l_subclass,
1429 !!(arg_flags & OCFS2_META_LOCK_NOQUEUE),
1437 static inline int ocfs2_cluster_lock(struct ocfs2_super *osb,
1438 struct ocfs2_lock_res *lockres,
1443 return __ocfs2_cluster_lock(osb, lockres, level, lkm_flags, arg_flags,
1448 static void __ocfs2_cluster_unlock(struct ocfs2_super *osb,
1449 struct ocfs2_lock_res *lockres,
1451 unsigned long caller_ip)
1453 unsigned long flags;
1456 spin_lock_irqsave(&lockres->l_lock, flags);
1457 ocfs2_dec_holders(lockres, level);
1458 ocfs2_downconvert_on_unlock(osb, lockres);
1459 spin_unlock_irqrestore(&lockres->l_lock, flags);
1460 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1461 if (lockres->l_lockdep_map.key != NULL)
1462 rwsem_release(&lockres->l_lockdep_map, 1, caller_ip);
1467 static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1468 struct ocfs2_lock_res *lockres,
1472 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1473 unsigned long flags;
1474 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
1476 spin_lock_irqsave(&lockres->l_lock, flags);
1477 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1478 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1479 spin_unlock_irqrestore(&lockres->l_lock, flags);
1481 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
1484 /* Grants us an EX lock on the data and metadata resources, skipping
1485 * the normal cluster directory lookup. Use this ONLY on newly created
1486 * inodes which other nodes can't possibly see, and which haven't been
1487 * hashed in the inode hash yet. This can give us a good performance
1488 * increase as it'll skip the network broadcast normally associated
1489 * with creating a new lock resource. */
1490 int ocfs2_create_new_inode_locks(struct inode *inode)
1493 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1496 BUG_ON(!ocfs2_inode_is_new(inode));
1500 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1502 /* NOTE: That we don't increment any of the holder counts, nor
1503 * do we add anything to a journal handle. Since this is
1504 * supposed to be a new inode which the cluster doesn't know
1505 * about yet, there is no need to. As far as the LVB handling
1506 * is concerned, this is basically like acquiring an EX lock
1507 * on a resource which has an invalid one -- we'll set it
1508 * valid when we release the EX. */
1510 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
1517 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
1518 * don't use a generation in their lock names.
1520 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
1526 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1537 int ocfs2_rw_lock(struct inode *inode, int write)
1540 struct ocfs2_lock_res *lockres;
1541 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1547 mlog(0, "inode %llu take %s RW lock\n",
1548 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1549 write ? "EXMODE" : "PRMODE");
1551 if (ocfs2_mount_local(osb))
1554 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1556 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1558 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1567 void ocfs2_rw_unlock(struct inode *inode, int write)
1569 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1570 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1571 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1575 mlog(0, "inode %llu drop %s RW lock\n",
1576 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1577 write ? "EXMODE" : "PRMODE");
1579 if (!ocfs2_mount_local(osb))
1580 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1586 * ocfs2_open_lock always get PR mode lock.
1588 int ocfs2_open_lock(struct inode *inode)
1591 struct ocfs2_lock_res *lockres;
1592 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1598 mlog(0, "inode %llu take PRMODE open lock\n",
1599 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1601 if (ocfs2_mount_local(osb))
1604 lockres = &OCFS2_I(inode)->ip_open_lockres;
1606 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1616 int ocfs2_try_open_lock(struct inode *inode, int write)
1618 int status = 0, level;
1619 struct ocfs2_lock_res *lockres;
1620 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1626 mlog(0, "inode %llu try to take %s open lock\n",
1627 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1628 write ? "EXMODE" : "PRMODE");
1630 if (ocfs2_mount_local(osb))
1633 lockres = &OCFS2_I(inode)->ip_open_lockres;
1635 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1638 * The file system may already holding a PRMODE/EXMODE open lock.
1639 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
1640 * other nodes and the -EAGAIN will indicate to the caller that
1641 * this inode is still in use.
1643 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1644 level, DLM_LKF_NOQUEUE, 0);
1652 * ocfs2_open_unlock unlock PR and EX mode open locks.
1654 void ocfs2_open_unlock(struct inode *inode)
1656 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1657 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1661 mlog(0, "inode %llu drop open lock\n",
1662 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1664 if (ocfs2_mount_local(osb))
1667 if(lockres->l_ro_holders)
1668 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1670 if(lockres->l_ex_holders)
1671 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1678 static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1682 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1683 unsigned long flags;
1684 struct ocfs2_mask_waiter mw;
1686 ocfs2_init_mask_waiter(&mw);
1689 spin_lock_irqsave(&lockres->l_lock, flags);
1690 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1691 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1693 spin_unlock_irqrestore(&lockres->l_lock, flags);
1694 ret = ocfs2_cancel_convert(osb, lockres);
1701 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1702 spin_unlock_irqrestore(&lockres->l_lock, flags);
1704 ocfs2_wait_for_mask(&mw);
1710 * We may still have gotten the lock, in which case there's no
1711 * point to restarting the syscall.
1713 if (lockres->l_level == level)
1716 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1717 lockres->l_flags, lockres->l_level, lockres->l_action);
1719 spin_unlock_irqrestore(&lockres->l_lock, flags);
1726 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1727 * flock() calls. The locking approach this requires is sufficiently
1728 * different from all other cluster lock types that we implement a
1729 * seperate path to the "low-level" dlm calls. In particular:
1731 * - No optimization of lock levels is done - we take at exactly
1732 * what's been requested.
1734 * - No lock caching is employed. We immediately downconvert to
1735 * no-lock at unlock time. This also means flock locks never go on
1736 * the blocking list).
1738 * - Since userspace can trivially deadlock itself with flock, we make
1739 * sure to allow cancellation of a misbehaving applications flock()
1742 * - Access to any flock lockres doesn't require concurrency, so we
1743 * can simplify the code by requiring the caller to guarantee
1744 * serialization of dlmglue flock calls.
1746 int ocfs2_file_lock(struct file *file, int ex, int trylock)
1748 int ret, level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1749 unsigned int lkm_flags = trylock ? DLM_LKF_NOQUEUE : 0;
1750 unsigned long flags;
1751 struct ocfs2_file_private *fp = file->private_data;
1752 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1753 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1754 struct ocfs2_mask_waiter mw;
1756 ocfs2_init_mask_waiter(&mw);
1758 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
1759 (lockres->l_level > DLM_LOCK_NL)) {
1761 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1762 "level: %u\n", lockres->l_name, lockres->l_flags,
1767 spin_lock_irqsave(&lockres->l_lock, flags);
1768 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1769 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1770 spin_unlock_irqrestore(&lockres->l_lock, flags);
1773 * Get the lock at NLMODE to start - that way we
1774 * can cancel the upconvert request if need be.
1776 ret = ocfs2_lock_create(osb, lockres, DLM_LOCK_NL, 0);
1782 ret = ocfs2_wait_for_mask(&mw);
1787 spin_lock_irqsave(&lockres->l_lock, flags);
1790 lockres->l_action = OCFS2_AST_CONVERT;
1791 lkm_flags |= DLM_LKF_CONVERT;
1792 lockres->l_requested = level;
1793 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1795 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1796 spin_unlock_irqrestore(&lockres->l_lock, flags);
1798 ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
1799 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1,
1802 if (!trylock || (ret != -EAGAIN)) {
1803 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
1807 ocfs2_recover_from_dlm_error(lockres, 1);
1808 lockres_remove_mask_waiter(lockres, &mw);
1812 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1813 if (ret == -ERESTARTSYS) {
1815 * Userspace can cause deadlock itself with
1816 * flock(). Current behavior locally is to allow the
1817 * deadlock, but abort the system call if a signal is
1818 * received. We follow this example, otherwise a
1819 * poorly written program could sit in kernel until
1822 * Handling this is a bit more complicated for Ocfs2
1823 * though. We can't exit this function with an
1824 * outstanding lock request, so a cancel convert is
1825 * required. We intentionally overwrite 'ret' - if the
1826 * cancel fails and the lock was granted, it's easier
1827 * to just bubble sucess back up to the user.
1829 ret = ocfs2_flock_handle_signal(lockres, level);
1830 } else if (!ret && (level > lockres->l_level)) {
1831 /* Trylock failed asynchronously */
1838 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1839 lockres->l_name, ex, trylock, ret);
1843 void ocfs2_file_unlock(struct file *file)
1847 unsigned long flags;
1848 struct ocfs2_file_private *fp = file->private_data;
1849 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1850 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1851 struct ocfs2_mask_waiter mw;
1853 ocfs2_init_mask_waiter(&mw);
1855 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
1858 if (lockres->l_level == DLM_LOCK_NL)
1861 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1862 lockres->l_name, lockres->l_flags, lockres->l_level,
1865 spin_lock_irqsave(&lockres->l_lock, flags);
1867 * Fake a blocking ast for the downconvert code.
1869 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
1870 lockres->l_blocking = DLM_LOCK_EX;
1872 gen = ocfs2_prepare_downconvert(lockres, DLM_LOCK_NL);
1873 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1874 spin_unlock_irqrestore(&lockres->l_lock, flags);
1876 ret = ocfs2_downconvert_lock(osb, lockres, DLM_LOCK_NL, 0, gen);
1882 ret = ocfs2_wait_for_mask(&mw);
1887 static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1888 struct ocfs2_lock_res *lockres)
1894 /* If we know that another node is waiting on our lock, kick
1895 * the downconvert thread * pre-emptively when we reach a release
1897 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1898 switch(lockres->l_blocking) {
1900 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1904 if (!lockres->l_ex_holders)
1913 ocfs2_wake_downconvert_thread(osb);
1918 #define OCFS2_SEC_BITS 34
1919 #define OCFS2_SEC_SHIFT (64 - 34)
1920 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1922 /* LVB only has room for 64 bits of time here so we pack it for
1924 static u64 ocfs2_pack_timespec(struct timespec *spec)
1927 u64 sec = spec->tv_sec;
1928 u32 nsec = spec->tv_nsec;
1930 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1935 /* Call this with the lockres locked. I am reasonably sure we don't
1936 * need ip_lock in this function as anyone who would be changing those
1937 * values is supposed to be blocked in ocfs2_inode_lock right now. */
1938 static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1940 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1941 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1942 struct ocfs2_meta_lvb *lvb;
1946 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
1949 * Invalidate the LVB of a deleted inode - this way other
1950 * nodes are forced to go to disk and discover the new inode
1953 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1954 lvb->lvb_version = 0;
1958 lvb->lvb_version = OCFS2_LVB_VERSION;
1959 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1960 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1961 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1962 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1963 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1964 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1965 lvb->lvb_iatime_packed =
1966 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1967 lvb->lvb_ictime_packed =
1968 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1969 lvb->lvb_imtime_packed =
1970 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1971 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
1972 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
1973 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
1976 mlog_meta_lvb(0, lockres);
1981 static void ocfs2_unpack_timespec(struct timespec *spec,
1984 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1985 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1988 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1990 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1991 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1992 struct ocfs2_meta_lvb *lvb;
1996 mlog_meta_lvb(0, lockres);
1998 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2000 /* We're safe here without the lockres lock... */
2001 spin_lock(&oi->ip_lock);
2002 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
2003 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
2005 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
2006 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
2007 ocfs2_set_inode_flags(inode);
2009 /* fast-symlinks are a special case */
2010 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
2011 inode->i_blocks = 0;
2013 inode->i_blocks = ocfs2_inode_sector_count(inode);
2015 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
2016 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
2017 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
2018 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
2019 ocfs2_unpack_timespec(&inode->i_atime,
2020 be64_to_cpu(lvb->lvb_iatime_packed));
2021 ocfs2_unpack_timespec(&inode->i_mtime,
2022 be64_to_cpu(lvb->lvb_imtime_packed));
2023 ocfs2_unpack_timespec(&inode->i_ctime,
2024 be64_to_cpu(lvb->lvb_ictime_packed));
2025 spin_unlock(&oi->ip_lock);
2030 static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
2031 struct ocfs2_lock_res *lockres)
2033 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2035 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb)
2036 && lvb->lvb_version == OCFS2_LVB_VERSION
2037 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
2042 /* Determine whether a lock resource needs to be refreshed, and
2043 * arbitrate who gets to refresh it.
2045 * 0 means no refresh needed.
2047 * > 0 means you need to refresh this and you MUST call
2048 * ocfs2_complete_lock_res_refresh afterwards. */
2049 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
2051 unsigned long flags;
2057 spin_lock_irqsave(&lockres->l_lock, flags);
2058 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2059 spin_unlock_irqrestore(&lockres->l_lock, flags);
2063 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2064 spin_unlock_irqrestore(&lockres->l_lock, flags);
2066 ocfs2_wait_on_refreshing_lock(lockres);
2070 /* Ok, I'll be the one to refresh this lock. */
2071 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
2072 spin_unlock_irqrestore(&lockres->l_lock, flags);
2080 /* If status is non zero, I'll mark it as not being in refresh
2081 * anymroe, but i won't clear the needs refresh flag. */
2082 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
2085 unsigned long flags;
2088 spin_lock_irqsave(&lockres->l_lock, flags);
2089 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
2091 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
2092 spin_unlock_irqrestore(&lockres->l_lock, flags);
2094 wake_up(&lockres->l_event);
2099 /* may or may not return a bh if it went to disk. */
2100 static int ocfs2_inode_lock_update(struct inode *inode,
2101 struct buffer_head **bh)
2104 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2105 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
2106 struct ocfs2_dinode *fe;
2107 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2111 if (ocfs2_mount_local(osb))
2114 spin_lock(&oi->ip_lock);
2115 if (oi->ip_flags & OCFS2_INODE_DELETED) {
2116 mlog(0, "Orphaned inode %llu was deleted while we "
2117 "were waiting on a lock. ip_flags = 0x%x\n",
2118 (unsigned long long)oi->ip_blkno, oi->ip_flags);
2119 spin_unlock(&oi->ip_lock);
2123 spin_unlock(&oi->ip_lock);
2125 if (!ocfs2_should_refresh_lock_res(lockres))
2128 /* This will discard any caching information we might have had
2129 * for the inode metadata. */
2130 ocfs2_metadata_cache_purge(inode);
2132 ocfs2_extent_map_trunc(inode, 0);
2134 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
2135 mlog(0, "Trusting LVB on inode %llu\n",
2136 (unsigned long long)oi->ip_blkno);
2137 ocfs2_refresh_inode_from_lvb(inode);
2139 /* Boo, we have to go to disk. */
2140 /* read bh, cast, ocfs2_refresh_inode */
2141 status = ocfs2_read_inode_block(inode, bh);
2146 fe = (struct ocfs2_dinode *) (*bh)->b_data;
2148 /* This is a good chance to make sure we're not
2149 * locking an invalid object. ocfs2_read_inode_block()
2150 * already checked that the inode block is sane.
2152 * We bug on a stale inode here because we checked
2153 * above whether it was wiped from disk. The wiping
2154 * node provides a guarantee that we receive that
2155 * message and can mark the inode before dropping any
2156 * locks associated with it. */
2157 mlog_bug_on_msg(inode->i_generation !=
2158 le32_to_cpu(fe->i_generation),
2159 "Invalid dinode %llu disk generation: %u "
2160 "inode->i_generation: %u\n",
2161 (unsigned long long)oi->ip_blkno,
2162 le32_to_cpu(fe->i_generation),
2163 inode->i_generation);
2164 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
2165 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
2166 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
2167 (unsigned long long)oi->ip_blkno,
2168 (unsigned long long)le64_to_cpu(fe->i_dtime),
2169 le32_to_cpu(fe->i_flags));
2171 ocfs2_refresh_inode(inode, fe);
2172 ocfs2_track_lock_refresh(lockres);
2177 ocfs2_complete_lock_res_refresh(lockres, status);
2183 static int ocfs2_assign_bh(struct inode *inode,
2184 struct buffer_head **ret_bh,
2185 struct buffer_head *passed_bh)
2190 /* Ok, the update went to disk for us, use the
2192 *ret_bh = passed_bh;
2198 status = ocfs2_read_inode_block(inode, ret_bh);
2206 * returns < 0 error if the callback will never be called, otherwise
2207 * the result of the lock will be communicated via the callback.
2209 int ocfs2_inode_lock_full_nested(struct inode *inode,
2210 struct buffer_head **ret_bh,
2215 int status, level, acquired;
2217 struct ocfs2_lock_res *lockres = NULL;
2218 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2219 struct buffer_head *local_bh = NULL;
2225 mlog(0, "inode %llu, take %s META lock\n",
2226 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2227 ex ? "EXMODE" : "PRMODE");
2231 /* We'll allow faking a readonly metadata lock for
2233 if (ocfs2_is_hard_readonly(osb)) {
2239 if (ocfs2_mount_local(osb))
2242 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
2243 ocfs2_wait_for_recovery(osb);
2245 lockres = &OCFS2_I(inode)->ip_inode_lockres;
2246 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2248 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
2249 dlm_flags |= DLM_LKF_NOQUEUE;
2251 status = __ocfs2_cluster_lock(osb, lockres, level, dlm_flags,
2252 arg_flags, subclass, _RET_IP_);
2254 if (status != -EAGAIN && status != -EIOCBRETRY)
2259 /* Notify the error cleanup path to drop the cluster lock. */
2262 /* We wait twice because a node may have died while we were in
2263 * the lower dlm layers. The second time though, we've
2264 * committed to owning this lock so we don't allow signals to
2265 * abort the operation. */
2266 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
2267 ocfs2_wait_for_recovery(osb);
2271 * We only see this flag if we're being called from
2272 * ocfs2_read_locked_inode(). It means we're locking an inode
2273 * which hasn't been populated yet, so clear the refresh flag
2274 * and let the caller handle it.
2276 if (inode->i_state & I_NEW) {
2279 ocfs2_complete_lock_res_refresh(lockres, 0);
2283 /* This is fun. The caller may want a bh back, or it may
2284 * not. ocfs2_inode_lock_update definitely wants one in, but
2285 * may or may not read one, depending on what's in the
2286 * LVB. The result of all of this is that we've *only* gone to
2287 * disk if we have to, so the complexity is worthwhile. */
2288 status = ocfs2_inode_lock_update(inode, &local_bh);
2290 if (status != -ENOENT)
2296 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
2305 if (ret_bh && (*ret_bh)) {
2310 ocfs2_inode_unlock(inode, ex);
2321 * This is working around a lock inversion between tasks acquiring DLM
2322 * locks while holding a page lock and the downconvert thread which
2323 * blocks dlm lock acquiry while acquiring page locks.
2325 * ** These _with_page variantes are only intended to be called from aop
2326 * methods that hold page locks and return a very specific *positive* error
2327 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2329 * The DLM is called such that it returns -EAGAIN if it would have
2330 * blocked waiting for the downconvert thread. In that case we unlock
2331 * our page so the downconvert thread can make progress. Once we've
2332 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2333 * that called us can bubble that back up into the VFS who will then
2334 * immediately retry the aop call.
2336 * We do a blocking lock and immediate unlock before returning, though, so that
2337 * the lock has a great chance of being cached on this node by the time the VFS
2338 * calls back to retry the aop. This has a potential to livelock as nodes
2339 * ping locks back and forth, but that's a risk we're willing to take to avoid
2340 * the lock inversion simply.
2342 int ocfs2_inode_lock_with_page(struct inode *inode,
2343 struct buffer_head **ret_bh,
2349 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
2350 if (ret == -EAGAIN) {
2352 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2353 ocfs2_inode_unlock(inode, ex);
2354 ret = AOP_TRUNCATED_PAGE;
2360 int ocfs2_inode_lock_atime(struct inode *inode,
2361 struct vfsmount *vfsmnt,
2367 ret = ocfs2_inode_lock(inode, NULL, 0);
2374 * If we should update atime, we will get EX lock,
2375 * otherwise we just get PR lock.
2377 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2378 struct buffer_head *bh = NULL;
2380 ocfs2_inode_unlock(inode, 0);
2381 ret = ocfs2_inode_lock(inode, &bh, 1);
2387 if (ocfs2_should_update_atime(inode, vfsmnt))
2388 ocfs2_update_inode_atime(inode, bh);
2398 void ocfs2_inode_unlock(struct inode *inode,
2401 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2402 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
2403 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2407 mlog(0, "inode %llu drop %s META lock\n",
2408 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2409 ex ? "EXMODE" : "PRMODE");
2411 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2412 !ocfs2_mount_local(osb))
2413 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
2418 int ocfs2_orphan_scan_lock(struct ocfs2_super *osb, u32 *seqno)
2420 struct ocfs2_lock_res *lockres;
2421 struct ocfs2_orphan_scan_lvb *lvb;
2424 if (ocfs2_is_hard_readonly(osb))
2427 if (ocfs2_mount_local(osb))
2430 lockres = &osb->osb_orphan_scan.os_lockres;
2431 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
2435 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2436 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
2437 lvb->lvb_version == OCFS2_ORPHAN_LVB_VERSION)
2438 *seqno = be32_to_cpu(lvb->lvb_os_seqno);
2440 *seqno = osb->osb_orphan_scan.os_seqno + 1;
2445 void ocfs2_orphan_scan_unlock(struct ocfs2_super *osb, u32 seqno)
2447 struct ocfs2_lock_res *lockres;
2448 struct ocfs2_orphan_scan_lvb *lvb;
2450 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb)) {
2451 lockres = &osb->osb_orphan_scan.os_lockres;
2452 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2453 lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION;
2454 lvb->lvb_os_seqno = cpu_to_be32(seqno);
2455 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2459 int ocfs2_super_lock(struct ocfs2_super *osb,
2463 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2464 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2468 if (ocfs2_is_hard_readonly(osb))
2471 if (ocfs2_mount_local(osb))
2474 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2480 /* The super block lock path is really in the best position to
2481 * know when resources covered by the lock need to be
2482 * refreshed, so we do it here. Of course, making sense of
2483 * everything is up to the caller :) */
2484 status = ocfs2_should_refresh_lock_res(lockres);
2490 status = ocfs2_refresh_slot_info(osb);
2492 ocfs2_complete_lock_res_refresh(lockres, status);
2496 ocfs2_track_lock_refresh(lockres);
2503 void ocfs2_super_unlock(struct ocfs2_super *osb,
2506 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2507 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2509 if (!ocfs2_mount_local(osb))
2510 ocfs2_cluster_unlock(osb, lockres, level);
2513 int ocfs2_rename_lock(struct ocfs2_super *osb)
2516 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2518 if (ocfs2_is_hard_readonly(osb))
2521 if (ocfs2_mount_local(osb))
2524 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
2531 void ocfs2_rename_unlock(struct ocfs2_super *osb)
2533 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2535 if (!ocfs2_mount_local(osb))
2536 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2539 int ocfs2_nfs_sync_lock(struct ocfs2_super *osb, int ex)
2542 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2544 if (ocfs2_is_hard_readonly(osb))
2547 if (ocfs2_mount_local(osb))
2550 status = ocfs2_cluster_lock(osb, lockres, ex ? LKM_EXMODE : LKM_PRMODE,
2553 mlog(ML_ERROR, "lock on nfs sync lock failed %d\n", status);
2558 void ocfs2_nfs_sync_unlock(struct ocfs2_super *osb, int ex)
2560 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2562 if (!ocfs2_mount_local(osb))
2563 ocfs2_cluster_unlock(osb, lockres,
2564 ex ? LKM_EXMODE : LKM_PRMODE);
2567 int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2570 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2571 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2572 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2576 if (ocfs2_is_hard_readonly(osb))
2579 if (ocfs2_mount_local(osb))
2582 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2589 void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2591 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2592 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2593 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2595 if (!ocfs2_mount_local(osb))
2596 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
2599 /* Reference counting of the dlm debug structure. We want this because
2600 * open references on the debug inodes can live on after a mount, so
2601 * we can't rely on the ocfs2_super to always exist. */
2602 static void ocfs2_dlm_debug_free(struct kref *kref)
2604 struct ocfs2_dlm_debug *dlm_debug;
2606 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2611 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2614 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2617 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2619 kref_get(&debug->d_refcnt);
2622 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2624 struct ocfs2_dlm_debug *dlm_debug;
2626 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2628 mlog_errno(-ENOMEM);
2632 kref_init(&dlm_debug->d_refcnt);
2633 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2634 dlm_debug->d_locking_state = NULL;
2639 /* Access to this is arbitrated for us via seq_file->sem. */
2640 struct ocfs2_dlm_seq_priv {
2641 struct ocfs2_dlm_debug *p_dlm_debug;
2642 struct ocfs2_lock_res p_iter_res;
2643 struct ocfs2_lock_res p_tmp_res;
2646 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2647 struct ocfs2_dlm_seq_priv *priv)
2649 struct ocfs2_lock_res *iter, *ret = NULL;
2650 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2652 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2654 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2655 /* discover the head of the list */
2656 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2657 mlog(0, "End of list found, %p\n", ret);
2661 /* We track our "dummy" iteration lockres' by a NULL
2663 if (iter->l_ops != NULL) {
2672 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2674 struct ocfs2_dlm_seq_priv *priv = m->private;
2675 struct ocfs2_lock_res *iter;
2677 spin_lock(&ocfs2_dlm_tracking_lock);
2678 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2680 /* Since lockres' have the lifetime of their container
2681 * (which can be inodes, ocfs2_supers, etc) we want to
2682 * copy this out to a temporary lockres while still
2683 * under the spinlock. Obviously after this we can't
2684 * trust any pointers on the copy returned, but that's
2685 * ok as the information we want isn't typically held
2687 priv->p_tmp_res = *iter;
2688 iter = &priv->p_tmp_res;
2690 spin_unlock(&ocfs2_dlm_tracking_lock);
2695 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2699 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2701 struct ocfs2_dlm_seq_priv *priv = m->private;
2702 struct ocfs2_lock_res *iter = v;
2703 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2705 spin_lock(&ocfs2_dlm_tracking_lock);
2706 iter = ocfs2_dlm_next_res(iter, priv);
2707 list_del_init(&dummy->l_debug_list);
2709 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2710 priv->p_tmp_res = *iter;
2711 iter = &priv->p_tmp_res;
2713 spin_unlock(&ocfs2_dlm_tracking_lock);
2718 /* So that debugfs.ocfs2 can determine which format is being used */
2719 #define OCFS2_DLM_DEBUG_STR_VERSION 2
2720 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2724 struct ocfs2_lock_res *lockres = v;
2729 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2731 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2732 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2734 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2736 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2738 seq_printf(m, "%d\t"
2749 lockres->l_unlock_action,
2750 lockres->l_ro_holders,
2751 lockres->l_ex_holders,
2752 lockres->l_requested,
2753 lockres->l_blocking);
2755 /* Dump the raw LVB */
2756 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2757 for(i = 0; i < DLM_LVB_LEN; i++)
2758 seq_printf(m, "0x%x\t", lvb[i]);
2760 #ifdef CONFIG_OCFS2_FS_STATS
2761 # define lock_num_prmode(_l) (_l)->l_lock_num_prmode
2762 # define lock_num_exmode(_l) (_l)->l_lock_num_exmode
2763 # define lock_num_prmode_failed(_l) (_l)->l_lock_num_prmode_failed
2764 # define lock_num_exmode_failed(_l) (_l)->l_lock_num_exmode_failed
2765 # define lock_total_prmode(_l) (_l)->l_lock_total_prmode
2766 # define lock_total_exmode(_l) (_l)->l_lock_total_exmode
2767 # define lock_max_prmode(_l) (_l)->l_lock_max_prmode
2768 # define lock_max_exmode(_l) (_l)->l_lock_max_exmode
2769 # define lock_refresh(_l) (_l)->l_lock_refresh
2771 # define lock_num_prmode(_l) (0ULL)
2772 # define lock_num_exmode(_l) (0ULL)
2773 # define lock_num_prmode_failed(_l) (0)
2774 # define lock_num_exmode_failed(_l) (0)
2775 # define lock_total_prmode(_l) (0ULL)
2776 # define lock_total_exmode(_l) (0ULL)
2777 # define lock_max_prmode(_l) (0)
2778 # define lock_max_exmode(_l) (0)
2779 # define lock_refresh(_l) (0)
2781 /* The following seq_print was added in version 2 of this output */
2782 seq_printf(m, "%llu\t"
2791 lock_num_prmode(lockres),
2792 lock_num_exmode(lockres),
2793 lock_num_prmode_failed(lockres),
2794 lock_num_exmode_failed(lockres),
2795 lock_total_prmode(lockres),
2796 lock_total_exmode(lockres),
2797 lock_max_prmode(lockres),
2798 lock_max_exmode(lockres),
2799 lock_refresh(lockres));
2802 seq_printf(m, "\n");
2806 static const struct seq_operations ocfs2_dlm_seq_ops = {
2807 .start = ocfs2_dlm_seq_start,
2808 .stop = ocfs2_dlm_seq_stop,
2809 .next = ocfs2_dlm_seq_next,
2810 .show = ocfs2_dlm_seq_show,
2813 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2815 struct seq_file *seq = (struct seq_file *) file->private_data;
2816 struct ocfs2_dlm_seq_priv *priv = seq->private;
2817 struct ocfs2_lock_res *res = &priv->p_iter_res;
2819 ocfs2_remove_lockres_tracking(res);
2820 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2821 return seq_release_private(inode, file);
2824 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2827 struct ocfs2_dlm_seq_priv *priv;
2828 struct seq_file *seq;
2829 struct ocfs2_super *osb;
2831 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2837 osb = inode->i_private;
2838 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2839 priv->p_dlm_debug = osb->osb_dlm_debug;
2840 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2842 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2849 seq = (struct seq_file *) file->private_data;
2850 seq->private = priv;
2852 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2859 static const struct file_operations ocfs2_dlm_debug_fops = {
2860 .open = ocfs2_dlm_debug_open,
2861 .release = ocfs2_dlm_debug_release,
2863 .llseek = seq_lseek,
2866 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2869 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2871 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2873 osb->osb_debug_root,
2875 &ocfs2_dlm_debug_fops);
2876 if (!dlm_debug->d_locking_state) {
2879 "Unable to create locking state debugfs file.\n");
2883 ocfs2_get_dlm_debug(dlm_debug);
2888 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2890 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2893 debugfs_remove(dlm_debug->d_locking_state);
2894 ocfs2_put_dlm_debug(dlm_debug);
2898 int ocfs2_dlm_init(struct ocfs2_super *osb)
2901 struct ocfs2_cluster_connection *conn = NULL;
2905 if (ocfs2_mount_local(osb)) {
2910 status = ocfs2_dlm_init_debug(osb);
2916 /* launch downconvert thread */
2917 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2918 if (IS_ERR(osb->dc_task)) {
2919 status = PTR_ERR(osb->dc_task);
2920 osb->dc_task = NULL;
2925 /* for now, uuid == domain */
2926 status = ocfs2_cluster_connect(osb->osb_cluster_stack,
2928 strlen(osb->uuid_str),
2929 ocfs2_do_node_down, osb,
2936 status = ocfs2_cluster_this_node(&osb->node_num);
2940 "could not find this host's node number\n");
2941 ocfs2_cluster_disconnect(conn, 0);
2946 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2947 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2948 ocfs2_nfs_sync_lock_res_init(&osb->osb_nfs_sync_lockres, osb);
2949 ocfs2_orphan_scan_lock_res_init(&osb->osb_orphan_scan.os_lockres, osb);
2956 ocfs2_dlm_shutdown_debug(osb);
2958 kthread_stop(osb->dc_task);
2965 void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
2970 ocfs2_drop_osb_locks(osb);
2973 * Now that we have dropped all locks and ocfs2_dismount_volume()
2974 * has disabled recovery, the DLM won't be talking to us. It's
2975 * safe to tear things down before disconnecting the cluster.
2979 kthread_stop(osb->dc_task);
2980 osb->dc_task = NULL;
2983 ocfs2_lock_res_free(&osb->osb_super_lockres);
2984 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2985 ocfs2_lock_res_free(&osb->osb_nfs_sync_lockres);
2986 ocfs2_lock_res_free(&osb->osb_orphan_scan.os_lockres);
2988 ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
2991 ocfs2_dlm_shutdown_debug(osb);
2996 static void ocfs2_unlock_ast(void *opaque, int error)
2998 struct ocfs2_lock_res *lockres = opaque;
2999 unsigned long flags;
3003 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
3004 lockres->l_unlock_action);
3006 spin_lock_irqsave(&lockres->l_lock, flags);
3008 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
3009 "unlock_action %d\n", error, lockres->l_name,
3010 lockres->l_unlock_action);
3011 spin_unlock_irqrestore(&lockres->l_lock, flags);
3015 switch(lockres->l_unlock_action) {
3016 case OCFS2_UNLOCK_CANCEL_CONVERT:
3017 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
3018 lockres->l_action = OCFS2_AST_INVALID;
3019 /* Downconvert thread may have requeued this lock, we
3020 * need to wake it. */
3021 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
3022 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres));
3024 case OCFS2_UNLOCK_DROP_LOCK:
3025 lockres->l_level = DLM_LOCK_IV;
3031 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
3032 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
3033 wake_up(&lockres->l_event);
3034 spin_unlock_irqrestore(&lockres->l_lock, flags);
3039 static int ocfs2_drop_lock(struct ocfs2_super *osb,
3040 struct ocfs2_lock_res *lockres)
3043 unsigned long flags;
3046 /* We didn't get anywhere near actually using this lockres. */
3047 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
3050 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
3051 lkm_flags |= DLM_LKF_VALBLK;
3053 spin_lock_irqsave(&lockres->l_lock, flags);
3055 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
3056 "lockres %s, flags 0x%lx\n",
3057 lockres->l_name, lockres->l_flags);
3059 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
3060 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
3061 "%u, unlock_action = %u\n",
3062 lockres->l_name, lockres->l_flags, lockres->l_action,
3063 lockres->l_unlock_action);
3065 spin_unlock_irqrestore(&lockres->l_lock, flags);
3067 /* XXX: Today we just wait on any busy
3068 * locks... Perhaps we need to cancel converts in the
3070 ocfs2_wait_on_busy_lock(lockres);
3072 spin_lock_irqsave(&lockres->l_lock, flags);
3075 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3076 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
3077 lockres->l_level == DLM_LOCK_EX &&
3078 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3079 lockres->l_ops->set_lvb(lockres);
3082 if (lockres->l_flags & OCFS2_LOCK_BUSY)
3083 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
3085 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
3086 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
3088 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
3089 spin_unlock_irqrestore(&lockres->l_lock, flags);
3093 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
3095 /* make sure we never get here while waiting for an ast to
3097 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
3099 /* is this necessary? */
3100 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3101 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
3102 spin_unlock_irqrestore(&lockres->l_lock, flags);
3104 mlog(0, "lock %s\n", lockres->l_name);
3106 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags,
3109 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
3110 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
3111 ocfs2_dlm_dump_lksb(&lockres->l_lksb);
3114 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
3117 ocfs2_wait_on_busy_lock(lockres);
3123 /* Mark the lockres as being dropped. It will no longer be
3124 * queued if blocking, but we still may have to wait on it
3125 * being dequeued from the downconvert thread before we can consider
3128 * You can *not* attempt to call cluster_lock on this lockres anymore. */
3129 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
3132 struct ocfs2_mask_waiter mw;
3133 unsigned long flags;
3135 ocfs2_init_mask_waiter(&mw);
3137 spin_lock_irqsave(&lockres->l_lock, flags);
3138 lockres->l_flags |= OCFS2_LOCK_FREEING;
3139 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
3140 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
3141 spin_unlock_irqrestore(&lockres->l_lock, flags);
3143 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
3145 status = ocfs2_wait_for_mask(&mw);
3149 spin_lock_irqsave(&lockres->l_lock, flags);
3151 spin_unlock_irqrestore(&lockres->l_lock, flags);
3154 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
3155 struct ocfs2_lock_res *lockres)
3159 ocfs2_mark_lockres_freeing(lockres);
3160 ret = ocfs2_drop_lock(osb, lockres);
3165 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
3167 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
3168 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
3169 ocfs2_simple_drop_lockres(osb, &osb->osb_nfs_sync_lockres);
3170 ocfs2_simple_drop_lockres(osb, &osb->osb_orphan_scan.os_lockres);
3173 int ocfs2_drop_inode_locks(struct inode *inode)
3179 /* No need to call ocfs2_mark_lockres_freeing here -
3180 * ocfs2_clear_inode has done it for us. */
3182 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
3183 &OCFS2_I(inode)->ip_open_lockres);
3189 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
3190 &OCFS2_I(inode)->ip_inode_lockres);
3193 if (err < 0 && !status)
3196 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
3197 &OCFS2_I(inode)->ip_rw_lockres);
3200 if (err < 0 && !status)
3207 static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
3210 assert_spin_locked(&lockres->l_lock);
3212 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
3214 if (lockres->l_level <= new_level) {
3215 mlog(ML_ERROR, "lockres->l_level (%d) <= new_level (%d)\n",
3216 lockres->l_level, new_level);
3220 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
3221 lockres->l_name, new_level, lockres->l_blocking);
3223 lockres->l_action = OCFS2_AST_DOWNCONVERT;
3224 lockres->l_requested = new_level;
3225 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3226 return lockres_set_pending(lockres);
3229 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
3230 struct ocfs2_lock_res *lockres,
3233 unsigned int generation)
3236 u32 dlm_flags = DLM_LKF_CONVERT;
3241 dlm_flags |= DLM_LKF_VALBLK;
3243 ret = ocfs2_dlm_lock(osb->cconn,
3248 OCFS2_LOCK_ID_MAX_LEN - 1,
3250 lockres_clear_pending(lockres, generation, osb);
3252 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
3253 ocfs2_recover_from_dlm_error(lockres, 1);
3263 /* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
3264 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
3265 struct ocfs2_lock_res *lockres)
3267 assert_spin_locked(&lockres->l_lock);
3270 mlog(0, "lock %s\n", lockres->l_name);
3272 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
3273 /* If we're already trying to cancel a lock conversion
3274 * then just drop the spinlock and allow the caller to
3275 * requeue this lock. */
3277 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
3281 /* were we in a convert when we got the bast fire? */
3282 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
3283 lockres->l_action != OCFS2_AST_DOWNCONVERT);
3284 /* set things up for the unlockast to know to just
3285 * clear out the ast_action and unset busy, etc. */
3286 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
3288 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
3289 "lock %s, invalid flags: 0x%lx\n",
3290 lockres->l_name, lockres->l_flags);
3295 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
3296 struct ocfs2_lock_res *lockres)
3301 mlog(0, "lock %s\n", lockres->l_name);
3303 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
3304 DLM_LKF_CANCEL, lockres);
3306 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
3307 ocfs2_recover_from_dlm_error(lockres, 0);
3310 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres->l_name);
3316 static int ocfs2_unblock_lock(struct ocfs2_super *osb,
3317 struct ocfs2_lock_res *lockres,
3318 struct ocfs2_unblock_ctl *ctl)
3320 unsigned long flags;
3329 spin_lock_irqsave(&lockres->l_lock, flags);
3331 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
3334 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
3336 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3337 * exists entirely for one reason - another thread has set
3338 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3340 * If we do ocfs2_cancel_convert() before the other thread
3341 * calls dlm_lock(), our cancel will do nothing. We will
3342 * get no ast, and we will have no way of knowing the
3343 * cancel failed. Meanwhile, the other thread will call
3344 * into dlm_lock() and wait...forever.
3346 * Why forever? Because another node has asked for the
3347 * lock first; that's why we're here in unblock_lock().
3349 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3350 * set, we just requeue the unblock. Only when the other
3351 * thread has called dlm_lock() and cleared PENDING will
3352 * we then cancel their request.
3354 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3355 * at the same time they set OCFS2_DLM_BUSY. They must
3356 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3358 if (lockres->l_flags & OCFS2_LOCK_PENDING)
3362 ret = ocfs2_prepare_cancel_convert(osb, lockres);
3363 spin_unlock_irqrestore(&lockres->l_lock, flags);
3365 ret = ocfs2_cancel_convert(osb, lockres);
3372 /* if we're blocking an exclusive and we have *any* holders,
3374 if ((lockres->l_blocking == DLM_LOCK_EX)
3375 && (lockres->l_ex_holders || lockres->l_ro_holders))
3378 /* If it's a PR we're blocking, then only
3379 * requeue if we've got any EX holders */
3380 if (lockres->l_blocking == DLM_LOCK_PR &&
3381 lockres->l_ex_holders)
3385 * Can we get a lock in this state if the holder counts are
3386 * zero? The meta data unblock code used to check this.
3388 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
3389 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
3392 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
3394 if (lockres->l_ops->check_downconvert
3395 && !lockres->l_ops->check_downconvert(lockres, new_level))
3398 /* If we get here, then we know that there are no more
3399 * incompatible holders (and anyone asking for an incompatible
3400 * lock is blocked). We can now downconvert the lock */
3401 if (!lockres->l_ops->downconvert_worker)
3404 /* Some lockres types want to do a bit of work before
3405 * downconverting a lock. Allow that here. The worker function
3406 * may sleep, so we save off a copy of what we're blocking as
3407 * it may change while we're not holding the spin lock. */
3408 blocking = lockres->l_blocking;
3409 spin_unlock_irqrestore(&lockres->l_lock, flags);
3411 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
3413 if (ctl->unblock_action == UNBLOCK_STOP_POST)
3416 spin_lock_irqsave(&lockres->l_lock, flags);
3417 if (blocking != lockres->l_blocking) {
3418 /* If this changed underneath us, then we can't drop
3426 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3427 if (lockres->l_level == DLM_LOCK_EX)
3431 * We only set the lvb if the lock has been fully
3432 * refreshed - otherwise we risk setting stale
3433 * data. Otherwise, there's no need to actually clear
3434 * out the lvb here as it's value is still valid.
3436 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3437 lockres->l_ops->set_lvb(lockres);
3440 gen = ocfs2_prepare_downconvert(lockres, new_level);
3441 spin_unlock_irqrestore(&lockres->l_lock, flags);
3442 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
3450 spin_unlock_irqrestore(&lockres->l_lock, flags);
3457 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3460 struct inode *inode;
3461 struct address_space *mapping;
3463 inode = ocfs2_lock_res_inode(lockres);
3464 mapping = inode->i_mapping;
3466 if (!S_ISREG(inode->i_mode))
3470 * We need this before the filemap_fdatawrite() so that it can
3471 * transfer the dirty bit from the PTE to the
3472 * page. Unfortunately this means that even for EX->PR
3473 * downconverts, we'll lose our mappings and have to build
3476 unmap_mapping_range(mapping, 0, 0, 0);
3478 if (filemap_fdatawrite(mapping)) {
3479 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3480 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3482 sync_mapping_buffers(mapping);
3483 if (blocking == DLM_LOCK_EX) {
3484 truncate_inode_pages(mapping, 0);
3486 /* We only need to wait on the I/O if we're not also
3487 * truncating pages because truncate_inode_pages waits
3488 * for us above. We don't truncate pages if we're
3489 * blocking anything < EXMODE because we want to keep
3490 * them around in that case. */
3491 filemap_fdatawait(mapping);
3495 return UNBLOCK_CONTINUE;
3498 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3501 struct inode *inode = ocfs2_lock_res_inode(lockres);
3502 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
3504 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3505 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
3510 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
3514 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3516 struct inode *inode = ocfs2_lock_res_inode(lockres);
3518 __ocfs2_stuff_meta_lvb(inode);
3522 * Does the final reference drop on our dentry lock. Right now this
3523 * happens in the downconvert thread, but we could choose to simplify the
3524 * dlmglue API and push these off to the ocfs2_wq in the future.
3526 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3527 struct ocfs2_lock_res *lockres)
3529 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3530 ocfs2_dentry_lock_put(osb, dl);
3534 * d_delete() matching dentries before the lock downconvert.
3536 * At this point, any process waiting to destroy the
3537 * dentry_lock due to last ref count is stopped by the
3538 * OCFS2_LOCK_QUEUED flag.
3540 * We have two potential problems
3542 * 1) If we do the last reference drop on our dentry_lock (via dput)
3543 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3544 * the downconvert to finish. Instead we take an elevated
3545 * reference and push the drop until after we've completed our
3546 * unblock processing.
3548 * 2) There might be another process with a final reference,
3549 * waiting on us to finish processing. If this is the case, we
3550 * detect it and exit out - there's no more dentries anyway.
3552 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3555 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3556 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3557 struct dentry *dentry;
3558 unsigned long flags;
3562 * This node is blocking another node from getting a read
3563 * lock. This happens when we've renamed within a
3564 * directory. We've forced the other nodes to d_delete(), but
3565 * we never actually dropped our lock because it's still
3566 * valid. The downconvert code will retain a PR for this node,
3567 * so there's no further work to do.
3569 if (blocking == DLM_LOCK_PR)
3570 return UNBLOCK_CONTINUE;
3573 * Mark this inode as potentially orphaned. The code in
3574 * ocfs2_delete_inode() will figure out whether it actually
3575 * needs to be freed or not.
3577 spin_lock(&oi->ip_lock);
3578 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3579 spin_unlock(&oi->ip_lock);
3582 * Yuck. We need to make sure however that the check of
3583 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3584 * respect to a reference decrement or the setting of that
3587 spin_lock_irqsave(&lockres->l_lock, flags);
3588 spin_lock(&dentry_attach_lock);
3589 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3594 spin_unlock(&dentry_attach_lock);
3595 spin_unlock_irqrestore(&lockres->l_lock, flags);
3597 mlog(0, "extra_ref = %d\n", extra_ref);
3600 * We have a process waiting on us in ocfs2_dentry_iput(),
3601 * which means we can't have any more outstanding
3602 * aliases. There's no need to do any more work.
3605 return UNBLOCK_CONTINUE;
3607 spin_lock(&dentry_attach_lock);
3609 dentry = ocfs2_find_local_alias(dl->dl_inode,
3610 dl->dl_parent_blkno, 1);
3613 spin_unlock(&dentry_attach_lock);
3615 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3616 dentry->d_name.name);
3619 * The following dcache calls may do an
3620 * iput(). Normally we don't want that from the
3621 * downconverting thread, but in this case it's ok
3622 * because the requesting node already has an
3623 * exclusive lock on the inode, so it can't be queued
3624 * for a downconvert.
3629 spin_lock(&dentry_attach_lock);
3631 spin_unlock(&dentry_attach_lock);
3634 * If we are the last holder of this dentry lock, there is no
3635 * reason to downconvert so skip straight to the unlock.
3637 if (dl->dl_count == 1)
3638 return UNBLOCK_STOP_POST;
3640 return UNBLOCK_CONTINUE_POST;
3643 static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres)
3645 struct ocfs2_qinfo_lvb *lvb;
3646 struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres);
3647 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3648 oinfo->dqi_gi.dqi_type);
3652 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
3653 lvb->lvb_version = OCFS2_QINFO_LVB_VERSION;
3654 lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace);
3655 lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace);
3656 lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms);
3657 lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks);
3658 lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk);
3659 lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry);
3664 void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3666 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3667 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3668 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3671 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
3672 ocfs2_cluster_unlock(osb, lockres, level);
3676 static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo)
3678 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3679 oinfo->dqi_gi.dqi_type);
3680 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3681 struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
3682 struct buffer_head *bh = NULL;
3683 struct ocfs2_global_disk_dqinfo *gdinfo;
3686 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
3687 lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) {
3688 info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace);
3689 info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace);
3690 oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms);
3691 oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks);
3692 oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk);
3693 oinfo->dqi_gi.dqi_free_entry =
3694 be32_to_cpu(lvb->lvb_free_entry);
3696 status = ocfs2_read_quota_block(oinfo->dqi_gqinode, 0, &bh);
3701 gdinfo = (struct ocfs2_global_disk_dqinfo *)
3702 (bh->b_data + OCFS2_GLOBAL_INFO_OFF);
3703 info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace);
3704 info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace);
3705 oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms);
3706 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks);
3707 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk);
3708 oinfo->dqi_gi.dqi_free_entry =
3709 le32_to_cpu(gdinfo->dqi_free_entry);
3711 ocfs2_track_lock_refresh(lockres);
3718 /* Lock quota info, this function expects at least shared lock on the quota file
3719 * so that we can safely refresh quota info from disk. */
3720 int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3722 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3723 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3724 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3729 /* On RO devices, locking really isn't needed... */
3730 if (ocfs2_is_hard_readonly(osb)) {
3735 if (ocfs2_mount_local(osb))
3738 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
3743 if (!ocfs2_should_refresh_lock_res(lockres))
3745 /* OK, we have the lock but we need to refresh the quota info */
3746 status = ocfs2_refresh_qinfo(oinfo);
3748 ocfs2_qinfo_unlock(oinfo, ex);
3749 ocfs2_complete_lock_res_refresh(lockres, status);
3756 * This is the filesystem locking protocol. It provides the lock handling
3757 * hooks for the underlying DLM. It has a maximum version number.
3758 * The version number allows interoperability with systems running at
3759 * the same major number and an equal or smaller minor number.
3761 * Whenever the filesystem does new things with locks (adds or removes a
3762 * lock, orders them differently, does different things underneath a lock),
3763 * the version must be changed. The protocol is negotiated when joining
3764 * the dlm domain. A node may join the domain if its major version is
3765 * identical to all other nodes and its minor version is greater than
3766 * or equal to all other nodes. When its minor version is greater than
3767 * the other nodes, it will run at the minor version specified by the
3770 * If a locking change is made that will not be compatible with older
3771 * versions, the major number must be increased and the minor version set
3772 * to zero. If a change merely adds a behavior that can be disabled when
3773 * speaking to older versions, the minor version must be increased. If a
3774 * change adds a fully backwards compatible change (eg, LVB changes that
3775 * are just ignored by older versions), the version does not need to be
3778 static struct ocfs2_locking_protocol lproto = {
3780 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
3781 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
3783 .lp_lock_ast = ocfs2_locking_ast,
3784 .lp_blocking_ast = ocfs2_blocking_ast,
3785 .lp_unlock_ast = ocfs2_unlock_ast,
3788 void ocfs2_set_locking_protocol(void)
3790 ocfs2_stack_glue_set_locking_protocol(&lproto);
3794 static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3795 struct ocfs2_lock_res *lockres)
3798 struct ocfs2_unblock_ctl ctl = {0, 0,};
3799 unsigned long flags;
3801 /* Our reference to the lockres in this function can be
3802 * considered valid until we remove the OCFS2_LOCK_QUEUED
3808 BUG_ON(!lockres->l_ops);
3810 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3812 /* Detect whether a lock has been marked as going away while
3813 * the downconvert thread was processing other things. A lock can
3814 * still be marked with OCFS2_LOCK_FREEING after this check,
3815 * but short circuiting here will still save us some
3817 spin_lock_irqsave(&lockres->l_lock, flags);
3818 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3820 spin_unlock_irqrestore(&lockres->l_lock, flags);
3822 status = ocfs2_unblock_lock(osb, lockres, &ctl);
3826 spin_lock_irqsave(&lockres->l_lock, flags);
3828 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
3829 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3831 ocfs2_schedule_blocked_lock(osb, lockres);
3833 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
3834 ctl.requeue ? "yes" : "no");
3835 spin_unlock_irqrestore(&lockres->l_lock, flags);
3837 if (ctl.unblock_action != UNBLOCK_CONTINUE
3838 && lockres->l_ops->post_unlock)
3839 lockres->l_ops->post_unlock(osb, lockres);
3844 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3845 struct ocfs2_lock_res *lockres)
3849 assert_spin_locked(&lockres->l_lock);
3851 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3852 /* Do not schedule a lock for downconvert when it's on
3853 * the way to destruction - any nodes wanting access
3854 * to the resource will get it soon. */
3855 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3856 lockres->l_name, lockres->l_flags);
3860 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3862 spin_lock(&osb->dc_task_lock);
3863 if (list_empty(&lockres->l_blocked_list)) {
3864 list_add_tail(&lockres->l_blocked_list,
3865 &osb->blocked_lock_list);
3866 osb->blocked_lock_count++;
3868 spin_unlock(&osb->dc_task_lock);
3873 static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3875 unsigned long processed;
3876 struct ocfs2_lock_res *lockres;
3880 spin_lock(&osb->dc_task_lock);
3881 /* grab this early so we know to try again if a state change and
3882 * wake happens part-way through our work */
3883 osb->dc_work_sequence = osb->dc_wake_sequence;
3885 processed = osb->blocked_lock_count;
3887 BUG_ON(list_empty(&osb->blocked_lock_list));
3889 lockres = list_entry(osb->blocked_lock_list.next,
3890 struct ocfs2_lock_res, l_blocked_list);
3891 list_del_init(&lockres->l_blocked_list);
3892 osb->blocked_lock_count--;
3893 spin_unlock(&osb->dc_task_lock);
3898 ocfs2_process_blocked_lock(osb, lockres);
3900 spin_lock(&osb->dc_task_lock);
3902 spin_unlock(&osb->dc_task_lock);
3907 static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3911 spin_lock(&osb->dc_task_lock);
3912 if (list_empty(&osb->blocked_lock_list))
3915 spin_unlock(&osb->dc_task_lock);
3919 static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
3921 int should_wake = 0;
3923 spin_lock(&osb->dc_task_lock);
3924 if (osb->dc_work_sequence != osb->dc_wake_sequence)
3926 spin_unlock(&osb->dc_task_lock);
3931 static int ocfs2_downconvert_thread(void *arg)
3934 struct ocfs2_super *osb = arg;
3936 /* only quit once we've been asked to stop and there is no more
3938 while (!(kthread_should_stop() &&
3939 ocfs2_downconvert_thread_lists_empty(osb))) {
3941 wait_event_interruptible(osb->dc_event,
3942 ocfs2_downconvert_thread_should_wake(osb) ||
3943 kthread_should_stop());
3945 mlog(0, "downconvert_thread: awoken\n");
3947 ocfs2_downconvert_thread_do_work(osb);
3950 osb->dc_task = NULL;
3954 void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
3956 spin_lock(&osb->dc_task_lock);
3957 /* make sure the voting thread gets a swipe at whatever changes
3958 * the caller may have made to the voting state */
3959 osb->dc_wake_sequence++;
3960 spin_unlock(&osb->dc_task_lock);
3961 wake_up(&osb->dc_event);