1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Code which implements an OCFS2 specific interface to our DLM.
8 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
26 #include <linux/types.h>
27 #include <linux/slab.h>
28 #include <linux/highmem.h>
30 #include <linux/kthread.h>
31 #include <linux/pagemap.h>
32 #include <linux/debugfs.h>
33 #include <linux/seq_file.h>
34 #include <linux/time.h>
35 #include <linux/quotaops.h>
37 #define MLOG_MASK_PREFIX ML_DLM_GLUE
38 #include <cluster/masklog.h>
41 #include "ocfs2_lockingver.h"
46 #include "extent_map.h"
48 #include "heartbeat.h"
51 #include "stackglue.h"
57 #include "buffer_head_io.h"
59 struct ocfs2_mask_waiter {
60 struct list_head mw_item;
62 struct completion mw_complete;
63 unsigned long mw_mask;
64 unsigned long mw_goal;
65 #ifdef CONFIG_OCFS2_FS_STATS
66 unsigned long long mw_lock_start;
70 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
71 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
72 static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres);
73 static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres);
76 * Return value from ->downconvert_worker functions.
78 * These control the precise actions of ocfs2_unblock_lock()
79 * and ocfs2_process_blocked_lock()
82 enum ocfs2_unblock_action {
83 UNBLOCK_CONTINUE = 0, /* Continue downconvert */
84 UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire
85 * ->post_unlock callback */
86 UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire
87 * ->post_unlock() callback. */
90 struct ocfs2_unblock_ctl {
92 enum ocfs2_unblock_action unblock_action;
95 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
97 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
99 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
102 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
105 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
106 struct ocfs2_lock_res *lockres);
108 static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres);
110 #define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres)
112 /* This aids in debugging situations where a bad LVB might be involved. */
113 static void ocfs2_dump_meta_lvb_info(u64 level,
114 const char *function,
116 struct ocfs2_lock_res *lockres)
118 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
120 mlog(level, "LVB information for %s (called from %s:%u):\n",
121 lockres->l_name, function, line);
122 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
123 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
124 be32_to_cpu(lvb->lvb_igeneration));
125 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
126 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
127 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
128 be16_to_cpu(lvb->lvb_imode));
129 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
130 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
131 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
132 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
133 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
134 be32_to_cpu(lvb->lvb_iattr));
139 * OCFS2 Lock Resource Operations
141 * These fine tune the behavior of the generic dlmglue locking infrastructure.
143 * The most basic of lock types can point ->l_priv to their respective
144 * struct ocfs2_super and allow the default actions to manage things.
146 * Right now, each lock type also needs to implement an init function,
147 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
148 * should be called when the lock is no longer needed (i.e., object
151 struct ocfs2_lock_res_ops {
153 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
154 * this callback if ->l_priv is not an ocfs2_super pointer
156 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
159 * Optionally called in the downconvert thread after a
160 * successful downconvert. The lockres will not be referenced
161 * after this callback is called, so it is safe to free
164 * The exact semantics of when this is called are controlled
165 * by ->downconvert_worker()
167 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
170 * Allow a lock type to add checks to determine whether it is
171 * safe to downconvert a lock. Return 0 to re-queue the
172 * downconvert at a later time, nonzero to continue.
174 * For most locks, the default checks that there are no
175 * incompatible holders are sufficient.
177 * Called with the lockres spinlock held.
179 int (*check_downconvert)(struct ocfs2_lock_res *, int);
182 * Allows a lock type to populate the lock value block. This
183 * is called on downconvert, and when we drop a lock.
185 * Locks that want to use this should set LOCK_TYPE_USES_LVB
186 * in the flags field.
188 * Called with the lockres spinlock held.
190 void (*set_lvb)(struct ocfs2_lock_res *);
193 * Called from the downconvert thread when it is determined
194 * that a lock will be downconverted. This is called without
195 * any locks held so the function can do work that might
196 * schedule (syncing out data, etc).
198 * This should return any one of the ocfs2_unblock_action
199 * values, depending on what it wants the thread to do.
201 int (*downconvert_worker)(struct ocfs2_lock_res *, int);
204 * LOCK_TYPE_* flags which describe the specific requirements
205 * of a lock type. Descriptions of each individual flag follow.
211 * Some locks want to "refresh" potentially stale data when a
212 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
213 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
214 * individual lockres l_flags member from the ast function. It is
215 * expected that the locking wrapper will clear the
216 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
218 #define LOCK_TYPE_REQUIRES_REFRESH 0x1
221 * Indicate that a lock type makes use of the lock value block. The
222 * ->set_lvb lock type callback must be defined.
224 #define LOCK_TYPE_USES_LVB 0x2
226 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
227 .get_osb = ocfs2_get_inode_osb,
231 static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops = {
232 .get_osb = ocfs2_get_inode_osb,
233 .check_downconvert = ocfs2_check_meta_downconvert,
234 .set_lvb = ocfs2_set_meta_lvb,
235 .downconvert_worker = ocfs2_data_convert_worker,
236 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
239 static struct ocfs2_lock_res_ops ocfs2_super_lops = {
240 .flags = LOCK_TYPE_REQUIRES_REFRESH,
243 static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
247 static struct ocfs2_lock_res_ops ocfs2_nfs_sync_lops = {
251 static struct ocfs2_lock_res_ops ocfs2_orphan_scan_lops = {
252 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
255 static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
256 .get_osb = ocfs2_get_dentry_osb,
257 .post_unlock = ocfs2_dentry_post_unlock,
258 .downconvert_worker = ocfs2_dentry_convert_worker,
262 static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
263 .get_osb = ocfs2_get_inode_osb,
267 static struct ocfs2_lock_res_ops ocfs2_flock_lops = {
268 .get_osb = ocfs2_get_file_osb,
272 static struct ocfs2_lock_res_ops ocfs2_qinfo_lops = {
273 .set_lvb = ocfs2_set_qinfo_lvb,
274 .get_osb = ocfs2_get_qinfo_osb,
275 .flags = LOCK_TYPE_REQUIRES_REFRESH | LOCK_TYPE_USES_LVB,
278 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
280 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
281 lockres->l_type == OCFS2_LOCK_TYPE_RW ||
282 lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
285 static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
287 BUG_ON(!ocfs2_is_inode_lock(lockres));
289 return (struct inode *) lockres->l_priv;
292 static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
294 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
296 return (struct ocfs2_dentry_lock *)lockres->l_priv;
299 static inline struct ocfs2_mem_dqinfo *ocfs2_lock_res_qinfo(struct ocfs2_lock_res *lockres)
301 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_QINFO);
303 return (struct ocfs2_mem_dqinfo *)lockres->l_priv;
306 static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
308 if (lockres->l_ops->get_osb)
309 return lockres->l_ops->get_osb(lockres);
311 return (struct ocfs2_super *)lockres->l_priv;
314 static int ocfs2_lock_create(struct ocfs2_super *osb,
315 struct ocfs2_lock_res *lockres,
318 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
320 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
321 struct ocfs2_lock_res *lockres,
323 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
324 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
325 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
326 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
327 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
328 struct ocfs2_lock_res *lockres);
329 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
331 #define ocfs2_log_dlm_error(_func, _err, _lockres) do { \
332 if ((_lockres)->l_type != OCFS2_LOCK_TYPE_DENTRY) \
333 mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \
334 _err, _func, _lockres->l_name); \
336 mlog(ML_ERROR, "DLM error %d while calling %s on resource %.*s%08x\n", \
337 _err, _func, OCFS2_DENTRY_LOCK_INO_START - 1, (_lockres)->l_name, \
338 (unsigned int)ocfs2_get_dentry_lock_ino(_lockres)); \
340 static int ocfs2_downconvert_thread(void *arg);
341 static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
342 struct ocfs2_lock_res *lockres);
343 static int ocfs2_inode_lock_update(struct inode *inode,
344 struct buffer_head **bh);
345 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
346 static inline int ocfs2_highest_compat_lock_level(int level);
347 static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
349 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
350 struct ocfs2_lock_res *lockres,
353 unsigned int generation);
354 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
355 struct ocfs2_lock_res *lockres);
356 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
357 struct ocfs2_lock_res *lockres);
360 static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
369 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
371 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
372 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
373 (long long)blkno, generation);
375 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
377 mlog(0, "built lock resource with name: %s\n", name);
382 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
384 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
385 struct ocfs2_dlm_debug *dlm_debug)
387 mlog(0, "Add tracking for lockres %s\n", res->l_name);
389 spin_lock(&ocfs2_dlm_tracking_lock);
390 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
391 spin_unlock(&ocfs2_dlm_tracking_lock);
394 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
396 spin_lock(&ocfs2_dlm_tracking_lock);
397 if (!list_empty(&res->l_debug_list))
398 list_del_init(&res->l_debug_list);
399 spin_unlock(&ocfs2_dlm_tracking_lock);
402 #ifdef CONFIG_OCFS2_FS_STATS
403 static void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
405 res->l_lock_num_prmode = 0;
406 res->l_lock_num_prmode_failed = 0;
407 res->l_lock_total_prmode = 0;
408 res->l_lock_max_prmode = 0;
409 res->l_lock_num_exmode = 0;
410 res->l_lock_num_exmode_failed = 0;
411 res->l_lock_total_exmode = 0;
412 res->l_lock_max_exmode = 0;
413 res->l_lock_refresh = 0;
416 static void ocfs2_update_lock_stats(struct ocfs2_lock_res *res, int level,
417 struct ocfs2_mask_waiter *mw, int ret)
419 unsigned long long *num, *sum;
420 unsigned int *max, *failed;
421 struct timespec ts = current_kernel_time();
422 unsigned long long time = timespec_to_ns(&ts) - mw->mw_lock_start;
424 if (level == LKM_PRMODE) {
425 num = &res->l_lock_num_prmode;
426 sum = &res->l_lock_total_prmode;
427 max = &res->l_lock_max_prmode;
428 failed = &res->l_lock_num_prmode_failed;
429 } else if (level == LKM_EXMODE) {
430 num = &res->l_lock_num_exmode;
431 sum = &res->l_lock_total_exmode;
432 max = &res->l_lock_max_exmode;
433 failed = &res->l_lock_num_exmode_failed;
445 static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
447 lockres->l_lock_refresh++;
450 static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
452 struct timespec ts = current_kernel_time();
453 mw->mw_lock_start = timespec_to_ns(&ts);
456 static inline void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
459 static inline void ocfs2_update_lock_stats(struct ocfs2_lock_res *res,
460 int level, struct ocfs2_mask_waiter *mw, int ret)
463 static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
466 static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
471 static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
472 struct ocfs2_lock_res *res,
473 enum ocfs2_lock_type type,
474 struct ocfs2_lock_res_ops *ops,
481 res->l_level = DLM_LOCK_IV;
482 res->l_requested = DLM_LOCK_IV;
483 res->l_blocking = DLM_LOCK_IV;
484 res->l_action = OCFS2_AST_INVALID;
485 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
487 res->l_flags = OCFS2_LOCK_INITIALIZED;
489 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
491 ocfs2_init_lock_stats(res);
494 void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
496 /* This also clears out the lock status block */
497 memset(res, 0, sizeof(struct ocfs2_lock_res));
498 spin_lock_init(&res->l_lock);
499 init_waitqueue_head(&res->l_event);
500 INIT_LIST_HEAD(&res->l_blocked_list);
501 INIT_LIST_HEAD(&res->l_mask_waiters);
504 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
505 enum ocfs2_lock_type type,
506 unsigned int generation,
509 struct ocfs2_lock_res_ops *ops;
512 case OCFS2_LOCK_TYPE_RW:
513 ops = &ocfs2_inode_rw_lops;
515 case OCFS2_LOCK_TYPE_META:
516 ops = &ocfs2_inode_inode_lops;
518 case OCFS2_LOCK_TYPE_OPEN:
519 ops = &ocfs2_inode_open_lops;
522 mlog_bug_on_msg(1, "type: %d\n", type);
523 ops = NULL; /* thanks, gcc */
527 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
528 generation, res->l_name);
529 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
532 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
534 struct inode *inode = ocfs2_lock_res_inode(lockres);
536 return OCFS2_SB(inode->i_sb);
539 static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres)
541 struct ocfs2_mem_dqinfo *info = lockres->l_priv;
543 return OCFS2_SB(info->dqi_gi.dqi_sb);
546 static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
548 struct ocfs2_file_private *fp = lockres->l_priv;
550 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
553 static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
555 __be64 inode_blkno_be;
557 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
560 return be64_to_cpu(inode_blkno_be);
563 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
565 struct ocfs2_dentry_lock *dl = lockres->l_priv;
567 return OCFS2_SB(dl->dl_inode->i_sb);
570 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
571 u64 parent, struct inode *inode)
574 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
575 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
576 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
578 ocfs2_lock_res_init_once(lockres);
581 * Unfortunately, the standard lock naming scheme won't work
582 * here because we have two 16 byte values to use. Instead,
583 * we'll stuff the inode number as a binary value. We still
584 * want error prints to show something without garbling the
585 * display, so drop a null byte in there before the inode
586 * number. A future version of OCFS2 will likely use all
587 * binary lock names. The stringified names have been a
588 * tremendous aid in debugging, but now that the debugfs
589 * interface exists, we can mangle things there if need be.
591 * NOTE: We also drop the standard "pad" value (the total lock
592 * name size stays the same though - the last part is all
593 * zeros due to the memset in ocfs2_lock_res_init_once()
595 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
597 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
600 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
602 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
605 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
606 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
610 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
611 struct ocfs2_super *osb)
613 /* Superblock lockres doesn't come from a slab so we call init
614 * once on it manually. */
615 ocfs2_lock_res_init_once(res);
616 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
618 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
619 &ocfs2_super_lops, osb);
622 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
623 struct ocfs2_super *osb)
625 /* Rename lockres doesn't come from a slab so we call init
626 * once on it manually. */
627 ocfs2_lock_res_init_once(res);
628 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
629 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
630 &ocfs2_rename_lops, osb);
633 static void ocfs2_nfs_sync_lock_res_init(struct ocfs2_lock_res *res,
634 struct ocfs2_super *osb)
636 /* nfs_sync lockres doesn't come from a slab so we call init
637 * once on it manually. */
638 ocfs2_lock_res_init_once(res);
639 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_NFS_SYNC, 0, 0, res->l_name);
640 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_NFS_SYNC,
641 &ocfs2_nfs_sync_lops, osb);
644 static void ocfs2_orphan_scan_lock_res_init(struct ocfs2_lock_res *res,
645 struct ocfs2_super *osb)
647 struct ocfs2_orphan_scan_lvb *lvb;
649 ocfs2_lock_res_init_once(res);
650 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_ORPHAN_SCAN, 0, 0, res->l_name);
651 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_ORPHAN_SCAN,
652 &ocfs2_orphan_scan_lops, osb);
653 lvb = ocfs2_dlm_lvb(&res->l_lksb);
654 lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION;
657 void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
658 struct ocfs2_file_private *fp)
660 struct inode *inode = fp->fp_file->f_mapping->host;
661 struct ocfs2_inode_info *oi = OCFS2_I(inode);
663 ocfs2_lock_res_init_once(lockres);
664 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
665 inode->i_generation, lockres->l_name);
666 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
667 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
669 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
672 void ocfs2_qinfo_lock_res_init(struct ocfs2_lock_res *lockres,
673 struct ocfs2_mem_dqinfo *info)
675 ocfs2_lock_res_init_once(lockres);
676 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_QINFO, info->dqi_gi.dqi_type,
678 ocfs2_lock_res_init_common(OCFS2_SB(info->dqi_gi.dqi_sb), lockres,
679 OCFS2_LOCK_TYPE_QINFO, &ocfs2_qinfo_lops,
683 void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
687 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
690 ocfs2_remove_lockres_tracking(res);
692 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
693 "Lockres %s is on the blocked list\n",
695 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
696 "Lockres %s has mask waiters pending\n",
698 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
699 "Lockres %s is locked\n",
701 mlog_bug_on_msg(res->l_ro_holders,
702 "Lockres %s has %u ro holders\n",
703 res->l_name, res->l_ro_holders);
704 mlog_bug_on_msg(res->l_ex_holders,
705 "Lockres %s has %u ex holders\n",
706 res->l_name, res->l_ex_holders);
708 /* Need to clear out the lock status block for the dlm */
709 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
715 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
724 lockres->l_ex_holders++;
727 lockres->l_ro_holders++;
736 static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
745 BUG_ON(!lockres->l_ex_holders);
746 lockres->l_ex_holders--;
749 BUG_ON(!lockres->l_ro_holders);
750 lockres->l_ro_holders--;
758 /* WARNING: This function lives in a world where the only three lock
759 * levels are EX, PR, and NL. It *will* have to be adjusted when more
760 * lock types are added. */
761 static inline int ocfs2_highest_compat_lock_level(int level)
763 int new_level = DLM_LOCK_EX;
765 if (level == DLM_LOCK_EX)
766 new_level = DLM_LOCK_NL;
767 else if (level == DLM_LOCK_PR)
768 new_level = DLM_LOCK_PR;
772 static void lockres_set_flags(struct ocfs2_lock_res *lockres,
773 unsigned long newflags)
775 struct ocfs2_mask_waiter *mw, *tmp;
777 assert_spin_locked(&lockres->l_lock);
779 lockres->l_flags = newflags;
781 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
782 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
785 list_del_init(&mw->mw_item);
787 complete(&mw->mw_complete);
790 static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
792 lockres_set_flags(lockres, lockres->l_flags | or);
794 static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
797 lockres_set_flags(lockres, lockres->l_flags & ~clear);
800 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
804 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
805 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
806 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
807 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
809 lockres->l_level = lockres->l_requested;
810 if (lockres->l_level <=
811 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
812 lockres->l_blocking = DLM_LOCK_NL;
813 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
815 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
820 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
824 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
825 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
827 /* Convert from RO to EX doesn't really need anything as our
828 * information is already up to data. Convert from NL to
829 * *anything* however should mark ourselves as needing an
831 if (lockres->l_level == DLM_LOCK_NL &&
832 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
833 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
835 lockres->l_level = lockres->l_requested;
836 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
841 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
845 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
846 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
848 if (lockres->l_requested > DLM_LOCK_NL &&
849 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
850 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
851 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
853 lockres->l_level = lockres->l_requested;
854 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
855 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
860 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
863 int needs_downconvert = 0;
866 assert_spin_locked(&lockres->l_lock);
868 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
870 if (level > lockres->l_blocking) {
871 /* only schedule a downconvert if we haven't already scheduled
872 * one that goes low enough to satisfy the level we're
873 * blocking. this also catches the case where we get
875 if (ocfs2_highest_compat_lock_level(level) <
876 ocfs2_highest_compat_lock_level(lockres->l_blocking))
877 needs_downconvert = 1;
879 lockres->l_blocking = level;
882 mlog_exit(needs_downconvert);
883 return needs_downconvert;
887 * OCFS2_LOCK_PENDING and l_pending_gen.
889 * Why does OCFS2_LOCK_PENDING exist? To close a race between setting
890 * OCFS2_LOCK_BUSY and calling ocfs2_dlm_lock(). See ocfs2_unblock_lock()
891 * for more details on the race.
893 * OCFS2_LOCK_PENDING closes the race quite nicely. However, it introduces
894 * a race on itself. In o2dlm, we can get the ast before ocfs2_dlm_lock()
895 * returns. The ast clears OCFS2_LOCK_BUSY, and must therefore clear
896 * OCFS2_LOCK_PENDING at the same time. When ocfs2_dlm_lock() returns,
897 * the caller is going to try to clear PENDING again. If nothing else is
898 * happening, __lockres_clear_pending() sees PENDING is unset and does
901 * But what if another path (eg downconvert thread) has just started a
902 * new locking action? The other path has re-set PENDING. Our path
903 * cannot clear PENDING, because that will re-open the original race
909 * ocfs2_cluster_lock()
914 * ocfs2_locking_ast() ocfs2_downconvert_thread()
915 * clear PENDING ocfs2_unblock_lock()
918 * ocfs2_prepare_downconvert()
928 * So as you can see, we now have a window where l_lock is not held,
929 * PENDING is not set, and ocfs2_dlm_lock() has not been called.
931 * The core problem is that ocfs2_cluster_lock() has cleared the PENDING
932 * set by ocfs2_prepare_downconvert(). That wasn't nice.
934 * To solve this we introduce l_pending_gen. A call to
935 * lockres_clear_pending() will only do so when it is passed a generation
936 * number that matches the lockres. lockres_set_pending() will return the
937 * current generation number. When ocfs2_cluster_lock() goes to clear
938 * PENDING, it passes the generation it got from set_pending(). In our
939 * example above, the generation numbers will *not* match. Thus,
940 * ocfs2_cluster_lock() will not clear the PENDING set by
941 * ocfs2_prepare_downconvert().
944 /* Unlocked version for ocfs2_locking_ast() */
945 static void __lockres_clear_pending(struct ocfs2_lock_res *lockres,
946 unsigned int generation,
947 struct ocfs2_super *osb)
949 assert_spin_locked(&lockres->l_lock);
952 * The ast and locking functions can race us here. The winner
953 * will clear pending, the loser will not.
955 if (!(lockres->l_flags & OCFS2_LOCK_PENDING) ||
956 (lockres->l_pending_gen != generation))
959 lockres_clear_flags(lockres, OCFS2_LOCK_PENDING);
960 lockres->l_pending_gen++;
963 * The downconvert thread may have skipped us because we
964 * were PENDING. Wake it up.
966 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
967 ocfs2_wake_downconvert_thread(osb);
970 /* Locked version for callers of ocfs2_dlm_lock() */
971 static void lockres_clear_pending(struct ocfs2_lock_res *lockres,
972 unsigned int generation,
973 struct ocfs2_super *osb)
977 spin_lock_irqsave(&lockres->l_lock, flags);
978 __lockres_clear_pending(lockres, generation, osb);
979 spin_unlock_irqrestore(&lockres->l_lock, flags);
982 static unsigned int lockres_set_pending(struct ocfs2_lock_res *lockres)
984 assert_spin_locked(&lockres->l_lock);
985 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
987 lockres_or_flags(lockres, OCFS2_LOCK_PENDING);
989 return lockres->l_pending_gen;
993 static void ocfs2_blocking_ast(void *opaque, int level)
995 struct ocfs2_lock_res *lockres = opaque;
996 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
997 int needs_downconvert;
1000 BUG_ON(level <= DLM_LOCK_NL);
1002 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
1003 lockres->l_name, level, lockres->l_level,
1004 ocfs2_lock_type_string(lockres->l_type));
1007 * We can skip the bast for locks which don't enable caching -
1008 * they'll be dropped at the earliest possible time anyway.
1010 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
1013 spin_lock_irqsave(&lockres->l_lock, flags);
1014 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
1015 if (needs_downconvert)
1016 ocfs2_schedule_blocked_lock(osb, lockres);
1017 spin_unlock_irqrestore(&lockres->l_lock, flags);
1019 wake_up(&lockres->l_event);
1021 ocfs2_wake_downconvert_thread(osb);
1024 static void ocfs2_locking_ast(void *opaque)
1026 struct ocfs2_lock_res *lockres = opaque;
1027 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1028 unsigned long flags;
1031 spin_lock_irqsave(&lockres->l_lock, flags);
1033 status = ocfs2_dlm_lock_status(&lockres->l_lksb);
1035 if (status == -EAGAIN) {
1036 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1041 mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
1042 lockres->l_name, status);
1043 spin_unlock_irqrestore(&lockres->l_lock, flags);
1047 switch(lockres->l_action) {
1048 case OCFS2_AST_ATTACH:
1049 ocfs2_generic_handle_attach_action(lockres);
1050 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
1052 case OCFS2_AST_CONVERT:
1053 ocfs2_generic_handle_convert_action(lockres);
1055 case OCFS2_AST_DOWNCONVERT:
1056 ocfs2_generic_handle_downconvert_action(lockres);
1059 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
1060 "lockres flags = 0x%lx, unlock action: %u\n",
1061 lockres->l_name, lockres->l_action, lockres->l_flags,
1062 lockres->l_unlock_action);
1066 /* set it to something invalid so if we get called again we
1068 lockres->l_action = OCFS2_AST_INVALID;
1070 /* Did we try to cancel this lock? Clear that state */
1071 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT)
1072 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1075 * We may have beaten the locking functions here. We certainly
1076 * know that dlm_lock() has been called :-)
1077 * Because we can't have two lock calls in flight at once, we
1078 * can use lockres->l_pending_gen.
1080 __lockres_clear_pending(lockres, lockres->l_pending_gen, osb);
1082 wake_up(&lockres->l_event);
1083 spin_unlock_irqrestore(&lockres->l_lock, flags);
1086 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
1089 unsigned long flags;
1092 spin_lock_irqsave(&lockres->l_lock, flags);
1093 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1095 lockres->l_action = OCFS2_AST_INVALID;
1097 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1098 spin_unlock_irqrestore(&lockres->l_lock, flags);
1100 wake_up(&lockres->l_event);
1104 /* Note: If we detect another process working on the lock (i.e.,
1105 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
1106 * to do the right thing in that case.
1108 static int ocfs2_lock_create(struct ocfs2_super *osb,
1109 struct ocfs2_lock_res *lockres,
1114 unsigned long flags;
1119 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
1122 spin_lock_irqsave(&lockres->l_lock, flags);
1123 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
1124 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
1125 spin_unlock_irqrestore(&lockres->l_lock, flags);
1129 lockres->l_action = OCFS2_AST_ATTACH;
1130 lockres->l_requested = level;
1131 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1132 gen = lockres_set_pending(lockres);
1133 spin_unlock_irqrestore(&lockres->l_lock, flags);
1135 ret = ocfs2_dlm_lock(osb->cconn,
1140 OCFS2_LOCK_ID_MAX_LEN - 1,
1142 lockres_clear_pending(lockres, gen, osb);
1144 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
1145 ocfs2_recover_from_dlm_error(lockres, 1);
1148 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
1155 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
1158 unsigned long flags;
1161 spin_lock_irqsave(&lockres->l_lock, flags);
1162 ret = lockres->l_flags & flag;
1163 spin_unlock_irqrestore(&lockres->l_lock, flags);
1168 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
1171 wait_event(lockres->l_event,
1172 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
1175 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
1178 wait_event(lockres->l_event,
1179 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
1182 /* predict what lock level we'll be dropping down to on behalf
1183 * of another node, and return true if the currently wanted
1184 * level will be compatible with it. */
1185 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
1188 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
1190 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
1193 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
1195 INIT_LIST_HEAD(&mw->mw_item);
1196 init_completion(&mw->mw_complete);
1197 ocfs2_init_start_time(mw);
1200 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
1202 wait_for_completion(&mw->mw_complete);
1203 /* Re-arm the completion in case we want to wait on it again */
1204 INIT_COMPLETION(mw->mw_complete);
1205 return mw->mw_status;
1208 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
1209 struct ocfs2_mask_waiter *mw,
1213 BUG_ON(!list_empty(&mw->mw_item));
1215 assert_spin_locked(&lockres->l_lock);
1217 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
1222 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
1223 * if the mask still hadn't reached its goal */
1224 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
1225 struct ocfs2_mask_waiter *mw)
1227 unsigned long flags;
1230 spin_lock_irqsave(&lockres->l_lock, flags);
1231 if (!list_empty(&mw->mw_item)) {
1232 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
1235 list_del_init(&mw->mw_item);
1236 init_completion(&mw->mw_complete);
1238 spin_unlock_irqrestore(&lockres->l_lock, flags);
1244 static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
1245 struct ocfs2_lock_res *lockres)
1249 ret = wait_for_completion_interruptible(&mw->mw_complete);
1251 lockres_remove_mask_waiter(lockres, mw);
1253 ret = mw->mw_status;
1254 /* Re-arm the completion in case we want to wait on it again */
1255 INIT_COMPLETION(mw->mw_complete);
1259 static int ocfs2_cluster_lock(struct ocfs2_super *osb,
1260 struct ocfs2_lock_res *lockres,
1265 struct ocfs2_mask_waiter mw;
1266 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
1267 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1268 unsigned long flags;
1270 int noqueue_attempted = 0;
1274 ocfs2_init_mask_waiter(&mw);
1276 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
1277 lkm_flags |= DLM_LKF_VALBLK;
1282 if (catch_signals && signal_pending(current)) {
1287 spin_lock_irqsave(&lockres->l_lock, flags);
1289 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1290 "Cluster lock called on freeing lockres %s! flags "
1291 "0x%lx\n", lockres->l_name, lockres->l_flags);
1293 /* We only compare against the currently granted level
1294 * here. If the lock is blocked waiting on a downconvert,
1295 * we'll get caught below. */
1296 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1297 level > lockres->l_level) {
1298 /* is someone sitting in dlm_lock? If so, wait on
1300 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1305 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1306 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1307 /* is the lock is currently blocked on behalf of
1309 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1314 if (level > lockres->l_level) {
1315 if (noqueue_attempted > 0) {
1319 if (lkm_flags & DLM_LKF_NOQUEUE)
1320 noqueue_attempted = 1;
1322 if (lockres->l_action != OCFS2_AST_INVALID)
1323 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1324 lockres->l_name, lockres->l_action);
1326 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1327 lockres->l_action = OCFS2_AST_ATTACH;
1328 lkm_flags &= ~DLM_LKF_CONVERT;
1330 lockres->l_action = OCFS2_AST_CONVERT;
1331 lkm_flags |= DLM_LKF_CONVERT;
1334 lockres->l_requested = level;
1335 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1336 gen = lockres_set_pending(lockres);
1337 spin_unlock_irqrestore(&lockres->l_lock, flags);
1339 BUG_ON(level == DLM_LOCK_IV);
1340 BUG_ON(level == DLM_LOCK_NL);
1342 mlog(0, "lock %s, convert from %d to level = %d\n",
1343 lockres->l_name, lockres->l_level, level);
1345 /* call dlm_lock to upgrade lock now */
1346 ret = ocfs2_dlm_lock(osb->cconn,
1351 OCFS2_LOCK_ID_MAX_LEN - 1,
1353 lockres_clear_pending(lockres, gen, osb);
1355 if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
1357 ocfs2_log_dlm_error("ocfs2_dlm_lock",
1360 ocfs2_recover_from_dlm_error(lockres, 1);
1364 mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n",
1367 /* At this point we've gone inside the dlm and need to
1368 * complete our work regardless. */
1371 /* wait for busy to clear and carry on */
1375 /* Ok, if we get here then we're good to go. */
1376 ocfs2_inc_holders(lockres, level);
1380 spin_unlock_irqrestore(&lockres->l_lock, flags);
1383 * This is helping work around a lock inversion between the page lock
1384 * and dlm locks. One path holds the page lock while calling aops
1385 * which block acquiring dlm locks. The voting thread holds dlm
1386 * locks while acquiring page locks while down converting data locks.
1387 * This block is helping an aop path notice the inversion and back
1388 * off to unlock its page lock before trying the dlm lock again.
1390 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1391 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1393 if (lockres_remove_mask_waiter(lockres, &mw))
1399 ret = ocfs2_wait_for_mask(&mw);
1404 ocfs2_update_lock_stats(lockres, level, &mw, ret);
1410 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1411 struct ocfs2_lock_res *lockres,
1414 unsigned long flags;
1417 spin_lock_irqsave(&lockres->l_lock, flags);
1418 ocfs2_dec_holders(lockres, level);
1419 ocfs2_downconvert_on_unlock(osb, lockres);
1420 spin_unlock_irqrestore(&lockres->l_lock, flags);
1424 static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1425 struct ocfs2_lock_res *lockres,
1429 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1430 unsigned long flags;
1431 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
1433 spin_lock_irqsave(&lockres->l_lock, flags);
1434 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1435 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1436 spin_unlock_irqrestore(&lockres->l_lock, flags);
1438 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
1441 /* Grants us an EX lock on the data and metadata resources, skipping
1442 * the normal cluster directory lookup. Use this ONLY on newly created
1443 * inodes which other nodes can't possibly see, and which haven't been
1444 * hashed in the inode hash yet. This can give us a good performance
1445 * increase as it'll skip the network broadcast normally associated
1446 * with creating a new lock resource. */
1447 int ocfs2_create_new_inode_locks(struct inode *inode)
1450 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1453 BUG_ON(!ocfs2_inode_is_new(inode));
1457 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1459 /* NOTE: That we don't increment any of the holder counts, nor
1460 * do we add anything to a journal handle. Since this is
1461 * supposed to be a new inode which the cluster doesn't know
1462 * about yet, there is no need to. As far as the LVB handling
1463 * is concerned, this is basically like acquiring an EX lock
1464 * on a resource which has an invalid one -- we'll set it
1465 * valid when we release the EX. */
1467 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
1474 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
1475 * don't use a generation in their lock names.
1477 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
1483 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1494 int ocfs2_rw_lock(struct inode *inode, int write)
1497 struct ocfs2_lock_res *lockres;
1498 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1504 mlog(0, "inode %llu take %s RW lock\n",
1505 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1506 write ? "EXMODE" : "PRMODE");
1508 if (ocfs2_mount_local(osb))
1511 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1513 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1515 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1524 void ocfs2_rw_unlock(struct inode *inode, int write)
1526 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1527 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1528 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1532 mlog(0, "inode %llu drop %s RW lock\n",
1533 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1534 write ? "EXMODE" : "PRMODE");
1536 if (!ocfs2_mount_local(osb))
1537 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1543 * ocfs2_open_lock always get PR mode lock.
1545 int ocfs2_open_lock(struct inode *inode)
1548 struct ocfs2_lock_res *lockres;
1549 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1555 mlog(0, "inode %llu take PRMODE open lock\n",
1556 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1558 if (ocfs2_mount_local(osb))
1561 lockres = &OCFS2_I(inode)->ip_open_lockres;
1563 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1573 int ocfs2_try_open_lock(struct inode *inode, int write)
1575 int status = 0, level;
1576 struct ocfs2_lock_res *lockres;
1577 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1583 mlog(0, "inode %llu try to take %s open lock\n",
1584 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1585 write ? "EXMODE" : "PRMODE");
1587 if (ocfs2_mount_local(osb))
1590 lockres = &OCFS2_I(inode)->ip_open_lockres;
1592 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1595 * The file system may already holding a PRMODE/EXMODE open lock.
1596 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
1597 * other nodes and the -EAGAIN will indicate to the caller that
1598 * this inode is still in use.
1600 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1601 level, DLM_LKF_NOQUEUE, 0);
1609 * ocfs2_open_unlock unlock PR and EX mode open locks.
1611 void ocfs2_open_unlock(struct inode *inode)
1613 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1614 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1618 mlog(0, "inode %llu drop open lock\n",
1619 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1621 if (ocfs2_mount_local(osb))
1624 if(lockres->l_ro_holders)
1625 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1627 if(lockres->l_ex_holders)
1628 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1635 static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1639 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1640 unsigned long flags;
1641 struct ocfs2_mask_waiter mw;
1643 ocfs2_init_mask_waiter(&mw);
1646 spin_lock_irqsave(&lockres->l_lock, flags);
1647 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1648 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1650 spin_unlock_irqrestore(&lockres->l_lock, flags);
1651 ret = ocfs2_cancel_convert(osb, lockres);
1658 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1659 spin_unlock_irqrestore(&lockres->l_lock, flags);
1661 ocfs2_wait_for_mask(&mw);
1667 * We may still have gotten the lock, in which case there's no
1668 * point to restarting the syscall.
1670 if (lockres->l_level == level)
1673 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1674 lockres->l_flags, lockres->l_level, lockres->l_action);
1676 spin_unlock_irqrestore(&lockres->l_lock, flags);
1683 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1684 * flock() calls. The locking approach this requires is sufficiently
1685 * different from all other cluster lock types that we implement a
1686 * seperate path to the "low-level" dlm calls. In particular:
1688 * - No optimization of lock levels is done - we take at exactly
1689 * what's been requested.
1691 * - No lock caching is employed. We immediately downconvert to
1692 * no-lock at unlock time. This also means flock locks never go on
1693 * the blocking list).
1695 * - Since userspace can trivially deadlock itself with flock, we make
1696 * sure to allow cancellation of a misbehaving applications flock()
1699 * - Access to any flock lockres doesn't require concurrency, so we
1700 * can simplify the code by requiring the caller to guarantee
1701 * serialization of dlmglue flock calls.
1703 int ocfs2_file_lock(struct file *file, int ex, int trylock)
1705 int ret, level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1706 unsigned int lkm_flags = trylock ? DLM_LKF_NOQUEUE : 0;
1707 unsigned long flags;
1708 struct ocfs2_file_private *fp = file->private_data;
1709 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1710 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1711 struct ocfs2_mask_waiter mw;
1713 ocfs2_init_mask_waiter(&mw);
1715 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
1716 (lockres->l_level > DLM_LOCK_NL)) {
1718 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1719 "level: %u\n", lockres->l_name, lockres->l_flags,
1724 spin_lock_irqsave(&lockres->l_lock, flags);
1725 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1726 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1727 spin_unlock_irqrestore(&lockres->l_lock, flags);
1730 * Get the lock at NLMODE to start - that way we
1731 * can cancel the upconvert request if need be.
1733 ret = ocfs2_lock_create(osb, lockres, DLM_LOCK_NL, 0);
1739 ret = ocfs2_wait_for_mask(&mw);
1744 spin_lock_irqsave(&lockres->l_lock, flags);
1747 lockres->l_action = OCFS2_AST_CONVERT;
1748 lkm_flags |= DLM_LKF_CONVERT;
1749 lockres->l_requested = level;
1750 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1752 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1753 spin_unlock_irqrestore(&lockres->l_lock, flags);
1755 ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
1756 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1,
1759 if (!trylock || (ret != -EAGAIN)) {
1760 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
1764 ocfs2_recover_from_dlm_error(lockres, 1);
1765 lockres_remove_mask_waiter(lockres, &mw);
1769 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1770 if (ret == -ERESTARTSYS) {
1772 * Userspace can cause deadlock itself with
1773 * flock(). Current behavior locally is to allow the
1774 * deadlock, but abort the system call if a signal is
1775 * received. We follow this example, otherwise a
1776 * poorly written program could sit in kernel until
1779 * Handling this is a bit more complicated for Ocfs2
1780 * though. We can't exit this function with an
1781 * outstanding lock request, so a cancel convert is
1782 * required. We intentionally overwrite 'ret' - if the
1783 * cancel fails and the lock was granted, it's easier
1784 * to just bubble sucess back up to the user.
1786 ret = ocfs2_flock_handle_signal(lockres, level);
1787 } else if (!ret && (level > lockres->l_level)) {
1788 /* Trylock failed asynchronously */
1795 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1796 lockres->l_name, ex, trylock, ret);
1800 void ocfs2_file_unlock(struct file *file)
1804 unsigned long flags;
1805 struct ocfs2_file_private *fp = file->private_data;
1806 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1807 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1808 struct ocfs2_mask_waiter mw;
1810 ocfs2_init_mask_waiter(&mw);
1812 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
1815 if (lockres->l_level == DLM_LOCK_NL)
1818 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1819 lockres->l_name, lockres->l_flags, lockres->l_level,
1822 spin_lock_irqsave(&lockres->l_lock, flags);
1824 * Fake a blocking ast for the downconvert code.
1826 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
1827 lockres->l_blocking = DLM_LOCK_EX;
1829 gen = ocfs2_prepare_downconvert(lockres, DLM_LOCK_NL);
1830 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1831 spin_unlock_irqrestore(&lockres->l_lock, flags);
1833 ret = ocfs2_downconvert_lock(osb, lockres, DLM_LOCK_NL, 0, gen);
1839 ret = ocfs2_wait_for_mask(&mw);
1844 static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1845 struct ocfs2_lock_res *lockres)
1851 /* If we know that another node is waiting on our lock, kick
1852 * the downconvert thread * pre-emptively when we reach a release
1854 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1855 switch(lockres->l_blocking) {
1857 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1861 if (!lockres->l_ex_holders)
1870 ocfs2_wake_downconvert_thread(osb);
1875 #define OCFS2_SEC_BITS 34
1876 #define OCFS2_SEC_SHIFT (64 - 34)
1877 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1879 /* LVB only has room for 64 bits of time here so we pack it for
1881 static u64 ocfs2_pack_timespec(struct timespec *spec)
1884 u64 sec = spec->tv_sec;
1885 u32 nsec = spec->tv_nsec;
1887 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1892 /* Call this with the lockres locked. I am reasonably sure we don't
1893 * need ip_lock in this function as anyone who would be changing those
1894 * values is supposed to be blocked in ocfs2_inode_lock right now. */
1895 static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1897 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1898 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1899 struct ocfs2_meta_lvb *lvb;
1903 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
1906 * Invalidate the LVB of a deleted inode - this way other
1907 * nodes are forced to go to disk and discover the new inode
1910 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1911 lvb->lvb_version = 0;
1915 lvb->lvb_version = OCFS2_LVB_VERSION;
1916 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1917 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1918 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1919 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1920 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1921 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1922 lvb->lvb_iatime_packed =
1923 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1924 lvb->lvb_ictime_packed =
1925 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1926 lvb->lvb_imtime_packed =
1927 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1928 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
1929 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
1930 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
1933 mlog_meta_lvb(0, lockres);
1938 static void ocfs2_unpack_timespec(struct timespec *spec,
1941 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1942 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1945 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1947 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1948 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1949 struct ocfs2_meta_lvb *lvb;
1953 mlog_meta_lvb(0, lockres);
1955 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
1957 /* We're safe here without the lockres lock... */
1958 spin_lock(&oi->ip_lock);
1959 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1960 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1962 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1963 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
1964 ocfs2_set_inode_flags(inode);
1966 /* fast-symlinks are a special case */
1967 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1968 inode->i_blocks = 0;
1970 inode->i_blocks = ocfs2_inode_sector_count(inode);
1972 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1973 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1974 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1975 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1976 ocfs2_unpack_timespec(&inode->i_atime,
1977 be64_to_cpu(lvb->lvb_iatime_packed));
1978 ocfs2_unpack_timespec(&inode->i_mtime,
1979 be64_to_cpu(lvb->lvb_imtime_packed));
1980 ocfs2_unpack_timespec(&inode->i_ctime,
1981 be64_to_cpu(lvb->lvb_ictime_packed));
1982 spin_unlock(&oi->ip_lock);
1987 static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1988 struct ocfs2_lock_res *lockres)
1990 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
1992 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb)
1993 && lvb->lvb_version == OCFS2_LVB_VERSION
1994 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
1999 /* Determine whether a lock resource needs to be refreshed, and
2000 * arbitrate who gets to refresh it.
2002 * 0 means no refresh needed.
2004 * > 0 means you need to refresh this and you MUST call
2005 * ocfs2_complete_lock_res_refresh afterwards. */
2006 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
2008 unsigned long flags;
2014 spin_lock_irqsave(&lockres->l_lock, flags);
2015 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2016 spin_unlock_irqrestore(&lockres->l_lock, flags);
2020 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2021 spin_unlock_irqrestore(&lockres->l_lock, flags);
2023 ocfs2_wait_on_refreshing_lock(lockres);
2027 /* Ok, I'll be the one to refresh this lock. */
2028 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
2029 spin_unlock_irqrestore(&lockres->l_lock, flags);
2037 /* If status is non zero, I'll mark it as not being in refresh
2038 * anymroe, but i won't clear the needs refresh flag. */
2039 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
2042 unsigned long flags;
2045 spin_lock_irqsave(&lockres->l_lock, flags);
2046 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
2048 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
2049 spin_unlock_irqrestore(&lockres->l_lock, flags);
2051 wake_up(&lockres->l_event);
2056 /* may or may not return a bh if it went to disk. */
2057 static int ocfs2_inode_lock_update(struct inode *inode,
2058 struct buffer_head **bh)
2061 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2062 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
2063 struct ocfs2_dinode *fe;
2064 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2068 if (ocfs2_mount_local(osb))
2071 spin_lock(&oi->ip_lock);
2072 if (oi->ip_flags & OCFS2_INODE_DELETED) {
2073 mlog(0, "Orphaned inode %llu was deleted while we "
2074 "were waiting on a lock. ip_flags = 0x%x\n",
2075 (unsigned long long)oi->ip_blkno, oi->ip_flags);
2076 spin_unlock(&oi->ip_lock);
2080 spin_unlock(&oi->ip_lock);
2082 if (!ocfs2_should_refresh_lock_res(lockres))
2085 /* This will discard any caching information we might have had
2086 * for the inode metadata. */
2087 ocfs2_metadata_cache_purge(inode);
2089 ocfs2_extent_map_trunc(inode, 0);
2091 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
2092 mlog(0, "Trusting LVB on inode %llu\n",
2093 (unsigned long long)oi->ip_blkno);
2094 ocfs2_refresh_inode_from_lvb(inode);
2096 /* Boo, we have to go to disk. */
2097 /* read bh, cast, ocfs2_refresh_inode */
2098 status = ocfs2_read_inode_block(inode, bh);
2103 fe = (struct ocfs2_dinode *) (*bh)->b_data;
2105 /* This is a good chance to make sure we're not
2106 * locking an invalid object. ocfs2_read_inode_block()
2107 * already checked that the inode block is sane.
2109 * We bug on a stale inode here because we checked
2110 * above whether it was wiped from disk. The wiping
2111 * node provides a guarantee that we receive that
2112 * message and can mark the inode before dropping any
2113 * locks associated with it. */
2114 mlog_bug_on_msg(inode->i_generation !=
2115 le32_to_cpu(fe->i_generation),
2116 "Invalid dinode %llu disk generation: %u "
2117 "inode->i_generation: %u\n",
2118 (unsigned long long)oi->ip_blkno,
2119 le32_to_cpu(fe->i_generation),
2120 inode->i_generation);
2121 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
2122 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
2123 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
2124 (unsigned long long)oi->ip_blkno,
2125 (unsigned long long)le64_to_cpu(fe->i_dtime),
2126 le32_to_cpu(fe->i_flags));
2128 ocfs2_refresh_inode(inode, fe);
2129 ocfs2_track_lock_refresh(lockres);
2134 ocfs2_complete_lock_res_refresh(lockres, status);
2140 static int ocfs2_assign_bh(struct inode *inode,
2141 struct buffer_head **ret_bh,
2142 struct buffer_head *passed_bh)
2147 /* Ok, the update went to disk for us, use the
2149 *ret_bh = passed_bh;
2155 status = ocfs2_read_inode_block(inode, ret_bh);
2163 * returns < 0 error if the callback will never be called, otherwise
2164 * the result of the lock will be communicated via the callback.
2166 int ocfs2_inode_lock_full(struct inode *inode,
2167 struct buffer_head **ret_bh,
2171 int status, level, acquired;
2173 struct ocfs2_lock_res *lockres = NULL;
2174 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2175 struct buffer_head *local_bh = NULL;
2181 mlog(0, "inode %llu, take %s META lock\n",
2182 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2183 ex ? "EXMODE" : "PRMODE");
2187 /* We'll allow faking a readonly metadata lock for
2189 if (ocfs2_is_hard_readonly(osb)) {
2195 if (ocfs2_mount_local(osb))
2198 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
2199 ocfs2_wait_for_recovery(osb);
2201 lockres = &OCFS2_I(inode)->ip_inode_lockres;
2202 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2204 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
2205 dlm_flags |= DLM_LKF_NOQUEUE;
2207 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
2209 if (status != -EAGAIN && status != -EIOCBRETRY)
2214 /* Notify the error cleanup path to drop the cluster lock. */
2217 /* We wait twice because a node may have died while we were in
2218 * the lower dlm layers. The second time though, we've
2219 * committed to owning this lock so we don't allow signals to
2220 * abort the operation. */
2221 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
2222 ocfs2_wait_for_recovery(osb);
2226 * We only see this flag if we're being called from
2227 * ocfs2_read_locked_inode(). It means we're locking an inode
2228 * which hasn't been populated yet, so clear the refresh flag
2229 * and let the caller handle it.
2231 if (inode->i_state & I_NEW) {
2234 ocfs2_complete_lock_res_refresh(lockres, 0);
2238 /* This is fun. The caller may want a bh back, or it may
2239 * not. ocfs2_inode_lock_update definitely wants one in, but
2240 * may or may not read one, depending on what's in the
2241 * LVB. The result of all of this is that we've *only* gone to
2242 * disk if we have to, so the complexity is worthwhile. */
2243 status = ocfs2_inode_lock_update(inode, &local_bh);
2245 if (status != -ENOENT)
2251 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
2260 if (ret_bh && (*ret_bh)) {
2265 ocfs2_inode_unlock(inode, ex);
2276 * This is working around a lock inversion between tasks acquiring DLM
2277 * locks while holding a page lock and the downconvert thread which
2278 * blocks dlm lock acquiry while acquiring page locks.
2280 * ** These _with_page variantes are only intended to be called from aop
2281 * methods that hold page locks and return a very specific *positive* error
2282 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2284 * The DLM is called such that it returns -EAGAIN if it would have
2285 * blocked waiting for the downconvert thread. In that case we unlock
2286 * our page so the downconvert thread can make progress. Once we've
2287 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2288 * that called us can bubble that back up into the VFS who will then
2289 * immediately retry the aop call.
2291 * We do a blocking lock and immediate unlock before returning, though, so that
2292 * the lock has a great chance of being cached on this node by the time the VFS
2293 * calls back to retry the aop. This has a potential to livelock as nodes
2294 * ping locks back and forth, but that's a risk we're willing to take to avoid
2295 * the lock inversion simply.
2297 int ocfs2_inode_lock_with_page(struct inode *inode,
2298 struct buffer_head **ret_bh,
2304 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
2305 if (ret == -EAGAIN) {
2307 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2308 ocfs2_inode_unlock(inode, ex);
2309 ret = AOP_TRUNCATED_PAGE;
2315 int ocfs2_inode_lock_atime(struct inode *inode,
2316 struct vfsmount *vfsmnt,
2322 ret = ocfs2_inode_lock(inode, NULL, 0);
2329 * If we should update atime, we will get EX lock,
2330 * otherwise we just get PR lock.
2332 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2333 struct buffer_head *bh = NULL;
2335 ocfs2_inode_unlock(inode, 0);
2336 ret = ocfs2_inode_lock(inode, &bh, 1);
2342 if (ocfs2_should_update_atime(inode, vfsmnt))
2343 ocfs2_update_inode_atime(inode, bh);
2353 void ocfs2_inode_unlock(struct inode *inode,
2356 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2357 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
2358 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2362 mlog(0, "inode %llu drop %s META lock\n",
2363 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2364 ex ? "EXMODE" : "PRMODE");
2366 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2367 !ocfs2_mount_local(osb))
2368 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
2373 int ocfs2_orphan_scan_lock(struct ocfs2_super *osb, u32 *seqno, int ex)
2375 struct ocfs2_lock_res *lockres;
2376 struct ocfs2_orphan_scan_lvb *lvb;
2377 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2380 lockres = &osb->osb_orphan_scan.os_lockres;
2381 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2385 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2386 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
2387 lvb->lvb_version == OCFS2_ORPHAN_LVB_VERSION)
2388 *seqno = be32_to_cpu(lvb->lvb_os_seqno);
2392 void ocfs2_orphan_scan_unlock(struct ocfs2_super *osb, u32 seqno, int ex)
2394 struct ocfs2_lock_res *lockres;
2395 struct ocfs2_orphan_scan_lvb *lvb;
2396 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2398 lockres = &osb->osb_orphan_scan.os_lockres;
2399 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2400 lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION;
2401 lvb->lvb_os_seqno = cpu_to_be32(seqno);
2402 ocfs2_cluster_unlock(osb, lockres, level);
2405 int ocfs2_super_lock(struct ocfs2_super *osb,
2409 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2410 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2414 if (ocfs2_is_hard_readonly(osb))
2417 if (ocfs2_mount_local(osb))
2420 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2426 /* The super block lock path is really in the best position to
2427 * know when resources covered by the lock need to be
2428 * refreshed, so we do it here. Of course, making sense of
2429 * everything is up to the caller :) */
2430 status = ocfs2_should_refresh_lock_res(lockres);
2436 status = ocfs2_refresh_slot_info(osb);
2438 ocfs2_complete_lock_res_refresh(lockres, status);
2442 ocfs2_track_lock_refresh(lockres);
2449 void ocfs2_super_unlock(struct ocfs2_super *osb,
2452 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2453 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2455 if (!ocfs2_mount_local(osb))
2456 ocfs2_cluster_unlock(osb, lockres, level);
2459 int ocfs2_rename_lock(struct ocfs2_super *osb)
2462 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2464 if (ocfs2_is_hard_readonly(osb))
2467 if (ocfs2_mount_local(osb))
2470 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
2477 void ocfs2_rename_unlock(struct ocfs2_super *osb)
2479 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2481 if (!ocfs2_mount_local(osb))
2482 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2485 int ocfs2_nfs_sync_lock(struct ocfs2_super *osb, int ex)
2488 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2490 if (ocfs2_is_hard_readonly(osb))
2493 if (ocfs2_mount_local(osb))
2496 status = ocfs2_cluster_lock(osb, lockres, ex ? LKM_EXMODE : LKM_PRMODE,
2499 mlog(ML_ERROR, "lock on nfs sync lock failed %d\n", status);
2504 void ocfs2_nfs_sync_unlock(struct ocfs2_super *osb, int ex)
2506 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2508 if (!ocfs2_mount_local(osb))
2509 ocfs2_cluster_unlock(osb, lockres,
2510 ex ? LKM_EXMODE : LKM_PRMODE);
2513 int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2516 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2517 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2518 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2522 if (ocfs2_is_hard_readonly(osb))
2525 if (ocfs2_mount_local(osb))
2528 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2535 void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2537 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2538 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2539 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2541 if (!ocfs2_mount_local(osb))
2542 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
2545 /* Reference counting of the dlm debug structure. We want this because
2546 * open references on the debug inodes can live on after a mount, so
2547 * we can't rely on the ocfs2_super to always exist. */
2548 static void ocfs2_dlm_debug_free(struct kref *kref)
2550 struct ocfs2_dlm_debug *dlm_debug;
2552 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2557 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2560 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2563 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2565 kref_get(&debug->d_refcnt);
2568 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2570 struct ocfs2_dlm_debug *dlm_debug;
2572 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2574 mlog_errno(-ENOMEM);
2578 kref_init(&dlm_debug->d_refcnt);
2579 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2580 dlm_debug->d_locking_state = NULL;
2585 /* Access to this is arbitrated for us via seq_file->sem. */
2586 struct ocfs2_dlm_seq_priv {
2587 struct ocfs2_dlm_debug *p_dlm_debug;
2588 struct ocfs2_lock_res p_iter_res;
2589 struct ocfs2_lock_res p_tmp_res;
2592 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2593 struct ocfs2_dlm_seq_priv *priv)
2595 struct ocfs2_lock_res *iter, *ret = NULL;
2596 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2598 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2600 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2601 /* discover the head of the list */
2602 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2603 mlog(0, "End of list found, %p\n", ret);
2607 /* We track our "dummy" iteration lockres' by a NULL
2609 if (iter->l_ops != NULL) {
2618 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2620 struct ocfs2_dlm_seq_priv *priv = m->private;
2621 struct ocfs2_lock_res *iter;
2623 spin_lock(&ocfs2_dlm_tracking_lock);
2624 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2626 /* Since lockres' have the lifetime of their container
2627 * (which can be inodes, ocfs2_supers, etc) we want to
2628 * copy this out to a temporary lockres while still
2629 * under the spinlock. Obviously after this we can't
2630 * trust any pointers on the copy returned, but that's
2631 * ok as the information we want isn't typically held
2633 priv->p_tmp_res = *iter;
2634 iter = &priv->p_tmp_res;
2636 spin_unlock(&ocfs2_dlm_tracking_lock);
2641 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2645 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2647 struct ocfs2_dlm_seq_priv *priv = m->private;
2648 struct ocfs2_lock_res *iter = v;
2649 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2651 spin_lock(&ocfs2_dlm_tracking_lock);
2652 iter = ocfs2_dlm_next_res(iter, priv);
2653 list_del_init(&dummy->l_debug_list);
2655 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2656 priv->p_tmp_res = *iter;
2657 iter = &priv->p_tmp_res;
2659 spin_unlock(&ocfs2_dlm_tracking_lock);
2664 /* So that debugfs.ocfs2 can determine which format is being used */
2665 #define OCFS2_DLM_DEBUG_STR_VERSION 2
2666 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2670 struct ocfs2_lock_res *lockres = v;
2675 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2677 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2678 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2680 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2682 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2684 seq_printf(m, "%d\t"
2695 lockres->l_unlock_action,
2696 lockres->l_ro_holders,
2697 lockres->l_ex_holders,
2698 lockres->l_requested,
2699 lockres->l_blocking);
2701 /* Dump the raw LVB */
2702 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2703 for(i = 0; i < DLM_LVB_LEN; i++)
2704 seq_printf(m, "0x%x\t", lvb[i]);
2706 #ifdef CONFIG_OCFS2_FS_STATS
2707 # define lock_num_prmode(_l) (_l)->l_lock_num_prmode
2708 # define lock_num_exmode(_l) (_l)->l_lock_num_exmode
2709 # define lock_num_prmode_failed(_l) (_l)->l_lock_num_prmode_failed
2710 # define lock_num_exmode_failed(_l) (_l)->l_lock_num_exmode_failed
2711 # define lock_total_prmode(_l) (_l)->l_lock_total_prmode
2712 # define lock_total_exmode(_l) (_l)->l_lock_total_exmode
2713 # define lock_max_prmode(_l) (_l)->l_lock_max_prmode
2714 # define lock_max_exmode(_l) (_l)->l_lock_max_exmode
2715 # define lock_refresh(_l) (_l)->l_lock_refresh
2717 # define lock_num_prmode(_l) (0ULL)
2718 # define lock_num_exmode(_l) (0ULL)
2719 # define lock_num_prmode_failed(_l) (0)
2720 # define lock_num_exmode_failed(_l) (0)
2721 # define lock_total_prmode(_l) (0ULL)
2722 # define lock_total_exmode(_l) (0ULL)
2723 # define lock_max_prmode(_l) (0)
2724 # define lock_max_exmode(_l) (0)
2725 # define lock_refresh(_l) (0)
2727 /* The following seq_print was added in version 2 of this output */
2728 seq_printf(m, "%llu\t"
2737 lock_num_prmode(lockres),
2738 lock_num_exmode(lockres),
2739 lock_num_prmode_failed(lockres),
2740 lock_num_exmode_failed(lockres),
2741 lock_total_prmode(lockres),
2742 lock_total_exmode(lockres),
2743 lock_max_prmode(lockres),
2744 lock_max_exmode(lockres),
2745 lock_refresh(lockres));
2748 seq_printf(m, "\n");
2752 static const struct seq_operations ocfs2_dlm_seq_ops = {
2753 .start = ocfs2_dlm_seq_start,
2754 .stop = ocfs2_dlm_seq_stop,
2755 .next = ocfs2_dlm_seq_next,
2756 .show = ocfs2_dlm_seq_show,
2759 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2761 struct seq_file *seq = (struct seq_file *) file->private_data;
2762 struct ocfs2_dlm_seq_priv *priv = seq->private;
2763 struct ocfs2_lock_res *res = &priv->p_iter_res;
2765 ocfs2_remove_lockres_tracking(res);
2766 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2767 return seq_release_private(inode, file);
2770 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2773 struct ocfs2_dlm_seq_priv *priv;
2774 struct seq_file *seq;
2775 struct ocfs2_super *osb;
2777 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2783 osb = inode->i_private;
2784 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2785 priv->p_dlm_debug = osb->osb_dlm_debug;
2786 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2788 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2795 seq = (struct seq_file *) file->private_data;
2796 seq->private = priv;
2798 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2805 static const struct file_operations ocfs2_dlm_debug_fops = {
2806 .open = ocfs2_dlm_debug_open,
2807 .release = ocfs2_dlm_debug_release,
2809 .llseek = seq_lseek,
2812 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2815 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2817 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2819 osb->osb_debug_root,
2821 &ocfs2_dlm_debug_fops);
2822 if (!dlm_debug->d_locking_state) {
2825 "Unable to create locking state debugfs file.\n");
2829 ocfs2_get_dlm_debug(dlm_debug);
2834 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2836 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2839 debugfs_remove(dlm_debug->d_locking_state);
2840 ocfs2_put_dlm_debug(dlm_debug);
2844 int ocfs2_dlm_init(struct ocfs2_super *osb)
2847 struct ocfs2_cluster_connection *conn = NULL;
2851 if (ocfs2_mount_local(osb)) {
2856 status = ocfs2_dlm_init_debug(osb);
2862 /* launch downconvert thread */
2863 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2864 if (IS_ERR(osb->dc_task)) {
2865 status = PTR_ERR(osb->dc_task);
2866 osb->dc_task = NULL;
2871 /* for now, uuid == domain */
2872 status = ocfs2_cluster_connect(osb->osb_cluster_stack,
2874 strlen(osb->uuid_str),
2875 ocfs2_do_node_down, osb,
2882 status = ocfs2_cluster_this_node(&osb->node_num);
2886 "could not find this host's node number\n");
2887 ocfs2_cluster_disconnect(conn, 0);
2892 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2893 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2894 ocfs2_nfs_sync_lock_res_init(&osb->osb_nfs_sync_lockres, osb);
2895 ocfs2_orphan_scan_lock_res_init(&osb->osb_orphan_scan.os_lockres, osb);
2902 ocfs2_dlm_shutdown_debug(osb);
2904 kthread_stop(osb->dc_task);
2911 void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
2916 ocfs2_drop_osb_locks(osb);
2919 * Now that we have dropped all locks and ocfs2_dismount_volume()
2920 * has disabled recovery, the DLM won't be talking to us. It's
2921 * safe to tear things down before disconnecting the cluster.
2925 kthread_stop(osb->dc_task);
2926 osb->dc_task = NULL;
2929 ocfs2_lock_res_free(&osb->osb_super_lockres);
2930 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2931 ocfs2_lock_res_free(&osb->osb_nfs_sync_lockres);
2932 ocfs2_lock_res_free(&osb->osb_orphan_scan.os_lockres);
2934 ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
2937 ocfs2_dlm_shutdown_debug(osb);
2942 static void ocfs2_unlock_ast(void *opaque, int error)
2944 struct ocfs2_lock_res *lockres = opaque;
2945 unsigned long flags;
2949 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2950 lockres->l_unlock_action);
2952 spin_lock_irqsave(&lockres->l_lock, flags);
2954 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
2955 "unlock_action %d\n", error, lockres->l_name,
2956 lockres->l_unlock_action);
2957 spin_unlock_irqrestore(&lockres->l_lock, flags);
2961 switch(lockres->l_unlock_action) {
2962 case OCFS2_UNLOCK_CANCEL_CONVERT:
2963 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2964 lockres->l_action = OCFS2_AST_INVALID;
2965 /* Downconvert thread may have requeued this lock, we
2966 * need to wake it. */
2967 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2968 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres));
2970 case OCFS2_UNLOCK_DROP_LOCK:
2971 lockres->l_level = DLM_LOCK_IV;
2977 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2978 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2979 wake_up(&lockres->l_event);
2980 spin_unlock_irqrestore(&lockres->l_lock, flags);
2985 static int ocfs2_drop_lock(struct ocfs2_super *osb,
2986 struct ocfs2_lock_res *lockres)
2989 unsigned long flags;
2992 /* We didn't get anywhere near actually using this lockres. */
2993 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2996 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
2997 lkm_flags |= DLM_LKF_VALBLK;
2999 spin_lock_irqsave(&lockres->l_lock, flags);
3001 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
3002 "lockres %s, flags 0x%lx\n",
3003 lockres->l_name, lockres->l_flags);
3005 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
3006 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
3007 "%u, unlock_action = %u\n",
3008 lockres->l_name, lockres->l_flags, lockres->l_action,
3009 lockres->l_unlock_action);
3011 spin_unlock_irqrestore(&lockres->l_lock, flags);
3013 /* XXX: Today we just wait on any busy
3014 * locks... Perhaps we need to cancel converts in the
3016 ocfs2_wait_on_busy_lock(lockres);
3018 spin_lock_irqsave(&lockres->l_lock, flags);
3021 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3022 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
3023 lockres->l_level == DLM_LOCK_EX &&
3024 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3025 lockres->l_ops->set_lvb(lockres);
3028 if (lockres->l_flags & OCFS2_LOCK_BUSY)
3029 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
3031 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
3032 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
3034 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
3035 spin_unlock_irqrestore(&lockres->l_lock, flags);
3039 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
3041 /* make sure we never get here while waiting for an ast to
3043 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
3045 /* is this necessary? */
3046 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3047 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
3048 spin_unlock_irqrestore(&lockres->l_lock, flags);
3050 mlog(0, "lock %s\n", lockres->l_name);
3052 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags,
3055 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
3056 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
3057 ocfs2_dlm_dump_lksb(&lockres->l_lksb);
3060 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
3063 ocfs2_wait_on_busy_lock(lockres);
3069 /* Mark the lockres as being dropped. It will no longer be
3070 * queued if blocking, but we still may have to wait on it
3071 * being dequeued from the downconvert thread before we can consider
3074 * You can *not* attempt to call cluster_lock on this lockres anymore. */
3075 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
3078 struct ocfs2_mask_waiter mw;
3079 unsigned long flags;
3081 ocfs2_init_mask_waiter(&mw);
3083 spin_lock_irqsave(&lockres->l_lock, flags);
3084 lockres->l_flags |= OCFS2_LOCK_FREEING;
3085 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
3086 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
3087 spin_unlock_irqrestore(&lockres->l_lock, flags);
3089 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
3091 status = ocfs2_wait_for_mask(&mw);
3095 spin_lock_irqsave(&lockres->l_lock, flags);
3097 spin_unlock_irqrestore(&lockres->l_lock, flags);
3100 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
3101 struct ocfs2_lock_res *lockres)
3105 ocfs2_mark_lockres_freeing(lockres);
3106 ret = ocfs2_drop_lock(osb, lockres);
3111 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
3113 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
3114 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
3115 ocfs2_simple_drop_lockres(osb, &osb->osb_nfs_sync_lockres);
3116 ocfs2_simple_drop_lockres(osb, &osb->osb_orphan_scan.os_lockres);
3119 int ocfs2_drop_inode_locks(struct inode *inode)
3125 /* No need to call ocfs2_mark_lockres_freeing here -
3126 * ocfs2_clear_inode has done it for us. */
3128 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
3129 &OCFS2_I(inode)->ip_open_lockres);
3135 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
3136 &OCFS2_I(inode)->ip_inode_lockres);
3139 if (err < 0 && !status)
3142 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
3143 &OCFS2_I(inode)->ip_rw_lockres);
3146 if (err < 0 && !status)
3153 static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
3156 assert_spin_locked(&lockres->l_lock);
3158 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
3160 if (lockres->l_level <= new_level) {
3161 mlog(ML_ERROR, "lockres->l_level (%d) <= new_level (%d)\n",
3162 lockres->l_level, new_level);
3166 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
3167 lockres->l_name, new_level, lockres->l_blocking);
3169 lockres->l_action = OCFS2_AST_DOWNCONVERT;
3170 lockres->l_requested = new_level;
3171 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3172 return lockres_set_pending(lockres);
3175 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
3176 struct ocfs2_lock_res *lockres,
3179 unsigned int generation)
3182 u32 dlm_flags = DLM_LKF_CONVERT;
3187 dlm_flags |= DLM_LKF_VALBLK;
3189 ret = ocfs2_dlm_lock(osb->cconn,
3194 OCFS2_LOCK_ID_MAX_LEN - 1,
3196 lockres_clear_pending(lockres, generation, osb);
3198 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
3199 ocfs2_recover_from_dlm_error(lockres, 1);
3209 /* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
3210 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
3211 struct ocfs2_lock_res *lockres)
3213 assert_spin_locked(&lockres->l_lock);
3216 mlog(0, "lock %s\n", lockres->l_name);
3218 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
3219 /* If we're already trying to cancel a lock conversion
3220 * then just drop the spinlock and allow the caller to
3221 * requeue this lock. */
3223 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
3227 /* were we in a convert when we got the bast fire? */
3228 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
3229 lockres->l_action != OCFS2_AST_DOWNCONVERT);
3230 /* set things up for the unlockast to know to just
3231 * clear out the ast_action and unset busy, etc. */
3232 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
3234 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
3235 "lock %s, invalid flags: 0x%lx\n",
3236 lockres->l_name, lockres->l_flags);
3241 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
3242 struct ocfs2_lock_res *lockres)
3247 mlog(0, "lock %s\n", lockres->l_name);
3249 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
3250 DLM_LKF_CANCEL, lockres);
3252 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
3253 ocfs2_recover_from_dlm_error(lockres, 0);
3256 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres->l_name);
3262 static int ocfs2_unblock_lock(struct ocfs2_super *osb,
3263 struct ocfs2_lock_res *lockres,
3264 struct ocfs2_unblock_ctl *ctl)
3266 unsigned long flags;
3275 spin_lock_irqsave(&lockres->l_lock, flags);
3277 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
3280 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
3282 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3283 * exists entirely for one reason - another thread has set
3284 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3286 * If we do ocfs2_cancel_convert() before the other thread
3287 * calls dlm_lock(), our cancel will do nothing. We will
3288 * get no ast, and we will have no way of knowing the
3289 * cancel failed. Meanwhile, the other thread will call
3290 * into dlm_lock() and wait...forever.
3292 * Why forever? Because another node has asked for the
3293 * lock first; that's why we're here in unblock_lock().
3295 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3296 * set, we just requeue the unblock. Only when the other
3297 * thread has called dlm_lock() and cleared PENDING will
3298 * we then cancel their request.
3300 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3301 * at the same time they set OCFS2_DLM_BUSY. They must
3302 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3304 if (lockres->l_flags & OCFS2_LOCK_PENDING)
3308 ret = ocfs2_prepare_cancel_convert(osb, lockres);
3309 spin_unlock_irqrestore(&lockres->l_lock, flags);
3311 ret = ocfs2_cancel_convert(osb, lockres);
3318 /* if we're blocking an exclusive and we have *any* holders,
3320 if ((lockres->l_blocking == DLM_LOCK_EX)
3321 && (lockres->l_ex_holders || lockres->l_ro_holders))
3324 /* If it's a PR we're blocking, then only
3325 * requeue if we've got any EX holders */
3326 if (lockres->l_blocking == DLM_LOCK_PR &&
3327 lockres->l_ex_holders)
3331 * Can we get a lock in this state if the holder counts are
3332 * zero? The meta data unblock code used to check this.
3334 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
3335 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
3338 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
3340 if (lockres->l_ops->check_downconvert
3341 && !lockres->l_ops->check_downconvert(lockres, new_level))
3344 /* If we get here, then we know that there are no more
3345 * incompatible holders (and anyone asking for an incompatible
3346 * lock is blocked). We can now downconvert the lock */
3347 if (!lockres->l_ops->downconvert_worker)
3350 /* Some lockres types want to do a bit of work before
3351 * downconverting a lock. Allow that here. The worker function
3352 * may sleep, so we save off a copy of what we're blocking as
3353 * it may change while we're not holding the spin lock. */
3354 blocking = lockres->l_blocking;
3355 spin_unlock_irqrestore(&lockres->l_lock, flags);
3357 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
3359 if (ctl->unblock_action == UNBLOCK_STOP_POST)
3362 spin_lock_irqsave(&lockres->l_lock, flags);
3363 if (blocking != lockres->l_blocking) {
3364 /* If this changed underneath us, then we can't drop
3372 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3373 if (lockres->l_level == DLM_LOCK_EX)
3377 * We only set the lvb if the lock has been fully
3378 * refreshed - otherwise we risk setting stale
3379 * data. Otherwise, there's no need to actually clear
3380 * out the lvb here as it's value is still valid.
3382 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3383 lockres->l_ops->set_lvb(lockres);
3386 gen = ocfs2_prepare_downconvert(lockres, new_level);
3387 spin_unlock_irqrestore(&lockres->l_lock, flags);
3388 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
3396 spin_unlock_irqrestore(&lockres->l_lock, flags);
3403 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3406 struct inode *inode;
3407 struct address_space *mapping;
3409 inode = ocfs2_lock_res_inode(lockres);
3410 mapping = inode->i_mapping;
3412 if (!S_ISREG(inode->i_mode))
3416 * We need this before the filemap_fdatawrite() so that it can
3417 * transfer the dirty bit from the PTE to the
3418 * page. Unfortunately this means that even for EX->PR
3419 * downconverts, we'll lose our mappings and have to build
3422 unmap_mapping_range(mapping, 0, 0, 0);
3424 if (filemap_fdatawrite(mapping)) {
3425 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3426 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3428 sync_mapping_buffers(mapping);
3429 if (blocking == DLM_LOCK_EX) {
3430 truncate_inode_pages(mapping, 0);
3432 /* We only need to wait on the I/O if we're not also
3433 * truncating pages because truncate_inode_pages waits
3434 * for us above. We don't truncate pages if we're
3435 * blocking anything < EXMODE because we want to keep
3436 * them around in that case. */
3437 filemap_fdatawait(mapping);
3441 return UNBLOCK_CONTINUE;
3444 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3447 struct inode *inode = ocfs2_lock_res_inode(lockres);
3448 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
3450 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3451 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
3456 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
3460 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3462 struct inode *inode = ocfs2_lock_res_inode(lockres);
3464 __ocfs2_stuff_meta_lvb(inode);
3468 * Does the final reference drop on our dentry lock. Right now this
3469 * happens in the downconvert thread, but we could choose to simplify the
3470 * dlmglue API and push these off to the ocfs2_wq in the future.
3472 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3473 struct ocfs2_lock_res *lockres)
3475 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3476 ocfs2_dentry_lock_put(osb, dl);
3480 * d_delete() matching dentries before the lock downconvert.
3482 * At this point, any process waiting to destroy the
3483 * dentry_lock due to last ref count is stopped by the
3484 * OCFS2_LOCK_QUEUED flag.
3486 * We have two potential problems
3488 * 1) If we do the last reference drop on our dentry_lock (via dput)
3489 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3490 * the downconvert to finish. Instead we take an elevated
3491 * reference and push the drop until after we've completed our
3492 * unblock processing.
3494 * 2) There might be another process with a final reference,
3495 * waiting on us to finish processing. If this is the case, we
3496 * detect it and exit out - there's no more dentries anyway.
3498 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3501 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3502 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3503 struct dentry *dentry;
3504 unsigned long flags;
3508 * This node is blocking another node from getting a read
3509 * lock. This happens when we've renamed within a
3510 * directory. We've forced the other nodes to d_delete(), but
3511 * we never actually dropped our lock because it's still
3512 * valid. The downconvert code will retain a PR for this node,
3513 * so there's no further work to do.
3515 if (blocking == DLM_LOCK_PR)
3516 return UNBLOCK_CONTINUE;
3519 * Mark this inode as potentially orphaned. The code in
3520 * ocfs2_delete_inode() will figure out whether it actually
3521 * needs to be freed or not.
3523 spin_lock(&oi->ip_lock);
3524 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3525 spin_unlock(&oi->ip_lock);
3528 * Yuck. We need to make sure however that the check of
3529 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3530 * respect to a reference decrement or the setting of that
3533 spin_lock_irqsave(&lockres->l_lock, flags);
3534 spin_lock(&dentry_attach_lock);
3535 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3540 spin_unlock(&dentry_attach_lock);
3541 spin_unlock_irqrestore(&lockres->l_lock, flags);
3543 mlog(0, "extra_ref = %d\n", extra_ref);
3546 * We have a process waiting on us in ocfs2_dentry_iput(),
3547 * which means we can't have any more outstanding
3548 * aliases. There's no need to do any more work.
3551 return UNBLOCK_CONTINUE;
3553 spin_lock(&dentry_attach_lock);
3555 dentry = ocfs2_find_local_alias(dl->dl_inode,
3556 dl->dl_parent_blkno, 1);
3559 spin_unlock(&dentry_attach_lock);
3561 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3562 dentry->d_name.name);
3565 * The following dcache calls may do an
3566 * iput(). Normally we don't want that from the
3567 * downconverting thread, but in this case it's ok
3568 * because the requesting node already has an
3569 * exclusive lock on the inode, so it can't be queued
3570 * for a downconvert.
3575 spin_lock(&dentry_attach_lock);
3577 spin_unlock(&dentry_attach_lock);
3580 * If we are the last holder of this dentry lock, there is no
3581 * reason to downconvert so skip straight to the unlock.
3583 if (dl->dl_count == 1)
3584 return UNBLOCK_STOP_POST;
3586 return UNBLOCK_CONTINUE_POST;
3589 static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres)
3591 struct ocfs2_qinfo_lvb *lvb;
3592 struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres);
3593 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3594 oinfo->dqi_gi.dqi_type);
3598 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
3599 lvb->lvb_version = OCFS2_QINFO_LVB_VERSION;
3600 lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace);
3601 lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace);
3602 lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms);
3603 lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks);
3604 lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk);
3605 lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry);
3610 void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3612 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3613 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3614 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3617 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
3618 ocfs2_cluster_unlock(osb, lockres, level);
3622 static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo)
3624 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3625 oinfo->dqi_gi.dqi_type);
3626 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3627 struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
3628 struct buffer_head *bh = NULL;
3629 struct ocfs2_global_disk_dqinfo *gdinfo;
3632 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
3633 lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) {
3634 info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace);
3635 info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace);
3636 oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms);
3637 oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks);
3638 oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk);
3639 oinfo->dqi_gi.dqi_free_entry =
3640 be32_to_cpu(lvb->lvb_free_entry);
3642 status = ocfs2_read_quota_block(oinfo->dqi_gqinode, 0, &bh);
3647 gdinfo = (struct ocfs2_global_disk_dqinfo *)
3648 (bh->b_data + OCFS2_GLOBAL_INFO_OFF);
3649 info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace);
3650 info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace);
3651 oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms);
3652 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks);
3653 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk);
3654 oinfo->dqi_gi.dqi_free_entry =
3655 le32_to_cpu(gdinfo->dqi_free_entry);
3657 ocfs2_track_lock_refresh(lockres);
3664 /* Lock quota info, this function expects at least shared lock on the quota file
3665 * so that we can safely refresh quota info from disk. */
3666 int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3668 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3669 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3670 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3675 /* On RO devices, locking really isn't needed... */
3676 if (ocfs2_is_hard_readonly(osb)) {
3681 if (ocfs2_mount_local(osb))
3684 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
3689 if (!ocfs2_should_refresh_lock_res(lockres))
3691 /* OK, we have the lock but we need to refresh the quota info */
3692 status = ocfs2_refresh_qinfo(oinfo);
3694 ocfs2_qinfo_unlock(oinfo, ex);
3695 ocfs2_complete_lock_res_refresh(lockres, status);
3702 * This is the filesystem locking protocol. It provides the lock handling
3703 * hooks for the underlying DLM. It has a maximum version number.
3704 * The version number allows interoperability with systems running at
3705 * the same major number and an equal or smaller minor number.
3707 * Whenever the filesystem does new things with locks (adds or removes a
3708 * lock, orders them differently, does different things underneath a lock),
3709 * the version must be changed. The protocol is negotiated when joining
3710 * the dlm domain. A node may join the domain if its major version is
3711 * identical to all other nodes and its minor version is greater than
3712 * or equal to all other nodes. When its minor version is greater than
3713 * the other nodes, it will run at the minor version specified by the
3716 * If a locking change is made that will not be compatible with older
3717 * versions, the major number must be increased and the minor version set
3718 * to zero. If a change merely adds a behavior that can be disabled when
3719 * speaking to older versions, the minor version must be increased. If a
3720 * change adds a fully backwards compatible change (eg, LVB changes that
3721 * are just ignored by older versions), the version does not need to be
3724 static struct ocfs2_locking_protocol lproto = {
3726 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
3727 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
3729 .lp_lock_ast = ocfs2_locking_ast,
3730 .lp_blocking_ast = ocfs2_blocking_ast,
3731 .lp_unlock_ast = ocfs2_unlock_ast,
3734 void ocfs2_set_locking_protocol(void)
3736 ocfs2_stack_glue_set_locking_protocol(&lproto);
3740 static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3741 struct ocfs2_lock_res *lockres)
3744 struct ocfs2_unblock_ctl ctl = {0, 0,};
3745 unsigned long flags;
3747 /* Our reference to the lockres in this function can be
3748 * considered valid until we remove the OCFS2_LOCK_QUEUED
3754 BUG_ON(!lockres->l_ops);
3756 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3758 /* Detect whether a lock has been marked as going away while
3759 * the downconvert thread was processing other things. A lock can
3760 * still be marked with OCFS2_LOCK_FREEING after this check,
3761 * but short circuiting here will still save us some
3763 spin_lock_irqsave(&lockres->l_lock, flags);
3764 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3766 spin_unlock_irqrestore(&lockres->l_lock, flags);
3768 status = ocfs2_unblock_lock(osb, lockres, &ctl);
3772 spin_lock_irqsave(&lockres->l_lock, flags);
3774 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
3775 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3777 ocfs2_schedule_blocked_lock(osb, lockres);
3779 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
3780 ctl.requeue ? "yes" : "no");
3781 spin_unlock_irqrestore(&lockres->l_lock, flags);
3783 if (ctl.unblock_action != UNBLOCK_CONTINUE
3784 && lockres->l_ops->post_unlock)
3785 lockres->l_ops->post_unlock(osb, lockres);
3790 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3791 struct ocfs2_lock_res *lockres)
3795 assert_spin_locked(&lockres->l_lock);
3797 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3798 /* Do not schedule a lock for downconvert when it's on
3799 * the way to destruction - any nodes wanting access
3800 * to the resource will get it soon. */
3801 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3802 lockres->l_name, lockres->l_flags);
3806 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3808 spin_lock(&osb->dc_task_lock);
3809 if (list_empty(&lockres->l_blocked_list)) {
3810 list_add_tail(&lockres->l_blocked_list,
3811 &osb->blocked_lock_list);
3812 osb->blocked_lock_count++;
3814 spin_unlock(&osb->dc_task_lock);
3819 static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3821 unsigned long processed;
3822 struct ocfs2_lock_res *lockres;
3826 spin_lock(&osb->dc_task_lock);
3827 /* grab this early so we know to try again if a state change and
3828 * wake happens part-way through our work */
3829 osb->dc_work_sequence = osb->dc_wake_sequence;
3831 processed = osb->blocked_lock_count;
3833 BUG_ON(list_empty(&osb->blocked_lock_list));
3835 lockres = list_entry(osb->blocked_lock_list.next,
3836 struct ocfs2_lock_res, l_blocked_list);
3837 list_del_init(&lockres->l_blocked_list);
3838 osb->blocked_lock_count--;
3839 spin_unlock(&osb->dc_task_lock);
3844 ocfs2_process_blocked_lock(osb, lockres);
3846 spin_lock(&osb->dc_task_lock);
3848 spin_unlock(&osb->dc_task_lock);
3853 static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3857 spin_lock(&osb->dc_task_lock);
3858 if (list_empty(&osb->blocked_lock_list))
3861 spin_unlock(&osb->dc_task_lock);
3865 static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
3867 int should_wake = 0;
3869 spin_lock(&osb->dc_task_lock);
3870 if (osb->dc_work_sequence != osb->dc_wake_sequence)
3872 spin_unlock(&osb->dc_task_lock);
3877 static int ocfs2_downconvert_thread(void *arg)
3880 struct ocfs2_super *osb = arg;
3882 /* only quit once we've been asked to stop and there is no more
3884 while (!(kthread_should_stop() &&
3885 ocfs2_downconvert_thread_lists_empty(osb))) {
3887 wait_event_interruptible(osb->dc_event,
3888 ocfs2_downconvert_thread_should_wake(osb) ||
3889 kthread_should_stop());
3891 mlog(0, "downconvert_thread: awoken\n");
3893 ocfs2_downconvert_thread_do_work(osb);
3896 osb->dc_task = NULL;
3900 void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
3902 spin_lock(&osb->dc_task_lock);
3903 /* make sure the voting thread gets a swipe at whatever changes
3904 * the caller may have made to the voting state */
3905 osb->dc_wake_sequence++;
3906 spin_unlock(&osb->dc_task_lock);
3907 wake_up(&osb->dc_event);