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 ocfs2_lock_res_init_once(res);
648 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_ORPHAN_SCAN, 0, 0, res->l_name);
649 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_ORPHAN_SCAN,
650 &ocfs2_orphan_scan_lops, osb);
653 void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
654 struct ocfs2_file_private *fp)
656 struct inode *inode = fp->fp_file->f_mapping->host;
657 struct ocfs2_inode_info *oi = OCFS2_I(inode);
659 ocfs2_lock_res_init_once(lockres);
660 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
661 inode->i_generation, lockres->l_name);
662 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
663 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
665 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
668 void ocfs2_qinfo_lock_res_init(struct ocfs2_lock_res *lockres,
669 struct ocfs2_mem_dqinfo *info)
671 ocfs2_lock_res_init_once(lockres);
672 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_QINFO, info->dqi_gi.dqi_type,
674 ocfs2_lock_res_init_common(OCFS2_SB(info->dqi_gi.dqi_sb), lockres,
675 OCFS2_LOCK_TYPE_QINFO, &ocfs2_qinfo_lops,
679 void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
683 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
686 ocfs2_remove_lockres_tracking(res);
688 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
689 "Lockres %s is on the blocked list\n",
691 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
692 "Lockres %s has mask waiters pending\n",
694 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
695 "Lockres %s is locked\n",
697 mlog_bug_on_msg(res->l_ro_holders,
698 "Lockres %s has %u ro holders\n",
699 res->l_name, res->l_ro_holders);
700 mlog_bug_on_msg(res->l_ex_holders,
701 "Lockres %s has %u ex holders\n",
702 res->l_name, res->l_ex_holders);
704 /* Need to clear out the lock status block for the dlm */
705 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
711 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
720 lockres->l_ex_holders++;
723 lockres->l_ro_holders++;
732 static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
741 BUG_ON(!lockres->l_ex_holders);
742 lockres->l_ex_holders--;
745 BUG_ON(!lockres->l_ro_holders);
746 lockres->l_ro_holders--;
754 /* WARNING: This function lives in a world where the only three lock
755 * levels are EX, PR, and NL. It *will* have to be adjusted when more
756 * lock types are added. */
757 static inline int ocfs2_highest_compat_lock_level(int level)
759 int new_level = DLM_LOCK_EX;
761 if (level == DLM_LOCK_EX)
762 new_level = DLM_LOCK_NL;
763 else if (level == DLM_LOCK_PR)
764 new_level = DLM_LOCK_PR;
768 static void lockres_set_flags(struct ocfs2_lock_res *lockres,
769 unsigned long newflags)
771 struct ocfs2_mask_waiter *mw, *tmp;
773 assert_spin_locked(&lockres->l_lock);
775 lockres->l_flags = newflags;
777 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
778 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
781 list_del_init(&mw->mw_item);
783 complete(&mw->mw_complete);
786 static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
788 lockres_set_flags(lockres, lockres->l_flags | or);
790 static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
793 lockres_set_flags(lockres, lockres->l_flags & ~clear);
796 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
800 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
801 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
802 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
803 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
805 lockres->l_level = lockres->l_requested;
806 if (lockres->l_level <=
807 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
808 lockres->l_blocking = DLM_LOCK_NL;
809 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
811 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
816 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
820 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
821 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
823 /* Convert from RO to EX doesn't really need anything as our
824 * information is already up to data. Convert from NL to
825 * *anything* however should mark ourselves as needing an
827 if (lockres->l_level == DLM_LOCK_NL &&
828 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
829 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
831 lockres->l_level = lockres->l_requested;
832 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
837 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
841 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
842 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
844 if (lockres->l_requested > DLM_LOCK_NL &&
845 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
846 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
847 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
849 lockres->l_level = lockres->l_requested;
850 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
851 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
856 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
859 int needs_downconvert = 0;
862 assert_spin_locked(&lockres->l_lock);
864 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
866 if (level > lockres->l_blocking) {
867 /* only schedule a downconvert if we haven't already scheduled
868 * one that goes low enough to satisfy the level we're
869 * blocking. this also catches the case where we get
871 if (ocfs2_highest_compat_lock_level(level) <
872 ocfs2_highest_compat_lock_level(lockres->l_blocking))
873 needs_downconvert = 1;
875 lockres->l_blocking = level;
878 mlog_exit(needs_downconvert);
879 return needs_downconvert;
883 * OCFS2_LOCK_PENDING and l_pending_gen.
885 * Why does OCFS2_LOCK_PENDING exist? To close a race between setting
886 * OCFS2_LOCK_BUSY and calling ocfs2_dlm_lock(). See ocfs2_unblock_lock()
887 * for more details on the race.
889 * OCFS2_LOCK_PENDING closes the race quite nicely. However, it introduces
890 * a race on itself. In o2dlm, we can get the ast before ocfs2_dlm_lock()
891 * returns. The ast clears OCFS2_LOCK_BUSY, and must therefore clear
892 * OCFS2_LOCK_PENDING at the same time. When ocfs2_dlm_lock() returns,
893 * the caller is going to try to clear PENDING again. If nothing else is
894 * happening, __lockres_clear_pending() sees PENDING is unset and does
897 * But what if another path (eg downconvert thread) has just started a
898 * new locking action? The other path has re-set PENDING. Our path
899 * cannot clear PENDING, because that will re-open the original race
905 * ocfs2_cluster_lock()
910 * ocfs2_locking_ast() ocfs2_downconvert_thread()
911 * clear PENDING ocfs2_unblock_lock()
914 * ocfs2_prepare_downconvert()
924 * So as you can see, we now have a window where l_lock is not held,
925 * PENDING is not set, and ocfs2_dlm_lock() has not been called.
927 * The core problem is that ocfs2_cluster_lock() has cleared the PENDING
928 * set by ocfs2_prepare_downconvert(). That wasn't nice.
930 * To solve this we introduce l_pending_gen. A call to
931 * lockres_clear_pending() will only do so when it is passed a generation
932 * number that matches the lockres. lockres_set_pending() will return the
933 * current generation number. When ocfs2_cluster_lock() goes to clear
934 * PENDING, it passes the generation it got from set_pending(). In our
935 * example above, the generation numbers will *not* match. Thus,
936 * ocfs2_cluster_lock() will not clear the PENDING set by
937 * ocfs2_prepare_downconvert().
940 /* Unlocked version for ocfs2_locking_ast() */
941 static void __lockres_clear_pending(struct ocfs2_lock_res *lockres,
942 unsigned int generation,
943 struct ocfs2_super *osb)
945 assert_spin_locked(&lockres->l_lock);
948 * The ast and locking functions can race us here. The winner
949 * will clear pending, the loser will not.
951 if (!(lockres->l_flags & OCFS2_LOCK_PENDING) ||
952 (lockres->l_pending_gen != generation))
955 lockres_clear_flags(lockres, OCFS2_LOCK_PENDING);
956 lockres->l_pending_gen++;
959 * The downconvert thread may have skipped us because we
960 * were PENDING. Wake it up.
962 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
963 ocfs2_wake_downconvert_thread(osb);
966 /* Locked version for callers of ocfs2_dlm_lock() */
967 static void lockres_clear_pending(struct ocfs2_lock_res *lockres,
968 unsigned int generation,
969 struct ocfs2_super *osb)
973 spin_lock_irqsave(&lockres->l_lock, flags);
974 __lockres_clear_pending(lockres, generation, osb);
975 spin_unlock_irqrestore(&lockres->l_lock, flags);
978 static unsigned int lockres_set_pending(struct ocfs2_lock_res *lockres)
980 assert_spin_locked(&lockres->l_lock);
981 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
983 lockres_or_flags(lockres, OCFS2_LOCK_PENDING);
985 return lockres->l_pending_gen;
989 static void ocfs2_blocking_ast(void *opaque, int level)
991 struct ocfs2_lock_res *lockres = opaque;
992 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
993 int needs_downconvert;
996 BUG_ON(level <= DLM_LOCK_NL);
998 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
999 lockres->l_name, level, lockres->l_level,
1000 ocfs2_lock_type_string(lockres->l_type));
1003 * We can skip the bast for locks which don't enable caching -
1004 * they'll be dropped at the earliest possible time anyway.
1006 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
1009 spin_lock_irqsave(&lockres->l_lock, flags);
1010 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
1011 if (needs_downconvert)
1012 ocfs2_schedule_blocked_lock(osb, lockres);
1013 spin_unlock_irqrestore(&lockres->l_lock, flags);
1015 wake_up(&lockres->l_event);
1017 ocfs2_wake_downconvert_thread(osb);
1020 static void ocfs2_locking_ast(void *opaque)
1022 struct ocfs2_lock_res *lockres = opaque;
1023 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1024 unsigned long flags;
1027 spin_lock_irqsave(&lockres->l_lock, flags);
1029 status = ocfs2_dlm_lock_status(&lockres->l_lksb);
1031 if (status == -EAGAIN) {
1032 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1037 mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
1038 lockres->l_name, status);
1039 spin_unlock_irqrestore(&lockres->l_lock, flags);
1043 switch(lockres->l_action) {
1044 case OCFS2_AST_ATTACH:
1045 ocfs2_generic_handle_attach_action(lockres);
1046 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
1048 case OCFS2_AST_CONVERT:
1049 ocfs2_generic_handle_convert_action(lockres);
1051 case OCFS2_AST_DOWNCONVERT:
1052 ocfs2_generic_handle_downconvert_action(lockres);
1055 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
1056 "lockres flags = 0x%lx, unlock action: %u\n",
1057 lockres->l_name, lockres->l_action, lockres->l_flags,
1058 lockres->l_unlock_action);
1062 /* set it to something invalid so if we get called again we
1064 lockres->l_action = OCFS2_AST_INVALID;
1066 /* Did we try to cancel this lock? Clear that state */
1067 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT)
1068 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1071 * We may have beaten the locking functions here. We certainly
1072 * know that dlm_lock() has been called :-)
1073 * Because we can't have two lock calls in flight at once, we
1074 * can use lockres->l_pending_gen.
1076 __lockres_clear_pending(lockres, lockres->l_pending_gen, osb);
1078 wake_up(&lockres->l_event);
1079 spin_unlock_irqrestore(&lockres->l_lock, flags);
1082 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
1085 unsigned long flags;
1088 spin_lock_irqsave(&lockres->l_lock, flags);
1089 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1091 lockres->l_action = OCFS2_AST_INVALID;
1093 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1094 spin_unlock_irqrestore(&lockres->l_lock, flags);
1096 wake_up(&lockres->l_event);
1100 /* Note: If we detect another process working on the lock (i.e.,
1101 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
1102 * to do the right thing in that case.
1104 static int ocfs2_lock_create(struct ocfs2_super *osb,
1105 struct ocfs2_lock_res *lockres,
1110 unsigned long flags;
1115 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
1118 spin_lock_irqsave(&lockres->l_lock, flags);
1119 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
1120 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
1121 spin_unlock_irqrestore(&lockres->l_lock, flags);
1125 lockres->l_action = OCFS2_AST_ATTACH;
1126 lockres->l_requested = level;
1127 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1128 gen = lockres_set_pending(lockres);
1129 spin_unlock_irqrestore(&lockres->l_lock, flags);
1131 ret = ocfs2_dlm_lock(osb->cconn,
1136 OCFS2_LOCK_ID_MAX_LEN - 1,
1138 lockres_clear_pending(lockres, gen, osb);
1140 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
1141 ocfs2_recover_from_dlm_error(lockres, 1);
1144 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
1151 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
1154 unsigned long flags;
1157 spin_lock_irqsave(&lockres->l_lock, flags);
1158 ret = lockres->l_flags & flag;
1159 spin_unlock_irqrestore(&lockres->l_lock, flags);
1164 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
1167 wait_event(lockres->l_event,
1168 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
1171 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
1174 wait_event(lockres->l_event,
1175 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
1178 /* predict what lock level we'll be dropping down to on behalf
1179 * of another node, and return true if the currently wanted
1180 * level will be compatible with it. */
1181 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
1184 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
1186 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
1189 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
1191 INIT_LIST_HEAD(&mw->mw_item);
1192 init_completion(&mw->mw_complete);
1193 ocfs2_init_start_time(mw);
1196 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
1198 wait_for_completion(&mw->mw_complete);
1199 /* Re-arm the completion in case we want to wait on it again */
1200 INIT_COMPLETION(mw->mw_complete);
1201 return mw->mw_status;
1204 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
1205 struct ocfs2_mask_waiter *mw,
1209 BUG_ON(!list_empty(&mw->mw_item));
1211 assert_spin_locked(&lockres->l_lock);
1213 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
1218 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
1219 * if the mask still hadn't reached its goal */
1220 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
1221 struct ocfs2_mask_waiter *mw)
1223 unsigned long flags;
1226 spin_lock_irqsave(&lockres->l_lock, flags);
1227 if (!list_empty(&mw->mw_item)) {
1228 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
1231 list_del_init(&mw->mw_item);
1232 init_completion(&mw->mw_complete);
1234 spin_unlock_irqrestore(&lockres->l_lock, flags);
1240 static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
1241 struct ocfs2_lock_res *lockres)
1245 ret = wait_for_completion_interruptible(&mw->mw_complete);
1247 lockres_remove_mask_waiter(lockres, mw);
1249 ret = mw->mw_status;
1250 /* Re-arm the completion in case we want to wait on it again */
1251 INIT_COMPLETION(mw->mw_complete);
1255 static int ocfs2_cluster_lock(struct ocfs2_super *osb,
1256 struct ocfs2_lock_res *lockres,
1261 struct ocfs2_mask_waiter mw;
1262 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
1263 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1264 unsigned long flags;
1266 int noqueue_attempted = 0;
1270 ocfs2_init_mask_waiter(&mw);
1272 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
1273 lkm_flags |= DLM_LKF_VALBLK;
1278 if (catch_signals && signal_pending(current)) {
1283 spin_lock_irqsave(&lockres->l_lock, flags);
1285 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1286 "Cluster lock called on freeing lockres %s! flags "
1287 "0x%lx\n", lockres->l_name, lockres->l_flags);
1289 /* We only compare against the currently granted level
1290 * here. If the lock is blocked waiting on a downconvert,
1291 * we'll get caught below. */
1292 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1293 level > lockres->l_level) {
1294 /* is someone sitting in dlm_lock? If so, wait on
1296 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1301 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1302 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1303 /* is the lock is currently blocked on behalf of
1305 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1310 if (level > lockres->l_level) {
1311 if (noqueue_attempted > 0) {
1315 if (lkm_flags & DLM_LKF_NOQUEUE)
1316 noqueue_attempted = 1;
1318 if (lockres->l_action != OCFS2_AST_INVALID)
1319 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1320 lockres->l_name, lockres->l_action);
1322 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1323 lockres->l_action = OCFS2_AST_ATTACH;
1324 lkm_flags &= ~DLM_LKF_CONVERT;
1326 lockres->l_action = OCFS2_AST_CONVERT;
1327 lkm_flags |= DLM_LKF_CONVERT;
1330 lockres->l_requested = level;
1331 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1332 gen = lockres_set_pending(lockres);
1333 spin_unlock_irqrestore(&lockres->l_lock, flags);
1335 BUG_ON(level == DLM_LOCK_IV);
1336 BUG_ON(level == DLM_LOCK_NL);
1338 mlog(0, "lock %s, convert from %d to level = %d\n",
1339 lockres->l_name, lockres->l_level, level);
1341 /* call dlm_lock to upgrade lock now */
1342 ret = ocfs2_dlm_lock(osb->cconn,
1347 OCFS2_LOCK_ID_MAX_LEN - 1,
1349 lockres_clear_pending(lockres, gen, osb);
1351 if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
1353 ocfs2_log_dlm_error("ocfs2_dlm_lock",
1356 ocfs2_recover_from_dlm_error(lockres, 1);
1360 mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n",
1363 /* At this point we've gone inside the dlm and need to
1364 * complete our work regardless. */
1367 /* wait for busy to clear and carry on */
1371 /* Ok, if we get here then we're good to go. */
1372 ocfs2_inc_holders(lockres, level);
1376 spin_unlock_irqrestore(&lockres->l_lock, flags);
1379 * This is helping work around a lock inversion between the page lock
1380 * and dlm locks. One path holds the page lock while calling aops
1381 * which block acquiring dlm locks. The voting thread holds dlm
1382 * locks while acquiring page locks while down converting data locks.
1383 * This block is helping an aop path notice the inversion and back
1384 * off to unlock its page lock before trying the dlm lock again.
1386 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1387 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1389 if (lockres_remove_mask_waiter(lockres, &mw))
1395 ret = ocfs2_wait_for_mask(&mw);
1400 ocfs2_update_lock_stats(lockres, level, &mw, ret);
1406 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1407 struct ocfs2_lock_res *lockres,
1410 unsigned long flags;
1413 spin_lock_irqsave(&lockres->l_lock, flags);
1414 ocfs2_dec_holders(lockres, level);
1415 ocfs2_downconvert_on_unlock(osb, lockres);
1416 spin_unlock_irqrestore(&lockres->l_lock, flags);
1420 static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1421 struct ocfs2_lock_res *lockres,
1425 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1426 unsigned long flags;
1427 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
1429 spin_lock_irqsave(&lockres->l_lock, flags);
1430 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1431 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1432 spin_unlock_irqrestore(&lockres->l_lock, flags);
1434 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
1437 /* Grants us an EX lock on the data and metadata resources, skipping
1438 * the normal cluster directory lookup. Use this ONLY on newly created
1439 * inodes which other nodes can't possibly see, and which haven't been
1440 * hashed in the inode hash yet. This can give us a good performance
1441 * increase as it'll skip the network broadcast normally associated
1442 * with creating a new lock resource. */
1443 int ocfs2_create_new_inode_locks(struct inode *inode)
1446 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1449 BUG_ON(!ocfs2_inode_is_new(inode));
1453 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1455 /* NOTE: That we don't increment any of the holder counts, nor
1456 * do we add anything to a journal handle. Since this is
1457 * supposed to be a new inode which the cluster doesn't know
1458 * about yet, there is no need to. As far as the LVB handling
1459 * is concerned, this is basically like acquiring an EX lock
1460 * on a resource which has an invalid one -- we'll set it
1461 * valid when we release the EX. */
1463 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
1470 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
1471 * don't use a generation in their lock names.
1473 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
1479 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1490 int ocfs2_rw_lock(struct inode *inode, int write)
1493 struct ocfs2_lock_res *lockres;
1494 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1500 mlog(0, "inode %llu take %s RW lock\n",
1501 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1502 write ? "EXMODE" : "PRMODE");
1504 if (ocfs2_mount_local(osb))
1507 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1509 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1511 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1520 void ocfs2_rw_unlock(struct inode *inode, int write)
1522 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1523 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1524 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1528 mlog(0, "inode %llu drop %s RW lock\n",
1529 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1530 write ? "EXMODE" : "PRMODE");
1532 if (!ocfs2_mount_local(osb))
1533 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1539 * ocfs2_open_lock always get PR mode lock.
1541 int ocfs2_open_lock(struct inode *inode)
1544 struct ocfs2_lock_res *lockres;
1545 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1551 mlog(0, "inode %llu take PRMODE open lock\n",
1552 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1554 if (ocfs2_mount_local(osb))
1557 lockres = &OCFS2_I(inode)->ip_open_lockres;
1559 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1569 int ocfs2_try_open_lock(struct inode *inode, int write)
1571 int status = 0, level;
1572 struct ocfs2_lock_res *lockres;
1573 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1579 mlog(0, "inode %llu try to take %s open lock\n",
1580 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1581 write ? "EXMODE" : "PRMODE");
1583 if (ocfs2_mount_local(osb))
1586 lockres = &OCFS2_I(inode)->ip_open_lockres;
1588 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1591 * The file system may already holding a PRMODE/EXMODE open lock.
1592 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
1593 * other nodes and the -EAGAIN will indicate to the caller that
1594 * this inode is still in use.
1596 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1597 level, DLM_LKF_NOQUEUE, 0);
1605 * ocfs2_open_unlock unlock PR and EX mode open locks.
1607 void ocfs2_open_unlock(struct inode *inode)
1609 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1610 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1614 mlog(0, "inode %llu drop open lock\n",
1615 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1617 if (ocfs2_mount_local(osb))
1620 if(lockres->l_ro_holders)
1621 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1623 if(lockres->l_ex_holders)
1624 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1631 static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1635 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1636 unsigned long flags;
1637 struct ocfs2_mask_waiter mw;
1639 ocfs2_init_mask_waiter(&mw);
1642 spin_lock_irqsave(&lockres->l_lock, flags);
1643 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1644 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1646 spin_unlock_irqrestore(&lockres->l_lock, flags);
1647 ret = ocfs2_cancel_convert(osb, lockres);
1654 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1655 spin_unlock_irqrestore(&lockres->l_lock, flags);
1657 ocfs2_wait_for_mask(&mw);
1663 * We may still have gotten the lock, in which case there's no
1664 * point to restarting the syscall.
1666 if (lockres->l_level == level)
1669 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1670 lockres->l_flags, lockres->l_level, lockres->l_action);
1672 spin_unlock_irqrestore(&lockres->l_lock, flags);
1679 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1680 * flock() calls. The locking approach this requires is sufficiently
1681 * different from all other cluster lock types that we implement a
1682 * seperate path to the "low-level" dlm calls. In particular:
1684 * - No optimization of lock levels is done - we take at exactly
1685 * what's been requested.
1687 * - No lock caching is employed. We immediately downconvert to
1688 * no-lock at unlock time. This also means flock locks never go on
1689 * the blocking list).
1691 * - Since userspace can trivially deadlock itself with flock, we make
1692 * sure to allow cancellation of a misbehaving applications flock()
1695 * - Access to any flock lockres doesn't require concurrency, so we
1696 * can simplify the code by requiring the caller to guarantee
1697 * serialization of dlmglue flock calls.
1699 int ocfs2_file_lock(struct file *file, int ex, int trylock)
1701 int ret, level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1702 unsigned int lkm_flags = trylock ? DLM_LKF_NOQUEUE : 0;
1703 unsigned long flags;
1704 struct ocfs2_file_private *fp = file->private_data;
1705 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1706 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1707 struct ocfs2_mask_waiter mw;
1709 ocfs2_init_mask_waiter(&mw);
1711 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
1712 (lockres->l_level > DLM_LOCK_NL)) {
1714 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1715 "level: %u\n", lockres->l_name, lockres->l_flags,
1720 spin_lock_irqsave(&lockres->l_lock, flags);
1721 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1722 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1723 spin_unlock_irqrestore(&lockres->l_lock, flags);
1726 * Get the lock at NLMODE to start - that way we
1727 * can cancel the upconvert request if need be.
1729 ret = ocfs2_lock_create(osb, lockres, DLM_LOCK_NL, 0);
1735 ret = ocfs2_wait_for_mask(&mw);
1740 spin_lock_irqsave(&lockres->l_lock, flags);
1743 lockres->l_action = OCFS2_AST_CONVERT;
1744 lkm_flags |= DLM_LKF_CONVERT;
1745 lockres->l_requested = level;
1746 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1748 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1749 spin_unlock_irqrestore(&lockres->l_lock, flags);
1751 ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
1752 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1,
1755 if (!trylock || (ret != -EAGAIN)) {
1756 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
1760 ocfs2_recover_from_dlm_error(lockres, 1);
1761 lockres_remove_mask_waiter(lockres, &mw);
1765 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1766 if (ret == -ERESTARTSYS) {
1768 * Userspace can cause deadlock itself with
1769 * flock(). Current behavior locally is to allow the
1770 * deadlock, but abort the system call if a signal is
1771 * received. We follow this example, otherwise a
1772 * poorly written program could sit in kernel until
1775 * Handling this is a bit more complicated for Ocfs2
1776 * though. We can't exit this function with an
1777 * outstanding lock request, so a cancel convert is
1778 * required. We intentionally overwrite 'ret' - if the
1779 * cancel fails and the lock was granted, it's easier
1780 * to just bubble sucess back up to the user.
1782 ret = ocfs2_flock_handle_signal(lockres, level);
1783 } else if (!ret && (level > lockres->l_level)) {
1784 /* Trylock failed asynchronously */
1791 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1792 lockres->l_name, ex, trylock, ret);
1796 void ocfs2_file_unlock(struct file *file)
1800 unsigned long flags;
1801 struct ocfs2_file_private *fp = file->private_data;
1802 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1803 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1804 struct ocfs2_mask_waiter mw;
1806 ocfs2_init_mask_waiter(&mw);
1808 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
1811 if (lockres->l_level == DLM_LOCK_NL)
1814 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1815 lockres->l_name, lockres->l_flags, lockres->l_level,
1818 spin_lock_irqsave(&lockres->l_lock, flags);
1820 * Fake a blocking ast for the downconvert code.
1822 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
1823 lockres->l_blocking = DLM_LOCK_EX;
1825 gen = ocfs2_prepare_downconvert(lockres, DLM_LOCK_NL);
1826 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1827 spin_unlock_irqrestore(&lockres->l_lock, flags);
1829 ret = ocfs2_downconvert_lock(osb, lockres, DLM_LOCK_NL, 0, gen);
1835 ret = ocfs2_wait_for_mask(&mw);
1840 static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1841 struct ocfs2_lock_res *lockres)
1847 /* If we know that another node is waiting on our lock, kick
1848 * the downconvert thread * pre-emptively when we reach a release
1850 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1851 switch(lockres->l_blocking) {
1853 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1857 if (!lockres->l_ex_holders)
1866 ocfs2_wake_downconvert_thread(osb);
1871 #define OCFS2_SEC_BITS 34
1872 #define OCFS2_SEC_SHIFT (64 - 34)
1873 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1875 /* LVB only has room for 64 bits of time here so we pack it for
1877 static u64 ocfs2_pack_timespec(struct timespec *spec)
1880 u64 sec = spec->tv_sec;
1881 u32 nsec = spec->tv_nsec;
1883 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1888 /* Call this with the lockres locked. I am reasonably sure we don't
1889 * need ip_lock in this function as anyone who would be changing those
1890 * values is supposed to be blocked in ocfs2_inode_lock right now. */
1891 static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1893 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1894 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1895 struct ocfs2_meta_lvb *lvb;
1899 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
1902 * Invalidate the LVB of a deleted inode - this way other
1903 * nodes are forced to go to disk and discover the new inode
1906 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1907 lvb->lvb_version = 0;
1911 lvb->lvb_version = OCFS2_LVB_VERSION;
1912 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1913 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1914 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1915 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1916 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1917 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1918 lvb->lvb_iatime_packed =
1919 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1920 lvb->lvb_ictime_packed =
1921 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1922 lvb->lvb_imtime_packed =
1923 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1924 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
1925 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
1926 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
1929 mlog_meta_lvb(0, lockres);
1934 static void ocfs2_unpack_timespec(struct timespec *spec,
1937 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1938 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1941 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1943 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1944 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1945 struct ocfs2_meta_lvb *lvb;
1949 mlog_meta_lvb(0, lockres);
1951 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
1953 /* We're safe here without the lockres lock... */
1954 spin_lock(&oi->ip_lock);
1955 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1956 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1958 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1959 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
1960 ocfs2_set_inode_flags(inode);
1962 /* fast-symlinks are a special case */
1963 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1964 inode->i_blocks = 0;
1966 inode->i_blocks = ocfs2_inode_sector_count(inode);
1968 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1969 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1970 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1971 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1972 ocfs2_unpack_timespec(&inode->i_atime,
1973 be64_to_cpu(lvb->lvb_iatime_packed));
1974 ocfs2_unpack_timespec(&inode->i_mtime,
1975 be64_to_cpu(lvb->lvb_imtime_packed));
1976 ocfs2_unpack_timespec(&inode->i_ctime,
1977 be64_to_cpu(lvb->lvb_ictime_packed));
1978 spin_unlock(&oi->ip_lock);
1983 static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1984 struct ocfs2_lock_res *lockres)
1986 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
1988 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb)
1989 && lvb->lvb_version == OCFS2_LVB_VERSION
1990 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
1995 /* Determine whether a lock resource needs to be refreshed, and
1996 * arbitrate who gets to refresh it.
1998 * 0 means no refresh needed.
2000 * > 0 means you need to refresh this and you MUST call
2001 * ocfs2_complete_lock_res_refresh afterwards. */
2002 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
2004 unsigned long flags;
2010 spin_lock_irqsave(&lockres->l_lock, flags);
2011 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2012 spin_unlock_irqrestore(&lockres->l_lock, flags);
2016 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2017 spin_unlock_irqrestore(&lockres->l_lock, flags);
2019 ocfs2_wait_on_refreshing_lock(lockres);
2023 /* Ok, I'll be the one to refresh this lock. */
2024 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
2025 spin_unlock_irqrestore(&lockres->l_lock, flags);
2033 /* If status is non zero, I'll mark it as not being in refresh
2034 * anymroe, but i won't clear the needs refresh flag. */
2035 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
2038 unsigned long flags;
2041 spin_lock_irqsave(&lockres->l_lock, flags);
2042 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
2044 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
2045 spin_unlock_irqrestore(&lockres->l_lock, flags);
2047 wake_up(&lockres->l_event);
2052 /* may or may not return a bh if it went to disk. */
2053 static int ocfs2_inode_lock_update(struct inode *inode,
2054 struct buffer_head **bh)
2057 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2058 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
2059 struct ocfs2_dinode *fe;
2060 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2064 if (ocfs2_mount_local(osb))
2067 spin_lock(&oi->ip_lock);
2068 if (oi->ip_flags & OCFS2_INODE_DELETED) {
2069 mlog(0, "Orphaned inode %llu was deleted while we "
2070 "were waiting on a lock. ip_flags = 0x%x\n",
2071 (unsigned long long)oi->ip_blkno, oi->ip_flags);
2072 spin_unlock(&oi->ip_lock);
2076 spin_unlock(&oi->ip_lock);
2078 if (!ocfs2_should_refresh_lock_res(lockres))
2081 /* This will discard any caching information we might have had
2082 * for the inode metadata. */
2083 ocfs2_metadata_cache_purge(inode);
2085 ocfs2_extent_map_trunc(inode, 0);
2087 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
2088 mlog(0, "Trusting LVB on inode %llu\n",
2089 (unsigned long long)oi->ip_blkno);
2090 ocfs2_refresh_inode_from_lvb(inode);
2092 /* Boo, we have to go to disk. */
2093 /* read bh, cast, ocfs2_refresh_inode */
2094 status = ocfs2_read_inode_block(inode, bh);
2099 fe = (struct ocfs2_dinode *) (*bh)->b_data;
2101 /* This is a good chance to make sure we're not
2102 * locking an invalid object. ocfs2_read_inode_block()
2103 * already checked that the inode block is sane.
2105 * We bug on a stale inode here because we checked
2106 * above whether it was wiped from disk. The wiping
2107 * node provides a guarantee that we receive that
2108 * message and can mark the inode before dropping any
2109 * locks associated with it. */
2110 mlog_bug_on_msg(inode->i_generation !=
2111 le32_to_cpu(fe->i_generation),
2112 "Invalid dinode %llu disk generation: %u "
2113 "inode->i_generation: %u\n",
2114 (unsigned long long)oi->ip_blkno,
2115 le32_to_cpu(fe->i_generation),
2116 inode->i_generation);
2117 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
2118 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
2119 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
2120 (unsigned long long)oi->ip_blkno,
2121 (unsigned long long)le64_to_cpu(fe->i_dtime),
2122 le32_to_cpu(fe->i_flags));
2124 ocfs2_refresh_inode(inode, fe);
2125 ocfs2_track_lock_refresh(lockres);
2130 ocfs2_complete_lock_res_refresh(lockres, status);
2136 static int ocfs2_assign_bh(struct inode *inode,
2137 struct buffer_head **ret_bh,
2138 struct buffer_head *passed_bh)
2143 /* Ok, the update went to disk for us, use the
2145 *ret_bh = passed_bh;
2151 status = ocfs2_read_inode_block(inode, ret_bh);
2159 * returns < 0 error if the callback will never be called, otherwise
2160 * the result of the lock will be communicated via the callback.
2162 int ocfs2_inode_lock_full(struct inode *inode,
2163 struct buffer_head **ret_bh,
2167 int status, level, acquired;
2169 struct ocfs2_lock_res *lockres = NULL;
2170 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2171 struct buffer_head *local_bh = NULL;
2177 mlog(0, "inode %llu, take %s META lock\n",
2178 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2179 ex ? "EXMODE" : "PRMODE");
2183 /* We'll allow faking a readonly metadata lock for
2185 if (ocfs2_is_hard_readonly(osb)) {
2191 if (ocfs2_mount_local(osb))
2194 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
2195 ocfs2_wait_for_recovery(osb);
2197 lockres = &OCFS2_I(inode)->ip_inode_lockres;
2198 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2200 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
2201 dlm_flags |= DLM_LKF_NOQUEUE;
2203 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
2205 if (status != -EAGAIN && status != -EIOCBRETRY)
2210 /* Notify the error cleanup path to drop the cluster lock. */
2213 /* We wait twice because a node may have died while we were in
2214 * the lower dlm layers. The second time though, we've
2215 * committed to owning this lock so we don't allow signals to
2216 * abort the operation. */
2217 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
2218 ocfs2_wait_for_recovery(osb);
2222 * We only see this flag if we're being called from
2223 * ocfs2_read_locked_inode(). It means we're locking an inode
2224 * which hasn't been populated yet, so clear the refresh flag
2225 * and let the caller handle it.
2227 if (inode->i_state & I_NEW) {
2230 ocfs2_complete_lock_res_refresh(lockres, 0);
2234 /* This is fun. The caller may want a bh back, or it may
2235 * not. ocfs2_inode_lock_update definitely wants one in, but
2236 * may or may not read one, depending on what's in the
2237 * LVB. The result of all of this is that we've *only* gone to
2238 * disk if we have to, so the complexity is worthwhile. */
2239 status = ocfs2_inode_lock_update(inode, &local_bh);
2241 if (status != -ENOENT)
2247 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
2256 if (ret_bh && (*ret_bh)) {
2261 ocfs2_inode_unlock(inode, ex);
2272 * This is working around a lock inversion between tasks acquiring DLM
2273 * locks while holding a page lock and the downconvert thread which
2274 * blocks dlm lock acquiry while acquiring page locks.
2276 * ** These _with_page variantes are only intended to be called from aop
2277 * methods that hold page locks and return a very specific *positive* error
2278 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2280 * The DLM is called such that it returns -EAGAIN if it would have
2281 * blocked waiting for the downconvert thread. In that case we unlock
2282 * our page so the downconvert thread can make progress. Once we've
2283 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2284 * that called us can bubble that back up into the VFS who will then
2285 * immediately retry the aop call.
2287 * We do a blocking lock and immediate unlock before returning, though, so that
2288 * the lock has a great chance of being cached on this node by the time the VFS
2289 * calls back to retry the aop. This has a potential to livelock as nodes
2290 * ping locks back and forth, but that's a risk we're willing to take to avoid
2291 * the lock inversion simply.
2293 int ocfs2_inode_lock_with_page(struct inode *inode,
2294 struct buffer_head **ret_bh,
2300 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
2301 if (ret == -EAGAIN) {
2303 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2304 ocfs2_inode_unlock(inode, ex);
2305 ret = AOP_TRUNCATED_PAGE;
2311 int ocfs2_inode_lock_atime(struct inode *inode,
2312 struct vfsmount *vfsmnt,
2318 ret = ocfs2_inode_lock(inode, NULL, 0);
2325 * If we should update atime, we will get EX lock,
2326 * otherwise we just get PR lock.
2328 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2329 struct buffer_head *bh = NULL;
2331 ocfs2_inode_unlock(inode, 0);
2332 ret = ocfs2_inode_lock(inode, &bh, 1);
2338 if (ocfs2_should_update_atime(inode, vfsmnt))
2339 ocfs2_update_inode_atime(inode, bh);
2349 void ocfs2_inode_unlock(struct inode *inode,
2352 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2353 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
2354 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2358 mlog(0, "inode %llu drop %s META lock\n",
2359 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2360 ex ? "EXMODE" : "PRMODE");
2362 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2363 !ocfs2_mount_local(osb))
2364 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
2369 int ocfs2_orphan_scan_lock(struct ocfs2_super *osb, u32 *seqno, int ex)
2371 struct ocfs2_lock_res *lockres;
2372 struct ocfs2_orphan_scan_lvb *lvb;
2373 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2376 lockres = &osb->osb_orphan_scan.os_lockres;
2377 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2381 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2382 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
2383 lvb->lvb_version == OCFS2_ORPHAN_LVB_VERSION)
2384 *seqno = be32_to_cpu(lvb->lvb_os_seqno);
2386 *seqno = osb->osb_orphan_scan.os_seqno + 1;
2391 void ocfs2_orphan_scan_unlock(struct ocfs2_super *osb, u32 seqno, int ex)
2393 struct ocfs2_lock_res *lockres;
2394 struct ocfs2_orphan_scan_lvb *lvb;
2395 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2397 lockres = &osb->osb_orphan_scan.os_lockres;
2398 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2399 lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION;
2400 lvb->lvb_os_seqno = cpu_to_be32(seqno);
2401 ocfs2_cluster_unlock(osb, lockres, level);
2404 int ocfs2_super_lock(struct ocfs2_super *osb,
2408 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2409 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2413 if (ocfs2_is_hard_readonly(osb))
2416 if (ocfs2_mount_local(osb))
2419 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2425 /* The super block lock path is really in the best position to
2426 * know when resources covered by the lock need to be
2427 * refreshed, so we do it here. Of course, making sense of
2428 * everything is up to the caller :) */
2429 status = ocfs2_should_refresh_lock_res(lockres);
2435 status = ocfs2_refresh_slot_info(osb);
2437 ocfs2_complete_lock_res_refresh(lockres, status);
2441 ocfs2_track_lock_refresh(lockres);
2448 void ocfs2_super_unlock(struct ocfs2_super *osb,
2451 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2452 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2454 if (!ocfs2_mount_local(osb))
2455 ocfs2_cluster_unlock(osb, lockres, level);
2458 int ocfs2_rename_lock(struct ocfs2_super *osb)
2461 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2463 if (ocfs2_is_hard_readonly(osb))
2466 if (ocfs2_mount_local(osb))
2469 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
2476 void ocfs2_rename_unlock(struct ocfs2_super *osb)
2478 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2480 if (!ocfs2_mount_local(osb))
2481 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2484 int ocfs2_nfs_sync_lock(struct ocfs2_super *osb, int ex)
2487 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2489 if (ocfs2_is_hard_readonly(osb))
2492 if (ocfs2_mount_local(osb))
2495 status = ocfs2_cluster_lock(osb, lockres, ex ? LKM_EXMODE : LKM_PRMODE,
2498 mlog(ML_ERROR, "lock on nfs sync lock failed %d\n", status);
2503 void ocfs2_nfs_sync_unlock(struct ocfs2_super *osb, int ex)
2505 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2507 if (!ocfs2_mount_local(osb))
2508 ocfs2_cluster_unlock(osb, lockres,
2509 ex ? LKM_EXMODE : LKM_PRMODE);
2512 int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2515 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2516 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2517 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2521 if (ocfs2_is_hard_readonly(osb))
2524 if (ocfs2_mount_local(osb))
2527 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2534 void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2536 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2537 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2538 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2540 if (!ocfs2_mount_local(osb))
2541 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
2544 /* Reference counting of the dlm debug structure. We want this because
2545 * open references on the debug inodes can live on after a mount, so
2546 * we can't rely on the ocfs2_super to always exist. */
2547 static void ocfs2_dlm_debug_free(struct kref *kref)
2549 struct ocfs2_dlm_debug *dlm_debug;
2551 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2556 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2559 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2562 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2564 kref_get(&debug->d_refcnt);
2567 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2569 struct ocfs2_dlm_debug *dlm_debug;
2571 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2573 mlog_errno(-ENOMEM);
2577 kref_init(&dlm_debug->d_refcnt);
2578 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2579 dlm_debug->d_locking_state = NULL;
2584 /* Access to this is arbitrated for us via seq_file->sem. */
2585 struct ocfs2_dlm_seq_priv {
2586 struct ocfs2_dlm_debug *p_dlm_debug;
2587 struct ocfs2_lock_res p_iter_res;
2588 struct ocfs2_lock_res p_tmp_res;
2591 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2592 struct ocfs2_dlm_seq_priv *priv)
2594 struct ocfs2_lock_res *iter, *ret = NULL;
2595 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2597 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2599 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2600 /* discover the head of the list */
2601 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2602 mlog(0, "End of list found, %p\n", ret);
2606 /* We track our "dummy" iteration lockres' by a NULL
2608 if (iter->l_ops != NULL) {
2617 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2619 struct ocfs2_dlm_seq_priv *priv = m->private;
2620 struct ocfs2_lock_res *iter;
2622 spin_lock(&ocfs2_dlm_tracking_lock);
2623 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2625 /* Since lockres' have the lifetime of their container
2626 * (which can be inodes, ocfs2_supers, etc) we want to
2627 * copy this out to a temporary lockres while still
2628 * under the spinlock. Obviously after this we can't
2629 * trust any pointers on the copy returned, but that's
2630 * ok as the information we want isn't typically held
2632 priv->p_tmp_res = *iter;
2633 iter = &priv->p_tmp_res;
2635 spin_unlock(&ocfs2_dlm_tracking_lock);
2640 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2644 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2646 struct ocfs2_dlm_seq_priv *priv = m->private;
2647 struct ocfs2_lock_res *iter = v;
2648 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2650 spin_lock(&ocfs2_dlm_tracking_lock);
2651 iter = ocfs2_dlm_next_res(iter, priv);
2652 list_del_init(&dummy->l_debug_list);
2654 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2655 priv->p_tmp_res = *iter;
2656 iter = &priv->p_tmp_res;
2658 spin_unlock(&ocfs2_dlm_tracking_lock);
2663 /* So that debugfs.ocfs2 can determine which format is being used */
2664 #define OCFS2_DLM_DEBUG_STR_VERSION 2
2665 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2669 struct ocfs2_lock_res *lockres = v;
2674 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2676 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2677 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2679 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2681 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2683 seq_printf(m, "%d\t"
2694 lockres->l_unlock_action,
2695 lockres->l_ro_holders,
2696 lockres->l_ex_holders,
2697 lockres->l_requested,
2698 lockres->l_blocking);
2700 /* Dump the raw LVB */
2701 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2702 for(i = 0; i < DLM_LVB_LEN; i++)
2703 seq_printf(m, "0x%x\t", lvb[i]);
2705 #ifdef CONFIG_OCFS2_FS_STATS
2706 # define lock_num_prmode(_l) (_l)->l_lock_num_prmode
2707 # define lock_num_exmode(_l) (_l)->l_lock_num_exmode
2708 # define lock_num_prmode_failed(_l) (_l)->l_lock_num_prmode_failed
2709 # define lock_num_exmode_failed(_l) (_l)->l_lock_num_exmode_failed
2710 # define lock_total_prmode(_l) (_l)->l_lock_total_prmode
2711 # define lock_total_exmode(_l) (_l)->l_lock_total_exmode
2712 # define lock_max_prmode(_l) (_l)->l_lock_max_prmode
2713 # define lock_max_exmode(_l) (_l)->l_lock_max_exmode
2714 # define lock_refresh(_l) (_l)->l_lock_refresh
2716 # define lock_num_prmode(_l) (0ULL)
2717 # define lock_num_exmode(_l) (0ULL)
2718 # define lock_num_prmode_failed(_l) (0)
2719 # define lock_num_exmode_failed(_l) (0)
2720 # define lock_total_prmode(_l) (0ULL)
2721 # define lock_total_exmode(_l) (0ULL)
2722 # define lock_max_prmode(_l) (0)
2723 # define lock_max_exmode(_l) (0)
2724 # define lock_refresh(_l) (0)
2726 /* The following seq_print was added in version 2 of this output */
2727 seq_printf(m, "%llu\t"
2736 lock_num_prmode(lockres),
2737 lock_num_exmode(lockres),
2738 lock_num_prmode_failed(lockres),
2739 lock_num_exmode_failed(lockres),
2740 lock_total_prmode(lockres),
2741 lock_total_exmode(lockres),
2742 lock_max_prmode(lockres),
2743 lock_max_exmode(lockres),
2744 lock_refresh(lockres));
2747 seq_printf(m, "\n");
2751 static const struct seq_operations ocfs2_dlm_seq_ops = {
2752 .start = ocfs2_dlm_seq_start,
2753 .stop = ocfs2_dlm_seq_stop,
2754 .next = ocfs2_dlm_seq_next,
2755 .show = ocfs2_dlm_seq_show,
2758 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2760 struct seq_file *seq = (struct seq_file *) file->private_data;
2761 struct ocfs2_dlm_seq_priv *priv = seq->private;
2762 struct ocfs2_lock_res *res = &priv->p_iter_res;
2764 ocfs2_remove_lockres_tracking(res);
2765 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2766 return seq_release_private(inode, file);
2769 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2772 struct ocfs2_dlm_seq_priv *priv;
2773 struct seq_file *seq;
2774 struct ocfs2_super *osb;
2776 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2782 osb = inode->i_private;
2783 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2784 priv->p_dlm_debug = osb->osb_dlm_debug;
2785 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2787 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2794 seq = (struct seq_file *) file->private_data;
2795 seq->private = priv;
2797 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2804 static const struct file_operations ocfs2_dlm_debug_fops = {
2805 .open = ocfs2_dlm_debug_open,
2806 .release = ocfs2_dlm_debug_release,
2808 .llseek = seq_lseek,
2811 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2814 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2816 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2818 osb->osb_debug_root,
2820 &ocfs2_dlm_debug_fops);
2821 if (!dlm_debug->d_locking_state) {
2824 "Unable to create locking state debugfs file.\n");
2828 ocfs2_get_dlm_debug(dlm_debug);
2833 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2835 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2838 debugfs_remove(dlm_debug->d_locking_state);
2839 ocfs2_put_dlm_debug(dlm_debug);
2843 int ocfs2_dlm_init(struct ocfs2_super *osb)
2846 struct ocfs2_cluster_connection *conn = NULL;
2850 if (ocfs2_mount_local(osb)) {
2855 status = ocfs2_dlm_init_debug(osb);
2861 /* launch downconvert thread */
2862 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2863 if (IS_ERR(osb->dc_task)) {
2864 status = PTR_ERR(osb->dc_task);
2865 osb->dc_task = NULL;
2870 /* for now, uuid == domain */
2871 status = ocfs2_cluster_connect(osb->osb_cluster_stack,
2873 strlen(osb->uuid_str),
2874 ocfs2_do_node_down, osb,
2881 status = ocfs2_cluster_this_node(&osb->node_num);
2885 "could not find this host's node number\n");
2886 ocfs2_cluster_disconnect(conn, 0);
2891 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2892 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2893 ocfs2_nfs_sync_lock_res_init(&osb->osb_nfs_sync_lockres, osb);
2894 ocfs2_orphan_scan_lock_res_init(&osb->osb_orphan_scan.os_lockres, osb);
2901 ocfs2_dlm_shutdown_debug(osb);
2903 kthread_stop(osb->dc_task);
2910 void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
2915 ocfs2_drop_osb_locks(osb);
2918 * Now that we have dropped all locks and ocfs2_dismount_volume()
2919 * has disabled recovery, the DLM won't be talking to us. It's
2920 * safe to tear things down before disconnecting the cluster.
2924 kthread_stop(osb->dc_task);
2925 osb->dc_task = NULL;
2928 ocfs2_lock_res_free(&osb->osb_super_lockres);
2929 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2930 ocfs2_lock_res_free(&osb->osb_nfs_sync_lockres);
2931 ocfs2_lock_res_free(&osb->osb_orphan_scan.os_lockres);
2933 ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
2936 ocfs2_dlm_shutdown_debug(osb);
2941 static void ocfs2_unlock_ast(void *opaque, int error)
2943 struct ocfs2_lock_res *lockres = opaque;
2944 unsigned long flags;
2948 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2949 lockres->l_unlock_action);
2951 spin_lock_irqsave(&lockres->l_lock, flags);
2953 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
2954 "unlock_action %d\n", error, lockres->l_name,
2955 lockres->l_unlock_action);
2956 spin_unlock_irqrestore(&lockres->l_lock, flags);
2960 switch(lockres->l_unlock_action) {
2961 case OCFS2_UNLOCK_CANCEL_CONVERT:
2962 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2963 lockres->l_action = OCFS2_AST_INVALID;
2964 /* Downconvert thread may have requeued this lock, we
2965 * need to wake it. */
2966 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2967 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres));
2969 case OCFS2_UNLOCK_DROP_LOCK:
2970 lockres->l_level = DLM_LOCK_IV;
2976 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2977 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2978 wake_up(&lockres->l_event);
2979 spin_unlock_irqrestore(&lockres->l_lock, flags);
2984 static int ocfs2_drop_lock(struct ocfs2_super *osb,
2985 struct ocfs2_lock_res *lockres)
2988 unsigned long flags;
2991 /* We didn't get anywhere near actually using this lockres. */
2992 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2995 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
2996 lkm_flags |= DLM_LKF_VALBLK;
2998 spin_lock_irqsave(&lockres->l_lock, flags);
3000 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
3001 "lockres %s, flags 0x%lx\n",
3002 lockres->l_name, lockres->l_flags);
3004 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
3005 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
3006 "%u, unlock_action = %u\n",
3007 lockres->l_name, lockres->l_flags, lockres->l_action,
3008 lockres->l_unlock_action);
3010 spin_unlock_irqrestore(&lockres->l_lock, flags);
3012 /* XXX: Today we just wait on any busy
3013 * locks... Perhaps we need to cancel converts in the
3015 ocfs2_wait_on_busy_lock(lockres);
3017 spin_lock_irqsave(&lockres->l_lock, flags);
3020 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3021 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
3022 lockres->l_level == DLM_LOCK_EX &&
3023 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3024 lockres->l_ops->set_lvb(lockres);
3027 if (lockres->l_flags & OCFS2_LOCK_BUSY)
3028 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
3030 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
3031 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
3033 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
3034 spin_unlock_irqrestore(&lockres->l_lock, flags);
3038 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
3040 /* make sure we never get here while waiting for an ast to
3042 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
3044 /* is this necessary? */
3045 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3046 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
3047 spin_unlock_irqrestore(&lockres->l_lock, flags);
3049 mlog(0, "lock %s\n", lockres->l_name);
3051 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags,
3054 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
3055 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
3056 ocfs2_dlm_dump_lksb(&lockres->l_lksb);
3059 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
3062 ocfs2_wait_on_busy_lock(lockres);
3068 /* Mark the lockres as being dropped. It will no longer be
3069 * queued if blocking, but we still may have to wait on it
3070 * being dequeued from the downconvert thread before we can consider
3073 * You can *not* attempt to call cluster_lock on this lockres anymore. */
3074 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
3077 struct ocfs2_mask_waiter mw;
3078 unsigned long flags;
3080 ocfs2_init_mask_waiter(&mw);
3082 spin_lock_irqsave(&lockres->l_lock, flags);
3083 lockres->l_flags |= OCFS2_LOCK_FREEING;
3084 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
3085 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
3086 spin_unlock_irqrestore(&lockres->l_lock, flags);
3088 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
3090 status = ocfs2_wait_for_mask(&mw);
3094 spin_lock_irqsave(&lockres->l_lock, flags);
3096 spin_unlock_irqrestore(&lockres->l_lock, flags);
3099 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
3100 struct ocfs2_lock_res *lockres)
3104 ocfs2_mark_lockres_freeing(lockres);
3105 ret = ocfs2_drop_lock(osb, lockres);
3110 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
3112 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
3113 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
3114 ocfs2_simple_drop_lockres(osb, &osb->osb_nfs_sync_lockres);
3115 ocfs2_simple_drop_lockres(osb, &osb->osb_orphan_scan.os_lockres);
3118 int ocfs2_drop_inode_locks(struct inode *inode)
3124 /* No need to call ocfs2_mark_lockres_freeing here -
3125 * ocfs2_clear_inode has done it for us. */
3127 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
3128 &OCFS2_I(inode)->ip_open_lockres);
3134 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
3135 &OCFS2_I(inode)->ip_inode_lockres);
3138 if (err < 0 && !status)
3141 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
3142 &OCFS2_I(inode)->ip_rw_lockres);
3145 if (err < 0 && !status)
3152 static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
3155 assert_spin_locked(&lockres->l_lock);
3157 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
3159 if (lockres->l_level <= new_level) {
3160 mlog(ML_ERROR, "lockres->l_level (%d) <= new_level (%d)\n",
3161 lockres->l_level, new_level);
3165 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
3166 lockres->l_name, new_level, lockres->l_blocking);
3168 lockres->l_action = OCFS2_AST_DOWNCONVERT;
3169 lockres->l_requested = new_level;
3170 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3171 return lockres_set_pending(lockres);
3174 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
3175 struct ocfs2_lock_res *lockres,
3178 unsigned int generation)
3181 u32 dlm_flags = DLM_LKF_CONVERT;
3186 dlm_flags |= DLM_LKF_VALBLK;
3188 ret = ocfs2_dlm_lock(osb->cconn,
3193 OCFS2_LOCK_ID_MAX_LEN - 1,
3195 lockres_clear_pending(lockres, generation, osb);
3197 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
3198 ocfs2_recover_from_dlm_error(lockres, 1);
3208 /* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
3209 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
3210 struct ocfs2_lock_res *lockres)
3212 assert_spin_locked(&lockres->l_lock);
3215 mlog(0, "lock %s\n", lockres->l_name);
3217 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
3218 /* If we're already trying to cancel a lock conversion
3219 * then just drop the spinlock and allow the caller to
3220 * requeue this lock. */
3222 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
3226 /* were we in a convert when we got the bast fire? */
3227 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
3228 lockres->l_action != OCFS2_AST_DOWNCONVERT);
3229 /* set things up for the unlockast to know to just
3230 * clear out the ast_action and unset busy, etc. */
3231 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
3233 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
3234 "lock %s, invalid flags: 0x%lx\n",
3235 lockres->l_name, lockres->l_flags);
3240 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
3241 struct ocfs2_lock_res *lockres)
3246 mlog(0, "lock %s\n", lockres->l_name);
3248 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
3249 DLM_LKF_CANCEL, lockres);
3251 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
3252 ocfs2_recover_from_dlm_error(lockres, 0);
3255 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres->l_name);
3261 static int ocfs2_unblock_lock(struct ocfs2_super *osb,
3262 struct ocfs2_lock_res *lockres,
3263 struct ocfs2_unblock_ctl *ctl)
3265 unsigned long flags;
3274 spin_lock_irqsave(&lockres->l_lock, flags);
3276 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
3279 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
3281 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3282 * exists entirely for one reason - another thread has set
3283 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3285 * If we do ocfs2_cancel_convert() before the other thread
3286 * calls dlm_lock(), our cancel will do nothing. We will
3287 * get no ast, and we will have no way of knowing the
3288 * cancel failed. Meanwhile, the other thread will call
3289 * into dlm_lock() and wait...forever.
3291 * Why forever? Because another node has asked for the
3292 * lock first; that's why we're here in unblock_lock().
3294 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3295 * set, we just requeue the unblock. Only when the other
3296 * thread has called dlm_lock() and cleared PENDING will
3297 * we then cancel their request.
3299 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3300 * at the same time they set OCFS2_DLM_BUSY. They must
3301 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3303 if (lockres->l_flags & OCFS2_LOCK_PENDING)
3307 ret = ocfs2_prepare_cancel_convert(osb, lockres);
3308 spin_unlock_irqrestore(&lockres->l_lock, flags);
3310 ret = ocfs2_cancel_convert(osb, lockres);
3317 /* if we're blocking an exclusive and we have *any* holders,
3319 if ((lockres->l_blocking == DLM_LOCK_EX)
3320 && (lockres->l_ex_holders || lockres->l_ro_holders))
3323 /* If it's a PR we're blocking, then only
3324 * requeue if we've got any EX holders */
3325 if (lockres->l_blocking == DLM_LOCK_PR &&
3326 lockres->l_ex_holders)
3330 * Can we get a lock in this state if the holder counts are
3331 * zero? The meta data unblock code used to check this.
3333 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
3334 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
3337 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
3339 if (lockres->l_ops->check_downconvert
3340 && !lockres->l_ops->check_downconvert(lockres, new_level))
3343 /* If we get here, then we know that there are no more
3344 * incompatible holders (and anyone asking for an incompatible
3345 * lock is blocked). We can now downconvert the lock */
3346 if (!lockres->l_ops->downconvert_worker)
3349 /* Some lockres types want to do a bit of work before
3350 * downconverting a lock. Allow that here. The worker function
3351 * may sleep, so we save off a copy of what we're blocking as
3352 * it may change while we're not holding the spin lock. */
3353 blocking = lockres->l_blocking;
3354 spin_unlock_irqrestore(&lockres->l_lock, flags);
3356 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
3358 if (ctl->unblock_action == UNBLOCK_STOP_POST)
3361 spin_lock_irqsave(&lockres->l_lock, flags);
3362 if (blocking != lockres->l_blocking) {
3363 /* If this changed underneath us, then we can't drop
3371 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3372 if (lockres->l_level == DLM_LOCK_EX)
3376 * We only set the lvb if the lock has been fully
3377 * refreshed - otherwise we risk setting stale
3378 * data. Otherwise, there's no need to actually clear
3379 * out the lvb here as it's value is still valid.
3381 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3382 lockres->l_ops->set_lvb(lockres);
3385 gen = ocfs2_prepare_downconvert(lockres, new_level);
3386 spin_unlock_irqrestore(&lockres->l_lock, flags);
3387 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
3395 spin_unlock_irqrestore(&lockres->l_lock, flags);
3402 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3405 struct inode *inode;
3406 struct address_space *mapping;
3408 inode = ocfs2_lock_res_inode(lockres);
3409 mapping = inode->i_mapping;
3411 if (!S_ISREG(inode->i_mode))
3415 * We need this before the filemap_fdatawrite() so that it can
3416 * transfer the dirty bit from the PTE to the
3417 * page. Unfortunately this means that even for EX->PR
3418 * downconverts, we'll lose our mappings and have to build
3421 unmap_mapping_range(mapping, 0, 0, 0);
3423 if (filemap_fdatawrite(mapping)) {
3424 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3425 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3427 sync_mapping_buffers(mapping);
3428 if (blocking == DLM_LOCK_EX) {
3429 truncate_inode_pages(mapping, 0);
3431 /* We only need to wait on the I/O if we're not also
3432 * truncating pages because truncate_inode_pages waits
3433 * for us above. We don't truncate pages if we're
3434 * blocking anything < EXMODE because we want to keep
3435 * them around in that case. */
3436 filemap_fdatawait(mapping);
3440 return UNBLOCK_CONTINUE;
3443 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3446 struct inode *inode = ocfs2_lock_res_inode(lockres);
3447 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
3449 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3450 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
3455 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
3459 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3461 struct inode *inode = ocfs2_lock_res_inode(lockres);
3463 __ocfs2_stuff_meta_lvb(inode);
3467 * Does the final reference drop on our dentry lock. Right now this
3468 * happens in the downconvert thread, but we could choose to simplify the
3469 * dlmglue API and push these off to the ocfs2_wq in the future.
3471 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3472 struct ocfs2_lock_res *lockres)
3474 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3475 ocfs2_dentry_lock_put(osb, dl);
3479 * d_delete() matching dentries before the lock downconvert.
3481 * At this point, any process waiting to destroy the
3482 * dentry_lock due to last ref count is stopped by the
3483 * OCFS2_LOCK_QUEUED flag.
3485 * We have two potential problems
3487 * 1) If we do the last reference drop on our dentry_lock (via dput)
3488 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3489 * the downconvert to finish. Instead we take an elevated
3490 * reference and push the drop until after we've completed our
3491 * unblock processing.
3493 * 2) There might be another process with a final reference,
3494 * waiting on us to finish processing. If this is the case, we
3495 * detect it and exit out - there's no more dentries anyway.
3497 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3500 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3501 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3502 struct dentry *dentry;
3503 unsigned long flags;
3507 * This node is blocking another node from getting a read
3508 * lock. This happens when we've renamed within a
3509 * directory. We've forced the other nodes to d_delete(), but
3510 * we never actually dropped our lock because it's still
3511 * valid. The downconvert code will retain a PR for this node,
3512 * so there's no further work to do.
3514 if (blocking == DLM_LOCK_PR)
3515 return UNBLOCK_CONTINUE;
3518 * Mark this inode as potentially orphaned. The code in
3519 * ocfs2_delete_inode() will figure out whether it actually
3520 * needs to be freed or not.
3522 spin_lock(&oi->ip_lock);
3523 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3524 spin_unlock(&oi->ip_lock);
3527 * Yuck. We need to make sure however that the check of
3528 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3529 * respect to a reference decrement or the setting of that
3532 spin_lock_irqsave(&lockres->l_lock, flags);
3533 spin_lock(&dentry_attach_lock);
3534 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3539 spin_unlock(&dentry_attach_lock);
3540 spin_unlock_irqrestore(&lockres->l_lock, flags);
3542 mlog(0, "extra_ref = %d\n", extra_ref);
3545 * We have a process waiting on us in ocfs2_dentry_iput(),
3546 * which means we can't have any more outstanding
3547 * aliases. There's no need to do any more work.
3550 return UNBLOCK_CONTINUE;
3552 spin_lock(&dentry_attach_lock);
3554 dentry = ocfs2_find_local_alias(dl->dl_inode,
3555 dl->dl_parent_blkno, 1);
3558 spin_unlock(&dentry_attach_lock);
3560 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3561 dentry->d_name.name);
3564 * The following dcache calls may do an
3565 * iput(). Normally we don't want that from the
3566 * downconverting thread, but in this case it's ok
3567 * because the requesting node already has an
3568 * exclusive lock on the inode, so it can't be queued
3569 * for a downconvert.
3574 spin_lock(&dentry_attach_lock);
3576 spin_unlock(&dentry_attach_lock);
3579 * If we are the last holder of this dentry lock, there is no
3580 * reason to downconvert so skip straight to the unlock.
3582 if (dl->dl_count == 1)
3583 return UNBLOCK_STOP_POST;
3585 return UNBLOCK_CONTINUE_POST;
3588 static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres)
3590 struct ocfs2_qinfo_lvb *lvb;
3591 struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres);
3592 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3593 oinfo->dqi_gi.dqi_type);
3597 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
3598 lvb->lvb_version = OCFS2_QINFO_LVB_VERSION;
3599 lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace);
3600 lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace);
3601 lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms);
3602 lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks);
3603 lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk);
3604 lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry);
3609 void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3611 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3612 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3613 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3616 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
3617 ocfs2_cluster_unlock(osb, lockres, level);
3621 static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo)
3623 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3624 oinfo->dqi_gi.dqi_type);
3625 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3626 struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
3627 struct buffer_head *bh = NULL;
3628 struct ocfs2_global_disk_dqinfo *gdinfo;
3631 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
3632 lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) {
3633 info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace);
3634 info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace);
3635 oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms);
3636 oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks);
3637 oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk);
3638 oinfo->dqi_gi.dqi_free_entry =
3639 be32_to_cpu(lvb->lvb_free_entry);
3641 status = ocfs2_read_quota_block(oinfo->dqi_gqinode, 0, &bh);
3646 gdinfo = (struct ocfs2_global_disk_dqinfo *)
3647 (bh->b_data + OCFS2_GLOBAL_INFO_OFF);
3648 info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace);
3649 info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace);
3650 oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms);
3651 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks);
3652 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk);
3653 oinfo->dqi_gi.dqi_free_entry =
3654 le32_to_cpu(gdinfo->dqi_free_entry);
3656 ocfs2_track_lock_refresh(lockres);
3663 /* Lock quota info, this function expects at least shared lock on the quota file
3664 * so that we can safely refresh quota info from disk. */
3665 int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3667 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3668 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3669 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3674 /* On RO devices, locking really isn't needed... */
3675 if (ocfs2_is_hard_readonly(osb)) {
3680 if (ocfs2_mount_local(osb))
3683 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
3688 if (!ocfs2_should_refresh_lock_res(lockres))
3690 /* OK, we have the lock but we need to refresh the quota info */
3691 status = ocfs2_refresh_qinfo(oinfo);
3693 ocfs2_qinfo_unlock(oinfo, ex);
3694 ocfs2_complete_lock_res_refresh(lockres, status);
3701 * This is the filesystem locking protocol. It provides the lock handling
3702 * hooks for the underlying DLM. It has a maximum version number.
3703 * The version number allows interoperability with systems running at
3704 * the same major number and an equal or smaller minor number.
3706 * Whenever the filesystem does new things with locks (adds or removes a
3707 * lock, orders them differently, does different things underneath a lock),
3708 * the version must be changed. The protocol is negotiated when joining
3709 * the dlm domain. A node may join the domain if its major version is
3710 * identical to all other nodes and its minor version is greater than
3711 * or equal to all other nodes. When its minor version is greater than
3712 * the other nodes, it will run at the minor version specified by the
3715 * If a locking change is made that will not be compatible with older
3716 * versions, the major number must be increased and the minor version set
3717 * to zero. If a change merely adds a behavior that can be disabled when
3718 * speaking to older versions, the minor version must be increased. If a
3719 * change adds a fully backwards compatible change (eg, LVB changes that
3720 * are just ignored by older versions), the version does not need to be
3723 static struct ocfs2_locking_protocol lproto = {
3725 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
3726 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
3728 .lp_lock_ast = ocfs2_locking_ast,
3729 .lp_blocking_ast = ocfs2_blocking_ast,
3730 .lp_unlock_ast = ocfs2_unlock_ast,
3733 void ocfs2_set_locking_protocol(void)
3735 ocfs2_stack_glue_set_locking_protocol(&lproto);
3739 static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3740 struct ocfs2_lock_res *lockres)
3743 struct ocfs2_unblock_ctl ctl = {0, 0,};
3744 unsigned long flags;
3746 /* Our reference to the lockres in this function can be
3747 * considered valid until we remove the OCFS2_LOCK_QUEUED
3753 BUG_ON(!lockres->l_ops);
3755 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3757 /* Detect whether a lock has been marked as going away while
3758 * the downconvert thread was processing other things. A lock can
3759 * still be marked with OCFS2_LOCK_FREEING after this check,
3760 * but short circuiting here will still save us some
3762 spin_lock_irqsave(&lockres->l_lock, flags);
3763 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3765 spin_unlock_irqrestore(&lockres->l_lock, flags);
3767 status = ocfs2_unblock_lock(osb, lockres, &ctl);
3771 spin_lock_irqsave(&lockres->l_lock, flags);
3773 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
3774 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3776 ocfs2_schedule_blocked_lock(osb, lockres);
3778 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
3779 ctl.requeue ? "yes" : "no");
3780 spin_unlock_irqrestore(&lockres->l_lock, flags);
3782 if (ctl.unblock_action != UNBLOCK_CONTINUE
3783 && lockres->l_ops->post_unlock)
3784 lockres->l_ops->post_unlock(osb, lockres);
3789 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3790 struct ocfs2_lock_res *lockres)
3794 assert_spin_locked(&lockres->l_lock);
3796 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3797 /* Do not schedule a lock for downconvert when it's on
3798 * the way to destruction - any nodes wanting access
3799 * to the resource will get it soon. */
3800 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3801 lockres->l_name, lockres->l_flags);
3805 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3807 spin_lock(&osb->dc_task_lock);
3808 if (list_empty(&lockres->l_blocked_list)) {
3809 list_add_tail(&lockres->l_blocked_list,
3810 &osb->blocked_lock_list);
3811 osb->blocked_lock_count++;
3813 spin_unlock(&osb->dc_task_lock);
3818 static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3820 unsigned long processed;
3821 struct ocfs2_lock_res *lockres;
3825 spin_lock(&osb->dc_task_lock);
3826 /* grab this early so we know to try again if a state change and
3827 * wake happens part-way through our work */
3828 osb->dc_work_sequence = osb->dc_wake_sequence;
3830 processed = osb->blocked_lock_count;
3832 BUG_ON(list_empty(&osb->blocked_lock_list));
3834 lockres = list_entry(osb->blocked_lock_list.next,
3835 struct ocfs2_lock_res, l_blocked_list);
3836 list_del_init(&lockres->l_blocked_list);
3837 osb->blocked_lock_count--;
3838 spin_unlock(&osb->dc_task_lock);
3843 ocfs2_process_blocked_lock(osb, lockres);
3845 spin_lock(&osb->dc_task_lock);
3847 spin_unlock(&osb->dc_task_lock);
3852 static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3856 spin_lock(&osb->dc_task_lock);
3857 if (list_empty(&osb->blocked_lock_list))
3860 spin_unlock(&osb->dc_task_lock);
3864 static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
3866 int should_wake = 0;
3868 spin_lock(&osb->dc_task_lock);
3869 if (osb->dc_work_sequence != osb->dc_wake_sequence)
3871 spin_unlock(&osb->dc_task_lock);
3876 static int ocfs2_downconvert_thread(void *arg)
3879 struct ocfs2_super *osb = arg;
3881 /* only quit once we've been asked to stop and there is no more
3883 while (!(kthread_should_stop() &&
3884 ocfs2_downconvert_thread_lists_empty(osb))) {
3886 wait_event_interruptible(osb->dc_event,
3887 ocfs2_downconvert_thread_should_wake(osb) ||
3888 kthread_should_stop());
3890 mlog(0, "downconvert_thread: awoken\n");
3892 ocfs2_downconvert_thread_do_work(osb);
3895 osb->dc_task = NULL;
3899 void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
3901 spin_lock(&osb->dc_task_lock);
3902 /* make sure the voting thread gets a swipe at whatever changes
3903 * the caller may have made to the voting state */
3904 osb->dc_wake_sequence++;
3905 spin_unlock(&osb->dc_task_lock);
3906 wake_up(&osb->dc_event);