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_dentry_lops
= {
248 .get_osb
= ocfs2_get_dentry_osb
,
249 .post_unlock
= ocfs2_dentry_post_unlock
,
250 .downconvert_worker
= ocfs2_dentry_convert_worker
,
254 static struct ocfs2_lock_res_ops ocfs2_inode_open_lops
= {
255 .get_osb
= ocfs2_get_inode_osb
,
259 static struct ocfs2_lock_res_ops ocfs2_flock_lops
= {
260 .get_osb
= ocfs2_get_file_osb
,
264 static struct ocfs2_lock_res_ops ocfs2_qinfo_lops
= {
265 .set_lvb
= ocfs2_set_qinfo_lvb
,
266 .get_osb
= ocfs2_get_qinfo_osb
,
267 .flags
= LOCK_TYPE_REQUIRES_REFRESH
| LOCK_TYPE_USES_LVB
,
270 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res
*lockres
)
272 return lockres
->l_type
== OCFS2_LOCK_TYPE_META
||
273 lockres
->l_type
== OCFS2_LOCK_TYPE_RW
||
274 lockres
->l_type
== OCFS2_LOCK_TYPE_OPEN
;
277 static inline struct inode
*ocfs2_lock_res_inode(struct ocfs2_lock_res
*lockres
)
279 BUG_ON(!ocfs2_is_inode_lock(lockres
));
281 return (struct inode
*) lockres
->l_priv
;
284 static inline struct ocfs2_dentry_lock
*ocfs2_lock_res_dl(struct ocfs2_lock_res
*lockres
)
286 BUG_ON(lockres
->l_type
!= OCFS2_LOCK_TYPE_DENTRY
);
288 return (struct ocfs2_dentry_lock
*)lockres
->l_priv
;
291 static inline struct ocfs2_mem_dqinfo
*ocfs2_lock_res_qinfo(struct ocfs2_lock_res
*lockres
)
293 BUG_ON(lockres
->l_type
!= OCFS2_LOCK_TYPE_QINFO
);
295 return (struct ocfs2_mem_dqinfo
*)lockres
->l_priv
;
298 static inline struct ocfs2_super
*ocfs2_get_lockres_osb(struct ocfs2_lock_res
*lockres
)
300 if (lockres
->l_ops
->get_osb
)
301 return lockres
->l_ops
->get_osb(lockres
);
303 return (struct ocfs2_super
*)lockres
->l_priv
;
306 static int ocfs2_lock_create(struct ocfs2_super
*osb
,
307 struct ocfs2_lock_res
*lockres
,
310 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res
*lockres
,
312 static void ocfs2_cluster_unlock(struct ocfs2_super
*osb
,
313 struct ocfs2_lock_res
*lockres
,
315 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res
*lockres
);
316 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res
*lockres
);
317 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res
*lockres
);
318 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res
*lockres
, int level
);
319 static void ocfs2_schedule_blocked_lock(struct ocfs2_super
*osb
,
320 struct ocfs2_lock_res
*lockres
);
321 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res
*lockres
,
323 #define ocfs2_log_dlm_error(_func, _err, _lockres) do { \
324 if ((_lockres)->l_type != OCFS2_LOCK_TYPE_DENTRY) \
325 mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \
326 _err, _func, _lockres->l_name); \
328 mlog(ML_ERROR, "DLM error %d while calling %s on resource %.*s%08x\n", \
329 _err, _func, OCFS2_DENTRY_LOCK_INO_START - 1, (_lockres)->l_name, \
330 (unsigned int)ocfs2_get_dentry_lock_ino(_lockres)); \
332 static int ocfs2_downconvert_thread(void *arg
);
333 static void ocfs2_downconvert_on_unlock(struct ocfs2_super
*osb
,
334 struct ocfs2_lock_res
*lockres
);
335 static int ocfs2_inode_lock_update(struct inode
*inode
,
336 struct buffer_head
**bh
);
337 static void ocfs2_drop_osb_locks(struct ocfs2_super
*osb
);
338 static inline int ocfs2_highest_compat_lock_level(int level
);
339 static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res
*lockres
,
341 static int ocfs2_downconvert_lock(struct ocfs2_super
*osb
,
342 struct ocfs2_lock_res
*lockres
,
345 unsigned int generation
);
346 static int ocfs2_prepare_cancel_convert(struct ocfs2_super
*osb
,
347 struct ocfs2_lock_res
*lockres
);
348 static int ocfs2_cancel_convert(struct ocfs2_super
*osb
,
349 struct ocfs2_lock_res
*lockres
);
352 static void ocfs2_build_lock_name(enum ocfs2_lock_type type
,
361 BUG_ON(type
>= OCFS2_NUM_LOCK_TYPES
);
363 len
= snprintf(name
, OCFS2_LOCK_ID_MAX_LEN
, "%c%s%016llx%08x",
364 ocfs2_lock_type_char(type
), OCFS2_LOCK_ID_PAD
,
365 (long long)blkno
, generation
);
367 BUG_ON(len
!= (OCFS2_LOCK_ID_MAX_LEN
- 1));
369 mlog(0, "built lock resource with name: %s\n", name
);
374 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock
);
376 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res
*res
,
377 struct ocfs2_dlm_debug
*dlm_debug
)
379 mlog(0, "Add tracking for lockres %s\n", res
->l_name
);
381 spin_lock(&ocfs2_dlm_tracking_lock
);
382 list_add(&res
->l_debug_list
, &dlm_debug
->d_lockres_tracking
);
383 spin_unlock(&ocfs2_dlm_tracking_lock
);
386 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res
*res
)
388 spin_lock(&ocfs2_dlm_tracking_lock
);
389 if (!list_empty(&res
->l_debug_list
))
390 list_del_init(&res
->l_debug_list
);
391 spin_unlock(&ocfs2_dlm_tracking_lock
);
394 #ifdef CONFIG_OCFS2_FS_STATS
395 static void ocfs2_init_lock_stats(struct ocfs2_lock_res
*res
)
397 res
->l_lock_num_prmode
= 0;
398 res
->l_lock_num_prmode_failed
= 0;
399 res
->l_lock_total_prmode
= 0;
400 res
->l_lock_max_prmode
= 0;
401 res
->l_lock_num_exmode
= 0;
402 res
->l_lock_num_exmode_failed
= 0;
403 res
->l_lock_total_exmode
= 0;
404 res
->l_lock_max_exmode
= 0;
405 res
->l_lock_refresh
= 0;
408 static void ocfs2_update_lock_stats(struct ocfs2_lock_res
*res
, int level
,
409 struct ocfs2_mask_waiter
*mw
, int ret
)
411 unsigned long long *num
, *sum
;
412 unsigned int *max
, *failed
;
413 struct timespec ts
= current_kernel_time();
414 unsigned long long time
= timespec_to_ns(&ts
) - mw
->mw_lock_start
;
416 if (level
== LKM_PRMODE
) {
417 num
= &res
->l_lock_num_prmode
;
418 sum
= &res
->l_lock_total_prmode
;
419 max
= &res
->l_lock_max_prmode
;
420 failed
= &res
->l_lock_num_prmode_failed
;
421 } else if (level
== LKM_EXMODE
) {
422 num
= &res
->l_lock_num_exmode
;
423 sum
= &res
->l_lock_total_exmode
;
424 max
= &res
->l_lock_max_exmode
;
425 failed
= &res
->l_lock_num_exmode_failed
;
437 static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res
*lockres
)
439 lockres
->l_lock_refresh
++;
442 static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter
*mw
)
444 struct timespec ts
= current_kernel_time();
445 mw
->mw_lock_start
= timespec_to_ns(&ts
);
448 static inline void ocfs2_init_lock_stats(struct ocfs2_lock_res
*res
)
451 static inline void ocfs2_update_lock_stats(struct ocfs2_lock_res
*res
,
452 int level
, struct ocfs2_mask_waiter
*mw
, int ret
)
455 static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res
*lockres
)
458 static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter
*mw
)
463 static void ocfs2_lock_res_init_common(struct ocfs2_super
*osb
,
464 struct ocfs2_lock_res
*res
,
465 enum ocfs2_lock_type type
,
466 struct ocfs2_lock_res_ops
*ops
,
473 res
->l_level
= DLM_LOCK_IV
;
474 res
->l_requested
= DLM_LOCK_IV
;
475 res
->l_blocking
= DLM_LOCK_IV
;
476 res
->l_action
= OCFS2_AST_INVALID
;
477 res
->l_unlock_action
= OCFS2_UNLOCK_INVALID
;
479 res
->l_flags
= OCFS2_LOCK_INITIALIZED
;
481 ocfs2_add_lockres_tracking(res
, osb
->osb_dlm_debug
);
483 ocfs2_init_lock_stats(res
);
486 void ocfs2_lock_res_init_once(struct ocfs2_lock_res
*res
)
488 /* This also clears out the lock status block */
489 memset(res
, 0, sizeof(struct ocfs2_lock_res
));
490 spin_lock_init(&res
->l_lock
);
491 init_waitqueue_head(&res
->l_event
);
492 INIT_LIST_HEAD(&res
->l_blocked_list
);
493 INIT_LIST_HEAD(&res
->l_mask_waiters
);
496 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res
*res
,
497 enum ocfs2_lock_type type
,
498 unsigned int generation
,
501 struct ocfs2_lock_res_ops
*ops
;
504 case OCFS2_LOCK_TYPE_RW
:
505 ops
= &ocfs2_inode_rw_lops
;
507 case OCFS2_LOCK_TYPE_META
:
508 ops
= &ocfs2_inode_inode_lops
;
510 case OCFS2_LOCK_TYPE_OPEN
:
511 ops
= &ocfs2_inode_open_lops
;
514 mlog_bug_on_msg(1, "type: %d\n", type
);
515 ops
= NULL
; /* thanks, gcc */
519 ocfs2_build_lock_name(type
, OCFS2_I(inode
)->ip_blkno
,
520 generation
, res
->l_name
);
521 ocfs2_lock_res_init_common(OCFS2_SB(inode
->i_sb
), res
, type
, ops
, inode
);
524 static struct ocfs2_super
*ocfs2_get_inode_osb(struct ocfs2_lock_res
*lockres
)
526 struct inode
*inode
= ocfs2_lock_res_inode(lockres
);
528 return OCFS2_SB(inode
->i_sb
);
531 static struct ocfs2_super
*ocfs2_get_qinfo_osb(struct ocfs2_lock_res
*lockres
)
533 struct ocfs2_mem_dqinfo
*info
= lockres
->l_priv
;
535 return OCFS2_SB(info
->dqi_gi
.dqi_sb
);
538 static struct ocfs2_super
*ocfs2_get_file_osb(struct ocfs2_lock_res
*lockres
)
540 struct ocfs2_file_private
*fp
= lockres
->l_priv
;
542 return OCFS2_SB(fp
->fp_file
->f_mapping
->host
->i_sb
);
545 static __u64
ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res
*lockres
)
547 __be64 inode_blkno_be
;
549 memcpy(&inode_blkno_be
, &lockres
->l_name
[OCFS2_DENTRY_LOCK_INO_START
],
552 return be64_to_cpu(inode_blkno_be
);
555 static struct ocfs2_super
*ocfs2_get_dentry_osb(struct ocfs2_lock_res
*lockres
)
557 struct ocfs2_dentry_lock
*dl
= lockres
->l_priv
;
559 return OCFS2_SB(dl
->dl_inode
->i_sb
);
562 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock
*dl
,
563 u64 parent
, struct inode
*inode
)
566 u64 inode_blkno
= OCFS2_I(inode
)->ip_blkno
;
567 __be64 inode_blkno_be
= cpu_to_be64(inode_blkno
);
568 struct ocfs2_lock_res
*lockres
= &dl
->dl_lockres
;
570 ocfs2_lock_res_init_once(lockres
);
573 * Unfortunately, the standard lock naming scheme won't work
574 * here because we have two 16 byte values to use. Instead,
575 * we'll stuff the inode number as a binary value. We still
576 * want error prints to show something without garbling the
577 * display, so drop a null byte in there before the inode
578 * number. A future version of OCFS2 will likely use all
579 * binary lock names. The stringified names have been a
580 * tremendous aid in debugging, but now that the debugfs
581 * interface exists, we can mangle things there if need be.
583 * NOTE: We also drop the standard "pad" value (the total lock
584 * name size stays the same though - the last part is all
585 * zeros due to the memset in ocfs2_lock_res_init_once()
587 len
= snprintf(lockres
->l_name
, OCFS2_DENTRY_LOCK_INO_START
,
589 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY
),
592 BUG_ON(len
!= (OCFS2_DENTRY_LOCK_INO_START
- 1));
594 memcpy(&lockres
->l_name
[OCFS2_DENTRY_LOCK_INO_START
], &inode_blkno_be
,
597 ocfs2_lock_res_init_common(OCFS2_SB(inode
->i_sb
), lockres
,
598 OCFS2_LOCK_TYPE_DENTRY
, &ocfs2_dentry_lops
,
602 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res
*res
,
603 struct ocfs2_super
*osb
)
605 /* Superblock lockres doesn't come from a slab so we call init
606 * once on it manually. */
607 ocfs2_lock_res_init_once(res
);
608 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER
, OCFS2_SUPER_BLOCK_BLKNO
,
610 ocfs2_lock_res_init_common(osb
, res
, OCFS2_LOCK_TYPE_SUPER
,
611 &ocfs2_super_lops
, osb
);
614 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res
*res
,
615 struct ocfs2_super
*osb
)
617 /* Rename lockres doesn't come from a slab so we call init
618 * once on it manually. */
619 ocfs2_lock_res_init_once(res
);
620 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME
, 0, 0, res
->l_name
);
621 ocfs2_lock_res_init_common(osb
, res
, OCFS2_LOCK_TYPE_RENAME
,
622 &ocfs2_rename_lops
, osb
);
625 void ocfs2_file_lock_res_init(struct ocfs2_lock_res
*lockres
,
626 struct ocfs2_file_private
*fp
)
628 struct inode
*inode
= fp
->fp_file
->f_mapping
->host
;
629 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
631 ocfs2_lock_res_init_once(lockres
);
632 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK
, oi
->ip_blkno
,
633 inode
->i_generation
, lockres
->l_name
);
634 ocfs2_lock_res_init_common(OCFS2_SB(inode
->i_sb
), lockres
,
635 OCFS2_LOCK_TYPE_FLOCK
, &ocfs2_flock_lops
,
637 lockres
->l_flags
|= OCFS2_LOCK_NOCACHE
;
640 void ocfs2_qinfo_lock_res_init(struct ocfs2_lock_res
*lockres
,
641 struct ocfs2_mem_dqinfo
*info
)
643 ocfs2_lock_res_init_once(lockres
);
644 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_QINFO
, info
->dqi_gi
.dqi_type
,
646 ocfs2_lock_res_init_common(OCFS2_SB(info
->dqi_gi
.dqi_sb
), lockres
,
647 OCFS2_LOCK_TYPE_QINFO
, &ocfs2_qinfo_lops
,
651 void ocfs2_lock_res_free(struct ocfs2_lock_res
*res
)
655 if (!(res
->l_flags
& OCFS2_LOCK_INITIALIZED
))
658 ocfs2_remove_lockres_tracking(res
);
660 mlog_bug_on_msg(!list_empty(&res
->l_blocked_list
),
661 "Lockres %s is on the blocked list\n",
663 mlog_bug_on_msg(!list_empty(&res
->l_mask_waiters
),
664 "Lockres %s has mask waiters pending\n",
666 mlog_bug_on_msg(spin_is_locked(&res
->l_lock
),
667 "Lockres %s is locked\n",
669 mlog_bug_on_msg(res
->l_ro_holders
,
670 "Lockres %s has %u ro holders\n",
671 res
->l_name
, res
->l_ro_holders
);
672 mlog_bug_on_msg(res
->l_ex_holders
,
673 "Lockres %s has %u ex holders\n",
674 res
->l_name
, res
->l_ex_holders
);
676 /* Need to clear out the lock status block for the dlm */
677 memset(&res
->l_lksb
, 0, sizeof(res
->l_lksb
));
683 static inline void ocfs2_inc_holders(struct ocfs2_lock_res
*lockres
,
692 lockres
->l_ex_holders
++;
695 lockres
->l_ro_holders
++;
704 static inline void ocfs2_dec_holders(struct ocfs2_lock_res
*lockres
,
713 BUG_ON(!lockres
->l_ex_holders
);
714 lockres
->l_ex_holders
--;
717 BUG_ON(!lockres
->l_ro_holders
);
718 lockres
->l_ro_holders
--;
726 /* WARNING: This function lives in a world where the only three lock
727 * levels are EX, PR, and NL. It *will* have to be adjusted when more
728 * lock types are added. */
729 static inline int ocfs2_highest_compat_lock_level(int level
)
731 int new_level
= DLM_LOCK_EX
;
733 if (level
== DLM_LOCK_EX
)
734 new_level
= DLM_LOCK_NL
;
735 else if (level
== DLM_LOCK_PR
)
736 new_level
= DLM_LOCK_PR
;
740 static void lockres_set_flags(struct ocfs2_lock_res
*lockres
,
741 unsigned long newflags
)
743 struct ocfs2_mask_waiter
*mw
, *tmp
;
745 assert_spin_locked(&lockres
->l_lock
);
747 lockres
->l_flags
= newflags
;
749 list_for_each_entry_safe(mw
, tmp
, &lockres
->l_mask_waiters
, mw_item
) {
750 if ((lockres
->l_flags
& mw
->mw_mask
) != mw
->mw_goal
)
753 list_del_init(&mw
->mw_item
);
755 complete(&mw
->mw_complete
);
758 static void lockres_or_flags(struct ocfs2_lock_res
*lockres
, unsigned long or)
760 lockres_set_flags(lockres
, lockres
->l_flags
| or);
762 static void lockres_clear_flags(struct ocfs2_lock_res
*lockres
,
765 lockres_set_flags(lockres
, lockres
->l_flags
& ~clear
);
768 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res
*lockres
)
772 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BUSY
));
773 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
));
774 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BLOCKED
));
775 BUG_ON(lockres
->l_blocking
<= DLM_LOCK_NL
);
777 lockres
->l_level
= lockres
->l_requested
;
778 if (lockres
->l_level
<=
779 ocfs2_highest_compat_lock_level(lockres
->l_blocking
)) {
780 lockres
->l_blocking
= DLM_LOCK_NL
;
781 lockres_clear_flags(lockres
, OCFS2_LOCK_BLOCKED
);
783 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
788 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res
*lockres
)
792 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BUSY
));
793 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
));
795 /* Convert from RO to EX doesn't really need anything as our
796 * information is already up to data. Convert from NL to
797 * *anything* however should mark ourselves as needing an
799 if (lockres
->l_level
== DLM_LOCK_NL
&&
800 lockres
->l_ops
->flags
& LOCK_TYPE_REQUIRES_REFRESH
)
801 lockres_or_flags(lockres
, OCFS2_LOCK_NEEDS_REFRESH
);
803 lockres
->l_level
= lockres
->l_requested
;
804 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
809 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res
*lockres
)
813 BUG_ON((!(lockres
->l_flags
& OCFS2_LOCK_BUSY
)));
814 BUG_ON(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
);
816 if (lockres
->l_requested
> DLM_LOCK_NL
&&
817 !(lockres
->l_flags
& OCFS2_LOCK_LOCAL
) &&
818 lockres
->l_ops
->flags
& LOCK_TYPE_REQUIRES_REFRESH
)
819 lockres_or_flags(lockres
, OCFS2_LOCK_NEEDS_REFRESH
);
821 lockres
->l_level
= lockres
->l_requested
;
822 lockres_or_flags(lockres
, OCFS2_LOCK_ATTACHED
);
823 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
828 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res
*lockres
,
831 int needs_downconvert
= 0;
834 assert_spin_locked(&lockres
->l_lock
);
836 lockres_or_flags(lockres
, OCFS2_LOCK_BLOCKED
);
838 if (level
> lockres
->l_blocking
) {
839 /* only schedule a downconvert if we haven't already scheduled
840 * one that goes low enough to satisfy the level we're
841 * blocking. this also catches the case where we get
843 if (ocfs2_highest_compat_lock_level(level
) <
844 ocfs2_highest_compat_lock_level(lockres
->l_blocking
))
845 needs_downconvert
= 1;
847 lockres
->l_blocking
= level
;
850 mlog_exit(needs_downconvert
);
851 return needs_downconvert
;
855 * OCFS2_LOCK_PENDING and l_pending_gen.
857 * Why does OCFS2_LOCK_PENDING exist? To close a race between setting
858 * OCFS2_LOCK_BUSY and calling ocfs2_dlm_lock(). See ocfs2_unblock_lock()
859 * for more details on the race.
861 * OCFS2_LOCK_PENDING closes the race quite nicely. However, it introduces
862 * a race on itself. In o2dlm, we can get the ast before ocfs2_dlm_lock()
863 * returns. The ast clears OCFS2_LOCK_BUSY, and must therefore clear
864 * OCFS2_LOCK_PENDING at the same time. When ocfs2_dlm_lock() returns,
865 * the caller is going to try to clear PENDING again. If nothing else is
866 * happening, __lockres_clear_pending() sees PENDING is unset and does
869 * But what if another path (eg downconvert thread) has just started a
870 * new locking action? The other path has re-set PENDING. Our path
871 * cannot clear PENDING, because that will re-open the original race
877 * ocfs2_cluster_lock()
882 * ocfs2_locking_ast() ocfs2_downconvert_thread()
883 * clear PENDING ocfs2_unblock_lock()
886 * ocfs2_prepare_downconvert()
896 * So as you can see, we now have a window where l_lock is not held,
897 * PENDING is not set, and ocfs2_dlm_lock() has not been called.
899 * The core problem is that ocfs2_cluster_lock() has cleared the PENDING
900 * set by ocfs2_prepare_downconvert(). That wasn't nice.
902 * To solve this we introduce l_pending_gen. A call to
903 * lockres_clear_pending() will only do so when it is passed a generation
904 * number that matches the lockres. lockres_set_pending() will return the
905 * current generation number. When ocfs2_cluster_lock() goes to clear
906 * PENDING, it passes the generation it got from set_pending(). In our
907 * example above, the generation numbers will *not* match. Thus,
908 * ocfs2_cluster_lock() will not clear the PENDING set by
909 * ocfs2_prepare_downconvert().
912 /* Unlocked version for ocfs2_locking_ast() */
913 static void __lockres_clear_pending(struct ocfs2_lock_res
*lockres
,
914 unsigned int generation
,
915 struct ocfs2_super
*osb
)
917 assert_spin_locked(&lockres
->l_lock
);
920 * The ast and locking functions can race us here. The winner
921 * will clear pending, the loser will not.
923 if (!(lockres
->l_flags
& OCFS2_LOCK_PENDING
) ||
924 (lockres
->l_pending_gen
!= generation
))
927 lockres_clear_flags(lockres
, OCFS2_LOCK_PENDING
);
928 lockres
->l_pending_gen
++;
931 * The downconvert thread may have skipped us because we
932 * were PENDING. Wake it up.
934 if (lockres
->l_flags
& OCFS2_LOCK_BLOCKED
)
935 ocfs2_wake_downconvert_thread(osb
);
938 /* Locked version for callers of ocfs2_dlm_lock() */
939 static void lockres_clear_pending(struct ocfs2_lock_res
*lockres
,
940 unsigned int generation
,
941 struct ocfs2_super
*osb
)
945 spin_lock_irqsave(&lockres
->l_lock
, flags
);
946 __lockres_clear_pending(lockres
, generation
, osb
);
947 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
950 static unsigned int lockres_set_pending(struct ocfs2_lock_res
*lockres
)
952 assert_spin_locked(&lockres
->l_lock
);
953 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BUSY
));
955 lockres_or_flags(lockres
, OCFS2_LOCK_PENDING
);
957 return lockres
->l_pending_gen
;
961 static void ocfs2_blocking_ast(void *opaque
, int level
)
963 struct ocfs2_lock_res
*lockres
= opaque
;
964 struct ocfs2_super
*osb
= ocfs2_get_lockres_osb(lockres
);
965 int needs_downconvert
;
968 BUG_ON(level
<= DLM_LOCK_NL
);
970 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
971 lockres
->l_name
, level
, lockres
->l_level
,
972 ocfs2_lock_type_string(lockres
->l_type
));
975 * We can skip the bast for locks which don't enable caching -
976 * they'll be dropped at the earliest possible time anyway.
978 if (lockres
->l_flags
& OCFS2_LOCK_NOCACHE
)
981 spin_lock_irqsave(&lockres
->l_lock
, flags
);
982 needs_downconvert
= ocfs2_generic_handle_bast(lockres
, level
);
983 if (needs_downconvert
)
984 ocfs2_schedule_blocked_lock(osb
, lockres
);
985 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
987 wake_up(&lockres
->l_event
);
989 ocfs2_wake_downconvert_thread(osb
);
992 static void ocfs2_locking_ast(void *opaque
)
994 struct ocfs2_lock_res
*lockres
= opaque
;
995 struct ocfs2_super
*osb
= ocfs2_get_lockres_osb(lockres
);
999 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1001 status
= ocfs2_dlm_lock_status(&lockres
->l_lksb
);
1003 if (status
== -EAGAIN
) {
1004 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
1009 mlog(ML_ERROR
, "lockres %s: lksb status value of %d!\n",
1010 lockres
->l_name
, status
);
1011 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1015 switch(lockres
->l_action
) {
1016 case OCFS2_AST_ATTACH
:
1017 ocfs2_generic_handle_attach_action(lockres
);
1018 lockres_clear_flags(lockres
, OCFS2_LOCK_LOCAL
);
1020 case OCFS2_AST_CONVERT
:
1021 ocfs2_generic_handle_convert_action(lockres
);
1023 case OCFS2_AST_DOWNCONVERT
:
1024 ocfs2_generic_handle_downconvert_action(lockres
);
1027 mlog(ML_ERROR
, "lockres %s: ast fired with invalid action: %u "
1028 "lockres flags = 0x%lx, unlock action: %u\n",
1029 lockres
->l_name
, lockres
->l_action
, lockres
->l_flags
,
1030 lockres
->l_unlock_action
);
1034 /* set it to something invalid so if we get called again we
1036 lockres
->l_action
= OCFS2_AST_INVALID
;
1038 /* Did we try to cancel this lock? Clear that state */
1039 if (lockres
->l_unlock_action
== OCFS2_UNLOCK_CANCEL_CONVERT
)
1040 lockres
->l_unlock_action
= OCFS2_UNLOCK_INVALID
;
1043 * We may have beaten the locking functions here. We certainly
1044 * know that dlm_lock() has been called :-)
1045 * Because we can't have two lock calls in flight at once, we
1046 * can use lockres->l_pending_gen.
1048 __lockres_clear_pending(lockres
, lockres
->l_pending_gen
, osb
);
1050 wake_up(&lockres
->l_event
);
1051 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1054 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res
*lockres
,
1057 unsigned long flags
;
1060 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1061 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
1063 lockres
->l_action
= OCFS2_AST_INVALID
;
1065 lockres
->l_unlock_action
= OCFS2_UNLOCK_INVALID
;
1066 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1068 wake_up(&lockres
->l_event
);
1072 /* Note: If we detect another process working on the lock (i.e.,
1073 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
1074 * to do the right thing in that case.
1076 static int ocfs2_lock_create(struct ocfs2_super
*osb
,
1077 struct ocfs2_lock_res
*lockres
,
1082 unsigned long flags
;
1087 mlog(0, "lock %s, level = %d, flags = %u\n", lockres
->l_name
, level
,
1090 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1091 if ((lockres
->l_flags
& OCFS2_LOCK_ATTACHED
) ||
1092 (lockres
->l_flags
& OCFS2_LOCK_BUSY
)) {
1093 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1097 lockres
->l_action
= OCFS2_AST_ATTACH
;
1098 lockres
->l_requested
= level
;
1099 lockres_or_flags(lockres
, OCFS2_LOCK_BUSY
);
1100 gen
= lockres_set_pending(lockres
);
1101 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1103 ret
= ocfs2_dlm_lock(osb
->cconn
,
1108 OCFS2_LOCK_ID_MAX_LEN
- 1,
1110 lockres_clear_pending(lockres
, gen
, osb
);
1112 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret
, lockres
);
1113 ocfs2_recover_from_dlm_error(lockres
, 1);
1116 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres
->l_name
);
1123 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res
*lockres
,
1126 unsigned long flags
;
1129 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1130 ret
= lockres
->l_flags
& flag
;
1131 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1136 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res
*lockres
)
1139 wait_event(lockres
->l_event
,
1140 !ocfs2_check_wait_flag(lockres
, OCFS2_LOCK_BUSY
));
1143 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res
*lockres
)
1146 wait_event(lockres
->l_event
,
1147 !ocfs2_check_wait_flag(lockres
, OCFS2_LOCK_REFRESHING
));
1150 /* predict what lock level we'll be dropping down to on behalf
1151 * of another node, and return true if the currently wanted
1152 * level will be compatible with it. */
1153 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res
*lockres
,
1156 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BLOCKED
));
1158 return wanted
<= ocfs2_highest_compat_lock_level(lockres
->l_blocking
);
1161 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter
*mw
)
1163 INIT_LIST_HEAD(&mw
->mw_item
);
1164 init_completion(&mw
->mw_complete
);
1165 ocfs2_init_start_time(mw
);
1168 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter
*mw
)
1170 wait_for_completion(&mw
->mw_complete
);
1171 /* Re-arm the completion in case we want to wait on it again */
1172 INIT_COMPLETION(mw
->mw_complete
);
1173 return mw
->mw_status
;
1176 static void lockres_add_mask_waiter(struct ocfs2_lock_res
*lockres
,
1177 struct ocfs2_mask_waiter
*mw
,
1181 BUG_ON(!list_empty(&mw
->mw_item
));
1183 assert_spin_locked(&lockres
->l_lock
);
1185 list_add_tail(&mw
->mw_item
, &lockres
->l_mask_waiters
);
1190 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
1191 * if the mask still hadn't reached its goal */
1192 static int lockres_remove_mask_waiter(struct ocfs2_lock_res
*lockres
,
1193 struct ocfs2_mask_waiter
*mw
)
1195 unsigned long flags
;
1198 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1199 if (!list_empty(&mw
->mw_item
)) {
1200 if ((lockres
->l_flags
& mw
->mw_mask
) != mw
->mw_goal
)
1203 list_del_init(&mw
->mw_item
);
1204 init_completion(&mw
->mw_complete
);
1206 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1212 static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter
*mw
,
1213 struct ocfs2_lock_res
*lockres
)
1217 ret
= wait_for_completion_interruptible(&mw
->mw_complete
);
1219 lockres_remove_mask_waiter(lockres
, mw
);
1221 ret
= mw
->mw_status
;
1222 /* Re-arm the completion in case we want to wait on it again */
1223 INIT_COMPLETION(mw
->mw_complete
);
1227 static int ocfs2_cluster_lock(struct ocfs2_super
*osb
,
1228 struct ocfs2_lock_res
*lockres
,
1233 struct ocfs2_mask_waiter mw
;
1234 int wait
, catch_signals
= !(osb
->s_mount_opt
& OCFS2_MOUNT_NOINTR
);
1235 int ret
= 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1236 unsigned long flags
;
1238 int noqueue_attempted
= 0;
1242 ocfs2_init_mask_waiter(&mw
);
1244 if (lockres
->l_ops
->flags
& LOCK_TYPE_USES_LVB
)
1245 lkm_flags
|= DLM_LKF_VALBLK
;
1250 if (catch_signals
&& signal_pending(current
)) {
1255 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1257 mlog_bug_on_msg(lockres
->l_flags
& OCFS2_LOCK_FREEING
,
1258 "Cluster lock called on freeing lockres %s! flags "
1259 "0x%lx\n", lockres
->l_name
, lockres
->l_flags
);
1261 /* We only compare against the currently granted level
1262 * here. If the lock is blocked waiting on a downconvert,
1263 * we'll get caught below. */
1264 if (lockres
->l_flags
& OCFS2_LOCK_BUSY
&&
1265 level
> lockres
->l_level
) {
1266 /* is someone sitting in dlm_lock? If so, wait on
1268 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BUSY
, 0);
1273 if (lockres
->l_flags
& OCFS2_LOCK_BLOCKED
&&
1274 !ocfs2_may_continue_on_blocked_lock(lockres
, level
)) {
1275 /* is the lock is currently blocked on behalf of
1277 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BLOCKED
, 0);
1282 if (level
> lockres
->l_level
) {
1283 if (noqueue_attempted
> 0) {
1287 if (lkm_flags
& DLM_LKF_NOQUEUE
)
1288 noqueue_attempted
= 1;
1290 if (lockres
->l_action
!= OCFS2_AST_INVALID
)
1291 mlog(ML_ERROR
, "lockres %s has action %u pending\n",
1292 lockres
->l_name
, lockres
->l_action
);
1294 if (!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
)) {
1295 lockres
->l_action
= OCFS2_AST_ATTACH
;
1296 lkm_flags
&= ~DLM_LKF_CONVERT
;
1298 lockres
->l_action
= OCFS2_AST_CONVERT
;
1299 lkm_flags
|= DLM_LKF_CONVERT
;
1302 lockres
->l_requested
= level
;
1303 lockres_or_flags(lockres
, OCFS2_LOCK_BUSY
);
1304 gen
= lockres_set_pending(lockres
);
1305 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1307 BUG_ON(level
== DLM_LOCK_IV
);
1308 BUG_ON(level
== DLM_LOCK_NL
);
1310 mlog(0, "lock %s, convert from %d to level = %d\n",
1311 lockres
->l_name
, lockres
->l_level
, level
);
1313 /* call dlm_lock to upgrade lock now */
1314 ret
= ocfs2_dlm_lock(osb
->cconn
,
1319 OCFS2_LOCK_ID_MAX_LEN
- 1,
1321 lockres_clear_pending(lockres
, gen
, osb
);
1323 if (!(lkm_flags
& DLM_LKF_NOQUEUE
) ||
1325 ocfs2_log_dlm_error("ocfs2_dlm_lock",
1328 ocfs2_recover_from_dlm_error(lockres
, 1);
1332 mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n",
1335 /* At this point we've gone inside the dlm and need to
1336 * complete our work regardless. */
1339 /* wait for busy to clear and carry on */
1343 /* Ok, if we get here then we're good to go. */
1344 ocfs2_inc_holders(lockres
, level
);
1348 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1351 * This is helping work around a lock inversion between the page lock
1352 * and dlm locks. One path holds the page lock while calling aops
1353 * which block acquiring dlm locks. The voting thread holds dlm
1354 * locks while acquiring page locks while down converting data locks.
1355 * This block is helping an aop path notice the inversion and back
1356 * off to unlock its page lock before trying the dlm lock again.
1358 if (wait
&& arg_flags
& OCFS2_LOCK_NONBLOCK
&&
1359 mw
.mw_mask
& (OCFS2_LOCK_BUSY
|OCFS2_LOCK_BLOCKED
)) {
1361 if (lockres_remove_mask_waiter(lockres
, &mw
))
1367 ret
= ocfs2_wait_for_mask(&mw
);
1372 ocfs2_update_lock_stats(lockres
, level
, &mw
, ret
);
1378 static void ocfs2_cluster_unlock(struct ocfs2_super
*osb
,
1379 struct ocfs2_lock_res
*lockres
,
1382 unsigned long flags
;
1385 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1386 ocfs2_dec_holders(lockres
, level
);
1387 ocfs2_downconvert_on_unlock(osb
, lockres
);
1388 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1392 static int ocfs2_create_new_lock(struct ocfs2_super
*osb
,
1393 struct ocfs2_lock_res
*lockres
,
1397 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
1398 unsigned long flags
;
1399 u32 lkm_flags
= local
? DLM_LKF_LOCAL
: 0;
1401 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1402 BUG_ON(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
);
1403 lockres_or_flags(lockres
, OCFS2_LOCK_LOCAL
);
1404 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1406 return ocfs2_lock_create(osb
, lockres
, level
, lkm_flags
);
1409 /* Grants us an EX lock on the data and metadata resources, skipping
1410 * the normal cluster directory lookup. Use this ONLY on newly created
1411 * inodes which other nodes can't possibly see, and which haven't been
1412 * hashed in the inode hash yet. This can give us a good performance
1413 * increase as it'll skip the network broadcast normally associated
1414 * with creating a new lock resource. */
1415 int ocfs2_create_new_inode_locks(struct inode
*inode
)
1418 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1421 BUG_ON(!ocfs2_inode_is_new(inode
));
1425 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1427 /* NOTE: That we don't increment any of the holder counts, nor
1428 * do we add anything to a journal handle. Since this is
1429 * supposed to be a new inode which the cluster doesn't know
1430 * about yet, there is no need to. As far as the LVB handling
1431 * is concerned, this is basically like acquiring an EX lock
1432 * on a resource which has an invalid one -- we'll set it
1433 * valid when we release the EX. */
1435 ret
= ocfs2_create_new_lock(osb
, &OCFS2_I(inode
)->ip_rw_lockres
, 1, 1);
1442 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
1443 * don't use a generation in their lock names.
1445 ret
= ocfs2_create_new_lock(osb
, &OCFS2_I(inode
)->ip_inode_lockres
, 1, 0);
1451 ret
= ocfs2_create_new_lock(osb
, &OCFS2_I(inode
)->ip_open_lockres
, 0, 0);
1462 int ocfs2_rw_lock(struct inode
*inode
, int write
)
1465 struct ocfs2_lock_res
*lockres
;
1466 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1472 mlog(0, "inode %llu take %s RW lock\n",
1473 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1474 write
? "EXMODE" : "PRMODE");
1476 if (ocfs2_mount_local(osb
))
1479 lockres
= &OCFS2_I(inode
)->ip_rw_lockres
;
1481 level
= write
? DLM_LOCK_EX
: DLM_LOCK_PR
;
1483 status
= ocfs2_cluster_lock(OCFS2_SB(inode
->i_sb
), lockres
, level
, 0,
1492 void ocfs2_rw_unlock(struct inode
*inode
, int write
)
1494 int level
= write
? DLM_LOCK_EX
: DLM_LOCK_PR
;
1495 struct ocfs2_lock_res
*lockres
= &OCFS2_I(inode
)->ip_rw_lockres
;
1496 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1500 mlog(0, "inode %llu drop %s RW lock\n",
1501 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1502 write
? "EXMODE" : "PRMODE");
1504 if (!ocfs2_mount_local(osb
))
1505 ocfs2_cluster_unlock(OCFS2_SB(inode
->i_sb
), lockres
, level
);
1511 * ocfs2_open_lock always get PR mode lock.
1513 int ocfs2_open_lock(struct inode
*inode
)
1516 struct ocfs2_lock_res
*lockres
;
1517 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1523 mlog(0, "inode %llu take PRMODE open lock\n",
1524 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1526 if (ocfs2_mount_local(osb
))
1529 lockres
= &OCFS2_I(inode
)->ip_open_lockres
;
1531 status
= ocfs2_cluster_lock(OCFS2_SB(inode
->i_sb
), lockres
,
1541 int ocfs2_try_open_lock(struct inode
*inode
, int write
)
1543 int status
= 0, level
;
1544 struct ocfs2_lock_res
*lockres
;
1545 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1551 mlog(0, "inode %llu try to take %s open lock\n",
1552 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1553 write
? "EXMODE" : "PRMODE");
1555 if (ocfs2_mount_local(osb
))
1558 lockres
= &OCFS2_I(inode
)->ip_open_lockres
;
1560 level
= write
? DLM_LOCK_EX
: DLM_LOCK_PR
;
1563 * The file system may already holding a PRMODE/EXMODE open lock.
1564 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
1565 * other nodes and the -EAGAIN will indicate to the caller that
1566 * this inode is still in use.
1568 status
= ocfs2_cluster_lock(OCFS2_SB(inode
->i_sb
), lockres
,
1569 level
, DLM_LKF_NOQUEUE
, 0);
1577 * ocfs2_open_unlock unlock PR and EX mode open locks.
1579 void ocfs2_open_unlock(struct inode
*inode
)
1581 struct ocfs2_lock_res
*lockres
= &OCFS2_I(inode
)->ip_open_lockres
;
1582 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1586 mlog(0, "inode %llu drop open lock\n",
1587 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1589 if (ocfs2_mount_local(osb
))
1592 if(lockres
->l_ro_holders
)
1593 ocfs2_cluster_unlock(OCFS2_SB(inode
->i_sb
), lockres
,
1595 if(lockres
->l_ex_holders
)
1596 ocfs2_cluster_unlock(OCFS2_SB(inode
->i_sb
), lockres
,
1603 static int ocfs2_flock_handle_signal(struct ocfs2_lock_res
*lockres
,
1607 struct ocfs2_super
*osb
= ocfs2_get_lockres_osb(lockres
);
1608 unsigned long flags
;
1609 struct ocfs2_mask_waiter mw
;
1611 ocfs2_init_mask_waiter(&mw
);
1614 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1615 if (lockres
->l_flags
& OCFS2_LOCK_BUSY
) {
1616 ret
= ocfs2_prepare_cancel_convert(osb
, lockres
);
1618 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1619 ret
= ocfs2_cancel_convert(osb
, lockres
);
1626 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BUSY
, 0);
1627 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1629 ocfs2_wait_for_mask(&mw
);
1635 * We may still have gotten the lock, in which case there's no
1636 * point to restarting the syscall.
1638 if (lockres
->l_level
== level
)
1641 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret
,
1642 lockres
->l_flags
, lockres
->l_level
, lockres
->l_action
);
1644 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1651 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1652 * flock() calls. The locking approach this requires is sufficiently
1653 * different from all other cluster lock types that we implement a
1654 * seperate path to the "low-level" dlm calls. In particular:
1656 * - No optimization of lock levels is done - we take at exactly
1657 * what's been requested.
1659 * - No lock caching is employed. We immediately downconvert to
1660 * no-lock at unlock time. This also means flock locks never go on
1661 * the blocking list).
1663 * - Since userspace can trivially deadlock itself with flock, we make
1664 * sure to allow cancellation of a misbehaving applications flock()
1667 * - Access to any flock lockres doesn't require concurrency, so we
1668 * can simplify the code by requiring the caller to guarantee
1669 * serialization of dlmglue flock calls.
1671 int ocfs2_file_lock(struct file
*file
, int ex
, int trylock
)
1673 int ret
, level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
1674 unsigned int lkm_flags
= trylock
? DLM_LKF_NOQUEUE
: 0;
1675 unsigned long flags
;
1676 struct ocfs2_file_private
*fp
= file
->private_data
;
1677 struct ocfs2_lock_res
*lockres
= &fp
->fp_flock
;
1678 struct ocfs2_super
*osb
= OCFS2_SB(file
->f_mapping
->host
->i_sb
);
1679 struct ocfs2_mask_waiter mw
;
1681 ocfs2_init_mask_waiter(&mw
);
1683 if ((lockres
->l_flags
& OCFS2_LOCK_BUSY
) ||
1684 (lockres
->l_level
> DLM_LOCK_NL
)) {
1686 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1687 "level: %u\n", lockres
->l_name
, lockres
->l_flags
,
1692 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1693 if (!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
)) {
1694 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BUSY
, 0);
1695 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1698 * Get the lock at NLMODE to start - that way we
1699 * can cancel the upconvert request if need be.
1701 ret
= ocfs2_lock_create(osb
, lockres
, DLM_LOCK_NL
, 0);
1707 ret
= ocfs2_wait_for_mask(&mw
);
1712 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1715 lockres
->l_action
= OCFS2_AST_CONVERT
;
1716 lkm_flags
|= DLM_LKF_CONVERT
;
1717 lockres
->l_requested
= level
;
1718 lockres_or_flags(lockres
, OCFS2_LOCK_BUSY
);
1720 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BUSY
, 0);
1721 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1723 ret
= ocfs2_dlm_lock(osb
->cconn
, level
, &lockres
->l_lksb
, lkm_flags
,
1724 lockres
->l_name
, OCFS2_LOCK_ID_MAX_LEN
- 1,
1727 if (!trylock
|| (ret
!= -EAGAIN
)) {
1728 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret
, lockres
);
1732 ocfs2_recover_from_dlm_error(lockres
, 1);
1733 lockres_remove_mask_waiter(lockres
, &mw
);
1737 ret
= ocfs2_wait_for_mask_interruptible(&mw
, lockres
);
1738 if (ret
== -ERESTARTSYS
) {
1740 * Userspace can cause deadlock itself with
1741 * flock(). Current behavior locally is to allow the
1742 * deadlock, but abort the system call if a signal is
1743 * received. We follow this example, otherwise a
1744 * poorly written program could sit in kernel until
1747 * Handling this is a bit more complicated for Ocfs2
1748 * though. We can't exit this function with an
1749 * outstanding lock request, so a cancel convert is
1750 * required. We intentionally overwrite 'ret' - if the
1751 * cancel fails and the lock was granted, it's easier
1752 * to just bubble sucess back up to the user.
1754 ret
= ocfs2_flock_handle_signal(lockres
, level
);
1755 } else if (!ret
&& (level
> lockres
->l_level
)) {
1756 /* Trylock failed asynchronously */
1763 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1764 lockres
->l_name
, ex
, trylock
, ret
);
1768 void ocfs2_file_unlock(struct file
*file
)
1772 unsigned long flags
;
1773 struct ocfs2_file_private
*fp
= file
->private_data
;
1774 struct ocfs2_lock_res
*lockres
= &fp
->fp_flock
;
1775 struct ocfs2_super
*osb
= OCFS2_SB(file
->f_mapping
->host
->i_sb
);
1776 struct ocfs2_mask_waiter mw
;
1778 ocfs2_init_mask_waiter(&mw
);
1780 if (!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
))
1783 if (lockres
->l_level
== DLM_LOCK_NL
)
1786 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1787 lockres
->l_name
, lockres
->l_flags
, lockres
->l_level
,
1790 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1792 * Fake a blocking ast for the downconvert code.
1794 lockres_or_flags(lockres
, OCFS2_LOCK_BLOCKED
);
1795 lockres
->l_blocking
= DLM_LOCK_EX
;
1797 gen
= ocfs2_prepare_downconvert(lockres
, DLM_LOCK_NL
);
1798 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_BUSY
, 0);
1799 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1801 ret
= ocfs2_downconvert_lock(osb
, lockres
, DLM_LOCK_NL
, 0, gen
);
1807 ret
= ocfs2_wait_for_mask(&mw
);
1812 static void ocfs2_downconvert_on_unlock(struct ocfs2_super
*osb
,
1813 struct ocfs2_lock_res
*lockres
)
1819 /* If we know that another node is waiting on our lock, kick
1820 * the downconvert thread * pre-emptively when we reach a release
1822 if (lockres
->l_flags
& OCFS2_LOCK_BLOCKED
) {
1823 switch(lockres
->l_blocking
) {
1825 if (!lockres
->l_ex_holders
&& !lockres
->l_ro_holders
)
1829 if (!lockres
->l_ex_holders
)
1838 ocfs2_wake_downconvert_thread(osb
);
1843 #define OCFS2_SEC_BITS 34
1844 #define OCFS2_SEC_SHIFT (64 - 34)
1845 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1847 /* LVB only has room for 64 bits of time here so we pack it for
1849 static u64
ocfs2_pack_timespec(struct timespec
*spec
)
1852 u64 sec
= spec
->tv_sec
;
1853 u32 nsec
= spec
->tv_nsec
;
1855 res
= (sec
<< OCFS2_SEC_SHIFT
) | (nsec
& OCFS2_NSEC_MASK
);
1860 /* Call this with the lockres locked. I am reasonably sure we don't
1861 * need ip_lock in this function as anyone who would be changing those
1862 * values is supposed to be blocked in ocfs2_inode_lock right now. */
1863 static void __ocfs2_stuff_meta_lvb(struct inode
*inode
)
1865 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1866 struct ocfs2_lock_res
*lockres
= &oi
->ip_inode_lockres
;
1867 struct ocfs2_meta_lvb
*lvb
;
1871 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
1874 * Invalidate the LVB of a deleted inode - this way other
1875 * nodes are forced to go to disk and discover the new inode
1878 if (oi
->ip_flags
& OCFS2_INODE_DELETED
) {
1879 lvb
->lvb_version
= 0;
1883 lvb
->lvb_version
= OCFS2_LVB_VERSION
;
1884 lvb
->lvb_isize
= cpu_to_be64(i_size_read(inode
));
1885 lvb
->lvb_iclusters
= cpu_to_be32(oi
->ip_clusters
);
1886 lvb
->lvb_iuid
= cpu_to_be32(inode
->i_uid
);
1887 lvb
->lvb_igid
= cpu_to_be32(inode
->i_gid
);
1888 lvb
->lvb_imode
= cpu_to_be16(inode
->i_mode
);
1889 lvb
->lvb_inlink
= cpu_to_be16(inode
->i_nlink
);
1890 lvb
->lvb_iatime_packed
=
1891 cpu_to_be64(ocfs2_pack_timespec(&inode
->i_atime
));
1892 lvb
->lvb_ictime_packed
=
1893 cpu_to_be64(ocfs2_pack_timespec(&inode
->i_ctime
));
1894 lvb
->lvb_imtime_packed
=
1895 cpu_to_be64(ocfs2_pack_timespec(&inode
->i_mtime
));
1896 lvb
->lvb_iattr
= cpu_to_be32(oi
->ip_attr
);
1897 lvb
->lvb_idynfeatures
= cpu_to_be16(oi
->ip_dyn_features
);
1898 lvb
->lvb_igeneration
= cpu_to_be32(inode
->i_generation
);
1901 mlog_meta_lvb(0, lockres
);
1906 static void ocfs2_unpack_timespec(struct timespec
*spec
,
1909 spec
->tv_sec
= packed_time
>> OCFS2_SEC_SHIFT
;
1910 spec
->tv_nsec
= packed_time
& OCFS2_NSEC_MASK
;
1913 static void ocfs2_refresh_inode_from_lvb(struct inode
*inode
)
1915 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1916 struct ocfs2_lock_res
*lockres
= &oi
->ip_inode_lockres
;
1917 struct ocfs2_meta_lvb
*lvb
;
1921 mlog_meta_lvb(0, lockres
);
1923 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
1925 /* We're safe here without the lockres lock... */
1926 spin_lock(&oi
->ip_lock
);
1927 oi
->ip_clusters
= be32_to_cpu(lvb
->lvb_iclusters
);
1928 i_size_write(inode
, be64_to_cpu(lvb
->lvb_isize
));
1930 oi
->ip_attr
= be32_to_cpu(lvb
->lvb_iattr
);
1931 oi
->ip_dyn_features
= be16_to_cpu(lvb
->lvb_idynfeatures
);
1932 ocfs2_set_inode_flags(inode
);
1934 /* fast-symlinks are a special case */
1935 if (S_ISLNK(inode
->i_mode
) && !oi
->ip_clusters
)
1936 inode
->i_blocks
= 0;
1938 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
1940 inode
->i_uid
= be32_to_cpu(lvb
->lvb_iuid
);
1941 inode
->i_gid
= be32_to_cpu(lvb
->lvb_igid
);
1942 inode
->i_mode
= be16_to_cpu(lvb
->lvb_imode
);
1943 inode
->i_nlink
= be16_to_cpu(lvb
->lvb_inlink
);
1944 ocfs2_unpack_timespec(&inode
->i_atime
,
1945 be64_to_cpu(lvb
->lvb_iatime_packed
));
1946 ocfs2_unpack_timespec(&inode
->i_mtime
,
1947 be64_to_cpu(lvb
->lvb_imtime_packed
));
1948 ocfs2_unpack_timespec(&inode
->i_ctime
,
1949 be64_to_cpu(lvb
->lvb_ictime_packed
));
1950 spin_unlock(&oi
->ip_lock
);
1955 static inline int ocfs2_meta_lvb_is_trustable(struct inode
*inode
,
1956 struct ocfs2_lock_res
*lockres
)
1958 struct ocfs2_meta_lvb
*lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
1960 if (lvb
->lvb_version
== OCFS2_LVB_VERSION
1961 && be32_to_cpu(lvb
->lvb_igeneration
) == inode
->i_generation
)
1966 /* Determine whether a lock resource needs to be refreshed, and
1967 * arbitrate who gets to refresh it.
1969 * 0 means no refresh needed.
1971 * > 0 means you need to refresh this and you MUST call
1972 * ocfs2_complete_lock_res_refresh afterwards. */
1973 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res
*lockres
)
1975 unsigned long flags
;
1981 spin_lock_irqsave(&lockres
->l_lock
, flags
);
1982 if (!(lockres
->l_flags
& OCFS2_LOCK_NEEDS_REFRESH
)) {
1983 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1987 if (lockres
->l_flags
& OCFS2_LOCK_REFRESHING
) {
1988 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
1990 ocfs2_wait_on_refreshing_lock(lockres
);
1994 /* Ok, I'll be the one to refresh this lock. */
1995 lockres_or_flags(lockres
, OCFS2_LOCK_REFRESHING
);
1996 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2004 /* If status is non zero, I'll mark it as not being in refresh
2005 * anymroe, but i won't clear the needs refresh flag. */
2006 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res
*lockres
,
2009 unsigned long flags
;
2012 spin_lock_irqsave(&lockres
->l_lock
, flags
);
2013 lockres_clear_flags(lockres
, OCFS2_LOCK_REFRESHING
);
2015 lockres_clear_flags(lockres
, OCFS2_LOCK_NEEDS_REFRESH
);
2016 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2018 wake_up(&lockres
->l_event
);
2023 /* may or may not return a bh if it went to disk. */
2024 static int ocfs2_inode_lock_update(struct inode
*inode
,
2025 struct buffer_head
**bh
)
2028 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2029 struct ocfs2_lock_res
*lockres
= &oi
->ip_inode_lockres
;
2030 struct ocfs2_dinode
*fe
;
2031 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2035 if (ocfs2_mount_local(osb
))
2038 spin_lock(&oi
->ip_lock
);
2039 if (oi
->ip_flags
& OCFS2_INODE_DELETED
) {
2040 mlog(0, "Orphaned inode %llu was deleted while we "
2041 "were waiting on a lock. ip_flags = 0x%x\n",
2042 (unsigned long long)oi
->ip_blkno
, oi
->ip_flags
);
2043 spin_unlock(&oi
->ip_lock
);
2047 spin_unlock(&oi
->ip_lock
);
2049 if (!ocfs2_should_refresh_lock_res(lockres
))
2052 /* This will discard any caching information we might have had
2053 * for the inode metadata. */
2054 ocfs2_metadata_cache_purge(inode
);
2056 ocfs2_extent_map_trunc(inode
, 0);
2058 if (ocfs2_meta_lvb_is_trustable(inode
, lockres
)) {
2059 mlog(0, "Trusting LVB on inode %llu\n",
2060 (unsigned long long)oi
->ip_blkno
);
2061 ocfs2_refresh_inode_from_lvb(inode
);
2063 /* Boo, we have to go to disk. */
2064 /* read bh, cast, ocfs2_refresh_inode */
2065 status
= ocfs2_read_inode_block(inode
, bh
);
2070 fe
= (struct ocfs2_dinode
*) (*bh
)->b_data
;
2072 /* This is a good chance to make sure we're not
2073 * locking an invalid object. ocfs2_read_inode_block()
2074 * already checked that the inode block is sane.
2076 * We bug on a stale inode here because we checked
2077 * above whether it was wiped from disk. The wiping
2078 * node provides a guarantee that we receive that
2079 * message and can mark the inode before dropping any
2080 * locks associated with it. */
2081 mlog_bug_on_msg(inode
->i_generation
!=
2082 le32_to_cpu(fe
->i_generation
),
2083 "Invalid dinode %llu disk generation: %u "
2084 "inode->i_generation: %u\n",
2085 (unsigned long long)oi
->ip_blkno
,
2086 le32_to_cpu(fe
->i_generation
),
2087 inode
->i_generation
);
2088 mlog_bug_on_msg(le64_to_cpu(fe
->i_dtime
) ||
2089 !(fe
->i_flags
& cpu_to_le32(OCFS2_VALID_FL
)),
2090 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
2091 (unsigned long long)oi
->ip_blkno
,
2092 (unsigned long long)le64_to_cpu(fe
->i_dtime
),
2093 le32_to_cpu(fe
->i_flags
));
2095 ocfs2_refresh_inode(inode
, fe
);
2096 ocfs2_track_lock_refresh(lockres
);
2101 ocfs2_complete_lock_res_refresh(lockres
, status
);
2107 static int ocfs2_assign_bh(struct inode
*inode
,
2108 struct buffer_head
**ret_bh
,
2109 struct buffer_head
*passed_bh
)
2114 /* Ok, the update went to disk for us, use the
2116 *ret_bh
= passed_bh
;
2122 status
= ocfs2_read_inode_block(inode
, ret_bh
);
2130 * returns < 0 error if the callback will never be called, otherwise
2131 * the result of the lock will be communicated via the callback.
2133 int ocfs2_inode_lock_full(struct inode
*inode
,
2134 struct buffer_head
**ret_bh
,
2138 int status
, level
, acquired
;
2140 struct ocfs2_lock_res
*lockres
= NULL
;
2141 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2142 struct buffer_head
*local_bh
= NULL
;
2148 mlog(0, "inode %llu, take %s META lock\n",
2149 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2150 ex
? "EXMODE" : "PRMODE");
2154 /* We'll allow faking a readonly metadata lock for
2156 if (ocfs2_is_hard_readonly(osb
)) {
2162 if (ocfs2_mount_local(osb
))
2165 if (!(arg_flags
& OCFS2_META_LOCK_RECOVERY
))
2166 ocfs2_wait_for_recovery(osb
);
2168 lockres
= &OCFS2_I(inode
)->ip_inode_lockres
;
2169 level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2171 if (arg_flags
& OCFS2_META_LOCK_NOQUEUE
)
2172 dlm_flags
|= DLM_LKF_NOQUEUE
;
2174 status
= ocfs2_cluster_lock(osb
, lockres
, level
, dlm_flags
, arg_flags
);
2176 if (status
!= -EAGAIN
&& status
!= -EIOCBRETRY
)
2181 /* Notify the error cleanup path to drop the cluster lock. */
2184 /* We wait twice because a node may have died while we were in
2185 * the lower dlm layers. The second time though, we've
2186 * committed to owning this lock so we don't allow signals to
2187 * abort the operation. */
2188 if (!(arg_flags
& OCFS2_META_LOCK_RECOVERY
))
2189 ocfs2_wait_for_recovery(osb
);
2193 * We only see this flag if we're being called from
2194 * ocfs2_read_locked_inode(). It means we're locking an inode
2195 * which hasn't been populated yet, so clear the refresh flag
2196 * and let the caller handle it.
2198 if (inode
->i_state
& I_NEW
) {
2201 ocfs2_complete_lock_res_refresh(lockres
, 0);
2205 /* This is fun. The caller may want a bh back, or it may
2206 * not. ocfs2_inode_lock_update definitely wants one in, but
2207 * may or may not read one, depending on what's in the
2208 * LVB. The result of all of this is that we've *only* gone to
2209 * disk if we have to, so the complexity is worthwhile. */
2210 status
= ocfs2_inode_lock_update(inode
, &local_bh
);
2212 if (status
!= -ENOENT
)
2218 status
= ocfs2_assign_bh(inode
, ret_bh
, local_bh
);
2227 if (ret_bh
&& (*ret_bh
)) {
2232 ocfs2_inode_unlock(inode
, ex
);
2243 * This is working around a lock inversion between tasks acquiring DLM
2244 * locks while holding a page lock and the downconvert thread which
2245 * blocks dlm lock acquiry while acquiring page locks.
2247 * ** These _with_page variantes are only intended to be called from aop
2248 * methods that hold page locks and return a very specific *positive* error
2249 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2251 * The DLM is called such that it returns -EAGAIN if it would have
2252 * blocked waiting for the downconvert thread. In that case we unlock
2253 * our page so the downconvert thread can make progress. Once we've
2254 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2255 * that called us can bubble that back up into the VFS who will then
2256 * immediately retry the aop call.
2258 * We do a blocking lock and immediate unlock before returning, though, so that
2259 * the lock has a great chance of being cached on this node by the time the VFS
2260 * calls back to retry the aop. This has a potential to livelock as nodes
2261 * ping locks back and forth, but that's a risk we're willing to take to avoid
2262 * the lock inversion simply.
2264 int ocfs2_inode_lock_with_page(struct inode
*inode
,
2265 struct buffer_head
**ret_bh
,
2271 ret
= ocfs2_inode_lock_full(inode
, ret_bh
, ex
, OCFS2_LOCK_NONBLOCK
);
2272 if (ret
== -EAGAIN
) {
2274 if (ocfs2_inode_lock(inode
, ret_bh
, ex
) == 0)
2275 ocfs2_inode_unlock(inode
, ex
);
2276 ret
= AOP_TRUNCATED_PAGE
;
2282 int ocfs2_inode_lock_atime(struct inode
*inode
,
2283 struct vfsmount
*vfsmnt
,
2289 ret
= ocfs2_inode_lock(inode
, NULL
, 0);
2296 * If we should update atime, we will get EX lock,
2297 * otherwise we just get PR lock.
2299 if (ocfs2_should_update_atime(inode
, vfsmnt
)) {
2300 struct buffer_head
*bh
= NULL
;
2302 ocfs2_inode_unlock(inode
, 0);
2303 ret
= ocfs2_inode_lock(inode
, &bh
, 1);
2309 if (ocfs2_should_update_atime(inode
, vfsmnt
))
2310 ocfs2_update_inode_atime(inode
, bh
);
2320 void ocfs2_inode_unlock(struct inode
*inode
,
2323 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2324 struct ocfs2_lock_res
*lockres
= &OCFS2_I(inode
)->ip_inode_lockres
;
2325 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2329 mlog(0, "inode %llu drop %s META lock\n",
2330 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2331 ex
? "EXMODE" : "PRMODE");
2333 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode
->i_sb
)) &&
2334 !ocfs2_mount_local(osb
))
2335 ocfs2_cluster_unlock(OCFS2_SB(inode
->i_sb
), lockres
, level
);
2340 int ocfs2_super_lock(struct ocfs2_super
*osb
,
2344 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2345 struct ocfs2_lock_res
*lockres
= &osb
->osb_super_lockres
;
2349 if (ocfs2_is_hard_readonly(osb
))
2352 if (ocfs2_mount_local(osb
))
2355 status
= ocfs2_cluster_lock(osb
, lockres
, level
, 0, 0);
2361 /* The super block lock path is really in the best position to
2362 * know when resources covered by the lock need to be
2363 * refreshed, so we do it here. Of course, making sense of
2364 * everything is up to the caller :) */
2365 status
= ocfs2_should_refresh_lock_res(lockres
);
2371 status
= ocfs2_refresh_slot_info(osb
);
2373 ocfs2_complete_lock_res_refresh(lockres
, status
);
2377 ocfs2_track_lock_refresh(lockres
);
2384 void ocfs2_super_unlock(struct ocfs2_super
*osb
,
2387 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2388 struct ocfs2_lock_res
*lockres
= &osb
->osb_super_lockres
;
2390 if (!ocfs2_mount_local(osb
))
2391 ocfs2_cluster_unlock(osb
, lockres
, level
);
2394 int ocfs2_rename_lock(struct ocfs2_super
*osb
)
2397 struct ocfs2_lock_res
*lockres
= &osb
->osb_rename_lockres
;
2399 if (ocfs2_is_hard_readonly(osb
))
2402 if (ocfs2_mount_local(osb
))
2405 status
= ocfs2_cluster_lock(osb
, lockres
, DLM_LOCK_EX
, 0, 0);
2412 void ocfs2_rename_unlock(struct ocfs2_super
*osb
)
2414 struct ocfs2_lock_res
*lockres
= &osb
->osb_rename_lockres
;
2416 if (!ocfs2_mount_local(osb
))
2417 ocfs2_cluster_unlock(osb
, lockres
, DLM_LOCK_EX
);
2420 int ocfs2_dentry_lock(struct dentry
*dentry
, int ex
)
2423 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2424 struct ocfs2_dentry_lock
*dl
= dentry
->d_fsdata
;
2425 struct ocfs2_super
*osb
= OCFS2_SB(dentry
->d_sb
);
2429 if (ocfs2_is_hard_readonly(osb
))
2432 if (ocfs2_mount_local(osb
))
2435 ret
= ocfs2_cluster_lock(osb
, &dl
->dl_lockres
, level
, 0, 0);
2442 void ocfs2_dentry_unlock(struct dentry
*dentry
, int ex
)
2444 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
2445 struct ocfs2_dentry_lock
*dl
= dentry
->d_fsdata
;
2446 struct ocfs2_super
*osb
= OCFS2_SB(dentry
->d_sb
);
2448 if (!ocfs2_mount_local(osb
))
2449 ocfs2_cluster_unlock(osb
, &dl
->dl_lockres
, level
);
2452 /* Reference counting of the dlm debug structure. We want this because
2453 * open references on the debug inodes can live on after a mount, so
2454 * we can't rely on the ocfs2_super to always exist. */
2455 static void ocfs2_dlm_debug_free(struct kref
*kref
)
2457 struct ocfs2_dlm_debug
*dlm_debug
;
2459 dlm_debug
= container_of(kref
, struct ocfs2_dlm_debug
, d_refcnt
);
2464 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug
*dlm_debug
)
2467 kref_put(&dlm_debug
->d_refcnt
, ocfs2_dlm_debug_free
);
2470 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug
*debug
)
2472 kref_get(&debug
->d_refcnt
);
2475 struct ocfs2_dlm_debug
*ocfs2_new_dlm_debug(void)
2477 struct ocfs2_dlm_debug
*dlm_debug
;
2479 dlm_debug
= kmalloc(sizeof(struct ocfs2_dlm_debug
), GFP_KERNEL
);
2481 mlog_errno(-ENOMEM
);
2485 kref_init(&dlm_debug
->d_refcnt
);
2486 INIT_LIST_HEAD(&dlm_debug
->d_lockres_tracking
);
2487 dlm_debug
->d_locking_state
= NULL
;
2492 /* Access to this is arbitrated for us via seq_file->sem. */
2493 struct ocfs2_dlm_seq_priv
{
2494 struct ocfs2_dlm_debug
*p_dlm_debug
;
2495 struct ocfs2_lock_res p_iter_res
;
2496 struct ocfs2_lock_res p_tmp_res
;
2499 static struct ocfs2_lock_res
*ocfs2_dlm_next_res(struct ocfs2_lock_res
*start
,
2500 struct ocfs2_dlm_seq_priv
*priv
)
2502 struct ocfs2_lock_res
*iter
, *ret
= NULL
;
2503 struct ocfs2_dlm_debug
*dlm_debug
= priv
->p_dlm_debug
;
2505 assert_spin_locked(&ocfs2_dlm_tracking_lock
);
2507 list_for_each_entry(iter
, &start
->l_debug_list
, l_debug_list
) {
2508 /* discover the head of the list */
2509 if (&iter
->l_debug_list
== &dlm_debug
->d_lockres_tracking
) {
2510 mlog(0, "End of list found, %p\n", ret
);
2514 /* We track our "dummy" iteration lockres' by a NULL
2516 if (iter
->l_ops
!= NULL
) {
2525 static void *ocfs2_dlm_seq_start(struct seq_file
*m
, loff_t
*pos
)
2527 struct ocfs2_dlm_seq_priv
*priv
= m
->private;
2528 struct ocfs2_lock_res
*iter
;
2530 spin_lock(&ocfs2_dlm_tracking_lock
);
2531 iter
= ocfs2_dlm_next_res(&priv
->p_iter_res
, priv
);
2533 /* Since lockres' have the lifetime of their container
2534 * (which can be inodes, ocfs2_supers, etc) we want to
2535 * copy this out to a temporary lockres while still
2536 * under the spinlock. Obviously after this we can't
2537 * trust any pointers on the copy returned, but that's
2538 * ok as the information we want isn't typically held
2540 priv
->p_tmp_res
= *iter
;
2541 iter
= &priv
->p_tmp_res
;
2543 spin_unlock(&ocfs2_dlm_tracking_lock
);
2548 static void ocfs2_dlm_seq_stop(struct seq_file
*m
, void *v
)
2552 static void *ocfs2_dlm_seq_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2554 struct ocfs2_dlm_seq_priv
*priv
= m
->private;
2555 struct ocfs2_lock_res
*iter
= v
;
2556 struct ocfs2_lock_res
*dummy
= &priv
->p_iter_res
;
2558 spin_lock(&ocfs2_dlm_tracking_lock
);
2559 iter
= ocfs2_dlm_next_res(iter
, priv
);
2560 list_del_init(&dummy
->l_debug_list
);
2562 list_add(&dummy
->l_debug_list
, &iter
->l_debug_list
);
2563 priv
->p_tmp_res
= *iter
;
2564 iter
= &priv
->p_tmp_res
;
2566 spin_unlock(&ocfs2_dlm_tracking_lock
);
2571 /* So that debugfs.ocfs2 can determine which format is being used */
2572 #define OCFS2_DLM_DEBUG_STR_VERSION 2
2573 static int ocfs2_dlm_seq_show(struct seq_file
*m
, void *v
)
2577 struct ocfs2_lock_res
*lockres
= v
;
2582 seq_printf(m
, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION
);
2584 if (lockres
->l_type
== OCFS2_LOCK_TYPE_DENTRY
)
2585 seq_printf(m
, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START
- 1,
2587 (unsigned int)ocfs2_get_dentry_lock_ino(lockres
));
2589 seq_printf(m
, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN
, lockres
->l_name
);
2591 seq_printf(m
, "%d\t"
2602 lockres
->l_unlock_action
,
2603 lockres
->l_ro_holders
,
2604 lockres
->l_ex_holders
,
2605 lockres
->l_requested
,
2606 lockres
->l_blocking
);
2608 /* Dump the raw LVB */
2609 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
2610 for(i
= 0; i
< DLM_LVB_LEN
; i
++)
2611 seq_printf(m
, "0x%x\t", lvb
[i
]);
2613 #ifdef CONFIG_OCFS2_FS_STATS
2614 # define lock_num_prmode(_l) (_l)->l_lock_num_prmode
2615 # define lock_num_exmode(_l) (_l)->l_lock_num_exmode
2616 # define lock_num_prmode_failed(_l) (_l)->l_lock_num_prmode_failed
2617 # define lock_num_exmode_failed(_l) (_l)->l_lock_num_exmode_failed
2618 # define lock_total_prmode(_l) (_l)->l_lock_total_prmode
2619 # define lock_total_exmode(_l) (_l)->l_lock_total_exmode
2620 # define lock_max_prmode(_l) (_l)->l_lock_max_prmode
2621 # define lock_max_exmode(_l) (_l)->l_lock_max_exmode
2622 # define lock_refresh(_l) (_l)->l_lock_refresh
2624 # define lock_num_prmode(_l) (0ULL)
2625 # define lock_num_exmode(_l) (0ULL)
2626 # define lock_num_prmode_failed(_l) (0)
2627 # define lock_num_exmode_failed(_l) (0)
2628 # define lock_total_prmode(_l) (0ULL)
2629 # define lock_total_exmode(_l) (0ULL)
2630 # define lock_max_prmode(_l) (0)
2631 # define lock_max_exmode(_l) (0)
2632 # define lock_refresh(_l) (0)
2634 /* The following seq_print was added in version 2 of this output */
2635 seq_printf(m
, "%llu\t"
2644 lock_num_prmode(lockres
),
2645 lock_num_exmode(lockres
),
2646 lock_num_prmode_failed(lockres
),
2647 lock_num_exmode_failed(lockres
),
2648 lock_total_prmode(lockres
),
2649 lock_total_exmode(lockres
),
2650 lock_max_prmode(lockres
),
2651 lock_max_exmode(lockres
),
2652 lock_refresh(lockres
));
2655 seq_printf(m
, "\n");
2659 static const struct seq_operations ocfs2_dlm_seq_ops
= {
2660 .start
= ocfs2_dlm_seq_start
,
2661 .stop
= ocfs2_dlm_seq_stop
,
2662 .next
= ocfs2_dlm_seq_next
,
2663 .show
= ocfs2_dlm_seq_show
,
2666 static int ocfs2_dlm_debug_release(struct inode
*inode
, struct file
*file
)
2668 struct seq_file
*seq
= (struct seq_file
*) file
->private_data
;
2669 struct ocfs2_dlm_seq_priv
*priv
= seq
->private;
2670 struct ocfs2_lock_res
*res
= &priv
->p_iter_res
;
2672 ocfs2_remove_lockres_tracking(res
);
2673 ocfs2_put_dlm_debug(priv
->p_dlm_debug
);
2674 return seq_release_private(inode
, file
);
2677 static int ocfs2_dlm_debug_open(struct inode
*inode
, struct file
*file
)
2680 struct ocfs2_dlm_seq_priv
*priv
;
2681 struct seq_file
*seq
;
2682 struct ocfs2_super
*osb
;
2684 priv
= kzalloc(sizeof(struct ocfs2_dlm_seq_priv
), GFP_KERNEL
);
2690 osb
= inode
->i_private
;
2691 ocfs2_get_dlm_debug(osb
->osb_dlm_debug
);
2692 priv
->p_dlm_debug
= osb
->osb_dlm_debug
;
2693 INIT_LIST_HEAD(&priv
->p_iter_res
.l_debug_list
);
2695 ret
= seq_open(file
, &ocfs2_dlm_seq_ops
);
2702 seq
= (struct seq_file
*) file
->private_data
;
2703 seq
->private = priv
;
2705 ocfs2_add_lockres_tracking(&priv
->p_iter_res
,
2712 static const struct file_operations ocfs2_dlm_debug_fops
= {
2713 .open
= ocfs2_dlm_debug_open
,
2714 .release
= ocfs2_dlm_debug_release
,
2716 .llseek
= seq_lseek
,
2719 static int ocfs2_dlm_init_debug(struct ocfs2_super
*osb
)
2722 struct ocfs2_dlm_debug
*dlm_debug
= osb
->osb_dlm_debug
;
2724 dlm_debug
->d_locking_state
= debugfs_create_file("locking_state",
2726 osb
->osb_debug_root
,
2728 &ocfs2_dlm_debug_fops
);
2729 if (!dlm_debug
->d_locking_state
) {
2732 "Unable to create locking state debugfs file.\n");
2736 ocfs2_get_dlm_debug(dlm_debug
);
2741 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super
*osb
)
2743 struct ocfs2_dlm_debug
*dlm_debug
= osb
->osb_dlm_debug
;
2746 debugfs_remove(dlm_debug
->d_locking_state
);
2747 ocfs2_put_dlm_debug(dlm_debug
);
2751 int ocfs2_dlm_init(struct ocfs2_super
*osb
)
2754 struct ocfs2_cluster_connection
*conn
= NULL
;
2758 if (ocfs2_mount_local(osb
)) {
2763 status
= ocfs2_dlm_init_debug(osb
);
2769 /* launch downconvert thread */
2770 osb
->dc_task
= kthread_run(ocfs2_downconvert_thread
, osb
, "ocfs2dc");
2771 if (IS_ERR(osb
->dc_task
)) {
2772 status
= PTR_ERR(osb
->dc_task
);
2773 osb
->dc_task
= NULL
;
2778 /* for now, uuid == domain */
2779 status
= ocfs2_cluster_connect(osb
->osb_cluster_stack
,
2781 strlen(osb
->uuid_str
),
2782 ocfs2_do_node_down
, osb
,
2789 status
= ocfs2_cluster_this_node(&osb
->node_num
);
2793 "could not find this host's node number\n");
2794 ocfs2_cluster_disconnect(conn
, 0);
2799 ocfs2_super_lock_res_init(&osb
->osb_super_lockres
, osb
);
2800 ocfs2_rename_lock_res_init(&osb
->osb_rename_lockres
, osb
);
2807 ocfs2_dlm_shutdown_debug(osb
);
2809 kthread_stop(osb
->dc_task
);
2816 void ocfs2_dlm_shutdown(struct ocfs2_super
*osb
,
2821 ocfs2_drop_osb_locks(osb
);
2824 * Now that we have dropped all locks and ocfs2_dismount_volume()
2825 * has disabled recovery, the DLM won't be talking to us. It's
2826 * safe to tear things down before disconnecting the cluster.
2830 kthread_stop(osb
->dc_task
);
2831 osb
->dc_task
= NULL
;
2834 ocfs2_lock_res_free(&osb
->osb_super_lockres
);
2835 ocfs2_lock_res_free(&osb
->osb_rename_lockres
);
2837 ocfs2_cluster_disconnect(osb
->cconn
, hangup_pending
);
2840 ocfs2_dlm_shutdown_debug(osb
);
2845 static void ocfs2_unlock_ast(void *opaque
, int error
)
2847 struct ocfs2_lock_res
*lockres
= opaque
;
2848 unsigned long flags
;
2852 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres
->l_name
,
2853 lockres
->l_unlock_action
);
2855 spin_lock_irqsave(&lockres
->l_lock
, flags
);
2857 mlog(ML_ERROR
, "Dlm passes error %d for lock %s, "
2858 "unlock_action %d\n", error
, lockres
->l_name
,
2859 lockres
->l_unlock_action
);
2860 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2864 switch(lockres
->l_unlock_action
) {
2865 case OCFS2_UNLOCK_CANCEL_CONVERT
:
2866 mlog(0, "Cancel convert success for %s\n", lockres
->l_name
);
2867 lockres
->l_action
= OCFS2_AST_INVALID
;
2868 /* Downconvert thread may have requeued this lock, we
2869 * need to wake it. */
2870 if (lockres
->l_flags
& OCFS2_LOCK_BLOCKED
)
2871 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres
));
2873 case OCFS2_UNLOCK_DROP_LOCK
:
2874 lockres
->l_level
= DLM_LOCK_IV
;
2880 lockres_clear_flags(lockres
, OCFS2_LOCK_BUSY
);
2881 lockres
->l_unlock_action
= OCFS2_UNLOCK_INVALID
;
2882 wake_up(&lockres
->l_event
);
2883 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2888 static int ocfs2_drop_lock(struct ocfs2_super
*osb
,
2889 struct ocfs2_lock_res
*lockres
)
2892 unsigned long flags
;
2895 /* We didn't get anywhere near actually using this lockres. */
2896 if (!(lockres
->l_flags
& OCFS2_LOCK_INITIALIZED
))
2899 if (lockres
->l_ops
->flags
& LOCK_TYPE_USES_LVB
)
2900 lkm_flags
|= DLM_LKF_VALBLK
;
2902 spin_lock_irqsave(&lockres
->l_lock
, flags
);
2904 mlog_bug_on_msg(!(lockres
->l_flags
& OCFS2_LOCK_FREEING
),
2905 "lockres %s, flags 0x%lx\n",
2906 lockres
->l_name
, lockres
->l_flags
);
2908 while (lockres
->l_flags
& OCFS2_LOCK_BUSY
) {
2909 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2910 "%u, unlock_action = %u\n",
2911 lockres
->l_name
, lockres
->l_flags
, lockres
->l_action
,
2912 lockres
->l_unlock_action
);
2914 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2916 /* XXX: Today we just wait on any busy
2917 * locks... Perhaps we need to cancel converts in the
2919 ocfs2_wait_on_busy_lock(lockres
);
2921 spin_lock_irqsave(&lockres
->l_lock
, flags
);
2924 if (lockres
->l_ops
->flags
& LOCK_TYPE_USES_LVB
) {
2925 if (lockres
->l_flags
& OCFS2_LOCK_ATTACHED
&&
2926 lockres
->l_level
== DLM_LOCK_EX
&&
2927 !(lockres
->l_flags
& OCFS2_LOCK_NEEDS_REFRESH
))
2928 lockres
->l_ops
->set_lvb(lockres
);
2931 if (lockres
->l_flags
& OCFS2_LOCK_BUSY
)
2932 mlog(ML_ERROR
, "destroying busy lock: \"%s\"\n",
2934 if (lockres
->l_flags
& OCFS2_LOCK_BLOCKED
)
2935 mlog(0, "destroying blocked lock: \"%s\"\n", lockres
->l_name
);
2937 if (!(lockres
->l_flags
& OCFS2_LOCK_ATTACHED
)) {
2938 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2942 lockres_clear_flags(lockres
, OCFS2_LOCK_ATTACHED
);
2944 /* make sure we never get here while waiting for an ast to
2946 BUG_ON(lockres
->l_action
!= OCFS2_AST_INVALID
);
2948 /* is this necessary? */
2949 lockres_or_flags(lockres
, OCFS2_LOCK_BUSY
);
2950 lockres
->l_unlock_action
= OCFS2_UNLOCK_DROP_LOCK
;
2951 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2953 mlog(0, "lock %s\n", lockres
->l_name
);
2955 ret
= ocfs2_dlm_unlock(osb
->cconn
, &lockres
->l_lksb
, lkm_flags
,
2958 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret
, lockres
);
2959 mlog(ML_ERROR
, "lockres flags: %lu\n", lockres
->l_flags
);
2960 ocfs2_dlm_dump_lksb(&lockres
->l_lksb
);
2963 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
2966 ocfs2_wait_on_busy_lock(lockres
);
2972 /* Mark the lockres as being dropped. It will no longer be
2973 * queued if blocking, but we still may have to wait on it
2974 * being dequeued from the downconvert thread before we can consider
2977 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2978 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res
*lockres
)
2981 struct ocfs2_mask_waiter mw
;
2982 unsigned long flags
;
2984 ocfs2_init_mask_waiter(&mw
);
2986 spin_lock_irqsave(&lockres
->l_lock
, flags
);
2987 lockres
->l_flags
|= OCFS2_LOCK_FREEING
;
2988 while (lockres
->l_flags
& OCFS2_LOCK_QUEUED
) {
2989 lockres_add_mask_waiter(lockres
, &mw
, OCFS2_LOCK_QUEUED
, 0);
2990 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
2992 mlog(0, "Waiting on lockres %s\n", lockres
->l_name
);
2994 status
= ocfs2_wait_for_mask(&mw
);
2998 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3000 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3003 void ocfs2_simple_drop_lockres(struct ocfs2_super
*osb
,
3004 struct ocfs2_lock_res
*lockres
)
3008 ocfs2_mark_lockres_freeing(lockres
);
3009 ret
= ocfs2_drop_lock(osb
, lockres
);
3014 static void ocfs2_drop_osb_locks(struct ocfs2_super
*osb
)
3016 ocfs2_simple_drop_lockres(osb
, &osb
->osb_super_lockres
);
3017 ocfs2_simple_drop_lockres(osb
, &osb
->osb_rename_lockres
);
3020 int ocfs2_drop_inode_locks(struct inode
*inode
)
3026 /* No need to call ocfs2_mark_lockres_freeing here -
3027 * ocfs2_clear_inode has done it for us. */
3029 err
= ocfs2_drop_lock(OCFS2_SB(inode
->i_sb
),
3030 &OCFS2_I(inode
)->ip_open_lockres
);
3036 err
= ocfs2_drop_lock(OCFS2_SB(inode
->i_sb
),
3037 &OCFS2_I(inode
)->ip_inode_lockres
);
3040 if (err
< 0 && !status
)
3043 err
= ocfs2_drop_lock(OCFS2_SB(inode
->i_sb
),
3044 &OCFS2_I(inode
)->ip_rw_lockres
);
3047 if (err
< 0 && !status
)
3054 static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res
*lockres
,
3057 assert_spin_locked(&lockres
->l_lock
);
3059 BUG_ON(lockres
->l_blocking
<= DLM_LOCK_NL
);
3061 if (lockres
->l_level
<= new_level
) {
3062 mlog(ML_ERROR
, "lockres->l_level (%d) <= new_level (%d)\n",
3063 lockres
->l_level
, new_level
);
3067 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
3068 lockres
->l_name
, new_level
, lockres
->l_blocking
);
3070 lockres
->l_action
= OCFS2_AST_DOWNCONVERT
;
3071 lockres
->l_requested
= new_level
;
3072 lockres_or_flags(lockres
, OCFS2_LOCK_BUSY
);
3073 return lockres_set_pending(lockres
);
3076 static int ocfs2_downconvert_lock(struct ocfs2_super
*osb
,
3077 struct ocfs2_lock_res
*lockres
,
3080 unsigned int generation
)
3083 u32 dlm_flags
= DLM_LKF_CONVERT
;
3088 dlm_flags
|= DLM_LKF_VALBLK
;
3090 ret
= ocfs2_dlm_lock(osb
->cconn
,
3095 OCFS2_LOCK_ID_MAX_LEN
- 1,
3097 lockres_clear_pending(lockres
, generation
, osb
);
3099 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret
, lockres
);
3100 ocfs2_recover_from_dlm_error(lockres
, 1);
3110 /* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
3111 static int ocfs2_prepare_cancel_convert(struct ocfs2_super
*osb
,
3112 struct ocfs2_lock_res
*lockres
)
3114 assert_spin_locked(&lockres
->l_lock
);
3117 mlog(0, "lock %s\n", lockres
->l_name
);
3119 if (lockres
->l_unlock_action
== OCFS2_UNLOCK_CANCEL_CONVERT
) {
3120 /* If we're already trying to cancel a lock conversion
3121 * then just drop the spinlock and allow the caller to
3122 * requeue this lock. */
3124 mlog(0, "Lockres %s, skip convert\n", lockres
->l_name
);
3128 /* were we in a convert when we got the bast fire? */
3129 BUG_ON(lockres
->l_action
!= OCFS2_AST_CONVERT
&&
3130 lockres
->l_action
!= OCFS2_AST_DOWNCONVERT
);
3131 /* set things up for the unlockast to know to just
3132 * clear out the ast_action and unset busy, etc. */
3133 lockres
->l_unlock_action
= OCFS2_UNLOCK_CANCEL_CONVERT
;
3135 mlog_bug_on_msg(!(lockres
->l_flags
& OCFS2_LOCK_BUSY
),
3136 "lock %s, invalid flags: 0x%lx\n",
3137 lockres
->l_name
, lockres
->l_flags
);
3142 static int ocfs2_cancel_convert(struct ocfs2_super
*osb
,
3143 struct ocfs2_lock_res
*lockres
)
3148 mlog(0, "lock %s\n", lockres
->l_name
);
3150 ret
= ocfs2_dlm_unlock(osb
->cconn
, &lockres
->l_lksb
,
3151 DLM_LKF_CANCEL
, lockres
);
3153 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret
, lockres
);
3154 ocfs2_recover_from_dlm_error(lockres
, 0);
3157 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres
->l_name
);
3163 static int ocfs2_unblock_lock(struct ocfs2_super
*osb
,
3164 struct ocfs2_lock_res
*lockres
,
3165 struct ocfs2_unblock_ctl
*ctl
)
3167 unsigned long flags
;
3176 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3178 BUG_ON(!(lockres
->l_flags
& OCFS2_LOCK_BLOCKED
));
3181 if (lockres
->l_flags
& OCFS2_LOCK_BUSY
) {
3183 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3184 * exists entirely for one reason - another thread has set
3185 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3187 * If we do ocfs2_cancel_convert() before the other thread
3188 * calls dlm_lock(), our cancel will do nothing. We will
3189 * get no ast, and we will have no way of knowing the
3190 * cancel failed. Meanwhile, the other thread will call
3191 * into dlm_lock() and wait...forever.
3193 * Why forever? Because another node has asked for the
3194 * lock first; that's why we're here in unblock_lock().
3196 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3197 * set, we just requeue the unblock. Only when the other
3198 * thread has called dlm_lock() and cleared PENDING will
3199 * we then cancel their request.
3201 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3202 * at the same time they set OCFS2_DLM_BUSY. They must
3203 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3205 if (lockres
->l_flags
& OCFS2_LOCK_PENDING
)
3209 ret
= ocfs2_prepare_cancel_convert(osb
, lockres
);
3210 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3212 ret
= ocfs2_cancel_convert(osb
, lockres
);
3219 /* if we're blocking an exclusive and we have *any* holders,
3221 if ((lockres
->l_blocking
== DLM_LOCK_EX
)
3222 && (lockres
->l_ex_holders
|| lockres
->l_ro_holders
))
3225 /* If it's a PR we're blocking, then only
3226 * requeue if we've got any EX holders */
3227 if (lockres
->l_blocking
== DLM_LOCK_PR
&&
3228 lockres
->l_ex_holders
)
3232 * Can we get a lock in this state if the holder counts are
3233 * zero? The meta data unblock code used to check this.
3235 if ((lockres
->l_ops
->flags
& LOCK_TYPE_REQUIRES_REFRESH
)
3236 && (lockres
->l_flags
& OCFS2_LOCK_REFRESHING
))
3239 new_level
= ocfs2_highest_compat_lock_level(lockres
->l_blocking
);
3241 if (lockres
->l_ops
->check_downconvert
3242 && !lockres
->l_ops
->check_downconvert(lockres
, new_level
))
3245 /* If we get here, then we know that there are no more
3246 * incompatible holders (and anyone asking for an incompatible
3247 * lock is blocked). We can now downconvert the lock */
3248 if (!lockres
->l_ops
->downconvert_worker
)
3251 /* Some lockres types want to do a bit of work before
3252 * downconverting a lock. Allow that here. The worker function
3253 * may sleep, so we save off a copy of what we're blocking as
3254 * it may change while we're not holding the spin lock. */
3255 blocking
= lockres
->l_blocking
;
3256 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3258 ctl
->unblock_action
= lockres
->l_ops
->downconvert_worker(lockres
, blocking
);
3260 if (ctl
->unblock_action
== UNBLOCK_STOP_POST
)
3263 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3264 if (blocking
!= lockres
->l_blocking
) {
3265 /* If this changed underneath us, then we can't drop
3273 if (lockres
->l_ops
->flags
& LOCK_TYPE_USES_LVB
) {
3274 if (lockres
->l_level
== DLM_LOCK_EX
)
3278 * We only set the lvb if the lock has been fully
3279 * refreshed - otherwise we risk setting stale
3280 * data. Otherwise, there's no need to actually clear
3281 * out the lvb here as it's value is still valid.
3283 if (set_lvb
&& !(lockres
->l_flags
& OCFS2_LOCK_NEEDS_REFRESH
))
3284 lockres
->l_ops
->set_lvb(lockres
);
3287 gen
= ocfs2_prepare_downconvert(lockres
, new_level
);
3288 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3289 ret
= ocfs2_downconvert_lock(osb
, lockres
, new_level
, set_lvb
,
3297 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3304 static int ocfs2_data_convert_worker(struct ocfs2_lock_res
*lockres
,
3307 struct inode
*inode
;
3308 struct address_space
*mapping
;
3310 inode
= ocfs2_lock_res_inode(lockres
);
3311 mapping
= inode
->i_mapping
;
3313 if (!S_ISREG(inode
->i_mode
))
3317 * We need this before the filemap_fdatawrite() so that it can
3318 * transfer the dirty bit from the PTE to the
3319 * page. Unfortunately this means that even for EX->PR
3320 * downconverts, we'll lose our mappings and have to build
3323 unmap_mapping_range(mapping
, 0, 0, 0);
3325 if (filemap_fdatawrite(mapping
)) {
3326 mlog(ML_ERROR
, "Could not sync inode %llu for downconvert!",
3327 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
3329 sync_mapping_buffers(mapping
);
3330 if (blocking
== DLM_LOCK_EX
) {
3331 truncate_inode_pages(mapping
, 0);
3333 /* We only need to wait on the I/O if we're not also
3334 * truncating pages because truncate_inode_pages waits
3335 * for us above. We don't truncate pages if we're
3336 * blocking anything < EXMODE because we want to keep
3337 * them around in that case. */
3338 filemap_fdatawait(mapping
);
3342 return UNBLOCK_CONTINUE
;
3345 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res
*lockres
,
3348 struct inode
*inode
= ocfs2_lock_res_inode(lockres
);
3349 int checkpointed
= ocfs2_inode_fully_checkpointed(inode
);
3351 BUG_ON(new_level
!= DLM_LOCK_NL
&& new_level
!= DLM_LOCK_PR
);
3352 BUG_ON(lockres
->l_level
!= DLM_LOCK_EX
&& !checkpointed
);
3357 ocfs2_start_checkpoint(OCFS2_SB(inode
->i_sb
));
3361 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res
*lockres
)
3363 struct inode
*inode
= ocfs2_lock_res_inode(lockres
);
3365 __ocfs2_stuff_meta_lvb(inode
);
3369 * Does the final reference drop on our dentry lock. Right now this
3370 * happens in the downconvert thread, but we could choose to simplify the
3371 * dlmglue API and push these off to the ocfs2_wq in the future.
3373 static void ocfs2_dentry_post_unlock(struct ocfs2_super
*osb
,
3374 struct ocfs2_lock_res
*lockres
)
3376 struct ocfs2_dentry_lock
*dl
= ocfs2_lock_res_dl(lockres
);
3377 ocfs2_dentry_lock_put(osb
, dl
);
3381 * d_delete() matching dentries before the lock downconvert.
3383 * At this point, any process waiting to destroy the
3384 * dentry_lock due to last ref count is stopped by the
3385 * OCFS2_LOCK_QUEUED flag.
3387 * We have two potential problems
3389 * 1) If we do the last reference drop on our dentry_lock (via dput)
3390 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3391 * the downconvert to finish. Instead we take an elevated
3392 * reference and push the drop until after we've completed our
3393 * unblock processing.
3395 * 2) There might be another process with a final reference,
3396 * waiting on us to finish processing. If this is the case, we
3397 * detect it and exit out - there's no more dentries anyway.
3399 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res
*lockres
,
3402 struct ocfs2_dentry_lock
*dl
= ocfs2_lock_res_dl(lockres
);
3403 struct ocfs2_inode_info
*oi
= OCFS2_I(dl
->dl_inode
);
3404 struct dentry
*dentry
;
3405 unsigned long flags
;
3409 * This node is blocking another node from getting a read
3410 * lock. This happens when we've renamed within a
3411 * directory. We've forced the other nodes to d_delete(), but
3412 * we never actually dropped our lock because it's still
3413 * valid. The downconvert code will retain a PR for this node,
3414 * so there's no further work to do.
3416 if (blocking
== DLM_LOCK_PR
)
3417 return UNBLOCK_CONTINUE
;
3420 * Mark this inode as potentially orphaned. The code in
3421 * ocfs2_delete_inode() will figure out whether it actually
3422 * needs to be freed or not.
3424 spin_lock(&oi
->ip_lock
);
3425 oi
->ip_flags
|= OCFS2_INODE_MAYBE_ORPHANED
;
3426 spin_unlock(&oi
->ip_lock
);
3429 * Yuck. We need to make sure however that the check of
3430 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3431 * respect to a reference decrement or the setting of that
3434 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3435 spin_lock(&dentry_attach_lock
);
3436 if (!(lockres
->l_flags
& OCFS2_LOCK_FREEING
)
3441 spin_unlock(&dentry_attach_lock
);
3442 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3444 mlog(0, "extra_ref = %d\n", extra_ref
);
3447 * We have a process waiting on us in ocfs2_dentry_iput(),
3448 * which means we can't have any more outstanding
3449 * aliases. There's no need to do any more work.
3452 return UNBLOCK_CONTINUE
;
3454 spin_lock(&dentry_attach_lock
);
3456 dentry
= ocfs2_find_local_alias(dl
->dl_inode
,
3457 dl
->dl_parent_blkno
, 1);
3460 spin_unlock(&dentry_attach_lock
);
3462 mlog(0, "d_delete(%.*s);\n", dentry
->d_name
.len
,
3463 dentry
->d_name
.name
);
3466 * The following dcache calls may do an
3467 * iput(). Normally we don't want that from the
3468 * downconverting thread, but in this case it's ok
3469 * because the requesting node already has an
3470 * exclusive lock on the inode, so it can't be queued
3471 * for a downconvert.
3476 spin_lock(&dentry_attach_lock
);
3478 spin_unlock(&dentry_attach_lock
);
3481 * If we are the last holder of this dentry lock, there is no
3482 * reason to downconvert so skip straight to the unlock.
3484 if (dl
->dl_count
== 1)
3485 return UNBLOCK_STOP_POST
;
3487 return UNBLOCK_CONTINUE_POST
;
3490 static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res
*lockres
)
3492 struct ocfs2_qinfo_lvb
*lvb
;
3493 struct ocfs2_mem_dqinfo
*oinfo
= ocfs2_lock_res_qinfo(lockres
);
3494 struct mem_dqinfo
*info
= sb_dqinfo(oinfo
->dqi_gi
.dqi_sb
,
3495 oinfo
->dqi_gi
.dqi_type
);
3499 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
3500 lvb
->lvb_version
= OCFS2_QINFO_LVB_VERSION
;
3501 lvb
->lvb_bgrace
= cpu_to_be32(info
->dqi_bgrace
);
3502 lvb
->lvb_igrace
= cpu_to_be32(info
->dqi_igrace
);
3503 lvb
->lvb_syncms
= cpu_to_be32(oinfo
->dqi_syncms
);
3504 lvb
->lvb_blocks
= cpu_to_be32(oinfo
->dqi_gi
.dqi_blocks
);
3505 lvb
->lvb_free_blk
= cpu_to_be32(oinfo
->dqi_gi
.dqi_free_blk
);
3506 lvb
->lvb_free_entry
= cpu_to_be32(oinfo
->dqi_gi
.dqi_free_entry
);
3511 void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo
*oinfo
, int ex
)
3513 struct ocfs2_lock_res
*lockres
= &oinfo
->dqi_gqlock
;
3514 struct ocfs2_super
*osb
= OCFS2_SB(oinfo
->dqi_gi
.dqi_sb
);
3515 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
3518 if (!ocfs2_is_hard_readonly(osb
) && !ocfs2_mount_local(osb
))
3519 ocfs2_cluster_unlock(osb
, lockres
, level
);
3523 static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo
*oinfo
)
3525 struct mem_dqinfo
*info
= sb_dqinfo(oinfo
->dqi_gi
.dqi_sb
,
3526 oinfo
->dqi_gi
.dqi_type
);
3527 struct ocfs2_lock_res
*lockres
= &oinfo
->dqi_gqlock
;
3528 struct ocfs2_qinfo_lvb
*lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
3529 struct buffer_head
*bh
= NULL
;
3530 struct ocfs2_global_disk_dqinfo
*gdinfo
;
3533 if (lvb
->lvb_version
== OCFS2_QINFO_LVB_VERSION
) {
3534 info
->dqi_bgrace
= be32_to_cpu(lvb
->lvb_bgrace
);
3535 info
->dqi_igrace
= be32_to_cpu(lvb
->lvb_igrace
);
3536 oinfo
->dqi_syncms
= be32_to_cpu(lvb
->lvb_syncms
);
3537 oinfo
->dqi_gi
.dqi_blocks
= be32_to_cpu(lvb
->lvb_blocks
);
3538 oinfo
->dqi_gi
.dqi_free_blk
= be32_to_cpu(lvb
->lvb_free_blk
);
3539 oinfo
->dqi_gi
.dqi_free_entry
=
3540 be32_to_cpu(lvb
->lvb_free_entry
);
3542 status
= ocfs2_read_quota_block(oinfo
->dqi_gqinode
, 0, &bh
);
3547 gdinfo
= (struct ocfs2_global_disk_dqinfo
*)
3548 (bh
->b_data
+ OCFS2_GLOBAL_INFO_OFF
);
3549 info
->dqi_bgrace
= le32_to_cpu(gdinfo
->dqi_bgrace
);
3550 info
->dqi_igrace
= le32_to_cpu(gdinfo
->dqi_igrace
);
3551 oinfo
->dqi_syncms
= le32_to_cpu(gdinfo
->dqi_syncms
);
3552 oinfo
->dqi_gi
.dqi_blocks
= le32_to_cpu(gdinfo
->dqi_blocks
);
3553 oinfo
->dqi_gi
.dqi_free_blk
= le32_to_cpu(gdinfo
->dqi_free_blk
);
3554 oinfo
->dqi_gi
.dqi_free_entry
=
3555 le32_to_cpu(gdinfo
->dqi_free_entry
);
3557 ocfs2_track_lock_refresh(lockres
);
3564 /* Lock quota info, this function expects at least shared lock on the quota file
3565 * so that we can safely refresh quota info from disk. */
3566 int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo
*oinfo
, int ex
)
3568 struct ocfs2_lock_res
*lockres
= &oinfo
->dqi_gqlock
;
3569 struct ocfs2_super
*osb
= OCFS2_SB(oinfo
->dqi_gi
.dqi_sb
);
3570 int level
= ex
? DLM_LOCK_EX
: DLM_LOCK_PR
;
3575 /* On RO devices, locking really isn't needed... */
3576 if (ocfs2_is_hard_readonly(osb
)) {
3581 if (ocfs2_mount_local(osb
))
3584 status
= ocfs2_cluster_lock(osb
, lockres
, level
, 0, 0);
3589 if (!ocfs2_should_refresh_lock_res(lockres
))
3591 /* OK, we have the lock but we need to refresh the quota info */
3592 status
= ocfs2_refresh_qinfo(oinfo
);
3594 ocfs2_qinfo_unlock(oinfo
, ex
);
3595 ocfs2_complete_lock_res_refresh(lockres
, status
);
3602 * This is the filesystem locking protocol. It provides the lock handling
3603 * hooks for the underlying DLM. It has a maximum version number.
3604 * The version number allows interoperability with systems running at
3605 * the same major number and an equal or smaller minor number.
3607 * Whenever the filesystem does new things with locks (adds or removes a
3608 * lock, orders them differently, does different things underneath a lock),
3609 * the version must be changed. The protocol is negotiated when joining
3610 * the dlm domain. A node may join the domain if its major version is
3611 * identical to all other nodes and its minor version is greater than
3612 * or equal to all other nodes. When its minor version is greater than
3613 * the other nodes, it will run at the minor version specified by the
3616 * If a locking change is made that will not be compatible with older
3617 * versions, the major number must be increased and the minor version set
3618 * to zero. If a change merely adds a behavior that can be disabled when
3619 * speaking to older versions, the minor version must be increased. If a
3620 * change adds a fully backwards compatible change (eg, LVB changes that
3621 * are just ignored by older versions), the version does not need to be
3624 static struct ocfs2_locking_protocol lproto
= {
3626 .pv_major
= OCFS2_LOCKING_PROTOCOL_MAJOR
,
3627 .pv_minor
= OCFS2_LOCKING_PROTOCOL_MINOR
,
3629 .lp_lock_ast
= ocfs2_locking_ast
,
3630 .lp_blocking_ast
= ocfs2_blocking_ast
,
3631 .lp_unlock_ast
= ocfs2_unlock_ast
,
3634 void ocfs2_set_locking_protocol(void)
3636 ocfs2_stack_glue_set_locking_protocol(&lproto
);
3640 static void ocfs2_process_blocked_lock(struct ocfs2_super
*osb
,
3641 struct ocfs2_lock_res
*lockres
)
3644 struct ocfs2_unblock_ctl ctl
= {0, 0,};
3645 unsigned long flags
;
3647 /* Our reference to the lockres in this function can be
3648 * considered valid until we remove the OCFS2_LOCK_QUEUED
3654 BUG_ON(!lockres
->l_ops
);
3656 mlog(0, "lockres %s blocked.\n", lockres
->l_name
);
3658 /* Detect whether a lock has been marked as going away while
3659 * the downconvert thread was processing other things. A lock can
3660 * still be marked with OCFS2_LOCK_FREEING after this check,
3661 * but short circuiting here will still save us some
3663 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3664 if (lockres
->l_flags
& OCFS2_LOCK_FREEING
)
3666 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3668 status
= ocfs2_unblock_lock(osb
, lockres
, &ctl
);
3672 spin_lock_irqsave(&lockres
->l_lock
, flags
);
3674 if (lockres
->l_flags
& OCFS2_LOCK_FREEING
|| !ctl
.requeue
) {
3675 lockres_clear_flags(lockres
, OCFS2_LOCK_QUEUED
);
3677 ocfs2_schedule_blocked_lock(osb
, lockres
);
3679 mlog(0, "lockres %s, requeue = %s.\n", lockres
->l_name
,
3680 ctl
.requeue
? "yes" : "no");
3681 spin_unlock_irqrestore(&lockres
->l_lock
, flags
);
3683 if (ctl
.unblock_action
!= UNBLOCK_CONTINUE
3684 && lockres
->l_ops
->post_unlock
)
3685 lockres
->l_ops
->post_unlock(osb
, lockres
);
3690 static void ocfs2_schedule_blocked_lock(struct ocfs2_super
*osb
,
3691 struct ocfs2_lock_res
*lockres
)
3695 assert_spin_locked(&lockres
->l_lock
);
3697 if (lockres
->l_flags
& OCFS2_LOCK_FREEING
) {
3698 /* Do not schedule a lock for downconvert when it's on
3699 * the way to destruction - any nodes wanting access
3700 * to the resource will get it soon. */
3701 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3702 lockres
->l_name
, lockres
->l_flags
);
3706 lockres_or_flags(lockres
, OCFS2_LOCK_QUEUED
);
3708 spin_lock(&osb
->dc_task_lock
);
3709 if (list_empty(&lockres
->l_blocked_list
)) {
3710 list_add_tail(&lockres
->l_blocked_list
,
3711 &osb
->blocked_lock_list
);
3712 osb
->blocked_lock_count
++;
3714 spin_unlock(&osb
->dc_task_lock
);
3719 static void ocfs2_downconvert_thread_do_work(struct ocfs2_super
*osb
)
3721 unsigned long processed
;
3722 struct ocfs2_lock_res
*lockres
;
3726 spin_lock(&osb
->dc_task_lock
);
3727 /* grab this early so we know to try again if a state change and
3728 * wake happens part-way through our work */
3729 osb
->dc_work_sequence
= osb
->dc_wake_sequence
;
3731 processed
= osb
->blocked_lock_count
;
3733 BUG_ON(list_empty(&osb
->blocked_lock_list
));
3735 lockres
= list_entry(osb
->blocked_lock_list
.next
,
3736 struct ocfs2_lock_res
, l_blocked_list
);
3737 list_del_init(&lockres
->l_blocked_list
);
3738 osb
->blocked_lock_count
--;
3739 spin_unlock(&osb
->dc_task_lock
);
3744 ocfs2_process_blocked_lock(osb
, lockres
);
3746 spin_lock(&osb
->dc_task_lock
);
3748 spin_unlock(&osb
->dc_task_lock
);
3753 static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super
*osb
)
3757 spin_lock(&osb
->dc_task_lock
);
3758 if (list_empty(&osb
->blocked_lock_list
))
3761 spin_unlock(&osb
->dc_task_lock
);
3765 static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super
*osb
)
3767 int should_wake
= 0;
3769 spin_lock(&osb
->dc_task_lock
);
3770 if (osb
->dc_work_sequence
!= osb
->dc_wake_sequence
)
3772 spin_unlock(&osb
->dc_task_lock
);
3777 static int ocfs2_downconvert_thread(void *arg
)
3780 struct ocfs2_super
*osb
= arg
;
3782 /* only quit once we've been asked to stop and there is no more
3784 while (!(kthread_should_stop() &&
3785 ocfs2_downconvert_thread_lists_empty(osb
))) {
3787 wait_event_interruptible(osb
->dc_event
,
3788 ocfs2_downconvert_thread_should_wake(osb
) ||
3789 kthread_should_stop());
3791 mlog(0, "downconvert_thread: awoken\n");
3793 ocfs2_downconvert_thread_do_work(osb
);
3796 osb
->dc_task
= NULL
;
3800 void ocfs2_wake_downconvert_thread(struct ocfs2_super
*osb
)
3802 spin_lock(&osb
->dc_task_lock
);
3803 /* make sure the voting thread gets a swipe at whatever changes
3804 * the caller may have made to the voting state */
3805 osb
->dc_wake_sequence
++;
3806 spin_unlock(&osb
->dc_task_lock
);
3807 wake_up(&osb
->dc_event
);