1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Code which implements an OCFS2 specific interface to our DLM.
8 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
26 #include <linux/types.h>
27 #include <linux/slab.h>
28 #include <linux/highmem.h>
30 #include <linux/kthread.h>
31 #include <linux/pagemap.h>
32 #include <linux/debugfs.h>
33 #include <linux/seq_file.h>
34 #include <linux/time.h>
35 #include <linux/quotaops.h>
37 #define MLOG_MASK_PREFIX ML_DLM_GLUE
38 #include <cluster/masklog.h>
41 #include "ocfs2_lockingver.h"
46 #include "extent_map.h"
48 #include "heartbeat.h"
51 #include "stackglue.h"
57 #include "buffer_head_io.h"
59 struct ocfs2_mask_waiter
{
60 struct list_head mw_item
;
62 struct completion mw_complete
;
63 unsigned long mw_mask
;
64 unsigned long mw_goal
;
65 #ifdef CONFIG_OCFS2_FS_STATS
66 unsigned long long mw_lock_start
;
70 static struct ocfs2_super
*ocfs2_get_dentry_osb(struct ocfs2_lock_res
*lockres
);
71 static struct ocfs2_super
*ocfs2_get_inode_osb(struct ocfs2_lock_res
*lockres
);
72 static struct ocfs2_super
*ocfs2_get_file_osb(struct ocfs2_lock_res
*lockres
);
73 static struct ocfs2_super
*ocfs2_get_qinfo_osb(struct ocfs2_lock_res
*lockres
);
76 * Return value from ->downconvert_worker functions.
78 * These control the precise actions of ocfs2_unblock_lock()
79 * and ocfs2_process_blocked_lock()
82 enum ocfs2_unblock_action
{
83 UNBLOCK_CONTINUE
= 0, /* Continue downconvert */
84 UNBLOCK_CONTINUE_POST
= 1, /* Continue downconvert, fire
85 * ->post_unlock callback */
86 UNBLOCK_STOP_POST
= 2, /* Do not downconvert, fire
87 * ->post_unlock() callback. */
90 struct ocfs2_unblock_ctl
{
92 enum ocfs2_unblock_action unblock_action
;
95 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res
*lockres
,
97 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res
*lockres
);
99 static int ocfs2_data_convert_worker(struct ocfs2_lock_res
*lockres
,
102 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res
*lockres
,
105 static void ocfs2_dentry_post_unlock(struct ocfs2_super
*osb
,
106 struct ocfs2_lock_res
*lockres
);
108 static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res
*lockres
);
110 #define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres)
112 /* This aids in debugging situations where a bad LVB might be involved. */
113 static void ocfs2_dump_meta_lvb_info(u64 level
,
114 const char *function
,
116 struct ocfs2_lock_res
*lockres
)
118 struct ocfs2_meta_lvb
*lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
120 mlog(level
, "LVB information for %s (called from %s:%u):\n",
121 lockres
->l_name
, function
, line
);
122 mlog(level
, "version: %u, clusters: %u, generation: 0x%x\n",
123 lvb
->lvb_version
, be32_to_cpu(lvb
->lvb_iclusters
),
124 be32_to_cpu(lvb
->lvb_igeneration
));
125 mlog(level
, "size: %llu, uid %u, gid %u, mode 0x%x\n",
126 (unsigned long long)be64_to_cpu(lvb
->lvb_isize
),
127 be32_to_cpu(lvb
->lvb_iuid
), be32_to_cpu(lvb
->lvb_igid
),
128 be16_to_cpu(lvb
->lvb_imode
));
129 mlog(level
, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
130 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb
->lvb_inlink
),
131 (long long)be64_to_cpu(lvb
->lvb_iatime_packed
),
132 (long long)be64_to_cpu(lvb
->lvb_ictime_packed
),
133 (long long)be64_to_cpu(lvb
->lvb_imtime_packed
),
134 be32_to_cpu(lvb
->lvb_iattr
));
139 * OCFS2 Lock Resource Operations
141 * These fine tune the behavior of the generic dlmglue locking infrastructure.
143 * The most basic of lock types can point ->l_priv to their respective
144 * struct ocfs2_super and allow the default actions to manage things.
146 * Right now, each lock type also needs to implement an init function,
147 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
148 * should be called when the lock is no longer needed (i.e., object
151 struct ocfs2_lock_res_ops
{
153 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
154 * this callback if ->l_priv is not an ocfs2_super pointer
156 struct ocfs2_super
* (*get_osb
)(struct ocfs2_lock_res
*);
159 * Optionally called in the downconvert thread after a
160 * successful downconvert. The lockres will not be referenced
161 * after this callback is called, so it is safe to free
164 * The exact semantics of when this is called are controlled
165 * by ->downconvert_worker()
167 void (*post_unlock
)(struct ocfs2_super
*, struct ocfs2_lock_res
*);
170 * Allow a lock type to add checks to determine whether it is
171 * safe to downconvert a lock. Return 0 to re-queue the
172 * downconvert at a later time, nonzero to continue.
174 * For most locks, the default checks that there are no
175 * incompatible holders are sufficient.
177 * Called with the lockres spinlock held.
179 int (*check_downconvert
)(struct ocfs2_lock_res
*, int);
182 * Allows a lock type to populate the lock value block. This
183 * is called on downconvert, and when we drop a lock.
185 * Locks that want to use this should set LOCK_TYPE_USES_LVB
186 * in the flags field.
188 * Called with the lockres spinlock held.
190 void (*set_lvb
)(struct ocfs2_lock_res
*);
193 * Called from the downconvert thread when it is determined
194 * that a lock will be downconverted. This is called without
195 * any locks held so the function can do work that might
196 * schedule (syncing out data, etc).
198 * This should return any one of the ocfs2_unblock_action
199 * values, depending on what it wants the thread to do.
201 int (*downconvert_worker
)(struct ocfs2_lock_res
*, int);
204 * LOCK_TYPE_* flags which describe the specific requirements
205 * of a lock type. Descriptions of each individual flag follow.
211 * Some locks want to "refresh" potentially stale data when a
212 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
213 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
214 * individual lockres l_flags member from the ast function. It is
215 * expected that the locking wrapper will clear the
216 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
218 #define LOCK_TYPE_REQUIRES_REFRESH 0x1
221 * Indicate that a lock type makes use of the lock value block. The
222 * ->set_lvb lock type callback must be defined.
224 #define LOCK_TYPE_USES_LVB 0x2
226 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops
= {
227 .get_osb
= ocfs2_get_inode_osb
,
231 static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops
= {
232 .get_osb
= ocfs2_get_inode_osb
,
233 .check_downconvert
= ocfs2_check_meta_downconvert
,
234 .set_lvb
= ocfs2_set_meta_lvb
,
235 .downconvert_worker
= ocfs2_data_convert_worker
,
236 .flags
= LOCK_TYPE_REQUIRES_REFRESH
|LOCK_TYPE_USES_LVB
,
239 static struct ocfs2_lock_res_ops ocfs2_super_lops
= {
240 .flags
= LOCK_TYPE_REQUIRES_REFRESH
,
243 static struct ocfs2_lock_res_ops ocfs2_rename_lops
= {
247 static struct ocfs2_lock_res_ops ocfs2_nfs_sync_lops
= {
251 static struct ocfs2_lock_res_ops ocfs2_orphan_scan_lops
= {
252 .flags
= LOCK_TYPE_REQUIRES_REFRESH
|LOCK_TYPE_USES_LVB
,
255 static struct ocfs2_lock_res_ops ocfs2_dentry_lops
= {
256 .get_osb
= ocfs2_get_dentry_osb
,
257 .post_unlock
= ocfs2_dentry_post_unlock
,
258 .downconvert_worker
= ocfs2_dentry_convert_worker
,
262 static struct ocfs2_lock_res_ops ocfs2_inode_open_lops
= {
263 .get_osb
= ocfs2_get_inode_osb
,
267 static struct ocfs2_lock_res_ops ocfs2_flock_lops
= {
268 .get_osb
= ocfs2_get_file_osb
,
272 static struct ocfs2_lock_res_ops ocfs2_qinfo_lops
= {
273 .set_lvb
= ocfs2_set_qinfo_lvb
,
274 .get_osb
= ocfs2_get_qinfo_osb
,
275 .flags
= LOCK_TYPE_REQUIRES_REFRESH
| LOCK_TYPE_USES_LVB
,
278 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res
*lockres
)
280 return lockres
->l_type
== OCFS2_LOCK_TYPE_META
||
281 lockres
->l_type
== OCFS2_LOCK_TYPE_RW
||
282 lockres
->l_type
== OCFS2_LOCK_TYPE_OPEN
;
285 static inline struct inode
*ocfs2_lock_res_inode(struct ocfs2_lock_res
*lockres
)
287 BUG_ON(!ocfs2_is_inode_lock(lockres
));
289 return (struct inode
*) lockres
->l_priv
;
292 static inline struct ocfs2_dentry_lock
*ocfs2_lock_res_dl(struct ocfs2_lock_res
*lockres
)
294 BUG_ON(lockres
->l_type
!= OCFS2_LOCK_TYPE_DENTRY
);
296 return (struct ocfs2_dentry_lock
*)lockres
->l_priv
;
299 static inline struct ocfs2_mem_dqinfo
*ocfs2_lock_res_qinfo(struct ocfs2_lock_res
*lockres
)
301 BUG_ON(lockres
->l_type
!= OCFS2_LOCK_TYPE_QINFO
);
303 return (struct ocfs2_mem_dqinfo
*)lockres
->l_priv
;
306 static inline struct ocfs2_super
*ocfs2_get_lockres_osb(struct ocfs2_lock_res
*lockres
)
308 if (lockres
->l_ops
->get_osb
)
309 return lockres
->l_ops
->get_osb(lockres
);
311 return (struct ocfs2_super
*)lockres
->l_priv
;
314 static int ocfs2_lock_create(struct ocfs2_super
*osb
,
315 struct ocfs2_lock_res
*lockres
,
318 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res
*lockres
,
320 static void ocfs2_cluster_unlock(struct ocfs2_super
*osb
,
321 struct ocfs2_lock_res
*lockres
,
323 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res
*lockres
);
324 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res
*lockres
);
325 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res
*lockres
);
326 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res
*lockres
, int level
);
327 static void ocfs2_schedule_blocked_lock(struct ocfs2_super
*osb
,
328 struct ocfs2_lock_res
*lockres
);
329 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res
*lockres
,
331 #define ocfs2_log_dlm_error(_func, _err, _lockres) do { \
332 if ((_lockres)->l_type != OCFS2_LOCK_TYPE_DENTRY) \
333 mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \
334 _err, _func, _lockres->l_name); \
336 mlog(ML_ERROR, "DLM error %d while calling %s on resource %.*s%08x\n", \
337 _err, _func, OCFS2_DENTRY_LOCK_INO_START - 1, (_lockres)->l_name, \
338 (unsigned int)ocfs2_get_dentry_lock_ino(_lockres)); \
340 static int ocfs2_downconvert_thread(void *arg
);
341 static void ocfs2_downconvert_on_unlock(struct ocfs2_super
*osb
,
342 struct ocfs2_lock_res
*lockres
);
343 static int ocfs2_inode_lock_update(struct inode
*inode
,
344 struct buffer_head
**bh
);
345 static void ocfs2_drop_osb_locks(struct ocfs2_super
*osb
);
346 static inline int ocfs2_highest_compat_lock_level(int level
);
347 static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res
*lockres
,
349 static int ocfs2_downconvert_lock(struct ocfs2_super
*osb
,
350 struct ocfs2_lock_res
*lockres
,
353 unsigned int generation
);
354 static int ocfs2_prepare_cancel_convert(struct ocfs2_super
*osb
,
355 struct ocfs2_lock_res
*lockres
);
356 static int ocfs2_cancel_convert(struct ocfs2_super
*osb
,
357 struct ocfs2_lock_res
*lockres
);
360 static void ocfs2_build_lock_name(enum ocfs2_lock_type type
,
369 BUG_ON(type
>= OCFS2_NUM_LOCK_TYPES
);
371 len
= snprintf(name
, OCFS2_LOCK_ID_MAX_LEN
, "%c%s%016llx%08x",
372 ocfs2_lock_type_char(type
), OCFS2_LOCK_ID_PAD
,
373 (long long)blkno
, generation
);
375 BUG_ON(len
!= (OCFS2_LOCK_ID_MAX_LEN
- 1));
377 mlog(0, "built lock resource with name: %s\n", name
);
382 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock
);
384 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res
*res
,
385 struct ocfs2_dlm_debug
*dlm_debug
)
387 mlog(0, "Add tracking for lockres %s\n", res
->l_name
);
389 spin_lock(&ocfs2_dlm_tracking_lock
);
390 list_add(&res
->l_debug_list
, &dlm_debug
->d_lockres_tracking
);
391 spin_unlock(&ocfs2_dlm_tracking_lock
);
394 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res
*res
)
396 spin_lock(&ocfs2_dlm_tracking_lock
);
397 if (!list_empty(&res
->l_debug_list
))
398 list_del_init(&res
->l_debug_list
);
399 spin_unlock(&ocfs2_dlm_tracking_lock
);
402 #ifdef CONFIG_OCFS2_FS_STATS
403 static void ocfs2_init_lock_stats(struct ocfs2_lock_res
*res
)
405 res
->l_lock_num_prmode
= 0;
406 res
->l_lock_num_prmode_failed
= 0;
407 res
->l_lock_total_prmode
= 0;
408 res
->l_lock_max_prmode
= 0;
409 res
->l_lock_num_exmode
= 0;
410 res
->l_lock_num_exmode_failed
= 0;
411 res
->l_lock_total_exmode
= 0;
412 res
->l_lock_max_exmode
= 0;
413 res
->l_lock_refresh
= 0;
416 static void ocfs2_update_lock_stats(struct ocfs2_lock_res
*res
, int level
,
417 struct ocfs2_mask_waiter
*mw
, int ret
)
419 unsigned long long *num
, *sum
;
420 unsigned int *max
, *failed
;
421 struct timespec ts
= current_kernel_time();
422 unsigned long long time
= timespec_to_ns(&ts
) - mw
->mw_lock_start
;
424 if (level
== LKM_PRMODE
) {
425 num
= &res
->l_lock_num_prmode
;
426 sum
= &res
->l_lock_total_prmode
;
427 max
= &res
->l_lock_max_prmode
;
428 failed
= &res
->l_lock_num_prmode_failed
;
429 } else if (level
== LKM_EXMODE
) {
430 num
= &res
->l_lock_num_exmode
;
431 sum
= &res
->l_lock_total_exmode
;
432 max
= &res
->l_lock_max_exmode
;
433 failed
= &res
->l_lock_num_exmode_failed
;
445 static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res
*lockres
)
447 lockres
->l_lock_refresh
++;
450 static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter
*mw
)
452 struct timespec ts
= current_kernel_time();
453 mw
->mw_lock_start
= timespec_to_ns(&ts
);
456 static inline void ocfs2_init_lock_stats(struct ocfs2_lock_res
*res
)
459 static inline void ocfs2_update_lock_stats(struct ocfs2_lock_res
*res
,
460 int level
, struct ocfs2_mask_waiter
*mw
, int ret
)
463 static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res
*lockres
)
466 static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter
*mw
)
471 static void ocfs2_lock_res_init_common(struct ocfs2_super
*osb
,
472 struct ocfs2_lock_res
*res
,
473 enum ocfs2_lock_type type
,
474 struct ocfs2_lock_res_ops
*ops
,
481 res
->l_level
= DLM_LOCK_IV
;
482 res
->l_requested
= DLM_LOCK_IV
;
483 res
->l_blocking
= DLM_LOCK_IV
;
484 res
->l_action
= OCFS2_AST_INVALID
;
485 res
->l_unlock_action
= OCFS2_UNLOCK_INVALID
;
487 res
->l_flags
= OCFS2_LOCK_INITIALIZED
;
489 ocfs2_add_lockres_tracking(res
, osb
->osb_dlm_debug
);
491 ocfs2_init_lock_stats(res
);
494 void ocfs2_lock_res_init_once(struct ocfs2_lock_res
*res
)
496 /* This also clears out the lock status block */
497 memset(res
, 0, sizeof(struct ocfs2_lock_res
));
498 spin_lock_init(&res
->l_lock
);
499 init_waitqueue_head(&res
->l_event
);
500 INIT_LIST_HEAD(&res
->l_blocked_list
);
501 INIT_LIST_HEAD(&res
->l_mask_waiters
);
504 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res
*res
,
505 enum ocfs2_lock_type type
,
506 unsigned int generation
,
509 struct ocfs2_lock_res_ops
*ops
;
512 case OCFS2_LOCK_TYPE_RW
:
513 ops
= &ocfs2_inode_rw_lops
;
515 case OCFS2_LOCK_TYPE_META
:
516 ops
= &ocfs2_inode_inode_lops
;
518 case OCFS2_LOCK_TYPE_OPEN
:
519 ops
= &ocfs2_inode_open_lops
;
522 mlog_bug_on_msg(1, "type: %d\n", type
);
523 ops
= NULL
; /* thanks, gcc */
527 ocfs2_build_lock_name(type
, OCFS2_I(inode
)->ip_blkno
,
528 generation
, res
->l_name
);
529 ocfs2_lock_res_init_common(OCFS2_SB(inode
->i_sb
), res
, type
, ops
, inode
);
532 static struct ocfs2_super
*ocfs2_get_inode_osb(struct ocfs2_lock_res
*lockres
)
534 struct inode
*inode
= ocfs2_lock_res_inode(lockres
);
536 return OCFS2_SB(inode
->i_sb
);
539 static struct ocfs2_super
*ocfs2_get_qinfo_osb(struct ocfs2_lock_res
*lockres
)
541 struct ocfs2_mem_dqinfo
*info
= lockres
->l_priv
;
543 return OCFS2_SB(info
->dqi_gi
.dqi_sb
);
546 static struct ocfs2_super
*ocfs2_get_file_osb(struct ocfs2_lock_res
*lockres
)
548 struct ocfs2_file_private
*fp
= lockres
->l_priv
;
550 return OCFS2_SB(fp
->fp_file
->f_mapping
->host
->i_sb
);
553 static __u64
ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res
*lockres
)
555 __be64 inode_blkno_be
;
557 memcpy(&inode_blkno_be
, &lockres
->l_name
[OCFS2_DENTRY_LOCK_INO_START
],
560 return be64_to_cpu(inode_blkno_be
);
563 static struct ocfs2_super
*ocfs2_get_dentry_osb(struct ocfs2_lock_res
*lockres
)
565 struct ocfs2_dentry_lock
*dl
= lockres
->l_priv
;
567 return OCFS2_SB(dl
->dl_inode
->i_sb
);
570 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock
*dl
,
571 u64 parent
, struct inode
*inode
)
574 u64 inode_blkno
= OCFS2_I(inode
)->ip_blkno
;
575 __be64 inode_blkno_be
= cpu_to_be64(inode_blkno
);
576 struct ocfs2_lock_res
*lockres
= &dl
->dl_lockres
;
578 ocfs2_lock_res_init_once(lockres
);
581 * Unfortunately, the standard lock naming scheme won't work
582 * here because we have two 16 byte values to use. Instead,
583 * we'll stuff the inode number as a binary value. We still
584 * want error prints to show something without garbling the
585 * display, so drop a null byte in there before the inode
586 * number. A future version of OCFS2 will likely use all
587 * binary lock names. The stringified names have been a
588 * tremendous aid in debugging, but now that the debugfs
589 * interface exists, we can mangle things there if need be.
591 * NOTE: We also drop the standard "pad" value (the total lock
592 * name size stays the same though - the last part is all
593 * zeros due to the memset in ocfs2_lock_res_init_once()
595 len
= snprintf(lockres
->l_name
, OCFS2_DENTRY_LOCK_INO_START
,
597 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY
),
600 BUG_ON(len
!= (OCFS2_DENTRY_LOCK_INO_START
- 1));
602 memcpy(&lockres
->l_name
[OCFS2_DENTRY_LOCK_INO_START
], &inode_blkno_be
,
605 ocfs2_lock_res_init_common(OCFS2_SB(inode
->i_sb
), lockres
,
606 OCFS2_LOCK_TYPE_DENTRY
, &ocfs2_dentry_lops
,
610 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res
*res
,
611 struct ocfs2_super
*osb
)
613 /* Superblock lockres doesn't come from a slab so we call init
614 * once on it manually. */
615 ocfs2_lock_res_init_once(res
);
616 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER
, OCFS2_SUPER_BLOCK_BLKNO
,
618 ocfs2_lock_res_init_common(osb
, res
, OCFS2_LOCK_TYPE_SUPER
,
619 &ocfs2_super_lops
, osb
);
622 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res
*res
,
623 struct ocfs2_super
*osb
)
625 /* Rename lockres doesn't come from a slab so we call init
626 * once on it manually. */
627 ocfs2_lock_res_init_once(res
);
628 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME
, 0, 0, res
->l_name
);
629 ocfs2_lock_res_init_common(osb
, res
, OCFS2_LOCK_TYPE_RENAME
,
630 &ocfs2_rename_lops
, osb
);
633 static void ocfs2_nfs_sync_lock_res_init(struct ocfs2_lock_res
*res
,
634 struct ocfs2_super
*osb
)
636 /* nfs_sync lockres doesn't come from a slab so we call init
637 * once on it manually. */
638 ocfs2_lock_res_init_once(res
);
639 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_NFS_SYNC
, 0, 0, res
->l_name
);
640 ocfs2_lock_res_init_common(osb
, res
, OCFS2_LOCK_TYPE_NFS_SYNC
,
641 &ocfs2_nfs_sync_lops
, osb
);
644 static void ocfs2_orphan_scan_lock_res_init(struct ocfs2_lock_res
*res
,
645 struct ocfs2_super
*osb
)
647 struct ocfs2_orphan_scan_lvb
*lvb
;
649 ocfs2_lock_res_init_once(res
);
650 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_ORPHAN_SCAN
, 0, 0, res
->l_name
);
651 ocfs2_lock_res_init_common(osb
, res
, OCFS2_LOCK_TYPE_ORPHAN_SCAN
,
652 &ocfs2_orphan_scan_lops
, osb
);
653 lvb
= ocfs2_dlm_lvb(&res
->l_lksb
);
654 lvb
->lvb_version
= OCFS2_ORPHAN_LVB_VERSION
;
657 void ocfs2_file_lock_res_init(struct ocfs2_lock_res
*lockres
,
658 struct ocfs2_file_private
*fp
)
660 struct inode
*inode
= fp
->fp_file
->f_mapping
->host
;
661 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
663 ocfs2_lock_res_init_once(lockres
);
664 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK
, oi
->ip_blkno
,
665 inode
->i_generation
, lockres
->l_name
);
666 ocfs2_lock_res_init_common(OCFS2_SB(inode
->i_sb
), lockres
,
667 OCFS2_LOCK_TYPE_FLOCK
, &ocfs2_flock_lops
,
669 lockres
->l_flags
|= OCFS2_LOCK_NOCACHE
;
672 void ocfs2_qinfo_lock_res_init(struct ocfs2_lock_res
*lockres
,
673 struct ocfs2_mem_dqinfo
*info
)
675 ocfs2_lock_res_init_once(lockres
);
676 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_QINFO
, info
->dqi_gi
.dqi_type
,
678 ocfs2_lock_res_init_common(OCFS2_SB(info
->dqi_gi
.dqi_sb
), lockres
,
679 OCFS2_LOCK_TYPE_QINFO
, &ocfs2_qinfo_lops
,
683 void ocfs2_lock_res_free(struct ocfs2_lock_res
*res
)
687 if (!(res
->l_flags
& OCFS2_LOCK_INITIALIZED
))
690 ocfs2_remove_lockres_tracking(res
);
692 mlog_bug_on_msg(!list_empty(&res
->l_blocked_list
),
693 "Lockres %s is on the blocked list\n",
695 mlog_bug_on_msg(!list_empty(&res
->l_mask_waiters
),
696 "Lockres %s has mask waiters pending\n",
698 mlog_bug_on_msg(spin_is_locked(&res
->l_lock
),
699 "Lockres %s is locked\n",
701 mlog_bug_on_msg(res
->l_ro_holders
,
702 "Lockres %s has %u ro holders\n",
703 res
->l_name
, res
->l_ro_holders
);
704 mlog_bug_on_msg(res
->l_ex_holders
,
705 "Lockres %s has %u ex holders\n",
706 res
->l_name
, res
->l_ex_holders
);
708 /* Need to clear out the lock status block for the dlm */
709 memset(&res
->l_lksb
, 0, sizeof(res
->l_lksb
));
715 static inline void ocfs2_inc_holders(struct ocfs2_lock_res
*lockres
,
724 lockres
->l_ex_holders
++;
727 lockres
->l_ro_holders
++;
736 static inline void ocfs2_dec_holders(struct ocfs2_lock_res
*lockres
,
745 BUG_ON(!lockres
->l_ex_holders
);
746 lockres
->l_ex_holders
--;
749 BUG_ON(!lockres
->l_ro_holders
);
750 lockres
->l_ro_holders
--;
758 /* WARNING: This function lives in a world where the only three lock
759 * levels are EX, PR, and NL. It *will* have to be adjusted when more
760 * lock types are added. */
761 static inline int ocfs2_highest_compat_lock_level(int level
)
763 int new_level
= DLM_LOCK_EX
;
765 if (level
== DLM_LOCK_EX
)
766 new_level
= DLM_LOCK_NL
;
767 else if (level
== DLM_LOCK_PR
)
768 new_level
= DLM_LOCK_PR
;
772 static void lockres_set_flags(struct ocfs2_lock_res
*lockres
,
773 unsigned long newflags
)
775 struct ocfs2_mask_waiter
*mw
, *tmp
;
777 assert_spin_locked(&lockres
->l_lock
);
779 lockres
->l_flags
= newflags
;
781 list_for_each_entry_safe(mw
, tmp
, &lockres
->l_mask_waiters
, mw_item
) {
782 if ((lockres
->l_flags
& mw
->mw_mask
) != mw
->mw_goal
)
785 list_del_init(&mw
->mw_item
);
787 complete(&mw
->mw_complete
);
790 static void lockres_or_flags(struct ocfs2_lock_res
*lockres
, unsigned long or)
792 lockres_set_flags(lockres
, lockres
->l_flags
| or);
794 static void lockres_clear_flags(struct ocfs2_lock_res
*lockres
,
797 lockres_set_flags(lockres
, lockres
->l_flags
& ~clear
);
800 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res
*lockres
)
804 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BUSY
));
805 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
));
806 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BLOCKED
));
807 BUG_ON(lockres
->l_blocking
<= DLM_LOCK_NL
);
809 lockres
->l_level
= lockres
->l_requested
;
810 if (lockres
->l_level
<=
811 ocfs2_highest_compat_lock_level(lockres
->l_blocking
)) {
812 lockres
->l_blocking
= DLM_LOCK_NL
;
813 lockres_clear_flags(lockres
, OCFS2_LOCK_BLOCKED
);
815 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
820 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res
*lockres
)
824 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BUSY
));
825 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
));
827 /* Convert from RO to EX doesn't really need anything as our
828 * information is already up to data. Convert from NL to
829 * *anything* however should mark ourselves as needing an
831 if (lockres
->l_level
== DLM_LOCK_NL
&&
832 lockres
->l_ops
->flags
& LOCK_TYPE_REQUIRES_REFRESH
)
833 lockres_or_flags(lockres
, OCFS2_LOCK_NEEDS_REFRESH
);
835 lockres
->l_level
= lockres
->l_requested
;
836 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
841 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res
*lockres
)
845 BUG_ON((!(lockres
->l_flags
& OCFS2_LOCK_BUSY
)));
846 BUG_ON(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
);
848 if (lockres
->l_requested
> DLM_LOCK_NL
&&
849 !(lockres
->l_flags
& OCFS2_LOCK_LOCAL
) &&
850 lockres
->l_ops
->flags
& LOCK_TYPE_REQUIRES_REFRESH
)
851 lockres_or_flags(lockres
, OCFS2_LOCK_NEEDS_REFRESH
);
853 lockres
->l_level
= lockres
->l_requested
;
854 lockres_or_flags(lockres
, OCFS2_LOCK_ATTACHED
);
855 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
860 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res
*lockres
,
863 int needs_downconvert
= 0;
866 assert_spin_locked(&lockres
->l_lock
);
868 lockres_or_flags(lockres
, OCFS2_LOCK_BLOCKED
);
870 if (level
> lockres
->l_blocking
) {
871 /* only schedule a downconvert if we haven't already scheduled
872 * one that goes low enough to satisfy the level we're
873 * blocking. this also catches the case where we get
875 if (ocfs2_highest_compat_lock_level(level
) <
876 ocfs2_highest_compat_lock_level(lockres
->l_blocking
))
877 needs_downconvert
= 1;
879 lockres
->l_blocking
= level
;
882 mlog_exit(needs_downconvert
);
883 return needs_downconvert
;
887 * OCFS2_LOCK_PENDING and l_pending_gen.
889 * Why does OCFS2_LOCK_PENDING exist? To close a race between setting
890 * OCFS2_LOCK_BUSY and calling ocfs2_dlm_lock(). See ocfs2_unblock_lock()
891 * for more details on the race.
893 * OCFS2_LOCK_PENDING closes the race quite nicely. However, it introduces
894 * a race on itself. In o2dlm, we can get the ast before ocfs2_dlm_lock()
895 * returns. The ast clears OCFS2_LOCK_BUSY, and must therefore clear
896 * OCFS2_LOCK_PENDING at the same time. When ocfs2_dlm_lock() returns,
897 * the caller is going to try to clear PENDING again. If nothing else is
898 * happening, __lockres_clear_pending() sees PENDING is unset and does
901 * But what if another path (eg downconvert thread) has just started a
902 * new locking action? The other path has re-set PENDING. Our path
903 * cannot clear PENDING, because that will re-open the original race
909 * ocfs2_cluster_lock()
914 * ocfs2_locking_ast() ocfs2_downconvert_thread()
915 * clear PENDING ocfs2_unblock_lock()
918 * ocfs2_prepare_downconvert()
928 * So as you can see, we now have a window where l_lock is not held,
929 * PENDING is not set, and ocfs2_dlm_lock() has not been called.
931 * The core problem is that ocfs2_cluster_lock() has cleared the PENDING
932 * set by ocfs2_prepare_downconvert(). That wasn't nice.
934 * To solve this we introduce l_pending_gen. A call to
935 * lockres_clear_pending() will only do so when it is passed a generation
936 * number that matches the lockres. lockres_set_pending() will return the
937 * current generation number. When ocfs2_cluster_lock() goes to clear
938 * PENDING, it passes the generation it got from set_pending(). In our
939 * example above, the generation numbers will *not* match. Thus,
940 * ocfs2_cluster_lock() will not clear the PENDING set by
941 * ocfs2_prepare_downconvert().
944 /* Unlocked version for ocfs2_locking_ast() */
945 static void __lockres_clear_pending(struct ocfs2_lock_res
*lockres
,
946 unsigned int generation
,
947 struct ocfs2_super
*osb
)
949 assert_spin_locked(&lockres
->l_lock
);
952 * The ast and locking functions can race us here. The winner
953 * will clear pending, the loser will not.
955 if (!(lockres
->l_flags
& OCFS2_LOCK_PENDING
) ||
956 (lockres
->l_pending_gen
!= generation
))
959 lockres_clear_flags(lockres
, OCFS2_LOCK_PENDING
);
960 lockres
->l_pending_gen
++;
963 * The downconvert thread may have skipped us because we
964 * were PENDING. Wake it up.
966 if (lockres
->l_flags
& OCFS2_LOCK_BLOCKED
)
967 ocfs2_wake_downconvert_thread(osb
);
970 /* Locked version for callers of ocfs2_dlm_lock() */
971 static void lockres_clear_pending(struct ocfs2_lock_res
*lockres
,
972 unsigned int generation
,
973 struct ocfs2_super
*osb
)
977 spin_lock_irqsave(&lockres
->l_lock
, flags
);
978 __lockres_clear_pending(lockres
, generation
, osb
);
979 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
982 static unsigned int lockres_set_pending(struct ocfs2_lock_res
*lockres
)
984 assert_spin_locked(&lockres
->l_lock
);
985 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BUSY
));
987 lockres_or_flags(lockres
, OCFS2_LOCK_PENDING
);
989 return lockres
->l_pending_gen
;
993 static void ocfs2_blocking_ast(void *opaque
, int level
)
995 struct ocfs2_lock_res
*lockres
= opaque
;
996 struct ocfs2_super
*osb
= ocfs2_get_lockres_osb(lockres
);
997 int needs_downconvert
;
1000 BUG_ON(level
<= DLM_LOCK_NL
);
1002 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
1003 lockres
->l_name
, level
, lockres
->l_level
,
1004 ocfs2_lock_type_string(lockres
->l_type
));
1007 * We can skip the bast for locks which don't enable caching -
1008 * they'll be dropped at the earliest possible time anyway.
1010 if (lockres
->l_flags
& OCFS2_LOCK_NOCACHE
)
1013 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1014 needs_downconvert
= ocfs2_generic_handle_bast(lockres
, level
);
1015 if (needs_downconvert
)
1016 ocfs2_schedule_blocked_lock(osb
, lockres
);
1017 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1019 wake_up(&lockres
->l_event
);
1021 ocfs2_wake_downconvert_thread(osb
);
1024 static void ocfs2_locking_ast(void *opaque
)
1026 struct ocfs2_lock_res
*lockres
= opaque
;
1027 struct ocfs2_super
*osb
= ocfs2_get_lockres_osb(lockres
);
1028 unsigned long flags
;
1031 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1033 status
= ocfs2_dlm_lock_status(&lockres
->l_lksb
);
1035 if (status
== -EAGAIN
) {
1036 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
1041 mlog(ML_ERROR
, "lockres %s: lksb status value of %d!\n",
1042 lockres
->l_name
, status
);
1043 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1047 switch(lockres
->l_action
) {
1048 case OCFS2_AST_ATTACH
:
1049 ocfs2_generic_handle_attach_action(lockres
);
1050 lockres_clear_flags(lockres
, OCFS2_LOCK_LOCAL
);
1052 case OCFS2_AST_CONVERT
:
1053 ocfs2_generic_handle_convert_action(lockres
);
1055 case OCFS2_AST_DOWNCONVERT
:
1056 ocfs2_generic_handle_downconvert_action(lockres
);
1059 mlog(ML_ERROR
, "lockres %s: ast fired with invalid action: %u "
1060 "lockres flags = 0x%lx, unlock action: %u\n",
1061 lockres
->l_name
, lockres
->l_action
, lockres
->l_flags
,
1062 lockres
->l_unlock_action
);
1066 /* set it to something invalid so if we get called again we
1068 lockres
->l_action
= OCFS2_AST_INVALID
;
1070 /* Did we try to cancel this lock? Clear that state */
1071 if (lockres
->l_unlock_action
== OCFS2_UNLOCK_CANCEL_CONVERT
)
1072 lockres
->l_unlock_action
= OCFS2_UNLOCK_INVALID
;
1075 * We may have beaten the locking functions here. We certainly
1076 * know that dlm_lock() has been called :-)
1077 * Because we can't have two lock calls in flight at once, we
1078 * can use lockres->l_pending_gen.
1080 __lockres_clear_pending(lockres
, lockres
->l_pending_gen
, osb
);
1082 wake_up(&lockres
->l_event
);
1083 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1086 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res
*lockres
,
1089 unsigned long flags
;
1092 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1093 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
1095 lockres
->l_action
= OCFS2_AST_INVALID
;
1097 lockres
->l_unlock_action
= OCFS2_UNLOCK_INVALID
;
1098 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1100 wake_up(&lockres
->l_event
);
1104 /* Note: If we detect another process working on the lock (i.e.,
1105 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
1106 * to do the right thing in that case.
1108 static int ocfs2_lock_create(struct ocfs2_super
*osb
,
1109 struct ocfs2_lock_res
*lockres
,
1114 unsigned long flags
;
1119 mlog(0, "lock %s, level = %d, flags = %u\n", lockres
->l_name
, level
,
1122 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1123 if ((lockres
->l_flags
& OCFS2_LOCK_ATTACHED
) ||
1124 (lockres
->l_flags
& OCFS2_LOCK_BUSY
)) {
1125 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1129 lockres
->l_action
= OCFS2_AST_ATTACH
;
1130 lockres
->l_requested
= level
;
1131 lockres_or_flags(lockres
, OCFS2_LOCK_BUSY
);
1132 gen
= lockres_set_pending(lockres
);
1133 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1135 ret
= ocfs2_dlm_lock(osb
->cconn
,
1140 OCFS2_LOCK_ID_MAX_LEN
- 1,
1142 lockres_clear_pending(lockres
, gen
, osb
);
1144 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret
, lockres
);
1145 ocfs2_recover_from_dlm_error(lockres
, 1);
1148 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres
->l_name
);
1155 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res
*lockres
,
1158 unsigned long flags
;
1161 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1162 ret
= lockres
->l_flags
& flag
;
1163 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1168 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res
*lockres
)
1171 wait_event(lockres
->l_event
,
1172 !ocfs2_check_wait_flag(lockres
, OCFS2_LOCK_BUSY
));
1175 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res
*lockres
)
1178 wait_event(lockres
->l_event
,
1179 !ocfs2_check_wait_flag(lockres
, OCFS2_LOCK_REFRESHING
));
1182 /* predict what lock level we'll be dropping down to on behalf
1183 * of another node, and return true if the currently wanted
1184 * level will be compatible with it. */
1185 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res
*lockres
,
1188 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BLOCKED
));
1190 return wanted
<= ocfs2_highest_compat_lock_level(lockres
->l_blocking
);
1193 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter
*mw
)
1195 INIT_LIST_HEAD(&mw
->mw_item
);
1196 init_completion(&mw
->mw_complete
);
1197 ocfs2_init_start_time(mw
);
1200 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter
*mw
)
1202 wait_for_completion(&mw
->mw_complete
);
1203 /* Re-arm the completion in case we want to wait on it again */
1204 INIT_COMPLETION(mw
->mw_complete
);
1205 return mw
->mw_status
;
1208 static void lockres_add_mask_waiter(struct ocfs2_lock_res
*lockres
,
1209 struct ocfs2_mask_waiter
*mw
,
1213 BUG_ON(!list_empty(&mw
->mw_item
));
1215 assert_spin_locked(&lockres
->l_lock
);
1217 list_add_tail(&mw
->mw_item
, &lockres
->l_mask_waiters
);
1222 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
1223 * if the mask still hadn't reached its goal */
1224 static int lockres_remove_mask_waiter(struct ocfs2_lock_res
*lockres
,
1225 struct ocfs2_mask_waiter
*mw
)
1227 unsigned long flags
;
1230 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1231 if (!list_empty(&mw
->mw_item
)) {
1232 if ((lockres
->l_flags
& mw
->mw_mask
) != mw
->mw_goal
)
1235 list_del_init(&mw
->mw_item
);
1236 init_completion(&mw
->mw_complete
);
1238 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1244 static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter
*mw
,
1245 struct ocfs2_lock_res
*lockres
)
1249 ret
= wait_for_completion_interruptible(&mw
->mw_complete
);
1251 lockres_remove_mask_waiter(lockres
, mw
);
1253 ret
= mw
->mw_status
;
1254 /* Re-arm the completion in case we want to wait on it again */
1255 INIT_COMPLETION(mw
->mw_complete
);
1259 static int ocfs2_cluster_lock(struct ocfs2_super
*osb
,
1260 struct ocfs2_lock_res
*lockres
,
1265 struct ocfs2_mask_waiter mw
;
1266 int wait
, catch_signals
= !(osb
->s_mount_opt
& OCFS2_MOUNT_NOINTR
);
1267 int ret
= 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1268 unsigned long flags
;
1270 int noqueue_attempted
= 0;
1274 ocfs2_init_mask_waiter(&mw
);
1276 if (lockres
->l_ops
->flags
& LOCK_TYPE_USES_LVB
)
1277 lkm_flags
|= DLM_LKF_VALBLK
;
1282 if (catch_signals
&& signal_pending(current
)) {
1287 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1289 mlog_bug_on_msg(lockres
->l_flags
& OCFS2_LOCK_FREEING
,
1290 "Cluster lock called on freeing lockres %s! flags "
1291 "0x%lx\n", lockres
->l_name
, lockres
->l_flags
);
1293 /* We only compare against the currently granted level
1294 * here. If the lock is blocked waiting on a downconvert,
1295 * we'll get caught below. */
1296 if (lockres
->l_flags
& OCFS2_LOCK_BUSY
&&
1297 level
> lockres
->l_level
) {
1298 /* is someone sitting in dlm_lock? If so, wait on
1300 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BUSY
, 0);
1305 if (lockres
->l_flags
& OCFS2_LOCK_BLOCKED
&&
1306 !ocfs2_may_continue_on_blocked_lock(lockres
, level
)) {
1307 /* is the lock is currently blocked on behalf of
1309 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BLOCKED
, 0);
1314 if (level
> lockres
->l_level
) {
1315 if (noqueue_attempted
> 0) {
1319 if (lkm_flags
& DLM_LKF_NOQUEUE
)
1320 noqueue_attempted
= 1;
1322 if (lockres
->l_action
!= OCFS2_AST_INVALID
)
1323 mlog(ML_ERROR
, "lockres %s has action %u pending\n",
1324 lockres
->l_name
, lockres
->l_action
);
1326 if (!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
)) {
1327 lockres
->l_action
= OCFS2_AST_ATTACH
;
1328 lkm_flags
&= ~DLM_LKF_CONVERT
;
1330 lockres
->l_action
= OCFS2_AST_CONVERT
;
1331 lkm_flags
|= DLM_LKF_CONVERT
;
1334 lockres
->l_requested
= level
;
1335 lockres_or_flags(lockres
, OCFS2_LOCK_BUSY
);
1336 gen
= lockres_set_pending(lockres
);
1337 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1339 BUG_ON(level
== DLM_LOCK_IV
);
1340 BUG_ON(level
== DLM_LOCK_NL
);
1342 mlog(0, "lock %s, convert from %d to level = %d\n",
1343 lockres
->l_name
, lockres
->l_level
, level
);
1345 /* call dlm_lock to upgrade lock now */
1346 ret
= ocfs2_dlm_lock(osb
->cconn
,
1351 OCFS2_LOCK_ID_MAX_LEN
- 1,
1353 lockres_clear_pending(lockres
, gen
, osb
);
1355 if (!(lkm_flags
& DLM_LKF_NOQUEUE
) ||
1357 ocfs2_log_dlm_error("ocfs2_dlm_lock",
1360 ocfs2_recover_from_dlm_error(lockres
, 1);
1364 mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n",
1367 /* At this point we've gone inside the dlm and need to
1368 * complete our work regardless. */
1371 /* wait for busy to clear and carry on */
1375 /* Ok, if we get here then we're good to go. */
1376 ocfs2_inc_holders(lockres
, level
);
1380 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1383 * This is helping work around a lock inversion between the page lock
1384 * and dlm locks. One path holds the page lock while calling aops
1385 * which block acquiring dlm locks. The voting thread holds dlm
1386 * locks while acquiring page locks while down converting data locks.
1387 * This block is helping an aop path notice the inversion and back
1388 * off to unlock its page lock before trying the dlm lock again.
1390 if (wait
&& arg_flags
& OCFS2_LOCK_NONBLOCK
&&
1391 mw
.mw_mask
& (OCFS2_LOCK_BUSY
|OCFS2_LOCK_BLOCKED
)) {
1393 if (lockres_remove_mask_waiter(lockres
, &mw
))
1399 ret
= ocfs2_wait_for_mask(&mw
);
1404 ocfs2_update_lock_stats(lockres
, level
, &mw
, ret
);
1410 static void ocfs2_cluster_unlock(struct ocfs2_super
*osb
,
1411 struct ocfs2_lock_res
*lockres
,
1414 unsigned long flags
;
1417 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1418 ocfs2_dec_holders(lockres
, level
);
1419 ocfs2_downconvert_on_unlock(osb
, lockres
);
1420 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1424 static int ocfs2_create_new_lock(struct ocfs2_super
*osb
,
1425 struct ocfs2_lock_res
*lockres
,
1429 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
1430 unsigned long flags
;
1431 u32 lkm_flags
= local
? DLM_LKF_LOCAL
: 0;
1433 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1434 BUG_ON(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
);
1435 lockres_or_flags(lockres
, OCFS2_LOCK_LOCAL
);
1436 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1438 return ocfs2_lock_create(osb
, lockres
, level
, lkm_flags
);
1441 /* Grants us an EX lock on the data and metadata resources, skipping
1442 * the normal cluster directory lookup. Use this ONLY on newly created
1443 * inodes which other nodes can't possibly see, and which haven't been
1444 * hashed in the inode hash yet. This can give us a good performance
1445 * increase as it'll skip the network broadcast normally associated
1446 * with creating a new lock resource. */
1447 int ocfs2_create_new_inode_locks(struct inode
*inode
)
1450 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1453 BUG_ON(!ocfs2_inode_is_new(inode
));
1457 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1459 /* NOTE: That we don't increment any of the holder counts, nor
1460 * do we add anything to a journal handle. Since this is
1461 * supposed to be a new inode which the cluster doesn't know
1462 * about yet, there is no need to. As far as the LVB handling
1463 * is concerned, this is basically like acquiring an EX lock
1464 * on a resource which has an invalid one -- we'll set it
1465 * valid when we release the EX. */
1467 ret
= ocfs2_create_new_lock(osb
, &OCFS2_I(inode
)->ip_rw_lockres
, 1, 1);
1474 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
1475 * don't use a generation in their lock names.
1477 ret
= ocfs2_create_new_lock(osb
, &OCFS2_I(inode
)->ip_inode_lockres
, 1, 0);
1483 ret
= ocfs2_create_new_lock(osb
, &OCFS2_I(inode
)->ip_open_lockres
, 0, 0);
1494 int ocfs2_rw_lock(struct inode
*inode
, int write
)
1497 struct ocfs2_lock_res
*lockres
;
1498 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1504 mlog(0, "inode %llu take %s RW lock\n",
1505 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1506 write
? "EXMODE" : "PRMODE");
1508 if (ocfs2_mount_local(osb
))
1511 lockres
= &OCFS2_I(inode
)->ip_rw_lockres
;
1513 level
= write
? DLM_LOCK_EX
: DLM_LOCK_PR
;
1515 status
= ocfs2_cluster_lock(OCFS2_SB(inode
->i_sb
), lockres
, level
, 0,
1524 void ocfs2_rw_unlock(struct inode
*inode
, int write
)
1526 int level
= write
? DLM_LOCK_EX
: DLM_LOCK_PR
;
1527 struct ocfs2_lock_res
*lockres
= &OCFS2_I(inode
)->ip_rw_lockres
;
1528 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1532 mlog(0, "inode %llu drop %s RW lock\n",
1533 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1534 write
? "EXMODE" : "PRMODE");
1536 if (!ocfs2_mount_local(osb
))
1537 ocfs2_cluster_unlock(OCFS2_SB(inode
->i_sb
), lockres
, level
);
1543 * ocfs2_open_lock always get PR mode lock.
1545 int ocfs2_open_lock(struct inode
*inode
)
1548 struct ocfs2_lock_res
*lockres
;
1549 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1555 mlog(0, "inode %llu take PRMODE open lock\n",
1556 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1558 if (ocfs2_mount_local(osb
))
1561 lockres
= &OCFS2_I(inode
)->ip_open_lockres
;
1563 status
= ocfs2_cluster_lock(OCFS2_SB(inode
->i_sb
), lockres
,
1573 int ocfs2_try_open_lock(struct inode
*inode
, int write
)
1575 int status
= 0, level
;
1576 struct ocfs2_lock_res
*lockres
;
1577 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1583 mlog(0, "inode %llu try to take %s open lock\n",
1584 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1585 write
? "EXMODE" : "PRMODE");
1587 if (ocfs2_mount_local(osb
))
1590 lockres
= &OCFS2_I(inode
)->ip_open_lockres
;
1592 level
= write
? DLM_LOCK_EX
: DLM_LOCK_PR
;
1595 * The file system may already holding a PRMODE/EXMODE open lock.
1596 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
1597 * other nodes and the -EAGAIN will indicate to the caller that
1598 * this inode is still in use.
1600 status
= ocfs2_cluster_lock(OCFS2_SB(inode
->i_sb
), lockres
,
1601 level
, DLM_LKF_NOQUEUE
, 0);
1609 * ocfs2_open_unlock unlock PR and EX mode open locks.
1611 void ocfs2_open_unlock(struct inode
*inode
)
1613 struct ocfs2_lock_res
*lockres
= &OCFS2_I(inode
)->ip_open_lockres
;
1614 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1618 mlog(0, "inode %llu drop open lock\n",
1619 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1621 if (ocfs2_mount_local(osb
))
1624 if(lockres
->l_ro_holders
)
1625 ocfs2_cluster_unlock(OCFS2_SB(inode
->i_sb
), lockres
,
1627 if(lockres
->l_ex_holders
)
1628 ocfs2_cluster_unlock(OCFS2_SB(inode
->i_sb
), lockres
,
1635 static int ocfs2_flock_handle_signal(struct ocfs2_lock_res
*lockres
,
1639 struct ocfs2_super
*osb
= ocfs2_get_lockres_osb(lockres
);
1640 unsigned long flags
;
1641 struct ocfs2_mask_waiter mw
;
1643 ocfs2_init_mask_waiter(&mw
);
1646 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1647 if (lockres
->l_flags
& OCFS2_LOCK_BUSY
) {
1648 ret
= ocfs2_prepare_cancel_convert(osb
, lockres
);
1650 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1651 ret
= ocfs2_cancel_convert(osb
, lockres
);
1658 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BUSY
, 0);
1659 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1661 ocfs2_wait_for_mask(&mw
);
1667 * We may still have gotten the lock, in which case there's no
1668 * point to restarting the syscall.
1670 if (lockres
->l_level
== level
)
1673 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret
,
1674 lockres
->l_flags
, lockres
->l_level
, lockres
->l_action
);
1676 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1683 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1684 * flock() calls. The locking approach this requires is sufficiently
1685 * different from all other cluster lock types that we implement a
1686 * seperate path to the "low-level" dlm calls. In particular:
1688 * - No optimization of lock levels is done - we take at exactly
1689 * what's been requested.
1691 * - No lock caching is employed. We immediately downconvert to
1692 * no-lock at unlock time. This also means flock locks never go on
1693 * the blocking list).
1695 * - Since userspace can trivially deadlock itself with flock, we make
1696 * sure to allow cancellation of a misbehaving applications flock()
1699 * - Access to any flock lockres doesn't require concurrency, so we
1700 * can simplify the code by requiring the caller to guarantee
1701 * serialization of dlmglue flock calls.
1703 int ocfs2_file_lock(struct file
*file
, int ex
, int trylock
)
1705 int ret
, level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
1706 unsigned int lkm_flags
= trylock
? DLM_LKF_NOQUEUE
: 0;
1707 unsigned long flags
;
1708 struct ocfs2_file_private
*fp
= file
->private_data
;
1709 struct ocfs2_lock_res
*lockres
= &fp
->fp_flock
;
1710 struct ocfs2_super
*osb
= OCFS2_SB(file
->f_mapping
->host
->i_sb
);
1711 struct ocfs2_mask_waiter mw
;
1713 ocfs2_init_mask_waiter(&mw
);
1715 if ((lockres
->l_flags
& OCFS2_LOCK_BUSY
) ||
1716 (lockres
->l_level
> DLM_LOCK_NL
)) {
1718 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1719 "level: %u\n", lockres
->l_name
, lockres
->l_flags
,
1724 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1725 if (!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
)) {
1726 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BUSY
, 0);
1727 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1730 * Get the lock at NLMODE to start - that way we
1731 * can cancel the upconvert request if need be.
1733 ret
= ocfs2_lock_create(osb
, lockres
, DLM_LOCK_NL
, 0);
1739 ret
= ocfs2_wait_for_mask(&mw
);
1744 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1747 lockres
->l_action
= OCFS2_AST_CONVERT
;
1748 lkm_flags
|= DLM_LKF_CONVERT
;
1749 lockres
->l_requested
= level
;
1750 lockres_or_flags(lockres
, OCFS2_LOCK_BUSY
);
1752 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BUSY
, 0);
1753 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1755 ret
= ocfs2_dlm_lock(osb
->cconn
, level
, &lockres
->l_lksb
, lkm_flags
,
1756 lockres
->l_name
, OCFS2_LOCK_ID_MAX_LEN
- 1,
1759 if (!trylock
|| (ret
!= -EAGAIN
)) {
1760 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret
, lockres
);
1764 ocfs2_recover_from_dlm_error(lockres
, 1);
1765 lockres_remove_mask_waiter(lockres
, &mw
);
1769 ret
= ocfs2_wait_for_mask_interruptible(&mw
, lockres
);
1770 if (ret
== -ERESTARTSYS
) {
1772 * Userspace can cause deadlock itself with
1773 * flock(). Current behavior locally is to allow the
1774 * deadlock, but abort the system call if a signal is
1775 * received. We follow this example, otherwise a
1776 * poorly written program could sit in kernel until
1779 * Handling this is a bit more complicated for Ocfs2
1780 * though. We can't exit this function with an
1781 * outstanding lock request, so a cancel convert is
1782 * required. We intentionally overwrite 'ret' - if the
1783 * cancel fails and the lock was granted, it's easier
1784 * to just bubble sucess back up to the user.
1786 ret
= ocfs2_flock_handle_signal(lockres
, level
);
1787 } else if (!ret
&& (level
> lockres
->l_level
)) {
1788 /* Trylock failed asynchronously */
1795 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1796 lockres
->l_name
, ex
, trylock
, ret
);
1800 void ocfs2_file_unlock(struct file
*file
)
1804 unsigned long flags
;
1805 struct ocfs2_file_private
*fp
= file
->private_data
;
1806 struct ocfs2_lock_res
*lockres
= &fp
->fp_flock
;
1807 struct ocfs2_super
*osb
= OCFS2_SB(file
->f_mapping
->host
->i_sb
);
1808 struct ocfs2_mask_waiter mw
;
1810 ocfs2_init_mask_waiter(&mw
);
1812 if (!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
))
1815 if (lockres
->l_level
== DLM_LOCK_NL
)
1818 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1819 lockres
->l_name
, lockres
->l_flags
, lockres
->l_level
,
1822 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1824 * Fake a blocking ast for the downconvert code.
1826 lockres_or_flags(lockres
, OCFS2_LOCK_BLOCKED
);
1827 lockres
->l_blocking
= DLM_LOCK_EX
;
1829 gen
= ocfs2_prepare_downconvert(lockres
, DLM_LOCK_NL
);
1830 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BUSY
, 0);
1831 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1833 ret
= ocfs2_downconvert_lock(osb
, lockres
, DLM_LOCK_NL
, 0, gen
);
1839 ret
= ocfs2_wait_for_mask(&mw
);
1844 static void ocfs2_downconvert_on_unlock(struct ocfs2_super
*osb
,
1845 struct ocfs2_lock_res
*lockres
)
1851 /* If we know that another node is waiting on our lock, kick
1852 * the downconvert thread * pre-emptively when we reach a release
1854 if (lockres
->l_flags
& OCFS2_LOCK_BLOCKED
) {
1855 switch(lockres
->l_blocking
) {
1857 if (!lockres
->l_ex_holders
&& !lockres
->l_ro_holders
)
1861 if (!lockres
->l_ex_holders
)
1870 ocfs2_wake_downconvert_thread(osb
);
1875 #define OCFS2_SEC_BITS 34
1876 #define OCFS2_SEC_SHIFT (64 - 34)
1877 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1879 /* LVB only has room for 64 bits of time here so we pack it for
1881 static u64
ocfs2_pack_timespec(struct timespec
*spec
)
1884 u64 sec
= spec
->tv_sec
;
1885 u32 nsec
= spec
->tv_nsec
;
1887 res
= (sec
<< OCFS2_SEC_SHIFT
) | (nsec
& OCFS2_NSEC_MASK
);
1892 /* Call this with the lockres locked. I am reasonably sure we don't
1893 * need ip_lock in this function as anyone who would be changing those
1894 * values is supposed to be blocked in ocfs2_inode_lock right now. */
1895 static void __ocfs2_stuff_meta_lvb(struct inode
*inode
)
1897 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1898 struct ocfs2_lock_res
*lockres
= &oi
->ip_inode_lockres
;
1899 struct ocfs2_meta_lvb
*lvb
;
1903 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
1906 * Invalidate the LVB of a deleted inode - this way other
1907 * nodes are forced to go to disk and discover the new inode
1910 if (oi
->ip_flags
& OCFS2_INODE_DELETED
) {
1911 lvb
->lvb_version
= 0;
1915 lvb
->lvb_version
= OCFS2_LVB_VERSION
;
1916 lvb
->lvb_isize
= cpu_to_be64(i_size_read(inode
));
1917 lvb
->lvb_iclusters
= cpu_to_be32(oi
->ip_clusters
);
1918 lvb
->lvb_iuid
= cpu_to_be32(inode
->i_uid
);
1919 lvb
->lvb_igid
= cpu_to_be32(inode
->i_gid
);
1920 lvb
->lvb_imode
= cpu_to_be16(inode
->i_mode
);
1921 lvb
->lvb_inlink
= cpu_to_be16(inode
->i_nlink
);
1922 lvb
->lvb_iatime_packed
=
1923 cpu_to_be64(ocfs2_pack_timespec(&inode
->i_atime
));
1924 lvb
->lvb_ictime_packed
=
1925 cpu_to_be64(ocfs2_pack_timespec(&inode
->i_ctime
));
1926 lvb
->lvb_imtime_packed
=
1927 cpu_to_be64(ocfs2_pack_timespec(&inode
->i_mtime
));
1928 lvb
->lvb_iattr
= cpu_to_be32(oi
->ip_attr
);
1929 lvb
->lvb_idynfeatures
= cpu_to_be16(oi
->ip_dyn_features
);
1930 lvb
->lvb_igeneration
= cpu_to_be32(inode
->i_generation
);
1933 mlog_meta_lvb(0, lockres
);
1938 static void ocfs2_unpack_timespec(struct timespec
*spec
,
1941 spec
->tv_sec
= packed_time
>> OCFS2_SEC_SHIFT
;
1942 spec
->tv_nsec
= packed_time
& OCFS2_NSEC_MASK
;
1945 static void ocfs2_refresh_inode_from_lvb(struct inode
*inode
)
1947 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1948 struct ocfs2_lock_res
*lockres
= &oi
->ip_inode_lockres
;
1949 struct ocfs2_meta_lvb
*lvb
;
1953 mlog_meta_lvb(0, lockres
);
1955 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
1957 /* We're safe here without the lockres lock... */
1958 spin_lock(&oi
->ip_lock
);
1959 oi
->ip_clusters
= be32_to_cpu(lvb
->lvb_iclusters
);
1960 i_size_write(inode
, be64_to_cpu(lvb
->lvb_isize
));
1962 oi
->ip_attr
= be32_to_cpu(lvb
->lvb_iattr
);
1963 oi
->ip_dyn_features
= be16_to_cpu(lvb
->lvb_idynfeatures
);
1964 ocfs2_set_inode_flags(inode
);
1966 /* fast-symlinks are a special case */
1967 if (S_ISLNK(inode
->i_mode
) && !oi
->ip_clusters
)
1968 inode
->i_blocks
= 0;
1970 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
1972 inode
->i_uid
= be32_to_cpu(lvb
->lvb_iuid
);
1973 inode
->i_gid
= be32_to_cpu(lvb
->lvb_igid
);
1974 inode
->i_mode
= be16_to_cpu(lvb
->lvb_imode
);
1975 inode
->i_nlink
= be16_to_cpu(lvb
->lvb_inlink
);
1976 ocfs2_unpack_timespec(&inode
->i_atime
,
1977 be64_to_cpu(lvb
->lvb_iatime_packed
));
1978 ocfs2_unpack_timespec(&inode
->i_mtime
,
1979 be64_to_cpu(lvb
->lvb_imtime_packed
));
1980 ocfs2_unpack_timespec(&inode
->i_ctime
,
1981 be64_to_cpu(lvb
->lvb_ictime_packed
));
1982 spin_unlock(&oi
->ip_lock
);
1987 static inline int ocfs2_meta_lvb_is_trustable(struct inode
*inode
,
1988 struct ocfs2_lock_res
*lockres
)
1990 struct ocfs2_meta_lvb
*lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
1992 if (lvb
->lvb_version
== OCFS2_LVB_VERSION
1993 && be32_to_cpu(lvb
->lvb_igeneration
) == inode
->i_generation
)
1998 /* Determine whether a lock resource needs to be refreshed, and
1999 * arbitrate who gets to refresh it.
2001 * 0 means no refresh needed.
2003 * > 0 means you need to refresh this and you MUST call
2004 * ocfs2_complete_lock_res_refresh afterwards. */
2005 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res
*lockres
)
2007 unsigned long flags
;
2013 spin_lock_irqsave(&lockres
->l_lock
, flags
);
2014 if (!(lockres
->l_flags
& OCFS2_LOCK_NEEDS_REFRESH
)) {
2015 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2019 if (lockres
->l_flags
& OCFS2_LOCK_REFRESHING
) {
2020 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2022 ocfs2_wait_on_refreshing_lock(lockres
);
2026 /* Ok, I'll be the one to refresh this lock. */
2027 lockres_or_flags(lockres
, OCFS2_LOCK_REFRESHING
);
2028 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2036 /* If status is non zero, I'll mark it as not being in refresh
2037 * anymroe, but i won't clear the needs refresh flag. */
2038 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res
*lockres
,
2041 unsigned long flags
;
2044 spin_lock_irqsave(&lockres
->l_lock
, flags
);
2045 lockres_clear_flags(lockres
, OCFS2_LOCK_REFRESHING
);
2047 lockres_clear_flags(lockres
, OCFS2_LOCK_NEEDS_REFRESH
);
2048 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2050 wake_up(&lockres
->l_event
);
2055 /* may or may not return a bh if it went to disk. */
2056 static int ocfs2_inode_lock_update(struct inode
*inode
,
2057 struct buffer_head
**bh
)
2060 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2061 struct ocfs2_lock_res
*lockres
= &oi
->ip_inode_lockres
;
2062 struct ocfs2_dinode
*fe
;
2063 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2067 if (ocfs2_mount_local(osb
))
2070 spin_lock(&oi
->ip_lock
);
2071 if (oi
->ip_flags
& OCFS2_INODE_DELETED
) {
2072 mlog(0, "Orphaned inode %llu was deleted while we "
2073 "were waiting on a lock. ip_flags = 0x%x\n",
2074 (unsigned long long)oi
->ip_blkno
, oi
->ip_flags
);
2075 spin_unlock(&oi
->ip_lock
);
2079 spin_unlock(&oi
->ip_lock
);
2081 if (!ocfs2_should_refresh_lock_res(lockres
))
2084 /* This will discard any caching information we might have had
2085 * for the inode metadata. */
2086 ocfs2_metadata_cache_purge(inode
);
2088 ocfs2_extent_map_trunc(inode
, 0);
2090 if (ocfs2_meta_lvb_is_trustable(inode
, lockres
)) {
2091 mlog(0, "Trusting LVB on inode %llu\n",
2092 (unsigned long long)oi
->ip_blkno
);
2093 ocfs2_refresh_inode_from_lvb(inode
);
2095 /* Boo, we have to go to disk. */
2096 /* read bh, cast, ocfs2_refresh_inode */
2097 status
= ocfs2_read_inode_block(inode
, bh
);
2102 fe
= (struct ocfs2_dinode
*) (*bh
)->b_data
;
2104 /* This is a good chance to make sure we're not
2105 * locking an invalid object. ocfs2_read_inode_block()
2106 * already checked that the inode block is sane.
2108 * We bug on a stale inode here because we checked
2109 * above whether it was wiped from disk. The wiping
2110 * node provides a guarantee that we receive that
2111 * message and can mark the inode before dropping any
2112 * locks associated with it. */
2113 mlog_bug_on_msg(inode
->i_generation
!=
2114 le32_to_cpu(fe
->i_generation
),
2115 "Invalid dinode %llu disk generation: %u "
2116 "inode->i_generation: %u\n",
2117 (unsigned long long)oi
->ip_blkno
,
2118 le32_to_cpu(fe
->i_generation
),
2119 inode
->i_generation
);
2120 mlog_bug_on_msg(le64_to_cpu(fe
->i_dtime
) ||
2121 !(fe
->i_flags
& cpu_to_le32(OCFS2_VALID_FL
)),
2122 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
2123 (unsigned long long)oi
->ip_blkno
,
2124 (unsigned long long)le64_to_cpu(fe
->i_dtime
),
2125 le32_to_cpu(fe
->i_flags
));
2127 ocfs2_refresh_inode(inode
, fe
);
2128 ocfs2_track_lock_refresh(lockres
);
2133 ocfs2_complete_lock_res_refresh(lockres
, status
);
2139 static int ocfs2_assign_bh(struct inode
*inode
,
2140 struct buffer_head
**ret_bh
,
2141 struct buffer_head
*passed_bh
)
2146 /* Ok, the update went to disk for us, use the
2148 *ret_bh
= passed_bh
;
2154 status
= ocfs2_read_inode_block(inode
, ret_bh
);
2162 * returns < 0 error if the callback will never be called, otherwise
2163 * the result of the lock will be communicated via the callback.
2165 int ocfs2_inode_lock_full(struct inode
*inode
,
2166 struct buffer_head
**ret_bh
,
2170 int status
, level
, acquired
;
2172 struct ocfs2_lock_res
*lockres
= NULL
;
2173 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2174 struct buffer_head
*local_bh
= NULL
;
2180 mlog(0, "inode %llu, take %s META lock\n",
2181 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2182 ex
? "EXMODE" : "PRMODE");
2186 /* We'll allow faking a readonly metadata lock for
2188 if (ocfs2_is_hard_readonly(osb
)) {
2194 if (ocfs2_mount_local(osb
))
2197 if (!(arg_flags
& OCFS2_META_LOCK_RECOVERY
))
2198 ocfs2_wait_for_recovery(osb
);
2200 lockres
= &OCFS2_I(inode
)->ip_inode_lockres
;
2201 level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2203 if (arg_flags
& OCFS2_META_LOCK_NOQUEUE
)
2204 dlm_flags
|= DLM_LKF_NOQUEUE
;
2206 status
= ocfs2_cluster_lock(osb
, lockres
, level
, dlm_flags
, arg_flags
);
2208 if (status
!= -EAGAIN
&& status
!= -EIOCBRETRY
)
2213 /* Notify the error cleanup path to drop the cluster lock. */
2216 /* We wait twice because a node may have died while we were in
2217 * the lower dlm layers. The second time though, we've
2218 * committed to owning this lock so we don't allow signals to
2219 * abort the operation. */
2220 if (!(arg_flags
& OCFS2_META_LOCK_RECOVERY
))
2221 ocfs2_wait_for_recovery(osb
);
2225 * We only see this flag if we're being called from
2226 * ocfs2_read_locked_inode(). It means we're locking an inode
2227 * which hasn't been populated yet, so clear the refresh flag
2228 * and let the caller handle it.
2230 if (inode
->i_state
& I_NEW
) {
2233 ocfs2_complete_lock_res_refresh(lockres
, 0);
2237 /* This is fun. The caller may want a bh back, or it may
2238 * not. ocfs2_inode_lock_update definitely wants one in, but
2239 * may or may not read one, depending on what's in the
2240 * LVB. The result of all of this is that we've *only* gone to
2241 * disk if we have to, so the complexity is worthwhile. */
2242 status
= ocfs2_inode_lock_update(inode
, &local_bh
);
2244 if (status
!= -ENOENT
)
2250 status
= ocfs2_assign_bh(inode
, ret_bh
, local_bh
);
2259 if (ret_bh
&& (*ret_bh
)) {
2264 ocfs2_inode_unlock(inode
, ex
);
2275 * This is working around a lock inversion between tasks acquiring DLM
2276 * locks while holding a page lock and the downconvert thread which
2277 * blocks dlm lock acquiry while acquiring page locks.
2279 * ** These _with_page variantes are only intended to be called from aop
2280 * methods that hold page locks and return a very specific *positive* error
2281 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2283 * The DLM is called such that it returns -EAGAIN if it would have
2284 * blocked waiting for the downconvert thread. In that case we unlock
2285 * our page so the downconvert thread can make progress. Once we've
2286 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2287 * that called us can bubble that back up into the VFS who will then
2288 * immediately retry the aop call.
2290 * We do a blocking lock and immediate unlock before returning, though, so that
2291 * the lock has a great chance of being cached on this node by the time the VFS
2292 * calls back to retry the aop. This has a potential to livelock as nodes
2293 * ping locks back and forth, but that's a risk we're willing to take to avoid
2294 * the lock inversion simply.
2296 int ocfs2_inode_lock_with_page(struct inode
*inode
,
2297 struct buffer_head
**ret_bh
,
2303 ret
= ocfs2_inode_lock_full(inode
, ret_bh
, ex
, OCFS2_LOCK_NONBLOCK
);
2304 if (ret
== -EAGAIN
) {
2306 if (ocfs2_inode_lock(inode
, ret_bh
, ex
) == 0)
2307 ocfs2_inode_unlock(inode
, ex
);
2308 ret
= AOP_TRUNCATED_PAGE
;
2314 int ocfs2_inode_lock_atime(struct inode
*inode
,
2315 struct vfsmount
*vfsmnt
,
2321 ret
= ocfs2_inode_lock(inode
, NULL
, 0);
2328 * If we should update atime, we will get EX lock,
2329 * otherwise we just get PR lock.
2331 if (ocfs2_should_update_atime(inode
, vfsmnt
)) {
2332 struct buffer_head
*bh
= NULL
;
2334 ocfs2_inode_unlock(inode
, 0);
2335 ret
= ocfs2_inode_lock(inode
, &bh
, 1);
2341 if (ocfs2_should_update_atime(inode
, vfsmnt
))
2342 ocfs2_update_inode_atime(inode
, bh
);
2352 void ocfs2_inode_unlock(struct inode
*inode
,
2355 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2356 struct ocfs2_lock_res
*lockres
= &OCFS2_I(inode
)->ip_inode_lockres
;
2357 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2361 mlog(0, "inode %llu drop %s META lock\n",
2362 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2363 ex
? "EXMODE" : "PRMODE");
2365 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode
->i_sb
)) &&
2366 !ocfs2_mount_local(osb
))
2367 ocfs2_cluster_unlock(OCFS2_SB(inode
->i_sb
), lockres
, level
);
2372 int ocfs2_orphan_scan_lock(struct ocfs2_super
*osb
, u32
*seqno
, int ex
)
2374 struct ocfs2_lock_res
*lockres
;
2375 struct ocfs2_orphan_scan_lvb
*lvb
;
2376 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2379 lockres
= &osb
->osb_orphan_scan
.os_lockres
;
2380 status
= ocfs2_cluster_lock(osb
, lockres
, level
, 0, 0);
2384 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
2385 if (lvb
->lvb_version
== OCFS2_ORPHAN_LVB_VERSION
)
2386 *seqno
= be32_to_cpu(lvb
->lvb_os_seqno
);
2390 void ocfs2_orphan_scan_unlock(struct ocfs2_super
*osb
, u32 seqno
, int ex
)
2392 struct ocfs2_lock_res
*lockres
;
2393 struct ocfs2_orphan_scan_lvb
*lvb
;
2394 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2396 lockres
= &osb
->osb_orphan_scan
.os_lockres
;
2397 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
2398 lvb
->lvb_version
= OCFS2_ORPHAN_LVB_VERSION
;
2399 lvb
->lvb_os_seqno
= cpu_to_be32(seqno
);
2400 ocfs2_cluster_unlock(osb
, lockres
, level
);
2403 int ocfs2_super_lock(struct ocfs2_super
*osb
,
2407 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2408 struct ocfs2_lock_res
*lockres
= &osb
->osb_super_lockres
;
2412 if (ocfs2_is_hard_readonly(osb
))
2415 if (ocfs2_mount_local(osb
))
2418 status
= ocfs2_cluster_lock(osb
, lockres
, level
, 0, 0);
2424 /* The super block lock path is really in the best position to
2425 * know when resources covered by the lock need to be
2426 * refreshed, so we do it here. Of course, making sense of
2427 * everything is up to the caller :) */
2428 status
= ocfs2_should_refresh_lock_res(lockres
);
2434 status
= ocfs2_refresh_slot_info(osb
);
2436 ocfs2_complete_lock_res_refresh(lockres
, status
);
2440 ocfs2_track_lock_refresh(lockres
);
2447 void ocfs2_super_unlock(struct ocfs2_super
*osb
,
2450 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2451 struct ocfs2_lock_res
*lockres
= &osb
->osb_super_lockres
;
2453 if (!ocfs2_mount_local(osb
))
2454 ocfs2_cluster_unlock(osb
, lockres
, level
);
2457 int ocfs2_rename_lock(struct ocfs2_super
*osb
)
2460 struct ocfs2_lock_res
*lockres
= &osb
->osb_rename_lockres
;
2462 if (ocfs2_is_hard_readonly(osb
))
2465 if (ocfs2_mount_local(osb
))
2468 status
= ocfs2_cluster_lock(osb
, lockres
, DLM_LOCK_EX
, 0, 0);
2475 void ocfs2_rename_unlock(struct ocfs2_super
*osb
)
2477 struct ocfs2_lock_res
*lockres
= &osb
->osb_rename_lockres
;
2479 if (!ocfs2_mount_local(osb
))
2480 ocfs2_cluster_unlock(osb
, lockres
, DLM_LOCK_EX
);
2483 int ocfs2_nfs_sync_lock(struct ocfs2_super
*osb
, int ex
)
2486 struct ocfs2_lock_res
*lockres
= &osb
->osb_nfs_sync_lockres
;
2488 if (ocfs2_is_hard_readonly(osb
))
2491 if (ocfs2_mount_local(osb
))
2494 status
= ocfs2_cluster_lock(osb
, lockres
, ex
? LKM_EXMODE
: LKM_PRMODE
,
2497 mlog(ML_ERROR
, "lock on nfs sync lock failed %d\n", status
);
2502 void ocfs2_nfs_sync_unlock(struct ocfs2_super
*osb
, int ex
)
2504 struct ocfs2_lock_res
*lockres
= &osb
->osb_nfs_sync_lockres
;
2506 if (!ocfs2_mount_local(osb
))
2507 ocfs2_cluster_unlock(osb
, lockres
,
2508 ex
? LKM_EXMODE
: LKM_PRMODE
);
2511 int ocfs2_dentry_lock(struct dentry
*dentry
, int ex
)
2514 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2515 struct ocfs2_dentry_lock
*dl
= dentry
->d_fsdata
;
2516 struct ocfs2_super
*osb
= OCFS2_SB(dentry
->d_sb
);
2520 if (ocfs2_is_hard_readonly(osb
))
2523 if (ocfs2_mount_local(osb
))
2526 ret
= ocfs2_cluster_lock(osb
, &dl
->dl_lockres
, level
, 0, 0);
2533 void ocfs2_dentry_unlock(struct dentry
*dentry
, int ex
)
2535 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2536 struct ocfs2_dentry_lock
*dl
= dentry
->d_fsdata
;
2537 struct ocfs2_super
*osb
= OCFS2_SB(dentry
->d_sb
);
2539 if (!ocfs2_mount_local(osb
))
2540 ocfs2_cluster_unlock(osb
, &dl
->dl_lockres
, level
);
2543 /* Reference counting of the dlm debug structure. We want this because
2544 * open references on the debug inodes can live on after a mount, so
2545 * we can't rely on the ocfs2_super to always exist. */
2546 static void ocfs2_dlm_debug_free(struct kref
*kref
)
2548 struct ocfs2_dlm_debug
*dlm_debug
;
2550 dlm_debug
= container_of(kref
, struct ocfs2_dlm_debug
, d_refcnt
);
2555 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug
*dlm_debug
)
2558 kref_put(&dlm_debug
->d_refcnt
, ocfs2_dlm_debug_free
);
2561 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug
*debug
)
2563 kref_get(&debug
->d_refcnt
);
2566 struct ocfs2_dlm_debug
*ocfs2_new_dlm_debug(void)
2568 struct ocfs2_dlm_debug
*dlm_debug
;
2570 dlm_debug
= kmalloc(sizeof(struct ocfs2_dlm_debug
), GFP_KERNEL
);
2572 mlog_errno(-ENOMEM
);
2576 kref_init(&dlm_debug
->d_refcnt
);
2577 INIT_LIST_HEAD(&dlm_debug
->d_lockres_tracking
);
2578 dlm_debug
->d_locking_state
= NULL
;
2583 /* Access to this is arbitrated for us via seq_file->sem. */
2584 struct ocfs2_dlm_seq_priv
{
2585 struct ocfs2_dlm_debug
*p_dlm_debug
;
2586 struct ocfs2_lock_res p_iter_res
;
2587 struct ocfs2_lock_res p_tmp_res
;
2590 static struct ocfs2_lock_res
*ocfs2_dlm_next_res(struct ocfs2_lock_res
*start
,
2591 struct ocfs2_dlm_seq_priv
*priv
)
2593 struct ocfs2_lock_res
*iter
, *ret
= NULL
;
2594 struct ocfs2_dlm_debug
*dlm_debug
= priv
->p_dlm_debug
;
2596 assert_spin_locked(&ocfs2_dlm_tracking_lock
);
2598 list_for_each_entry(iter
, &start
->l_debug_list
, l_debug_list
) {
2599 /* discover the head of the list */
2600 if (&iter
->l_debug_list
== &dlm_debug
->d_lockres_tracking
) {
2601 mlog(0, "End of list found, %p\n", ret
);
2605 /* We track our "dummy" iteration lockres' by a NULL
2607 if (iter
->l_ops
!= NULL
) {
2616 static void *ocfs2_dlm_seq_start(struct seq_file
*m
, loff_t
*pos
)
2618 struct ocfs2_dlm_seq_priv
*priv
= m
->private;
2619 struct ocfs2_lock_res
*iter
;
2621 spin_lock(&ocfs2_dlm_tracking_lock
);
2622 iter
= ocfs2_dlm_next_res(&priv
->p_iter_res
, priv
);
2624 /* Since lockres' have the lifetime of their container
2625 * (which can be inodes, ocfs2_supers, etc) we want to
2626 * copy this out to a temporary lockres while still
2627 * under the spinlock. Obviously after this we can't
2628 * trust any pointers on the copy returned, but that's
2629 * ok as the information we want isn't typically held
2631 priv
->p_tmp_res
= *iter
;
2632 iter
= &priv
->p_tmp_res
;
2634 spin_unlock(&ocfs2_dlm_tracking_lock
);
2639 static void ocfs2_dlm_seq_stop(struct seq_file
*m
, void *v
)
2643 static void *ocfs2_dlm_seq_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2645 struct ocfs2_dlm_seq_priv
*priv
= m
->private;
2646 struct ocfs2_lock_res
*iter
= v
;
2647 struct ocfs2_lock_res
*dummy
= &priv
->p_iter_res
;
2649 spin_lock(&ocfs2_dlm_tracking_lock
);
2650 iter
= ocfs2_dlm_next_res(iter
, priv
);
2651 list_del_init(&dummy
->l_debug_list
);
2653 list_add(&dummy
->l_debug_list
, &iter
->l_debug_list
);
2654 priv
->p_tmp_res
= *iter
;
2655 iter
= &priv
->p_tmp_res
;
2657 spin_unlock(&ocfs2_dlm_tracking_lock
);
2662 /* So that debugfs.ocfs2 can determine which format is being used */
2663 #define OCFS2_DLM_DEBUG_STR_VERSION 2
2664 static int ocfs2_dlm_seq_show(struct seq_file
*m
, void *v
)
2668 struct ocfs2_lock_res
*lockres
= v
;
2673 seq_printf(m
, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION
);
2675 if (lockres
->l_type
== OCFS2_LOCK_TYPE_DENTRY
)
2676 seq_printf(m
, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START
- 1,
2678 (unsigned int)ocfs2_get_dentry_lock_ino(lockres
));
2680 seq_printf(m
, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN
, lockres
->l_name
);
2682 seq_printf(m
, "%d\t"
2693 lockres
->l_unlock_action
,
2694 lockres
->l_ro_holders
,
2695 lockres
->l_ex_holders
,
2696 lockres
->l_requested
,
2697 lockres
->l_blocking
);
2699 /* Dump the raw LVB */
2700 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
2701 for(i
= 0; i
< DLM_LVB_LEN
; i
++)
2702 seq_printf(m
, "0x%x\t", lvb
[i
]);
2704 #ifdef CONFIG_OCFS2_FS_STATS
2705 # define lock_num_prmode(_l) (_l)->l_lock_num_prmode
2706 # define lock_num_exmode(_l) (_l)->l_lock_num_exmode
2707 # define lock_num_prmode_failed(_l) (_l)->l_lock_num_prmode_failed
2708 # define lock_num_exmode_failed(_l) (_l)->l_lock_num_exmode_failed
2709 # define lock_total_prmode(_l) (_l)->l_lock_total_prmode
2710 # define lock_total_exmode(_l) (_l)->l_lock_total_exmode
2711 # define lock_max_prmode(_l) (_l)->l_lock_max_prmode
2712 # define lock_max_exmode(_l) (_l)->l_lock_max_exmode
2713 # define lock_refresh(_l) (_l)->l_lock_refresh
2715 # define lock_num_prmode(_l) (0ULL)
2716 # define lock_num_exmode(_l) (0ULL)
2717 # define lock_num_prmode_failed(_l) (0)
2718 # define lock_num_exmode_failed(_l) (0)
2719 # define lock_total_prmode(_l) (0ULL)
2720 # define lock_total_exmode(_l) (0ULL)
2721 # define lock_max_prmode(_l) (0)
2722 # define lock_max_exmode(_l) (0)
2723 # define lock_refresh(_l) (0)
2725 /* The following seq_print was added in version 2 of this output */
2726 seq_printf(m
, "%llu\t"
2735 lock_num_prmode(lockres
),
2736 lock_num_exmode(lockres
),
2737 lock_num_prmode_failed(lockres
),
2738 lock_num_exmode_failed(lockres
),
2739 lock_total_prmode(lockres
),
2740 lock_total_exmode(lockres
),
2741 lock_max_prmode(lockres
),
2742 lock_max_exmode(lockres
),
2743 lock_refresh(lockres
));
2746 seq_printf(m
, "\n");
2750 static const struct seq_operations ocfs2_dlm_seq_ops
= {
2751 .start
= ocfs2_dlm_seq_start
,
2752 .stop
= ocfs2_dlm_seq_stop
,
2753 .next
= ocfs2_dlm_seq_next
,
2754 .show
= ocfs2_dlm_seq_show
,
2757 static int ocfs2_dlm_debug_release(struct inode
*inode
, struct file
*file
)
2759 struct seq_file
*seq
= (struct seq_file
*) file
->private_data
;
2760 struct ocfs2_dlm_seq_priv
*priv
= seq
->private;
2761 struct ocfs2_lock_res
*res
= &priv
->p_iter_res
;
2763 ocfs2_remove_lockres_tracking(res
);
2764 ocfs2_put_dlm_debug(priv
->p_dlm_debug
);
2765 return seq_release_private(inode
, file
);
2768 static int ocfs2_dlm_debug_open(struct inode
*inode
, struct file
*file
)
2771 struct ocfs2_dlm_seq_priv
*priv
;
2772 struct seq_file
*seq
;
2773 struct ocfs2_super
*osb
;
2775 priv
= kzalloc(sizeof(struct ocfs2_dlm_seq_priv
), GFP_KERNEL
);
2781 osb
= inode
->i_private
;
2782 ocfs2_get_dlm_debug(osb
->osb_dlm_debug
);
2783 priv
->p_dlm_debug
= osb
->osb_dlm_debug
;
2784 INIT_LIST_HEAD(&priv
->p_iter_res
.l_debug_list
);
2786 ret
= seq_open(file
, &ocfs2_dlm_seq_ops
);
2793 seq
= (struct seq_file
*) file
->private_data
;
2794 seq
->private = priv
;
2796 ocfs2_add_lockres_tracking(&priv
->p_iter_res
,
2803 static const struct file_operations ocfs2_dlm_debug_fops
= {
2804 .open
= ocfs2_dlm_debug_open
,
2805 .release
= ocfs2_dlm_debug_release
,
2807 .llseek
= seq_lseek
,
2810 static int ocfs2_dlm_init_debug(struct ocfs2_super
*osb
)
2813 struct ocfs2_dlm_debug
*dlm_debug
= osb
->osb_dlm_debug
;
2815 dlm_debug
->d_locking_state
= debugfs_create_file("locking_state",
2817 osb
->osb_debug_root
,
2819 &ocfs2_dlm_debug_fops
);
2820 if (!dlm_debug
->d_locking_state
) {
2823 "Unable to create locking state debugfs file.\n");
2827 ocfs2_get_dlm_debug(dlm_debug
);
2832 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super
*osb
)
2834 struct ocfs2_dlm_debug
*dlm_debug
= osb
->osb_dlm_debug
;
2837 debugfs_remove(dlm_debug
->d_locking_state
);
2838 ocfs2_put_dlm_debug(dlm_debug
);
2842 int ocfs2_dlm_init(struct ocfs2_super
*osb
)
2845 struct ocfs2_cluster_connection
*conn
= NULL
;
2849 if (ocfs2_mount_local(osb
)) {
2854 status
= ocfs2_dlm_init_debug(osb
);
2860 /* launch downconvert thread */
2861 osb
->dc_task
= kthread_run(ocfs2_downconvert_thread
, osb
, "ocfs2dc");
2862 if (IS_ERR(osb
->dc_task
)) {
2863 status
= PTR_ERR(osb
->dc_task
);
2864 osb
->dc_task
= NULL
;
2869 /* for now, uuid == domain */
2870 status
= ocfs2_cluster_connect(osb
->osb_cluster_stack
,
2872 strlen(osb
->uuid_str
),
2873 ocfs2_do_node_down
, osb
,
2880 status
= ocfs2_cluster_this_node(&osb
->node_num
);
2884 "could not find this host's node number\n");
2885 ocfs2_cluster_disconnect(conn
, 0);
2890 ocfs2_super_lock_res_init(&osb
->osb_super_lockres
, osb
);
2891 ocfs2_rename_lock_res_init(&osb
->osb_rename_lockres
, osb
);
2892 ocfs2_nfs_sync_lock_res_init(&osb
->osb_nfs_sync_lockres
, osb
);
2893 ocfs2_orphan_scan_lock_res_init(&osb
->osb_orphan_scan
.os_lockres
, osb
);
2900 ocfs2_dlm_shutdown_debug(osb
);
2902 kthread_stop(osb
->dc_task
);
2909 void ocfs2_dlm_shutdown(struct ocfs2_super
*osb
,
2914 ocfs2_drop_osb_locks(osb
);
2917 * Now that we have dropped all locks and ocfs2_dismount_volume()
2918 * has disabled recovery, the DLM won't be talking to us. It's
2919 * safe to tear things down before disconnecting the cluster.
2923 kthread_stop(osb
->dc_task
);
2924 osb
->dc_task
= NULL
;
2927 ocfs2_lock_res_free(&osb
->osb_super_lockres
);
2928 ocfs2_lock_res_free(&osb
->osb_rename_lockres
);
2929 ocfs2_lock_res_free(&osb
->osb_nfs_sync_lockres
);
2930 ocfs2_lock_res_free(&osb
->osb_orphan_scan
.os_lockres
);
2932 ocfs2_cluster_disconnect(osb
->cconn
, hangup_pending
);
2935 ocfs2_dlm_shutdown_debug(osb
);
2940 static void ocfs2_unlock_ast(void *opaque
, int error
)
2942 struct ocfs2_lock_res
*lockres
= opaque
;
2943 unsigned long flags
;
2947 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres
->l_name
,
2948 lockres
->l_unlock_action
);
2950 spin_lock_irqsave(&lockres
->l_lock
, flags
);
2952 mlog(ML_ERROR
, "Dlm passes error %d for lock %s, "
2953 "unlock_action %d\n", error
, lockres
->l_name
,
2954 lockres
->l_unlock_action
);
2955 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2959 switch(lockres
->l_unlock_action
) {
2960 case OCFS2_UNLOCK_CANCEL_CONVERT
:
2961 mlog(0, "Cancel convert success for %s\n", lockres
->l_name
);
2962 lockres
->l_action
= OCFS2_AST_INVALID
;
2963 /* Downconvert thread may have requeued this lock, we
2964 * need to wake it. */
2965 if (lockres
->l_flags
& OCFS2_LOCK_BLOCKED
)
2966 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres
));
2968 case OCFS2_UNLOCK_DROP_LOCK
:
2969 lockres
->l_level
= DLM_LOCK_IV
;
2975 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
2976 lockres
->l_unlock_action
= OCFS2_UNLOCK_INVALID
;
2977 wake_up(&lockres
->l_event
);
2978 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2983 static int ocfs2_drop_lock(struct ocfs2_super
*osb
,
2984 struct ocfs2_lock_res
*lockres
)
2987 unsigned long flags
;
2990 /* We didn't get anywhere near actually using this lockres. */
2991 if (!(lockres
->l_flags
& OCFS2_LOCK_INITIALIZED
))
2994 if (lockres
->l_ops
->flags
& LOCK_TYPE_USES_LVB
)
2995 lkm_flags
|= DLM_LKF_VALBLK
;
2997 spin_lock_irqsave(&lockres
->l_lock
, flags
);
2999 mlog_bug_on_msg(!(lockres
->l_flags
& OCFS2_LOCK_FREEING
),
3000 "lockres %s, flags 0x%lx\n",
3001 lockres
->l_name
, lockres
->l_flags
);
3003 while (lockres
->l_flags
& OCFS2_LOCK_BUSY
) {
3004 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
3005 "%u, unlock_action = %u\n",
3006 lockres
->l_name
, lockres
->l_flags
, lockres
->l_action
,
3007 lockres
->l_unlock_action
);
3009 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3011 /* XXX: Today we just wait on any busy
3012 * locks... Perhaps we need to cancel converts in the
3014 ocfs2_wait_on_busy_lock(lockres
);
3016 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3019 if (lockres
->l_ops
->flags
& LOCK_TYPE_USES_LVB
) {
3020 if (lockres
->l_flags
& OCFS2_LOCK_ATTACHED
&&
3021 lockres
->l_level
== DLM_LOCK_EX
&&
3022 !(lockres
->l_flags
& OCFS2_LOCK_NEEDS_REFRESH
))
3023 lockres
->l_ops
->set_lvb(lockres
);
3026 if (lockres
->l_flags
& OCFS2_LOCK_BUSY
)
3027 mlog(ML_ERROR
, "destroying busy lock: \"%s\"\n",
3029 if (lockres
->l_flags
& OCFS2_LOCK_BLOCKED
)
3030 mlog(0, "destroying blocked lock: \"%s\"\n", lockres
->l_name
);
3032 if (!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
)) {
3033 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3037 lockres_clear_flags(lockres
, OCFS2_LOCK_ATTACHED
);
3039 /* make sure we never get here while waiting for an ast to
3041 BUG_ON(lockres
->l_action
!= OCFS2_AST_INVALID
);
3043 /* is this necessary? */
3044 lockres_or_flags(lockres
, OCFS2_LOCK_BUSY
);
3045 lockres
->l_unlock_action
= OCFS2_UNLOCK_DROP_LOCK
;
3046 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3048 mlog(0, "lock %s\n", lockres
->l_name
);
3050 ret
= ocfs2_dlm_unlock(osb
->cconn
, &lockres
->l_lksb
, lkm_flags
,
3053 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret
, lockres
);
3054 mlog(ML_ERROR
, "lockres flags: %lu\n", lockres
->l_flags
);
3055 ocfs2_dlm_dump_lksb(&lockres
->l_lksb
);
3058 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
3061 ocfs2_wait_on_busy_lock(lockres
);
3067 /* Mark the lockres as being dropped. It will no longer be
3068 * queued if blocking, but we still may have to wait on it
3069 * being dequeued from the downconvert thread before we can consider
3072 * You can *not* attempt to call cluster_lock on this lockres anymore. */
3073 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res
*lockres
)
3076 struct ocfs2_mask_waiter mw
;
3077 unsigned long flags
;
3079 ocfs2_init_mask_waiter(&mw
);
3081 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3082 lockres
->l_flags
|= OCFS2_LOCK_FREEING
;
3083 while (lockres
->l_flags
& OCFS2_LOCK_QUEUED
) {
3084 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_QUEUED
, 0);
3085 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3087 mlog(0, "Waiting on lockres %s\n", lockres
->l_name
);
3089 status
= ocfs2_wait_for_mask(&mw
);
3093 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3095 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3098 void ocfs2_simple_drop_lockres(struct ocfs2_super
*osb
,
3099 struct ocfs2_lock_res
*lockres
)
3103 ocfs2_mark_lockres_freeing(lockres
);
3104 ret
= ocfs2_drop_lock(osb
, lockres
);
3109 static void ocfs2_drop_osb_locks(struct ocfs2_super
*osb
)
3111 ocfs2_simple_drop_lockres(osb
, &osb
->osb_super_lockres
);
3112 ocfs2_simple_drop_lockres(osb
, &osb
->osb_rename_lockres
);
3113 ocfs2_simple_drop_lockres(osb
, &osb
->osb_nfs_sync_lockres
);
3114 ocfs2_simple_drop_lockres(osb
, &osb
->osb_orphan_scan
.os_lockres
);
3117 int ocfs2_drop_inode_locks(struct inode
*inode
)
3123 /* No need to call ocfs2_mark_lockres_freeing here -
3124 * ocfs2_clear_inode has done it for us. */
3126 err
= ocfs2_drop_lock(OCFS2_SB(inode
->i_sb
),
3127 &OCFS2_I(inode
)->ip_open_lockres
);
3133 err
= ocfs2_drop_lock(OCFS2_SB(inode
->i_sb
),
3134 &OCFS2_I(inode
)->ip_inode_lockres
);
3137 if (err
< 0 && !status
)
3140 err
= ocfs2_drop_lock(OCFS2_SB(inode
->i_sb
),
3141 &OCFS2_I(inode
)->ip_rw_lockres
);
3144 if (err
< 0 && !status
)
3151 static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res
*lockres
,
3154 assert_spin_locked(&lockres
->l_lock
);
3156 BUG_ON(lockres
->l_blocking
<= DLM_LOCK_NL
);
3158 if (lockres
->l_level
<= new_level
) {
3159 mlog(ML_ERROR
, "lockres->l_level (%d) <= new_level (%d)\n",
3160 lockres
->l_level
, new_level
);
3164 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
3165 lockres
->l_name
, new_level
, lockres
->l_blocking
);
3167 lockres
->l_action
= OCFS2_AST_DOWNCONVERT
;
3168 lockres
->l_requested
= new_level
;
3169 lockres_or_flags(lockres
, OCFS2_LOCK_BUSY
);
3170 return lockres_set_pending(lockres
);
3173 static int ocfs2_downconvert_lock(struct ocfs2_super
*osb
,
3174 struct ocfs2_lock_res
*lockres
,
3177 unsigned int generation
)
3180 u32 dlm_flags
= DLM_LKF_CONVERT
;
3185 dlm_flags
|= DLM_LKF_VALBLK
;
3187 ret
= ocfs2_dlm_lock(osb
->cconn
,
3192 OCFS2_LOCK_ID_MAX_LEN
- 1,
3194 lockres_clear_pending(lockres
, generation
, osb
);
3196 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret
, lockres
);
3197 ocfs2_recover_from_dlm_error(lockres
, 1);
3207 /* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
3208 static int ocfs2_prepare_cancel_convert(struct ocfs2_super
*osb
,
3209 struct ocfs2_lock_res
*lockres
)
3211 assert_spin_locked(&lockres
->l_lock
);
3214 mlog(0, "lock %s\n", lockres
->l_name
);
3216 if (lockres
->l_unlock_action
== OCFS2_UNLOCK_CANCEL_CONVERT
) {
3217 /* If we're already trying to cancel a lock conversion
3218 * then just drop the spinlock and allow the caller to
3219 * requeue this lock. */
3221 mlog(0, "Lockres %s, skip convert\n", lockres
->l_name
);
3225 /* were we in a convert when we got the bast fire? */
3226 BUG_ON(lockres
->l_action
!= OCFS2_AST_CONVERT
&&
3227 lockres
->l_action
!= OCFS2_AST_DOWNCONVERT
);
3228 /* set things up for the unlockast to know to just
3229 * clear out the ast_action and unset busy, etc. */
3230 lockres
->l_unlock_action
= OCFS2_UNLOCK_CANCEL_CONVERT
;
3232 mlog_bug_on_msg(!(lockres
->l_flags
& OCFS2_LOCK_BUSY
),
3233 "lock %s, invalid flags: 0x%lx\n",
3234 lockres
->l_name
, lockres
->l_flags
);
3239 static int ocfs2_cancel_convert(struct ocfs2_super
*osb
,
3240 struct ocfs2_lock_res
*lockres
)
3245 mlog(0, "lock %s\n", lockres
->l_name
);
3247 ret
= ocfs2_dlm_unlock(osb
->cconn
, &lockres
->l_lksb
,
3248 DLM_LKF_CANCEL
, lockres
);
3250 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret
, lockres
);
3251 ocfs2_recover_from_dlm_error(lockres
, 0);
3254 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres
->l_name
);
3260 static int ocfs2_unblock_lock(struct ocfs2_super
*osb
,
3261 struct ocfs2_lock_res
*lockres
,
3262 struct ocfs2_unblock_ctl
*ctl
)
3264 unsigned long flags
;
3273 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3275 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BLOCKED
));
3278 if (lockres
->l_flags
& OCFS2_LOCK_BUSY
) {
3280 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3281 * exists entirely for one reason - another thread has set
3282 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3284 * If we do ocfs2_cancel_convert() before the other thread
3285 * calls dlm_lock(), our cancel will do nothing. We will
3286 * get no ast, and we will have no way of knowing the
3287 * cancel failed. Meanwhile, the other thread will call
3288 * into dlm_lock() and wait...forever.
3290 * Why forever? Because another node has asked for the
3291 * lock first; that's why we're here in unblock_lock().
3293 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3294 * set, we just requeue the unblock. Only when the other
3295 * thread has called dlm_lock() and cleared PENDING will
3296 * we then cancel their request.
3298 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3299 * at the same time they set OCFS2_DLM_BUSY. They must
3300 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3302 if (lockres
->l_flags
& OCFS2_LOCK_PENDING
)
3306 ret
= ocfs2_prepare_cancel_convert(osb
, lockres
);
3307 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3309 ret
= ocfs2_cancel_convert(osb
, lockres
);
3316 /* if we're blocking an exclusive and we have *any* holders,
3318 if ((lockres
->l_blocking
== DLM_LOCK_EX
)
3319 && (lockres
->l_ex_holders
|| lockres
->l_ro_holders
))
3322 /* If it's a PR we're blocking, then only
3323 * requeue if we've got any EX holders */
3324 if (lockres
->l_blocking
== DLM_LOCK_PR
&&
3325 lockres
->l_ex_holders
)
3329 * Can we get a lock in this state if the holder counts are
3330 * zero? The meta data unblock code used to check this.
3332 if ((lockres
->l_ops
->flags
& LOCK_TYPE_REQUIRES_REFRESH
)
3333 && (lockres
->l_flags
& OCFS2_LOCK_REFRESHING
))
3336 new_level
= ocfs2_highest_compat_lock_level(lockres
->l_blocking
);
3338 if (lockres
->l_ops
->check_downconvert
3339 && !lockres
->l_ops
->check_downconvert(lockres
, new_level
))
3342 /* If we get here, then we know that there are no more
3343 * incompatible holders (and anyone asking for an incompatible
3344 * lock is blocked). We can now downconvert the lock */
3345 if (!lockres
->l_ops
->downconvert_worker
)
3348 /* Some lockres types want to do a bit of work before
3349 * downconverting a lock. Allow that here. The worker function
3350 * may sleep, so we save off a copy of what we're blocking as
3351 * it may change while we're not holding the spin lock. */
3352 blocking
= lockres
->l_blocking
;
3353 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3355 ctl
->unblock_action
= lockres
->l_ops
->downconvert_worker(lockres
, blocking
);
3357 if (ctl
->unblock_action
== UNBLOCK_STOP_POST
)
3360 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3361 if (blocking
!= lockres
->l_blocking
) {
3362 /* If this changed underneath us, then we can't drop
3370 if (lockres
->l_ops
->flags
& LOCK_TYPE_USES_LVB
) {
3371 if (lockres
->l_level
== DLM_LOCK_EX
)
3375 * We only set the lvb if the lock has been fully
3376 * refreshed - otherwise we risk setting stale
3377 * data. Otherwise, there's no need to actually clear
3378 * out the lvb here as it's value is still valid.
3380 if (set_lvb
&& !(lockres
->l_flags
& OCFS2_LOCK_NEEDS_REFRESH
))
3381 lockres
->l_ops
->set_lvb(lockres
);
3384 gen
= ocfs2_prepare_downconvert(lockres
, new_level
);
3385 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3386 ret
= ocfs2_downconvert_lock(osb
, lockres
, new_level
, set_lvb
,
3394 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3401 static int ocfs2_data_convert_worker(struct ocfs2_lock_res
*lockres
,
3404 struct inode
*inode
;
3405 struct address_space
*mapping
;
3407 inode
= ocfs2_lock_res_inode(lockres
);
3408 mapping
= inode
->i_mapping
;
3410 if (!S_ISREG(inode
->i_mode
))
3414 * We need this before the filemap_fdatawrite() so that it can
3415 * transfer the dirty bit from the PTE to the
3416 * page. Unfortunately this means that even for EX->PR
3417 * downconverts, we'll lose our mappings and have to build
3420 unmap_mapping_range(mapping
, 0, 0, 0);
3422 if (filemap_fdatawrite(mapping
)) {
3423 mlog(ML_ERROR
, "Could not sync inode %llu for downconvert!",
3424 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
3426 sync_mapping_buffers(mapping
);
3427 if (blocking
== DLM_LOCK_EX
) {
3428 truncate_inode_pages(mapping
, 0);
3430 /* We only need to wait on the I/O if we're not also
3431 * truncating pages because truncate_inode_pages waits
3432 * for us above. We don't truncate pages if we're
3433 * blocking anything < EXMODE because we want to keep
3434 * them around in that case. */
3435 filemap_fdatawait(mapping
);
3439 return UNBLOCK_CONTINUE
;
3442 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res
*lockres
,
3445 struct inode
*inode
= ocfs2_lock_res_inode(lockres
);
3446 int checkpointed
= ocfs2_inode_fully_checkpointed(inode
);
3448 BUG_ON(new_level
!= DLM_LOCK_NL
&& new_level
!= DLM_LOCK_PR
);
3449 BUG_ON(lockres
->l_level
!= DLM_LOCK_EX
&& !checkpointed
);
3454 ocfs2_start_checkpoint(OCFS2_SB(inode
->i_sb
));
3458 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res
*lockres
)
3460 struct inode
*inode
= ocfs2_lock_res_inode(lockres
);
3462 __ocfs2_stuff_meta_lvb(inode
);
3466 * Does the final reference drop on our dentry lock. Right now this
3467 * happens in the downconvert thread, but we could choose to simplify the
3468 * dlmglue API and push these off to the ocfs2_wq in the future.
3470 static void ocfs2_dentry_post_unlock(struct ocfs2_super
*osb
,
3471 struct ocfs2_lock_res
*lockres
)
3473 struct ocfs2_dentry_lock
*dl
= ocfs2_lock_res_dl(lockres
);
3474 ocfs2_dentry_lock_put(osb
, dl
);
3478 * d_delete() matching dentries before the lock downconvert.
3480 * At this point, any process waiting to destroy the
3481 * dentry_lock due to last ref count is stopped by the
3482 * OCFS2_LOCK_QUEUED flag.
3484 * We have two potential problems
3486 * 1) If we do the last reference drop on our dentry_lock (via dput)
3487 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3488 * the downconvert to finish. Instead we take an elevated
3489 * reference and push the drop until after we've completed our
3490 * unblock processing.
3492 * 2) There might be another process with a final reference,
3493 * waiting on us to finish processing. If this is the case, we
3494 * detect it and exit out - there's no more dentries anyway.
3496 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res
*lockres
,
3499 struct ocfs2_dentry_lock
*dl
= ocfs2_lock_res_dl(lockres
);
3500 struct ocfs2_inode_info
*oi
= OCFS2_I(dl
->dl_inode
);
3501 struct dentry
*dentry
;
3502 unsigned long flags
;
3506 * This node is blocking another node from getting a read
3507 * lock. This happens when we've renamed within a
3508 * directory. We've forced the other nodes to d_delete(), but
3509 * we never actually dropped our lock because it's still
3510 * valid. The downconvert code will retain a PR for this node,
3511 * so there's no further work to do.
3513 if (blocking
== DLM_LOCK_PR
)
3514 return UNBLOCK_CONTINUE
;
3517 * Mark this inode as potentially orphaned. The code in
3518 * ocfs2_delete_inode() will figure out whether it actually
3519 * needs to be freed or not.
3521 spin_lock(&oi
->ip_lock
);
3522 oi
->ip_flags
|= OCFS2_INODE_MAYBE_ORPHANED
;
3523 spin_unlock(&oi
->ip_lock
);
3526 * Yuck. We need to make sure however that the check of
3527 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3528 * respect to a reference decrement or the setting of that
3531 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3532 spin_lock(&dentry_attach_lock
);
3533 if (!(lockres
->l_flags
& OCFS2_LOCK_FREEING
)
3538 spin_unlock(&dentry_attach_lock
);
3539 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3541 mlog(0, "extra_ref = %d\n", extra_ref
);
3544 * We have a process waiting on us in ocfs2_dentry_iput(),
3545 * which means we can't have any more outstanding
3546 * aliases. There's no need to do any more work.
3549 return UNBLOCK_CONTINUE
;
3551 spin_lock(&dentry_attach_lock
);
3553 dentry
= ocfs2_find_local_alias(dl
->dl_inode
,
3554 dl
->dl_parent_blkno
, 1);
3557 spin_unlock(&dentry_attach_lock
);
3559 mlog(0, "d_delete(%.*s);\n", dentry
->d_name
.len
,
3560 dentry
->d_name
.name
);
3563 * The following dcache calls may do an
3564 * iput(). Normally we don't want that from the
3565 * downconverting thread, but in this case it's ok
3566 * because the requesting node already has an
3567 * exclusive lock on the inode, so it can't be queued
3568 * for a downconvert.
3573 spin_lock(&dentry_attach_lock
);
3575 spin_unlock(&dentry_attach_lock
);
3578 * If we are the last holder of this dentry lock, there is no
3579 * reason to downconvert so skip straight to the unlock.
3581 if (dl
->dl_count
== 1)
3582 return UNBLOCK_STOP_POST
;
3584 return UNBLOCK_CONTINUE_POST
;
3587 static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res
*lockres
)
3589 struct ocfs2_qinfo_lvb
*lvb
;
3590 struct ocfs2_mem_dqinfo
*oinfo
= ocfs2_lock_res_qinfo(lockres
);
3591 struct mem_dqinfo
*info
= sb_dqinfo(oinfo
->dqi_gi
.dqi_sb
,
3592 oinfo
->dqi_gi
.dqi_type
);
3596 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
3597 lvb
->lvb_version
= OCFS2_QINFO_LVB_VERSION
;
3598 lvb
->lvb_bgrace
= cpu_to_be32(info
->dqi_bgrace
);
3599 lvb
->lvb_igrace
= cpu_to_be32(info
->dqi_igrace
);
3600 lvb
->lvb_syncms
= cpu_to_be32(oinfo
->dqi_syncms
);
3601 lvb
->lvb_blocks
= cpu_to_be32(oinfo
->dqi_gi
.dqi_blocks
);
3602 lvb
->lvb_free_blk
= cpu_to_be32(oinfo
->dqi_gi
.dqi_free_blk
);
3603 lvb
->lvb_free_entry
= cpu_to_be32(oinfo
->dqi_gi
.dqi_free_entry
);
3608 void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo
*oinfo
, int ex
)
3610 struct ocfs2_lock_res
*lockres
= &oinfo
->dqi_gqlock
;
3611 struct ocfs2_super
*osb
= OCFS2_SB(oinfo
->dqi_gi
.dqi_sb
);
3612 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
3615 if (!ocfs2_is_hard_readonly(osb
) && !ocfs2_mount_local(osb
))
3616 ocfs2_cluster_unlock(osb
, lockres
, level
);
3620 static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo
*oinfo
)
3622 struct mem_dqinfo
*info
= sb_dqinfo(oinfo
->dqi_gi
.dqi_sb
,
3623 oinfo
->dqi_gi
.dqi_type
);
3624 struct ocfs2_lock_res
*lockres
= &oinfo
->dqi_gqlock
;
3625 struct ocfs2_qinfo_lvb
*lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
3626 struct buffer_head
*bh
= NULL
;
3627 struct ocfs2_global_disk_dqinfo
*gdinfo
;
3630 if (lvb
->lvb_version
== OCFS2_QINFO_LVB_VERSION
) {
3631 info
->dqi_bgrace
= be32_to_cpu(lvb
->lvb_bgrace
);
3632 info
->dqi_igrace
= be32_to_cpu(lvb
->lvb_igrace
);
3633 oinfo
->dqi_syncms
= be32_to_cpu(lvb
->lvb_syncms
);
3634 oinfo
->dqi_gi
.dqi_blocks
= be32_to_cpu(lvb
->lvb_blocks
);
3635 oinfo
->dqi_gi
.dqi_free_blk
= be32_to_cpu(lvb
->lvb_free_blk
);
3636 oinfo
->dqi_gi
.dqi_free_entry
=
3637 be32_to_cpu(lvb
->lvb_free_entry
);
3639 status
= ocfs2_read_quota_block(oinfo
->dqi_gqinode
, 0, &bh
);
3644 gdinfo
= (struct ocfs2_global_disk_dqinfo
*)
3645 (bh
->b_data
+ OCFS2_GLOBAL_INFO_OFF
);
3646 info
->dqi_bgrace
= le32_to_cpu(gdinfo
->dqi_bgrace
);
3647 info
->dqi_igrace
= le32_to_cpu(gdinfo
->dqi_igrace
);
3648 oinfo
->dqi_syncms
= le32_to_cpu(gdinfo
->dqi_syncms
);
3649 oinfo
->dqi_gi
.dqi_blocks
= le32_to_cpu(gdinfo
->dqi_blocks
);
3650 oinfo
->dqi_gi
.dqi_free_blk
= le32_to_cpu(gdinfo
->dqi_free_blk
);
3651 oinfo
->dqi_gi
.dqi_free_entry
=
3652 le32_to_cpu(gdinfo
->dqi_free_entry
);
3654 ocfs2_track_lock_refresh(lockres
);
3661 /* Lock quota info, this function expects at least shared lock on the quota file
3662 * so that we can safely refresh quota info from disk. */
3663 int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo
*oinfo
, int ex
)
3665 struct ocfs2_lock_res
*lockres
= &oinfo
->dqi_gqlock
;
3666 struct ocfs2_super
*osb
= OCFS2_SB(oinfo
->dqi_gi
.dqi_sb
);
3667 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
3672 /* On RO devices, locking really isn't needed... */
3673 if (ocfs2_is_hard_readonly(osb
)) {
3678 if (ocfs2_mount_local(osb
))
3681 status
= ocfs2_cluster_lock(osb
, lockres
, level
, 0, 0);
3686 if (!ocfs2_should_refresh_lock_res(lockres
))
3688 /* OK, we have the lock but we need to refresh the quota info */
3689 status
= ocfs2_refresh_qinfo(oinfo
);
3691 ocfs2_qinfo_unlock(oinfo
, ex
);
3692 ocfs2_complete_lock_res_refresh(lockres
, status
);
3699 * This is the filesystem locking protocol. It provides the lock handling
3700 * hooks for the underlying DLM. It has a maximum version number.
3701 * The version number allows interoperability with systems running at
3702 * the same major number and an equal or smaller minor number.
3704 * Whenever the filesystem does new things with locks (adds or removes a
3705 * lock, orders them differently, does different things underneath a lock),
3706 * the version must be changed. The protocol is negotiated when joining
3707 * the dlm domain. A node may join the domain if its major version is
3708 * identical to all other nodes and its minor version is greater than
3709 * or equal to all other nodes. When its minor version is greater than
3710 * the other nodes, it will run at the minor version specified by the
3713 * If a locking change is made that will not be compatible with older
3714 * versions, the major number must be increased and the minor version set
3715 * to zero. If a change merely adds a behavior that can be disabled when
3716 * speaking to older versions, the minor version must be increased. If a
3717 * change adds a fully backwards compatible change (eg, LVB changes that
3718 * are just ignored by older versions), the version does not need to be
3721 static struct ocfs2_locking_protocol lproto
= {
3723 .pv_major
= OCFS2_LOCKING_PROTOCOL_MAJOR
,
3724 .pv_minor
= OCFS2_LOCKING_PROTOCOL_MINOR
,
3726 .lp_lock_ast
= ocfs2_locking_ast
,
3727 .lp_blocking_ast
= ocfs2_blocking_ast
,
3728 .lp_unlock_ast
= ocfs2_unlock_ast
,
3731 void ocfs2_set_locking_protocol(void)
3733 ocfs2_stack_glue_set_locking_protocol(&lproto
);
3737 static void ocfs2_process_blocked_lock(struct ocfs2_super
*osb
,
3738 struct ocfs2_lock_res
*lockres
)
3741 struct ocfs2_unblock_ctl ctl
= {0, 0,};
3742 unsigned long flags
;
3744 /* Our reference to the lockres in this function can be
3745 * considered valid until we remove the OCFS2_LOCK_QUEUED
3751 BUG_ON(!lockres
->l_ops
);
3753 mlog(0, "lockres %s blocked.\n", lockres
->l_name
);
3755 /* Detect whether a lock has been marked as going away while
3756 * the downconvert thread was processing other things. A lock can
3757 * still be marked with OCFS2_LOCK_FREEING after this check,
3758 * but short circuiting here will still save us some
3760 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3761 if (lockres
->l_flags
& OCFS2_LOCK_FREEING
)
3763 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3765 status
= ocfs2_unblock_lock(osb
, lockres
, &ctl
);
3769 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3771 if (lockres
->l_flags
& OCFS2_LOCK_FREEING
|| !ctl
.requeue
) {
3772 lockres_clear_flags(lockres
, OCFS2_LOCK_QUEUED
);
3774 ocfs2_schedule_blocked_lock(osb
, lockres
);
3776 mlog(0, "lockres %s, requeue = %s.\n", lockres
->l_name
,
3777 ctl
.requeue
? "yes" : "no");
3778 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3780 if (ctl
.unblock_action
!= UNBLOCK_CONTINUE
3781 && lockres
->l_ops
->post_unlock
)
3782 lockres
->l_ops
->post_unlock(osb
, lockres
);
3787 static void ocfs2_schedule_blocked_lock(struct ocfs2_super
*osb
,
3788 struct ocfs2_lock_res
*lockres
)
3792 assert_spin_locked(&lockres
->l_lock
);
3794 if (lockres
->l_flags
& OCFS2_LOCK_FREEING
) {
3795 /* Do not schedule a lock for downconvert when it's on
3796 * the way to destruction - any nodes wanting access
3797 * to the resource will get it soon. */
3798 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3799 lockres
->l_name
, lockres
->l_flags
);
3803 lockres_or_flags(lockres
, OCFS2_LOCK_QUEUED
);
3805 spin_lock(&osb
->dc_task_lock
);
3806 if (list_empty(&lockres
->l_blocked_list
)) {
3807 list_add_tail(&lockres
->l_blocked_list
,
3808 &osb
->blocked_lock_list
);
3809 osb
->blocked_lock_count
++;
3811 spin_unlock(&osb
->dc_task_lock
);
3816 static void ocfs2_downconvert_thread_do_work(struct ocfs2_super
*osb
)
3818 unsigned long processed
;
3819 struct ocfs2_lock_res
*lockres
;
3823 spin_lock(&osb
->dc_task_lock
);
3824 /* grab this early so we know to try again if a state change and
3825 * wake happens part-way through our work */
3826 osb
->dc_work_sequence
= osb
->dc_wake_sequence
;
3828 processed
= osb
->blocked_lock_count
;
3830 BUG_ON(list_empty(&osb
->blocked_lock_list
));
3832 lockres
= list_entry(osb
->blocked_lock_list
.next
,
3833 struct ocfs2_lock_res
, l_blocked_list
);
3834 list_del_init(&lockres
->l_blocked_list
);
3835 osb
->blocked_lock_count
--;
3836 spin_unlock(&osb
->dc_task_lock
);
3841 ocfs2_process_blocked_lock(osb
, lockres
);
3843 spin_lock(&osb
->dc_task_lock
);
3845 spin_unlock(&osb
->dc_task_lock
);
3850 static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super
*osb
)
3854 spin_lock(&osb
->dc_task_lock
);
3855 if (list_empty(&osb
->blocked_lock_list
))
3858 spin_unlock(&osb
->dc_task_lock
);
3862 static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super
*osb
)
3864 int should_wake
= 0;
3866 spin_lock(&osb
->dc_task_lock
);
3867 if (osb
->dc_work_sequence
!= osb
->dc_wake_sequence
)
3869 spin_unlock(&osb
->dc_task_lock
);
3874 static int ocfs2_downconvert_thread(void *arg
)
3877 struct ocfs2_super
*osb
= arg
;
3879 /* only quit once we've been asked to stop and there is no more
3881 while (!(kthread_should_stop() &&
3882 ocfs2_downconvert_thread_lists_empty(osb
))) {
3884 wait_event_interruptible(osb
->dc_event
,
3885 ocfs2_downconvert_thread_should_wake(osb
) ||
3886 kthread_should_stop());
3888 mlog(0, "downconvert_thread: awoken\n");
3890 ocfs2_downconvert_thread_do_work(osb
);
3893 osb
->dc_task
= NULL
;
3897 void ocfs2_wake_downconvert_thread(struct ocfs2_super
*osb
)
3899 spin_lock(&osb
->dc_task_lock
);
3900 /* make sure the voting thread gets a swipe at whatever changes
3901 * the caller may have made to the voting state */
3902 osb
->dc_wake_sequence
++;
3903 spin_unlock(&osb
->dc_task_lock
);
3904 wake_up(&osb
->dc_event
);