2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/buffer_head.h>
14 #include <linux/delay.h>
15 #include <linux/sort.h>
16 #include <linux/jhash.h>
17 #include <linux/kallsyms.h>
18 #include <linux/gfs2_ondisk.h>
19 #include <linux/list.h>
20 #include <linux/wait.h>
21 #include <linux/module.h>
22 #include <asm/uaccess.h>
23 #include <linux/seq_file.h>
24 #include <linux/debugfs.h>
25 #include <linux/kthread.h>
26 #include <linux/freezer.h>
27 #include <linux/workqueue.h>
28 #include <linux/jiffies.h>
29 #include <linux/rcupdate.h>
30 #include <linux/rculist_bl.h>
31 #include <linux/bit_spinlock.h>
44 #define CREATE_TRACE_POINTS
45 #include "trace_gfs2.h"
47 struct gfs2_glock_iter
{
48 int hash
; /* hash bucket index */
49 struct gfs2_sbd
*sdp
; /* incore superblock */
50 struct gfs2_glock
*gl
; /* current glock struct */
51 char string
[512]; /* scratch space */
54 typedef void (*glock_examiner
) (struct gfs2_glock
* gl
);
56 static int __dump_glock(struct seq_file
*seq
, const struct gfs2_glock
*gl
);
57 #define GLOCK_BUG_ON(gl,x) do { if (unlikely(x)) { __dump_glock(NULL, gl); BUG(); } } while(0)
58 static void do_xmote(struct gfs2_glock
*gl
, struct gfs2_holder
*gh
, unsigned int target
);
60 static struct dentry
*gfs2_root
;
61 static struct workqueue_struct
*glock_workqueue
;
62 struct workqueue_struct
*gfs2_delete_workqueue
;
63 static LIST_HEAD(lru_list
);
64 static atomic_t lru_count
= ATOMIC_INIT(0);
65 static DEFINE_SPINLOCK(lru_lock
);
67 #define GFS2_GL_HASH_SHIFT 15
68 #define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT)
69 #define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1)
71 static struct hlist_bl_head gl_hash_table
[GFS2_GL_HASH_SIZE
];
72 static struct dentry
*gfs2_root
;
75 * gl_hash() - Turn glock number into hash bucket number
76 * @lock: The glock number
78 * Returns: The number of the corresponding hash bucket
81 static unsigned int gl_hash(const struct gfs2_sbd
*sdp
,
82 const struct lm_lockname
*name
)
86 h
= jhash(&name
->ln_number
, sizeof(u64
), 0);
87 h
= jhash(&name
->ln_type
, sizeof(unsigned int), h
);
88 h
= jhash(&sdp
, sizeof(struct gfs2_sbd
*), h
);
89 h
&= GFS2_GL_HASH_MASK
;
94 static inline void spin_lock_bucket(unsigned int hash
)
96 struct hlist_bl_head
*bl
= &gl_hash_table
[hash
];
97 bit_spin_lock(0, (unsigned long *)bl
);
100 static inline void spin_unlock_bucket(unsigned int hash
)
102 struct hlist_bl_head
*bl
= &gl_hash_table
[hash
];
103 __bit_spin_unlock(0, (unsigned long *)bl
);
106 static void gfs2_glock_dealloc(struct rcu_head
*rcu
)
108 struct gfs2_glock
*gl
= container_of(rcu
, struct gfs2_glock
, gl_rcu
);
110 if (gl
->gl_ops
->go_flags
& GLOF_ASPACE
)
111 kmem_cache_free(gfs2_glock_aspace_cachep
, gl
);
113 kmem_cache_free(gfs2_glock_cachep
, gl
);
116 void gfs2_glock_free(struct gfs2_glock
*gl
)
118 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
120 call_rcu(&gl
->gl_rcu
, gfs2_glock_dealloc
);
121 if (atomic_dec_and_test(&sdp
->sd_glock_disposal
))
122 wake_up(&sdp
->sd_glock_wait
);
126 * gfs2_glock_hold() - increment reference count on glock
127 * @gl: The glock to hold
131 void gfs2_glock_hold(struct gfs2_glock
*gl
)
133 GLOCK_BUG_ON(gl
, atomic_read(&gl
->gl_ref
) == 0);
134 atomic_inc(&gl
->gl_ref
);
138 * demote_ok - Check to see if it's ok to unlock a glock
141 * Returns: 1 if it's ok
144 static int demote_ok(const struct gfs2_glock
*gl
)
146 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
148 if (gl
->gl_state
== LM_ST_UNLOCKED
)
150 if (!list_empty(&gl
->gl_holders
))
152 if (glops
->go_demote_ok
)
153 return glops
->go_demote_ok(gl
);
158 void gfs2_glock_add_to_lru(struct gfs2_glock
*gl
)
160 spin_lock(&lru_lock
);
162 if (!list_empty(&gl
->gl_lru
))
163 list_del_init(&gl
->gl_lru
);
165 atomic_inc(&lru_count
);
167 list_add_tail(&gl
->gl_lru
, &lru_list
);
168 set_bit(GLF_LRU
, &gl
->gl_flags
);
169 spin_unlock(&lru_lock
);
172 static void gfs2_glock_remove_from_lru(struct gfs2_glock
*gl
)
174 spin_lock(&lru_lock
);
175 if (!list_empty(&gl
->gl_lru
)) {
176 list_del_init(&gl
->gl_lru
);
177 atomic_dec(&lru_count
);
178 clear_bit(GLF_LRU
, &gl
->gl_flags
);
180 spin_unlock(&lru_lock
);
184 * __gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
187 * If the glock is demotable, then we add it (or move it) to the end
188 * of the glock LRU list.
191 static void __gfs2_glock_schedule_for_reclaim(struct gfs2_glock
*gl
)
194 gfs2_glock_add_to_lru(gl
);
198 * gfs2_glock_put_nolock() - Decrement reference count on glock
199 * @gl: The glock to put
201 * This function should only be used if the caller has its own reference
202 * to the glock, in addition to the one it is dropping.
205 void gfs2_glock_put_nolock(struct gfs2_glock
*gl
)
207 if (atomic_dec_and_test(&gl
->gl_ref
))
212 * gfs2_glock_put() - Decrement reference count on glock
213 * @gl: The glock to put
217 void gfs2_glock_put(struct gfs2_glock
*gl
)
219 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
220 struct address_space
*mapping
= gfs2_glock2aspace(gl
);
222 if (atomic_dec_and_test(&gl
->gl_ref
)) {
223 spin_lock_bucket(gl
->gl_hash
);
224 hlist_bl_del_rcu(&gl
->gl_list
);
225 spin_unlock_bucket(gl
->gl_hash
);
226 gfs2_glock_remove_from_lru(gl
);
227 GLOCK_BUG_ON(gl
, !list_empty(&gl
->gl_holders
));
228 GLOCK_BUG_ON(gl
, mapping
&& mapping
->nrpages
);
229 trace_gfs2_glock_put(gl
);
230 sdp
->sd_lockstruct
.ls_ops
->lm_put_lock(gl
);
235 * search_bucket() - Find struct gfs2_glock by lock number
236 * @bucket: the bucket to search
237 * @name: The lock name
239 * Returns: NULL, or the struct gfs2_glock with the requested number
242 static struct gfs2_glock
*search_bucket(unsigned int hash
,
243 const struct gfs2_sbd
*sdp
,
244 const struct lm_lockname
*name
)
246 struct gfs2_glock
*gl
;
247 struct hlist_bl_node
*h
;
249 hlist_bl_for_each_entry_rcu(gl
, h
, &gl_hash_table
[hash
], gl_list
) {
250 if (!lm_name_equal(&gl
->gl_name
, name
))
252 if (gl
->gl_sbd
!= sdp
)
254 if (atomic_inc_not_zero(&gl
->gl_ref
))
262 * may_grant - check if its ok to grant a new lock
264 * @gh: The lock request which we wish to grant
266 * Returns: true if its ok to grant the lock
269 static inline int may_grant(const struct gfs2_glock
*gl
, const struct gfs2_holder
*gh
)
271 const struct gfs2_holder
*gh_head
= list_entry(gl
->gl_holders
.next
, const struct gfs2_holder
, gh_list
);
272 if ((gh
->gh_state
== LM_ST_EXCLUSIVE
||
273 gh_head
->gh_state
== LM_ST_EXCLUSIVE
) && gh
!= gh_head
)
275 if (gl
->gl_state
== gh
->gh_state
)
277 if (gh
->gh_flags
& GL_EXACT
)
279 if (gl
->gl_state
== LM_ST_EXCLUSIVE
) {
280 if (gh
->gh_state
== LM_ST_SHARED
&& gh_head
->gh_state
== LM_ST_SHARED
)
282 if (gh
->gh_state
== LM_ST_DEFERRED
&& gh_head
->gh_state
== LM_ST_DEFERRED
)
285 if (gl
->gl_state
!= LM_ST_UNLOCKED
&& (gh
->gh_flags
& LM_FLAG_ANY
))
290 static void gfs2_holder_wake(struct gfs2_holder
*gh
)
292 clear_bit(HIF_WAIT
, &gh
->gh_iflags
);
293 smp_mb__after_clear_bit();
294 wake_up_bit(&gh
->gh_iflags
, HIF_WAIT
);
298 * do_error - Something unexpected has happened during a lock request
302 static inline void do_error(struct gfs2_glock
*gl
, const int ret
)
304 struct gfs2_holder
*gh
, *tmp
;
306 list_for_each_entry_safe(gh
, tmp
, &gl
->gl_holders
, gh_list
) {
307 if (test_bit(HIF_HOLDER
, &gh
->gh_iflags
))
309 if (ret
& LM_OUT_ERROR
)
311 else if (gh
->gh_flags
& (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
))
312 gh
->gh_error
= GLR_TRYFAILED
;
315 list_del_init(&gh
->gh_list
);
316 trace_gfs2_glock_queue(gh
, 0);
317 gfs2_holder_wake(gh
);
322 * do_promote - promote as many requests as possible on the current queue
325 * Returns: 1 if there is a blocked holder at the head of the list, or 2
326 * if a type specific operation is underway.
329 static int do_promote(struct gfs2_glock
*gl
)
330 __releases(&gl
->gl_spin
)
331 __acquires(&gl
->gl_spin
)
333 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
334 struct gfs2_holder
*gh
, *tmp
;
338 list_for_each_entry_safe(gh
, tmp
, &gl
->gl_holders
, gh_list
) {
339 if (test_bit(HIF_HOLDER
, &gh
->gh_iflags
))
341 if (may_grant(gl
, gh
)) {
342 if (gh
->gh_list
.prev
== &gl
->gl_holders
&&
344 spin_unlock(&gl
->gl_spin
);
345 /* FIXME: eliminate this eventually */
346 ret
= glops
->go_lock(gh
);
347 spin_lock(&gl
->gl_spin
);
352 list_del_init(&gh
->gh_list
);
353 trace_gfs2_glock_queue(gh
, 0);
354 gfs2_holder_wake(gh
);
357 set_bit(HIF_HOLDER
, &gh
->gh_iflags
);
358 trace_gfs2_promote(gh
, 1);
359 gfs2_holder_wake(gh
);
362 set_bit(HIF_HOLDER
, &gh
->gh_iflags
);
363 trace_gfs2_promote(gh
, 0);
364 gfs2_holder_wake(gh
);
367 if (gh
->gh_list
.prev
== &gl
->gl_holders
)
376 * find_first_waiter - find the first gh that's waiting for the glock
380 static inline struct gfs2_holder
*find_first_waiter(const struct gfs2_glock
*gl
)
382 struct gfs2_holder
*gh
;
384 list_for_each_entry(gh
, &gl
->gl_holders
, gh_list
) {
385 if (!test_bit(HIF_HOLDER
, &gh
->gh_iflags
))
392 * state_change - record that the glock is now in a different state
394 * @new_state the new state
398 static void state_change(struct gfs2_glock
*gl
, unsigned int new_state
)
402 held1
= (gl
->gl_state
!= LM_ST_UNLOCKED
);
403 held2
= (new_state
!= LM_ST_UNLOCKED
);
405 if (held1
!= held2
) {
409 gfs2_glock_put_nolock(gl
);
411 if (held1
&& held2
&& list_empty(&gl
->gl_holders
))
412 clear_bit(GLF_QUEUED
, &gl
->gl_flags
);
414 gl
->gl_state
= new_state
;
415 gl
->gl_tchange
= jiffies
;
418 static void gfs2_demote_wake(struct gfs2_glock
*gl
)
420 gl
->gl_demote_state
= LM_ST_EXCLUSIVE
;
421 clear_bit(GLF_DEMOTE
, &gl
->gl_flags
);
422 smp_mb__after_clear_bit();
423 wake_up_bit(&gl
->gl_flags
, GLF_DEMOTE
);
427 * finish_xmote - The DLM has replied to one of our lock requests
429 * @ret: The status from the DLM
433 static void finish_xmote(struct gfs2_glock
*gl
, unsigned int ret
)
435 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
436 struct gfs2_holder
*gh
;
437 unsigned state
= ret
& LM_OUT_ST_MASK
;
440 spin_lock(&gl
->gl_spin
);
441 trace_gfs2_glock_state_change(gl
, state
);
442 state_change(gl
, state
);
443 gh
= find_first_waiter(gl
);
445 /* Demote to UN request arrived during demote to SH or DF */
446 if (test_bit(GLF_DEMOTE_IN_PROGRESS
, &gl
->gl_flags
) &&
447 state
!= LM_ST_UNLOCKED
&& gl
->gl_demote_state
== LM_ST_UNLOCKED
)
448 gl
->gl_target
= LM_ST_UNLOCKED
;
450 /* Check for state != intended state */
451 if (unlikely(state
!= gl
->gl_target
)) {
452 if (gh
&& !test_bit(GLF_DEMOTE_IN_PROGRESS
, &gl
->gl_flags
)) {
453 /* move to back of queue and try next entry */
454 if (ret
& LM_OUT_CANCELED
) {
455 if ((gh
->gh_flags
& LM_FLAG_PRIORITY
) == 0)
456 list_move_tail(&gh
->gh_list
, &gl
->gl_holders
);
457 gh
= find_first_waiter(gl
);
458 gl
->gl_target
= gh
->gh_state
;
461 /* Some error or failed "try lock" - report it */
462 if ((ret
& LM_OUT_ERROR
) ||
463 (gh
->gh_flags
& (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
))) {
464 gl
->gl_target
= gl
->gl_state
;
470 /* Unlocked due to conversion deadlock, try again */
473 do_xmote(gl
, gh
, gl
->gl_target
);
475 /* Conversion fails, unlock and try again */
478 do_xmote(gl
, gh
, LM_ST_UNLOCKED
);
480 default: /* Everything else */
481 printk(KERN_ERR
"GFS2: wanted %u got %u\n", gl
->gl_target
, state
);
484 spin_unlock(&gl
->gl_spin
);
488 /* Fast path - we got what we asked for */
489 if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS
, &gl
->gl_flags
))
490 gfs2_demote_wake(gl
);
491 if (state
!= LM_ST_UNLOCKED
) {
492 if (glops
->go_xmote_bh
) {
493 spin_unlock(&gl
->gl_spin
);
494 rv
= glops
->go_xmote_bh(gl
, gh
);
495 spin_lock(&gl
->gl_spin
);
506 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
508 spin_unlock(&gl
->gl_spin
);
512 * do_xmote - Calls the DLM to change the state of a lock
513 * @gl: The lock state
514 * @gh: The holder (only for promotes)
515 * @target: The target lock state
519 static void do_xmote(struct gfs2_glock
*gl
, struct gfs2_holder
*gh
, unsigned int target
)
520 __releases(&gl
->gl_spin
)
521 __acquires(&gl
->gl_spin
)
523 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
524 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
525 unsigned int lck_flags
= gh
? gh
->gh_flags
: 0;
528 lck_flags
&= (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
| LM_FLAG_NOEXP
|
530 GLOCK_BUG_ON(gl
, gl
->gl_state
== target
);
531 GLOCK_BUG_ON(gl
, gl
->gl_state
== gl
->gl_target
);
532 if ((target
== LM_ST_UNLOCKED
|| target
== LM_ST_DEFERRED
) &&
534 set_bit(GLF_INVALIDATE_IN_PROGRESS
, &gl
->gl_flags
);
535 do_error(gl
, 0); /* Fail queued try locks */
538 spin_unlock(&gl
->gl_spin
);
539 if (glops
->go_xmote_th
)
540 glops
->go_xmote_th(gl
);
541 if (test_bit(GLF_INVALIDATE_IN_PROGRESS
, &gl
->gl_flags
))
542 glops
->go_inval(gl
, target
== LM_ST_DEFERRED
? 0 : DIO_METADATA
);
543 clear_bit(GLF_INVALIDATE_IN_PROGRESS
, &gl
->gl_flags
);
546 if (target
!= LM_ST_UNLOCKED
&& (gl
->gl_state
== LM_ST_SHARED
||
547 gl
->gl_state
== LM_ST_DEFERRED
) &&
548 !(lck_flags
& (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
)))
549 lck_flags
|= LM_FLAG_TRY_1CB
;
551 if (sdp
->sd_lockstruct
.ls_ops
->lm_lock
) {
553 ret
= sdp
->sd_lockstruct
.ls_ops
->lm_lock(gl
, target
, lck_flags
);
554 GLOCK_BUG_ON(gl
, ret
);
555 } else { /* lock_nolock */
556 finish_xmote(gl
, target
);
557 if (queue_delayed_work(glock_workqueue
, &gl
->gl_work
, 0) == 0)
561 spin_lock(&gl
->gl_spin
);
565 * find_first_holder - find the first "holder" gh
569 static inline struct gfs2_holder
*find_first_holder(const struct gfs2_glock
*gl
)
571 struct gfs2_holder
*gh
;
573 if (!list_empty(&gl
->gl_holders
)) {
574 gh
= list_entry(gl
->gl_holders
.next
, struct gfs2_holder
, gh_list
);
575 if (test_bit(HIF_HOLDER
, &gh
->gh_iflags
))
582 * run_queue - do all outstanding tasks related to a glock
583 * @gl: The glock in question
584 * @nonblock: True if we must not block in run_queue
588 static void run_queue(struct gfs2_glock
*gl
, const int nonblock
)
589 __releases(&gl
->gl_spin
)
590 __acquires(&gl
->gl_spin
)
592 struct gfs2_holder
*gh
= NULL
;
595 if (test_and_set_bit(GLF_LOCK
, &gl
->gl_flags
))
598 GLOCK_BUG_ON(gl
, test_bit(GLF_DEMOTE_IN_PROGRESS
, &gl
->gl_flags
));
600 if (test_bit(GLF_DEMOTE
, &gl
->gl_flags
) &&
601 gl
->gl_demote_state
!= gl
->gl_state
) {
602 if (find_first_holder(gl
))
606 set_bit(GLF_DEMOTE_IN_PROGRESS
, &gl
->gl_flags
);
607 GLOCK_BUG_ON(gl
, gl
->gl_demote_state
== LM_ST_EXCLUSIVE
);
608 gl
->gl_target
= gl
->gl_demote_state
;
610 if (test_bit(GLF_DEMOTE
, &gl
->gl_flags
))
611 gfs2_demote_wake(gl
);
612 ret
= do_promote(gl
);
617 gh
= find_first_waiter(gl
);
618 gl
->gl_target
= gh
->gh_state
;
619 if (!(gh
->gh_flags
& (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
)))
620 do_error(gl
, 0); /* Fail queued try locks */
622 do_xmote(gl
, gh
, gl
->gl_target
);
627 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
628 smp_mb__after_clear_bit();
630 if (queue_delayed_work(glock_workqueue
, &gl
->gl_work
, 0) == 0)
631 gfs2_glock_put_nolock(gl
);
635 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
636 smp_mb__after_clear_bit();
640 static void delete_work_func(struct work_struct
*work
)
642 struct gfs2_glock
*gl
= container_of(work
, struct gfs2_glock
, gl_delete
);
643 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
644 struct gfs2_inode
*ip
;
646 u64 no_addr
= gl
->gl_name
.ln_number
;
649 /* Note: Unsafe to dereference ip as we don't hold right refs/locks */
652 inode
= gfs2_ilookup(sdp
->sd_vfs
, no_addr
);
654 inode
= gfs2_lookup_by_inum(sdp
, no_addr
, NULL
, GFS2_BLKST_UNLINKED
);
655 if (inode
&& !IS_ERR(inode
)) {
656 d_prune_aliases(inode
);
662 static void glock_work_func(struct work_struct
*work
)
664 unsigned long delay
= 0;
665 struct gfs2_glock
*gl
= container_of(work
, struct gfs2_glock
, gl_work
.work
);
668 if (test_and_clear_bit(GLF_REPLY_PENDING
, &gl
->gl_flags
)) {
669 finish_xmote(gl
, gl
->gl_reply
);
672 spin_lock(&gl
->gl_spin
);
673 if (test_and_clear_bit(GLF_PENDING_DEMOTE
, &gl
->gl_flags
) &&
674 gl
->gl_state
!= LM_ST_UNLOCKED
&&
675 gl
->gl_demote_state
!= LM_ST_EXCLUSIVE
) {
676 unsigned long holdtime
, now
= jiffies
;
677 holdtime
= gl
->gl_tchange
+ gl
->gl_ops
->go_min_hold_time
;
678 if (time_before(now
, holdtime
))
679 delay
= holdtime
- now
;
680 set_bit(delay
? GLF_PENDING_DEMOTE
: GLF_DEMOTE
, &gl
->gl_flags
);
683 spin_unlock(&gl
->gl_spin
);
685 queue_delayed_work(glock_workqueue
, &gl
->gl_work
, delay
) == 0)
692 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
693 * @sdp: The GFS2 superblock
694 * @number: the lock number
695 * @glops: The glock_operations to use
696 * @create: If 0, don't create the glock if it doesn't exist
697 * @glp: the glock is returned here
699 * This does not lock a glock, just finds/creates structures for one.
704 int gfs2_glock_get(struct gfs2_sbd
*sdp
, u64 number
,
705 const struct gfs2_glock_operations
*glops
, int create
,
706 struct gfs2_glock
**glp
)
708 struct super_block
*s
= sdp
->sd_vfs
;
709 struct lm_lockname name
= { .ln_number
= number
, .ln_type
= glops
->go_type
};
710 struct gfs2_glock
*gl
, *tmp
;
711 unsigned int hash
= gl_hash(sdp
, &name
);
712 struct address_space
*mapping
;
713 struct kmem_cache
*cachep
;
716 gl
= search_bucket(hash
, sdp
, &name
);
725 if (glops
->go_flags
& GLOF_ASPACE
)
726 cachep
= gfs2_glock_aspace_cachep
;
728 cachep
= gfs2_glock_cachep
;
729 gl
= kmem_cache_alloc(cachep
, GFP_KERNEL
);
733 atomic_inc(&sdp
->sd_glock_disposal
);
736 atomic_set(&gl
->gl_ref
, 1);
737 gl
->gl_state
= LM_ST_UNLOCKED
;
738 gl
->gl_target
= LM_ST_UNLOCKED
;
739 gl
->gl_demote_state
= LM_ST_EXCLUSIVE
;
742 snprintf(gl
->gl_strname
, GDLM_STRNAME_BYTES
, "%8x%16llx", name
.ln_type
, (unsigned long long)number
);
743 memset(&gl
->gl_lksb
, 0, sizeof(struct dlm_lksb
));
744 gl
->gl_lksb
.sb_lvbptr
= gl
->gl_lvb
;
745 gl
->gl_tchange
= jiffies
;
746 gl
->gl_object
= NULL
;
748 INIT_DELAYED_WORK(&gl
->gl_work
, glock_work_func
);
749 INIT_WORK(&gl
->gl_delete
, delete_work_func
);
751 mapping
= gfs2_glock2aspace(gl
);
753 mapping
->a_ops
= &gfs2_meta_aops
;
754 mapping
->host
= s
->s_bdev
->bd_inode
;
756 mapping_set_gfp_mask(mapping
, GFP_NOFS
);
757 mapping
->assoc_mapping
= NULL
;
758 mapping
->backing_dev_info
= s
->s_bdi
;
759 mapping
->writeback_index
= 0;
762 spin_lock_bucket(hash
);
763 tmp
= search_bucket(hash
, sdp
, &name
);
765 spin_unlock_bucket(hash
);
766 kmem_cache_free(cachep
, gl
);
767 atomic_dec(&sdp
->sd_glock_disposal
);
770 hlist_bl_add_head_rcu(&gl
->gl_list
, &gl_hash_table
[hash
]);
771 spin_unlock_bucket(hash
);
780 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
782 * @state: the state we're requesting
783 * @flags: the modifier flags
784 * @gh: the holder structure
788 void gfs2_holder_init(struct gfs2_glock
*gl
, unsigned int state
, unsigned flags
,
789 struct gfs2_holder
*gh
)
791 INIT_LIST_HEAD(&gh
->gh_list
);
793 gh
->gh_ip
= (unsigned long)__builtin_return_address(0);
794 gh
->gh_owner_pid
= get_pid(task_pid(current
));
795 gh
->gh_state
= state
;
796 gh
->gh_flags
= flags
;
803 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
804 * @state: the state we're requesting
805 * @flags: the modifier flags
806 * @gh: the holder structure
808 * Don't mess with the glock.
812 void gfs2_holder_reinit(unsigned int state
, unsigned flags
, struct gfs2_holder
*gh
)
814 gh
->gh_state
= state
;
815 gh
->gh_flags
= flags
;
817 gh
->gh_ip
= (unsigned long)__builtin_return_address(0);
818 if (gh
->gh_owner_pid
)
819 put_pid(gh
->gh_owner_pid
);
820 gh
->gh_owner_pid
= get_pid(task_pid(current
));
824 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
825 * @gh: the holder structure
829 void gfs2_holder_uninit(struct gfs2_holder
*gh
)
831 put_pid(gh
->gh_owner_pid
);
832 gfs2_glock_put(gh
->gh_gl
);
838 * gfs2_glock_holder_wait
841 * This function and gfs2_glock_demote_wait both show up in the WCHAN
842 * field. Thus I've separated these otherwise identical functions in
843 * order to be more informative to the user.
846 static int gfs2_glock_holder_wait(void *word
)
852 static int gfs2_glock_demote_wait(void *word
)
858 static void wait_on_holder(struct gfs2_holder
*gh
)
861 wait_on_bit(&gh
->gh_iflags
, HIF_WAIT
, gfs2_glock_holder_wait
, TASK_UNINTERRUPTIBLE
);
864 static void wait_on_demote(struct gfs2_glock
*gl
)
867 wait_on_bit(&gl
->gl_flags
, GLF_DEMOTE
, gfs2_glock_demote_wait
, TASK_UNINTERRUPTIBLE
);
871 * handle_callback - process a demote request
873 * @state: the state the caller wants us to change to
875 * There are only two requests that we are going to see in actual
876 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
879 static void handle_callback(struct gfs2_glock
*gl
, unsigned int state
,
882 int bit
= delay
? GLF_PENDING_DEMOTE
: GLF_DEMOTE
;
884 set_bit(bit
, &gl
->gl_flags
);
885 if (gl
->gl_demote_state
== LM_ST_EXCLUSIVE
) {
886 gl
->gl_demote_state
= state
;
887 gl
->gl_demote_time
= jiffies
;
888 } else if (gl
->gl_demote_state
!= LM_ST_UNLOCKED
&&
889 gl
->gl_demote_state
!= state
) {
890 gl
->gl_demote_state
= LM_ST_UNLOCKED
;
892 if (gl
->gl_ops
->go_callback
)
893 gl
->gl_ops
->go_callback(gl
);
894 trace_gfs2_demote_rq(gl
);
898 * gfs2_glock_wait - wait on a glock acquisition
899 * @gh: the glock holder
901 * Returns: 0 on success
904 int gfs2_glock_wait(struct gfs2_holder
*gh
)
910 void gfs2_print_dbg(struct seq_file
*seq
, const char *fmt
, ...)
912 struct va_format vaf
;
918 struct gfs2_glock_iter
*gi
= seq
->private;
919 vsprintf(gi
->string
, fmt
, args
);
920 seq_printf(seq
, gi
->string
);
925 printk(KERN_ERR
" %pV", &vaf
);
932 * add_to_queue - Add a holder to the wait queue (but look for recursion)
933 * @gh: the holder structure to add
935 * Eventually we should move the recursive locking trap to a
936 * debugging option or something like that. This is the fast
937 * path and needs to have the minimum number of distractions.
941 static inline void add_to_queue(struct gfs2_holder
*gh
)
942 __releases(&gl
->gl_spin
)
943 __acquires(&gl
->gl_spin
)
945 struct gfs2_glock
*gl
= gh
->gh_gl
;
946 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
947 struct list_head
*insert_pt
= NULL
;
948 struct gfs2_holder
*gh2
;
951 BUG_ON(gh
->gh_owner_pid
== NULL
);
952 if (test_and_set_bit(HIF_WAIT
, &gh
->gh_iflags
))
955 if (gh
->gh_flags
& (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
)) {
956 if (test_bit(GLF_LOCK
, &gl
->gl_flags
))
958 if (test_bit(GLF_INVALIDATE_IN_PROGRESS
, &gl
->gl_flags
))
962 list_for_each_entry(gh2
, &gl
->gl_holders
, gh_list
) {
963 if (unlikely(gh2
->gh_owner_pid
== gh
->gh_owner_pid
&&
964 (gh
->gh_gl
->gl_ops
->go_type
!= LM_TYPE_FLOCK
)))
967 !(gh2
->gh_flags
& (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
)) &&
968 !may_grant(gl
, gh
)) {
970 gh
->gh_error
= GLR_TRYFAILED
;
971 gfs2_holder_wake(gh
);
974 if (test_bit(HIF_HOLDER
, &gh2
->gh_iflags
))
976 if (unlikely((gh
->gh_flags
& LM_FLAG_PRIORITY
) && !insert_pt
))
977 insert_pt
= &gh2
->gh_list
;
979 set_bit(GLF_QUEUED
, &gl
->gl_flags
);
980 trace_gfs2_glock_queue(gh
, 1);
981 if (likely(insert_pt
== NULL
)) {
982 list_add_tail(&gh
->gh_list
, &gl
->gl_holders
);
983 if (unlikely(gh
->gh_flags
& LM_FLAG_PRIORITY
))
987 list_add_tail(&gh
->gh_list
, insert_pt
);
989 gh
= list_entry(gl
->gl_holders
.next
, struct gfs2_holder
, gh_list
);
990 if (!(gh
->gh_flags
& LM_FLAG_PRIORITY
)) {
991 spin_unlock(&gl
->gl_spin
);
992 if (sdp
->sd_lockstruct
.ls_ops
->lm_cancel
)
993 sdp
->sd_lockstruct
.ls_ops
->lm_cancel(gl
);
994 spin_lock(&gl
->gl_spin
);
999 print_symbol(KERN_ERR
"original: %s\n", gh2
->gh_ip
);
1000 printk(KERN_ERR
"pid: %d\n", pid_nr(gh2
->gh_owner_pid
));
1001 printk(KERN_ERR
"lock type: %d req lock state : %d\n",
1002 gh2
->gh_gl
->gl_name
.ln_type
, gh2
->gh_state
);
1003 print_symbol(KERN_ERR
"new: %s\n", gh
->gh_ip
);
1004 printk(KERN_ERR
"pid: %d\n", pid_nr(gh
->gh_owner_pid
));
1005 printk(KERN_ERR
"lock type: %d req lock state : %d\n",
1006 gh
->gh_gl
->gl_name
.ln_type
, gh
->gh_state
);
1007 __dump_glock(NULL
, gl
);
1012 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1013 * @gh: the holder structure
1015 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1017 * Returns: 0, GLR_TRYFAILED, or errno on failure
1020 int gfs2_glock_nq(struct gfs2_holder
*gh
)
1022 struct gfs2_glock
*gl
= gh
->gh_gl
;
1023 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1026 if (unlikely(test_bit(SDF_SHUTDOWN
, &sdp
->sd_flags
)))
1029 if (test_bit(GLF_LRU
, &gl
->gl_flags
))
1030 gfs2_glock_remove_from_lru(gl
);
1032 spin_lock(&gl
->gl_spin
);
1034 if ((LM_FLAG_NOEXP
& gh
->gh_flags
) &&
1035 test_and_clear_bit(GLF_FROZEN
, &gl
->gl_flags
))
1036 set_bit(GLF_REPLY_PENDING
, &gl
->gl_flags
);
1038 spin_unlock(&gl
->gl_spin
);
1040 if (!(gh
->gh_flags
& GL_ASYNC
))
1041 error
= gfs2_glock_wait(gh
);
1047 * gfs2_glock_poll - poll to see if an async request has been completed
1050 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1053 int gfs2_glock_poll(struct gfs2_holder
*gh
)
1055 return test_bit(HIF_WAIT
, &gh
->gh_iflags
) ? 0 : 1;
1059 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1060 * @gh: the glock holder
1064 void gfs2_glock_dq(struct gfs2_holder
*gh
)
1066 struct gfs2_glock
*gl
= gh
->gh_gl
;
1067 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1071 spin_lock(&gl
->gl_spin
);
1072 if (gh
->gh_flags
& GL_NOCACHE
)
1073 handle_callback(gl
, LM_ST_UNLOCKED
, 0);
1075 list_del_init(&gh
->gh_list
);
1076 if (find_first_holder(gl
) == NULL
) {
1077 if (glops
->go_unlock
) {
1078 GLOCK_BUG_ON(gl
, test_and_set_bit(GLF_LOCK
, &gl
->gl_flags
));
1079 spin_unlock(&gl
->gl_spin
);
1080 glops
->go_unlock(gh
);
1081 spin_lock(&gl
->gl_spin
);
1082 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
1084 if (list_empty(&gl
->gl_holders
) &&
1085 !test_bit(GLF_PENDING_DEMOTE
, &gl
->gl_flags
) &&
1086 !test_bit(GLF_DEMOTE
, &gl
->gl_flags
))
1089 if (!test_bit(GLF_LFLUSH
, &gl
->gl_flags
))
1090 __gfs2_glock_schedule_for_reclaim(gl
);
1091 trace_gfs2_glock_queue(gh
, 0);
1092 spin_unlock(&gl
->gl_spin
);
1093 if (likely(fast_path
))
1096 gfs2_glock_hold(gl
);
1097 if (test_bit(GLF_PENDING_DEMOTE
, &gl
->gl_flags
) &&
1098 !test_bit(GLF_DEMOTE
, &gl
->gl_flags
))
1099 delay
= gl
->gl_ops
->go_min_hold_time
;
1100 if (queue_delayed_work(glock_workqueue
, &gl
->gl_work
, delay
) == 0)
1104 void gfs2_glock_dq_wait(struct gfs2_holder
*gh
)
1106 struct gfs2_glock
*gl
= gh
->gh_gl
;
1112 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1113 * @gh: the holder structure
1117 void gfs2_glock_dq_uninit(struct gfs2_holder
*gh
)
1120 gfs2_holder_uninit(gh
);
1124 * gfs2_glock_nq_num - acquire a glock based on lock number
1125 * @sdp: the filesystem
1126 * @number: the lock number
1127 * @glops: the glock operations for the type of glock
1128 * @state: the state to acquire the glock in
1129 * @flags: modifier flags for the acquisition
1130 * @gh: the struct gfs2_holder
1135 int gfs2_glock_nq_num(struct gfs2_sbd
*sdp
, u64 number
,
1136 const struct gfs2_glock_operations
*glops
,
1137 unsigned int state
, int flags
, struct gfs2_holder
*gh
)
1139 struct gfs2_glock
*gl
;
1142 error
= gfs2_glock_get(sdp
, number
, glops
, CREATE
, &gl
);
1144 error
= gfs2_glock_nq_init(gl
, state
, flags
, gh
);
1152 * glock_compare - Compare two struct gfs2_glock structures for sorting
1153 * @arg_a: the first structure
1154 * @arg_b: the second structure
1158 static int glock_compare(const void *arg_a
, const void *arg_b
)
1160 const struct gfs2_holder
*gh_a
= *(const struct gfs2_holder
**)arg_a
;
1161 const struct gfs2_holder
*gh_b
= *(const struct gfs2_holder
**)arg_b
;
1162 const struct lm_lockname
*a
= &gh_a
->gh_gl
->gl_name
;
1163 const struct lm_lockname
*b
= &gh_b
->gh_gl
->gl_name
;
1165 if (a
->ln_number
> b
->ln_number
)
1167 if (a
->ln_number
< b
->ln_number
)
1169 BUG_ON(gh_a
->gh_gl
->gl_ops
->go_type
== gh_b
->gh_gl
->gl_ops
->go_type
);
1174 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1175 * @num_gh: the number of structures
1176 * @ghs: an array of struct gfs2_holder structures
1178 * Returns: 0 on success (all glocks acquired),
1179 * errno on failure (no glocks acquired)
1182 static int nq_m_sync(unsigned int num_gh
, struct gfs2_holder
*ghs
,
1183 struct gfs2_holder
**p
)
1188 for (x
= 0; x
< num_gh
; x
++)
1191 sort(p
, num_gh
, sizeof(struct gfs2_holder
*), glock_compare
, NULL
);
1193 for (x
= 0; x
< num_gh
; x
++) {
1194 p
[x
]->gh_flags
&= ~(LM_FLAG_TRY
| GL_ASYNC
);
1196 error
= gfs2_glock_nq(p
[x
]);
1199 gfs2_glock_dq(p
[x
]);
1208 * gfs2_glock_nq_m - acquire multiple glocks
1209 * @num_gh: the number of structures
1210 * @ghs: an array of struct gfs2_holder structures
1213 * Returns: 0 on success (all glocks acquired),
1214 * errno on failure (no glocks acquired)
1217 int gfs2_glock_nq_m(unsigned int num_gh
, struct gfs2_holder
*ghs
)
1219 struct gfs2_holder
*tmp
[4];
1220 struct gfs2_holder
**pph
= tmp
;
1227 ghs
->gh_flags
&= ~(LM_FLAG_TRY
| GL_ASYNC
);
1228 return gfs2_glock_nq(ghs
);
1232 pph
= kmalloc(num_gh
* sizeof(struct gfs2_holder
*), GFP_NOFS
);
1237 error
= nq_m_sync(num_gh
, ghs
, pph
);
1246 * gfs2_glock_dq_m - release multiple glocks
1247 * @num_gh: the number of structures
1248 * @ghs: an array of struct gfs2_holder structures
1252 void gfs2_glock_dq_m(unsigned int num_gh
, struct gfs2_holder
*ghs
)
1255 gfs2_glock_dq(&ghs
[num_gh
]);
1259 * gfs2_glock_dq_uninit_m - release multiple glocks
1260 * @num_gh: the number of structures
1261 * @ghs: an array of struct gfs2_holder structures
1265 void gfs2_glock_dq_uninit_m(unsigned int num_gh
, struct gfs2_holder
*ghs
)
1268 gfs2_glock_dq_uninit(&ghs
[num_gh
]);
1271 void gfs2_glock_cb(struct gfs2_glock
*gl
, unsigned int state
)
1273 unsigned long delay
= 0;
1274 unsigned long holdtime
;
1275 unsigned long now
= jiffies
;
1277 gfs2_glock_hold(gl
);
1278 holdtime
= gl
->gl_tchange
+ gl
->gl_ops
->go_min_hold_time
;
1279 if (test_bit(GLF_QUEUED
, &gl
->gl_flags
)) {
1280 if (time_before(now
, holdtime
))
1281 delay
= holdtime
- now
;
1282 if (test_bit(GLF_REPLY_PENDING
, &gl
->gl_flags
))
1283 delay
= gl
->gl_ops
->go_min_hold_time
;
1286 spin_lock(&gl
->gl_spin
);
1287 handle_callback(gl
, state
, delay
);
1288 spin_unlock(&gl
->gl_spin
);
1289 if (queue_delayed_work(glock_workqueue
, &gl
->gl_work
, delay
) == 0)
1294 * gfs2_should_freeze - Figure out if glock should be frozen
1295 * @gl: The glock in question
1297 * Glocks are not frozen if (a) the result of the dlm operation is
1298 * an error, (b) the locking operation was an unlock operation or
1299 * (c) if there is a "noexp" flagged request anywhere in the queue
1301 * Returns: 1 if freezing should occur, 0 otherwise
1304 static int gfs2_should_freeze(const struct gfs2_glock
*gl
)
1306 const struct gfs2_holder
*gh
;
1308 if (gl
->gl_reply
& ~LM_OUT_ST_MASK
)
1310 if (gl
->gl_target
== LM_ST_UNLOCKED
)
1313 list_for_each_entry(gh
, &gl
->gl_holders
, gh_list
) {
1314 if (test_bit(HIF_HOLDER
, &gh
->gh_iflags
))
1316 if (LM_FLAG_NOEXP
& gh
->gh_flags
)
1324 * gfs2_glock_complete - Callback used by locking
1325 * @gl: Pointer to the glock
1326 * @ret: The return value from the dlm
1328 * The gl_reply field is under the gl_spin lock so that it is ok
1329 * to use a bitfield shared with other glock state fields.
1332 void gfs2_glock_complete(struct gfs2_glock
*gl
, int ret
)
1334 struct lm_lockstruct
*ls
= &gl
->gl_sbd
->sd_lockstruct
;
1336 spin_lock(&gl
->gl_spin
);
1339 if (unlikely(test_bit(DFL_BLOCK_LOCKS
, &ls
->ls_flags
))) {
1340 if (gfs2_should_freeze(gl
)) {
1341 set_bit(GLF_FROZEN
, &gl
->gl_flags
);
1342 spin_unlock(&gl
->gl_spin
);
1347 spin_unlock(&gl
->gl_spin
);
1348 set_bit(GLF_REPLY_PENDING
, &gl
->gl_flags
);
1350 gfs2_glock_hold(gl
);
1351 if (queue_delayed_work(glock_workqueue
, &gl
->gl_work
, 0) == 0)
1356 static int gfs2_shrink_glock_memory(struct shrinker
*shrink
, int nr
, gfp_t gfp_mask
)
1358 struct gfs2_glock
*gl
;
1366 if (!(gfp_mask
& __GFP_FS
))
1369 spin_lock(&lru_lock
);
1370 while(nr
&& !list_empty(&lru_list
)) {
1371 gl
= list_entry(lru_list
.next
, struct gfs2_glock
, gl_lru
);
1372 list_del_init(&gl
->gl_lru
);
1373 clear_bit(GLF_LRU
, &gl
->gl_flags
);
1374 atomic_dec(&lru_count
);
1376 /* Test for being demotable */
1377 if (!test_and_set_bit(GLF_LOCK
, &gl
->gl_flags
)) {
1378 gfs2_glock_hold(gl
);
1379 spin_unlock(&lru_lock
);
1380 spin_lock(&gl
->gl_spin
);
1381 may_demote
= demote_ok(gl
);
1383 handle_callback(gl
, LM_ST_UNLOCKED
, 0);
1386 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
1387 smp_mb__after_clear_bit();
1388 if (queue_delayed_work(glock_workqueue
, &gl
->gl_work
, 0) == 0)
1389 gfs2_glock_put_nolock(gl
);
1390 spin_unlock(&gl
->gl_spin
);
1391 spin_lock(&lru_lock
);
1395 list_add(&gl
->gl_lru
, &skipped
);
1396 set_bit(GLF_LRU
, &gl
->gl_flags
);
1398 list_splice(&skipped
, &lru_list
);
1399 atomic_add(nr_skipped
, &lru_count
);
1400 spin_unlock(&lru_lock
);
1402 return (atomic_read(&lru_count
) / 100) * sysctl_vfs_cache_pressure
;
1405 static struct shrinker glock_shrinker
= {
1406 .shrink
= gfs2_shrink_glock_memory
,
1407 .seeks
= DEFAULT_SEEKS
,
1411 * examine_bucket - Call a function for glock in a hash bucket
1412 * @examiner: the function
1413 * @sdp: the filesystem
1414 * @bucket: the bucket
1418 static void examine_bucket(glock_examiner examiner
, const struct gfs2_sbd
*sdp
,
1421 struct gfs2_glock
*gl
;
1422 struct hlist_bl_head
*head
= &gl_hash_table
[hash
];
1423 struct hlist_bl_node
*pos
;
1426 hlist_bl_for_each_entry_rcu(gl
, pos
, head
, gl_list
) {
1427 if ((gl
->gl_sbd
== sdp
) && atomic_read(&gl
->gl_ref
))
1434 static void glock_hash_walk(glock_examiner examiner
, const struct gfs2_sbd
*sdp
)
1438 for (x
= 0; x
< GFS2_GL_HASH_SIZE
; x
++)
1439 examine_bucket(examiner
, sdp
, x
);
1444 * thaw_glock - thaw out a glock which has an unprocessed reply waiting
1445 * @gl: The glock to thaw
1447 * N.B. When we freeze a glock, we leave a ref to the glock outstanding,
1448 * so this has to result in the ref count being dropped by one.
1451 static void thaw_glock(struct gfs2_glock
*gl
)
1453 if (!test_and_clear_bit(GLF_FROZEN
, &gl
->gl_flags
))
1455 set_bit(GLF_REPLY_PENDING
, &gl
->gl_flags
);
1456 gfs2_glock_hold(gl
);
1457 if (queue_delayed_work(glock_workqueue
, &gl
->gl_work
, 0) == 0)
1462 * clear_glock - look at a glock and see if we can free it from glock cache
1463 * @gl: the glock to look at
1467 static void clear_glock(struct gfs2_glock
*gl
)
1469 gfs2_glock_remove_from_lru(gl
);
1471 spin_lock(&gl
->gl_spin
);
1472 if (gl
->gl_state
!= LM_ST_UNLOCKED
)
1473 handle_callback(gl
, LM_ST_UNLOCKED
, 0);
1474 spin_unlock(&gl
->gl_spin
);
1475 gfs2_glock_hold(gl
);
1476 if (queue_delayed_work(glock_workqueue
, &gl
->gl_work
, 0) == 0)
1481 * gfs2_glock_thaw - Thaw any frozen glocks
1482 * @sdp: The super block
1486 void gfs2_glock_thaw(struct gfs2_sbd
*sdp
)
1488 glock_hash_walk(thaw_glock
, sdp
);
1491 static int dump_glock(struct seq_file
*seq
, struct gfs2_glock
*gl
)
1494 spin_lock(&gl
->gl_spin
);
1495 ret
= __dump_glock(seq
, gl
);
1496 spin_unlock(&gl
->gl_spin
);
1500 static void dump_glock_func(struct gfs2_glock
*gl
)
1502 dump_glock(NULL
, gl
);
1506 * gfs2_gl_hash_clear - Empty out the glock hash table
1507 * @sdp: the filesystem
1508 * @wait: wait until it's all gone
1510 * Called when unmounting the filesystem.
1513 void gfs2_gl_hash_clear(struct gfs2_sbd
*sdp
)
1515 glock_hash_walk(clear_glock
, sdp
);
1516 flush_workqueue(glock_workqueue
);
1517 wait_event(sdp
->sd_glock_wait
, atomic_read(&sdp
->sd_glock_disposal
) == 0);
1518 glock_hash_walk(dump_glock_func
, sdp
);
1521 void gfs2_glock_finish_truncate(struct gfs2_inode
*ip
)
1523 struct gfs2_glock
*gl
= ip
->i_gl
;
1526 ret
= gfs2_truncatei_resume(ip
);
1527 gfs2_assert_withdraw(gl
->gl_sbd
, ret
== 0);
1529 spin_lock(&gl
->gl_spin
);
1530 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
1532 spin_unlock(&gl
->gl_spin
);
1535 static const char *state2str(unsigned state
)
1538 case LM_ST_UNLOCKED
:
1542 case LM_ST_DEFERRED
:
1544 case LM_ST_EXCLUSIVE
:
1550 static const char *hflags2str(char *buf
, unsigned flags
, unsigned long iflags
)
1553 if (flags
& LM_FLAG_TRY
)
1555 if (flags
& LM_FLAG_TRY_1CB
)
1557 if (flags
& LM_FLAG_NOEXP
)
1559 if (flags
& LM_FLAG_ANY
)
1561 if (flags
& LM_FLAG_PRIORITY
)
1563 if (flags
& GL_ASYNC
)
1565 if (flags
& GL_EXACT
)
1567 if (flags
& GL_NOCACHE
)
1569 if (test_bit(HIF_HOLDER
, &iflags
))
1571 if (test_bit(HIF_WAIT
, &iflags
))
1573 if (test_bit(HIF_FIRST
, &iflags
))
1580 * dump_holder - print information about a glock holder
1581 * @seq: the seq_file struct
1582 * @gh: the glock holder
1584 * Returns: 0 on success, -ENOBUFS when we run out of space
1587 static int dump_holder(struct seq_file
*seq
, const struct gfs2_holder
*gh
)
1589 struct task_struct
*gh_owner
= NULL
;
1592 if (gh
->gh_owner_pid
)
1593 gh_owner
= pid_task(gh
->gh_owner_pid
, PIDTYPE_PID
);
1594 gfs2_print_dbg(seq
, " H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
1595 state2str(gh
->gh_state
),
1596 hflags2str(flags_buf
, gh
->gh_flags
, gh
->gh_iflags
),
1598 gh
->gh_owner_pid
? (long)pid_nr(gh
->gh_owner_pid
) : -1,
1599 gh_owner
? gh_owner
->comm
: "(ended)",
1604 static const char *gflags2str(char *buf
, const struct gfs2_glock
*gl
)
1606 const unsigned long *gflags
= &gl
->gl_flags
;
1609 if (test_bit(GLF_LOCK
, gflags
))
1611 if (test_bit(GLF_DEMOTE
, gflags
))
1613 if (test_bit(GLF_PENDING_DEMOTE
, gflags
))
1615 if (test_bit(GLF_DEMOTE_IN_PROGRESS
, gflags
))
1617 if (test_bit(GLF_DIRTY
, gflags
))
1619 if (test_bit(GLF_LFLUSH
, gflags
))
1621 if (test_bit(GLF_INVALIDATE_IN_PROGRESS
, gflags
))
1623 if (test_bit(GLF_REPLY_PENDING
, gflags
))
1625 if (test_bit(GLF_INITIAL
, gflags
))
1627 if (test_bit(GLF_FROZEN
, gflags
))
1629 if (test_bit(GLF_QUEUED
, gflags
))
1631 if (test_bit(GLF_LRU
, gflags
))
1640 * __dump_glock - print information about a glock
1641 * @seq: The seq_file struct
1644 * The file format is as follows:
1645 * One line per object, capital letters are used to indicate objects
1646 * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
1647 * other objects are indented by a single space and follow the glock to
1648 * which they are related. Fields are indicated by lower case letters
1649 * followed by a colon and the field value, except for strings which are in
1650 * [] so that its possible to see if they are composed of spaces for
1651 * example. The field's are n = number (id of the object), f = flags,
1652 * t = type, s = state, r = refcount, e = error, p = pid.
1654 * Returns: 0 on success, -ENOBUFS when we run out of space
1657 static int __dump_glock(struct seq_file
*seq
, const struct gfs2_glock
*gl
)
1659 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1660 unsigned long long dtime
;
1661 const struct gfs2_holder
*gh
;
1662 char gflags_buf
[32];
1665 dtime
= jiffies
- gl
->gl_demote_time
;
1666 dtime
*= 1000000/HZ
; /* demote time in uSec */
1667 if (!test_bit(GLF_DEMOTE
, &gl
->gl_flags
))
1669 gfs2_print_dbg(seq
, "G: s:%s n:%u/%llx f:%s t:%s d:%s/%llu a:%d v:%d r:%d\n",
1670 state2str(gl
->gl_state
),
1671 gl
->gl_name
.ln_type
,
1672 (unsigned long long)gl
->gl_name
.ln_number
,
1673 gflags2str(gflags_buf
, gl
),
1674 state2str(gl
->gl_target
),
1675 state2str(gl
->gl_demote_state
), dtime
,
1676 atomic_read(&gl
->gl_ail_count
),
1677 atomic_read(&gl
->gl_revokes
),
1678 atomic_read(&gl
->gl_ref
));
1680 list_for_each_entry(gh
, &gl
->gl_holders
, gh_list
) {
1681 error
= dump_holder(seq
, gh
);
1685 if (gl
->gl_state
!= LM_ST_UNLOCKED
&& glops
->go_dump
)
1686 error
= glops
->go_dump(seq
, gl
);
1694 int __init
gfs2_glock_init(void)
1697 for(i
= 0; i
< GFS2_GL_HASH_SIZE
; i
++) {
1698 INIT_HLIST_BL_HEAD(&gl_hash_table
[i
]);
1701 glock_workqueue
= alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM
|
1702 WQ_HIGHPRI
| WQ_FREEZABLE
, 0);
1703 if (IS_ERR(glock_workqueue
))
1704 return PTR_ERR(glock_workqueue
);
1705 gfs2_delete_workqueue
= alloc_workqueue("delete_workqueue",
1706 WQ_MEM_RECLAIM
| WQ_FREEZABLE
,
1708 if (IS_ERR(gfs2_delete_workqueue
)) {
1709 destroy_workqueue(glock_workqueue
);
1710 return PTR_ERR(gfs2_delete_workqueue
);
1713 register_shrinker(&glock_shrinker
);
1718 void gfs2_glock_exit(void)
1720 unregister_shrinker(&glock_shrinker
);
1721 destroy_workqueue(glock_workqueue
);
1722 destroy_workqueue(gfs2_delete_workqueue
);
1725 static inline struct gfs2_glock
*glock_hash_chain(unsigned hash
)
1727 return hlist_bl_entry(hlist_bl_first_rcu(&gl_hash_table
[hash
]),
1728 struct gfs2_glock
, gl_list
);
1731 static inline struct gfs2_glock
*glock_hash_next(struct gfs2_glock
*gl
)
1733 return hlist_bl_entry(rcu_dereference(gl
->gl_list
.next
),
1734 struct gfs2_glock
, gl_list
);
1737 static int gfs2_glock_iter_next(struct gfs2_glock_iter
*gi
)
1739 struct gfs2_glock
*gl
;
1744 gi
->gl
= glock_hash_next(gl
);
1746 gi
->gl
= glock_hash_chain(gi
->hash
);
1748 while (gi
->gl
== NULL
) {
1750 if (gi
->hash
>= GFS2_GL_HASH_SIZE
) {
1754 gi
->gl
= glock_hash_chain(gi
->hash
);
1756 /* Skip entries for other sb and dead entries */
1757 } while (gi
->sdp
!= gi
->gl
->gl_sbd
|| atomic_read(&gi
->gl
->gl_ref
) == 0);
1762 static void *gfs2_glock_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1764 struct gfs2_glock_iter
*gi
= seq
->private;
1771 if (gfs2_glock_iter_next(gi
))
1778 static void *gfs2_glock_seq_next(struct seq_file
*seq
, void *iter_ptr
,
1781 struct gfs2_glock_iter
*gi
= seq
->private;
1785 if (gfs2_glock_iter_next(gi
))
1791 static void gfs2_glock_seq_stop(struct seq_file
*seq
, void *iter_ptr
)
1793 struct gfs2_glock_iter
*gi
= seq
->private;
1800 static int gfs2_glock_seq_show(struct seq_file
*seq
, void *iter_ptr
)
1802 return dump_glock(seq
, iter_ptr
);
1805 static const struct seq_operations gfs2_glock_seq_ops
= {
1806 .start
= gfs2_glock_seq_start
,
1807 .next
= gfs2_glock_seq_next
,
1808 .stop
= gfs2_glock_seq_stop
,
1809 .show
= gfs2_glock_seq_show
,
1812 static int gfs2_debugfs_open(struct inode
*inode
, struct file
*file
)
1814 int ret
= seq_open_private(file
, &gfs2_glock_seq_ops
,
1815 sizeof(struct gfs2_glock_iter
));
1817 struct seq_file
*seq
= file
->private_data
;
1818 struct gfs2_glock_iter
*gi
= seq
->private;
1819 gi
->sdp
= inode
->i_private
;
1824 static const struct file_operations gfs2_debug_fops
= {
1825 .owner
= THIS_MODULE
,
1826 .open
= gfs2_debugfs_open
,
1828 .llseek
= seq_lseek
,
1829 .release
= seq_release_private
,
1832 int gfs2_create_debugfs_file(struct gfs2_sbd
*sdp
)
1834 sdp
->debugfs_dir
= debugfs_create_dir(sdp
->sd_table_name
, gfs2_root
);
1835 if (!sdp
->debugfs_dir
)
1837 sdp
->debugfs_dentry_glocks
= debugfs_create_file("glocks",
1839 sdp
->debugfs_dir
, sdp
,
1841 if (!sdp
->debugfs_dentry_glocks
)
1847 void gfs2_delete_debugfs_file(struct gfs2_sbd
*sdp
)
1849 if (sdp
&& sdp
->debugfs_dir
) {
1850 if (sdp
->debugfs_dentry_glocks
) {
1851 debugfs_remove(sdp
->debugfs_dentry_glocks
);
1852 sdp
->debugfs_dentry_glocks
= NULL
;
1854 debugfs_remove(sdp
->debugfs_dir
);
1855 sdp
->debugfs_dir
= NULL
;
1859 int gfs2_register_debugfs(void)
1861 gfs2_root
= debugfs_create_dir("gfs2", NULL
);
1862 return gfs2_root
? 0 : -ENOMEM
;
1865 void gfs2_unregister_debugfs(void)
1867 debugfs_remove(gfs2_root
);