1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Code which implements an OCFS2 specific interface to our DLM.
7 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
10 #include <linux/types.h>
11 #include <linux/slab.h>
12 #include <linux/highmem.h>
14 #include <linux/kthread.h>
15 #include <linux/pagemap.h>
16 #include <linux/debugfs.h>
17 #include <linux/seq_file.h>
18 #include <linux/time.h>
19 #include <linux/quotaops.h>
20 #include <linux/sched/signal.h>
22 #define MLOG_MASK_PREFIX ML_DLM_GLUE
23 #include <cluster/masklog.h>
26 #include "ocfs2_lockingver.h"
31 #include "extent_map.h"
33 #include "heartbeat.h"
36 #include "stackglue.h"
41 #include "refcounttree.h"
44 #include "buffer_head_io.h"
46 struct ocfs2_mask_waiter
{
47 struct list_head mw_item
;
49 struct completion mw_complete
;
50 unsigned long mw_mask
;
51 unsigned long mw_goal
;
52 #ifdef CONFIG_OCFS2_FS_STATS
53 ktime_t mw_lock_start
;
57 static struct ocfs2_super
*ocfs2_get_dentry_osb(struct ocfs2_lock_res
*lockres
);
58 static struct ocfs2_super
*ocfs2_get_inode_osb(struct ocfs2_lock_res
*lockres
);
59 static struct ocfs2_super
*ocfs2_get_file_osb(struct ocfs2_lock_res
*lockres
);
60 static struct ocfs2_super
*ocfs2_get_qinfo_osb(struct ocfs2_lock_res
*lockres
);
63 * Return value from ->downconvert_worker functions.
65 * These control the precise actions of ocfs2_unblock_lock()
66 * and ocfs2_process_blocked_lock()
69 enum ocfs2_unblock_action
{
70 UNBLOCK_CONTINUE
= 0, /* Continue downconvert */
71 UNBLOCK_CONTINUE_POST
= 1, /* Continue downconvert, fire
72 * ->post_unlock callback */
73 UNBLOCK_STOP_POST
= 2, /* Do not downconvert, fire
74 * ->post_unlock() callback. */
77 struct ocfs2_unblock_ctl
{
79 enum ocfs2_unblock_action unblock_action
;
82 /* Lockdep class keys */
83 #ifdef CONFIG_DEBUG_LOCK_ALLOC
84 static struct lock_class_key lockdep_keys
[OCFS2_NUM_LOCK_TYPES
];
87 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res
*lockres
,
89 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res
*lockres
);
91 static int ocfs2_data_convert_worker(struct ocfs2_lock_res
*lockres
,
94 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res
*lockres
,
97 static void ocfs2_dentry_post_unlock(struct ocfs2_super
*osb
,
98 struct ocfs2_lock_res
*lockres
);
100 static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res
*lockres
);
102 static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res
*lockres
,
104 static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res
*lockres
,
107 #define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres)
109 /* This aids in debugging situations where a bad LVB might be involved. */
110 static void ocfs2_dump_meta_lvb_info(u64 level
,
111 const char *function
,
113 struct ocfs2_lock_res
*lockres
)
115 struct ocfs2_meta_lvb
*lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
117 mlog(level
, "LVB information for %s (called from %s:%u):\n",
118 lockres
->l_name
, function
, line
);
119 mlog(level
, "version: %u, clusters: %u, generation: 0x%x\n",
120 lvb
->lvb_version
, be32_to_cpu(lvb
->lvb_iclusters
),
121 be32_to_cpu(lvb
->lvb_igeneration
));
122 mlog(level
, "size: %llu, uid %u, gid %u, mode 0x%x\n",
123 (unsigned long long)be64_to_cpu(lvb
->lvb_isize
),
124 be32_to_cpu(lvb
->lvb_iuid
), be32_to_cpu(lvb
->lvb_igid
),
125 be16_to_cpu(lvb
->lvb_imode
));
126 mlog(level
, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
127 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb
->lvb_inlink
),
128 (long long)be64_to_cpu(lvb
->lvb_iatime_packed
),
129 (long long)be64_to_cpu(lvb
->lvb_ictime_packed
),
130 (long long)be64_to_cpu(lvb
->lvb_imtime_packed
),
131 be32_to_cpu(lvb
->lvb_iattr
));
136 * OCFS2 Lock Resource Operations
138 * These fine tune the behavior of the generic dlmglue locking infrastructure.
140 * The most basic of lock types can point ->l_priv to their respective
141 * struct ocfs2_super and allow the default actions to manage things.
143 * Right now, each lock type also needs to implement an init function,
144 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
145 * should be called when the lock is no longer needed (i.e., object
148 struct ocfs2_lock_res_ops
{
150 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
151 * this callback if ->l_priv is not an ocfs2_super pointer
153 struct ocfs2_super
* (*get_osb
)(struct ocfs2_lock_res
*);
156 * Optionally called in the downconvert thread after a
157 * successful downconvert. The lockres will not be referenced
158 * after this callback is called, so it is safe to free
161 * The exact semantics of when this is called are controlled
162 * by ->downconvert_worker()
164 void (*post_unlock
)(struct ocfs2_super
*, struct ocfs2_lock_res
*);
167 * Allow a lock type to add checks to determine whether it is
168 * safe to downconvert a lock. Return 0 to re-queue the
169 * downconvert at a later time, nonzero to continue.
171 * For most locks, the default checks that there are no
172 * incompatible holders are sufficient.
174 * Called with the lockres spinlock held.
176 int (*check_downconvert
)(struct ocfs2_lock_res
*, int);
179 * Allows a lock type to populate the lock value block. This
180 * is called on downconvert, and when we drop a lock.
182 * Locks that want to use this should set LOCK_TYPE_USES_LVB
183 * in the flags field.
185 * Called with the lockres spinlock held.
187 void (*set_lvb
)(struct ocfs2_lock_res
*);
190 * Called from the downconvert thread when it is determined
191 * that a lock will be downconverted. This is called without
192 * any locks held so the function can do work that might
193 * schedule (syncing out data, etc).
195 * This should return any one of the ocfs2_unblock_action
196 * values, depending on what it wants the thread to do.
198 int (*downconvert_worker
)(struct ocfs2_lock_res
*, int);
201 * LOCK_TYPE_* flags which describe the specific requirements
202 * of a lock type. Descriptions of each individual flag follow.
208 * Some locks want to "refresh" potentially stale data when a
209 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
210 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
211 * individual lockres l_flags member from the ast function. It is
212 * expected that the locking wrapper will clear the
213 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
215 #define LOCK_TYPE_REQUIRES_REFRESH 0x1
218 * Indicate that a lock type makes use of the lock value block. The
219 * ->set_lvb lock type callback must be defined.
221 #define LOCK_TYPE_USES_LVB 0x2
223 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops
= {
224 .get_osb
= ocfs2_get_inode_osb
,
228 static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops
= {
229 .get_osb
= ocfs2_get_inode_osb
,
230 .check_downconvert
= ocfs2_check_meta_downconvert
,
231 .set_lvb
= ocfs2_set_meta_lvb
,
232 .downconvert_worker
= ocfs2_data_convert_worker
,
233 .flags
= LOCK_TYPE_REQUIRES_REFRESH
|LOCK_TYPE_USES_LVB
,
236 static struct ocfs2_lock_res_ops ocfs2_super_lops
= {
237 .flags
= LOCK_TYPE_REQUIRES_REFRESH
,
240 static struct ocfs2_lock_res_ops ocfs2_rename_lops
= {
244 static struct ocfs2_lock_res_ops ocfs2_nfs_sync_lops
= {
248 static struct ocfs2_lock_res_ops ocfs2_trim_fs_lops
= {
249 .flags
= LOCK_TYPE_REQUIRES_REFRESH
|LOCK_TYPE_USES_LVB
,
252 static struct ocfs2_lock_res_ops ocfs2_orphan_scan_lops
= {
253 .flags
= LOCK_TYPE_REQUIRES_REFRESH
|LOCK_TYPE_USES_LVB
,
256 static struct ocfs2_lock_res_ops ocfs2_dentry_lops
= {
257 .get_osb
= ocfs2_get_dentry_osb
,
258 .post_unlock
= ocfs2_dentry_post_unlock
,
259 .downconvert_worker
= ocfs2_dentry_convert_worker
,
263 static struct ocfs2_lock_res_ops ocfs2_inode_open_lops
= {
264 .get_osb
= ocfs2_get_inode_osb
,
268 static struct ocfs2_lock_res_ops ocfs2_flock_lops
= {
269 .get_osb
= ocfs2_get_file_osb
,
273 static struct ocfs2_lock_res_ops ocfs2_qinfo_lops
= {
274 .set_lvb
= ocfs2_set_qinfo_lvb
,
275 .get_osb
= ocfs2_get_qinfo_osb
,
276 .flags
= LOCK_TYPE_REQUIRES_REFRESH
| LOCK_TYPE_USES_LVB
,
279 static struct ocfs2_lock_res_ops ocfs2_refcount_block_lops
= {
280 .check_downconvert
= ocfs2_check_refcount_downconvert
,
281 .downconvert_worker
= ocfs2_refcount_convert_worker
,
285 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res
*lockres
)
287 return lockres
->l_type
== OCFS2_LOCK_TYPE_META
||
288 lockres
->l_type
== OCFS2_LOCK_TYPE_RW
||
289 lockres
->l_type
== OCFS2_LOCK_TYPE_OPEN
;
292 static inline struct ocfs2_lock_res
*ocfs2_lksb_to_lock_res(struct ocfs2_dlm_lksb
*lksb
)
294 return container_of(lksb
, struct ocfs2_lock_res
, l_lksb
);
297 static inline struct inode
*ocfs2_lock_res_inode(struct ocfs2_lock_res
*lockres
)
299 BUG_ON(!ocfs2_is_inode_lock(lockres
));
301 return (struct inode
*) lockres
->l_priv
;
304 static inline struct ocfs2_dentry_lock
*ocfs2_lock_res_dl(struct ocfs2_lock_res
*lockres
)
306 BUG_ON(lockres
->l_type
!= OCFS2_LOCK_TYPE_DENTRY
);
308 return (struct ocfs2_dentry_lock
*)lockres
->l_priv
;
311 static inline struct ocfs2_mem_dqinfo
*ocfs2_lock_res_qinfo(struct ocfs2_lock_res
*lockres
)
313 BUG_ON(lockres
->l_type
!= OCFS2_LOCK_TYPE_QINFO
);
315 return (struct ocfs2_mem_dqinfo
*)lockres
->l_priv
;
318 static inline struct ocfs2_refcount_tree
*
319 ocfs2_lock_res_refcount_tree(struct ocfs2_lock_res
*res
)
321 return container_of(res
, struct ocfs2_refcount_tree
, rf_lockres
);
324 static inline struct ocfs2_super
*ocfs2_get_lockres_osb(struct ocfs2_lock_res
*lockres
)
326 if (lockres
->l_ops
->get_osb
)
327 return lockres
->l_ops
->get_osb(lockres
);
329 return (struct ocfs2_super
*)lockres
->l_priv
;
332 static int ocfs2_lock_create(struct ocfs2_super
*osb
,
333 struct ocfs2_lock_res
*lockres
,
336 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res
*lockres
,
338 static void __ocfs2_cluster_unlock(struct ocfs2_super
*osb
,
339 struct ocfs2_lock_res
*lockres
,
340 int level
, unsigned long caller_ip
);
341 static inline void ocfs2_cluster_unlock(struct ocfs2_super
*osb
,
342 struct ocfs2_lock_res
*lockres
,
345 __ocfs2_cluster_unlock(osb
, lockres
, level
, _RET_IP_
);
348 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res
*lockres
);
349 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res
*lockres
);
350 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res
*lockres
);
351 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res
*lockres
, int level
);
352 static void ocfs2_schedule_blocked_lock(struct ocfs2_super
*osb
,
353 struct ocfs2_lock_res
*lockres
);
354 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res
*lockres
,
356 #define ocfs2_log_dlm_error(_func, _err, _lockres) do { \
357 if ((_lockres)->l_type != OCFS2_LOCK_TYPE_DENTRY) \
358 mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \
359 _err, _func, _lockres->l_name); \
361 mlog(ML_ERROR, "DLM error %d while calling %s on resource %.*s%08x\n", \
362 _err, _func, OCFS2_DENTRY_LOCK_INO_START - 1, (_lockres)->l_name, \
363 (unsigned int)ocfs2_get_dentry_lock_ino(_lockres)); \
365 static int ocfs2_downconvert_thread(void *arg
);
366 static void ocfs2_downconvert_on_unlock(struct ocfs2_super
*osb
,
367 struct ocfs2_lock_res
*lockres
);
368 static int ocfs2_inode_lock_update(struct inode
*inode
,
369 struct buffer_head
**bh
);
370 static void ocfs2_drop_osb_locks(struct ocfs2_super
*osb
);
371 static inline int ocfs2_highest_compat_lock_level(int level
);
372 static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res
*lockres
,
374 static int ocfs2_downconvert_lock(struct ocfs2_super
*osb
,
375 struct ocfs2_lock_res
*lockres
,
378 unsigned int generation
);
379 static int ocfs2_prepare_cancel_convert(struct ocfs2_super
*osb
,
380 struct ocfs2_lock_res
*lockres
);
381 static int ocfs2_cancel_convert(struct ocfs2_super
*osb
,
382 struct ocfs2_lock_res
*lockres
);
385 static void ocfs2_build_lock_name(enum ocfs2_lock_type type
,
392 BUG_ON(type
>= OCFS2_NUM_LOCK_TYPES
);
394 len
= snprintf(name
, OCFS2_LOCK_ID_MAX_LEN
, "%c%s%016llx%08x",
395 ocfs2_lock_type_char(type
), OCFS2_LOCK_ID_PAD
,
396 (long long)blkno
, generation
);
398 BUG_ON(len
!= (OCFS2_LOCK_ID_MAX_LEN
- 1));
400 mlog(0, "built lock resource with name: %s\n", name
);
403 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock
);
405 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res
*res
,
406 struct ocfs2_dlm_debug
*dlm_debug
)
408 mlog(0, "Add tracking for lockres %s\n", res
->l_name
);
410 spin_lock(&ocfs2_dlm_tracking_lock
);
411 list_add(&res
->l_debug_list
, &dlm_debug
->d_lockres_tracking
);
412 spin_unlock(&ocfs2_dlm_tracking_lock
);
415 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res
*res
)
417 spin_lock(&ocfs2_dlm_tracking_lock
);
418 if (!list_empty(&res
->l_debug_list
))
419 list_del_init(&res
->l_debug_list
);
420 spin_unlock(&ocfs2_dlm_tracking_lock
);
423 #ifdef CONFIG_OCFS2_FS_STATS
424 static void ocfs2_init_lock_stats(struct ocfs2_lock_res
*res
)
426 res
->l_lock_refresh
= 0;
427 res
->l_lock_wait
= 0;
428 memset(&res
->l_lock_prmode
, 0, sizeof(struct ocfs2_lock_stats
));
429 memset(&res
->l_lock_exmode
, 0, sizeof(struct ocfs2_lock_stats
));
432 static void ocfs2_update_lock_stats(struct ocfs2_lock_res
*res
, int level
,
433 struct ocfs2_mask_waiter
*mw
, int ret
)
437 struct ocfs2_lock_stats
*stats
;
439 if (level
== LKM_PRMODE
)
440 stats
= &res
->l_lock_prmode
;
441 else if (level
== LKM_EXMODE
)
442 stats
= &res
->l_lock_exmode
;
446 kt
= ktime_sub(ktime_get(), mw
->mw_lock_start
);
447 usec
= ktime_to_us(kt
);
450 stats
->ls_total
+= ktime_to_ns(kt
);
452 if (unlikely(stats
->ls_gets
== 0)) {
454 stats
->ls_total
= ktime_to_ns(kt
);
457 if (stats
->ls_max
< usec
)
458 stats
->ls_max
= usec
;
463 stats
->ls_last
= ktime_to_us(ktime_get_real());
466 static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res
*lockres
)
468 lockres
->l_lock_refresh
++;
471 static inline void ocfs2_track_lock_wait(struct ocfs2_lock_res
*lockres
)
473 struct ocfs2_mask_waiter
*mw
;
475 if (list_empty(&lockres
->l_mask_waiters
)) {
476 lockres
->l_lock_wait
= 0;
480 mw
= list_first_entry(&lockres
->l_mask_waiters
,
481 struct ocfs2_mask_waiter
, mw_item
);
482 lockres
->l_lock_wait
=
483 ktime_to_us(ktime_mono_to_real(mw
->mw_lock_start
));
486 static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter
*mw
)
488 mw
->mw_lock_start
= ktime_get();
491 static inline void ocfs2_init_lock_stats(struct ocfs2_lock_res
*res
)
494 static inline void ocfs2_update_lock_stats(struct ocfs2_lock_res
*res
,
495 int level
, struct ocfs2_mask_waiter
*mw
, int ret
)
498 static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res
*lockres
)
501 static inline void ocfs2_track_lock_wait(struct ocfs2_lock_res
*lockres
)
504 static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter
*mw
)
509 static void ocfs2_lock_res_init_common(struct ocfs2_super
*osb
,
510 struct ocfs2_lock_res
*res
,
511 enum ocfs2_lock_type type
,
512 struct ocfs2_lock_res_ops
*ops
,
519 res
->l_level
= DLM_LOCK_IV
;
520 res
->l_requested
= DLM_LOCK_IV
;
521 res
->l_blocking
= DLM_LOCK_IV
;
522 res
->l_action
= OCFS2_AST_INVALID
;
523 res
->l_unlock_action
= OCFS2_UNLOCK_INVALID
;
525 res
->l_flags
= OCFS2_LOCK_INITIALIZED
;
527 ocfs2_add_lockres_tracking(res
, osb
->osb_dlm_debug
);
529 ocfs2_init_lock_stats(res
);
530 #ifdef CONFIG_DEBUG_LOCK_ALLOC
531 if (type
!= OCFS2_LOCK_TYPE_OPEN
)
532 lockdep_init_map(&res
->l_lockdep_map
, ocfs2_lock_type_strings
[type
],
533 &lockdep_keys
[type
], 0);
535 res
->l_lockdep_map
.key
= NULL
;
539 void ocfs2_lock_res_init_once(struct ocfs2_lock_res
*res
)
541 /* This also clears out the lock status block */
542 memset(res
, 0, sizeof(struct ocfs2_lock_res
));
543 spin_lock_init(&res
->l_lock
);
544 init_waitqueue_head(&res
->l_event
);
545 INIT_LIST_HEAD(&res
->l_blocked_list
);
546 INIT_LIST_HEAD(&res
->l_mask_waiters
);
547 INIT_LIST_HEAD(&res
->l_holders
);
550 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res
*res
,
551 enum ocfs2_lock_type type
,
552 unsigned int generation
,
555 struct ocfs2_lock_res_ops
*ops
;
558 case OCFS2_LOCK_TYPE_RW
:
559 ops
= &ocfs2_inode_rw_lops
;
561 case OCFS2_LOCK_TYPE_META
:
562 ops
= &ocfs2_inode_inode_lops
;
564 case OCFS2_LOCK_TYPE_OPEN
:
565 ops
= &ocfs2_inode_open_lops
;
568 mlog_bug_on_msg(1, "type: %d\n", type
);
569 ops
= NULL
; /* thanks, gcc */
573 ocfs2_build_lock_name(type
, OCFS2_I(inode
)->ip_blkno
,
574 generation
, res
->l_name
);
575 ocfs2_lock_res_init_common(OCFS2_SB(inode
->i_sb
), res
, type
, ops
, inode
);
578 static struct ocfs2_super
*ocfs2_get_inode_osb(struct ocfs2_lock_res
*lockres
)
580 struct inode
*inode
= ocfs2_lock_res_inode(lockres
);
582 return OCFS2_SB(inode
->i_sb
);
585 static struct ocfs2_super
*ocfs2_get_qinfo_osb(struct ocfs2_lock_res
*lockres
)
587 struct ocfs2_mem_dqinfo
*info
= lockres
->l_priv
;
589 return OCFS2_SB(info
->dqi_gi
.dqi_sb
);
592 static struct ocfs2_super
*ocfs2_get_file_osb(struct ocfs2_lock_res
*lockres
)
594 struct ocfs2_file_private
*fp
= lockres
->l_priv
;
596 return OCFS2_SB(fp
->fp_file
->f_mapping
->host
->i_sb
);
599 static __u64
ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res
*lockres
)
601 __be64 inode_blkno_be
;
603 memcpy(&inode_blkno_be
, &lockres
->l_name
[OCFS2_DENTRY_LOCK_INO_START
],
606 return be64_to_cpu(inode_blkno_be
);
609 static struct ocfs2_super
*ocfs2_get_dentry_osb(struct ocfs2_lock_res
*lockres
)
611 struct ocfs2_dentry_lock
*dl
= lockres
->l_priv
;
613 return OCFS2_SB(dl
->dl_inode
->i_sb
);
616 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock
*dl
,
617 u64 parent
, struct inode
*inode
)
620 u64 inode_blkno
= OCFS2_I(inode
)->ip_blkno
;
621 __be64 inode_blkno_be
= cpu_to_be64(inode_blkno
);
622 struct ocfs2_lock_res
*lockres
= &dl
->dl_lockres
;
624 ocfs2_lock_res_init_once(lockres
);
627 * Unfortunately, the standard lock naming scheme won't work
628 * here because we have two 16 byte values to use. Instead,
629 * we'll stuff the inode number as a binary value. We still
630 * want error prints to show something without garbling the
631 * display, so drop a null byte in there before the inode
632 * number. A future version of OCFS2 will likely use all
633 * binary lock names. The stringified names have been a
634 * tremendous aid in debugging, but now that the debugfs
635 * interface exists, we can mangle things there if need be.
637 * NOTE: We also drop the standard "pad" value (the total lock
638 * name size stays the same though - the last part is all
639 * zeros due to the memset in ocfs2_lock_res_init_once()
641 len
= snprintf(lockres
->l_name
, OCFS2_DENTRY_LOCK_INO_START
,
643 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY
),
646 BUG_ON(len
!= (OCFS2_DENTRY_LOCK_INO_START
- 1));
648 memcpy(&lockres
->l_name
[OCFS2_DENTRY_LOCK_INO_START
], &inode_blkno_be
,
651 ocfs2_lock_res_init_common(OCFS2_SB(inode
->i_sb
), lockres
,
652 OCFS2_LOCK_TYPE_DENTRY
, &ocfs2_dentry_lops
,
656 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res
*res
,
657 struct ocfs2_super
*osb
)
659 /* Superblock lockres doesn't come from a slab so we call init
660 * once on it manually. */
661 ocfs2_lock_res_init_once(res
);
662 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER
, OCFS2_SUPER_BLOCK_BLKNO
,
664 ocfs2_lock_res_init_common(osb
, res
, OCFS2_LOCK_TYPE_SUPER
,
665 &ocfs2_super_lops
, osb
);
668 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res
*res
,
669 struct ocfs2_super
*osb
)
671 /* Rename lockres doesn't come from a slab so we call init
672 * once on it manually. */
673 ocfs2_lock_res_init_once(res
);
674 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME
, 0, 0, res
->l_name
);
675 ocfs2_lock_res_init_common(osb
, res
, OCFS2_LOCK_TYPE_RENAME
,
676 &ocfs2_rename_lops
, osb
);
679 static void ocfs2_nfs_sync_lock_res_init(struct ocfs2_lock_res
*res
,
680 struct ocfs2_super
*osb
)
682 /* nfs_sync lockres doesn't come from a slab so we call init
683 * once on it manually. */
684 ocfs2_lock_res_init_once(res
);
685 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_NFS_SYNC
, 0, 0, res
->l_name
);
686 ocfs2_lock_res_init_common(osb
, res
, OCFS2_LOCK_TYPE_NFS_SYNC
,
687 &ocfs2_nfs_sync_lops
, osb
);
690 static void ocfs2_nfs_sync_lock_init(struct ocfs2_super
*osb
)
692 ocfs2_nfs_sync_lock_res_init(&osb
->osb_nfs_sync_lockres
, osb
);
693 init_rwsem(&osb
->nfs_sync_rwlock
);
696 void ocfs2_trim_fs_lock_res_init(struct ocfs2_super
*osb
)
698 struct ocfs2_lock_res
*lockres
= &osb
->osb_trim_fs_lockres
;
700 /* Only one trimfs thread are allowed to work at the same time. */
701 mutex_lock(&osb
->obs_trim_fs_mutex
);
703 ocfs2_lock_res_init_once(lockres
);
704 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_TRIM_FS
, 0, 0, lockres
->l_name
);
705 ocfs2_lock_res_init_common(osb
, lockres
, OCFS2_LOCK_TYPE_TRIM_FS
,
706 &ocfs2_trim_fs_lops
, osb
);
709 void ocfs2_trim_fs_lock_res_uninit(struct ocfs2_super
*osb
)
711 struct ocfs2_lock_res
*lockres
= &osb
->osb_trim_fs_lockres
;
713 ocfs2_simple_drop_lockres(osb
, lockres
);
714 ocfs2_lock_res_free(lockres
);
716 mutex_unlock(&osb
->obs_trim_fs_mutex
);
719 static void ocfs2_orphan_scan_lock_res_init(struct ocfs2_lock_res
*res
,
720 struct ocfs2_super
*osb
)
722 ocfs2_lock_res_init_once(res
);
723 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_ORPHAN_SCAN
, 0, 0, res
->l_name
);
724 ocfs2_lock_res_init_common(osb
, res
, OCFS2_LOCK_TYPE_ORPHAN_SCAN
,
725 &ocfs2_orphan_scan_lops
, osb
);
728 void ocfs2_file_lock_res_init(struct ocfs2_lock_res
*lockres
,
729 struct ocfs2_file_private
*fp
)
731 struct inode
*inode
= fp
->fp_file
->f_mapping
->host
;
732 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
734 ocfs2_lock_res_init_once(lockres
);
735 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK
, oi
->ip_blkno
,
736 inode
->i_generation
, lockres
->l_name
);
737 ocfs2_lock_res_init_common(OCFS2_SB(inode
->i_sb
), lockres
,
738 OCFS2_LOCK_TYPE_FLOCK
, &ocfs2_flock_lops
,
740 lockres
->l_flags
|= OCFS2_LOCK_NOCACHE
;
743 void ocfs2_qinfo_lock_res_init(struct ocfs2_lock_res
*lockres
,
744 struct ocfs2_mem_dqinfo
*info
)
746 ocfs2_lock_res_init_once(lockres
);
747 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_QINFO
, info
->dqi_gi
.dqi_type
,
749 ocfs2_lock_res_init_common(OCFS2_SB(info
->dqi_gi
.dqi_sb
), lockres
,
750 OCFS2_LOCK_TYPE_QINFO
, &ocfs2_qinfo_lops
,
754 void ocfs2_refcount_lock_res_init(struct ocfs2_lock_res
*lockres
,
755 struct ocfs2_super
*osb
, u64 ref_blkno
,
756 unsigned int generation
)
758 ocfs2_lock_res_init_once(lockres
);
759 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_REFCOUNT
, ref_blkno
,
760 generation
, lockres
->l_name
);
761 ocfs2_lock_res_init_common(osb
, lockres
, OCFS2_LOCK_TYPE_REFCOUNT
,
762 &ocfs2_refcount_block_lops
, osb
);
765 void ocfs2_lock_res_free(struct ocfs2_lock_res
*res
)
767 if (!(res
->l_flags
& OCFS2_LOCK_INITIALIZED
))
770 ocfs2_remove_lockres_tracking(res
);
772 mlog_bug_on_msg(!list_empty(&res
->l_blocked_list
),
773 "Lockres %s is on the blocked list\n",
775 mlog_bug_on_msg(!list_empty(&res
->l_mask_waiters
),
776 "Lockres %s has mask waiters pending\n",
778 mlog_bug_on_msg(spin_is_locked(&res
->l_lock
),
779 "Lockres %s is locked\n",
781 mlog_bug_on_msg(res
->l_ro_holders
,
782 "Lockres %s has %u ro holders\n",
783 res
->l_name
, res
->l_ro_holders
);
784 mlog_bug_on_msg(res
->l_ex_holders
,
785 "Lockres %s has %u ex holders\n",
786 res
->l_name
, res
->l_ex_holders
);
788 /* Need to clear out the lock status block for the dlm */
789 memset(&res
->l_lksb
, 0, sizeof(res
->l_lksb
));
795 * Keep a list of processes who have interest in a lockres.
796 * Note: this is now only uesed for check recursive cluster locking.
798 static inline void ocfs2_add_holder(struct ocfs2_lock_res
*lockres
,
799 struct ocfs2_lock_holder
*oh
)
801 INIT_LIST_HEAD(&oh
->oh_list
);
802 oh
->oh_owner_pid
= get_pid(task_pid(current
));
804 spin_lock(&lockres
->l_lock
);
805 list_add_tail(&oh
->oh_list
, &lockres
->l_holders
);
806 spin_unlock(&lockres
->l_lock
);
809 static struct ocfs2_lock_holder
*
810 ocfs2_pid_holder(struct ocfs2_lock_res
*lockres
,
813 struct ocfs2_lock_holder
*oh
;
815 spin_lock(&lockres
->l_lock
);
816 list_for_each_entry(oh
, &lockres
->l_holders
, oh_list
) {
817 if (oh
->oh_owner_pid
== pid
) {
818 spin_unlock(&lockres
->l_lock
);
822 spin_unlock(&lockres
->l_lock
);
826 static inline void ocfs2_remove_holder(struct ocfs2_lock_res
*lockres
,
827 struct ocfs2_lock_holder
*oh
)
829 spin_lock(&lockres
->l_lock
);
830 list_del(&oh
->oh_list
);
831 spin_unlock(&lockres
->l_lock
);
833 put_pid(oh
->oh_owner_pid
);
837 static inline void ocfs2_inc_holders(struct ocfs2_lock_res
*lockres
,
844 lockres
->l_ex_holders
++;
847 lockres
->l_ro_holders
++;
854 static inline void ocfs2_dec_holders(struct ocfs2_lock_res
*lockres
,
861 BUG_ON(!lockres
->l_ex_holders
);
862 lockres
->l_ex_holders
--;
865 BUG_ON(!lockres
->l_ro_holders
);
866 lockres
->l_ro_holders
--;
873 /* WARNING: This function lives in a world where the only three lock
874 * levels are EX, PR, and NL. It *will* have to be adjusted when more
875 * lock types are added. */
876 static inline int ocfs2_highest_compat_lock_level(int level
)
878 int new_level
= DLM_LOCK_EX
;
880 if (level
== DLM_LOCK_EX
)
881 new_level
= DLM_LOCK_NL
;
882 else if (level
== DLM_LOCK_PR
)
883 new_level
= DLM_LOCK_PR
;
887 static void lockres_set_flags(struct ocfs2_lock_res
*lockres
,
888 unsigned long newflags
)
890 struct ocfs2_mask_waiter
*mw
, *tmp
;
892 assert_spin_locked(&lockres
->l_lock
);
894 lockres
->l_flags
= newflags
;
896 list_for_each_entry_safe(mw
, tmp
, &lockres
->l_mask_waiters
, mw_item
) {
897 if ((lockres
->l_flags
& mw
->mw_mask
) != mw
->mw_goal
)
900 list_del_init(&mw
->mw_item
);
902 complete(&mw
->mw_complete
);
903 ocfs2_track_lock_wait(lockres
);
906 static void lockres_or_flags(struct ocfs2_lock_res
*lockres
, unsigned long or)
908 lockres_set_flags(lockres
, lockres
->l_flags
| or);
910 static void lockres_clear_flags(struct ocfs2_lock_res
*lockres
,
913 lockres_set_flags(lockres
, lockres
->l_flags
& ~clear
);
916 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res
*lockres
)
918 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BUSY
));
919 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
));
920 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BLOCKED
));
921 BUG_ON(lockres
->l_blocking
<= DLM_LOCK_NL
);
923 lockres
->l_level
= lockres
->l_requested
;
924 if (lockres
->l_level
<=
925 ocfs2_highest_compat_lock_level(lockres
->l_blocking
)) {
926 lockres
->l_blocking
= DLM_LOCK_NL
;
927 lockres_clear_flags(lockres
, OCFS2_LOCK_BLOCKED
);
929 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
932 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res
*lockres
)
934 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BUSY
));
935 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
));
937 /* Convert from RO to EX doesn't really need anything as our
938 * information is already up to data. Convert from NL to
939 * *anything* however should mark ourselves as needing an
941 if (lockres
->l_level
== DLM_LOCK_NL
&&
942 lockres
->l_ops
->flags
& LOCK_TYPE_REQUIRES_REFRESH
)
943 lockres_or_flags(lockres
, OCFS2_LOCK_NEEDS_REFRESH
);
945 lockres
->l_level
= lockres
->l_requested
;
948 * We set the OCFS2_LOCK_UPCONVERT_FINISHING flag before clearing
949 * the OCFS2_LOCK_BUSY flag to prevent the dc thread from
950 * downconverting the lock before the upconvert has fully completed.
951 * Do not prevent the dc thread from downconverting if NONBLOCK lock
952 * had already returned.
954 if (!(lockres
->l_flags
& OCFS2_LOCK_NONBLOCK_FINISHED
))
955 lockres_or_flags(lockres
, OCFS2_LOCK_UPCONVERT_FINISHING
);
957 lockres_clear_flags(lockres
, OCFS2_LOCK_NONBLOCK_FINISHED
);
959 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
962 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res
*lockres
)
964 BUG_ON((!(lockres
->l_flags
& OCFS2_LOCK_BUSY
)));
965 BUG_ON(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
);
967 if (lockres
->l_requested
> DLM_LOCK_NL
&&
968 !(lockres
->l_flags
& OCFS2_LOCK_LOCAL
) &&
969 lockres
->l_ops
->flags
& LOCK_TYPE_REQUIRES_REFRESH
)
970 lockres_or_flags(lockres
, OCFS2_LOCK_NEEDS_REFRESH
);
972 lockres
->l_level
= lockres
->l_requested
;
973 lockres_or_flags(lockres
, OCFS2_LOCK_ATTACHED
);
974 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
977 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res
*lockres
,
980 int needs_downconvert
= 0;
982 assert_spin_locked(&lockres
->l_lock
);
984 if (level
> lockres
->l_blocking
) {
985 /* only schedule a downconvert if we haven't already scheduled
986 * one that goes low enough to satisfy the level we're
987 * blocking. this also catches the case where we get
989 if (ocfs2_highest_compat_lock_level(level
) <
990 ocfs2_highest_compat_lock_level(lockres
->l_blocking
))
991 needs_downconvert
= 1;
993 lockres
->l_blocking
= level
;
996 mlog(ML_BASTS
, "lockres %s, block %d, level %d, l_block %d, dwn %d\n",
997 lockres
->l_name
, level
, lockres
->l_level
, lockres
->l_blocking
,
1000 if (needs_downconvert
)
1001 lockres_or_flags(lockres
, OCFS2_LOCK_BLOCKED
);
1002 mlog(0, "needs_downconvert = %d\n", needs_downconvert
);
1003 return needs_downconvert
;
1007 * OCFS2_LOCK_PENDING and l_pending_gen.
1009 * Why does OCFS2_LOCK_PENDING exist? To close a race between setting
1010 * OCFS2_LOCK_BUSY and calling ocfs2_dlm_lock(). See ocfs2_unblock_lock()
1011 * for more details on the race.
1013 * OCFS2_LOCK_PENDING closes the race quite nicely. However, it introduces
1014 * a race on itself. In o2dlm, we can get the ast before ocfs2_dlm_lock()
1015 * returns. The ast clears OCFS2_LOCK_BUSY, and must therefore clear
1016 * OCFS2_LOCK_PENDING at the same time. When ocfs2_dlm_lock() returns,
1017 * the caller is going to try to clear PENDING again. If nothing else is
1018 * happening, __lockres_clear_pending() sees PENDING is unset and does
1021 * But what if another path (eg downconvert thread) has just started a
1022 * new locking action? The other path has re-set PENDING. Our path
1023 * cannot clear PENDING, because that will re-open the original race
1029 * ocfs2_cluster_lock()
1034 * ocfs2_locking_ast() ocfs2_downconvert_thread()
1035 * clear PENDING ocfs2_unblock_lock()
1038 * ocfs2_prepare_downconvert()
1048 * So as you can see, we now have a window where l_lock is not held,
1049 * PENDING is not set, and ocfs2_dlm_lock() has not been called.
1051 * The core problem is that ocfs2_cluster_lock() has cleared the PENDING
1052 * set by ocfs2_prepare_downconvert(). That wasn't nice.
1054 * To solve this we introduce l_pending_gen. A call to
1055 * lockres_clear_pending() will only do so when it is passed a generation
1056 * number that matches the lockres. lockres_set_pending() will return the
1057 * current generation number. When ocfs2_cluster_lock() goes to clear
1058 * PENDING, it passes the generation it got from set_pending(). In our
1059 * example above, the generation numbers will *not* match. Thus,
1060 * ocfs2_cluster_lock() will not clear the PENDING set by
1061 * ocfs2_prepare_downconvert().
1064 /* Unlocked version for ocfs2_locking_ast() */
1065 static void __lockres_clear_pending(struct ocfs2_lock_res
*lockres
,
1066 unsigned int generation
,
1067 struct ocfs2_super
*osb
)
1069 assert_spin_locked(&lockres
->l_lock
);
1072 * The ast and locking functions can race us here. The winner
1073 * will clear pending, the loser will not.
1075 if (!(lockres
->l_flags
& OCFS2_LOCK_PENDING
) ||
1076 (lockres
->l_pending_gen
!= generation
))
1079 lockres_clear_flags(lockres
, OCFS2_LOCK_PENDING
);
1080 lockres
->l_pending_gen
++;
1083 * The downconvert thread may have skipped us because we
1084 * were PENDING. Wake it up.
1086 if (lockres
->l_flags
& OCFS2_LOCK_BLOCKED
)
1087 ocfs2_wake_downconvert_thread(osb
);
1090 /* Locked version for callers of ocfs2_dlm_lock() */
1091 static void lockres_clear_pending(struct ocfs2_lock_res
*lockres
,
1092 unsigned int generation
,
1093 struct ocfs2_super
*osb
)
1095 unsigned long flags
;
1097 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1098 __lockres_clear_pending(lockres
, generation
, osb
);
1099 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1102 static unsigned int lockres_set_pending(struct ocfs2_lock_res
*lockres
)
1104 assert_spin_locked(&lockres
->l_lock
);
1105 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BUSY
));
1107 lockres_or_flags(lockres
, OCFS2_LOCK_PENDING
);
1109 return lockres
->l_pending_gen
;
1112 static void ocfs2_blocking_ast(struct ocfs2_dlm_lksb
*lksb
, int level
)
1114 struct ocfs2_lock_res
*lockres
= ocfs2_lksb_to_lock_res(lksb
);
1115 struct ocfs2_super
*osb
= ocfs2_get_lockres_osb(lockres
);
1116 int needs_downconvert
;
1117 unsigned long flags
;
1119 BUG_ON(level
<= DLM_LOCK_NL
);
1121 mlog(ML_BASTS
, "BAST fired for lockres %s, blocking %d, level %d, "
1122 "type %s\n", lockres
->l_name
, level
, lockres
->l_level
,
1123 ocfs2_lock_type_string(lockres
->l_type
));
1126 * We can skip the bast for locks which don't enable caching -
1127 * they'll be dropped at the earliest possible time anyway.
1129 if (lockres
->l_flags
& OCFS2_LOCK_NOCACHE
)
1132 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1133 needs_downconvert
= ocfs2_generic_handle_bast(lockres
, level
);
1134 if (needs_downconvert
)
1135 ocfs2_schedule_blocked_lock(osb
, lockres
);
1136 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1138 wake_up(&lockres
->l_event
);
1140 ocfs2_wake_downconvert_thread(osb
);
1143 static void ocfs2_locking_ast(struct ocfs2_dlm_lksb
*lksb
)
1145 struct ocfs2_lock_res
*lockres
= ocfs2_lksb_to_lock_res(lksb
);
1146 struct ocfs2_super
*osb
= ocfs2_get_lockres_osb(lockres
);
1147 unsigned long flags
;
1150 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1152 status
= ocfs2_dlm_lock_status(&lockres
->l_lksb
);
1154 if (status
== -EAGAIN
) {
1155 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
1160 mlog(ML_ERROR
, "lockres %s: lksb status value of %d!\n",
1161 lockres
->l_name
, status
);
1162 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1166 mlog(ML_BASTS
, "AST fired for lockres %s, action %d, unlock %d, "
1167 "level %d => %d\n", lockres
->l_name
, lockres
->l_action
,
1168 lockres
->l_unlock_action
, lockres
->l_level
, lockres
->l_requested
);
1170 switch(lockres
->l_action
) {
1171 case OCFS2_AST_ATTACH
:
1172 ocfs2_generic_handle_attach_action(lockres
);
1173 lockres_clear_flags(lockres
, OCFS2_LOCK_LOCAL
);
1175 case OCFS2_AST_CONVERT
:
1176 ocfs2_generic_handle_convert_action(lockres
);
1178 case OCFS2_AST_DOWNCONVERT
:
1179 ocfs2_generic_handle_downconvert_action(lockres
);
1182 mlog(ML_ERROR
, "lockres %s: AST fired with invalid action: %u, "
1183 "flags 0x%lx, unlock: %u\n",
1184 lockres
->l_name
, lockres
->l_action
, lockres
->l_flags
,
1185 lockres
->l_unlock_action
);
1189 /* set it to something invalid so if we get called again we
1191 lockres
->l_action
= OCFS2_AST_INVALID
;
1193 /* Did we try to cancel this lock? Clear that state */
1194 if (lockres
->l_unlock_action
== OCFS2_UNLOCK_CANCEL_CONVERT
)
1195 lockres
->l_unlock_action
= OCFS2_UNLOCK_INVALID
;
1198 * We may have beaten the locking functions here. We certainly
1199 * know that dlm_lock() has been called :-)
1200 * Because we can't have two lock calls in flight at once, we
1201 * can use lockres->l_pending_gen.
1203 __lockres_clear_pending(lockres
, lockres
->l_pending_gen
, osb
);
1205 wake_up(&lockres
->l_event
);
1206 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1209 static void ocfs2_unlock_ast(struct ocfs2_dlm_lksb
*lksb
, int error
)
1211 struct ocfs2_lock_res
*lockres
= ocfs2_lksb_to_lock_res(lksb
);
1212 unsigned long flags
;
1214 mlog(ML_BASTS
, "UNLOCK AST fired for lockres %s, action = %d\n",
1215 lockres
->l_name
, lockres
->l_unlock_action
);
1217 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1219 mlog(ML_ERROR
, "Dlm passes error %d for lock %s, "
1220 "unlock_action %d\n", error
, lockres
->l_name
,
1221 lockres
->l_unlock_action
);
1222 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1226 switch(lockres
->l_unlock_action
) {
1227 case OCFS2_UNLOCK_CANCEL_CONVERT
:
1228 mlog(0, "Cancel convert success for %s\n", lockres
->l_name
);
1229 lockres
->l_action
= OCFS2_AST_INVALID
;
1230 /* Downconvert thread may have requeued this lock, we
1231 * need to wake it. */
1232 if (lockres
->l_flags
& OCFS2_LOCK_BLOCKED
)
1233 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres
));
1235 case OCFS2_UNLOCK_DROP_LOCK
:
1236 lockres
->l_level
= DLM_LOCK_IV
;
1242 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
1243 lockres
->l_unlock_action
= OCFS2_UNLOCK_INVALID
;
1244 wake_up(&lockres
->l_event
);
1245 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1249 * This is the filesystem locking protocol. It provides the lock handling
1250 * hooks for the underlying DLM. It has a maximum version number.
1251 * The version number allows interoperability with systems running at
1252 * the same major number and an equal or smaller minor number.
1254 * Whenever the filesystem does new things with locks (adds or removes a
1255 * lock, orders them differently, does different things underneath a lock),
1256 * the version must be changed. The protocol is negotiated when joining
1257 * the dlm domain. A node may join the domain if its major version is
1258 * identical to all other nodes and its minor version is greater than
1259 * or equal to all other nodes. When its minor version is greater than
1260 * the other nodes, it will run at the minor version specified by the
1263 * If a locking change is made that will not be compatible with older
1264 * versions, the major number must be increased and the minor version set
1265 * to zero. If a change merely adds a behavior that can be disabled when
1266 * speaking to older versions, the minor version must be increased. If a
1267 * change adds a fully backwards compatible change (eg, LVB changes that
1268 * are just ignored by older versions), the version does not need to be
1271 static struct ocfs2_locking_protocol lproto
= {
1273 .pv_major
= OCFS2_LOCKING_PROTOCOL_MAJOR
,
1274 .pv_minor
= OCFS2_LOCKING_PROTOCOL_MINOR
,
1276 .lp_lock_ast
= ocfs2_locking_ast
,
1277 .lp_blocking_ast
= ocfs2_blocking_ast
,
1278 .lp_unlock_ast
= ocfs2_unlock_ast
,
1281 void ocfs2_set_locking_protocol(void)
1283 ocfs2_stack_glue_set_max_proto_version(&lproto
.lp_max_version
);
1286 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res
*lockres
,
1289 unsigned long flags
;
1291 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1292 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
1293 lockres_clear_flags(lockres
, OCFS2_LOCK_UPCONVERT_FINISHING
);
1295 lockres
->l_action
= OCFS2_AST_INVALID
;
1297 lockres
->l_unlock_action
= OCFS2_UNLOCK_INVALID
;
1298 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1300 wake_up(&lockres
->l_event
);
1303 /* Note: If we detect another process working on the lock (i.e.,
1304 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
1305 * to do the right thing in that case.
1307 static int ocfs2_lock_create(struct ocfs2_super
*osb
,
1308 struct ocfs2_lock_res
*lockres
,
1313 unsigned long flags
;
1316 mlog(0, "lock %s, level = %d, flags = %u\n", lockres
->l_name
, level
,
1319 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1320 if ((lockres
->l_flags
& OCFS2_LOCK_ATTACHED
) ||
1321 (lockres
->l_flags
& OCFS2_LOCK_BUSY
)) {
1322 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1326 lockres
->l_action
= OCFS2_AST_ATTACH
;
1327 lockres
->l_requested
= level
;
1328 lockres_or_flags(lockres
, OCFS2_LOCK_BUSY
);
1329 gen
= lockres_set_pending(lockres
);
1330 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1332 ret
= ocfs2_dlm_lock(osb
->cconn
,
1337 OCFS2_LOCK_ID_MAX_LEN
- 1);
1338 lockres_clear_pending(lockres
, gen
, osb
);
1340 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret
, lockres
);
1341 ocfs2_recover_from_dlm_error(lockres
, 1);
1344 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres
->l_name
);
1350 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res
*lockres
,
1353 unsigned long flags
;
1356 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1357 ret
= lockres
->l_flags
& flag
;
1358 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1363 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res
*lockres
)
1366 wait_event(lockres
->l_event
,
1367 !ocfs2_check_wait_flag(lockres
, OCFS2_LOCK_BUSY
));
1370 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res
*lockres
)
1373 wait_event(lockres
->l_event
,
1374 !ocfs2_check_wait_flag(lockres
, OCFS2_LOCK_REFRESHING
));
1377 /* predict what lock level we'll be dropping down to on behalf
1378 * of another node, and return true if the currently wanted
1379 * level will be compatible with it. */
1380 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res
*lockres
,
1383 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BLOCKED
));
1385 return wanted
<= ocfs2_highest_compat_lock_level(lockres
->l_blocking
);
1388 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter
*mw
)
1390 INIT_LIST_HEAD(&mw
->mw_item
);
1391 init_completion(&mw
->mw_complete
);
1392 ocfs2_init_start_time(mw
);
1395 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter
*mw
)
1397 wait_for_completion(&mw
->mw_complete
);
1398 /* Re-arm the completion in case we want to wait on it again */
1399 reinit_completion(&mw
->mw_complete
);
1400 return mw
->mw_status
;
1403 static void lockres_add_mask_waiter(struct ocfs2_lock_res
*lockres
,
1404 struct ocfs2_mask_waiter
*mw
,
1408 BUG_ON(!list_empty(&mw
->mw_item
));
1410 assert_spin_locked(&lockres
->l_lock
);
1412 list_add_tail(&mw
->mw_item
, &lockres
->l_mask_waiters
);
1415 ocfs2_track_lock_wait(lockres
);
1418 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
1419 * if the mask still hadn't reached its goal */
1420 static int __lockres_remove_mask_waiter(struct ocfs2_lock_res
*lockres
,
1421 struct ocfs2_mask_waiter
*mw
)
1425 assert_spin_locked(&lockres
->l_lock
);
1426 if (!list_empty(&mw
->mw_item
)) {
1427 if ((lockres
->l_flags
& mw
->mw_mask
) != mw
->mw_goal
)
1430 list_del_init(&mw
->mw_item
);
1431 init_completion(&mw
->mw_complete
);
1432 ocfs2_track_lock_wait(lockres
);
1438 static int lockres_remove_mask_waiter(struct ocfs2_lock_res
*lockres
,
1439 struct ocfs2_mask_waiter
*mw
)
1441 unsigned long flags
;
1444 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1445 ret
= __lockres_remove_mask_waiter(lockres
, mw
);
1446 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1452 static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter
*mw
,
1453 struct ocfs2_lock_res
*lockres
)
1457 ret
= wait_for_completion_interruptible(&mw
->mw_complete
);
1459 lockres_remove_mask_waiter(lockres
, mw
);
1461 ret
= mw
->mw_status
;
1462 /* Re-arm the completion in case we want to wait on it again */
1463 reinit_completion(&mw
->mw_complete
);
1467 static int __ocfs2_cluster_lock(struct ocfs2_super
*osb
,
1468 struct ocfs2_lock_res
*lockres
,
1473 unsigned long caller_ip
)
1475 struct ocfs2_mask_waiter mw
;
1476 int wait
, catch_signals
= !(osb
->s_mount_opt
& OCFS2_MOUNT_NOINTR
);
1477 int ret
= 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1478 unsigned long flags
;
1480 int noqueue_attempted
= 0;
1484 if (!(lockres
->l_flags
& OCFS2_LOCK_INITIALIZED
)) {
1485 mlog_errno(-EINVAL
);
1489 ocfs2_init_mask_waiter(&mw
);
1491 if (lockres
->l_ops
->flags
& LOCK_TYPE_USES_LVB
)
1492 lkm_flags
|= DLM_LKF_VALBLK
;
1497 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1499 if (catch_signals
&& signal_pending(current
)) {
1504 mlog_bug_on_msg(lockres
->l_flags
& OCFS2_LOCK_FREEING
,
1505 "Cluster lock called on freeing lockres %s! flags "
1506 "0x%lx\n", lockres
->l_name
, lockres
->l_flags
);
1508 /* We only compare against the currently granted level
1509 * here. If the lock is blocked waiting on a downconvert,
1510 * we'll get caught below. */
1511 if (lockres
->l_flags
& OCFS2_LOCK_BUSY
&&
1512 level
> lockres
->l_level
) {
1513 /* is someone sitting in dlm_lock? If so, wait on
1515 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BUSY
, 0);
1520 if (lockres
->l_flags
& OCFS2_LOCK_UPCONVERT_FINISHING
) {
1522 * We've upconverted. If the lock now has a level we can
1523 * work with, we take it. If, however, the lock is not at the
1524 * required level, we go thru the full cycle. One way this could
1525 * happen is if a process requesting an upconvert to PR is
1526 * closely followed by another requesting upconvert to an EX.
1527 * If the process requesting EX lands here, we want it to
1528 * continue attempting to upconvert and let the process
1529 * requesting PR take the lock.
1530 * If multiple processes request upconvert to PR, the first one
1531 * here will take the lock. The others will have to go thru the
1532 * OCFS2_LOCK_BLOCKED check to ensure that there is no pending
1533 * downconvert request.
1535 if (level
<= lockres
->l_level
)
1536 goto update_holders
;
1539 if (lockres
->l_flags
& OCFS2_LOCK_BLOCKED
&&
1540 !ocfs2_may_continue_on_blocked_lock(lockres
, level
)) {
1541 /* is the lock is currently blocked on behalf of
1543 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BLOCKED
, 0);
1548 if (level
> lockres
->l_level
) {
1549 if (noqueue_attempted
> 0) {
1553 if (lkm_flags
& DLM_LKF_NOQUEUE
)
1554 noqueue_attempted
= 1;
1556 if (lockres
->l_action
!= OCFS2_AST_INVALID
)
1557 mlog(ML_ERROR
, "lockres %s has action %u pending\n",
1558 lockres
->l_name
, lockres
->l_action
);
1560 if (!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
)) {
1561 lockres
->l_action
= OCFS2_AST_ATTACH
;
1562 lkm_flags
&= ~DLM_LKF_CONVERT
;
1564 lockres
->l_action
= OCFS2_AST_CONVERT
;
1565 lkm_flags
|= DLM_LKF_CONVERT
;
1568 lockres
->l_requested
= level
;
1569 lockres_or_flags(lockres
, OCFS2_LOCK_BUSY
);
1570 gen
= lockres_set_pending(lockres
);
1571 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1573 BUG_ON(level
== DLM_LOCK_IV
);
1574 BUG_ON(level
== DLM_LOCK_NL
);
1576 mlog(ML_BASTS
, "lockres %s, convert from %d to %d\n",
1577 lockres
->l_name
, lockres
->l_level
, level
);
1579 /* call dlm_lock to upgrade lock now */
1580 ret
= ocfs2_dlm_lock(osb
->cconn
,
1585 OCFS2_LOCK_ID_MAX_LEN
- 1);
1586 lockres_clear_pending(lockres
, gen
, osb
);
1588 if (!(lkm_flags
& DLM_LKF_NOQUEUE
) ||
1590 ocfs2_log_dlm_error("ocfs2_dlm_lock",
1593 ocfs2_recover_from_dlm_error(lockres
, 1);
1598 mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n",
1601 /* At this point we've gone inside the dlm and need to
1602 * complete our work regardless. */
1605 /* wait for busy to clear and carry on */
1610 /* Ok, if we get here then we're good to go. */
1611 ocfs2_inc_holders(lockres
, level
);
1615 lockres_clear_flags(lockres
, OCFS2_LOCK_UPCONVERT_FINISHING
);
1617 /* ocfs2_unblock_lock reques on seeing OCFS2_LOCK_UPCONVERT_FINISHING */
1618 kick_dc
= (lockres
->l_flags
& OCFS2_LOCK_BLOCKED
);
1620 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1622 ocfs2_wake_downconvert_thread(osb
);
1625 * This is helping work around a lock inversion between the page lock
1626 * and dlm locks. One path holds the page lock while calling aops
1627 * which block acquiring dlm locks. The voting thread holds dlm
1628 * locks while acquiring page locks while down converting data locks.
1629 * This block is helping an aop path notice the inversion and back
1630 * off to unlock its page lock before trying the dlm lock again.
1632 if (wait
&& arg_flags
& OCFS2_LOCK_NONBLOCK
&&
1633 mw
.mw_mask
& (OCFS2_LOCK_BUSY
|OCFS2_LOCK_BLOCKED
)) {
1635 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1636 if (__lockres_remove_mask_waiter(lockres
, &mw
)) {
1638 lockres_or_flags(lockres
,
1639 OCFS2_LOCK_NONBLOCK_FINISHED
);
1640 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1643 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1648 ret
= ocfs2_wait_for_mask(&mw
);
1653 ocfs2_update_lock_stats(lockres
, level
, &mw
, ret
);
1655 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1656 if (!ret
&& lockres
->l_lockdep_map
.key
!= NULL
) {
1657 if (level
== DLM_LOCK_PR
)
1658 rwsem_acquire_read(&lockres
->l_lockdep_map
, l_subclass
,
1659 !!(arg_flags
& OCFS2_META_LOCK_NOQUEUE
),
1662 rwsem_acquire(&lockres
->l_lockdep_map
, l_subclass
,
1663 !!(arg_flags
& OCFS2_META_LOCK_NOQUEUE
),
1670 static inline int ocfs2_cluster_lock(struct ocfs2_super
*osb
,
1671 struct ocfs2_lock_res
*lockres
,
1676 return __ocfs2_cluster_lock(osb
, lockres
, level
, lkm_flags
, arg_flags
,
1681 static void __ocfs2_cluster_unlock(struct ocfs2_super
*osb
,
1682 struct ocfs2_lock_res
*lockres
,
1684 unsigned long caller_ip
)
1686 unsigned long flags
;
1688 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1689 ocfs2_dec_holders(lockres
, level
);
1690 ocfs2_downconvert_on_unlock(osb
, lockres
);
1691 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1692 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1693 if (lockres
->l_lockdep_map
.key
!= NULL
)
1694 rwsem_release(&lockres
->l_lockdep_map
, caller_ip
);
1698 static int ocfs2_create_new_lock(struct ocfs2_super
*osb
,
1699 struct ocfs2_lock_res
*lockres
,
1703 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
1704 unsigned long flags
;
1705 u32 lkm_flags
= local
? DLM_LKF_LOCAL
: 0;
1707 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1708 BUG_ON(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
);
1709 lockres_or_flags(lockres
, OCFS2_LOCK_LOCAL
);
1710 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1712 return ocfs2_lock_create(osb
, lockres
, level
, lkm_flags
);
1715 /* Grants us an EX lock on the data and metadata resources, skipping
1716 * the normal cluster directory lookup. Use this ONLY on newly created
1717 * inodes which other nodes can't possibly see, and which haven't been
1718 * hashed in the inode hash yet. This can give us a good performance
1719 * increase as it'll skip the network broadcast normally associated
1720 * with creating a new lock resource. */
1721 int ocfs2_create_new_inode_locks(struct inode
*inode
)
1724 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1726 BUG_ON(!ocfs2_inode_is_new(inode
));
1728 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1730 /* NOTE: That we don't increment any of the holder counts, nor
1731 * do we add anything to a journal handle. Since this is
1732 * supposed to be a new inode which the cluster doesn't know
1733 * about yet, there is no need to. As far as the LVB handling
1734 * is concerned, this is basically like acquiring an EX lock
1735 * on a resource which has an invalid one -- we'll set it
1736 * valid when we release the EX. */
1738 ret
= ocfs2_create_new_lock(osb
, &OCFS2_I(inode
)->ip_rw_lockres
, 1, 1);
1745 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
1746 * don't use a generation in their lock names.
1748 ret
= ocfs2_create_new_lock(osb
, &OCFS2_I(inode
)->ip_inode_lockres
, 1, 0);
1754 ret
= ocfs2_create_new_lock(osb
, &OCFS2_I(inode
)->ip_open_lockres
, 0, 0);
1762 int ocfs2_rw_lock(struct inode
*inode
, int write
)
1765 struct ocfs2_lock_res
*lockres
;
1766 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1768 mlog(0, "inode %llu take %s RW lock\n",
1769 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1770 write
? "EXMODE" : "PRMODE");
1772 if (ocfs2_mount_local(osb
))
1775 lockres
= &OCFS2_I(inode
)->ip_rw_lockres
;
1777 level
= write
? DLM_LOCK_EX
: DLM_LOCK_PR
;
1779 status
= ocfs2_cluster_lock(osb
, lockres
, level
, 0, 0);
1786 int ocfs2_try_rw_lock(struct inode
*inode
, int write
)
1789 struct ocfs2_lock_res
*lockres
;
1790 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1792 mlog(0, "inode %llu try to take %s RW lock\n",
1793 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1794 write
? "EXMODE" : "PRMODE");
1796 if (ocfs2_mount_local(osb
))
1799 lockres
= &OCFS2_I(inode
)->ip_rw_lockres
;
1801 level
= write
? DLM_LOCK_EX
: DLM_LOCK_PR
;
1803 status
= ocfs2_cluster_lock(osb
, lockres
, level
, DLM_LKF_NOQUEUE
, 0);
1807 void ocfs2_rw_unlock(struct inode
*inode
, int write
)
1809 int level
= write
? DLM_LOCK_EX
: DLM_LOCK_PR
;
1810 struct ocfs2_lock_res
*lockres
= &OCFS2_I(inode
)->ip_rw_lockres
;
1811 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1813 mlog(0, "inode %llu drop %s RW lock\n",
1814 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1815 write
? "EXMODE" : "PRMODE");
1817 if (!ocfs2_mount_local(osb
))
1818 ocfs2_cluster_unlock(osb
, lockres
, level
);
1822 * ocfs2_open_lock always get PR mode lock.
1824 int ocfs2_open_lock(struct inode
*inode
)
1827 struct ocfs2_lock_res
*lockres
;
1828 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1830 mlog(0, "inode %llu take PRMODE open lock\n",
1831 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1833 if (ocfs2_is_hard_readonly(osb
) || ocfs2_mount_local(osb
))
1836 lockres
= &OCFS2_I(inode
)->ip_open_lockres
;
1838 status
= ocfs2_cluster_lock(osb
, lockres
, DLM_LOCK_PR
, 0, 0);
1846 int ocfs2_try_open_lock(struct inode
*inode
, int write
)
1848 int status
= 0, level
;
1849 struct ocfs2_lock_res
*lockres
;
1850 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1852 mlog(0, "inode %llu try to take %s open lock\n",
1853 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1854 write
? "EXMODE" : "PRMODE");
1856 if (ocfs2_is_hard_readonly(osb
)) {
1862 if (ocfs2_mount_local(osb
))
1865 lockres
= &OCFS2_I(inode
)->ip_open_lockres
;
1867 level
= write
? DLM_LOCK_EX
: DLM_LOCK_PR
;
1870 * The file system may already holding a PRMODE/EXMODE open lock.
1871 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
1872 * other nodes and the -EAGAIN will indicate to the caller that
1873 * this inode is still in use.
1875 status
= ocfs2_cluster_lock(osb
, lockres
, level
, DLM_LKF_NOQUEUE
, 0);
1882 * ocfs2_open_unlock unlock PR and EX mode open locks.
1884 void ocfs2_open_unlock(struct inode
*inode
)
1886 struct ocfs2_lock_res
*lockres
= &OCFS2_I(inode
)->ip_open_lockres
;
1887 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1889 mlog(0, "inode %llu drop open lock\n",
1890 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1892 if (ocfs2_mount_local(osb
))
1895 if(lockres
->l_ro_holders
)
1896 ocfs2_cluster_unlock(osb
, lockres
, DLM_LOCK_PR
);
1897 if(lockres
->l_ex_holders
)
1898 ocfs2_cluster_unlock(osb
, lockres
, DLM_LOCK_EX
);
1904 static int ocfs2_flock_handle_signal(struct ocfs2_lock_res
*lockres
,
1908 struct ocfs2_super
*osb
= ocfs2_get_lockres_osb(lockres
);
1909 unsigned long flags
;
1910 struct ocfs2_mask_waiter mw
;
1912 ocfs2_init_mask_waiter(&mw
);
1915 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1916 if (lockres
->l_flags
& OCFS2_LOCK_BUSY
) {
1917 ret
= ocfs2_prepare_cancel_convert(osb
, lockres
);
1919 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1920 ret
= ocfs2_cancel_convert(osb
, lockres
);
1927 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BUSY
, 0);
1928 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1930 ocfs2_wait_for_mask(&mw
);
1936 * We may still have gotten the lock, in which case there's no
1937 * point to restarting the syscall.
1939 if (lockres
->l_level
== level
)
1942 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret
,
1943 lockres
->l_flags
, lockres
->l_level
, lockres
->l_action
);
1945 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1952 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1953 * flock() calls. The locking approach this requires is sufficiently
1954 * different from all other cluster lock types that we implement a
1955 * separate path to the "low-level" dlm calls. In particular:
1957 * - No optimization of lock levels is done - we take at exactly
1958 * what's been requested.
1960 * - No lock caching is employed. We immediately downconvert to
1961 * no-lock at unlock time. This also means flock locks never go on
1962 * the blocking list).
1964 * - Since userspace can trivially deadlock itself with flock, we make
1965 * sure to allow cancellation of a misbehaving applications flock()
1968 * - Access to any flock lockres doesn't require concurrency, so we
1969 * can simplify the code by requiring the caller to guarantee
1970 * serialization of dlmglue flock calls.
1972 int ocfs2_file_lock(struct file
*file
, int ex
, int trylock
)
1974 int ret
, level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
1975 unsigned int lkm_flags
= trylock
? DLM_LKF_NOQUEUE
: 0;
1976 unsigned long flags
;
1977 struct ocfs2_file_private
*fp
= file
->private_data
;
1978 struct ocfs2_lock_res
*lockres
= &fp
->fp_flock
;
1979 struct ocfs2_super
*osb
= OCFS2_SB(file
->f_mapping
->host
->i_sb
);
1980 struct ocfs2_mask_waiter mw
;
1982 ocfs2_init_mask_waiter(&mw
);
1984 if ((lockres
->l_flags
& OCFS2_LOCK_BUSY
) ||
1985 (lockres
->l_level
> DLM_LOCK_NL
)) {
1987 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1988 "level: %u\n", lockres
->l_name
, lockres
->l_flags
,
1993 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1994 if (!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
)) {
1995 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BUSY
, 0);
1996 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1999 * Get the lock at NLMODE to start - that way we
2000 * can cancel the upconvert request if need be.
2002 ret
= ocfs2_lock_create(osb
, lockres
, DLM_LOCK_NL
, 0);
2008 ret
= ocfs2_wait_for_mask(&mw
);
2013 spin_lock_irqsave(&lockres
->l_lock
, flags
);
2016 lockres
->l_action
= OCFS2_AST_CONVERT
;
2017 lkm_flags
|= DLM_LKF_CONVERT
;
2018 lockres
->l_requested
= level
;
2019 lockres_or_flags(lockres
, OCFS2_LOCK_BUSY
);
2021 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BUSY
, 0);
2022 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2024 ret
= ocfs2_dlm_lock(osb
->cconn
, level
, &lockres
->l_lksb
, lkm_flags
,
2025 lockres
->l_name
, OCFS2_LOCK_ID_MAX_LEN
- 1);
2027 if (!trylock
|| (ret
!= -EAGAIN
)) {
2028 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret
, lockres
);
2032 ocfs2_recover_from_dlm_error(lockres
, 1);
2033 lockres_remove_mask_waiter(lockres
, &mw
);
2037 ret
= ocfs2_wait_for_mask_interruptible(&mw
, lockres
);
2038 if (ret
== -ERESTARTSYS
) {
2040 * Userspace can cause deadlock itself with
2041 * flock(). Current behavior locally is to allow the
2042 * deadlock, but abort the system call if a signal is
2043 * received. We follow this example, otherwise a
2044 * poorly written program could sit in kernel until
2047 * Handling this is a bit more complicated for Ocfs2
2048 * though. We can't exit this function with an
2049 * outstanding lock request, so a cancel convert is
2050 * required. We intentionally overwrite 'ret' - if the
2051 * cancel fails and the lock was granted, it's easier
2052 * to just bubble success back up to the user.
2054 ret
= ocfs2_flock_handle_signal(lockres
, level
);
2055 } else if (!ret
&& (level
> lockres
->l_level
)) {
2056 /* Trylock failed asynchronously */
2063 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
2064 lockres
->l_name
, ex
, trylock
, ret
);
2068 void ocfs2_file_unlock(struct file
*file
)
2072 unsigned long flags
;
2073 struct ocfs2_file_private
*fp
= file
->private_data
;
2074 struct ocfs2_lock_res
*lockres
= &fp
->fp_flock
;
2075 struct ocfs2_super
*osb
= OCFS2_SB(file
->f_mapping
->host
->i_sb
);
2076 struct ocfs2_mask_waiter mw
;
2078 ocfs2_init_mask_waiter(&mw
);
2080 if (!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
))
2083 if (lockres
->l_level
== DLM_LOCK_NL
)
2086 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
2087 lockres
->l_name
, lockres
->l_flags
, lockres
->l_level
,
2090 spin_lock_irqsave(&lockres
->l_lock
, flags
);
2092 * Fake a blocking ast for the downconvert code.
2094 lockres_or_flags(lockres
, OCFS2_LOCK_BLOCKED
);
2095 lockres
->l_blocking
= DLM_LOCK_EX
;
2097 gen
= ocfs2_prepare_downconvert(lockres
, DLM_LOCK_NL
);
2098 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BUSY
, 0);
2099 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2101 ret
= ocfs2_downconvert_lock(osb
, lockres
, DLM_LOCK_NL
, 0, gen
);
2107 ret
= ocfs2_wait_for_mask(&mw
);
2112 static void ocfs2_downconvert_on_unlock(struct ocfs2_super
*osb
,
2113 struct ocfs2_lock_res
*lockres
)
2117 /* If we know that another node is waiting on our lock, kick
2118 * the downconvert thread * pre-emptively when we reach a release
2120 if (lockres
->l_flags
& OCFS2_LOCK_BLOCKED
) {
2121 switch(lockres
->l_blocking
) {
2123 if (!lockres
->l_ex_holders
&& !lockres
->l_ro_holders
)
2127 if (!lockres
->l_ex_holders
)
2136 ocfs2_wake_downconvert_thread(osb
);
2139 #define OCFS2_SEC_BITS 34
2140 #define OCFS2_SEC_SHIFT (64 - OCFS2_SEC_BITS)
2141 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
2143 /* LVB only has room for 64 bits of time here so we pack it for
2145 static u64
ocfs2_pack_timespec(struct timespec64
*spec
)
2148 u64 sec
= clamp_t(time64_t
, spec
->tv_sec
, 0, 0x3ffffffffull
);
2149 u32 nsec
= spec
->tv_nsec
;
2151 res
= (sec
<< OCFS2_SEC_SHIFT
) | (nsec
& OCFS2_NSEC_MASK
);
2156 /* Call this with the lockres locked. I am reasonably sure we don't
2157 * need ip_lock in this function as anyone who would be changing those
2158 * values is supposed to be blocked in ocfs2_inode_lock right now. */
2159 static void __ocfs2_stuff_meta_lvb(struct inode
*inode
)
2161 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2162 struct ocfs2_lock_res
*lockres
= &oi
->ip_inode_lockres
;
2163 struct ocfs2_meta_lvb
*lvb
;
2165 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
2168 * Invalidate the LVB of a deleted inode - this way other
2169 * nodes are forced to go to disk and discover the new inode
2172 if (oi
->ip_flags
& OCFS2_INODE_DELETED
) {
2173 lvb
->lvb_version
= 0;
2177 lvb
->lvb_version
= OCFS2_LVB_VERSION
;
2178 lvb
->lvb_isize
= cpu_to_be64(i_size_read(inode
));
2179 lvb
->lvb_iclusters
= cpu_to_be32(oi
->ip_clusters
);
2180 lvb
->lvb_iuid
= cpu_to_be32(i_uid_read(inode
));
2181 lvb
->lvb_igid
= cpu_to_be32(i_gid_read(inode
));
2182 lvb
->lvb_imode
= cpu_to_be16(inode
->i_mode
);
2183 lvb
->lvb_inlink
= cpu_to_be16(inode
->i_nlink
);
2184 lvb
->lvb_iatime_packed
=
2185 cpu_to_be64(ocfs2_pack_timespec(&inode
->i_atime
));
2186 lvb
->lvb_ictime_packed
=
2187 cpu_to_be64(ocfs2_pack_timespec(&inode
->i_ctime
));
2188 lvb
->lvb_imtime_packed
=
2189 cpu_to_be64(ocfs2_pack_timespec(&inode
->i_mtime
));
2190 lvb
->lvb_iattr
= cpu_to_be32(oi
->ip_attr
);
2191 lvb
->lvb_idynfeatures
= cpu_to_be16(oi
->ip_dyn_features
);
2192 lvb
->lvb_igeneration
= cpu_to_be32(inode
->i_generation
);
2195 mlog_meta_lvb(0, lockres
);
2198 static void ocfs2_unpack_timespec(struct timespec64
*spec
,
2201 spec
->tv_sec
= packed_time
>> OCFS2_SEC_SHIFT
;
2202 spec
->tv_nsec
= packed_time
& OCFS2_NSEC_MASK
;
2205 static int ocfs2_refresh_inode_from_lvb(struct inode
*inode
)
2207 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2208 struct ocfs2_lock_res
*lockres
= &oi
->ip_inode_lockres
;
2209 struct ocfs2_meta_lvb
*lvb
;
2211 mlog_meta_lvb(0, lockres
);
2213 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
2214 if (inode_wrong_type(inode
, be16_to_cpu(lvb
->lvb_imode
)))
2217 /* We're safe here without the lockres lock... */
2218 spin_lock(&oi
->ip_lock
);
2219 oi
->ip_clusters
= be32_to_cpu(lvb
->lvb_iclusters
);
2220 i_size_write(inode
, be64_to_cpu(lvb
->lvb_isize
));
2222 oi
->ip_attr
= be32_to_cpu(lvb
->lvb_iattr
);
2223 oi
->ip_dyn_features
= be16_to_cpu(lvb
->lvb_idynfeatures
);
2224 ocfs2_set_inode_flags(inode
);
2226 /* fast-symlinks are a special case */
2227 if (S_ISLNK(inode
->i_mode
) && !oi
->ip_clusters
)
2228 inode
->i_blocks
= 0;
2230 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
2232 i_uid_write(inode
, be32_to_cpu(lvb
->lvb_iuid
));
2233 i_gid_write(inode
, be32_to_cpu(lvb
->lvb_igid
));
2234 inode
->i_mode
= be16_to_cpu(lvb
->lvb_imode
);
2235 set_nlink(inode
, be16_to_cpu(lvb
->lvb_inlink
));
2236 ocfs2_unpack_timespec(&inode
->i_atime
,
2237 be64_to_cpu(lvb
->lvb_iatime_packed
));
2238 ocfs2_unpack_timespec(&inode
->i_mtime
,
2239 be64_to_cpu(lvb
->lvb_imtime_packed
));
2240 ocfs2_unpack_timespec(&inode
->i_ctime
,
2241 be64_to_cpu(lvb
->lvb_ictime_packed
));
2242 spin_unlock(&oi
->ip_lock
);
2246 static inline int ocfs2_meta_lvb_is_trustable(struct inode
*inode
,
2247 struct ocfs2_lock_res
*lockres
)
2249 struct ocfs2_meta_lvb
*lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
2251 if (ocfs2_dlm_lvb_valid(&lockres
->l_lksb
)
2252 && lvb
->lvb_version
== OCFS2_LVB_VERSION
2253 && be32_to_cpu(lvb
->lvb_igeneration
) == inode
->i_generation
)
2258 /* Determine whether a lock resource needs to be refreshed, and
2259 * arbitrate who gets to refresh it.
2261 * 0 means no refresh needed.
2263 * > 0 means you need to refresh this and you MUST call
2264 * ocfs2_complete_lock_res_refresh afterwards. */
2265 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res
*lockres
)
2267 unsigned long flags
;
2271 spin_lock_irqsave(&lockres
->l_lock
, flags
);
2272 if (!(lockres
->l_flags
& OCFS2_LOCK_NEEDS_REFRESH
)) {
2273 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2277 if (lockres
->l_flags
& OCFS2_LOCK_REFRESHING
) {
2278 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2280 ocfs2_wait_on_refreshing_lock(lockres
);
2284 /* Ok, I'll be the one to refresh this lock. */
2285 lockres_or_flags(lockres
, OCFS2_LOCK_REFRESHING
);
2286 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2290 mlog(0, "status %d\n", status
);
2294 /* If status is non zero, I'll mark it as not being in refresh
2295 * anymroe, but i won't clear the needs refresh flag. */
2296 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res
*lockres
,
2299 unsigned long flags
;
2301 spin_lock_irqsave(&lockres
->l_lock
, flags
);
2302 lockres_clear_flags(lockres
, OCFS2_LOCK_REFRESHING
);
2304 lockres_clear_flags(lockres
, OCFS2_LOCK_NEEDS_REFRESH
);
2305 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2307 wake_up(&lockres
->l_event
);
2310 /* may or may not return a bh if it went to disk. */
2311 static int ocfs2_inode_lock_update(struct inode
*inode
,
2312 struct buffer_head
**bh
)
2315 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2316 struct ocfs2_lock_res
*lockres
= &oi
->ip_inode_lockres
;
2317 struct ocfs2_dinode
*fe
;
2318 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2320 if (ocfs2_mount_local(osb
))
2323 spin_lock(&oi
->ip_lock
);
2324 if (oi
->ip_flags
& OCFS2_INODE_DELETED
) {
2325 mlog(0, "Orphaned inode %llu was deleted while we "
2326 "were waiting on a lock. ip_flags = 0x%x\n",
2327 (unsigned long long)oi
->ip_blkno
, oi
->ip_flags
);
2328 spin_unlock(&oi
->ip_lock
);
2332 spin_unlock(&oi
->ip_lock
);
2334 if (!ocfs2_should_refresh_lock_res(lockres
))
2337 /* This will discard any caching information we might have had
2338 * for the inode metadata. */
2339 ocfs2_metadata_cache_purge(INODE_CACHE(inode
));
2341 ocfs2_extent_map_trunc(inode
, 0);
2343 if (ocfs2_meta_lvb_is_trustable(inode
, lockres
)) {
2344 mlog(0, "Trusting LVB on inode %llu\n",
2345 (unsigned long long)oi
->ip_blkno
);
2346 status
= ocfs2_refresh_inode_from_lvb(inode
);
2349 /* Boo, we have to go to disk. */
2350 /* read bh, cast, ocfs2_refresh_inode */
2351 status
= ocfs2_read_inode_block(inode
, bh
);
2356 fe
= (struct ocfs2_dinode
*) (*bh
)->b_data
;
2357 if (inode_wrong_type(inode
, le16_to_cpu(fe
->i_mode
))) {
2362 /* This is a good chance to make sure we're not
2363 * locking an invalid object. ocfs2_read_inode_block()
2364 * already checked that the inode block is sane.
2366 * We bug on a stale inode here because we checked
2367 * above whether it was wiped from disk. The wiping
2368 * node provides a guarantee that we receive that
2369 * message and can mark the inode before dropping any
2370 * locks associated with it. */
2371 mlog_bug_on_msg(inode
->i_generation
!=
2372 le32_to_cpu(fe
->i_generation
),
2373 "Invalid dinode %llu disk generation: %u "
2374 "inode->i_generation: %u\n",
2375 (unsigned long long)oi
->ip_blkno
,
2376 le32_to_cpu(fe
->i_generation
),
2377 inode
->i_generation
);
2378 mlog_bug_on_msg(le64_to_cpu(fe
->i_dtime
) ||
2379 !(fe
->i_flags
& cpu_to_le32(OCFS2_VALID_FL
)),
2380 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
2381 (unsigned long long)oi
->ip_blkno
,
2382 (unsigned long long)le64_to_cpu(fe
->i_dtime
),
2383 le32_to_cpu(fe
->i_flags
));
2385 ocfs2_refresh_inode(inode
, fe
);
2386 ocfs2_track_lock_refresh(lockres
);
2391 ocfs2_complete_lock_res_refresh(lockres
, status
);
2396 static int ocfs2_assign_bh(struct inode
*inode
,
2397 struct buffer_head
**ret_bh
,
2398 struct buffer_head
*passed_bh
)
2403 /* Ok, the update went to disk for us, use the
2405 *ret_bh
= passed_bh
;
2411 status
= ocfs2_read_inode_block(inode
, ret_bh
);
2419 * returns < 0 error if the callback will never be called, otherwise
2420 * the result of the lock will be communicated via the callback.
2422 int ocfs2_inode_lock_full_nested(struct inode
*inode
,
2423 struct buffer_head
**ret_bh
,
2428 int status
, level
, acquired
;
2430 struct ocfs2_lock_res
*lockres
= NULL
;
2431 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2432 struct buffer_head
*local_bh
= NULL
;
2434 mlog(0, "inode %llu, take %s META lock\n",
2435 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2436 ex
? "EXMODE" : "PRMODE");
2440 /* We'll allow faking a readonly metadata lock for
2442 if (ocfs2_is_hard_readonly(osb
)) {
2448 if ((arg_flags
& OCFS2_META_LOCK_GETBH
) ||
2449 ocfs2_mount_local(osb
))
2452 if (!(arg_flags
& OCFS2_META_LOCK_RECOVERY
))
2453 ocfs2_wait_for_recovery(osb
);
2455 lockres
= &OCFS2_I(inode
)->ip_inode_lockres
;
2456 level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2458 if (arg_flags
& OCFS2_META_LOCK_NOQUEUE
)
2459 dlm_flags
|= DLM_LKF_NOQUEUE
;
2461 status
= __ocfs2_cluster_lock(osb
, lockres
, level
, dlm_flags
,
2462 arg_flags
, subclass
, _RET_IP_
);
2464 if (status
!= -EAGAIN
)
2469 /* Notify the error cleanup path to drop the cluster lock. */
2472 /* We wait twice because a node may have died while we were in
2473 * the lower dlm layers. The second time though, we've
2474 * committed to owning this lock so we don't allow signals to
2475 * abort the operation. */
2476 if (!(arg_flags
& OCFS2_META_LOCK_RECOVERY
))
2477 ocfs2_wait_for_recovery(osb
);
2481 * We only see this flag if we're being called from
2482 * ocfs2_read_locked_inode(). It means we're locking an inode
2483 * which hasn't been populated yet, so clear the refresh flag
2484 * and let the caller handle it.
2486 if (inode
->i_state
& I_NEW
) {
2489 ocfs2_complete_lock_res_refresh(lockres
, 0);
2493 /* This is fun. The caller may want a bh back, or it may
2494 * not. ocfs2_inode_lock_update definitely wants one in, but
2495 * may or may not read one, depending on what's in the
2496 * LVB. The result of all of this is that we've *only* gone to
2497 * disk if we have to, so the complexity is worthwhile. */
2498 status
= ocfs2_inode_lock_update(inode
, &local_bh
);
2500 if (status
!= -ENOENT
)
2506 status
= ocfs2_assign_bh(inode
, ret_bh
, local_bh
);
2515 if (ret_bh
&& (*ret_bh
)) {
2520 ocfs2_inode_unlock(inode
, ex
);
2528 * This is working around a lock inversion between tasks acquiring DLM
2529 * locks while holding a page lock and the downconvert thread which
2530 * blocks dlm lock acquiry while acquiring page locks.
2532 * ** These _with_page variantes are only intended to be called from aop
2533 * methods that hold page locks and return a very specific *positive* error
2534 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2536 * The DLM is called such that it returns -EAGAIN if it would have
2537 * blocked waiting for the downconvert thread. In that case we unlock
2538 * our page so the downconvert thread can make progress. Once we've
2539 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2540 * that called us can bubble that back up into the VFS who will then
2541 * immediately retry the aop call.
2543 int ocfs2_inode_lock_with_page(struct inode
*inode
,
2544 struct buffer_head
**ret_bh
,
2550 ret
= ocfs2_inode_lock_full(inode
, ret_bh
, ex
, OCFS2_LOCK_NONBLOCK
);
2551 if (ret
== -EAGAIN
) {
2554 * If we can't get inode lock immediately, we should not return
2555 * directly here, since this will lead to a softlockup problem.
2556 * The method is to get a blocking lock and immediately unlock
2557 * before returning, this can avoid CPU resource waste due to
2558 * lots of retries, and benefits fairness in getting lock.
2560 if (ocfs2_inode_lock(inode
, ret_bh
, ex
) == 0)
2561 ocfs2_inode_unlock(inode
, ex
);
2562 ret
= AOP_TRUNCATED_PAGE
;
2568 int ocfs2_inode_lock_atime(struct inode
*inode
,
2569 struct vfsmount
*vfsmnt
,
2570 int *level
, int wait
)
2575 ret
= ocfs2_inode_lock(inode
, NULL
, 0);
2577 ret
= ocfs2_try_inode_lock(inode
, NULL
, 0);
2586 * If we should update atime, we will get EX lock,
2587 * otherwise we just get PR lock.
2589 if (ocfs2_should_update_atime(inode
, vfsmnt
)) {
2590 struct buffer_head
*bh
= NULL
;
2592 ocfs2_inode_unlock(inode
, 0);
2594 ret
= ocfs2_inode_lock(inode
, &bh
, 1);
2596 ret
= ocfs2_try_inode_lock(inode
, &bh
, 1);
2604 if (ocfs2_should_update_atime(inode
, vfsmnt
))
2605 ocfs2_update_inode_atime(inode
, bh
);
2613 void ocfs2_inode_unlock(struct inode
*inode
,
2616 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2617 struct ocfs2_lock_res
*lockres
= &OCFS2_I(inode
)->ip_inode_lockres
;
2618 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2620 mlog(0, "inode %llu drop %s META lock\n",
2621 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2622 ex
? "EXMODE" : "PRMODE");
2624 if (!ocfs2_is_hard_readonly(osb
) &&
2625 !ocfs2_mount_local(osb
))
2626 ocfs2_cluster_unlock(osb
, lockres
, level
);
2630 * This _tracker variantes are introduced to deal with the recursive cluster
2631 * locking issue. The idea is to keep track of a lock holder on the stack of
2632 * the current process. If there's a lock holder on the stack, we know the
2633 * task context is already protected by cluster locking. Currently, they're
2634 * used in some VFS entry routines.
2636 * return < 0 on error, return == 0 if there's no lock holder on the stack
2637 * before this call, return == 1 if this call would be a recursive locking.
2638 * return == -1 if this lock attempt will cause an upgrade which is forbidden.
2640 * When taking lock levels into account,we face some different situations.
2642 * 1. no lock is held
2643 * In this case, just lock the inode as requested and return 0
2645 * 2. We are holding a lock
2646 * For this situation, things diverges into several cases
2648 * wanted holding what to do
2649 * ex ex see 2.1 below
2650 * ex pr see 2.2 below
2651 * pr ex see 2.1 below
2652 * pr pr see 2.1 below
2654 * 2.1 lock level that is been held is compatible
2655 * with the wanted level, so no lock action will be tacken.
2657 * 2.2 Otherwise, an upgrade is needed, but it is forbidden.
2659 * Reason why upgrade within a process is forbidden is that
2660 * lock upgrade may cause dead lock. The following illustrates
2663 * thread on node1 thread on node2
2664 * ocfs2_inode_lock_tracker(ex=0)
2666 * <====== ocfs2_inode_lock_tracker(ex=1)
2668 * ocfs2_inode_lock_tracker(ex=1)
2670 int ocfs2_inode_lock_tracker(struct inode
*inode
,
2671 struct buffer_head
**ret_bh
,
2673 struct ocfs2_lock_holder
*oh
)
2676 struct ocfs2_lock_res
*lockres
;
2677 struct ocfs2_lock_holder
*tmp_oh
;
2678 struct pid
*pid
= task_pid(current
);
2681 lockres
= &OCFS2_I(inode
)->ip_inode_lockres
;
2682 tmp_oh
= ocfs2_pid_holder(lockres
, pid
);
2686 * This corresponds to the case 1.
2687 * We haven't got any lock before.
2689 status
= ocfs2_inode_lock_full(inode
, ret_bh
, ex
, 0);
2691 if (status
!= -ENOENT
)
2697 ocfs2_add_holder(lockres
, oh
);
2701 if (unlikely(ex
&& !tmp_oh
->oh_ex
)) {
2703 * case 2.2 upgrade may cause dead lock, forbid it.
2705 mlog(ML_ERROR
, "Recursive locking is not permitted to "
2706 "upgrade to EX level from PR level.\n");
2712 * case 2.1 OCFS2_META_LOCK_GETBH flag make ocfs2_inode_lock_full.
2713 * ignore the lock level and just update it.
2716 status
= ocfs2_inode_lock_full(inode
, ret_bh
, ex
,
2717 OCFS2_META_LOCK_GETBH
);
2719 if (status
!= -ENOENT
)
2724 return tmp_oh
? 1 : 0;
2727 void ocfs2_inode_unlock_tracker(struct inode
*inode
,
2729 struct ocfs2_lock_holder
*oh
,
2732 struct ocfs2_lock_res
*lockres
;
2734 lockres
= &OCFS2_I(inode
)->ip_inode_lockres
;
2735 /* had_lock means that the currect process already takes the cluster
2737 * If had_lock is 1, we have nothing to do here.
2738 * If had_lock is 0, we will release the lock.
2741 ocfs2_inode_unlock(inode
, oh
->oh_ex
);
2742 ocfs2_remove_holder(lockres
, oh
);
2746 int ocfs2_orphan_scan_lock(struct ocfs2_super
*osb
, u32
*seqno
)
2748 struct ocfs2_lock_res
*lockres
;
2749 struct ocfs2_orphan_scan_lvb
*lvb
;
2752 if (ocfs2_is_hard_readonly(osb
))
2755 if (ocfs2_mount_local(osb
))
2758 lockres
= &osb
->osb_orphan_scan
.os_lockres
;
2759 status
= ocfs2_cluster_lock(osb
, lockres
, DLM_LOCK_EX
, 0, 0);
2763 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
2764 if (ocfs2_dlm_lvb_valid(&lockres
->l_lksb
) &&
2765 lvb
->lvb_version
== OCFS2_ORPHAN_LVB_VERSION
)
2766 *seqno
= be32_to_cpu(lvb
->lvb_os_seqno
);
2768 *seqno
= osb
->osb_orphan_scan
.os_seqno
+ 1;
2773 void ocfs2_orphan_scan_unlock(struct ocfs2_super
*osb
, u32 seqno
)
2775 struct ocfs2_lock_res
*lockres
;
2776 struct ocfs2_orphan_scan_lvb
*lvb
;
2778 if (!ocfs2_is_hard_readonly(osb
) && !ocfs2_mount_local(osb
)) {
2779 lockres
= &osb
->osb_orphan_scan
.os_lockres
;
2780 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
2781 lvb
->lvb_version
= OCFS2_ORPHAN_LVB_VERSION
;
2782 lvb
->lvb_os_seqno
= cpu_to_be32(seqno
);
2783 ocfs2_cluster_unlock(osb
, lockres
, DLM_LOCK_EX
);
2787 int ocfs2_super_lock(struct ocfs2_super
*osb
,
2791 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2792 struct ocfs2_lock_res
*lockres
= &osb
->osb_super_lockres
;
2794 if (ocfs2_is_hard_readonly(osb
))
2797 if (ocfs2_mount_local(osb
))
2800 status
= ocfs2_cluster_lock(osb
, lockres
, level
, 0, 0);
2806 /* The super block lock path is really in the best position to
2807 * know when resources covered by the lock need to be
2808 * refreshed, so we do it here. Of course, making sense of
2809 * everything is up to the caller :) */
2810 status
= ocfs2_should_refresh_lock_res(lockres
);
2812 status
= ocfs2_refresh_slot_info(osb
);
2814 ocfs2_complete_lock_res_refresh(lockres
, status
);
2817 ocfs2_cluster_unlock(osb
, lockres
, level
);
2820 ocfs2_track_lock_refresh(lockres
);
2826 void ocfs2_super_unlock(struct ocfs2_super
*osb
,
2829 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2830 struct ocfs2_lock_res
*lockres
= &osb
->osb_super_lockres
;
2832 if (!ocfs2_mount_local(osb
))
2833 ocfs2_cluster_unlock(osb
, lockres
, level
);
2836 int ocfs2_rename_lock(struct ocfs2_super
*osb
)
2839 struct ocfs2_lock_res
*lockres
= &osb
->osb_rename_lockres
;
2841 if (ocfs2_is_hard_readonly(osb
))
2844 if (ocfs2_mount_local(osb
))
2847 status
= ocfs2_cluster_lock(osb
, lockres
, DLM_LOCK_EX
, 0, 0);
2854 void ocfs2_rename_unlock(struct ocfs2_super
*osb
)
2856 struct ocfs2_lock_res
*lockres
= &osb
->osb_rename_lockres
;
2858 if (!ocfs2_mount_local(osb
))
2859 ocfs2_cluster_unlock(osb
, lockres
, DLM_LOCK_EX
);
2862 int ocfs2_nfs_sync_lock(struct ocfs2_super
*osb
, int ex
)
2865 struct ocfs2_lock_res
*lockres
= &osb
->osb_nfs_sync_lockres
;
2867 if (ocfs2_is_hard_readonly(osb
))
2871 down_write(&osb
->nfs_sync_rwlock
);
2873 down_read(&osb
->nfs_sync_rwlock
);
2875 if (ocfs2_mount_local(osb
))
2878 status
= ocfs2_cluster_lock(osb
, lockres
, ex
? LKM_EXMODE
: LKM_PRMODE
,
2881 mlog(ML_ERROR
, "lock on nfs sync lock failed %d\n", status
);
2884 up_write(&osb
->nfs_sync_rwlock
);
2886 up_read(&osb
->nfs_sync_rwlock
);
2892 void ocfs2_nfs_sync_unlock(struct ocfs2_super
*osb
, int ex
)
2894 struct ocfs2_lock_res
*lockres
= &osb
->osb_nfs_sync_lockres
;
2896 if (!ocfs2_mount_local(osb
))
2897 ocfs2_cluster_unlock(osb
, lockres
,
2898 ex
? LKM_EXMODE
: LKM_PRMODE
);
2900 up_write(&osb
->nfs_sync_rwlock
);
2902 up_read(&osb
->nfs_sync_rwlock
);
2905 int ocfs2_trim_fs_lock(struct ocfs2_super
*osb
,
2906 struct ocfs2_trim_fs_info
*info
, int trylock
)
2909 struct ocfs2_trim_fs_lvb
*lvb
;
2910 struct ocfs2_lock_res
*lockres
= &osb
->osb_trim_fs_lockres
;
2915 if (ocfs2_is_hard_readonly(osb
))
2918 if (ocfs2_mount_local(osb
))
2921 status
= ocfs2_cluster_lock(osb
, lockres
, DLM_LOCK_EX
,
2922 trylock
? DLM_LKF_NOQUEUE
: 0, 0);
2924 if (status
!= -EAGAIN
)
2930 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
2931 if (ocfs2_dlm_lvb_valid(&lockres
->l_lksb
) &&
2932 lvb
->lvb_version
== OCFS2_TRIMFS_LVB_VERSION
) {
2934 info
->tf_success
= lvb
->lvb_success
;
2935 info
->tf_nodenum
= be32_to_cpu(lvb
->lvb_nodenum
);
2936 info
->tf_start
= be64_to_cpu(lvb
->lvb_start
);
2937 info
->tf_len
= be64_to_cpu(lvb
->lvb_len
);
2938 info
->tf_minlen
= be64_to_cpu(lvb
->lvb_minlen
);
2939 info
->tf_trimlen
= be64_to_cpu(lvb
->lvb_trimlen
);
2946 void ocfs2_trim_fs_unlock(struct ocfs2_super
*osb
,
2947 struct ocfs2_trim_fs_info
*info
)
2949 struct ocfs2_trim_fs_lvb
*lvb
;
2950 struct ocfs2_lock_res
*lockres
= &osb
->osb_trim_fs_lockres
;
2952 if (ocfs2_mount_local(osb
))
2956 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
2957 lvb
->lvb_version
= OCFS2_TRIMFS_LVB_VERSION
;
2958 lvb
->lvb_success
= info
->tf_success
;
2959 lvb
->lvb_nodenum
= cpu_to_be32(info
->tf_nodenum
);
2960 lvb
->lvb_start
= cpu_to_be64(info
->tf_start
);
2961 lvb
->lvb_len
= cpu_to_be64(info
->tf_len
);
2962 lvb
->lvb_minlen
= cpu_to_be64(info
->tf_minlen
);
2963 lvb
->lvb_trimlen
= cpu_to_be64(info
->tf_trimlen
);
2966 ocfs2_cluster_unlock(osb
, lockres
, DLM_LOCK_EX
);
2969 int ocfs2_dentry_lock(struct dentry
*dentry
, int ex
)
2972 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2973 struct ocfs2_dentry_lock
*dl
= dentry
->d_fsdata
;
2974 struct ocfs2_super
*osb
= OCFS2_SB(dentry
->d_sb
);
2978 if (ocfs2_is_hard_readonly(osb
)) {
2984 if (ocfs2_mount_local(osb
))
2987 ret
= ocfs2_cluster_lock(osb
, &dl
->dl_lockres
, level
, 0, 0);
2994 void ocfs2_dentry_unlock(struct dentry
*dentry
, int ex
)
2996 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2997 struct ocfs2_dentry_lock
*dl
= dentry
->d_fsdata
;
2998 struct ocfs2_super
*osb
= OCFS2_SB(dentry
->d_sb
);
3000 if (!ocfs2_is_hard_readonly(osb
) && !ocfs2_mount_local(osb
))
3001 ocfs2_cluster_unlock(osb
, &dl
->dl_lockres
, level
);
3004 /* Reference counting of the dlm debug structure. We want this because
3005 * open references on the debug inodes can live on after a mount, so
3006 * we can't rely on the ocfs2_super to always exist. */
3007 static void ocfs2_dlm_debug_free(struct kref
*kref
)
3009 struct ocfs2_dlm_debug
*dlm_debug
;
3011 dlm_debug
= container_of(kref
, struct ocfs2_dlm_debug
, d_refcnt
);
3016 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug
*dlm_debug
)
3019 kref_put(&dlm_debug
->d_refcnt
, ocfs2_dlm_debug_free
);
3022 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug
*debug
)
3024 kref_get(&debug
->d_refcnt
);
3027 struct ocfs2_dlm_debug
*ocfs2_new_dlm_debug(void)
3029 struct ocfs2_dlm_debug
*dlm_debug
;
3031 dlm_debug
= kmalloc(sizeof(struct ocfs2_dlm_debug
), GFP_KERNEL
);
3033 mlog_errno(-ENOMEM
);
3037 kref_init(&dlm_debug
->d_refcnt
);
3038 INIT_LIST_HEAD(&dlm_debug
->d_lockres_tracking
);
3039 dlm_debug
->d_filter_secs
= 0;
3044 /* Access to this is arbitrated for us via seq_file->sem. */
3045 struct ocfs2_dlm_seq_priv
{
3046 struct ocfs2_dlm_debug
*p_dlm_debug
;
3047 struct ocfs2_lock_res p_iter_res
;
3048 struct ocfs2_lock_res p_tmp_res
;
3051 static struct ocfs2_lock_res
*ocfs2_dlm_next_res(struct ocfs2_lock_res
*start
,
3052 struct ocfs2_dlm_seq_priv
*priv
)
3054 struct ocfs2_lock_res
*iter
, *ret
= NULL
;
3055 struct ocfs2_dlm_debug
*dlm_debug
= priv
->p_dlm_debug
;
3057 assert_spin_locked(&ocfs2_dlm_tracking_lock
);
3059 list_for_each_entry(iter
, &start
->l_debug_list
, l_debug_list
) {
3060 /* discover the head of the list */
3061 if (&iter
->l_debug_list
== &dlm_debug
->d_lockres_tracking
) {
3062 mlog(0, "End of list found, %p\n", ret
);
3066 /* We track our "dummy" iteration lockres' by a NULL
3068 if (iter
->l_ops
!= NULL
) {
3077 static void *ocfs2_dlm_seq_start(struct seq_file
*m
, loff_t
*pos
)
3079 struct ocfs2_dlm_seq_priv
*priv
= m
->private;
3080 struct ocfs2_lock_res
*iter
;
3082 spin_lock(&ocfs2_dlm_tracking_lock
);
3083 iter
= ocfs2_dlm_next_res(&priv
->p_iter_res
, priv
);
3085 /* Since lockres' have the lifetime of their container
3086 * (which can be inodes, ocfs2_supers, etc) we want to
3087 * copy this out to a temporary lockres while still
3088 * under the spinlock. Obviously after this we can't
3089 * trust any pointers on the copy returned, but that's
3090 * ok as the information we want isn't typically held
3092 priv
->p_tmp_res
= *iter
;
3093 iter
= &priv
->p_tmp_res
;
3095 spin_unlock(&ocfs2_dlm_tracking_lock
);
3100 static void ocfs2_dlm_seq_stop(struct seq_file
*m
, void *v
)
3104 static void *ocfs2_dlm_seq_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3106 struct ocfs2_dlm_seq_priv
*priv
= m
->private;
3107 struct ocfs2_lock_res
*iter
= v
;
3108 struct ocfs2_lock_res
*dummy
= &priv
->p_iter_res
;
3110 spin_lock(&ocfs2_dlm_tracking_lock
);
3111 iter
= ocfs2_dlm_next_res(iter
, priv
);
3112 list_del_init(&dummy
->l_debug_list
);
3114 list_add(&dummy
->l_debug_list
, &iter
->l_debug_list
);
3115 priv
->p_tmp_res
= *iter
;
3116 iter
= &priv
->p_tmp_res
;
3118 spin_unlock(&ocfs2_dlm_tracking_lock
);
3124 * Version is used by debugfs.ocfs2 to determine the format being used
3127 * - Lock stats printed
3129 * - Max time in lock stats is in usecs (instead of nsecs)
3131 * - Add last pr/ex unlock times and first lock wait time in usecs
3133 #define OCFS2_DLM_DEBUG_STR_VERSION 4
3134 static int ocfs2_dlm_seq_show(struct seq_file
*m
, void *v
)
3138 struct ocfs2_lock_res
*lockres
= v
;
3139 #ifdef CONFIG_OCFS2_FS_STATS
3141 struct ocfs2_dlm_debug
*dlm_debug
=
3142 ((struct ocfs2_dlm_seq_priv
*)m
->private)->p_dlm_debug
;
3148 #ifdef CONFIG_OCFS2_FS_STATS
3149 if (!lockres
->l_lock_wait
&& dlm_debug
->d_filter_secs
) {
3150 now
= ktime_to_us(ktime_get_real());
3151 if (lockres
->l_lock_prmode
.ls_last
>
3152 lockres
->l_lock_exmode
.ls_last
)
3153 last
= lockres
->l_lock_prmode
.ls_last
;
3155 last
= lockres
->l_lock_exmode
.ls_last
;
3157 * Use d_filter_secs field to filter lock resources dump,
3158 * the default d_filter_secs(0) value filters nothing,
3159 * otherwise, only dump the last N seconds active lock
3162 if (div_u64(now
- last
, 1000000) > dlm_debug
->d_filter_secs
)
3167 seq_printf(m
, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION
);
3169 if (lockres
->l_type
== OCFS2_LOCK_TYPE_DENTRY
)
3170 seq_printf(m
, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START
- 1,
3172 (unsigned int)ocfs2_get_dentry_lock_ino(lockres
));
3174 seq_printf(m
, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN
, lockres
->l_name
);
3176 seq_printf(m
, "%d\t"
3187 lockres
->l_unlock_action
,
3188 lockres
->l_ro_holders
,
3189 lockres
->l_ex_holders
,
3190 lockres
->l_requested
,
3191 lockres
->l_blocking
);
3193 /* Dump the raw LVB */
3194 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
3195 for(i
= 0; i
< DLM_LVB_LEN
; i
++)
3196 seq_printf(m
, "0x%x\t", lvb
[i
]);
3198 #ifdef CONFIG_OCFS2_FS_STATS
3199 # define lock_num_prmode(_l) ((_l)->l_lock_prmode.ls_gets)
3200 # define lock_num_exmode(_l) ((_l)->l_lock_exmode.ls_gets)
3201 # define lock_num_prmode_failed(_l) ((_l)->l_lock_prmode.ls_fail)
3202 # define lock_num_exmode_failed(_l) ((_l)->l_lock_exmode.ls_fail)
3203 # define lock_total_prmode(_l) ((_l)->l_lock_prmode.ls_total)
3204 # define lock_total_exmode(_l) ((_l)->l_lock_exmode.ls_total)
3205 # define lock_max_prmode(_l) ((_l)->l_lock_prmode.ls_max)
3206 # define lock_max_exmode(_l) ((_l)->l_lock_exmode.ls_max)
3207 # define lock_refresh(_l) ((_l)->l_lock_refresh)
3208 # define lock_last_prmode(_l) ((_l)->l_lock_prmode.ls_last)
3209 # define lock_last_exmode(_l) ((_l)->l_lock_exmode.ls_last)
3210 # define lock_wait(_l) ((_l)->l_lock_wait)
3212 # define lock_num_prmode(_l) (0)
3213 # define lock_num_exmode(_l) (0)
3214 # define lock_num_prmode_failed(_l) (0)
3215 # define lock_num_exmode_failed(_l) (0)
3216 # define lock_total_prmode(_l) (0ULL)
3217 # define lock_total_exmode(_l) (0ULL)
3218 # define lock_max_prmode(_l) (0)
3219 # define lock_max_exmode(_l) (0)
3220 # define lock_refresh(_l) (0)
3221 # define lock_last_prmode(_l) (0ULL)
3222 # define lock_last_exmode(_l) (0ULL)
3223 # define lock_wait(_l) (0ULL)
3225 /* The following seq_print was added in version 2 of this output */
3226 seq_printf(m
, "%u\t"
3238 lock_num_prmode(lockres
),
3239 lock_num_exmode(lockres
),
3240 lock_num_prmode_failed(lockres
),
3241 lock_num_exmode_failed(lockres
),
3242 lock_total_prmode(lockres
),
3243 lock_total_exmode(lockres
),
3244 lock_max_prmode(lockres
),
3245 lock_max_exmode(lockres
),
3246 lock_refresh(lockres
),
3247 lock_last_prmode(lockres
),
3248 lock_last_exmode(lockres
),
3249 lock_wait(lockres
));
3252 seq_printf(m
, "\n");
3256 static const struct seq_operations ocfs2_dlm_seq_ops
= {
3257 .start
= ocfs2_dlm_seq_start
,
3258 .stop
= ocfs2_dlm_seq_stop
,
3259 .next
= ocfs2_dlm_seq_next
,
3260 .show
= ocfs2_dlm_seq_show
,
3263 static int ocfs2_dlm_debug_release(struct inode
*inode
, struct file
*file
)
3265 struct seq_file
*seq
= file
->private_data
;
3266 struct ocfs2_dlm_seq_priv
*priv
= seq
->private;
3267 struct ocfs2_lock_res
*res
= &priv
->p_iter_res
;
3269 ocfs2_remove_lockres_tracking(res
);
3270 ocfs2_put_dlm_debug(priv
->p_dlm_debug
);
3271 return seq_release_private(inode
, file
);
3274 static int ocfs2_dlm_debug_open(struct inode
*inode
, struct file
*file
)
3276 struct ocfs2_dlm_seq_priv
*priv
;
3277 struct ocfs2_super
*osb
;
3279 priv
= __seq_open_private(file
, &ocfs2_dlm_seq_ops
, sizeof(*priv
));
3281 mlog_errno(-ENOMEM
);
3285 osb
= inode
->i_private
;
3286 ocfs2_get_dlm_debug(osb
->osb_dlm_debug
);
3287 priv
->p_dlm_debug
= osb
->osb_dlm_debug
;
3288 INIT_LIST_HEAD(&priv
->p_iter_res
.l_debug_list
);
3290 ocfs2_add_lockres_tracking(&priv
->p_iter_res
,
3296 static const struct file_operations ocfs2_dlm_debug_fops
= {
3297 .open
= ocfs2_dlm_debug_open
,
3298 .release
= ocfs2_dlm_debug_release
,
3300 .llseek
= seq_lseek
,
3303 static void ocfs2_dlm_init_debug(struct ocfs2_super
*osb
)
3305 struct ocfs2_dlm_debug
*dlm_debug
= osb
->osb_dlm_debug
;
3307 debugfs_create_file("locking_state", S_IFREG
|S_IRUSR
,
3308 osb
->osb_debug_root
, osb
, &ocfs2_dlm_debug_fops
);
3310 debugfs_create_u32("locking_filter", 0600, osb
->osb_debug_root
,
3311 &dlm_debug
->d_filter_secs
);
3312 ocfs2_get_dlm_debug(dlm_debug
);
3315 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super
*osb
)
3317 struct ocfs2_dlm_debug
*dlm_debug
= osb
->osb_dlm_debug
;
3320 ocfs2_put_dlm_debug(dlm_debug
);
3323 int ocfs2_dlm_init(struct ocfs2_super
*osb
)
3326 struct ocfs2_cluster_connection
*conn
= NULL
;
3328 if (ocfs2_mount_local(osb
)) {
3333 ocfs2_dlm_init_debug(osb
);
3335 /* launch downconvert thread */
3336 osb
->dc_task
= kthread_run(ocfs2_downconvert_thread
, osb
, "ocfs2dc-%s",
3338 if (IS_ERR(osb
->dc_task
)) {
3339 status
= PTR_ERR(osb
->dc_task
);
3340 osb
->dc_task
= NULL
;
3345 /* for now, uuid == domain */
3346 status
= ocfs2_cluster_connect(osb
->osb_cluster_stack
,
3347 osb
->osb_cluster_name
,
3348 strlen(osb
->osb_cluster_name
),
3350 strlen(osb
->uuid_str
),
3351 &lproto
, ocfs2_do_node_down
, osb
,
3358 status
= ocfs2_cluster_this_node(conn
, &osb
->node_num
);
3362 "could not find this host's node number\n");
3363 ocfs2_cluster_disconnect(conn
, 0);
3368 ocfs2_super_lock_res_init(&osb
->osb_super_lockres
, osb
);
3369 ocfs2_rename_lock_res_init(&osb
->osb_rename_lockres
, osb
);
3370 ocfs2_nfs_sync_lock_init(osb
);
3371 ocfs2_orphan_scan_lock_res_init(&osb
->osb_orphan_scan
.os_lockres
, osb
);
3376 ocfs2_dlm_shutdown_debug(osb
);
3378 kthread_stop(osb
->dc_task
);
3384 void ocfs2_dlm_shutdown(struct ocfs2_super
*osb
,
3387 ocfs2_drop_osb_locks(osb
);
3390 * Now that we have dropped all locks and ocfs2_dismount_volume()
3391 * has disabled recovery, the DLM won't be talking to us. It's
3392 * safe to tear things down before disconnecting the cluster.
3396 kthread_stop(osb
->dc_task
);
3397 osb
->dc_task
= NULL
;
3400 ocfs2_lock_res_free(&osb
->osb_super_lockres
);
3401 ocfs2_lock_res_free(&osb
->osb_rename_lockres
);
3402 ocfs2_lock_res_free(&osb
->osb_nfs_sync_lockres
);
3403 ocfs2_lock_res_free(&osb
->osb_orphan_scan
.os_lockres
);
3405 ocfs2_cluster_disconnect(osb
->cconn
, hangup_pending
);
3408 ocfs2_dlm_shutdown_debug(osb
);
3411 static int ocfs2_drop_lock(struct ocfs2_super
*osb
,
3412 struct ocfs2_lock_res
*lockres
)
3415 unsigned long flags
;
3418 /* We didn't get anywhere near actually using this lockres. */
3419 if (!(lockres
->l_flags
& OCFS2_LOCK_INITIALIZED
))
3422 if (lockres
->l_ops
->flags
& LOCK_TYPE_USES_LVB
)
3423 lkm_flags
|= DLM_LKF_VALBLK
;
3425 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3427 mlog_bug_on_msg(!(lockres
->l_flags
& OCFS2_LOCK_FREEING
),
3428 "lockres %s, flags 0x%lx\n",
3429 lockres
->l_name
, lockres
->l_flags
);
3431 while (lockres
->l_flags
& OCFS2_LOCK_BUSY
) {
3432 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
3433 "%u, unlock_action = %u\n",
3434 lockres
->l_name
, lockres
->l_flags
, lockres
->l_action
,
3435 lockres
->l_unlock_action
);
3437 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3439 /* XXX: Today we just wait on any busy
3440 * locks... Perhaps we need to cancel converts in the
3442 ocfs2_wait_on_busy_lock(lockres
);
3444 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3447 if (lockres
->l_ops
->flags
& LOCK_TYPE_USES_LVB
) {
3448 if (lockres
->l_flags
& OCFS2_LOCK_ATTACHED
&&
3449 lockres
->l_level
== DLM_LOCK_EX
&&
3450 !(lockres
->l_flags
& OCFS2_LOCK_NEEDS_REFRESH
))
3451 lockres
->l_ops
->set_lvb(lockres
);
3454 if (lockres
->l_flags
& OCFS2_LOCK_BUSY
)
3455 mlog(ML_ERROR
, "destroying busy lock: \"%s\"\n",
3457 if (lockres
->l_flags
& OCFS2_LOCK_BLOCKED
)
3458 mlog(0, "destroying blocked lock: \"%s\"\n", lockres
->l_name
);
3460 if (!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
)) {
3461 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3465 lockres_clear_flags(lockres
, OCFS2_LOCK_ATTACHED
);
3467 /* make sure we never get here while waiting for an ast to
3469 BUG_ON(lockres
->l_action
!= OCFS2_AST_INVALID
);
3471 /* is this necessary? */
3472 lockres_or_flags(lockres
, OCFS2_LOCK_BUSY
);
3473 lockres
->l_unlock_action
= OCFS2_UNLOCK_DROP_LOCK
;
3474 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3476 mlog(0, "lock %s\n", lockres
->l_name
);
3478 ret
= ocfs2_dlm_unlock(osb
->cconn
, &lockres
->l_lksb
, lkm_flags
);
3480 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret
, lockres
);
3481 mlog(ML_ERROR
, "lockres flags: %lu\n", lockres
->l_flags
);
3482 ocfs2_dlm_dump_lksb(&lockres
->l_lksb
);
3485 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
3488 ocfs2_wait_on_busy_lock(lockres
);
3493 static void ocfs2_process_blocked_lock(struct ocfs2_super
*osb
,
3494 struct ocfs2_lock_res
*lockres
);
3496 /* Mark the lockres as being dropped. It will no longer be
3497 * queued if blocking, but we still may have to wait on it
3498 * being dequeued from the downconvert thread before we can consider
3501 * You can *not* attempt to call cluster_lock on this lockres anymore. */
3502 void ocfs2_mark_lockres_freeing(struct ocfs2_super
*osb
,
3503 struct ocfs2_lock_res
*lockres
)
3506 struct ocfs2_mask_waiter mw
;
3507 unsigned long flags
, flags2
;
3509 ocfs2_init_mask_waiter(&mw
);
3511 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3512 lockres
->l_flags
|= OCFS2_LOCK_FREEING
;
3513 if (lockres
->l_flags
& OCFS2_LOCK_QUEUED
&& current
== osb
->dc_task
) {
3515 * We know the downconvert is queued but not in progress
3516 * because we are the downconvert thread and processing
3517 * different lock. So we can just remove the lock from the
3518 * queue. This is not only an optimization but also a way
3519 * to avoid the following deadlock:
3520 * ocfs2_dentry_post_unlock()
3521 * ocfs2_dentry_lock_put()
3522 * ocfs2_drop_dentry_lock()
3524 * ocfs2_evict_inode()
3525 * ocfs2_clear_inode()
3526 * ocfs2_mark_lockres_freeing()
3527 * ... blocks waiting for OCFS2_LOCK_QUEUED
3528 * since we are the downconvert thread which
3529 * should clear the flag.
3531 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3532 spin_lock_irqsave(&osb
->dc_task_lock
, flags2
);
3533 list_del_init(&lockres
->l_blocked_list
);
3534 osb
->blocked_lock_count
--;
3535 spin_unlock_irqrestore(&osb
->dc_task_lock
, flags2
);
3537 * Warn if we recurse into another post_unlock call. Strictly
3538 * speaking it isn't a problem but we need to be careful if
3539 * that happens (stack overflow, deadlocks, ...) so warn if
3540 * ocfs2 grows a path for which this can happen.
3542 WARN_ON_ONCE(lockres
->l_ops
->post_unlock
);
3543 /* Since the lock is freeing we don't do much in the fn below */
3544 ocfs2_process_blocked_lock(osb
, lockres
);
3547 while (lockres
->l_flags
& OCFS2_LOCK_QUEUED
) {
3548 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_QUEUED
, 0);
3549 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3551 mlog(0, "Waiting on lockres %s\n", lockres
->l_name
);
3553 status
= ocfs2_wait_for_mask(&mw
);
3557 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3559 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3562 void ocfs2_simple_drop_lockres(struct ocfs2_super
*osb
,
3563 struct ocfs2_lock_res
*lockres
)
3567 ocfs2_mark_lockres_freeing(osb
, lockres
);
3568 ret
= ocfs2_drop_lock(osb
, lockres
);
3573 static void ocfs2_drop_osb_locks(struct ocfs2_super
*osb
)
3575 ocfs2_simple_drop_lockres(osb
, &osb
->osb_super_lockres
);
3576 ocfs2_simple_drop_lockres(osb
, &osb
->osb_rename_lockres
);
3577 ocfs2_simple_drop_lockres(osb
, &osb
->osb_nfs_sync_lockres
);
3578 ocfs2_simple_drop_lockres(osb
, &osb
->osb_orphan_scan
.os_lockres
);
3581 int ocfs2_drop_inode_locks(struct inode
*inode
)
3585 /* No need to call ocfs2_mark_lockres_freeing here -
3586 * ocfs2_clear_inode has done it for us. */
3588 err
= ocfs2_drop_lock(OCFS2_SB(inode
->i_sb
),
3589 &OCFS2_I(inode
)->ip_open_lockres
);
3595 err
= ocfs2_drop_lock(OCFS2_SB(inode
->i_sb
),
3596 &OCFS2_I(inode
)->ip_inode_lockres
);
3599 if (err
< 0 && !status
)
3602 err
= ocfs2_drop_lock(OCFS2_SB(inode
->i_sb
),
3603 &OCFS2_I(inode
)->ip_rw_lockres
);
3606 if (err
< 0 && !status
)
3612 static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res
*lockres
,
3615 assert_spin_locked(&lockres
->l_lock
);
3617 BUG_ON(lockres
->l_blocking
<= DLM_LOCK_NL
);
3619 if (lockres
->l_level
<= new_level
) {
3620 mlog(ML_ERROR
, "lockres %s, lvl %d <= %d, blcklst %d, mask %d, "
3621 "type %d, flags 0x%lx, hold %d %d, act %d %d, req %d, "
3622 "block %d, pgen %d\n", lockres
->l_name
, lockres
->l_level
,
3623 new_level
, list_empty(&lockres
->l_blocked_list
),
3624 list_empty(&lockres
->l_mask_waiters
), lockres
->l_type
,
3625 lockres
->l_flags
, lockres
->l_ro_holders
,
3626 lockres
->l_ex_holders
, lockres
->l_action
,
3627 lockres
->l_unlock_action
, lockres
->l_requested
,
3628 lockres
->l_blocking
, lockres
->l_pending_gen
);
3632 mlog(ML_BASTS
, "lockres %s, level %d => %d, blocking %d\n",
3633 lockres
->l_name
, lockres
->l_level
, new_level
, lockres
->l_blocking
);
3635 lockres
->l_action
= OCFS2_AST_DOWNCONVERT
;
3636 lockres
->l_requested
= new_level
;
3637 lockres_or_flags(lockres
, OCFS2_LOCK_BUSY
);
3638 return lockres_set_pending(lockres
);
3641 static int ocfs2_downconvert_lock(struct ocfs2_super
*osb
,
3642 struct ocfs2_lock_res
*lockres
,
3645 unsigned int generation
)
3648 u32 dlm_flags
= DLM_LKF_CONVERT
;
3650 mlog(ML_BASTS
, "lockres %s, level %d => %d\n", lockres
->l_name
,
3651 lockres
->l_level
, new_level
);
3654 * On DLM_LKF_VALBLK, fsdlm behaves differently with o2cb. It always
3655 * expects DLM_LKF_VALBLK being set if the LKB has LVB, so that
3656 * we can recover correctly from node failure. Otherwise, we may get
3657 * invalid LVB in LKB, but without DLM_SBF_VALNOTVALID being set.
3659 if (ocfs2_userspace_stack(osb
) &&
3660 lockres
->l_ops
->flags
& LOCK_TYPE_USES_LVB
)
3664 dlm_flags
|= DLM_LKF_VALBLK
;
3666 ret
= ocfs2_dlm_lock(osb
->cconn
,
3671 OCFS2_LOCK_ID_MAX_LEN
- 1);
3672 lockres_clear_pending(lockres
, generation
, osb
);
3674 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret
, lockres
);
3675 ocfs2_recover_from_dlm_error(lockres
, 1);
3684 /* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
3685 static int ocfs2_prepare_cancel_convert(struct ocfs2_super
*osb
,
3686 struct ocfs2_lock_res
*lockres
)
3688 assert_spin_locked(&lockres
->l_lock
);
3690 if (lockres
->l_unlock_action
== OCFS2_UNLOCK_CANCEL_CONVERT
) {
3691 /* If we're already trying to cancel a lock conversion
3692 * then just drop the spinlock and allow the caller to
3693 * requeue this lock. */
3694 mlog(ML_BASTS
, "lockres %s, skip convert\n", lockres
->l_name
);
3698 /* were we in a convert when we got the bast fire? */
3699 BUG_ON(lockres
->l_action
!= OCFS2_AST_CONVERT
&&
3700 lockres
->l_action
!= OCFS2_AST_DOWNCONVERT
);
3701 /* set things up for the unlockast to know to just
3702 * clear out the ast_action and unset busy, etc. */
3703 lockres
->l_unlock_action
= OCFS2_UNLOCK_CANCEL_CONVERT
;
3705 mlog_bug_on_msg(!(lockres
->l_flags
& OCFS2_LOCK_BUSY
),
3706 "lock %s, invalid flags: 0x%lx\n",
3707 lockres
->l_name
, lockres
->l_flags
);
3709 mlog(ML_BASTS
, "lockres %s\n", lockres
->l_name
);
3714 static int ocfs2_cancel_convert(struct ocfs2_super
*osb
,
3715 struct ocfs2_lock_res
*lockres
)
3719 ret
= ocfs2_dlm_unlock(osb
->cconn
, &lockres
->l_lksb
,
3722 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret
, lockres
);
3723 ocfs2_recover_from_dlm_error(lockres
, 0);
3726 mlog(ML_BASTS
, "lockres %s\n", lockres
->l_name
);
3731 static int ocfs2_unblock_lock(struct ocfs2_super
*osb
,
3732 struct ocfs2_lock_res
*lockres
,
3733 struct ocfs2_unblock_ctl
*ctl
)
3735 unsigned long flags
;
3743 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3747 * Is it still blocking? If not, we have no more work to do.
3749 if (!(lockres
->l_flags
& OCFS2_LOCK_BLOCKED
)) {
3750 BUG_ON(lockres
->l_blocking
!= DLM_LOCK_NL
);
3751 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3756 if (lockres
->l_flags
& OCFS2_LOCK_BUSY
) {
3758 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3759 * exists entirely for one reason - another thread has set
3760 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3762 * If we do ocfs2_cancel_convert() before the other thread
3763 * calls dlm_lock(), our cancel will do nothing. We will
3764 * get no ast, and we will have no way of knowing the
3765 * cancel failed. Meanwhile, the other thread will call
3766 * into dlm_lock() and wait...forever.
3768 * Why forever? Because another node has asked for the
3769 * lock first; that's why we're here in unblock_lock().
3771 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3772 * set, we just requeue the unblock. Only when the other
3773 * thread has called dlm_lock() and cleared PENDING will
3774 * we then cancel their request.
3776 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3777 * at the same time they set OCFS2_DLM_BUSY. They must
3778 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3780 if (lockres
->l_flags
& OCFS2_LOCK_PENDING
) {
3781 mlog(ML_BASTS
, "lockres %s, ReQ: Pending\n",
3787 ret
= ocfs2_prepare_cancel_convert(osb
, lockres
);
3788 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3790 ret
= ocfs2_cancel_convert(osb
, lockres
);
3798 * This prevents livelocks. OCFS2_LOCK_UPCONVERT_FINISHING flag is
3799 * set when the ast is received for an upconvert just before the
3800 * OCFS2_LOCK_BUSY flag is cleared. Now if the fs received a bast
3801 * on the heels of the ast, we want to delay the downconvert just
3802 * enough to allow the up requestor to do its task. Because this
3803 * lock is in the blocked queue, the lock will be downconverted
3804 * as soon as the requestor is done with the lock.
3806 if (lockres
->l_flags
& OCFS2_LOCK_UPCONVERT_FINISHING
)
3810 * How can we block and yet be at NL? We were trying to upconvert
3811 * from NL and got canceled. The code comes back here, and now
3812 * we notice and clear BLOCKING.
3814 if (lockres
->l_level
== DLM_LOCK_NL
) {
3815 BUG_ON(lockres
->l_ex_holders
|| lockres
->l_ro_holders
);
3816 mlog(ML_BASTS
, "lockres %s, Aborting dc\n", lockres
->l_name
);
3817 lockres
->l_blocking
= DLM_LOCK_NL
;
3818 lockres_clear_flags(lockres
, OCFS2_LOCK_BLOCKED
);
3819 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3823 /* if we're blocking an exclusive and we have *any* holders,
3825 if ((lockres
->l_blocking
== DLM_LOCK_EX
)
3826 && (lockres
->l_ex_holders
|| lockres
->l_ro_holders
)) {
3827 mlog(ML_BASTS
, "lockres %s, ReQ: EX/PR Holders %u,%u\n",
3828 lockres
->l_name
, lockres
->l_ex_holders
,
3829 lockres
->l_ro_holders
);
3833 /* If it's a PR we're blocking, then only
3834 * requeue if we've got any EX holders */
3835 if (lockres
->l_blocking
== DLM_LOCK_PR
&&
3836 lockres
->l_ex_holders
) {
3837 mlog(ML_BASTS
, "lockres %s, ReQ: EX Holders %u\n",
3838 lockres
->l_name
, lockres
->l_ex_holders
);
3843 * Can we get a lock in this state if the holder counts are
3844 * zero? The meta data unblock code used to check this.
3846 if ((lockres
->l_ops
->flags
& LOCK_TYPE_REQUIRES_REFRESH
)
3847 && (lockres
->l_flags
& OCFS2_LOCK_REFRESHING
)) {
3848 mlog(ML_BASTS
, "lockres %s, ReQ: Lock Refreshing\n",
3853 new_level
= ocfs2_highest_compat_lock_level(lockres
->l_blocking
);
3855 if (lockres
->l_ops
->check_downconvert
3856 && !lockres
->l_ops
->check_downconvert(lockres
, new_level
)) {
3857 mlog(ML_BASTS
, "lockres %s, ReQ: Checkpointing\n",
3862 /* If we get here, then we know that there are no more
3863 * incompatible holders (and anyone asking for an incompatible
3864 * lock is blocked). We can now downconvert the lock */
3865 if (!lockres
->l_ops
->downconvert_worker
)
3868 /* Some lockres types want to do a bit of work before
3869 * downconverting a lock. Allow that here. The worker function
3870 * may sleep, so we save off a copy of what we're blocking as
3871 * it may change while we're not holding the spin lock. */
3872 blocking
= lockres
->l_blocking
;
3873 level
= lockres
->l_level
;
3874 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3876 ctl
->unblock_action
= lockres
->l_ops
->downconvert_worker(lockres
, blocking
);
3878 if (ctl
->unblock_action
== UNBLOCK_STOP_POST
) {
3879 mlog(ML_BASTS
, "lockres %s, UNBLOCK_STOP_POST\n",
3884 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3885 if ((blocking
!= lockres
->l_blocking
) || (level
!= lockres
->l_level
)) {
3886 /* If this changed underneath us, then we can't drop
3888 mlog(ML_BASTS
, "lockres %s, block=%d:%d, level=%d:%d, "
3889 "Recheck\n", lockres
->l_name
, blocking
,
3890 lockres
->l_blocking
, level
, lockres
->l_level
);
3897 if (lockres
->l_ops
->flags
& LOCK_TYPE_USES_LVB
) {
3898 if (lockres
->l_level
== DLM_LOCK_EX
)
3902 * We only set the lvb if the lock has been fully
3903 * refreshed - otherwise we risk setting stale
3904 * data. Otherwise, there's no need to actually clear
3905 * out the lvb here as it's value is still valid.
3907 if (set_lvb
&& !(lockres
->l_flags
& OCFS2_LOCK_NEEDS_REFRESH
))
3908 lockres
->l_ops
->set_lvb(lockres
);
3911 gen
= ocfs2_prepare_downconvert(lockres
, new_level
);
3912 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3913 ret
= ocfs2_downconvert_lock(osb
, lockres
, new_level
, set_lvb
,
3922 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3928 static int ocfs2_data_convert_worker(struct ocfs2_lock_res
*lockres
,
3931 struct inode
*inode
;
3932 struct address_space
*mapping
;
3933 struct ocfs2_inode_info
*oi
;
3935 inode
= ocfs2_lock_res_inode(lockres
);
3936 mapping
= inode
->i_mapping
;
3938 if (S_ISDIR(inode
->i_mode
)) {
3939 oi
= OCFS2_I(inode
);
3940 oi
->ip_dir_lock_gen
++;
3941 mlog(0, "generation: %u\n", oi
->ip_dir_lock_gen
);
3945 if (!S_ISREG(inode
->i_mode
))
3949 * We need this before the filemap_fdatawrite() so that it can
3950 * transfer the dirty bit from the PTE to the
3951 * page. Unfortunately this means that even for EX->PR
3952 * downconverts, we'll lose our mappings and have to build
3955 unmap_mapping_range(mapping
, 0, 0, 0);
3957 if (filemap_fdatawrite(mapping
)) {
3958 mlog(ML_ERROR
, "Could not sync inode %llu for downconvert!",
3959 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
3961 sync_mapping_buffers(mapping
);
3962 if (blocking
== DLM_LOCK_EX
) {
3963 truncate_inode_pages(mapping
, 0);
3965 /* We only need to wait on the I/O if we're not also
3966 * truncating pages because truncate_inode_pages waits
3967 * for us above. We don't truncate pages if we're
3968 * blocking anything < EXMODE because we want to keep
3969 * them around in that case. */
3970 filemap_fdatawait(mapping
);
3973 forget_all_cached_acls(inode
);
3976 return UNBLOCK_CONTINUE
;
3979 static int ocfs2_ci_checkpointed(struct ocfs2_caching_info
*ci
,
3980 struct ocfs2_lock_res
*lockres
,
3983 int checkpointed
= ocfs2_ci_fully_checkpointed(ci
);
3985 BUG_ON(new_level
!= DLM_LOCK_NL
&& new_level
!= DLM_LOCK_PR
);
3986 BUG_ON(lockres
->l_level
!= DLM_LOCK_EX
&& !checkpointed
);
3991 ocfs2_start_checkpoint(OCFS2_SB(ocfs2_metadata_cache_get_super(ci
)));
3995 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res
*lockres
,
3998 struct inode
*inode
= ocfs2_lock_res_inode(lockres
);
4000 return ocfs2_ci_checkpointed(INODE_CACHE(inode
), lockres
, new_level
);
4003 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res
*lockres
)
4005 struct inode
*inode
= ocfs2_lock_res_inode(lockres
);
4007 __ocfs2_stuff_meta_lvb(inode
);
4011 * Does the final reference drop on our dentry lock. Right now this
4012 * happens in the downconvert thread, but we could choose to simplify the
4013 * dlmglue API and push these off to the ocfs2_wq in the future.
4015 static void ocfs2_dentry_post_unlock(struct ocfs2_super
*osb
,
4016 struct ocfs2_lock_res
*lockres
)
4018 struct ocfs2_dentry_lock
*dl
= ocfs2_lock_res_dl(lockres
);
4019 ocfs2_dentry_lock_put(osb
, dl
);
4023 * d_delete() matching dentries before the lock downconvert.
4025 * At this point, any process waiting to destroy the
4026 * dentry_lock due to last ref count is stopped by the
4027 * OCFS2_LOCK_QUEUED flag.
4029 * We have two potential problems
4031 * 1) If we do the last reference drop on our dentry_lock (via dput)
4032 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
4033 * the downconvert to finish. Instead we take an elevated
4034 * reference and push the drop until after we've completed our
4035 * unblock processing.
4037 * 2) There might be another process with a final reference,
4038 * waiting on us to finish processing. If this is the case, we
4039 * detect it and exit out - there's no more dentries anyway.
4041 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res
*lockres
,
4044 struct ocfs2_dentry_lock
*dl
= ocfs2_lock_res_dl(lockres
);
4045 struct ocfs2_inode_info
*oi
= OCFS2_I(dl
->dl_inode
);
4046 struct dentry
*dentry
;
4047 unsigned long flags
;
4051 * This node is blocking another node from getting a read
4052 * lock. This happens when we've renamed within a
4053 * directory. We've forced the other nodes to d_delete(), but
4054 * we never actually dropped our lock because it's still
4055 * valid. The downconvert code will retain a PR for this node,
4056 * so there's no further work to do.
4058 if (blocking
== DLM_LOCK_PR
)
4059 return UNBLOCK_CONTINUE
;
4062 * Mark this inode as potentially orphaned. The code in
4063 * ocfs2_delete_inode() will figure out whether it actually
4064 * needs to be freed or not.
4066 spin_lock(&oi
->ip_lock
);
4067 oi
->ip_flags
|= OCFS2_INODE_MAYBE_ORPHANED
;
4068 spin_unlock(&oi
->ip_lock
);
4071 * Yuck. We need to make sure however that the check of
4072 * OCFS2_LOCK_FREEING and the extra reference are atomic with
4073 * respect to a reference decrement or the setting of that
4076 spin_lock_irqsave(&lockres
->l_lock
, flags
);
4077 spin_lock(&dentry_attach_lock
);
4078 if (!(lockres
->l_flags
& OCFS2_LOCK_FREEING
)
4083 spin_unlock(&dentry_attach_lock
);
4084 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
4086 mlog(0, "extra_ref = %d\n", extra_ref
);
4089 * We have a process waiting on us in ocfs2_dentry_iput(),
4090 * which means we can't have any more outstanding
4091 * aliases. There's no need to do any more work.
4094 return UNBLOCK_CONTINUE
;
4096 spin_lock(&dentry_attach_lock
);
4098 dentry
= ocfs2_find_local_alias(dl
->dl_inode
,
4099 dl
->dl_parent_blkno
, 1);
4102 spin_unlock(&dentry_attach_lock
);
4104 if (S_ISDIR(dl
->dl_inode
->i_mode
))
4105 shrink_dcache_parent(dentry
);
4107 mlog(0, "d_delete(%pd);\n", dentry
);
4110 * The following dcache calls may do an
4111 * iput(). Normally we don't want that from the
4112 * downconverting thread, but in this case it's ok
4113 * because the requesting node already has an
4114 * exclusive lock on the inode, so it can't be queued
4115 * for a downconvert.
4120 spin_lock(&dentry_attach_lock
);
4122 spin_unlock(&dentry_attach_lock
);
4125 * If we are the last holder of this dentry lock, there is no
4126 * reason to downconvert so skip straight to the unlock.
4128 if (dl
->dl_count
== 1)
4129 return UNBLOCK_STOP_POST
;
4131 return UNBLOCK_CONTINUE_POST
;
4134 static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res
*lockres
,
4137 struct ocfs2_refcount_tree
*tree
=
4138 ocfs2_lock_res_refcount_tree(lockres
);
4140 return ocfs2_ci_checkpointed(&tree
->rf_ci
, lockres
, new_level
);
4143 static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res
*lockres
,
4146 struct ocfs2_refcount_tree
*tree
=
4147 ocfs2_lock_res_refcount_tree(lockres
);
4149 ocfs2_metadata_cache_purge(&tree
->rf_ci
);
4151 return UNBLOCK_CONTINUE
;
4154 static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res
*lockres
)
4156 struct ocfs2_qinfo_lvb
*lvb
;
4157 struct ocfs2_mem_dqinfo
*oinfo
= ocfs2_lock_res_qinfo(lockres
);
4158 struct mem_dqinfo
*info
= sb_dqinfo(oinfo
->dqi_gi
.dqi_sb
,
4159 oinfo
->dqi_gi
.dqi_type
);
4161 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
4162 lvb
->lvb_version
= OCFS2_QINFO_LVB_VERSION
;
4163 lvb
->lvb_bgrace
= cpu_to_be32(info
->dqi_bgrace
);
4164 lvb
->lvb_igrace
= cpu_to_be32(info
->dqi_igrace
);
4165 lvb
->lvb_syncms
= cpu_to_be32(oinfo
->dqi_syncms
);
4166 lvb
->lvb_blocks
= cpu_to_be32(oinfo
->dqi_gi
.dqi_blocks
);
4167 lvb
->lvb_free_blk
= cpu_to_be32(oinfo
->dqi_gi
.dqi_free_blk
);
4168 lvb
->lvb_free_entry
= cpu_to_be32(oinfo
->dqi_gi
.dqi_free_entry
);
4171 void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo
*oinfo
, int ex
)
4173 struct ocfs2_lock_res
*lockres
= &oinfo
->dqi_gqlock
;
4174 struct ocfs2_super
*osb
= OCFS2_SB(oinfo
->dqi_gi
.dqi_sb
);
4175 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
4177 if (!ocfs2_is_hard_readonly(osb
) && !ocfs2_mount_local(osb
))
4178 ocfs2_cluster_unlock(osb
, lockres
, level
);
4181 static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo
*oinfo
)
4183 struct mem_dqinfo
*info
= sb_dqinfo(oinfo
->dqi_gi
.dqi_sb
,
4184 oinfo
->dqi_gi
.dqi_type
);
4185 struct ocfs2_lock_res
*lockres
= &oinfo
->dqi_gqlock
;
4186 struct ocfs2_qinfo_lvb
*lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
4187 struct buffer_head
*bh
= NULL
;
4188 struct ocfs2_global_disk_dqinfo
*gdinfo
;
4191 if (ocfs2_dlm_lvb_valid(&lockres
->l_lksb
) &&
4192 lvb
->lvb_version
== OCFS2_QINFO_LVB_VERSION
) {
4193 info
->dqi_bgrace
= be32_to_cpu(lvb
->lvb_bgrace
);
4194 info
->dqi_igrace
= be32_to_cpu(lvb
->lvb_igrace
);
4195 oinfo
->dqi_syncms
= be32_to_cpu(lvb
->lvb_syncms
);
4196 oinfo
->dqi_gi
.dqi_blocks
= be32_to_cpu(lvb
->lvb_blocks
);
4197 oinfo
->dqi_gi
.dqi_free_blk
= be32_to_cpu(lvb
->lvb_free_blk
);
4198 oinfo
->dqi_gi
.dqi_free_entry
=
4199 be32_to_cpu(lvb
->lvb_free_entry
);
4201 status
= ocfs2_read_quota_phys_block(oinfo
->dqi_gqinode
,
4202 oinfo
->dqi_giblk
, &bh
);
4207 gdinfo
= (struct ocfs2_global_disk_dqinfo
*)
4208 (bh
->b_data
+ OCFS2_GLOBAL_INFO_OFF
);
4209 info
->dqi_bgrace
= le32_to_cpu(gdinfo
->dqi_bgrace
);
4210 info
->dqi_igrace
= le32_to_cpu(gdinfo
->dqi_igrace
);
4211 oinfo
->dqi_syncms
= le32_to_cpu(gdinfo
->dqi_syncms
);
4212 oinfo
->dqi_gi
.dqi_blocks
= le32_to_cpu(gdinfo
->dqi_blocks
);
4213 oinfo
->dqi_gi
.dqi_free_blk
= le32_to_cpu(gdinfo
->dqi_free_blk
);
4214 oinfo
->dqi_gi
.dqi_free_entry
=
4215 le32_to_cpu(gdinfo
->dqi_free_entry
);
4217 ocfs2_track_lock_refresh(lockres
);
4224 /* Lock quota info, this function expects at least shared lock on the quota file
4225 * so that we can safely refresh quota info from disk. */
4226 int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo
*oinfo
, int ex
)
4228 struct ocfs2_lock_res
*lockres
= &oinfo
->dqi_gqlock
;
4229 struct ocfs2_super
*osb
= OCFS2_SB(oinfo
->dqi_gi
.dqi_sb
);
4230 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
4233 /* On RO devices, locking really isn't needed... */
4234 if (ocfs2_is_hard_readonly(osb
)) {
4239 if (ocfs2_mount_local(osb
))
4242 status
= ocfs2_cluster_lock(osb
, lockres
, level
, 0, 0);
4247 if (!ocfs2_should_refresh_lock_res(lockres
))
4249 /* OK, we have the lock but we need to refresh the quota info */
4250 status
= ocfs2_refresh_qinfo(oinfo
);
4252 ocfs2_qinfo_unlock(oinfo
, ex
);
4253 ocfs2_complete_lock_res_refresh(lockres
, status
);
4258 int ocfs2_refcount_lock(struct ocfs2_refcount_tree
*ref_tree
, int ex
)
4261 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
4262 struct ocfs2_lock_res
*lockres
= &ref_tree
->rf_lockres
;
4263 struct ocfs2_super
*osb
= lockres
->l_priv
;
4266 if (ocfs2_is_hard_readonly(osb
))
4269 if (ocfs2_mount_local(osb
))
4272 status
= ocfs2_cluster_lock(osb
, lockres
, level
, 0, 0);
4279 void ocfs2_refcount_unlock(struct ocfs2_refcount_tree
*ref_tree
, int ex
)
4281 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
4282 struct ocfs2_lock_res
*lockres
= &ref_tree
->rf_lockres
;
4283 struct ocfs2_super
*osb
= lockres
->l_priv
;
4285 if (!ocfs2_mount_local(osb
))
4286 ocfs2_cluster_unlock(osb
, lockres
, level
);
4289 static void ocfs2_process_blocked_lock(struct ocfs2_super
*osb
,
4290 struct ocfs2_lock_res
*lockres
)
4293 struct ocfs2_unblock_ctl ctl
= {0, 0,};
4294 unsigned long flags
;
4296 /* Our reference to the lockres in this function can be
4297 * considered valid until we remove the OCFS2_LOCK_QUEUED
4301 BUG_ON(!lockres
->l_ops
);
4303 mlog(ML_BASTS
, "lockres %s blocked\n", lockres
->l_name
);
4305 /* Detect whether a lock has been marked as going away while
4306 * the downconvert thread was processing other things. A lock can
4307 * still be marked with OCFS2_LOCK_FREEING after this check,
4308 * but short circuiting here will still save us some
4310 spin_lock_irqsave(&lockres
->l_lock
, flags
);
4311 if (lockres
->l_flags
& OCFS2_LOCK_FREEING
)
4313 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
4315 status
= ocfs2_unblock_lock(osb
, lockres
, &ctl
);
4319 spin_lock_irqsave(&lockres
->l_lock
, flags
);
4321 if (lockres
->l_flags
& OCFS2_LOCK_FREEING
|| !ctl
.requeue
) {
4322 lockres_clear_flags(lockres
, OCFS2_LOCK_QUEUED
);
4324 ocfs2_schedule_blocked_lock(osb
, lockres
);
4326 mlog(ML_BASTS
, "lockres %s, requeue = %s.\n", lockres
->l_name
,
4327 ctl
.requeue
? "yes" : "no");
4328 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
4330 if (ctl
.unblock_action
!= UNBLOCK_CONTINUE
4331 && lockres
->l_ops
->post_unlock
)
4332 lockres
->l_ops
->post_unlock(osb
, lockres
);
4335 static void ocfs2_schedule_blocked_lock(struct ocfs2_super
*osb
,
4336 struct ocfs2_lock_res
*lockres
)
4338 unsigned long flags
;
4340 assert_spin_locked(&lockres
->l_lock
);
4342 if (lockres
->l_flags
& OCFS2_LOCK_FREEING
) {
4343 /* Do not schedule a lock for downconvert when it's on
4344 * the way to destruction - any nodes wanting access
4345 * to the resource will get it soon. */
4346 mlog(ML_BASTS
, "lockres %s won't be scheduled: flags 0x%lx\n",
4347 lockres
->l_name
, lockres
->l_flags
);
4351 lockres_or_flags(lockres
, OCFS2_LOCK_QUEUED
);
4353 spin_lock_irqsave(&osb
->dc_task_lock
, flags
);
4354 if (list_empty(&lockres
->l_blocked_list
)) {
4355 list_add_tail(&lockres
->l_blocked_list
,
4356 &osb
->blocked_lock_list
);
4357 osb
->blocked_lock_count
++;
4359 spin_unlock_irqrestore(&osb
->dc_task_lock
, flags
);
4362 static void ocfs2_downconvert_thread_do_work(struct ocfs2_super
*osb
)
4364 unsigned long processed
;
4365 unsigned long flags
;
4366 struct ocfs2_lock_res
*lockres
;
4368 spin_lock_irqsave(&osb
->dc_task_lock
, flags
);
4369 /* grab this early so we know to try again if a state change and
4370 * wake happens part-way through our work */
4371 osb
->dc_work_sequence
= osb
->dc_wake_sequence
;
4373 processed
= osb
->blocked_lock_count
;
4375 * blocked lock processing in this loop might call iput which can
4376 * remove items off osb->blocked_lock_list. Downconvert up to
4377 * 'processed' number of locks, but stop short if we had some
4378 * removed in ocfs2_mark_lockres_freeing when downconverting.
4380 while (processed
&& !list_empty(&osb
->blocked_lock_list
)) {
4381 lockres
= list_entry(osb
->blocked_lock_list
.next
,
4382 struct ocfs2_lock_res
, l_blocked_list
);
4383 list_del_init(&lockres
->l_blocked_list
);
4384 osb
->blocked_lock_count
--;
4385 spin_unlock_irqrestore(&osb
->dc_task_lock
, flags
);
4390 ocfs2_process_blocked_lock(osb
, lockres
);
4392 spin_lock_irqsave(&osb
->dc_task_lock
, flags
);
4394 spin_unlock_irqrestore(&osb
->dc_task_lock
, flags
);
4397 static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super
*osb
)
4400 unsigned long flags
;
4402 spin_lock_irqsave(&osb
->dc_task_lock
, flags
);
4403 if (list_empty(&osb
->blocked_lock_list
))
4406 spin_unlock_irqrestore(&osb
->dc_task_lock
, flags
);
4410 static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super
*osb
)
4412 int should_wake
= 0;
4413 unsigned long flags
;
4415 spin_lock_irqsave(&osb
->dc_task_lock
, flags
);
4416 if (osb
->dc_work_sequence
!= osb
->dc_wake_sequence
)
4418 spin_unlock_irqrestore(&osb
->dc_task_lock
, flags
);
4423 static int ocfs2_downconvert_thread(void *arg
)
4425 struct ocfs2_super
*osb
= arg
;
4427 /* only quit once we've been asked to stop and there is no more
4429 while (!(kthread_should_stop() &&
4430 ocfs2_downconvert_thread_lists_empty(osb
))) {
4432 wait_event_interruptible(osb
->dc_event
,
4433 ocfs2_downconvert_thread_should_wake(osb
) ||
4434 kthread_should_stop());
4436 mlog(0, "downconvert_thread: awoken\n");
4438 ocfs2_downconvert_thread_do_work(osb
);
4441 osb
->dc_task
= NULL
;
4445 void ocfs2_wake_downconvert_thread(struct ocfs2_super
*osb
)
4447 unsigned long flags
;
4449 spin_lock_irqsave(&osb
->dc_task_lock
, flags
);
4450 /* make sure the voting thread gets a swipe at whatever changes
4451 * the caller may have made to the voting state */
4452 osb
->dc_wake_sequence
++;
4453 spin_unlock_irqrestore(&osb
->dc_task_lock
, flags
);
4454 wake_up(&osb
->dc_event
);