1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Code which implements an OCFS2 specific interface to our DLM.
8 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
26 #include <linux/types.h>
27 #include <linux/slab.h>
28 #include <linux/highmem.h>
30 #include <linux/kthread.h>
31 #include <linux/pagemap.h>
32 #include <linux/debugfs.h>
33 #include <linux/seq_file.h>
34 #include <linux/time.h>
35 #include <linux/quotaops.h>
37 #define MLOG_MASK_PREFIX ML_DLM_GLUE
38 #include <cluster/masklog.h>
41 #include "ocfs2_lockingver.h"
46 #include "extent_map.h"
48 #include "heartbeat.h"
51 #include "stackglue.h"
56 #include "refcounttree.h"
58 #include "buffer_head_io.h"
60 struct ocfs2_mask_waiter
{
61 struct list_head mw_item
;
63 struct completion mw_complete
;
64 unsigned long mw_mask
;
65 unsigned long mw_goal
;
66 #ifdef CONFIG_OCFS2_FS_STATS
67 unsigned long long mw_lock_start
;
71 static struct ocfs2_super
*ocfs2_get_dentry_osb(struct ocfs2_lock_res
*lockres
);
72 static struct ocfs2_super
*ocfs2_get_inode_osb(struct ocfs2_lock_res
*lockres
);
73 static struct ocfs2_super
*ocfs2_get_file_osb(struct ocfs2_lock_res
*lockres
);
74 static struct ocfs2_super
*ocfs2_get_qinfo_osb(struct ocfs2_lock_res
*lockres
);
77 * Return value from ->downconvert_worker functions.
79 * These control the precise actions of ocfs2_unblock_lock()
80 * and ocfs2_process_blocked_lock()
83 enum ocfs2_unblock_action
{
84 UNBLOCK_CONTINUE
= 0, /* Continue downconvert */
85 UNBLOCK_CONTINUE_POST
= 1, /* Continue downconvert, fire
86 * ->post_unlock callback */
87 UNBLOCK_STOP_POST
= 2, /* Do not downconvert, fire
88 * ->post_unlock() callback. */
91 struct ocfs2_unblock_ctl
{
93 enum ocfs2_unblock_action unblock_action
;
96 /* Lockdep class keys */
97 struct lock_class_key lockdep_keys
[OCFS2_NUM_LOCK_TYPES
];
99 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res
*lockres
,
101 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res
*lockres
);
103 static int ocfs2_data_convert_worker(struct ocfs2_lock_res
*lockres
,
106 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res
*lockres
,
109 static void ocfs2_dentry_post_unlock(struct ocfs2_super
*osb
,
110 struct ocfs2_lock_res
*lockres
);
112 static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res
*lockres
);
114 static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res
*lockres
,
116 static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res
*lockres
,
119 #define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres)
121 /* This aids in debugging situations where a bad LVB might be involved. */
122 static void ocfs2_dump_meta_lvb_info(u64 level
,
123 const char *function
,
125 struct ocfs2_lock_res
*lockres
)
127 struct ocfs2_meta_lvb
*lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
129 mlog(level
, "LVB information for %s (called from %s:%u):\n",
130 lockres
->l_name
, function
, line
);
131 mlog(level
, "version: %u, clusters: %u, generation: 0x%x\n",
132 lvb
->lvb_version
, be32_to_cpu(lvb
->lvb_iclusters
),
133 be32_to_cpu(lvb
->lvb_igeneration
));
134 mlog(level
, "size: %llu, uid %u, gid %u, mode 0x%x\n",
135 (unsigned long long)be64_to_cpu(lvb
->lvb_isize
),
136 be32_to_cpu(lvb
->lvb_iuid
), be32_to_cpu(lvb
->lvb_igid
),
137 be16_to_cpu(lvb
->lvb_imode
));
138 mlog(level
, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
139 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb
->lvb_inlink
),
140 (long long)be64_to_cpu(lvb
->lvb_iatime_packed
),
141 (long long)be64_to_cpu(lvb
->lvb_ictime_packed
),
142 (long long)be64_to_cpu(lvb
->lvb_imtime_packed
),
143 be32_to_cpu(lvb
->lvb_iattr
));
148 * OCFS2 Lock Resource Operations
150 * These fine tune the behavior of the generic dlmglue locking infrastructure.
152 * The most basic of lock types can point ->l_priv to their respective
153 * struct ocfs2_super and allow the default actions to manage things.
155 * Right now, each lock type also needs to implement an init function,
156 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
157 * should be called when the lock is no longer needed (i.e., object
160 struct ocfs2_lock_res_ops
{
162 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
163 * this callback if ->l_priv is not an ocfs2_super pointer
165 struct ocfs2_super
* (*get_osb
)(struct ocfs2_lock_res
*);
168 * Optionally called in the downconvert thread after a
169 * successful downconvert. The lockres will not be referenced
170 * after this callback is called, so it is safe to free
173 * The exact semantics of when this is called are controlled
174 * by ->downconvert_worker()
176 void (*post_unlock
)(struct ocfs2_super
*, struct ocfs2_lock_res
*);
179 * Allow a lock type to add checks to determine whether it is
180 * safe to downconvert a lock. Return 0 to re-queue the
181 * downconvert at a later time, nonzero to continue.
183 * For most locks, the default checks that there are no
184 * incompatible holders are sufficient.
186 * Called with the lockres spinlock held.
188 int (*check_downconvert
)(struct ocfs2_lock_res
*, int);
191 * Allows a lock type to populate the lock value block. This
192 * is called on downconvert, and when we drop a lock.
194 * Locks that want to use this should set LOCK_TYPE_USES_LVB
195 * in the flags field.
197 * Called with the lockres spinlock held.
199 void (*set_lvb
)(struct ocfs2_lock_res
*);
202 * Called from the downconvert thread when it is determined
203 * that a lock will be downconverted. This is called without
204 * any locks held so the function can do work that might
205 * schedule (syncing out data, etc).
207 * This should return any one of the ocfs2_unblock_action
208 * values, depending on what it wants the thread to do.
210 int (*downconvert_worker
)(struct ocfs2_lock_res
*, int);
213 * LOCK_TYPE_* flags which describe the specific requirements
214 * of a lock type. Descriptions of each individual flag follow.
220 * Some locks want to "refresh" potentially stale data when a
221 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
222 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
223 * individual lockres l_flags member from the ast function. It is
224 * expected that the locking wrapper will clear the
225 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
227 #define LOCK_TYPE_REQUIRES_REFRESH 0x1
230 * Indicate that a lock type makes use of the lock value block. The
231 * ->set_lvb lock type callback must be defined.
233 #define LOCK_TYPE_USES_LVB 0x2
235 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops
= {
236 .get_osb
= ocfs2_get_inode_osb
,
240 static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops
= {
241 .get_osb
= ocfs2_get_inode_osb
,
242 .check_downconvert
= ocfs2_check_meta_downconvert
,
243 .set_lvb
= ocfs2_set_meta_lvb
,
244 .downconvert_worker
= ocfs2_data_convert_worker
,
245 .flags
= LOCK_TYPE_REQUIRES_REFRESH
|LOCK_TYPE_USES_LVB
,
248 static struct ocfs2_lock_res_ops ocfs2_super_lops
= {
249 .flags
= LOCK_TYPE_REQUIRES_REFRESH
,
252 static struct ocfs2_lock_res_ops ocfs2_rename_lops
= {
256 static struct ocfs2_lock_res_ops ocfs2_nfs_sync_lops
= {
260 static struct ocfs2_lock_res_ops ocfs2_orphan_scan_lops
= {
261 .flags
= LOCK_TYPE_REQUIRES_REFRESH
|LOCK_TYPE_USES_LVB
,
264 static struct ocfs2_lock_res_ops ocfs2_dentry_lops
= {
265 .get_osb
= ocfs2_get_dentry_osb
,
266 .post_unlock
= ocfs2_dentry_post_unlock
,
267 .downconvert_worker
= ocfs2_dentry_convert_worker
,
271 static struct ocfs2_lock_res_ops ocfs2_inode_open_lops
= {
272 .get_osb
= ocfs2_get_inode_osb
,
276 static struct ocfs2_lock_res_ops ocfs2_flock_lops
= {
277 .get_osb
= ocfs2_get_file_osb
,
281 static struct ocfs2_lock_res_ops ocfs2_qinfo_lops
= {
282 .set_lvb
= ocfs2_set_qinfo_lvb
,
283 .get_osb
= ocfs2_get_qinfo_osb
,
284 .flags
= LOCK_TYPE_REQUIRES_REFRESH
| LOCK_TYPE_USES_LVB
,
287 static struct ocfs2_lock_res_ops ocfs2_refcount_block_lops
= {
288 .check_downconvert
= ocfs2_check_refcount_downconvert
,
289 .downconvert_worker
= ocfs2_refcount_convert_worker
,
293 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res
*lockres
)
295 return lockres
->l_type
== OCFS2_LOCK_TYPE_META
||
296 lockres
->l_type
== OCFS2_LOCK_TYPE_RW
||
297 lockres
->l_type
== OCFS2_LOCK_TYPE_OPEN
;
300 static inline struct inode
*ocfs2_lock_res_inode(struct ocfs2_lock_res
*lockres
)
302 BUG_ON(!ocfs2_is_inode_lock(lockres
));
304 return (struct inode
*) lockres
->l_priv
;
307 static inline struct ocfs2_dentry_lock
*ocfs2_lock_res_dl(struct ocfs2_lock_res
*lockres
)
309 BUG_ON(lockres
->l_type
!= OCFS2_LOCK_TYPE_DENTRY
);
311 return (struct ocfs2_dentry_lock
*)lockres
->l_priv
;
314 static inline struct ocfs2_mem_dqinfo
*ocfs2_lock_res_qinfo(struct ocfs2_lock_res
*lockres
)
316 BUG_ON(lockres
->l_type
!= OCFS2_LOCK_TYPE_QINFO
);
318 return (struct ocfs2_mem_dqinfo
*)lockres
->l_priv
;
321 static inline struct ocfs2_refcount_tree
*
322 ocfs2_lock_res_refcount_tree(struct ocfs2_lock_res
*res
)
324 return container_of(res
, struct ocfs2_refcount_tree
, rf_lockres
);
327 static inline struct ocfs2_super
*ocfs2_get_lockres_osb(struct ocfs2_lock_res
*lockres
)
329 if (lockres
->l_ops
->get_osb
)
330 return lockres
->l_ops
->get_osb(lockres
);
332 return (struct ocfs2_super
*)lockres
->l_priv
;
335 static int ocfs2_lock_create(struct ocfs2_super
*osb
,
336 struct ocfs2_lock_res
*lockres
,
339 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res
*lockres
,
341 static void __ocfs2_cluster_unlock(struct ocfs2_super
*osb
,
342 struct ocfs2_lock_res
*lockres
,
343 int level
, unsigned long caller_ip
);
344 static inline void ocfs2_cluster_unlock(struct ocfs2_super
*osb
,
345 struct ocfs2_lock_res
*lockres
,
348 __ocfs2_cluster_unlock(osb
, lockres
, level
, _RET_IP_
);
351 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res
*lockres
);
352 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res
*lockres
);
353 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res
*lockres
);
354 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res
*lockres
, int level
);
355 static void ocfs2_schedule_blocked_lock(struct ocfs2_super
*osb
,
356 struct ocfs2_lock_res
*lockres
);
357 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res
*lockres
,
359 #define ocfs2_log_dlm_error(_func, _err, _lockres) do { \
360 if ((_lockres)->l_type != OCFS2_LOCK_TYPE_DENTRY) \
361 mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \
362 _err, _func, _lockres->l_name); \
364 mlog(ML_ERROR, "DLM error %d while calling %s on resource %.*s%08x\n", \
365 _err, _func, OCFS2_DENTRY_LOCK_INO_START - 1, (_lockres)->l_name, \
366 (unsigned int)ocfs2_get_dentry_lock_ino(_lockres)); \
368 static int ocfs2_downconvert_thread(void *arg
);
369 static void ocfs2_downconvert_on_unlock(struct ocfs2_super
*osb
,
370 struct ocfs2_lock_res
*lockres
);
371 static int ocfs2_inode_lock_update(struct inode
*inode
,
372 struct buffer_head
**bh
);
373 static void ocfs2_drop_osb_locks(struct ocfs2_super
*osb
);
374 static inline int ocfs2_highest_compat_lock_level(int level
);
375 static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res
*lockres
,
377 static int ocfs2_downconvert_lock(struct ocfs2_super
*osb
,
378 struct ocfs2_lock_res
*lockres
,
381 unsigned int generation
);
382 static int ocfs2_prepare_cancel_convert(struct ocfs2_super
*osb
,
383 struct ocfs2_lock_res
*lockres
);
384 static int ocfs2_cancel_convert(struct ocfs2_super
*osb
,
385 struct ocfs2_lock_res
*lockres
);
388 static void ocfs2_build_lock_name(enum ocfs2_lock_type type
,
397 BUG_ON(type
>= OCFS2_NUM_LOCK_TYPES
);
399 len
= snprintf(name
, OCFS2_LOCK_ID_MAX_LEN
, "%c%s%016llx%08x",
400 ocfs2_lock_type_char(type
), OCFS2_LOCK_ID_PAD
,
401 (long long)blkno
, generation
);
403 BUG_ON(len
!= (OCFS2_LOCK_ID_MAX_LEN
- 1));
405 mlog(0, "built lock resource with name: %s\n", name
);
410 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock
);
412 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res
*res
,
413 struct ocfs2_dlm_debug
*dlm_debug
)
415 mlog(0, "Add tracking for lockres %s\n", res
->l_name
);
417 spin_lock(&ocfs2_dlm_tracking_lock
);
418 list_add(&res
->l_debug_list
, &dlm_debug
->d_lockres_tracking
);
419 spin_unlock(&ocfs2_dlm_tracking_lock
);
422 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res
*res
)
424 spin_lock(&ocfs2_dlm_tracking_lock
);
425 if (!list_empty(&res
->l_debug_list
))
426 list_del_init(&res
->l_debug_list
);
427 spin_unlock(&ocfs2_dlm_tracking_lock
);
430 #ifdef CONFIG_OCFS2_FS_STATS
431 static void ocfs2_init_lock_stats(struct ocfs2_lock_res
*res
)
433 res
->l_lock_num_prmode
= 0;
434 res
->l_lock_num_prmode_failed
= 0;
435 res
->l_lock_total_prmode
= 0;
436 res
->l_lock_max_prmode
= 0;
437 res
->l_lock_num_exmode
= 0;
438 res
->l_lock_num_exmode_failed
= 0;
439 res
->l_lock_total_exmode
= 0;
440 res
->l_lock_max_exmode
= 0;
441 res
->l_lock_refresh
= 0;
444 static void ocfs2_update_lock_stats(struct ocfs2_lock_res
*res
, int level
,
445 struct ocfs2_mask_waiter
*mw
, int ret
)
447 unsigned long long *num
, *sum
;
448 unsigned int *max
, *failed
;
449 struct timespec ts
= current_kernel_time();
450 unsigned long long time
= timespec_to_ns(&ts
) - mw
->mw_lock_start
;
452 if (level
== LKM_PRMODE
) {
453 num
= &res
->l_lock_num_prmode
;
454 sum
= &res
->l_lock_total_prmode
;
455 max
= &res
->l_lock_max_prmode
;
456 failed
= &res
->l_lock_num_prmode_failed
;
457 } else if (level
== LKM_EXMODE
) {
458 num
= &res
->l_lock_num_exmode
;
459 sum
= &res
->l_lock_total_exmode
;
460 max
= &res
->l_lock_max_exmode
;
461 failed
= &res
->l_lock_num_exmode_failed
;
473 static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res
*lockres
)
475 lockres
->l_lock_refresh
++;
478 static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter
*mw
)
480 struct timespec ts
= current_kernel_time();
481 mw
->mw_lock_start
= timespec_to_ns(&ts
);
484 static inline void ocfs2_init_lock_stats(struct ocfs2_lock_res
*res
)
487 static inline void ocfs2_update_lock_stats(struct ocfs2_lock_res
*res
,
488 int level
, struct ocfs2_mask_waiter
*mw
, int ret
)
491 static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res
*lockres
)
494 static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter
*mw
)
499 static void ocfs2_lock_res_init_common(struct ocfs2_super
*osb
,
500 struct ocfs2_lock_res
*res
,
501 enum ocfs2_lock_type type
,
502 struct ocfs2_lock_res_ops
*ops
,
509 res
->l_level
= DLM_LOCK_IV
;
510 res
->l_requested
= DLM_LOCK_IV
;
511 res
->l_blocking
= DLM_LOCK_IV
;
512 res
->l_action
= OCFS2_AST_INVALID
;
513 res
->l_unlock_action
= OCFS2_UNLOCK_INVALID
;
515 res
->l_flags
= OCFS2_LOCK_INITIALIZED
;
517 ocfs2_add_lockres_tracking(res
, osb
->osb_dlm_debug
);
519 ocfs2_init_lock_stats(res
);
520 #ifdef CONFIG_DEBUG_LOCK_ALLOC
521 if (type
!= OCFS2_LOCK_TYPE_OPEN
)
522 lockdep_init_map(&res
->l_lockdep_map
, ocfs2_lock_type_strings
[type
],
523 &lockdep_keys
[type
], 0);
525 res
->l_lockdep_map
.key
= NULL
;
529 void ocfs2_lock_res_init_once(struct ocfs2_lock_res
*res
)
531 /* This also clears out the lock status block */
532 memset(res
, 0, sizeof(struct ocfs2_lock_res
));
533 spin_lock_init(&res
->l_lock
);
534 init_waitqueue_head(&res
->l_event
);
535 INIT_LIST_HEAD(&res
->l_blocked_list
);
536 INIT_LIST_HEAD(&res
->l_mask_waiters
);
539 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res
*res
,
540 enum ocfs2_lock_type type
,
541 unsigned int generation
,
544 struct ocfs2_lock_res_ops
*ops
;
547 case OCFS2_LOCK_TYPE_RW
:
548 ops
= &ocfs2_inode_rw_lops
;
550 case OCFS2_LOCK_TYPE_META
:
551 ops
= &ocfs2_inode_inode_lops
;
553 case OCFS2_LOCK_TYPE_OPEN
:
554 ops
= &ocfs2_inode_open_lops
;
557 mlog_bug_on_msg(1, "type: %d\n", type
);
558 ops
= NULL
; /* thanks, gcc */
562 ocfs2_build_lock_name(type
, OCFS2_I(inode
)->ip_blkno
,
563 generation
, res
->l_name
);
564 ocfs2_lock_res_init_common(OCFS2_SB(inode
->i_sb
), res
, type
, ops
, inode
);
567 static struct ocfs2_super
*ocfs2_get_inode_osb(struct ocfs2_lock_res
*lockres
)
569 struct inode
*inode
= ocfs2_lock_res_inode(lockres
);
571 return OCFS2_SB(inode
->i_sb
);
574 static struct ocfs2_super
*ocfs2_get_qinfo_osb(struct ocfs2_lock_res
*lockres
)
576 struct ocfs2_mem_dqinfo
*info
= lockres
->l_priv
;
578 return OCFS2_SB(info
->dqi_gi
.dqi_sb
);
581 static struct ocfs2_super
*ocfs2_get_file_osb(struct ocfs2_lock_res
*lockres
)
583 struct ocfs2_file_private
*fp
= lockres
->l_priv
;
585 return OCFS2_SB(fp
->fp_file
->f_mapping
->host
->i_sb
);
588 static __u64
ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res
*lockres
)
590 __be64 inode_blkno_be
;
592 memcpy(&inode_blkno_be
, &lockres
->l_name
[OCFS2_DENTRY_LOCK_INO_START
],
595 return be64_to_cpu(inode_blkno_be
);
598 static struct ocfs2_super
*ocfs2_get_dentry_osb(struct ocfs2_lock_res
*lockres
)
600 struct ocfs2_dentry_lock
*dl
= lockres
->l_priv
;
602 return OCFS2_SB(dl
->dl_inode
->i_sb
);
605 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock
*dl
,
606 u64 parent
, struct inode
*inode
)
609 u64 inode_blkno
= OCFS2_I(inode
)->ip_blkno
;
610 __be64 inode_blkno_be
= cpu_to_be64(inode_blkno
);
611 struct ocfs2_lock_res
*lockres
= &dl
->dl_lockres
;
613 ocfs2_lock_res_init_once(lockres
);
616 * Unfortunately, the standard lock naming scheme won't work
617 * here because we have two 16 byte values to use. Instead,
618 * we'll stuff the inode number as a binary value. We still
619 * want error prints to show something without garbling the
620 * display, so drop a null byte in there before the inode
621 * number. A future version of OCFS2 will likely use all
622 * binary lock names. The stringified names have been a
623 * tremendous aid in debugging, but now that the debugfs
624 * interface exists, we can mangle things there if need be.
626 * NOTE: We also drop the standard "pad" value (the total lock
627 * name size stays the same though - the last part is all
628 * zeros due to the memset in ocfs2_lock_res_init_once()
630 len
= snprintf(lockres
->l_name
, OCFS2_DENTRY_LOCK_INO_START
,
632 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY
),
635 BUG_ON(len
!= (OCFS2_DENTRY_LOCK_INO_START
- 1));
637 memcpy(&lockres
->l_name
[OCFS2_DENTRY_LOCK_INO_START
], &inode_blkno_be
,
640 ocfs2_lock_res_init_common(OCFS2_SB(inode
->i_sb
), lockres
,
641 OCFS2_LOCK_TYPE_DENTRY
, &ocfs2_dentry_lops
,
645 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res
*res
,
646 struct ocfs2_super
*osb
)
648 /* Superblock lockres doesn't come from a slab so we call init
649 * once on it manually. */
650 ocfs2_lock_res_init_once(res
);
651 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER
, OCFS2_SUPER_BLOCK_BLKNO
,
653 ocfs2_lock_res_init_common(osb
, res
, OCFS2_LOCK_TYPE_SUPER
,
654 &ocfs2_super_lops
, osb
);
657 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res
*res
,
658 struct ocfs2_super
*osb
)
660 /* Rename lockres doesn't come from a slab so we call init
661 * once on it manually. */
662 ocfs2_lock_res_init_once(res
);
663 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME
, 0, 0, res
->l_name
);
664 ocfs2_lock_res_init_common(osb
, res
, OCFS2_LOCK_TYPE_RENAME
,
665 &ocfs2_rename_lops
, osb
);
668 static void ocfs2_nfs_sync_lock_res_init(struct ocfs2_lock_res
*res
,
669 struct ocfs2_super
*osb
)
671 /* nfs_sync 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_NFS_SYNC
, 0, 0, res
->l_name
);
675 ocfs2_lock_res_init_common(osb
, res
, OCFS2_LOCK_TYPE_NFS_SYNC
,
676 &ocfs2_nfs_sync_lops
, osb
);
679 static void ocfs2_orphan_scan_lock_res_init(struct ocfs2_lock_res
*res
,
680 struct ocfs2_super
*osb
)
682 ocfs2_lock_res_init_once(res
);
683 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_ORPHAN_SCAN
, 0, 0, res
->l_name
);
684 ocfs2_lock_res_init_common(osb
, res
, OCFS2_LOCK_TYPE_ORPHAN_SCAN
,
685 &ocfs2_orphan_scan_lops
, osb
);
688 void ocfs2_file_lock_res_init(struct ocfs2_lock_res
*lockres
,
689 struct ocfs2_file_private
*fp
)
691 struct inode
*inode
= fp
->fp_file
->f_mapping
->host
;
692 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
694 ocfs2_lock_res_init_once(lockres
);
695 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK
, oi
->ip_blkno
,
696 inode
->i_generation
, lockres
->l_name
);
697 ocfs2_lock_res_init_common(OCFS2_SB(inode
->i_sb
), lockres
,
698 OCFS2_LOCK_TYPE_FLOCK
, &ocfs2_flock_lops
,
700 lockres
->l_flags
|= OCFS2_LOCK_NOCACHE
;
703 void ocfs2_qinfo_lock_res_init(struct ocfs2_lock_res
*lockres
,
704 struct ocfs2_mem_dqinfo
*info
)
706 ocfs2_lock_res_init_once(lockres
);
707 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_QINFO
, info
->dqi_gi
.dqi_type
,
709 ocfs2_lock_res_init_common(OCFS2_SB(info
->dqi_gi
.dqi_sb
), lockres
,
710 OCFS2_LOCK_TYPE_QINFO
, &ocfs2_qinfo_lops
,
714 void ocfs2_refcount_lock_res_init(struct ocfs2_lock_res
*lockres
,
715 struct ocfs2_super
*osb
, u64 ref_blkno
,
716 unsigned int generation
)
718 ocfs2_lock_res_init_once(lockres
);
719 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_REFCOUNT
, ref_blkno
,
720 generation
, lockres
->l_name
);
721 ocfs2_lock_res_init_common(osb
, lockres
, OCFS2_LOCK_TYPE_REFCOUNT
,
722 &ocfs2_refcount_block_lops
, osb
);
725 void ocfs2_lock_res_free(struct ocfs2_lock_res
*res
)
729 if (!(res
->l_flags
& OCFS2_LOCK_INITIALIZED
))
732 ocfs2_remove_lockres_tracking(res
);
734 mlog_bug_on_msg(!list_empty(&res
->l_blocked_list
),
735 "Lockres %s is on the blocked list\n",
737 mlog_bug_on_msg(!list_empty(&res
->l_mask_waiters
),
738 "Lockres %s has mask waiters pending\n",
740 mlog_bug_on_msg(spin_is_locked(&res
->l_lock
),
741 "Lockres %s is locked\n",
743 mlog_bug_on_msg(res
->l_ro_holders
,
744 "Lockres %s has %u ro holders\n",
745 res
->l_name
, res
->l_ro_holders
);
746 mlog_bug_on_msg(res
->l_ex_holders
,
747 "Lockres %s has %u ex holders\n",
748 res
->l_name
, res
->l_ex_holders
);
750 /* Need to clear out the lock status block for the dlm */
751 memset(&res
->l_lksb
, 0, sizeof(res
->l_lksb
));
757 static inline void ocfs2_inc_holders(struct ocfs2_lock_res
*lockres
,
766 lockres
->l_ex_holders
++;
769 lockres
->l_ro_holders
++;
778 static inline void ocfs2_dec_holders(struct ocfs2_lock_res
*lockres
,
787 BUG_ON(!lockres
->l_ex_holders
);
788 lockres
->l_ex_holders
--;
791 BUG_ON(!lockres
->l_ro_holders
);
792 lockres
->l_ro_holders
--;
800 /* WARNING: This function lives in a world where the only three lock
801 * levels are EX, PR, and NL. It *will* have to be adjusted when more
802 * lock types are added. */
803 static inline int ocfs2_highest_compat_lock_level(int level
)
805 int new_level
= DLM_LOCK_EX
;
807 if (level
== DLM_LOCK_EX
)
808 new_level
= DLM_LOCK_NL
;
809 else if (level
== DLM_LOCK_PR
)
810 new_level
= DLM_LOCK_PR
;
814 static void lockres_set_flags(struct ocfs2_lock_res
*lockres
,
815 unsigned long newflags
)
817 struct ocfs2_mask_waiter
*mw
, *tmp
;
819 assert_spin_locked(&lockres
->l_lock
);
821 lockres
->l_flags
= newflags
;
823 list_for_each_entry_safe(mw
, tmp
, &lockres
->l_mask_waiters
, mw_item
) {
824 if ((lockres
->l_flags
& mw
->mw_mask
) != mw
->mw_goal
)
827 list_del_init(&mw
->mw_item
);
829 complete(&mw
->mw_complete
);
832 static void lockres_or_flags(struct ocfs2_lock_res
*lockres
, unsigned long or)
834 lockres_set_flags(lockres
, lockres
->l_flags
| or);
836 static void lockres_clear_flags(struct ocfs2_lock_res
*lockres
,
839 lockres_set_flags(lockres
, lockres
->l_flags
& ~clear
);
842 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res
*lockres
)
846 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BUSY
));
847 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
));
848 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BLOCKED
));
849 BUG_ON(lockres
->l_blocking
<= DLM_LOCK_NL
);
851 lockres
->l_level
= lockres
->l_requested
;
852 if (lockres
->l_level
<=
853 ocfs2_highest_compat_lock_level(lockres
->l_blocking
)) {
854 lockres
->l_blocking
= DLM_LOCK_NL
;
855 lockres_clear_flags(lockres
, OCFS2_LOCK_BLOCKED
);
857 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
862 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res
*lockres
)
866 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BUSY
));
867 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
));
869 /* Convert from RO to EX doesn't really need anything as our
870 * information is already up to data. Convert from NL to
871 * *anything* however should mark ourselves as needing an
873 if (lockres
->l_level
== DLM_LOCK_NL
&&
874 lockres
->l_ops
->flags
& LOCK_TYPE_REQUIRES_REFRESH
)
875 lockres_or_flags(lockres
, OCFS2_LOCK_NEEDS_REFRESH
);
877 lockres
->l_level
= lockres
->l_requested
;
880 * We set the OCFS2_LOCK_UPCONVERT_FINISHING flag before clearing
881 * the OCFS2_LOCK_BUSY flag to prevent the dc thread from
882 * downconverting the lock before the upconvert has fully completed.
884 lockres_or_flags(lockres
, OCFS2_LOCK_UPCONVERT_FINISHING
);
886 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
891 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res
*lockres
)
895 BUG_ON((!(lockres
->l_flags
& OCFS2_LOCK_BUSY
)));
896 BUG_ON(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
);
898 if (lockres
->l_requested
> DLM_LOCK_NL
&&
899 !(lockres
->l_flags
& OCFS2_LOCK_LOCAL
) &&
900 lockres
->l_ops
->flags
& LOCK_TYPE_REQUIRES_REFRESH
)
901 lockres_or_flags(lockres
, OCFS2_LOCK_NEEDS_REFRESH
);
903 lockres
->l_level
= lockres
->l_requested
;
904 lockres_or_flags(lockres
, OCFS2_LOCK_ATTACHED
);
905 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
910 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res
*lockres
,
913 int needs_downconvert
= 0;
916 assert_spin_locked(&lockres
->l_lock
);
918 if (level
> lockres
->l_blocking
) {
919 /* only schedule a downconvert if we haven't already scheduled
920 * one that goes low enough to satisfy the level we're
921 * blocking. this also catches the case where we get
923 if (ocfs2_highest_compat_lock_level(level
) <
924 ocfs2_highest_compat_lock_level(lockres
->l_blocking
))
925 needs_downconvert
= 1;
927 lockres
->l_blocking
= level
;
930 if (needs_downconvert
)
931 lockres_or_flags(lockres
, OCFS2_LOCK_BLOCKED
);
933 mlog_exit(needs_downconvert
);
934 return needs_downconvert
;
938 * OCFS2_LOCK_PENDING and l_pending_gen.
940 * Why does OCFS2_LOCK_PENDING exist? To close a race between setting
941 * OCFS2_LOCK_BUSY and calling ocfs2_dlm_lock(). See ocfs2_unblock_lock()
942 * for more details on the race.
944 * OCFS2_LOCK_PENDING closes the race quite nicely. However, it introduces
945 * a race on itself. In o2dlm, we can get the ast before ocfs2_dlm_lock()
946 * returns. The ast clears OCFS2_LOCK_BUSY, and must therefore clear
947 * OCFS2_LOCK_PENDING at the same time. When ocfs2_dlm_lock() returns,
948 * the caller is going to try to clear PENDING again. If nothing else is
949 * happening, __lockres_clear_pending() sees PENDING is unset and does
952 * But what if another path (eg downconvert thread) has just started a
953 * new locking action? The other path has re-set PENDING. Our path
954 * cannot clear PENDING, because that will re-open the original race
960 * ocfs2_cluster_lock()
965 * ocfs2_locking_ast() ocfs2_downconvert_thread()
966 * clear PENDING ocfs2_unblock_lock()
969 * ocfs2_prepare_downconvert()
979 * So as you can see, we now have a window where l_lock is not held,
980 * PENDING is not set, and ocfs2_dlm_lock() has not been called.
982 * The core problem is that ocfs2_cluster_lock() has cleared the PENDING
983 * set by ocfs2_prepare_downconvert(). That wasn't nice.
985 * To solve this we introduce l_pending_gen. A call to
986 * lockres_clear_pending() will only do so when it is passed a generation
987 * number that matches the lockres. lockres_set_pending() will return the
988 * current generation number. When ocfs2_cluster_lock() goes to clear
989 * PENDING, it passes the generation it got from set_pending(). In our
990 * example above, the generation numbers will *not* match. Thus,
991 * ocfs2_cluster_lock() will not clear the PENDING set by
992 * ocfs2_prepare_downconvert().
995 /* Unlocked version for ocfs2_locking_ast() */
996 static void __lockres_clear_pending(struct ocfs2_lock_res
*lockres
,
997 unsigned int generation
,
998 struct ocfs2_super
*osb
)
1000 assert_spin_locked(&lockres
->l_lock
);
1003 * The ast and locking functions can race us here. The winner
1004 * will clear pending, the loser will not.
1006 if (!(lockres
->l_flags
& OCFS2_LOCK_PENDING
) ||
1007 (lockres
->l_pending_gen
!= generation
))
1010 lockres_clear_flags(lockres
, OCFS2_LOCK_PENDING
);
1011 lockres
->l_pending_gen
++;
1014 * The downconvert thread may have skipped us because we
1015 * were PENDING. Wake it up.
1017 if (lockres
->l_flags
& OCFS2_LOCK_BLOCKED
)
1018 ocfs2_wake_downconvert_thread(osb
);
1021 /* Locked version for callers of ocfs2_dlm_lock() */
1022 static void lockres_clear_pending(struct ocfs2_lock_res
*lockres
,
1023 unsigned int generation
,
1024 struct ocfs2_super
*osb
)
1026 unsigned long flags
;
1028 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1029 __lockres_clear_pending(lockres
, generation
, osb
);
1030 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1033 static unsigned int lockres_set_pending(struct ocfs2_lock_res
*lockres
)
1035 assert_spin_locked(&lockres
->l_lock
);
1036 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BUSY
));
1038 lockres_or_flags(lockres
, OCFS2_LOCK_PENDING
);
1040 return lockres
->l_pending_gen
;
1044 static void ocfs2_blocking_ast(void *opaque
, int level
)
1046 struct ocfs2_lock_res
*lockres
= opaque
;
1047 struct ocfs2_super
*osb
= ocfs2_get_lockres_osb(lockres
);
1048 int needs_downconvert
;
1049 unsigned long flags
;
1051 BUG_ON(level
<= DLM_LOCK_NL
);
1053 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
1054 lockres
->l_name
, level
, lockres
->l_level
,
1055 ocfs2_lock_type_string(lockres
->l_type
));
1058 * We can skip the bast for locks which don't enable caching -
1059 * they'll be dropped at the earliest possible time anyway.
1061 if (lockres
->l_flags
& OCFS2_LOCK_NOCACHE
)
1064 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1065 needs_downconvert
= ocfs2_generic_handle_bast(lockres
, level
);
1066 if (needs_downconvert
)
1067 ocfs2_schedule_blocked_lock(osb
, lockres
);
1068 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1070 wake_up(&lockres
->l_event
);
1072 ocfs2_wake_downconvert_thread(osb
);
1075 static void ocfs2_locking_ast(void *opaque
)
1077 struct ocfs2_lock_res
*lockres
= opaque
;
1078 struct ocfs2_super
*osb
= ocfs2_get_lockres_osb(lockres
);
1079 unsigned long flags
;
1082 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1084 status
= ocfs2_dlm_lock_status(&lockres
->l_lksb
);
1086 if (status
== -EAGAIN
) {
1087 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
1092 mlog(ML_ERROR
, "lockres %s: lksb status value of %d!\n",
1093 lockres
->l_name
, status
);
1094 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1098 switch(lockres
->l_action
) {
1099 case OCFS2_AST_ATTACH
:
1100 ocfs2_generic_handle_attach_action(lockres
);
1101 lockres_clear_flags(lockres
, OCFS2_LOCK_LOCAL
);
1103 case OCFS2_AST_CONVERT
:
1104 ocfs2_generic_handle_convert_action(lockres
);
1106 case OCFS2_AST_DOWNCONVERT
:
1107 ocfs2_generic_handle_downconvert_action(lockres
);
1110 mlog(ML_ERROR
, "lockres %s: ast fired with invalid action: %u "
1111 "lockres flags = 0x%lx, unlock action: %u\n",
1112 lockres
->l_name
, lockres
->l_action
, lockres
->l_flags
,
1113 lockres
->l_unlock_action
);
1117 /* set it to something invalid so if we get called again we
1119 lockres
->l_action
= OCFS2_AST_INVALID
;
1121 /* Did we try to cancel this lock? Clear that state */
1122 if (lockres
->l_unlock_action
== OCFS2_UNLOCK_CANCEL_CONVERT
)
1123 lockres
->l_unlock_action
= OCFS2_UNLOCK_INVALID
;
1126 * We may have beaten the locking functions here. We certainly
1127 * know that dlm_lock() has been called :-)
1128 * Because we can't have two lock calls in flight at once, we
1129 * can use lockres->l_pending_gen.
1131 __lockres_clear_pending(lockres
, lockres
->l_pending_gen
, osb
);
1133 wake_up(&lockres
->l_event
);
1134 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1137 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res
*lockres
,
1140 unsigned long flags
;
1143 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1144 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
1145 lockres_clear_flags(lockres
, OCFS2_LOCK_UPCONVERT_FINISHING
);
1147 lockres
->l_action
= OCFS2_AST_INVALID
;
1149 lockres
->l_unlock_action
= OCFS2_UNLOCK_INVALID
;
1150 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1152 wake_up(&lockres
->l_event
);
1156 /* Note: If we detect another process working on the lock (i.e.,
1157 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
1158 * to do the right thing in that case.
1160 static int ocfs2_lock_create(struct ocfs2_super
*osb
,
1161 struct ocfs2_lock_res
*lockres
,
1166 unsigned long flags
;
1171 mlog(0, "lock %s, level = %d, flags = %u\n", lockres
->l_name
, level
,
1174 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1175 if ((lockres
->l_flags
& OCFS2_LOCK_ATTACHED
) ||
1176 (lockres
->l_flags
& OCFS2_LOCK_BUSY
)) {
1177 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1181 lockres
->l_action
= OCFS2_AST_ATTACH
;
1182 lockres
->l_requested
= level
;
1183 lockres_or_flags(lockres
, OCFS2_LOCK_BUSY
);
1184 gen
= lockres_set_pending(lockres
);
1185 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1187 ret
= ocfs2_dlm_lock(osb
->cconn
,
1192 OCFS2_LOCK_ID_MAX_LEN
- 1,
1194 lockres_clear_pending(lockres
, gen
, osb
);
1196 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret
, lockres
);
1197 ocfs2_recover_from_dlm_error(lockres
, 1);
1200 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres
->l_name
);
1207 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res
*lockres
,
1210 unsigned long flags
;
1213 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1214 ret
= lockres
->l_flags
& flag
;
1215 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1220 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res
*lockres
)
1223 wait_event(lockres
->l_event
,
1224 !ocfs2_check_wait_flag(lockres
, OCFS2_LOCK_BUSY
));
1227 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res
*lockres
)
1230 wait_event(lockres
->l_event
,
1231 !ocfs2_check_wait_flag(lockres
, OCFS2_LOCK_REFRESHING
));
1234 /* predict what lock level we'll be dropping down to on behalf
1235 * of another node, and return true if the currently wanted
1236 * level will be compatible with it. */
1237 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res
*lockres
,
1240 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BLOCKED
));
1242 return wanted
<= ocfs2_highest_compat_lock_level(lockres
->l_blocking
);
1245 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter
*mw
)
1247 INIT_LIST_HEAD(&mw
->mw_item
);
1248 init_completion(&mw
->mw_complete
);
1249 ocfs2_init_start_time(mw
);
1252 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter
*mw
)
1254 wait_for_completion(&mw
->mw_complete
);
1255 /* Re-arm the completion in case we want to wait on it again */
1256 INIT_COMPLETION(mw
->mw_complete
);
1257 return mw
->mw_status
;
1260 static void lockres_add_mask_waiter(struct ocfs2_lock_res
*lockres
,
1261 struct ocfs2_mask_waiter
*mw
,
1265 BUG_ON(!list_empty(&mw
->mw_item
));
1267 assert_spin_locked(&lockres
->l_lock
);
1269 list_add_tail(&mw
->mw_item
, &lockres
->l_mask_waiters
);
1274 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
1275 * if the mask still hadn't reached its goal */
1276 static int lockres_remove_mask_waiter(struct ocfs2_lock_res
*lockres
,
1277 struct ocfs2_mask_waiter
*mw
)
1279 unsigned long flags
;
1282 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1283 if (!list_empty(&mw
->mw_item
)) {
1284 if ((lockres
->l_flags
& mw
->mw_mask
) != mw
->mw_goal
)
1287 list_del_init(&mw
->mw_item
);
1288 init_completion(&mw
->mw_complete
);
1290 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1296 static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter
*mw
,
1297 struct ocfs2_lock_res
*lockres
)
1301 ret
= wait_for_completion_interruptible(&mw
->mw_complete
);
1303 lockres_remove_mask_waiter(lockres
, mw
);
1305 ret
= mw
->mw_status
;
1306 /* Re-arm the completion in case we want to wait on it again */
1307 INIT_COMPLETION(mw
->mw_complete
);
1311 static int __ocfs2_cluster_lock(struct ocfs2_super
*osb
,
1312 struct ocfs2_lock_res
*lockres
,
1317 unsigned long caller_ip
)
1319 struct ocfs2_mask_waiter mw
;
1320 int wait
, catch_signals
= !(osb
->s_mount_opt
& OCFS2_MOUNT_NOINTR
);
1321 int ret
= 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1322 unsigned long flags
;
1324 int noqueue_attempted
= 0;
1328 ocfs2_init_mask_waiter(&mw
);
1330 if (lockres
->l_ops
->flags
& LOCK_TYPE_USES_LVB
)
1331 lkm_flags
|= DLM_LKF_VALBLK
;
1336 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1338 if (catch_signals
&& signal_pending(current
)) {
1343 mlog_bug_on_msg(lockres
->l_flags
& OCFS2_LOCK_FREEING
,
1344 "Cluster lock called on freeing lockres %s! flags "
1345 "0x%lx\n", lockres
->l_name
, lockres
->l_flags
);
1347 /* We only compare against the currently granted level
1348 * here. If the lock is blocked waiting on a downconvert,
1349 * we'll get caught below. */
1350 if (lockres
->l_flags
& OCFS2_LOCK_BUSY
&&
1351 level
> lockres
->l_level
) {
1352 /* is someone sitting in dlm_lock? If so, wait on
1354 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BUSY
, 0);
1359 if (lockres
->l_flags
& OCFS2_LOCK_UPCONVERT_FINISHING
) {
1361 * We've upconverted. If the lock now has a level we can
1362 * work with, we take it. If, however, the lock is not at the
1363 * required level, we go thru the full cycle. One way this could
1364 * happen is if a process requesting an upconvert to PR is
1365 * closely followed by another requesting upconvert to an EX.
1366 * If the process requesting EX lands here, we want it to
1367 * continue attempting to upconvert and let the process
1368 * requesting PR take the lock.
1369 * If multiple processes request upconvert to PR, the first one
1370 * here will take the lock. The others will have to go thru the
1371 * OCFS2_LOCK_BLOCKED check to ensure that there is no pending
1372 * downconvert request.
1374 if (level
<= lockres
->l_level
)
1375 goto update_holders
;
1378 if (lockres
->l_flags
& OCFS2_LOCK_BLOCKED
&&
1379 !ocfs2_may_continue_on_blocked_lock(lockres
, level
)) {
1380 /* is the lock is currently blocked on behalf of
1382 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BLOCKED
, 0);
1387 if (level
> lockres
->l_level
) {
1388 if (noqueue_attempted
> 0) {
1392 if (lkm_flags
& DLM_LKF_NOQUEUE
)
1393 noqueue_attempted
= 1;
1395 if (lockres
->l_action
!= OCFS2_AST_INVALID
)
1396 mlog(ML_ERROR
, "lockres %s has action %u pending\n",
1397 lockres
->l_name
, lockres
->l_action
);
1399 if (!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
)) {
1400 lockres
->l_action
= OCFS2_AST_ATTACH
;
1401 lkm_flags
&= ~DLM_LKF_CONVERT
;
1403 lockres
->l_action
= OCFS2_AST_CONVERT
;
1404 lkm_flags
|= DLM_LKF_CONVERT
;
1407 lockres
->l_requested
= level
;
1408 lockres_or_flags(lockres
, OCFS2_LOCK_BUSY
);
1409 gen
= lockres_set_pending(lockres
);
1410 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1412 BUG_ON(level
== DLM_LOCK_IV
);
1413 BUG_ON(level
== DLM_LOCK_NL
);
1415 mlog(0, "lock %s, convert from %d to level = %d\n",
1416 lockres
->l_name
, lockres
->l_level
, level
);
1418 /* call dlm_lock to upgrade lock now */
1419 ret
= ocfs2_dlm_lock(osb
->cconn
,
1424 OCFS2_LOCK_ID_MAX_LEN
- 1,
1426 lockres_clear_pending(lockres
, gen
, osb
);
1428 if (!(lkm_flags
& DLM_LKF_NOQUEUE
) ||
1430 ocfs2_log_dlm_error("ocfs2_dlm_lock",
1433 ocfs2_recover_from_dlm_error(lockres
, 1);
1437 mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n",
1440 /* At this point we've gone inside the dlm and need to
1441 * complete our work regardless. */
1444 /* wait for busy to clear and carry on */
1449 /* Ok, if we get here then we're good to go. */
1450 ocfs2_inc_holders(lockres
, level
);
1454 lockres_clear_flags(lockres
, OCFS2_LOCK_UPCONVERT_FINISHING
);
1456 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1459 * This is helping work around a lock inversion between the page lock
1460 * and dlm locks. One path holds the page lock while calling aops
1461 * which block acquiring dlm locks. The voting thread holds dlm
1462 * locks while acquiring page locks while down converting data locks.
1463 * This block is helping an aop path notice the inversion and back
1464 * off to unlock its page lock before trying the dlm lock again.
1466 if (wait
&& arg_flags
& OCFS2_LOCK_NONBLOCK
&&
1467 mw
.mw_mask
& (OCFS2_LOCK_BUSY
|OCFS2_LOCK_BLOCKED
)) {
1469 if (lockres_remove_mask_waiter(lockres
, &mw
))
1475 ret
= ocfs2_wait_for_mask(&mw
);
1480 ocfs2_update_lock_stats(lockres
, level
, &mw
, ret
);
1482 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1483 if (!ret
&& lockres
->l_lockdep_map
.key
!= NULL
) {
1484 if (level
== DLM_LOCK_PR
)
1485 rwsem_acquire_read(&lockres
->l_lockdep_map
, l_subclass
,
1486 !!(arg_flags
& OCFS2_META_LOCK_NOQUEUE
),
1489 rwsem_acquire(&lockres
->l_lockdep_map
, l_subclass
,
1490 !!(arg_flags
& OCFS2_META_LOCK_NOQUEUE
),
1498 static inline int ocfs2_cluster_lock(struct ocfs2_super
*osb
,
1499 struct ocfs2_lock_res
*lockres
,
1504 return __ocfs2_cluster_lock(osb
, lockres
, level
, lkm_flags
, arg_flags
,
1509 static void __ocfs2_cluster_unlock(struct ocfs2_super
*osb
,
1510 struct ocfs2_lock_res
*lockres
,
1512 unsigned long caller_ip
)
1514 unsigned long flags
;
1517 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1518 ocfs2_dec_holders(lockres
, level
);
1519 ocfs2_downconvert_on_unlock(osb
, lockres
);
1520 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1521 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1522 if (lockres
->l_lockdep_map
.key
!= NULL
)
1523 rwsem_release(&lockres
->l_lockdep_map
, 1, caller_ip
);
1528 static int ocfs2_create_new_lock(struct ocfs2_super
*osb
,
1529 struct ocfs2_lock_res
*lockres
,
1533 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
1534 unsigned long flags
;
1535 u32 lkm_flags
= local
? DLM_LKF_LOCAL
: 0;
1537 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1538 BUG_ON(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
);
1539 lockres_or_flags(lockres
, OCFS2_LOCK_LOCAL
);
1540 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1542 return ocfs2_lock_create(osb
, lockres
, level
, lkm_flags
);
1545 /* Grants us an EX lock on the data and metadata resources, skipping
1546 * the normal cluster directory lookup. Use this ONLY on newly created
1547 * inodes which other nodes can't possibly see, and which haven't been
1548 * hashed in the inode hash yet. This can give us a good performance
1549 * increase as it'll skip the network broadcast normally associated
1550 * with creating a new lock resource. */
1551 int ocfs2_create_new_inode_locks(struct inode
*inode
)
1554 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1557 BUG_ON(!ocfs2_inode_is_new(inode
));
1561 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1563 /* NOTE: That we don't increment any of the holder counts, nor
1564 * do we add anything to a journal handle. Since this is
1565 * supposed to be a new inode which the cluster doesn't know
1566 * about yet, there is no need to. As far as the LVB handling
1567 * is concerned, this is basically like acquiring an EX lock
1568 * on a resource which has an invalid one -- we'll set it
1569 * valid when we release the EX. */
1571 ret
= ocfs2_create_new_lock(osb
, &OCFS2_I(inode
)->ip_rw_lockres
, 1, 1);
1578 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
1579 * don't use a generation in their lock names.
1581 ret
= ocfs2_create_new_lock(osb
, &OCFS2_I(inode
)->ip_inode_lockres
, 1, 0);
1587 ret
= ocfs2_create_new_lock(osb
, &OCFS2_I(inode
)->ip_open_lockres
, 0, 0);
1598 int ocfs2_rw_lock(struct inode
*inode
, int write
)
1601 struct ocfs2_lock_res
*lockres
;
1602 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1608 mlog(0, "inode %llu take %s RW lock\n",
1609 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1610 write
? "EXMODE" : "PRMODE");
1612 if (ocfs2_mount_local(osb
)) {
1617 lockres
= &OCFS2_I(inode
)->ip_rw_lockres
;
1619 level
= write
? DLM_LOCK_EX
: DLM_LOCK_PR
;
1621 status
= ocfs2_cluster_lock(OCFS2_SB(inode
->i_sb
), lockres
, level
, 0,
1630 void ocfs2_rw_unlock(struct inode
*inode
, int write
)
1632 int level
= write
? DLM_LOCK_EX
: DLM_LOCK_PR
;
1633 struct ocfs2_lock_res
*lockres
= &OCFS2_I(inode
)->ip_rw_lockres
;
1634 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1638 mlog(0, "inode %llu drop %s RW lock\n",
1639 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1640 write
? "EXMODE" : "PRMODE");
1642 if (!ocfs2_mount_local(osb
))
1643 ocfs2_cluster_unlock(OCFS2_SB(inode
->i_sb
), lockres
, level
);
1649 * ocfs2_open_lock always get PR mode lock.
1651 int ocfs2_open_lock(struct inode
*inode
)
1654 struct ocfs2_lock_res
*lockres
;
1655 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1661 mlog(0, "inode %llu take PRMODE open lock\n",
1662 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1664 if (ocfs2_mount_local(osb
))
1667 lockres
= &OCFS2_I(inode
)->ip_open_lockres
;
1669 status
= ocfs2_cluster_lock(OCFS2_SB(inode
->i_sb
), lockres
,
1679 int ocfs2_try_open_lock(struct inode
*inode
, int write
)
1681 int status
= 0, level
;
1682 struct ocfs2_lock_res
*lockres
;
1683 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1689 mlog(0, "inode %llu try to take %s open lock\n",
1690 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1691 write
? "EXMODE" : "PRMODE");
1693 if (ocfs2_mount_local(osb
))
1696 lockres
= &OCFS2_I(inode
)->ip_open_lockres
;
1698 level
= write
? DLM_LOCK_EX
: DLM_LOCK_PR
;
1701 * The file system may already holding a PRMODE/EXMODE open lock.
1702 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
1703 * other nodes and the -EAGAIN will indicate to the caller that
1704 * this inode is still in use.
1706 status
= ocfs2_cluster_lock(OCFS2_SB(inode
->i_sb
), lockres
,
1707 level
, DLM_LKF_NOQUEUE
, 0);
1715 * ocfs2_open_unlock unlock PR and EX mode open locks.
1717 void ocfs2_open_unlock(struct inode
*inode
)
1719 struct ocfs2_lock_res
*lockres
= &OCFS2_I(inode
)->ip_open_lockres
;
1720 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1724 mlog(0, "inode %llu drop open lock\n",
1725 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1727 if (ocfs2_mount_local(osb
))
1730 if(lockres
->l_ro_holders
)
1731 ocfs2_cluster_unlock(OCFS2_SB(inode
->i_sb
), lockres
,
1733 if(lockres
->l_ex_holders
)
1734 ocfs2_cluster_unlock(OCFS2_SB(inode
->i_sb
), lockres
,
1741 static int ocfs2_flock_handle_signal(struct ocfs2_lock_res
*lockres
,
1745 struct ocfs2_super
*osb
= ocfs2_get_lockres_osb(lockres
);
1746 unsigned long flags
;
1747 struct ocfs2_mask_waiter mw
;
1749 ocfs2_init_mask_waiter(&mw
);
1752 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1753 if (lockres
->l_flags
& OCFS2_LOCK_BUSY
) {
1754 ret
= ocfs2_prepare_cancel_convert(osb
, lockres
);
1756 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1757 ret
= ocfs2_cancel_convert(osb
, lockres
);
1764 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BUSY
, 0);
1765 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1767 ocfs2_wait_for_mask(&mw
);
1773 * We may still have gotten the lock, in which case there's no
1774 * point to restarting the syscall.
1776 if (lockres
->l_level
== level
)
1779 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret
,
1780 lockres
->l_flags
, lockres
->l_level
, lockres
->l_action
);
1782 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1789 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1790 * flock() calls. The locking approach this requires is sufficiently
1791 * different from all other cluster lock types that we implement a
1792 * seperate path to the "low-level" dlm calls. In particular:
1794 * - No optimization of lock levels is done - we take at exactly
1795 * what's been requested.
1797 * - No lock caching is employed. We immediately downconvert to
1798 * no-lock at unlock time. This also means flock locks never go on
1799 * the blocking list).
1801 * - Since userspace can trivially deadlock itself with flock, we make
1802 * sure to allow cancellation of a misbehaving applications flock()
1805 * - Access to any flock lockres doesn't require concurrency, so we
1806 * can simplify the code by requiring the caller to guarantee
1807 * serialization of dlmglue flock calls.
1809 int ocfs2_file_lock(struct file
*file
, int ex
, int trylock
)
1811 int ret
, level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
1812 unsigned int lkm_flags
= trylock
? DLM_LKF_NOQUEUE
: 0;
1813 unsigned long flags
;
1814 struct ocfs2_file_private
*fp
= file
->private_data
;
1815 struct ocfs2_lock_res
*lockres
= &fp
->fp_flock
;
1816 struct ocfs2_super
*osb
= OCFS2_SB(file
->f_mapping
->host
->i_sb
);
1817 struct ocfs2_mask_waiter mw
;
1819 ocfs2_init_mask_waiter(&mw
);
1821 if ((lockres
->l_flags
& OCFS2_LOCK_BUSY
) ||
1822 (lockres
->l_level
> DLM_LOCK_NL
)) {
1824 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1825 "level: %u\n", lockres
->l_name
, lockres
->l_flags
,
1830 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1831 if (!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
)) {
1832 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BUSY
, 0);
1833 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1836 * Get the lock at NLMODE to start - that way we
1837 * can cancel the upconvert request if need be.
1839 ret
= ocfs2_lock_create(osb
, lockres
, DLM_LOCK_NL
, 0);
1845 ret
= ocfs2_wait_for_mask(&mw
);
1850 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1853 lockres
->l_action
= OCFS2_AST_CONVERT
;
1854 lkm_flags
|= DLM_LKF_CONVERT
;
1855 lockres
->l_requested
= level
;
1856 lockres_or_flags(lockres
, OCFS2_LOCK_BUSY
);
1858 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BUSY
, 0);
1859 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1861 ret
= ocfs2_dlm_lock(osb
->cconn
, level
, &lockres
->l_lksb
, lkm_flags
,
1862 lockres
->l_name
, OCFS2_LOCK_ID_MAX_LEN
- 1,
1865 if (!trylock
|| (ret
!= -EAGAIN
)) {
1866 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret
, lockres
);
1870 ocfs2_recover_from_dlm_error(lockres
, 1);
1871 lockres_remove_mask_waiter(lockres
, &mw
);
1875 ret
= ocfs2_wait_for_mask_interruptible(&mw
, lockres
);
1876 if (ret
== -ERESTARTSYS
) {
1878 * Userspace can cause deadlock itself with
1879 * flock(). Current behavior locally is to allow the
1880 * deadlock, but abort the system call if a signal is
1881 * received. We follow this example, otherwise a
1882 * poorly written program could sit in kernel until
1885 * Handling this is a bit more complicated for Ocfs2
1886 * though. We can't exit this function with an
1887 * outstanding lock request, so a cancel convert is
1888 * required. We intentionally overwrite 'ret' - if the
1889 * cancel fails and the lock was granted, it's easier
1890 * to just bubble success back up to the user.
1892 ret
= ocfs2_flock_handle_signal(lockres
, level
);
1893 } else if (!ret
&& (level
> lockres
->l_level
)) {
1894 /* Trylock failed asynchronously */
1901 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1902 lockres
->l_name
, ex
, trylock
, ret
);
1906 void ocfs2_file_unlock(struct file
*file
)
1910 unsigned long flags
;
1911 struct ocfs2_file_private
*fp
= file
->private_data
;
1912 struct ocfs2_lock_res
*lockres
= &fp
->fp_flock
;
1913 struct ocfs2_super
*osb
= OCFS2_SB(file
->f_mapping
->host
->i_sb
);
1914 struct ocfs2_mask_waiter mw
;
1916 ocfs2_init_mask_waiter(&mw
);
1918 if (!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
))
1921 if (lockres
->l_level
== DLM_LOCK_NL
)
1924 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1925 lockres
->l_name
, lockres
->l_flags
, lockres
->l_level
,
1928 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1930 * Fake a blocking ast for the downconvert code.
1932 lockres_or_flags(lockres
, OCFS2_LOCK_BLOCKED
);
1933 lockres
->l_blocking
= DLM_LOCK_EX
;
1935 gen
= ocfs2_prepare_downconvert(lockres
, DLM_LOCK_NL
);
1936 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BUSY
, 0);
1937 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1939 ret
= ocfs2_downconvert_lock(osb
, lockres
, DLM_LOCK_NL
, 0, gen
);
1945 ret
= ocfs2_wait_for_mask(&mw
);
1950 static void ocfs2_downconvert_on_unlock(struct ocfs2_super
*osb
,
1951 struct ocfs2_lock_res
*lockres
)
1957 /* If we know that another node is waiting on our lock, kick
1958 * the downconvert thread * pre-emptively when we reach a release
1960 if (lockres
->l_flags
& OCFS2_LOCK_BLOCKED
) {
1961 switch(lockres
->l_blocking
) {
1963 if (!lockres
->l_ex_holders
&& !lockres
->l_ro_holders
)
1967 if (!lockres
->l_ex_holders
)
1976 ocfs2_wake_downconvert_thread(osb
);
1981 #define OCFS2_SEC_BITS 34
1982 #define OCFS2_SEC_SHIFT (64 - 34)
1983 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1985 /* LVB only has room for 64 bits of time here so we pack it for
1987 static u64
ocfs2_pack_timespec(struct timespec
*spec
)
1990 u64 sec
= spec
->tv_sec
;
1991 u32 nsec
= spec
->tv_nsec
;
1993 res
= (sec
<< OCFS2_SEC_SHIFT
) | (nsec
& OCFS2_NSEC_MASK
);
1998 /* Call this with the lockres locked. I am reasonably sure we don't
1999 * need ip_lock in this function as anyone who would be changing those
2000 * values is supposed to be blocked in ocfs2_inode_lock right now. */
2001 static void __ocfs2_stuff_meta_lvb(struct inode
*inode
)
2003 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2004 struct ocfs2_lock_res
*lockres
= &oi
->ip_inode_lockres
;
2005 struct ocfs2_meta_lvb
*lvb
;
2009 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
2012 * Invalidate the LVB of a deleted inode - this way other
2013 * nodes are forced to go to disk and discover the new inode
2016 if (oi
->ip_flags
& OCFS2_INODE_DELETED
) {
2017 lvb
->lvb_version
= 0;
2021 lvb
->lvb_version
= OCFS2_LVB_VERSION
;
2022 lvb
->lvb_isize
= cpu_to_be64(i_size_read(inode
));
2023 lvb
->lvb_iclusters
= cpu_to_be32(oi
->ip_clusters
);
2024 lvb
->lvb_iuid
= cpu_to_be32(inode
->i_uid
);
2025 lvb
->lvb_igid
= cpu_to_be32(inode
->i_gid
);
2026 lvb
->lvb_imode
= cpu_to_be16(inode
->i_mode
);
2027 lvb
->lvb_inlink
= cpu_to_be16(inode
->i_nlink
);
2028 lvb
->lvb_iatime_packed
=
2029 cpu_to_be64(ocfs2_pack_timespec(&inode
->i_atime
));
2030 lvb
->lvb_ictime_packed
=
2031 cpu_to_be64(ocfs2_pack_timespec(&inode
->i_ctime
));
2032 lvb
->lvb_imtime_packed
=
2033 cpu_to_be64(ocfs2_pack_timespec(&inode
->i_mtime
));
2034 lvb
->lvb_iattr
= cpu_to_be32(oi
->ip_attr
);
2035 lvb
->lvb_idynfeatures
= cpu_to_be16(oi
->ip_dyn_features
);
2036 lvb
->lvb_igeneration
= cpu_to_be32(inode
->i_generation
);
2039 mlog_meta_lvb(0, lockres
);
2044 static void ocfs2_unpack_timespec(struct timespec
*spec
,
2047 spec
->tv_sec
= packed_time
>> OCFS2_SEC_SHIFT
;
2048 spec
->tv_nsec
= packed_time
& OCFS2_NSEC_MASK
;
2051 static void ocfs2_refresh_inode_from_lvb(struct inode
*inode
)
2053 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2054 struct ocfs2_lock_res
*lockres
= &oi
->ip_inode_lockres
;
2055 struct ocfs2_meta_lvb
*lvb
;
2059 mlog_meta_lvb(0, lockres
);
2061 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
2063 /* We're safe here without the lockres lock... */
2064 spin_lock(&oi
->ip_lock
);
2065 oi
->ip_clusters
= be32_to_cpu(lvb
->lvb_iclusters
);
2066 i_size_write(inode
, be64_to_cpu(lvb
->lvb_isize
));
2068 oi
->ip_attr
= be32_to_cpu(lvb
->lvb_iattr
);
2069 oi
->ip_dyn_features
= be16_to_cpu(lvb
->lvb_idynfeatures
);
2070 ocfs2_set_inode_flags(inode
);
2072 /* fast-symlinks are a special case */
2073 if (S_ISLNK(inode
->i_mode
) && !oi
->ip_clusters
)
2074 inode
->i_blocks
= 0;
2076 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
2078 inode
->i_uid
= be32_to_cpu(lvb
->lvb_iuid
);
2079 inode
->i_gid
= be32_to_cpu(lvb
->lvb_igid
);
2080 inode
->i_mode
= be16_to_cpu(lvb
->lvb_imode
);
2081 inode
->i_nlink
= be16_to_cpu(lvb
->lvb_inlink
);
2082 ocfs2_unpack_timespec(&inode
->i_atime
,
2083 be64_to_cpu(lvb
->lvb_iatime_packed
));
2084 ocfs2_unpack_timespec(&inode
->i_mtime
,
2085 be64_to_cpu(lvb
->lvb_imtime_packed
));
2086 ocfs2_unpack_timespec(&inode
->i_ctime
,
2087 be64_to_cpu(lvb
->lvb_ictime_packed
));
2088 spin_unlock(&oi
->ip_lock
);
2093 static inline int ocfs2_meta_lvb_is_trustable(struct inode
*inode
,
2094 struct ocfs2_lock_res
*lockres
)
2096 struct ocfs2_meta_lvb
*lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
2098 if (ocfs2_dlm_lvb_valid(&lockres
->l_lksb
)
2099 && lvb
->lvb_version
== OCFS2_LVB_VERSION
2100 && be32_to_cpu(lvb
->lvb_igeneration
) == inode
->i_generation
)
2105 /* Determine whether a lock resource needs to be refreshed, and
2106 * arbitrate who gets to refresh it.
2108 * 0 means no refresh needed.
2110 * > 0 means you need to refresh this and you MUST call
2111 * ocfs2_complete_lock_res_refresh afterwards. */
2112 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res
*lockres
)
2114 unsigned long flags
;
2120 spin_lock_irqsave(&lockres
->l_lock
, flags
);
2121 if (!(lockres
->l_flags
& OCFS2_LOCK_NEEDS_REFRESH
)) {
2122 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2126 if (lockres
->l_flags
& OCFS2_LOCK_REFRESHING
) {
2127 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2129 ocfs2_wait_on_refreshing_lock(lockres
);
2133 /* Ok, I'll be the one to refresh this lock. */
2134 lockres_or_flags(lockres
, OCFS2_LOCK_REFRESHING
);
2135 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2143 /* If status is non zero, I'll mark it as not being in refresh
2144 * anymroe, but i won't clear the needs refresh flag. */
2145 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res
*lockres
,
2148 unsigned long flags
;
2151 spin_lock_irqsave(&lockres
->l_lock
, flags
);
2152 lockres_clear_flags(lockres
, OCFS2_LOCK_REFRESHING
);
2154 lockres_clear_flags(lockres
, OCFS2_LOCK_NEEDS_REFRESH
);
2155 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2157 wake_up(&lockres
->l_event
);
2162 /* may or may not return a bh if it went to disk. */
2163 static int ocfs2_inode_lock_update(struct inode
*inode
,
2164 struct buffer_head
**bh
)
2167 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2168 struct ocfs2_lock_res
*lockres
= &oi
->ip_inode_lockres
;
2169 struct ocfs2_dinode
*fe
;
2170 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2174 if (ocfs2_mount_local(osb
))
2177 spin_lock(&oi
->ip_lock
);
2178 if (oi
->ip_flags
& OCFS2_INODE_DELETED
) {
2179 mlog(0, "Orphaned inode %llu was deleted while we "
2180 "were waiting on a lock. ip_flags = 0x%x\n",
2181 (unsigned long long)oi
->ip_blkno
, oi
->ip_flags
);
2182 spin_unlock(&oi
->ip_lock
);
2186 spin_unlock(&oi
->ip_lock
);
2188 if (!ocfs2_should_refresh_lock_res(lockres
))
2191 /* This will discard any caching information we might have had
2192 * for the inode metadata. */
2193 ocfs2_metadata_cache_purge(INODE_CACHE(inode
));
2195 ocfs2_extent_map_trunc(inode
, 0);
2197 if (ocfs2_meta_lvb_is_trustable(inode
, lockres
)) {
2198 mlog(0, "Trusting LVB on inode %llu\n",
2199 (unsigned long long)oi
->ip_blkno
);
2200 ocfs2_refresh_inode_from_lvb(inode
);
2202 /* Boo, we have to go to disk. */
2203 /* read bh, cast, ocfs2_refresh_inode */
2204 status
= ocfs2_read_inode_block(inode
, bh
);
2209 fe
= (struct ocfs2_dinode
*) (*bh
)->b_data
;
2211 /* This is a good chance to make sure we're not
2212 * locking an invalid object. ocfs2_read_inode_block()
2213 * already checked that the inode block is sane.
2215 * We bug on a stale inode here because we checked
2216 * above whether it was wiped from disk. The wiping
2217 * node provides a guarantee that we receive that
2218 * message and can mark the inode before dropping any
2219 * locks associated with it. */
2220 mlog_bug_on_msg(inode
->i_generation
!=
2221 le32_to_cpu(fe
->i_generation
),
2222 "Invalid dinode %llu disk generation: %u "
2223 "inode->i_generation: %u\n",
2224 (unsigned long long)oi
->ip_blkno
,
2225 le32_to_cpu(fe
->i_generation
),
2226 inode
->i_generation
);
2227 mlog_bug_on_msg(le64_to_cpu(fe
->i_dtime
) ||
2228 !(fe
->i_flags
& cpu_to_le32(OCFS2_VALID_FL
)),
2229 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
2230 (unsigned long long)oi
->ip_blkno
,
2231 (unsigned long long)le64_to_cpu(fe
->i_dtime
),
2232 le32_to_cpu(fe
->i_flags
));
2234 ocfs2_refresh_inode(inode
, fe
);
2235 ocfs2_track_lock_refresh(lockres
);
2240 ocfs2_complete_lock_res_refresh(lockres
, status
);
2246 static int ocfs2_assign_bh(struct inode
*inode
,
2247 struct buffer_head
**ret_bh
,
2248 struct buffer_head
*passed_bh
)
2253 /* Ok, the update went to disk for us, use the
2255 *ret_bh
= passed_bh
;
2261 status
= ocfs2_read_inode_block(inode
, ret_bh
);
2269 * returns < 0 error if the callback will never be called, otherwise
2270 * the result of the lock will be communicated via the callback.
2272 int ocfs2_inode_lock_full_nested(struct inode
*inode
,
2273 struct buffer_head
**ret_bh
,
2278 int status
, level
, acquired
;
2280 struct ocfs2_lock_res
*lockres
= NULL
;
2281 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2282 struct buffer_head
*local_bh
= NULL
;
2288 mlog(0, "inode %llu, take %s META lock\n",
2289 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2290 ex
? "EXMODE" : "PRMODE");
2294 /* We'll allow faking a readonly metadata lock for
2296 if (ocfs2_is_hard_readonly(osb
)) {
2302 if (ocfs2_mount_local(osb
))
2305 if (!(arg_flags
& OCFS2_META_LOCK_RECOVERY
))
2306 ocfs2_wait_for_recovery(osb
);
2308 lockres
= &OCFS2_I(inode
)->ip_inode_lockres
;
2309 level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2311 if (arg_flags
& OCFS2_META_LOCK_NOQUEUE
)
2312 dlm_flags
|= DLM_LKF_NOQUEUE
;
2314 status
= __ocfs2_cluster_lock(osb
, lockres
, level
, dlm_flags
,
2315 arg_flags
, subclass
, _RET_IP_
);
2317 if (status
!= -EAGAIN
&& status
!= -EIOCBRETRY
)
2322 /* Notify the error cleanup path to drop the cluster lock. */
2325 /* We wait twice because a node may have died while we were in
2326 * the lower dlm layers. The second time though, we've
2327 * committed to owning this lock so we don't allow signals to
2328 * abort the operation. */
2329 if (!(arg_flags
& OCFS2_META_LOCK_RECOVERY
))
2330 ocfs2_wait_for_recovery(osb
);
2334 * We only see this flag if we're being called from
2335 * ocfs2_read_locked_inode(). It means we're locking an inode
2336 * which hasn't been populated yet, so clear the refresh flag
2337 * and let the caller handle it.
2339 if (inode
->i_state
& I_NEW
) {
2342 ocfs2_complete_lock_res_refresh(lockres
, 0);
2346 /* This is fun. The caller may want a bh back, or it may
2347 * not. ocfs2_inode_lock_update definitely wants one in, but
2348 * may or may not read one, depending on what's in the
2349 * LVB. The result of all of this is that we've *only* gone to
2350 * disk if we have to, so the complexity is worthwhile. */
2351 status
= ocfs2_inode_lock_update(inode
, &local_bh
);
2353 if (status
!= -ENOENT
)
2359 status
= ocfs2_assign_bh(inode
, ret_bh
, local_bh
);
2368 if (ret_bh
&& (*ret_bh
)) {
2373 ocfs2_inode_unlock(inode
, ex
);
2384 * This is working around a lock inversion between tasks acquiring DLM
2385 * locks while holding a page lock and the downconvert thread which
2386 * blocks dlm lock acquiry while acquiring page locks.
2388 * ** These _with_page variantes are only intended to be called from aop
2389 * methods that hold page locks and return a very specific *positive* error
2390 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2392 * The DLM is called such that it returns -EAGAIN if it would have
2393 * blocked waiting for the downconvert thread. In that case we unlock
2394 * our page so the downconvert thread can make progress. Once we've
2395 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2396 * that called us can bubble that back up into the VFS who will then
2397 * immediately retry the aop call.
2399 * We do a blocking lock and immediate unlock before returning, though, so that
2400 * the lock has a great chance of being cached on this node by the time the VFS
2401 * calls back to retry the aop. This has a potential to livelock as nodes
2402 * ping locks back and forth, but that's a risk we're willing to take to avoid
2403 * the lock inversion simply.
2405 int ocfs2_inode_lock_with_page(struct inode
*inode
,
2406 struct buffer_head
**ret_bh
,
2412 ret
= ocfs2_inode_lock_full(inode
, ret_bh
, ex
, OCFS2_LOCK_NONBLOCK
);
2413 if (ret
== -EAGAIN
) {
2415 if (ocfs2_inode_lock(inode
, ret_bh
, ex
) == 0)
2416 ocfs2_inode_unlock(inode
, ex
);
2417 ret
= AOP_TRUNCATED_PAGE
;
2423 int ocfs2_inode_lock_atime(struct inode
*inode
,
2424 struct vfsmount
*vfsmnt
,
2430 ret
= ocfs2_inode_lock(inode
, NULL
, 0);
2437 * If we should update atime, we will get EX lock,
2438 * otherwise we just get PR lock.
2440 if (ocfs2_should_update_atime(inode
, vfsmnt
)) {
2441 struct buffer_head
*bh
= NULL
;
2443 ocfs2_inode_unlock(inode
, 0);
2444 ret
= ocfs2_inode_lock(inode
, &bh
, 1);
2450 if (ocfs2_should_update_atime(inode
, vfsmnt
))
2451 ocfs2_update_inode_atime(inode
, bh
);
2461 void ocfs2_inode_unlock(struct inode
*inode
,
2464 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2465 struct ocfs2_lock_res
*lockres
= &OCFS2_I(inode
)->ip_inode_lockres
;
2466 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2470 mlog(0, "inode %llu drop %s META lock\n",
2471 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2472 ex
? "EXMODE" : "PRMODE");
2474 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode
->i_sb
)) &&
2475 !ocfs2_mount_local(osb
))
2476 ocfs2_cluster_unlock(OCFS2_SB(inode
->i_sb
), lockres
, level
);
2481 int ocfs2_orphan_scan_lock(struct ocfs2_super
*osb
, u32
*seqno
)
2483 struct ocfs2_lock_res
*lockres
;
2484 struct ocfs2_orphan_scan_lvb
*lvb
;
2487 if (ocfs2_is_hard_readonly(osb
))
2490 if (ocfs2_mount_local(osb
))
2493 lockres
= &osb
->osb_orphan_scan
.os_lockres
;
2494 status
= ocfs2_cluster_lock(osb
, lockres
, DLM_LOCK_EX
, 0, 0);
2498 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
2499 if (ocfs2_dlm_lvb_valid(&lockres
->l_lksb
) &&
2500 lvb
->lvb_version
== OCFS2_ORPHAN_LVB_VERSION
)
2501 *seqno
= be32_to_cpu(lvb
->lvb_os_seqno
);
2503 *seqno
= osb
->osb_orphan_scan
.os_seqno
+ 1;
2508 void ocfs2_orphan_scan_unlock(struct ocfs2_super
*osb
, u32 seqno
)
2510 struct ocfs2_lock_res
*lockres
;
2511 struct ocfs2_orphan_scan_lvb
*lvb
;
2513 if (!ocfs2_is_hard_readonly(osb
) && !ocfs2_mount_local(osb
)) {
2514 lockres
= &osb
->osb_orphan_scan
.os_lockres
;
2515 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
2516 lvb
->lvb_version
= OCFS2_ORPHAN_LVB_VERSION
;
2517 lvb
->lvb_os_seqno
= cpu_to_be32(seqno
);
2518 ocfs2_cluster_unlock(osb
, lockres
, DLM_LOCK_EX
);
2522 int ocfs2_super_lock(struct ocfs2_super
*osb
,
2526 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2527 struct ocfs2_lock_res
*lockres
= &osb
->osb_super_lockres
;
2531 if (ocfs2_is_hard_readonly(osb
))
2534 if (ocfs2_mount_local(osb
))
2537 status
= ocfs2_cluster_lock(osb
, lockres
, level
, 0, 0);
2543 /* The super block lock path is really in the best position to
2544 * know when resources covered by the lock need to be
2545 * refreshed, so we do it here. Of course, making sense of
2546 * everything is up to the caller :) */
2547 status
= ocfs2_should_refresh_lock_res(lockres
);
2553 status
= ocfs2_refresh_slot_info(osb
);
2555 ocfs2_complete_lock_res_refresh(lockres
, status
);
2559 ocfs2_track_lock_refresh(lockres
);
2566 void ocfs2_super_unlock(struct ocfs2_super
*osb
,
2569 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2570 struct ocfs2_lock_res
*lockres
= &osb
->osb_super_lockres
;
2572 if (!ocfs2_mount_local(osb
))
2573 ocfs2_cluster_unlock(osb
, lockres
, level
);
2576 int ocfs2_rename_lock(struct ocfs2_super
*osb
)
2579 struct ocfs2_lock_res
*lockres
= &osb
->osb_rename_lockres
;
2581 if (ocfs2_is_hard_readonly(osb
))
2584 if (ocfs2_mount_local(osb
))
2587 status
= ocfs2_cluster_lock(osb
, lockres
, DLM_LOCK_EX
, 0, 0);
2594 void ocfs2_rename_unlock(struct ocfs2_super
*osb
)
2596 struct ocfs2_lock_res
*lockres
= &osb
->osb_rename_lockres
;
2598 if (!ocfs2_mount_local(osb
))
2599 ocfs2_cluster_unlock(osb
, lockres
, DLM_LOCK_EX
);
2602 int ocfs2_nfs_sync_lock(struct ocfs2_super
*osb
, int ex
)
2605 struct ocfs2_lock_res
*lockres
= &osb
->osb_nfs_sync_lockres
;
2607 if (ocfs2_is_hard_readonly(osb
))
2610 if (ocfs2_mount_local(osb
))
2613 status
= ocfs2_cluster_lock(osb
, lockres
, ex
? LKM_EXMODE
: LKM_PRMODE
,
2616 mlog(ML_ERROR
, "lock on nfs sync lock failed %d\n", status
);
2621 void ocfs2_nfs_sync_unlock(struct ocfs2_super
*osb
, int ex
)
2623 struct ocfs2_lock_res
*lockres
= &osb
->osb_nfs_sync_lockres
;
2625 if (!ocfs2_mount_local(osb
))
2626 ocfs2_cluster_unlock(osb
, lockres
,
2627 ex
? LKM_EXMODE
: LKM_PRMODE
);
2630 int ocfs2_dentry_lock(struct dentry
*dentry
, int ex
)
2633 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2634 struct ocfs2_dentry_lock
*dl
= dentry
->d_fsdata
;
2635 struct ocfs2_super
*osb
= OCFS2_SB(dentry
->d_sb
);
2639 if (ocfs2_is_hard_readonly(osb
))
2642 if (ocfs2_mount_local(osb
))
2645 ret
= ocfs2_cluster_lock(osb
, &dl
->dl_lockres
, level
, 0, 0);
2652 void ocfs2_dentry_unlock(struct dentry
*dentry
, int ex
)
2654 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2655 struct ocfs2_dentry_lock
*dl
= dentry
->d_fsdata
;
2656 struct ocfs2_super
*osb
= OCFS2_SB(dentry
->d_sb
);
2658 if (!ocfs2_mount_local(osb
))
2659 ocfs2_cluster_unlock(osb
, &dl
->dl_lockres
, level
);
2662 /* Reference counting of the dlm debug structure. We want this because
2663 * open references on the debug inodes can live on after a mount, so
2664 * we can't rely on the ocfs2_super to always exist. */
2665 static void ocfs2_dlm_debug_free(struct kref
*kref
)
2667 struct ocfs2_dlm_debug
*dlm_debug
;
2669 dlm_debug
= container_of(kref
, struct ocfs2_dlm_debug
, d_refcnt
);
2674 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug
*dlm_debug
)
2677 kref_put(&dlm_debug
->d_refcnt
, ocfs2_dlm_debug_free
);
2680 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug
*debug
)
2682 kref_get(&debug
->d_refcnt
);
2685 struct ocfs2_dlm_debug
*ocfs2_new_dlm_debug(void)
2687 struct ocfs2_dlm_debug
*dlm_debug
;
2689 dlm_debug
= kmalloc(sizeof(struct ocfs2_dlm_debug
), GFP_KERNEL
);
2691 mlog_errno(-ENOMEM
);
2695 kref_init(&dlm_debug
->d_refcnt
);
2696 INIT_LIST_HEAD(&dlm_debug
->d_lockres_tracking
);
2697 dlm_debug
->d_locking_state
= NULL
;
2702 /* Access to this is arbitrated for us via seq_file->sem. */
2703 struct ocfs2_dlm_seq_priv
{
2704 struct ocfs2_dlm_debug
*p_dlm_debug
;
2705 struct ocfs2_lock_res p_iter_res
;
2706 struct ocfs2_lock_res p_tmp_res
;
2709 static struct ocfs2_lock_res
*ocfs2_dlm_next_res(struct ocfs2_lock_res
*start
,
2710 struct ocfs2_dlm_seq_priv
*priv
)
2712 struct ocfs2_lock_res
*iter
, *ret
= NULL
;
2713 struct ocfs2_dlm_debug
*dlm_debug
= priv
->p_dlm_debug
;
2715 assert_spin_locked(&ocfs2_dlm_tracking_lock
);
2717 list_for_each_entry(iter
, &start
->l_debug_list
, l_debug_list
) {
2718 /* discover the head of the list */
2719 if (&iter
->l_debug_list
== &dlm_debug
->d_lockres_tracking
) {
2720 mlog(0, "End of list found, %p\n", ret
);
2724 /* We track our "dummy" iteration lockres' by a NULL
2726 if (iter
->l_ops
!= NULL
) {
2735 static void *ocfs2_dlm_seq_start(struct seq_file
*m
, loff_t
*pos
)
2737 struct ocfs2_dlm_seq_priv
*priv
= m
->private;
2738 struct ocfs2_lock_res
*iter
;
2740 spin_lock(&ocfs2_dlm_tracking_lock
);
2741 iter
= ocfs2_dlm_next_res(&priv
->p_iter_res
, priv
);
2743 /* Since lockres' have the lifetime of their container
2744 * (which can be inodes, ocfs2_supers, etc) we want to
2745 * copy this out to a temporary lockres while still
2746 * under the spinlock. Obviously after this we can't
2747 * trust any pointers on the copy returned, but that's
2748 * ok as the information we want isn't typically held
2750 priv
->p_tmp_res
= *iter
;
2751 iter
= &priv
->p_tmp_res
;
2753 spin_unlock(&ocfs2_dlm_tracking_lock
);
2758 static void ocfs2_dlm_seq_stop(struct seq_file
*m
, void *v
)
2762 static void *ocfs2_dlm_seq_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2764 struct ocfs2_dlm_seq_priv
*priv
= m
->private;
2765 struct ocfs2_lock_res
*iter
= v
;
2766 struct ocfs2_lock_res
*dummy
= &priv
->p_iter_res
;
2768 spin_lock(&ocfs2_dlm_tracking_lock
);
2769 iter
= ocfs2_dlm_next_res(iter
, priv
);
2770 list_del_init(&dummy
->l_debug_list
);
2772 list_add(&dummy
->l_debug_list
, &iter
->l_debug_list
);
2773 priv
->p_tmp_res
= *iter
;
2774 iter
= &priv
->p_tmp_res
;
2776 spin_unlock(&ocfs2_dlm_tracking_lock
);
2781 /* So that debugfs.ocfs2 can determine which format is being used */
2782 #define OCFS2_DLM_DEBUG_STR_VERSION 2
2783 static int ocfs2_dlm_seq_show(struct seq_file
*m
, void *v
)
2787 struct ocfs2_lock_res
*lockres
= v
;
2792 seq_printf(m
, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION
);
2794 if (lockres
->l_type
== OCFS2_LOCK_TYPE_DENTRY
)
2795 seq_printf(m
, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START
- 1,
2797 (unsigned int)ocfs2_get_dentry_lock_ino(lockres
));
2799 seq_printf(m
, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN
, lockres
->l_name
);
2801 seq_printf(m
, "%d\t"
2812 lockres
->l_unlock_action
,
2813 lockres
->l_ro_holders
,
2814 lockres
->l_ex_holders
,
2815 lockres
->l_requested
,
2816 lockres
->l_blocking
);
2818 /* Dump the raw LVB */
2819 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
2820 for(i
= 0; i
< DLM_LVB_LEN
; i
++)
2821 seq_printf(m
, "0x%x\t", lvb
[i
]);
2823 #ifdef CONFIG_OCFS2_FS_STATS
2824 # define lock_num_prmode(_l) (_l)->l_lock_num_prmode
2825 # define lock_num_exmode(_l) (_l)->l_lock_num_exmode
2826 # define lock_num_prmode_failed(_l) (_l)->l_lock_num_prmode_failed
2827 # define lock_num_exmode_failed(_l) (_l)->l_lock_num_exmode_failed
2828 # define lock_total_prmode(_l) (_l)->l_lock_total_prmode
2829 # define lock_total_exmode(_l) (_l)->l_lock_total_exmode
2830 # define lock_max_prmode(_l) (_l)->l_lock_max_prmode
2831 # define lock_max_exmode(_l) (_l)->l_lock_max_exmode
2832 # define lock_refresh(_l) (_l)->l_lock_refresh
2834 # define lock_num_prmode(_l) (0ULL)
2835 # define lock_num_exmode(_l) (0ULL)
2836 # define lock_num_prmode_failed(_l) (0)
2837 # define lock_num_exmode_failed(_l) (0)
2838 # define lock_total_prmode(_l) (0ULL)
2839 # define lock_total_exmode(_l) (0ULL)
2840 # define lock_max_prmode(_l) (0)
2841 # define lock_max_exmode(_l) (0)
2842 # define lock_refresh(_l) (0)
2844 /* The following seq_print was added in version 2 of this output */
2845 seq_printf(m
, "%llu\t"
2854 lock_num_prmode(lockres
),
2855 lock_num_exmode(lockres
),
2856 lock_num_prmode_failed(lockres
),
2857 lock_num_exmode_failed(lockres
),
2858 lock_total_prmode(lockres
),
2859 lock_total_exmode(lockres
),
2860 lock_max_prmode(lockres
),
2861 lock_max_exmode(lockres
),
2862 lock_refresh(lockres
));
2865 seq_printf(m
, "\n");
2869 static const struct seq_operations ocfs2_dlm_seq_ops
= {
2870 .start
= ocfs2_dlm_seq_start
,
2871 .stop
= ocfs2_dlm_seq_stop
,
2872 .next
= ocfs2_dlm_seq_next
,
2873 .show
= ocfs2_dlm_seq_show
,
2876 static int ocfs2_dlm_debug_release(struct inode
*inode
, struct file
*file
)
2878 struct seq_file
*seq
= (struct seq_file
*) file
->private_data
;
2879 struct ocfs2_dlm_seq_priv
*priv
= seq
->private;
2880 struct ocfs2_lock_res
*res
= &priv
->p_iter_res
;
2882 ocfs2_remove_lockres_tracking(res
);
2883 ocfs2_put_dlm_debug(priv
->p_dlm_debug
);
2884 return seq_release_private(inode
, file
);
2887 static int ocfs2_dlm_debug_open(struct inode
*inode
, struct file
*file
)
2890 struct ocfs2_dlm_seq_priv
*priv
;
2891 struct seq_file
*seq
;
2892 struct ocfs2_super
*osb
;
2894 priv
= kzalloc(sizeof(struct ocfs2_dlm_seq_priv
), GFP_KERNEL
);
2900 osb
= inode
->i_private
;
2901 ocfs2_get_dlm_debug(osb
->osb_dlm_debug
);
2902 priv
->p_dlm_debug
= osb
->osb_dlm_debug
;
2903 INIT_LIST_HEAD(&priv
->p_iter_res
.l_debug_list
);
2905 ret
= seq_open(file
, &ocfs2_dlm_seq_ops
);
2912 seq
= (struct seq_file
*) file
->private_data
;
2913 seq
->private = priv
;
2915 ocfs2_add_lockres_tracking(&priv
->p_iter_res
,
2922 static const struct file_operations ocfs2_dlm_debug_fops
= {
2923 .open
= ocfs2_dlm_debug_open
,
2924 .release
= ocfs2_dlm_debug_release
,
2926 .llseek
= seq_lseek
,
2929 static int ocfs2_dlm_init_debug(struct ocfs2_super
*osb
)
2932 struct ocfs2_dlm_debug
*dlm_debug
= osb
->osb_dlm_debug
;
2934 dlm_debug
->d_locking_state
= debugfs_create_file("locking_state",
2936 osb
->osb_debug_root
,
2938 &ocfs2_dlm_debug_fops
);
2939 if (!dlm_debug
->d_locking_state
) {
2942 "Unable to create locking state debugfs file.\n");
2946 ocfs2_get_dlm_debug(dlm_debug
);
2951 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super
*osb
)
2953 struct ocfs2_dlm_debug
*dlm_debug
= osb
->osb_dlm_debug
;
2956 debugfs_remove(dlm_debug
->d_locking_state
);
2957 ocfs2_put_dlm_debug(dlm_debug
);
2961 int ocfs2_dlm_init(struct ocfs2_super
*osb
)
2964 struct ocfs2_cluster_connection
*conn
= NULL
;
2968 if (ocfs2_mount_local(osb
)) {
2973 status
= ocfs2_dlm_init_debug(osb
);
2979 /* launch downconvert thread */
2980 osb
->dc_task
= kthread_run(ocfs2_downconvert_thread
, osb
, "ocfs2dc");
2981 if (IS_ERR(osb
->dc_task
)) {
2982 status
= PTR_ERR(osb
->dc_task
);
2983 osb
->dc_task
= NULL
;
2988 /* for now, uuid == domain */
2989 status
= ocfs2_cluster_connect(osb
->osb_cluster_stack
,
2991 strlen(osb
->uuid_str
),
2992 ocfs2_do_node_down
, osb
,
2999 status
= ocfs2_cluster_this_node(&osb
->node_num
);
3003 "could not find this host's node number\n");
3004 ocfs2_cluster_disconnect(conn
, 0);
3009 ocfs2_super_lock_res_init(&osb
->osb_super_lockres
, osb
);
3010 ocfs2_rename_lock_res_init(&osb
->osb_rename_lockres
, osb
);
3011 ocfs2_nfs_sync_lock_res_init(&osb
->osb_nfs_sync_lockres
, osb
);
3012 ocfs2_orphan_scan_lock_res_init(&osb
->osb_orphan_scan
.os_lockres
, osb
);
3019 ocfs2_dlm_shutdown_debug(osb
);
3021 kthread_stop(osb
->dc_task
);
3028 void ocfs2_dlm_shutdown(struct ocfs2_super
*osb
,
3033 ocfs2_drop_osb_locks(osb
);
3036 * Now that we have dropped all locks and ocfs2_dismount_volume()
3037 * has disabled recovery, the DLM won't be talking to us. It's
3038 * safe to tear things down before disconnecting the cluster.
3042 kthread_stop(osb
->dc_task
);
3043 osb
->dc_task
= NULL
;
3046 ocfs2_lock_res_free(&osb
->osb_super_lockres
);
3047 ocfs2_lock_res_free(&osb
->osb_rename_lockres
);
3048 ocfs2_lock_res_free(&osb
->osb_nfs_sync_lockres
);
3049 ocfs2_lock_res_free(&osb
->osb_orphan_scan
.os_lockres
);
3051 ocfs2_cluster_disconnect(osb
->cconn
, hangup_pending
);
3054 ocfs2_dlm_shutdown_debug(osb
);
3059 static void ocfs2_unlock_ast(void *opaque
, int error
)
3061 struct ocfs2_lock_res
*lockres
= opaque
;
3062 unsigned long flags
;
3066 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres
->l_name
,
3067 lockres
->l_unlock_action
);
3069 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3071 mlog(ML_ERROR
, "Dlm passes error %d for lock %s, "
3072 "unlock_action %d\n", error
, lockres
->l_name
,
3073 lockres
->l_unlock_action
);
3074 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3079 switch(lockres
->l_unlock_action
) {
3080 case OCFS2_UNLOCK_CANCEL_CONVERT
:
3081 mlog(0, "Cancel convert success for %s\n", lockres
->l_name
);
3082 lockres
->l_action
= OCFS2_AST_INVALID
;
3083 /* Downconvert thread may have requeued this lock, we
3084 * need to wake it. */
3085 if (lockres
->l_flags
& OCFS2_LOCK_BLOCKED
)
3086 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres
));
3088 case OCFS2_UNLOCK_DROP_LOCK
:
3089 lockres
->l_level
= DLM_LOCK_IV
;
3095 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
3096 lockres
->l_unlock_action
= OCFS2_UNLOCK_INVALID
;
3097 wake_up(&lockres
->l_event
);
3098 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3103 static int ocfs2_drop_lock(struct ocfs2_super
*osb
,
3104 struct ocfs2_lock_res
*lockres
)
3107 unsigned long flags
;
3110 /* We didn't get anywhere near actually using this lockres. */
3111 if (!(lockres
->l_flags
& OCFS2_LOCK_INITIALIZED
))
3114 if (lockres
->l_ops
->flags
& LOCK_TYPE_USES_LVB
)
3115 lkm_flags
|= DLM_LKF_VALBLK
;
3117 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3119 mlog_bug_on_msg(!(lockres
->l_flags
& OCFS2_LOCK_FREEING
),
3120 "lockres %s, flags 0x%lx\n",
3121 lockres
->l_name
, lockres
->l_flags
);
3123 while (lockres
->l_flags
& OCFS2_LOCK_BUSY
) {
3124 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
3125 "%u, unlock_action = %u\n",
3126 lockres
->l_name
, lockres
->l_flags
, lockres
->l_action
,
3127 lockres
->l_unlock_action
);
3129 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3131 /* XXX: Today we just wait on any busy
3132 * locks... Perhaps we need to cancel converts in the
3134 ocfs2_wait_on_busy_lock(lockres
);
3136 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3139 if (lockres
->l_ops
->flags
& LOCK_TYPE_USES_LVB
) {
3140 if (lockres
->l_flags
& OCFS2_LOCK_ATTACHED
&&
3141 lockres
->l_level
== DLM_LOCK_EX
&&
3142 !(lockres
->l_flags
& OCFS2_LOCK_NEEDS_REFRESH
))
3143 lockres
->l_ops
->set_lvb(lockres
);
3146 if (lockres
->l_flags
& OCFS2_LOCK_BUSY
)
3147 mlog(ML_ERROR
, "destroying busy lock: \"%s\"\n",
3149 if (lockres
->l_flags
& OCFS2_LOCK_BLOCKED
)
3150 mlog(0, "destroying blocked lock: \"%s\"\n", lockres
->l_name
);
3152 if (!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
)) {
3153 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3157 lockres_clear_flags(lockres
, OCFS2_LOCK_ATTACHED
);
3159 /* make sure we never get here while waiting for an ast to
3161 BUG_ON(lockres
->l_action
!= OCFS2_AST_INVALID
);
3163 /* is this necessary? */
3164 lockres_or_flags(lockres
, OCFS2_LOCK_BUSY
);
3165 lockres
->l_unlock_action
= OCFS2_UNLOCK_DROP_LOCK
;
3166 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3168 mlog(0, "lock %s\n", lockres
->l_name
);
3170 ret
= ocfs2_dlm_unlock(osb
->cconn
, &lockres
->l_lksb
, lkm_flags
,
3173 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret
, lockres
);
3174 mlog(ML_ERROR
, "lockres flags: %lu\n", lockres
->l_flags
);
3175 ocfs2_dlm_dump_lksb(&lockres
->l_lksb
);
3178 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
3181 ocfs2_wait_on_busy_lock(lockres
);
3187 /* Mark the lockres as being dropped. It will no longer be
3188 * queued if blocking, but we still may have to wait on it
3189 * being dequeued from the downconvert thread before we can consider
3192 * You can *not* attempt to call cluster_lock on this lockres anymore. */
3193 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res
*lockres
)
3196 struct ocfs2_mask_waiter mw
;
3197 unsigned long flags
;
3199 ocfs2_init_mask_waiter(&mw
);
3201 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3202 lockres
->l_flags
|= OCFS2_LOCK_FREEING
;
3203 while (lockres
->l_flags
& OCFS2_LOCK_QUEUED
) {
3204 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_QUEUED
, 0);
3205 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3207 mlog(0, "Waiting on lockres %s\n", lockres
->l_name
);
3209 status
= ocfs2_wait_for_mask(&mw
);
3213 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3215 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3218 void ocfs2_simple_drop_lockres(struct ocfs2_super
*osb
,
3219 struct ocfs2_lock_res
*lockres
)
3223 ocfs2_mark_lockres_freeing(lockres
);
3224 ret
= ocfs2_drop_lock(osb
, lockres
);
3229 static void ocfs2_drop_osb_locks(struct ocfs2_super
*osb
)
3231 ocfs2_simple_drop_lockres(osb
, &osb
->osb_super_lockres
);
3232 ocfs2_simple_drop_lockres(osb
, &osb
->osb_rename_lockres
);
3233 ocfs2_simple_drop_lockres(osb
, &osb
->osb_nfs_sync_lockres
);
3234 ocfs2_simple_drop_lockres(osb
, &osb
->osb_orphan_scan
.os_lockres
);
3237 int ocfs2_drop_inode_locks(struct inode
*inode
)
3243 /* No need to call ocfs2_mark_lockres_freeing here -
3244 * ocfs2_clear_inode has done it for us. */
3246 err
= ocfs2_drop_lock(OCFS2_SB(inode
->i_sb
),
3247 &OCFS2_I(inode
)->ip_open_lockres
);
3253 err
= ocfs2_drop_lock(OCFS2_SB(inode
->i_sb
),
3254 &OCFS2_I(inode
)->ip_inode_lockres
);
3257 if (err
< 0 && !status
)
3260 err
= ocfs2_drop_lock(OCFS2_SB(inode
->i_sb
),
3261 &OCFS2_I(inode
)->ip_rw_lockres
);
3264 if (err
< 0 && !status
)
3271 static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res
*lockres
,
3274 assert_spin_locked(&lockres
->l_lock
);
3276 BUG_ON(lockres
->l_blocking
<= DLM_LOCK_NL
);
3278 if (lockres
->l_level
<= new_level
) {
3279 mlog(ML_ERROR
, "lockres->l_level (%d) <= new_level (%d)\n",
3280 lockres
->l_level
, new_level
);
3284 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
3285 lockres
->l_name
, new_level
, lockres
->l_blocking
);
3287 lockres
->l_action
= OCFS2_AST_DOWNCONVERT
;
3288 lockres
->l_requested
= new_level
;
3289 lockres_or_flags(lockres
, OCFS2_LOCK_BUSY
);
3290 return lockres_set_pending(lockres
);
3293 static int ocfs2_downconvert_lock(struct ocfs2_super
*osb
,
3294 struct ocfs2_lock_res
*lockres
,
3297 unsigned int generation
)
3300 u32 dlm_flags
= DLM_LKF_CONVERT
;
3305 dlm_flags
|= DLM_LKF_VALBLK
;
3307 ret
= ocfs2_dlm_lock(osb
->cconn
,
3312 OCFS2_LOCK_ID_MAX_LEN
- 1,
3314 lockres_clear_pending(lockres
, generation
, osb
);
3316 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret
, lockres
);
3317 ocfs2_recover_from_dlm_error(lockres
, 1);
3327 /* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
3328 static int ocfs2_prepare_cancel_convert(struct ocfs2_super
*osb
,
3329 struct ocfs2_lock_res
*lockres
)
3331 assert_spin_locked(&lockres
->l_lock
);
3334 mlog(0, "lock %s\n", lockres
->l_name
);
3336 if (lockres
->l_unlock_action
== OCFS2_UNLOCK_CANCEL_CONVERT
) {
3337 /* If we're already trying to cancel a lock conversion
3338 * then just drop the spinlock and allow the caller to
3339 * requeue this lock. */
3341 mlog(0, "Lockres %s, skip convert\n", lockres
->l_name
);
3345 /* were we in a convert when we got the bast fire? */
3346 BUG_ON(lockres
->l_action
!= OCFS2_AST_CONVERT
&&
3347 lockres
->l_action
!= OCFS2_AST_DOWNCONVERT
);
3348 /* set things up for the unlockast to know to just
3349 * clear out the ast_action and unset busy, etc. */
3350 lockres
->l_unlock_action
= OCFS2_UNLOCK_CANCEL_CONVERT
;
3352 mlog_bug_on_msg(!(lockres
->l_flags
& OCFS2_LOCK_BUSY
),
3353 "lock %s, invalid flags: 0x%lx\n",
3354 lockres
->l_name
, lockres
->l_flags
);
3359 static int ocfs2_cancel_convert(struct ocfs2_super
*osb
,
3360 struct ocfs2_lock_res
*lockres
)
3365 mlog(0, "lock %s\n", lockres
->l_name
);
3367 ret
= ocfs2_dlm_unlock(osb
->cconn
, &lockres
->l_lksb
,
3368 DLM_LKF_CANCEL
, lockres
);
3370 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret
, lockres
);
3371 ocfs2_recover_from_dlm_error(lockres
, 0);
3374 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres
->l_name
);
3380 static int ocfs2_unblock_lock(struct ocfs2_super
*osb
,
3381 struct ocfs2_lock_res
*lockres
,
3382 struct ocfs2_unblock_ctl
*ctl
)
3384 unsigned long flags
;
3394 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3398 * Is it still blocking? If not, we have no more work to do.
3400 if (!(lockres
->l_flags
& OCFS2_LOCK_BLOCKED
)) {
3401 BUG_ON(lockres
->l_blocking
!= DLM_LOCK_NL
);
3402 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3407 if (lockres
->l_flags
& OCFS2_LOCK_BUSY
) {
3409 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3410 * exists entirely for one reason - another thread has set
3411 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3413 * If we do ocfs2_cancel_convert() before the other thread
3414 * calls dlm_lock(), our cancel will do nothing. We will
3415 * get no ast, and we will have no way of knowing the
3416 * cancel failed. Meanwhile, the other thread will call
3417 * into dlm_lock() and wait...forever.
3419 * Why forever? Because another node has asked for the
3420 * lock first; that's why we're here in unblock_lock().
3422 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3423 * set, we just requeue the unblock. Only when the other
3424 * thread has called dlm_lock() and cleared PENDING will
3425 * we then cancel their request.
3427 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3428 * at the same time they set OCFS2_DLM_BUSY. They must
3429 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3431 if (lockres
->l_flags
& OCFS2_LOCK_PENDING
)
3435 ret
= ocfs2_prepare_cancel_convert(osb
, lockres
);
3436 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3438 ret
= ocfs2_cancel_convert(osb
, lockres
);
3446 * This prevents livelocks. OCFS2_LOCK_UPCONVERT_FINISHING flag is
3447 * set when the ast is received for an upconvert just before the
3448 * OCFS2_LOCK_BUSY flag is cleared. Now if the fs received a bast
3449 * on the heels of the ast, we want to delay the downconvert just
3450 * enough to allow the up requestor to do its task. Because this
3451 * lock is in the blocked queue, the lock will be downconverted
3452 * as soon as the requestor is done with the lock.
3454 if (lockres
->l_flags
& OCFS2_LOCK_UPCONVERT_FINISHING
)
3458 * How can we block and yet be at NL? We were trying to upconvert
3459 * from NL and got canceled. The code comes back here, and now
3460 * we notice and clear BLOCKING.
3462 if (lockres
->l_level
== DLM_LOCK_NL
) {
3463 BUG_ON(lockres
->l_ex_holders
|| lockres
->l_ro_holders
);
3464 lockres
->l_blocking
= DLM_LOCK_NL
;
3465 lockres_clear_flags(lockres
, OCFS2_LOCK_BLOCKED
);
3466 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3470 /* if we're blocking an exclusive and we have *any* holders,
3472 if ((lockres
->l_blocking
== DLM_LOCK_EX
)
3473 && (lockres
->l_ex_holders
|| lockres
->l_ro_holders
))
3476 /* If it's a PR we're blocking, then only
3477 * requeue if we've got any EX holders */
3478 if (lockres
->l_blocking
== DLM_LOCK_PR
&&
3479 lockres
->l_ex_holders
)
3483 * Can we get a lock in this state if the holder counts are
3484 * zero? The meta data unblock code used to check this.
3486 if ((lockres
->l_ops
->flags
& LOCK_TYPE_REQUIRES_REFRESH
)
3487 && (lockres
->l_flags
& OCFS2_LOCK_REFRESHING
))
3490 new_level
= ocfs2_highest_compat_lock_level(lockres
->l_blocking
);
3492 if (lockres
->l_ops
->check_downconvert
3493 && !lockres
->l_ops
->check_downconvert(lockres
, new_level
))
3496 /* If we get here, then we know that there are no more
3497 * incompatible holders (and anyone asking for an incompatible
3498 * lock is blocked). We can now downconvert the lock */
3499 if (!lockres
->l_ops
->downconvert_worker
)
3502 /* Some lockres types want to do a bit of work before
3503 * downconverting a lock. Allow that here. The worker function
3504 * may sleep, so we save off a copy of what we're blocking as
3505 * it may change while we're not holding the spin lock. */
3506 blocking
= lockres
->l_blocking
;
3507 level
= lockres
->l_level
;
3508 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3510 ctl
->unblock_action
= lockres
->l_ops
->downconvert_worker(lockres
, blocking
);
3512 if (ctl
->unblock_action
== UNBLOCK_STOP_POST
)
3515 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3516 if ((blocking
!= lockres
->l_blocking
) || (level
!= lockres
->l_level
)) {
3517 /* If this changed underneath us, then we can't drop
3525 if (lockres
->l_ops
->flags
& LOCK_TYPE_USES_LVB
) {
3526 if (lockres
->l_level
== DLM_LOCK_EX
)
3530 * We only set the lvb if the lock has been fully
3531 * refreshed - otherwise we risk setting stale
3532 * data. Otherwise, there's no need to actually clear
3533 * out the lvb here as it's value is still valid.
3535 if (set_lvb
&& !(lockres
->l_flags
& OCFS2_LOCK_NEEDS_REFRESH
))
3536 lockres
->l_ops
->set_lvb(lockres
);
3539 gen
= ocfs2_prepare_downconvert(lockres
, new_level
);
3540 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3541 ret
= ocfs2_downconvert_lock(osb
, lockres
, new_level
, set_lvb
,
3549 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3556 static int ocfs2_data_convert_worker(struct ocfs2_lock_res
*lockres
,
3559 struct inode
*inode
;
3560 struct address_space
*mapping
;
3562 inode
= ocfs2_lock_res_inode(lockres
);
3563 mapping
= inode
->i_mapping
;
3565 if (!S_ISREG(inode
->i_mode
))
3569 * We need this before the filemap_fdatawrite() so that it can
3570 * transfer the dirty bit from the PTE to the
3571 * page. Unfortunately this means that even for EX->PR
3572 * downconverts, we'll lose our mappings and have to build
3575 unmap_mapping_range(mapping
, 0, 0, 0);
3577 if (filemap_fdatawrite(mapping
)) {
3578 mlog(ML_ERROR
, "Could not sync inode %llu for downconvert!",
3579 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
3581 sync_mapping_buffers(mapping
);
3582 if (blocking
== DLM_LOCK_EX
) {
3583 truncate_inode_pages(mapping
, 0);
3585 /* We only need to wait on the I/O if we're not also
3586 * truncating pages because truncate_inode_pages waits
3587 * for us above. We don't truncate pages if we're
3588 * blocking anything < EXMODE because we want to keep
3589 * them around in that case. */
3590 filemap_fdatawait(mapping
);
3594 return UNBLOCK_CONTINUE
;
3597 static int ocfs2_ci_checkpointed(struct ocfs2_caching_info
*ci
,
3598 struct ocfs2_lock_res
*lockres
,
3601 int checkpointed
= ocfs2_ci_fully_checkpointed(ci
);
3603 BUG_ON(new_level
!= DLM_LOCK_NL
&& new_level
!= DLM_LOCK_PR
);
3604 BUG_ON(lockres
->l_level
!= DLM_LOCK_EX
&& !checkpointed
);
3609 ocfs2_start_checkpoint(OCFS2_SB(ocfs2_metadata_cache_get_super(ci
)));
3613 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res
*lockres
,
3616 struct inode
*inode
= ocfs2_lock_res_inode(lockres
);
3618 return ocfs2_ci_checkpointed(INODE_CACHE(inode
), lockres
, new_level
);
3621 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res
*lockres
)
3623 struct inode
*inode
= ocfs2_lock_res_inode(lockres
);
3625 __ocfs2_stuff_meta_lvb(inode
);
3629 * Does the final reference drop on our dentry lock. Right now this
3630 * happens in the downconvert thread, but we could choose to simplify the
3631 * dlmglue API and push these off to the ocfs2_wq in the future.
3633 static void ocfs2_dentry_post_unlock(struct ocfs2_super
*osb
,
3634 struct ocfs2_lock_res
*lockres
)
3636 struct ocfs2_dentry_lock
*dl
= ocfs2_lock_res_dl(lockres
);
3637 ocfs2_dentry_lock_put(osb
, dl
);
3641 * d_delete() matching dentries before the lock downconvert.
3643 * At this point, any process waiting to destroy the
3644 * dentry_lock due to last ref count is stopped by the
3645 * OCFS2_LOCK_QUEUED flag.
3647 * We have two potential problems
3649 * 1) If we do the last reference drop on our dentry_lock (via dput)
3650 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3651 * the downconvert to finish. Instead we take an elevated
3652 * reference and push the drop until after we've completed our
3653 * unblock processing.
3655 * 2) There might be another process with a final reference,
3656 * waiting on us to finish processing. If this is the case, we
3657 * detect it and exit out - there's no more dentries anyway.
3659 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res
*lockres
,
3662 struct ocfs2_dentry_lock
*dl
= ocfs2_lock_res_dl(lockres
);
3663 struct ocfs2_inode_info
*oi
= OCFS2_I(dl
->dl_inode
);
3664 struct dentry
*dentry
;
3665 unsigned long flags
;
3669 * This node is blocking another node from getting a read
3670 * lock. This happens when we've renamed within a
3671 * directory. We've forced the other nodes to d_delete(), but
3672 * we never actually dropped our lock because it's still
3673 * valid. The downconvert code will retain a PR for this node,
3674 * so there's no further work to do.
3676 if (blocking
== DLM_LOCK_PR
)
3677 return UNBLOCK_CONTINUE
;
3680 * Mark this inode as potentially orphaned. The code in
3681 * ocfs2_delete_inode() will figure out whether it actually
3682 * needs to be freed or not.
3684 spin_lock(&oi
->ip_lock
);
3685 oi
->ip_flags
|= OCFS2_INODE_MAYBE_ORPHANED
;
3686 spin_unlock(&oi
->ip_lock
);
3689 * Yuck. We need to make sure however that the check of
3690 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3691 * respect to a reference decrement or the setting of that
3694 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3695 spin_lock(&dentry_attach_lock
);
3696 if (!(lockres
->l_flags
& OCFS2_LOCK_FREEING
)
3701 spin_unlock(&dentry_attach_lock
);
3702 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3704 mlog(0, "extra_ref = %d\n", extra_ref
);
3707 * We have a process waiting on us in ocfs2_dentry_iput(),
3708 * which means we can't have any more outstanding
3709 * aliases. There's no need to do any more work.
3712 return UNBLOCK_CONTINUE
;
3714 spin_lock(&dentry_attach_lock
);
3716 dentry
= ocfs2_find_local_alias(dl
->dl_inode
,
3717 dl
->dl_parent_blkno
, 1);
3720 spin_unlock(&dentry_attach_lock
);
3722 mlog(0, "d_delete(%.*s);\n", dentry
->d_name
.len
,
3723 dentry
->d_name
.name
);
3726 * The following dcache calls may do an
3727 * iput(). Normally we don't want that from the
3728 * downconverting thread, but in this case it's ok
3729 * because the requesting node already has an
3730 * exclusive lock on the inode, so it can't be queued
3731 * for a downconvert.
3736 spin_lock(&dentry_attach_lock
);
3738 spin_unlock(&dentry_attach_lock
);
3741 * If we are the last holder of this dentry lock, there is no
3742 * reason to downconvert so skip straight to the unlock.
3744 if (dl
->dl_count
== 1)
3745 return UNBLOCK_STOP_POST
;
3747 return UNBLOCK_CONTINUE_POST
;
3750 static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res
*lockres
,
3753 struct ocfs2_refcount_tree
*tree
=
3754 ocfs2_lock_res_refcount_tree(lockres
);
3756 return ocfs2_ci_checkpointed(&tree
->rf_ci
, lockres
, new_level
);
3759 static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res
*lockres
,
3762 struct ocfs2_refcount_tree
*tree
=
3763 ocfs2_lock_res_refcount_tree(lockres
);
3765 ocfs2_metadata_cache_purge(&tree
->rf_ci
);
3767 return UNBLOCK_CONTINUE
;
3770 static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res
*lockres
)
3772 struct ocfs2_qinfo_lvb
*lvb
;
3773 struct ocfs2_mem_dqinfo
*oinfo
= ocfs2_lock_res_qinfo(lockres
);
3774 struct mem_dqinfo
*info
= sb_dqinfo(oinfo
->dqi_gi
.dqi_sb
,
3775 oinfo
->dqi_gi
.dqi_type
);
3779 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
3780 lvb
->lvb_version
= OCFS2_QINFO_LVB_VERSION
;
3781 lvb
->lvb_bgrace
= cpu_to_be32(info
->dqi_bgrace
);
3782 lvb
->lvb_igrace
= cpu_to_be32(info
->dqi_igrace
);
3783 lvb
->lvb_syncms
= cpu_to_be32(oinfo
->dqi_syncms
);
3784 lvb
->lvb_blocks
= cpu_to_be32(oinfo
->dqi_gi
.dqi_blocks
);
3785 lvb
->lvb_free_blk
= cpu_to_be32(oinfo
->dqi_gi
.dqi_free_blk
);
3786 lvb
->lvb_free_entry
= cpu_to_be32(oinfo
->dqi_gi
.dqi_free_entry
);
3791 void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo
*oinfo
, int ex
)
3793 struct ocfs2_lock_res
*lockres
= &oinfo
->dqi_gqlock
;
3794 struct ocfs2_super
*osb
= OCFS2_SB(oinfo
->dqi_gi
.dqi_sb
);
3795 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
3798 if (!ocfs2_is_hard_readonly(osb
) && !ocfs2_mount_local(osb
))
3799 ocfs2_cluster_unlock(osb
, lockres
, level
);
3803 static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo
*oinfo
)
3805 struct mem_dqinfo
*info
= sb_dqinfo(oinfo
->dqi_gi
.dqi_sb
,
3806 oinfo
->dqi_gi
.dqi_type
);
3807 struct ocfs2_lock_res
*lockres
= &oinfo
->dqi_gqlock
;
3808 struct ocfs2_qinfo_lvb
*lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
3809 struct buffer_head
*bh
= NULL
;
3810 struct ocfs2_global_disk_dqinfo
*gdinfo
;
3813 if (ocfs2_dlm_lvb_valid(&lockres
->l_lksb
) &&
3814 lvb
->lvb_version
== OCFS2_QINFO_LVB_VERSION
) {
3815 info
->dqi_bgrace
= be32_to_cpu(lvb
->lvb_bgrace
);
3816 info
->dqi_igrace
= be32_to_cpu(lvb
->lvb_igrace
);
3817 oinfo
->dqi_syncms
= be32_to_cpu(lvb
->lvb_syncms
);
3818 oinfo
->dqi_gi
.dqi_blocks
= be32_to_cpu(lvb
->lvb_blocks
);
3819 oinfo
->dqi_gi
.dqi_free_blk
= be32_to_cpu(lvb
->lvb_free_blk
);
3820 oinfo
->dqi_gi
.dqi_free_entry
=
3821 be32_to_cpu(lvb
->lvb_free_entry
);
3823 status
= ocfs2_read_quota_block(oinfo
->dqi_gqinode
, 0, &bh
);
3828 gdinfo
= (struct ocfs2_global_disk_dqinfo
*)
3829 (bh
->b_data
+ OCFS2_GLOBAL_INFO_OFF
);
3830 info
->dqi_bgrace
= le32_to_cpu(gdinfo
->dqi_bgrace
);
3831 info
->dqi_igrace
= le32_to_cpu(gdinfo
->dqi_igrace
);
3832 oinfo
->dqi_syncms
= le32_to_cpu(gdinfo
->dqi_syncms
);
3833 oinfo
->dqi_gi
.dqi_blocks
= le32_to_cpu(gdinfo
->dqi_blocks
);
3834 oinfo
->dqi_gi
.dqi_free_blk
= le32_to_cpu(gdinfo
->dqi_free_blk
);
3835 oinfo
->dqi_gi
.dqi_free_entry
=
3836 le32_to_cpu(gdinfo
->dqi_free_entry
);
3838 ocfs2_track_lock_refresh(lockres
);
3845 /* Lock quota info, this function expects at least shared lock on the quota file
3846 * so that we can safely refresh quota info from disk. */
3847 int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo
*oinfo
, int ex
)
3849 struct ocfs2_lock_res
*lockres
= &oinfo
->dqi_gqlock
;
3850 struct ocfs2_super
*osb
= OCFS2_SB(oinfo
->dqi_gi
.dqi_sb
);
3851 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
3856 /* On RO devices, locking really isn't needed... */
3857 if (ocfs2_is_hard_readonly(osb
)) {
3862 if (ocfs2_mount_local(osb
))
3865 status
= ocfs2_cluster_lock(osb
, lockres
, level
, 0, 0);
3870 if (!ocfs2_should_refresh_lock_res(lockres
))
3872 /* OK, we have the lock but we need to refresh the quota info */
3873 status
= ocfs2_refresh_qinfo(oinfo
);
3875 ocfs2_qinfo_unlock(oinfo
, ex
);
3876 ocfs2_complete_lock_res_refresh(lockres
, status
);
3882 int ocfs2_refcount_lock(struct ocfs2_refcount_tree
*ref_tree
, int ex
)
3885 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
3886 struct ocfs2_lock_res
*lockres
= &ref_tree
->rf_lockres
;
3887 struct ocfs2_super
*osb
= lockres
->l_priv
;
3890 if (ocfs2_is_hard_readonly(osb
))
3893 if (ocfs2_mount_local(osb
))
3896 status
= ocfs2_cluster_lock(osb
, lockres
, level
, 0, 0);
3903 void ocfs2_refcount_unlock(struct ocfs2_refcount_tree
*ref_tree
, int ex
)
3905 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
3906 struct ocfs2_lock_res
*lockres
= &ref_tree
->rf_lockres
;
3907 struct ocfs2_super
*osb
= lockres
->l_priv
;
3909 if (!ocfs2_mount_local(osb
))
3910 ocfs2_cluster_unlock(osb
, lockres
, level
);
3914 * This is the filesystem locking protocol. It provides the lock handling
3915 * hooks for the underlying DLM. It has a maximum version number.
3916 * The version number allows interoperability with systems running at
3917 * the same major number and an equal or smaller minor number.
3919 * Whenever the filesystem does new things with locks (adds or removes a
3920 * lock, orders them differently, does different things underneath a lock),
3921 * the version must be changed. The protocol is negotiated when joining
3922 * the dlm domain. A node may join the domain if its major version is
3923 * identical to all other nodes and its minor version is greater than
3924 * or equal to all other nodes. When its minor version is greater than
3925 * the other nodes, it will run at the minor version specified by the
3928 * If a locking change is made that will not be compatible with older
3929 * versions, the major number must be increased and the minor version set
3930 * to zero. If a change merely adds a behavior that can be disabled when
3931 * speaking to older versions, the minor version must be increased. If a
3932 * change adds a fully backwards compatible change (eg, LVB changes that
3933 * are just ignored by older versions), the version does not need to be
3936 static struct ocfs2_locking_protocol lproto
= {
3938 .pv_major
= OCFS2_LOCKING_PROTOCOL_MAJOR
,
3939 .pv_minor
= OCFS2_LOCKING_PROTOCOL_MINOR
,
3941 .lp_lock_ast
= ocfs2_locking_ast
,
3942 .lp_blocking_ast
= ocfs2_blocking_ast
,
3943 .lp_unlock_ast
= ocfs2_unlock_ast
,
3946 void ocfs2_set_locking_protocol(void)
3948 ocfs2_stack_glue_set_locking_protocol(&lproto
);
3952 static void ocfs2_process_blocked_lock(struct ocfs2_super
*osb
,
3953 struct ocfs2_lock_res
*lockres
)
3956 struct ocfs2_unblock_ctl ctl
= {0, 0,};
3957 unsigned long flags
;
3959 /* Our reference to the lockres in this function can be
3960 * considered valid until we remove the OCFS2_LOCK_QUEUED
3966 BUG_ON(!lockres
->l_ops
);
3968 mlog(0, "lockres %s blocked.\n", lockres
->l_name
);
3970 /* Detect whether a lock has been marked as going away while
3971 * the downconvert thread was processing other things. A lock can
3972 * still be marked with OCFS2_LOCK_FREEING after this check,
3973 * but short circuiting here will still save us some
3975 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3976 if (lockres
->l_flags
& OCFS2_LOCK_FREEING
)
3978 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3980 status
= ocfs2_unblock_lock(osb
, lockres
, &ctl
);
3984 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3986 if (lockres
->l_flags
& OCFS2_LOCK_FREEING
|| !ctl
.requeue
) {
3987 lockres_clear_flags(lockres
, OCFS2_LOCK_QUEUED
);
3989 ocfs2_schedule_blocked_lock(osb
, lockres
);
3991 mlog(0, "lockres %s, requeue = %s.\n", lockres
->l_name
,
3992 ctl
.requeue
? "yes" : "no");
3993 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3995 if (ctl
.unblock_action
!= UNBLOCK_CONTINUE
3996 && lockres
->l_ops
->post_unlock
)
3997 lockres
->l_ops
->post_unlock(osb
, lockres
);
4002 static void ocfs2_schedule_blocked_lock(struct ocfs2_super
*osb
,
4003 struct ocfs2_lock_res
*lockres
)
4007 assert_spin_locked(&lockres
->l_lock
);
4009 if (lockres
->l_flags
& OCFS2_LOCK_FREEING
) {
4010 /* Do not schedule a lock for downconvert when it's on
4011 * the way to destruction - any nodes wanting access
4012 * to the resource will get it soon. */
4013 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
4014 lockres
->l_name
, lockres
->l_flags
);
4018 lockres_or_flags(lockres
, OCFS2_LOCK_QUEUED
);
4020 spin_lock(&osb
->dc_task_lock
);
4021 if (list_empty(&lockres
->l_blocked_list
)) {
4022 list_add_tail(&lockres
->l_blocked_list
,
4023 &osb
->blocked_lock_list
);
4024 osb
->blocked_lock_count
++;
4026 spin_unlock(&osb
->dc_task_lock
);
4031 static void ocfs2_downconvert_thread_do_work(struct ocfs2_super
*osb
)
4033 unsigned long processed
;
4034 struct ocfs2_lock_res
*lockres
;
4038 spin_lock(&osb
->dc_task_lock
);
4039 /* grab this early so we know to try again if a state change and
4040 * wake happens part-way through our work */
4041 osb
->dc_work_sequence
= osb
->dc_wake_sequence
;
4043 processed
= osb
->blocked_lock_count
;
4045 BUG_ON(list_empty(&osb
->blocked_lock_list
));
4047 lockres
= list_entry(osb
->blocked_lock_list
.next
,
4048 struct ocfs2_lock_res
, l_blocked_list
);
4049 list_del_init(&lockres
->l_blocked_list
);
4050 osb
->blocked_lock_count
--;
4051 spin_unlock(&osb
->dc_task_lock
);
4056 ocfs2_process_blocked_lock(osb
, lockres
);
4058 spin_lock(&osb
->dc_task_lock
);
4060 spin_unlock(&osb
->dc_task_lock
);
4065 static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super
*osb
)
4069 spin_lock(&osb
->dc_task_lock
);
4070 if (list_empty(&osb
->blocked_lock_list
))
4073 spin_unlock(&osb
->dc_task_lock
);
4077 static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super
*osb
)
4079 int should_wake
= 0;
4081 spin_lock(&osb
->dc_task_lock
);
4082 if (osb
->dc_work_sequence
!= osb
->dc_wake_sequence
)
4084 spin_unlock(&osb
->dc_task_lock
);
4089 static int ocfs2_downconvert_thread(void *arg
)
4092 struct ocfs2_super
*osb
= arg
;
4094 /* only quit once we've been asked to stop and there is no more
4096 while (!(kthread_should_stop() &&
4097 ocfs2_downconvert_thread_lists_empty(osb
))) {
4099 wait_event_interruptible(osb
->dc_event
,
4100 ocfs2_downconvert_thread_should_wake(osb
) ||
4101 kthread_should_stop());
4103 mlog(0, "downconvert_thread: awoken\n");
4105 ocfs2_downconvert_thread_do_work(osb
);
4108 osb
->dc_task
= NULL
;
4112 void ocfs2_wake_downconvert_thread(struct ocfs2_super
*osb
)
4114 spin_lock(&osb
->dc_task_lock
);
4115 /* make sure the voting thread gets a swipe at whatever changes
4116 * the caller may have made to the voting state */
4117 osb
->dc_wake_sequence
++;
4118 spin_unlock(&osb
->dc_task_lock
);
4119 wake_up(&osb
->dc_event
);