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)
2371 struct ocfs2_lock_res *lockres;
2372 struct ocfs2_orphan_scan_lvb *lvb;
2375 if (ocfs2_is_hard_readonly(osb))
2378 if (ocfs2_mount_local(osb))
2381 lockres = &osb->osb_orphan_scan.os_lockres;
2382 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
2386 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2387 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
2388 lvb->lvb_version == OCFS2_ORPHAN_LVB_VERSION)
2389 *seqno = be32_to_cpu(lvb->lvb_os_seqno);
2391 *seqno = osb->osb_orphan_scan.os_seqno + 1;
2396 void ocfs2_orphan_scan_unlock(struct ocfs2_super *osb, u32 seqno)
2398 struct ocfs2_lock_res *lockres;
2399 struct ocfs2_orphan_scan_lvb *lvb;
2401 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb)) {
2402 lockres = &osb->osb_orphan_scan.os_lockres;
2403 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2404 lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION;
2405 lvb->lvb_os_seqno = cpu_to_be32(seqno);
2406 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2410 int ocfs2_super_lock(struct ocfs2_super *osb,
2414 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2415 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2419 if (ocfs2_is_hard_readonly(osb))
2422 if (ocfs2_mount_local(osb))
2425 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2431 /* The super block lock path is really in the best position to
2432 * know when resources covered by the lock need to be
2433 * refreshed, so we do it here. Of course, making sense of
2434 * everything is up to the caller :) */
2435 status = ocfs2_should_refresh_lock_res(lockres);
2441 status = ocfs2_refresh_slot_info(osb);
2443 ocfs2_complete_lock_res_refresh(lockres, status);
2447 ocfs2_track_lock_refresh(lockres);
2454 void ocfs2_super_unlock(struct ocfs2_super *osb,
2457 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2458 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2460 if (!ocfs2_mount_local(osb))
2461 ocfs2_cluster_unlock(osb, lockres, level);
2464 int ocfs2_rename_lock(struct ocfs2_super *osb)
2467 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2469 if (ocfs2_is_hard_readonly(osb))
2472 if (ocfs2_mount_local(osb))
2475 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
2482 void ocfs2_rename_unlock(struct ocfs2_super *osb)
2484 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2486 if (!ocfs2_mount_local(osb))
2487 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2490 int ocfs2_nfs_sync_lock(struct ocfs2_super *osb, int ex)
2493 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2495 if (ocfs2_is_hard_readonly(osb))
2498 if (ocfs2_mount_local(osb))
2501 status = ocfs2_cluster_lock(osb, lockres, ex ? LKM_EXMODE : LKM_PRMODE,
2504 mlog(ML_ERROR, "lock on nfs sync lock failed %d\n", status);
2509 void ocfs2_nfs_sync_unlock(struct ocfs2_super *osb, int ex)
2511 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2513 if (!ocfs2_mount_local(osb))
2514 ocfs2_cluster_unlock(osb, lockres,
2515 ex ? LKM_EXMODE : LKM_PRMODE);
2518 int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2521 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2522 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2523 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2527 if (ocfs2_is_hard_readonly(osb))
2530 if (ocfs2_mount_local(osb))
2533 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2540 void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2542 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2543 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2544 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2546 if (!ocfs2_mount_local(osb))
2547 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
2550 /* Reference counting of the dlm debug structure. We want this because
2551 * open references on the debug inodes can live on after a mount, so
2552 * we can't rely on the ocfs2_super to always exist. */
2553 static void ocfs2_dlm_debug_free(struct kref *kref)
2555 struct ocfs2_dlm_debug *dlm_debug;
2557 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2562 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2565 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2568 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2570 kref_get(&debug->d_refcnt);
2573 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2575 struct ocfs2_dlm_debug *dlm_debug;
2577 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2579 mlog_errno(-ENOMEM);
2583 kref_init(&dlm_debug->d_refcnt);
2584 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2585 dlm_debug->d_locking_state = NULL;
2590 /* Access to this is arbitrated for us via seq_file->sem. */
2591 struct ocfs2_dlm_seq_priv {
2592 struct ocfs2_dlm_debug *p_dlm_debug;
2593 struct ocfs2_lock_res p_iter_res;
2594 struct ocfs2_lock_res p_tmp_res;
2597 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2598 struct ocfs2_dlm_seq_priv *priv)
2600 struct ocfs2_lock_res *iter, *ret = NULL;
2601 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2603 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2605 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2606 /* discover the head of the list */
2607 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2608 mlog(0, "End of list found, %p\n", ret);
2612 /* We track our "dummy" iteration lockres' by a NULL
2614 if (iter->l_ops != NULL) {
2623 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2625 struct ocfs2_dlm_seq_priv *priv = m->private;
2626 struct ocfs2_lock_res *iter;
2628 spin_lock(&ocfs2_dlm_tracking_lock);
2629 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2631 /* Since lockres' have the lifetime of their container
2632 * (which can be inodes, ocfs2_supers, etc) we want to
2633 * copy this out to a temporary lockres while still
2634 * under the spinlock. Obviously after this we can't
2635 * trust any pointers on the copy returned, but that's
2636 * ok as the information we want isn't typically held
2638 priv->p_tmp_res = *iter;
2639 iter = &priv->p_tmp_res;
2641 spin_unlock(&ocfs2_dlm_tracking_lock);
2646 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2650 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2652 struct ocfs2_dlm_seq_priv *priv = m->private;
2653 struct ocfs2_lock_res *iter = v;
2654 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2656 spin_lock(&ocfs2_dlm_tracking_lock);
2657 iter = ocfs2_dlm_next_res(iter, priv);
2658 list_del_init(&dummy->l_debug_list);
2660 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2661 priv->p_tmp_res = *iter;
2662 iter = &priv->p_tmp_res;
2664 spin_unlock(&ocfs2_dlm_tracking_lock);
2669 /* So that debugfs.ocfs2 can determine which format is being used */
2670 #define OCFS2_DLM_DEBUG_STR_VERSION 2
2671 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2675 struct ocfs2_lock_res *lockres = v;
2680 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2682 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2683 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2685 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2687 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2689 seq_printf(m, "%d\t"
2700 lockres->l_unlock_action,
2701 lockres->l_ro_holders,
2702 lockres->l_ex_holders,
2703 lockres->l_requested,
2704 lockres->l_blocking);
2706 /* Dump the raw LVB */
2707 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2708 for(i = 0; i < DLM_LVB_LEN; i++)
2709 seq_printf(m, "0x%x\t", lvb[i]);
2711 #ifdef CONFIG_OCFS2_FS_STATS
2712 # define lock_num_prmode(_l) (_l)->l_lock_num_prmode
2713 # define lock_num_exmode(_l) (_l)->l_lock_num_exmode
2714 # define lock_num_prmode_failed(_l) (_l)->l_lock_num_prmode_failed
2715 # define lock_num_exmode_failed(_l) (_l)->l_lock_num_exmode_failed
2716 # define lock_total_prmode(_l) (_l)->l_lock_total_prmode
2717 # define lock_total_exmode(_l) (_l)->l_lock_total_exmode
2718 # define lock_max_prmode(_l) (_l)->l_lock_max_prmode
2719 # define lock_max_exmode(_l) (_l)->l_lock_max_exmode
2720 # define lock_refresh(_l) (_l)->l_lock_refresh
2722 # define lock_num_prmode(_l) (0ULL)
2723 # define lock_num_exmode(_l) (0ULL)
2724 # define lock_num_prmode_failed(_l) (0)
2725 # define lock_num_exmode_failed(_l) (0)
2726 # define lock_total_prmode(_l) (0ULL)
2727 # define lock_total_exmode(_l) (0ULL)
2728 # define lock_max_prmode(_l) (0)
2729 # define lock_max_exmode(_l) (0)
2730 # define lock_refresh(_l) (0)
2732 /* The following seq_print was added in version 2 of this output */
2733 seq_printf(m, "%llu\t"
2742 lock_num_prmode(lockres),
2743 lock_num_exmode(lockres),
2744 lock_num_prmode_failed(lockres),
2745 lock_num_exmode_failed(lockres),
2746 lock_total_prmode(lockres),
2747 lock_total_exmode(lockres),
2748 lock_max_prmode(lockres),
2749 lock_max_exmode(lockres),
2750 lock_refresh(lockres));
2753 seq_printf(m, "\n");
2757 static const struct seq_operations ocfs2_dlm_seq_ops = {
2758 .start = ocfs2_dlm_seq_start,
2759 .stop = ocfs2_dlm_seq_stop,
2760 .next = ocfs2_dlm_seq_next,
2761 .show = ocfs2_dlm_seq_show,
2764 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2766 struct seq_file *seq = (struct seq_file *) file->private_data;
2767 struct ocfs2_dlm_seq_priv *priv = seq->private;
2768 struct ocfs2_lock_res *res = &priv->p_iter_res;
2770 ocfs2_remove_lockres_tracking(res);
2771 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2772 return seq_release_private(inode, file);
2775 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2778 struct ocfs2_dlm_seq_priv *priv;
2779 struct seq_file *seq;
2780 struct ocfs2_super *osb;
2782 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2788 osb = inode->i_private;
2789 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2790 priv->p_dlm_debug = osb->osb_dlm_debug;
2791 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2793 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2800 seq = (struct seq_file *) file->private_data;
2801 seq->private = priv;
2803 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2810 static const struct file_operations ocfs2_dlm_debug_fops = {
2811 .open = ocfs2_dlm_debug_open,
2812 .release = ocfs2_dlm_debug_release,
2814 .llseek = seq_lseek,
2817 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2820 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2822 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2824 osb->osb_debug_root,
2826 &ocfs2_dlm_debug_fops);
2827 if (!dlm_debug->d_locking_state) {
2830 "Unable to create locking state debugfs file.\n");
2834 ocfs2_get_dlm_debug(dlm_debug);
2839 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2841 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2844 debugfs_remove(dlm_debug->d_locking_state);
2845 ocfs2_put_dlm_debug(dlm_debug);
2849 int ocfs2_dlm_init(struct ocfs2_super *osb)
2852 struct ocfs2_cluster_connection *conn = NULL;
2856 if (ocfs2_mount_local(osb)) {
2861 status = ocfs2_dlm_init_debug(osb);
2867 /* launch downconvert thread */
2868 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2869 if (IS_ERR(osb->dc_task)) {
2870 status = PTR_ERR(osb->dc_task);
2871 osb->dc_task = NULL;
2876 /* for now, uuid == domain */
2877 status = ocfs2_cluster_connect(osb->osb_cluster_stack,
2879 strlen(osb->uuid_str),
2880 ocfs2_do_node_down, osb,
2887 status = ocfs2_cluster_this_node(&osb->node_num);
2891 "could not find this host's node number\n");
2892 ocfs2_cluster_disconnect(conn, 0);
2897 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2898 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2899 ocfs2_nfs_sync_lock_res_init(&osb->osb_nfs_sync_lockres, osb);
2900 ocfs2_orphan_scan_lock_res_init(&osb->osb_orphan_scan.os_lockres, osb);
2907 ocfs2_dlm_shutdown_debug(osb);
2909 kthread_stop(osb->dc_task);
2916 void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
2921 ocfs2_drop_osb_locks(osb);
2924 * Now that we have dropped all locks and ocfs2_dismount_volume()
2925 * has disabled recovery, the DLM won't be talking to us. It's
2926 * safe to tear things down before disconnecting the cluster.
2930 kthread_stop(osb->dc_task);
2931 osb->dc_task = NULL;
2934 ocfs2_lock_res_free(&osb->osb_super_lockres);
2935 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2936 ocfs2_lock_res_free(&osb->osb_nfs_sync_lockres);
2937 ocfs2_lock_res_free(&osb->osb_orphan_scan.os_lockres);
2939 ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
2942 ocfs2_dlm_shutdown_debug(osb);
2947 static void ocfs2_unlock_ast(void *opaque, int error)
2949 struct ocfs2_lock_res *lockres = opaque;
2950 unsigned long flags;
2954 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2955 lockres->l_unlock_action);
2957 spin_lock_irqsave(&lockres->l_lock, flags);
2959 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
2960 "unlock_action %d\n", error, lockres->l_name,
2961 lockres->l_unlock_action);
2962 spin_unlock_irqrestore(&lockres->l_lock, flags);
2966 switch(lockres->l_unlock_action) {
2967 case OCFS2_UNLOCK_CANCEL_CONVERT:
2968 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2969 lockres->l_action = OCFS2_AST_INVALID;
2970 /* Downconvert thread may have requeued this lock, we
2971 * need to wake it. */
2972 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2973 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres));
2975 case OCFS2_UNLOCK_DROP_LOCK:
2976 lockres->l_level = DLM_LOCK_IV;
2982 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2983 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2984 wake_up(&lockres->l_event);
2985 spin_unlock_irqrestore(&lockres->l_lock, flags);
2990 static int ocfs2_drop_lock(struct ocfs2_super *osb,
2991 struct ocfs2_lock_res *lockres)
2994 unsigned long flags;
2997 /* We didn't get anywhere near actually using this lockres. */
2998 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
3001 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
3002 lkm_flags |= DLM_LKF_VALBLK;
3004 spin_lock_irqsave(&lockres->l_lock, flags);
3006 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
3007 "lockres %s, flags 0x%lx\n",
3008 lockres->l_name, lockres->l_flags);
3010 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
3011 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
3012 "%u, unlock_action = %u\n",
3013 lockres->l_name, lockres->l_flags, lockres->l_action,
3014 lockres->l_unlock_action);
3016 spin_unlock_irqrestore(&lockres->l_lock, flags);
3018 /* XXX: Today we just wait on any busy
3019 * locks... Perhaps we need to cancel converts in the
3021 ocfs2_wait_on_busy_lock(lockres);
3023 spin_lock_irqsave(&lockres->l_lock, flags);
3026 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3027 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
3028 lockres->l_level == DLM_LOCK_EX &&
3029 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3030 lockres->l_ops->set_lvb(lockres);
3033 if (lockres->l_flags & OCFS2_LOCK_BUSY)
3034 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
3036 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
3037 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
3039 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
3040 spin_unlock_irqrestore(&lockres->l_lock, flags);
3044 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
3046 /* make sure we never get here while waiting for an ast to
3048 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
3050 /* is this necessary? */
3051 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3052 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
3053 spin_unlock_irqrestore(&lockres->l_lock, flags);
3055 mlog(0, "lock %s\n", lockres->l_name);
3057 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags,
3060 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
3061 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
3062 ocfs2_dlm_dump_lksb(&lockres->l_lksb);
3065 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
3068 ocfs2_wait_on_busy_lock(lockres);
3074 /* Mark the lockres as being dropped. It will no longer be
3075 * queued if blocking, but we still may have to wait on it
3076 * being dequeued from the downconvert thread before we can consider
3079 * You can *not* attempt to call cluster_lock on this lockres anymore. */
3080 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
3083 struct ocfs2_mask_waiter mw;
3084 unsigned long flags;
3086 ocfs2_init_mask_waiter(&mw);
3088 spin_lock_irqsave(&lockres->l_lock, flags);
3089 lockres->l_flags |= OCFS2_LOCK_FREEING;
3090 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
3091 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
3092 spin_unlock_irqrestore(&lockres->l_lock, flags);
3094 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
3096 status = ocfs2_wait_for_mask(&mw);
3100 spin_lock_irqsave(&lockres->l_lock, flags);
3102 spin_unlock_irqrestore(&lockres->l_lock, flags);
3105 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
3106 struct ocfs2_lock_res *lockres)
3110 ocfs2_mark_lockres_freeing(lockres);
3111 ret = ocfs2_drop_lock(osb, lockres);
3116 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
3118 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
3119 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
3120 ocfs2_simple_drop_lockres(osb, &osb->osb_nfs_sync_lockres);
3121 ocfs2_simple_drop_lockres(osb, &osb->osb_orphan_scan.os_lockres);
3124 int ocfs2_drop_inode_locks(struct inode *inode)
3130 /* No need to call ocfs2_mark_lockres_freeing here -
3131 * ocfs2_clear_inode has done it for us. */
3133 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
3134 &OCFS2_I(inode)->ip_open_lockres);
3140 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
3141 &OCFS2_I(inode)->ip_inode_lockres);
3144 if (err < 0 && !status)
3147 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
3148 &OCFS2_I(inode)->ip_rw_lockres);
3151 if (err < 0 && !status)
3158 static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
3161 assert_spin_locked(&lockres->l_lock);
3163 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
3165 if (lockres->l_level <= new_level) {
3166 mlog(ML_ERROR, "lockres->l_level (%d) <= new_level (%d)\n",
3167 lockres->l_level, new_level);
3171 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
3172 lockres->l_name, new_level, lockres->l_blocking);
3174 lockres->l_action = OCFS2_AST_DOWNCONVERT;
3175 lockres->l_requested = new_level;
3176 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3177 return lockres_set_pending(lockres);
3180 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
3181 struct ocfs2_lock_res *lockres,
3184 unsigned int generation)
3187 u32 dlm_flags = DLM_LKF_CONVERT;
3192 dlm_flags |= DLM_LKF_VALBLK;
3194 ret = ocfs2_dlm_lock(osb->cconn,
3199 OCFS2_LOCK_ID_MAX_LEN - 1,
3201 lockres_clear_pending(lockres, generation, osb);
3203 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
3204 ocfs2_recover_from_dlm_error(lockres, 1);
3214 /* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
3215 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
3216 struct ocfs2_lock_res *lockres)
3218 assert_spin_locked(&lockres->l_lock);
3221 mlog(0, "lock %s\n", lockres->l_name);
3223 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
3224 /* If we're already trying to cancel a lock conversion
3225 * then just drop the spinlock and allow the caller to
3226 * requeue this lock. */
3228 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
3232 /* were we in a convert when we got the bast fire? */
3233 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
3234 lockres->l_action != OCFS2_AST_DOWNCONVERT);
3235 /* set things up for the unlockast to know to just
3236 * clear out the ast_action and unset busy, etc. */
3237 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
3239 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
3240 "lock %s, invalid flags: 0x%lx\n",
3241 lockres->l_name, lockres->l_flags);
3246 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
3247 struct ocfs2_lock_res *lockres)
3252 mlog(0, "lock %s\n", lockres->l_name);
3254 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
3255 DLM_LKF_CANCEL, lockres);
3257 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
3258 ocfs2_recover_from_dlm_error(lockres, 0);
3261 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres->l_name);
3267 static int ocfs2_unblock_lock(struct ocfs2_super *osb,
3268 struct ocfs2_lock_res *lockres,
3269 struct ocfs2_unblock_ctl *ctl)
3271 unsigned long flags;
3280 spin_lock_irqsave(&lockres->l_lock, flags);
3282 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
3285 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
3287 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3288 * exists entirely for one reason - another thread has set
3289 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3291 * If we do ocfs2_cancel_convert() before the other thread
3292 * calls dlm_lock(), our cancel will do nothing. We will
3293 * get no ast, and we will have no way of knowing the
3294 * cancel failed. Meanwhile, the other thread will call
3295 * into dlm_lock() and wait...forever.
3297 * Why forever? Because another node has asked for the
3298 * lock first; that's why we're here in unblock_lock().
3300 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3301 * set, we just requeue the unblock. Only when the other
3302 * thread has called dlm_lock() and cleared PENDING will
3303 * we then cancel their request.
3305 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3306 * at the same time they set OCFS2_DLM_BUSY. They must
3307 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3309 if (lockres->l_flags & OCFS2_LOCK_PENDING)
3313 ret = ocfs2_prepare_cancel_convert(osb, lockres);
3314 spin_unlock_irqrestore(&lockres->l_lock, flags);
3316 ret = ocfs2_cancel_convert(osb, lockres);
3323 /* if we're blocking an exclusive and we have *any* holders,
3325 if ((lockres->l_blocking == DLM_LOCK_EX)
3326 && (lockres->l_ex_holders || lockres->l_ro_holders))
3329 /* If it's a PR we're blocking, then only
3330 * requeue if we've got any EX holders */
3331 if (lockres->l_blocking == DLM_LOCK_PR &&
3332 lockres->l_ex_holders)
3336 * Can we get a lock in this state if the holder counts are
3337 * zero? The meta data unblock code used to check this.
3339 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
3340 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
3343 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
3345 if (lockres->l_ops->check_downconvert
3346 && !lockres->l_ops->check_downconvert(lockres, new_level))
3349 /* If we get here, then we know that there are no more
3350 * incompatible holders (and anyone asking for an incompatible
3351 * lock is blocked). We can now downconvert the lock */
3352 if (!lockres->l_ops->downconvert_worker)
3355 /* Some lockres types want to do a bit of work before
3356 * downconverting a lock. Allow that here. The worker function
3357 * may sleep, so we save off a copy of what we're blocking as
3358 * it may change while we're not holding the spin lock. */
3359 blocking = lockres->l_blocking;
3360 spin_unlock_irqrestore(&lockres->l_lock, flags);
3362 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
3364 if (ctl->unblock_action == UNBLOCK_STOP_POST)
3367 spin_lock_irqsave(&lockres->l_lock, flags);
3368 if (blocking != lockres->l_blocking) {
3369 /* If this changed underneath us, then we can't drop
3377 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3378 if (lockres->l_level == DLM_LOCK_EX)
3382 * We only set the lvb if the lock has been fully
3383 * refreshed - otherwise we risk setting stale
3384 * data. Otherwise, there's no need to actually clear
3385 * out the lvb here as it's value is still valid.
3387 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3388 lockres->l_ops->set_lvb(lockres);
3391 gen = ocfs2_prepare_downconvert(lockres, new_level);
3392 spin_unlock_irqrestore(&lockres->l_lock, flags);
3393 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
3401 spin_unlock_irqrestore(&lockres->l_lock, flags);
3408 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3411 struct inode *inode;
3412 struct address_space *mapping;
3414 inode = ocfs2_lock_res_inode(lockres);
3415 mapping = inode->i_mapping;
3417 if (!S_ISREG(inode->i_mode))
3421 * We need this before the filemap_fdatawrite() so that it can
3422 * transfer the dirty bit from the PTE to the
3423 * page. Unfortunately this means that even for EX->PR
3424 * downconverts, we'll lose our mappings and have to build
3427 unmap_mapping_range(mapping, 0, 0, 0);
3429 if (filemap_fdatawrite(mapping)) {
3430 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3431 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3433 sync_mapping_buffers(mapping);
3434 if (blocking == DLM_LOCK_EX) {
3435 truncate_inode_pages(mapping, 0);
3437 /* We only need to wait on the I/O if we're not also
3438 * truncating pages because truncate_inode_pages waits
3439 * for us above. We don't truncate pages if we're
3440 * blocking anything < EXMODE because we want to keep
3441 * them around in that case. */
3442 filemap_fdatawait(mapping);
3446 return UNBLOCK_CONTINUE;
3449 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3452 struct inode *inode = ocfs2_lock_res_inode(lockres);
3453 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
3455 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3456 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
3461 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
3465 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3467 struct inode *inode = ocfs2_lock_res_inode(lockres);
3469 __ocfs2_stuff_meta_lvb(inode);
3473 * Does the final reference drop on our dentry lock. Right now this
3474 * happens in the downconvert thread, but we could choose to simplify the
3475 * dlmglue API and push these off to the ocfs2_wq in the future.
3477 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3478 struct ocfs2_lock_res *lockres)
3480 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3481 ocfs2_dentry_lock_put(osb, dl);
3485 * d_delete() matching dentries before the lock downconvert.
3487 * At this point, any process waiting to destroy the
3488 * dentry_lock due to last ref count is stopped by the
3489 * OCFS2_LOCK_QUEUED flag.
3491 * We have two potential problems
3493 * 1) If we do the last reference drop on our dentry_lock (via dput)
3494 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3495 * the downconvert to finish. Instead we take an elevated
3496 * reference and push the drop until after we've completed our
3497 * unblock processing.
3499 * 2) There might be another process with a final reference,
3500 * waiting on us to finish processing. If this is the case, we
3501 * detect it and exit out - there's no more dentries anyway.
3503 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3506 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3507 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3508 struct dentry *dentry;
3509 unsigned long flags;
3513 * This node is blocking another node from getting a read
3514 * lock. This happens when we've renamed within a
3515 * directory. We've forced the other nodes to d_delete(), but
3516 * we never actually dropped our lock because it's still
3517 * valid. The downconvert code will retain a PR for this node,
3518 * so there's no further work to do.
3520 if (blocking == DLM_LOCK_PR)
3521 return UNBLOCK_CONTINUE;
3524 * Mark this inode as potentially orphaned. The code in
3525 * ocfs2_delete_inode() will figure out whether it actually
3526 * needs to be freed or not.
3528 spin_lock(&oi->ip_lock);
3529 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3530 spin_unlock(&oi->ip_lock);
3533 * Yuck. We need to make sure however that the check of
3534 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3535 * respect to a reference decrement or the setting of that
3538 spin_lock_irqsave(&lockres->l_lock, flags);
3539 spin_lock(&dentry_attach_lock);
3540 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3545 spin_unlock(&dentry_attach_lock);
3546 spin_unlock_irqrestore(&lockres->l_lock, flags);
3548 mlog(0, "extra_ref = %d\n", extra_ref);
3551 * We have a process waiting on us in ocfs2_dentry_iput(),
3552 * which means we can't have any more outstanding
3553 * aliases. There's no need to do any more work.
3556 return UNBLOCK_CONTINUE;
3558 spin_lock(&dentry_attach_lock);
3560 dentry = ocfs2_find_local_alias(dl->dl_inode,
3561 dl->dl_parent_blkno, 1);
3564 spin_unlock(&dentry_attach_lock);
3566 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3567 dentry->d_name.name);
3570 * The following dcache calls may do an
3571 * iput(). Normally we don't want that from the
3572 * downconverting thread, but in this case it's ok
3573 * because the requesting node already has an
3574 * exclusive lock on the inode, so it can't be queued
3575 * for a downconvert.
3580 spin_lock(&dentry_attach_lock);
3582 spin_unlock(&dentry_attach_lock);
3585 * If we are the last holder of this dentry lock, there is no
3586 * reason to downconvert so skip straight to the unlock.
3588 if (dl->dl_count == 1)
3589 return UNBLOCK_STOP_POST;
3591 return UNBLOCK_CONTINUE_POST;
3594 static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres)
3596 struct ocfs2_qinfo_lvb *lvb;
3597 struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres);
3598 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3599 oinfo->dqi_gi.dqi_type);
3603 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
3604 lvb->lvb_version = OCFS2_QINFO_LVB_VERSION;
3605 lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace);
3606 lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace);
3607 lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms);
3608 lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks);
3609 lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk);
3610 lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry);
3615 void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3617 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3618 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3619 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3622 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
3623 ocfs2_cluster_unlock(osb, lockres, level);
3627 static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo)
3629 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3630 oinfo->dqi_gi.dqi_type);
3631 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3632 struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
3633 struct buffer_head *bh = NULL;
3634 struct ocfs2_global_disk_dqinfo *gdinfo;
3637 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
3638 lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) {
3639 info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace);
3640 info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace);
3641 oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms);
3642 oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks);
3643 oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk);
3644 oinfo->dqi_gi.dqi_free_entry =
3645 be32_to_cpu(lvb->lvb_free_entry);
3647 status = ocfs2_read_quota_block(oinfo->dqi_gqinode, 0, &bh);
3652 gdinfo = (struct ocfs2_global_disk_dqinfo *)
3653 (bh->b_data + OCFS2_GLOBAL_INFO_OFF);
3654 info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace);
3655 info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace);
3656 oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms);
3657 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks);
3658 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk);
3659 oinfo->dqi_gi.dqi_free_entry =
3660 le32_to_cpu(gdinfo->dqi_free_entry);
3662 ocfs2_track_lock_refresh(lockres);
3669 /* Lock quota info, this function expects at least shared lock on the quota file
3670 * so that we can safely refresh quota info from disk. */
3671 int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3673 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3674 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3675 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3680 /* On RO devices, locking really isn't needed... */
3681 if (ocfs2_is_hard_readonly(osb)) {
3686 if (ocfs2_mount_local(osb))
3689 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
3694 if (!ocfs2_should_refresh_lock_res(lockres))
3696 /* OK, we have the lock but we need to refresh the quota info */
3697 status = ocfs2_refresh_qinfo(oinfo);
3699 ocfs2_qinfo_unlock(oinfo, ex);
3700 ocfs2_complete_lock_res_refresh(lockres, status);
3707 * This is the filesystem locking protocol. It provides the lock handling
3708 * hooks for the underlying DLM. It has a maximum version number.
3709 * The version number allows interoperability with systems running at
3710 * the same major number and an equal or smaller minor number.
3712 * Whenever the filesystem does new things with locks (adds or removes a
3713 * lock, orders them differently, does different things underneath a lock),
3714 * the version must be changed. The protocol is negotiated when joining
3715 * the dlm domain. A node may join the domain if its major version is
3716 * identical to all other nodes and its minor version is greater than
3717 * or equal to all other nodes. When its minor version is greater than
3718 * the other nodes, it will run at the minor version specified by the
3721 * If a locking change is made that will not be compatible with older
3722 * versions, the major number must be increased and the minor version set
3723 * to zero. If a change merely adds a behavior that can be disabled when
3724 * speaking to older versions, the minor version must be increased. If a
3725 * change adds a fully backwards compatible change (eg, LVB changes that
3726 * are just ignored by older versions), the version does not need to be
3729 static struct ocfs2_locking_protocol lproto = {
3731 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
3732 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
3734 .lp_lock_ast = ocfs2_locking_ast,
3735 .lp_blocking_ast = ocfs2_blocking_ast,
3736 .lp_unlock_ast = ocfs2_unlock_ast,
3739 void ocfs2_set_locking_protocol(void)
3741 ocfs2_stack_glue_set_locking_protocol(&lproto);
3745 static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3746 struct ocfs2_lock_res *lockres)
3749 struct ocfs2_unblock_ctl ctl = {0, 0,};
3750 unsigned long flags;
3752 /* Our reference to the lockres in this function can be
3753 * considered valid until we remove the OCFS2_LOCK_QUEUED
3759 BUG_ON(!lockres->l_ops);
3761 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3763 /* Detect whether a lock has been marked as going away while
3764 * the downconvert thread was processing other things. A lock can
3765 * still be marked with OCFS2_LOCK_FREEING after this check,
3766 * but short circuiting here will still save us some
3768 spin_lock_irqsave(&lockres->l_lock, flags);
3769 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3771 spin_unlock_irqrestore(&lockres->l_lock, flags);
3773 status = ocfs2_unblock_lock(osb, lockres, &ctl);
3777 spin_lock_irqsave(&lockres->l_lock, flags);
3779 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
3780 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3782 ocfs2_schedule_blocked_lock(osb, lockres);
3784 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
3785 ctl.requeue ? "yes" : "no");
3786 spin_unlock_irqrestore(&lockres->l_lock, flags);
3788 if (ctl.unblock_action != UNBLOCK_CONTINUE
3789 && lockres->l_ops->post_unlock)
3790 lockres->l_ops->post_unlock(osb, lockres);
3795 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3796 struct ocfs2_lock_res *lockres)
3800 assert_spin_locked(&lockres->l_lock);
3802 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3803 /* Do not schedule a lock for downconvert when it's on
3804 * the way to destruction - any nodes wanting access
3805 * to the resource will get it soon. */
3806 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3807 lockres->l_name, lockres->l_flags);
3811 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3813 spin_lock(&osb->dc_task_lock);
3814 if (list_empty(&lockres->l_blocked_list)) {
3815 list_add_tail(&lockres->l_blocked_list,
3816 &osb->blocked_lock_list);
3817 osb->blocked_lock_count++;
3819 spin_unlock(&osb->dc_task_lock);
3824 static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3826 unsigned long processed;
3827 struct ocfs2_lock_res *lockres;
3831 spin_lock(&osb->dc_task_lock);
3832 /* grab this early so we know to try again if a state change and
3833 * wake happens part-way through our work */
3834 osb->dc_work_sequence = osb->dc_wake_sequence;
3836 processed = osb->blocked_lock_count;
3838 BUG_ON(list_empty(&osb->blocked_lock_list));
3840 lockres = list_entry(osb->blocked_lock_list.next,
3841 struct ocfs2_lock_res, l_blocked_list);
3842 list_del_init(&lockres->l_blocked_list);
3843 osb->blocked_lock_count--;
3844 spin_unlock(&osb->dc_task_lock);
3849 ocfs2_process_blocked_lock(osb, lockres);
3851 spin_lock(&osb->dc_task_lock);
3853 spin_unlock(&osb->dc_task_lock);
3858 static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3862 spin_lock(&osb->dc_task_lock);
3863 if (list_empty(&osb->blocked_lock_list))
3866 spin_unlock(&osb->dc_task_lock);
3870 static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
3872 int should_wake = 0;
3874 spin_lock(&osb->dc_task_lock);
3875 if (osb->dc_work_sequence != osb->dc_wake_sequence)
3877 spin_unlock(&osb->dc_task_lock);
3882 static int ocfs2_downconvert_thread(void *arg)
3885 struct ocfs2_super *osb = arg;
3887 /* only quit once we've been asked to stop and there is no more
3889 while (!(kthread_should_stop() &&
3890 ocfs2_downconvert_thread_lists_empty(osb))) {
3892 wait_event_interruptible(osb->dc_event,
3893 ocfs2_downconvert_thread_should_wake(osb) ||
3894 kthread_should_stop());
3896 mlog(0, "downconvert_thread: awoken\n");
3898 ocfs2_downconvert_thread_do_work(osb);
3901 osb->dc_task = NULL;
3905 void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
3907 spin_lock(&osb->dc_task_lock);
3908 /* make sure the voting thread gets a swipe at whatever changes
3909 * the caller may have made to the voting state */
3910 osb->dc_wake_sequence++;
3911 spin_unlock(&osb->dc_task_lock);
3912 wake_up(&osb->dc_event);