2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 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/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/delay.h>
16 #include <linux/sort.h>
17 #include <linux/jhash.h>
18 #include <linux/kallsyms.h>
19 #include <linux/gfs2_ondisk.h>
20 #include <linux/list.h>
21 #include <linux/lm_interface.h>
22 #include <linux/wait.h>
23 #include <linux/module.h>
24 #include <linux/rwsem.h>
25 #include <asm/uaccess.h>
26 #include <linux/seq_file.h>
27 #include <linux/debugfs.h>
41 struct gfs2_gl_hash_bucket
{
42 struct hlist_head hb_list
;
46 int hash
; /* hash bucket index */
47 struct gfs2_sbd
*sdp
; /* incore superblock */
48 struct gfs2_glock
*gl
; /* current glock struct */
49 struct hlist_head
*hb_list
; /* current hash bucket ptr */
50 struct seq_file
*seq
; /* sequence file for debugfs */
51 char string
[512]; /* scratch space */
54 typedef void (*glock_examiner
) (struct gfs2_glock
* gl
);
56 static int gfs2_dump_lockstate(struct gfs2_sbd
*sdp
);
57 static int dump_glock(struct glock_iter
*gi
, struct gfs2_glock
*gl
);
58 static void gfs2_glock_xmote_th(struct gfs2_glock
*gl
, struct gfs2_holder
*gh
);
59 static void gfs2_glock_drop_th(struct gfs2_glock
*gl
);
60 static DECLARE_RWSEM(gfs2_umount_flush_sem
);
61 static struct dentry
*gfs2_root
;
63 #define GFS2_GL_HASH_SHIFT 15
64 #define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT)
65 #define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1)
67 static struct gfs2_gl_hash_bucket gl_hash_table
[GFS2_GL_HASH_SIZE
];
68 static struct dentry
*gfs2_root
;
71 * Despite what you might think, the numbers below are not arbitrary :-)
72 * They are taken from the ipv4 routing hash code, which is well tested
73 * and thus should be nearly optimal. Later on we might tweek the numbers
74 * but for now this should be fine.
76 * The reason for putting the locks in a separate array from the list heads
77 * is that we can have fewer locks than list heads and save memory. We use
78 * the same hash function for both, but with a different hash mask.
80 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
81 defined(CONFIG_PROVE_LOCKING)
84 # define GL_HASH_LOCK_SZ 256
87 # define GL_HASH_LOCK_SZ 4096
89 # define GL_HASH_LOCK_SZ 2048
91 # define GL_HASH_LOCK_SZ 1024
93 # define GL_HASH_LOCK_SZ 512
95 # define GL_HASH_LOCK_SZ 256
99 /* We never want more locks than chains */
100 #if GFS2_GL_HASH_SIZE < GL_HASH_LOCK_SZ
101 # undef GL_HASH_LOCK_SZ
102 # define GL_HASH_LOCK_SZ GFS2_GL_HASH_SIZE
105 static rwlock_t gl_hash_locks
[GL_HASH_LOCK_SZ
];
107 static inline rwlock_t
*gl_lock_addr(unsigned int x
)
109 return &gl_hash_locks
[x
& (GL_HASH_LOCK_SZ
-1)];
111 #else /* not SMP, so no spinlocks required */
112 static inline rwlock_t
*gl_lock_addr(unsigned int x
)
119 * relaxed_state_ok - is a requested lock compatible with the current lock mode?
120 * @actual: the current state of the lock
121 * @requested: the lock state that was requested by the caller
122 * @flags: the modifier flags passed in by the caller
124 * Returns: 1 if the locks are compatible, 0 otherwise
127 static inline int relaxed_state_ok(unsigned int actual
, unsigned requested
,
130 if (actual
== requested
)
133 if (flags
& GL_EXACT
)
136 if (actual
== LM_ST_EXCLUSIVE
&& requested
== LM_ST_SHARED
)
139 if (actual
!= LM_ST_UNLOCKED
&& (flags
& LM_FLAG_ANY
))
146 * gl_hash() - Turn glock number into hash bucket number
147 * @lock: The glock number
149 * Returns: The number of the corresponding hash bucket
152 static unsigned int gl_hash(const struct gfs2_sbd
*sdp
,
153 const struct lm_lockname
*name
)
157 h
= jhash(&name
->ln_number
, sizeof(u64
), 0);
158 h
= jhash(&name
->ln_type
, sizeof(unsigned int), h
);
159 h
= jhash(&sdp
, sizeof(struct gfs2_sbd
*), h
);
160 h
&= GFS2_GL_HASH_MASK
;
166 * glock_free() - Perform a few checks and then release struct gfs2_glock
167 * @gl: The glock to release
169 * Also calls lock module to release its internal structure for this glock.
173 static void glock_free(struct gfs2_glock
*gl
)
175 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
176 struct inode
*aspace
= gl
->gl_aspace
;
178 gfs2_lm_put_lock(sdp
, gl
->gl_lock
);
181 gfs2_aspace_put(aspace
);
183 kmem_cache_free(gfs2_glock_cachep
, gl
);
187 * gfs2_glock_hold() - increment reference count on glock
188 * @gl: The glock to hold
192 void gfs2_glock_hold(struct gfs2_glock
*gl
)
194 atomic_inc(&gl
->gl_ref
);
198 * gfs2_glock_put() - Decrement reference count on glock
199 * @gl: The glock to put
203 int gfs2_glock_put(struct gfs2_glock
*gl
)
206 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
208 write_lock(gl_lock_addr(gl
->gl_hash
));
209 if (atomic_dec_and_test(&gl
->gl_ref
)) {
210 hlist_del(&gl
->gl_list
);
211 write_unlock(gl_lock_addr(gl
->gl_hash
));
212 BUG_ON(spin_is_locked(&gl
->gl_spin
));
213 gfs2_assert(sdp
, gl
->gl_state
== LM_ST_UNLOCKED
);
214 gfs2_assert(sdp
, list_empty(&gl
->gl_reclaim
));
215 gfs2_assert(sdp
, list_empty(&gl
->gl_holders
));
216 gfs2_assert(sdp
, list_empty(&gl
->gl_waiters1
));
217 gfs2_assert(sdp
, list_empty(&gl
->gl_waiters3
));
222 write_unlock(gl_lock_addr(gl
->gl_hash
));
228 * search_bucket() - Find struct gfs2_glock by lock number
229 * @bucket: the bucket to search
230 * @name: The lock name
232 * Returns: NULL, or the struct gfs2_glock with the requested number
235 static struct gfs2_glock
*search_bucket(unsigned int hash
,
236 const struct gfs2_sbd
*sdp
,
237 const struct lm_lockname
*name
)
239 struct gfs2_glock
*gl
;
240 struct hlist_node
*h
;
242 hlist_for_each_entry(gl
, h
, &gl_hash_table
[hash
].hb_list
, gl_list
) {
243 if (!lm_name_equal(&gl
->gl_name
, name
))
245 if (gl
->gl_sbd
!= sdp
)
248 atomic_inc(&gl
->gl_ref
);
257 * gfs2_glock_find() - Find glock by lock number
258 * @sdp: The GFS2 superblock
259 * @name: The lock name
261 * Returns: NULL, or the struct gfs2_glock with the requested number
264 static struct gfs2_glock
*gfs2_glock_find(const struct gfs2_sbd
*sdp
,
265 const struct lm_lockname
*name
)
267 unsigned int hash
= gl_hash(sdp
, name
);
268 struct gfs2_glock
*gl
;
270 read_lock(gl_lock_addr(hash
));
271 gl
= search_bucket(hash
, sdp
, name
);
272 read_unlock(gl_lock_addr(hash
));
278 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
279 * @sdp: The GFS2 superblock
280 * @number: the lock number
281 * @glops: The glock_operations to use
282 * @create: If 0, don't create the glock if it doesn't exist
283 * @glp: the glock is returned here
285 * This does not lock a glock, just finds/creates structures for one.
290 int gfs2_glock_get(struct gfs2_sbd
*sdp
, u64 number
,
291 const struct gfs2_glock_operations
*glops
, int create
,
292 struct gfs2_glock
**glp
)
294 struct lm_lockname name
= { .ln_number
= number
, .ln_type
= glops
->go_type
};
295 struct gfs2_glock
*gl
, *tmp
;
296 unsigned int hash
= gl_hash(sdp
, &name
);
299 read_lock(gl_lock_addr(hash
));
300 gl
= search_bucket(hash
, sdp
, &name
);
301 read_unlock(gl_lock_addr(hash
));
308 gl
= kmem_cache_alloc(gfs2_glock_cachep
, GFP_KERNEL
);
314 atomic_set(&gl
->gl_ref
, 1);
315 gl
->gl_state
= LM_ST_UNLOCKED
;
317 gl
->gl_owner_pid
= 0;
320 gl
->gl_req_gh
= NULL
;
321 gl
->gl_req_bh
= NULL
;
323 gl
->gl_stamp
= jiffies
;
324 gl
->gl_object
= NULL
;
326 gl
->gl_aspace
= NULL
;
327 lops_init_le(&gl
->gl_le
, &gfs2_glock_lops
);
329 /* If this glock protects actual on-disk data or metadata blocks,
330 create a VFS inode to manage the pages/buffers holding them. */
331 if (glops
== &gfs2_inode_glops
|| glops
== &gfs2_rgrp_glops
) {
332 gl
->gl_aspace
= gfs2_aspace_get(sdp
);
333 if (!gl
->gl_aspace
) {
339 error
= gfs2_lm_get_lock(sdp
, &name
, &gl
->gl_lock
);
343 write_lock(gl_lock_addr(hash
));
344 tmp
= search_bucket(hash
, sdp
, &name
);
346 write_unlock(gl_lock_addr(hash
));
350 hlist_add_head(&gl
->gl_list
, &gl_hash_table
[hash
].hb_list
);
351 write_unlock(gl_lock_addr(hash
));
360 gfs2_aspace_put(gl
->gl_aspace
);
362 kmem_cache_free(gfs2_glock_cachep
, gl
);
367 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
369 * @state: the state we're requesting
370 * @flags: the modifier flags
371 * @gh: the holder structure
375 void gfs2_holder_init(struct gfs2_glock
*gl
, unsigned int state
, unsigned flags
,
376 struct gfs2_holder
*gh
)
378 INIT_LIST_HEAD(&gh
->gh_list
);
380 gh
->gh_ip
= (unsigned long)__builtin_return_address(0);
381 gh
->gh_owner_pid
= current
->pid
;
382 gh
->gh_state
= state
;
383 gh
->gh_flags
= flags
;
390 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
391 * @state: the state we're requesting
392 * @flags: the modifier flags
393 * @gh: the holder structure
395 * Don't mess with the glock.
399 void gfs2_holder_reinit(unsigned int state
, unsigned flags
, struct gfs2_holder
*gh
)
401 gh
->gh_state
= state
;
402 gh
->gh_flags
= flags
;
404 gh
->gh_ip
= (unsigned long)__builtin_return_address(0);
408 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
409 * @gh: the holder structure
413 void gfs2_holder_uninit(struct gfs2_holder
*gh
)
415 gfs2_glock_put(gh
->gh_gl
);
420 static void gfs2_holder_wake(struct gfs2_holder
*gh
)
422 clear_bit(HIF_WAIT
, &gh
->gh_iflags
);
423 smp_mb__after_clear_bit();
424 wake_up_bit(&gh
->gh_iflags
, HIF_WAIT
);
427 static int just_schedule(void *word
)
433 static void wait_on_holder(struct gfs2_holder
*gh
)
436 wait_on_bit(&gh
->gh_iflags
, HIF_WAIT
, just_schedule
, TASK_UNINTERRUPTIBLE
);
439 static void gfs2_demote_wake(struct gfs2_glock
*gl
)
441 clear_bit(GLF_DEMOTE
, &gl
->gl_flags
);
442 smp_mb__after_clear_bit();
443 wake_up_bit(&gl
->gl_flags
, GLF_DEMOTE
);
446 static void wait_on_demote(struct gfs2_glock
*gl
)
449 wait_on_bit(&gl
->gl_flags
, GLF_DEMOTE
, just_schedule
, TASK_UNINTERRUPTIBLE
);
453 * rq_mutex - process a mutex request in the queue
454 * @gh: the glock holder
456 * Returns: 1 if the queue is blocked
459 static int rq_mutex(struct gfs2_holder
*gh
)
461 struct gfs2_glock
*gl
= gh
->gh_gl
;
463 list_del_init(&gh
->gh_list
);
464 /* gh->gh_error never examined. */
465 set_bit(GLF_LOCK
, &gl
->gl_flags
);
466 clear_bit(HIF_WAIT
, &gh
->gh_iflags
);
468 wake_up_bit(&gh
->gh_iflags
, HIF_WAIT
);
474 * rq_promote - process a promote request in the queue
475 * @gh: the glock holder
477 * Acquire a new inter-node lock, or change a lock state to more restrictive.
479 * Returns: 1 if the queue is blocked
482 static int rq_promote(struct gfs2_holder
*gh
)
484 struct gfs2_glock
*gl
= gh
->gh_gl
;
485 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
487 if (!relaxed_state_ok(gl
->gl_state
, gh
->gh_state
, gh
->gh_flags
)) {
488 if (list_empty(&gl
->gl_holders
)) {
490 set_bit(GLF_LOCK
, &gl
->gl_flags
);
491 spin_unlock(&gl
->gl_spin
);
493 if (atomic_read(&sdp
->sd_reclaim_count
) >
494 gfs2_tune_get(sdp
, gt_reclaim_limit
) &&
495 !(gh
->gh_flags
& LM_FLAG_PRIORITY
)) {
496 gfs2_reclaim_glock(sdp
);
497 gfs2_reclaim_glock(sdp
);
500 gfs2_glock_xmote_th(gh
->gh_gl
, gh
);
501 spin_lock(&gl
->gl_spin
);
506 if (list_empty(&gl
->gl_holders
)) {
507 set_bit(HIF_FIRST
, &gh
->gh_iflags
);
508 set_bit(GLF_LOCK
, &gl
->gl_flags
);
510 struct gfs2_holder
*next_gh
;
511 if (gh
->gh_state
== LM_ST_EXCLUSIVE
)
513 next_gh
= list_entry(gl
->gl_holders
.next
, struct gfs2_holder
,
515 if (next_gh
->gh_state
== LM_ST_EXCLUSIVE
)
519 list_move_tail(&gh
->gh_list
, &gl
->gl_holders
);
521 set_bit(HIF_HOLDER
, &gh
->gh_iflags
);
523 gfs2_holder_wake(gh
);
529 * rq_demote - process a demote request in the queue
530 * @gh: the glock holder
532 * Returns: 1 if the queue is blocked
535 static int rq_demote(struct gfs2_glock
*gl
)
537 if (!list_empty(&gl
->gl_holders
))
540 if (gl
->gl_state
== gl
->gl_demote_state
||
541 gl
->gl_state
== LM_ST_UNLOCKED
) {
542 gfs2_demote_wake(gl
);
545 set_bit(GLF_LOCK
, &gl
->gl_flags
);
546 if (gl
->gl_demote_state
== LM_ST_UNLOCKED
||
547 gl
->gl_state
!= LM_ST_EXCLUSIVE
) {
548 spin_unlock(&gl
->gl_spin
);
549 gfs2_glock_drop_th(gl
);
551 spin_unlock(&gl
->gl_spin
);
552 gfs2_glock_xmote_th(gl
, NULL
);
554 spin_lock(&gl
->gl_spin
);
560 * run_queue - process holder structures on a glock
564 static void run_queue(struct gfs2_glock
*gl
)
566 struct gfs2_holder
*gh
;
570 if (test_bit(GLF_LOCK
, &gl
->gl_flags
))
573 if (!list_empty(&gl
->gl_waiters1
)) {
574 gh
= list_entry(gl
->gl_waiters1
.next
,
575 struct gfs2_holder
, gh_list
);
577 if (test_bit(HIF_MUTEX
, &gh
->gh_iflags
))
578 blocked
= rq_mutex(gh
);
580 gfs2_assert_warn(gl
->gl_sbd
, 0);
582 } else if (test_bit(GLF_DEMOTE
, &gl
->gl_flags
)) {
583 blocked
= rq_demote(gl
);
584 } else if (!list_empty(&gl
->gl_waiters3
)) {
585 gh
= list_entry(gl
->gl_waiters3
.next
,
586 struct gfs2_holder
, gh_list
);
588 if (test_bit(HIF_PROMOTE
, &gh
->gh_iflags
))
589 blocked
= rq_promote(gh
);
591 gfs2_assert_warn(gl
->gl_sbd
, 0);
602 * gfs2_glmutex_lock - acquire a local lock on a glock
605 * Gives caller exclusive access to manipulate a glock structure.
608 static void gfs2_glmutex_lock(struct gfs2_glock
*gl
)
610 struct gfs2_holder gh
;
612 gfs2_holder_init(gl
, 0, 0, &gh
);
613 set_bit(HIF_MUTEX
, &gh
.gh_iflags
);
614 if (test_and_set_bit(HIF_WAIT
, &gh
.gh_iflags
))
617 spin_lock(&gl
->gl_spin
);
618 if (test_and_set_bit(GLF_LOCK
, &gl
->gl_flags
)) {
619 list_add_tail(&gh
.gh_list
, &gl
->gl_waiters1
);
621 gl
->gl_owner_pid
= current
->pid
;
622 gl
->gl_ip
= (unsigned long)__builtin_return_address(0);
623 clear_bit(HIF_WAIT
, &gh
.gh_iflags
);
625 wake_up_bit(&gh
.gh_iflags
, HIF_WAIT
);
627 spin_unlock(&gl
->gl_spin
);
630 gfs2_holder_uninit(&gh
);
634 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
637 * Returns: 1 if the glock is acquired
640 static int gfs2_glmutex_trylock(struct gfs2_glock
*gl
)
644 spin_lock(&gl
->gl_spin
);
645 if (test_and_set_bit(GLF_LOCK
, &gl
->gl_flags
)) {
648 gl
->gl_owner_pid
= current
->pid
;
649 gl
->gl_ip
= (unsigned long)__builtin_return_address(0);
651 spin_unlock(&gl
->gl_spin
);
657 * gfs2_glmutex_unlock - release a local lock on a glock
662 static void gfs2_glmutex_unlock(struct gfs2_glock
*gl
)
664 spin_lock(&gl
->gl_spin
);
665 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
666 gl
->gl_owner_pid
= 0;
669 BUG_ON(!spin_is_locked(&gl
->gl_spin
));
670 spin_unlock(&gl
->gl_spin
);
674 * handle_callback - process a demote request
676 * @state: the state the caller wants us to change to
678 * There are only two requests that we are going to see in actual
679 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
682 static void handle_callback(struct gfs2_glock
*gl
, unsigned int state
, int remote
)
684 spin_lock(&gl
->gl_spin
);
685 if (test_and_set_bit(GLF_DEMOTE
, &gl
->gl_flags
) == 0) {
686 gl
->gl_demote_state
= state
;
687 gl
->gl_demote_time
= jiffies
;
688 if (remote
&& gl
->gl_ops
->go_type
== LM_TYPE_IOPEN
&&
690 struct inode
*inode
= igrab(gl
->gl_object
);
691 spin_unlock(&gl
->gl_spin
);
693 d_prune_aliases(inode
);
698 } else if (gl
->gl_demote_state
!= LM_ST_UNLOCKED
&&
699 gl
->gl_demote_state
!= state
) {
700 gl
->gl_demote_state
= LM_ST_UNLOCKED
;
702 spin_unlock(&gl
->gl_spin
);
706 * state_change - record that the glock is now in a different state
708 * @new_state the new state
712 static void state_change(struct gfs2_glock
*gl
, unsigned int new_state
)
716 held1
= (gl
->gl_state
!= LM_ST_UNLOCKED
);
717 held2
= (new_state
!= LM_ST_UNLOCKED
);
719 if (held1
!= held2
) {
726 gl
->gl_state
= new_state
;
730 * xmote_bh - Called after the lock module is done acquiring a lock
731 * @gl: The glock in question
732 * @ret: the int returned from the lock module
736 static void xmote_bh(struct gfs2_glock
*gl
, unsigned int ret
)
738 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
739 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
740 struct gfs2_holder
*gh
= gl
->gl_req_gh
;
741 int prev_state
= gl
->gl_state
;
744 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
745 gfs2_assert_warn(sdp
, list_empty(&gl
->gl_holders
));
746 gfs2_assert_warn(sdp
, !(ret
& LM_OUT_ASYNC
));
748 state_change(gl
, ret
& LM_OUT_ST_MASK
);
750 if (prev_state
!= LM_ST_UNLOCKED
&& !(ret
& LM_OUT_CACHEABLE
)) {
752 glops
->go_inval(gl
, DIO_METADATA
);
753 } else if (gl
->gl_state
== LM_ST_DEFERRED
) {
754 /* We might not want to do this here.
755 Look at moving to the inode glops. */
757 glops
->go_inval(gl
, 0);
760 /* Deal with each possible exit condition */
763 gl
->gl_stamp
= jiffies
;
764 if (ret
& LM_OUT_CANCELED
) {
767 spin_lock(&gl
->gl_spin
);
768 if (gl
->gl_state
!= gl
->gl_demote_state
) {
769 gl
->gl_req_bh
= NULL
;
770 spin_unlock(&gl
->gl_spin
);
771 gfs2_glock_drop_th(gl
);
775 gfs2_demote_wake(gl
);
776 spin_unlock(&gl
->gl_spin
);
779 spin_lock(&gl
->gl_spin
);
780 list_del_init(&gh
->gh_list
);
782 if (unlikely(test_bit(SDF_SHUTDOWN
, &sdp
->sd_flags
)))
784 gh
->gh_error
= GLR_CANCELED
;
785 if (ret
& LM_OUT_CANCELED
)
787 if (relaxed_state_ok(gl
->gl_state
, gh
->gh_state
, gh
->gh_flags
)) {
788 list_add_tail(&gh
->gh_list
, &gl
->gl_holders
);
790 set_bit(HIF_HOLDER
, &gh
->gh_iflags
);
791 set_bit(HIF_FIRST
, &gh
->gh_iflags
);
795 gh
->gh_error
= GLR_TRYFAILED
;
796 if (gh
->gh_flags
& (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
))
798 gh
->gh_error
= -EINVAL
;
799 if (gfs2_assert_withdraw(sdp
, 0) == -1)
800 fs_err(sdp
, "ret = 0x%.8X\n", ret
);
802 spin_unlock(&gl
->gl_spin
);
805 if (glops
->go_xmote_bh
)
806 glops
->go_xmote_bh(gl
);
809 spin_lock(&gl
->gl_spin
);
810 gl
->gl_req_gh
= NULL
;
811 gl
->gl_req_bh
= NULL
;
812 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
814 spin_unlock(&gl
->gl_spin
);
820 gfs2_holder_wake(gh
);
824 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
825 * @gl: The glock in question
826 * @state: the requested state
827 * @flags: modifier flags to the lock call
831 static void gfs2_glock_xmote_th(struct gfs2_glock
*gl
, struct gfs2_holder
*gh
)
833 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
834 int flags
= gh
? gh
->gh_flags
: 0;
835 unsigned state
= gh
? gh
->gh_state
: gl
->gl_demote_state
;
836 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
837 int lck_flags
= flags
& (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
|
838 LM_FLAG_NOEXP
| LM_FLAG_ANY
|
840 unsigned int lck_ret
;
842 if (glops
->go_xmote_th
)
843 glops
->go_xmote_th(gl
);
845 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
846 gfs2_assert_warn(sdp
, list_empty(&gl
->gl_holders
));
847 gfs2_assert_warn(sdp
, state
!= LM_ST_UNLOCKED
);
848 gfs2_assert_warn(sdp
, state
!= gl
->gl_state
);
851 gl
->gl_req_bh
= xmote_bh
;
853 lck_ret
= gfs2_lm_lock(sdp
, gl
->gl_lock
, gl
->gl_state
, state
, lck_flags
);
855 if (gfs2_assert_withdraw(sdp
, !(lck_ret
& LM_OUT_ERROR
)))
858 if (lck_ret
& LM_OUT_ASYNC
)
859 gfs2_assert_warn(sdp
, lck_ret
== LM_OUT_ASYNC
);
861 xmote_bh(gl
, lck_ret
);
865 * drop_bh - Called after a lock module unlock completes
867 * @ret: the return status
869 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
870 * Doesn't drop the reference on the glock the top half took out
874 static void drop_bh(struct gfs2_glock
*gl
, unsigned int ret
)
876 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
877 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
878 struct gfs2_holder
*gh
= gl
->gl_req_gh
;
880 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
881 gfs2_assert_warn(sdp
, list_empty(&gl
->gl_holders
));
882 gfs2_assert_warn(sdp
, !ret
);
884 state_change(gl
, LM_ST_UNLOCKED
);
885 gfs2_demote_wake(gl
);
888 glops
->go_inval(gl
, DIO_METADATA
);
891 spin_lock(&gl
->gl_spin
);
892 list_del_init(&gh
->gh_list
);
894 spin_unlock(&gl
->gl_spin
);
897 spin_lock(&gl
->gl_spin
);
898 gl
->gl_req_gh
= NULL
;
899 gl
->gl_req_bh
= NULL
;
900 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
902 spin_unlock(&gl
->gl_spin
);
907 gfs2_holder_wake(gh
);
911 * gfs2_glock_drop_th - call into the lock module to unlock a lock
916 static void gfs2_glock_drop_th(struct gfs2_glock
*gl
)
918 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
919 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
922 if (glops
->go_drop_th
)
923 glops
->go_drop_th(gl
);
925 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
926 gfs2_assert_warn(sdp
, list_empty(&gl
->gl_holders
));
927 gfs2_assert_warn(sdp
, gl
->gl_state
!= LM_ST_UNLOCKED
);
930 gl
->gl_req_bh
= drop_bh
;
932 ret
= gfs2_lm_unlock(sdp
, gl
->gl_lock
, gl
->gl_state
);
934 if (gfs2_assert_withdraw(sdp
, !(ret
& LM_OUT_ERROR
)))
940 gfs2_assert_warn(sdp
, ret
== LM_OUT_ASYNC
);
944 * do_cancels - cancel requests for locks stuck waiting on an expire flag
945 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
947 * Don't cancel GL_NOCANCEL requests.
950 static void do_cancels(struct gfs2_holder
*gh
)
952 struct gfs2_glock
*gl
= gh
->gh_gl
;
954 spin_lock(&gl
->gl_spin
);
956 while (gl
->gl_req_gh
!= gh
&&
957 !test_bit(HIF_HOLDER
, &gh
->gh_iflags
) &&
958 !list_empty(&gh
->gh_list
)) {
959 if (gl
->gl_req_bh
&& !(gl
->gl_req_gh
&&
960 (gl
->gl_req_gh
->gh_flags
& GL_NOCANCEL
))) {
961 spin_unlock(&gl
->gl_spin
);
962 gfs2_lm_cancel(gl
->gl_sbd
, gl
->gl_lock
);
964 spin_lock(&gl
->gl_spin
);
966 spin_unlock(&gl
->gl_spin
);
968 spin_lock(&gl
->gl_spin
);
972 spin_unlock(&gl
->gl_spin
);
976 * glock_wait_internal - wait on a glock acquisition
977 * @gh: the glock holder
979 * Returns: 0 on success
982 static int glock_wait_internal(struct gfs2_holder
*gh
)
984 struct gfs2_glock
*gl
= gh
->gh_gl
;
985 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
986 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
988 if (test_bit(HIF_ABORTED
, &gh
->gh_iflags
))
991 if (gh
->gh_flags
& (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
)) {
992 spin_lock(&gl
->gl_spin
);
993 if (gl
->gl_req_gh
!= gh
&&
994 !test_bit(HIF_HOLDER
, &gh
->gh_iflags
) &&
995 !list_empty(&gh
->gh_list
)) {
996 list_del_init(&gh
->gh_list
);
997 gh
->gh_error
= GLR_TRYFAILED
;
999 spin_unlock(&gl
->gl_spin
);
1000 return gh
->gh_error
;
1002 spin_unlock(&gl
->gl_spin
);
1005 if (gh
->gh_flags
& LM_FLAG_PRIORITY
)
1010 return gh
->gh_error
;
1012 gfs2_assert_withdraw(sdp
, test_bit(HIF_HOLDER
, &gh
->gh_iflags
));
1013 gfs2_assert_withdraw(sdp
, relaxed_state_ok(gl
->gl_state
, gh
->gh_state
,
1016 if (test_bit(HIF_FIRST
, &gh
->gh_iflags
)) {
1017 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
1019 if (glops
->go_lock
) {
1020 gh
->gh_error
= glops
->go_lock(gh
);
1022 spin_lock(&gl
->gl_spin
);
1023 list_del_init(&gh
->gh_list
);
1024 spin_unlock(&gl
->gl_spin
);
1028 spin_lock(&gl
->gl_spin
);
1029 gl
->gl_req_gh
= NULL
;
1030 gl
->gl_req_bh
= NULL
;
1031 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
1033 spin_unlock(&gl
->gl_spin
);
1036 return gh
->gh_error
;
1039 static inline struct gfs2_holder
*
1040 find_holder_by_owner(struct list_head
*head
, pid_t pid
)
1042 struct gfs2_holder
*gh
;
1044 list_for_each_entry(gh
, head
, gh_list
) {
1045 if (gh
->gh_owner_pid
== pid
)
1052 static void print_dbg(struct glock_iter
*gi
, const char *fmt
, ...)
1056 va_start(args
, fmt
);
1058 vsprintf(gi
->string
, fmt
, args
);
1059 seq_printf(gi
->seq
, gi
->string
);
1067 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1068 * @gh: the holder structure to add
1072 static void add_to_queue(struct gfs2_holder
*gh
)
1074 struct gfs2_glock
*gl
= gh
->gh_gl
;
1075 struct gfs2_holder
*existing
;
1077 BUG_ON(!gh
->gh_owner_pid
);
1078 if (test_and_set_bit(HIF_WAIT
, &gh
->gh_iflags
))
1081 existing
= find_holder_by_owner(&gl
->gl_holders
, gh
->gh_owner_pid
);
1083 print_symbol(KERN_WARNING
"original: %s\n", existing
->gh_ip
);
1084 printk(KERN_INFO
"pid : %d\n", existing
->gh_owner_pid
);
1085 printk(KERN_INFO
"lock type : %d lock state : %d\n",
1086 existing
->gh_gl
->gl_name
.ln_type
, existing
->gh_gl
->gl_state
);
1087 print_symbol(KERN_WARNING
"new: %s\n", gh
->gh_ip
);
1088 printk(KERN_INFO
"pid : %d\n", gh
->gh_owner_pid
);
1089 printk(KERN_INFO
"lock type : %d lock state : %d\n",
1090 gl
->gl_name
.ln_type
, gl
->gl_state
);
1094 existing
= find_holder_by_owner(&gl
->gl_waiters3
, gh
->gh_owner_pid
);
1096 print_symbol(KERN_WARNING
"original: %s\n", existing
->gh_ip
);
1097 print_symbol(KERN_WARNING
"new: %s\n", gh
->gh_ip
);
1101 if (gh
->gh_flags
& LM_FLAG_PRIORITY
)
1102 list_add(&gh
->gh_list
, &gl
->gl_waiters3
);
1104 list_add_tail(&gh
->gh_list
, &gl
->gl_waiters3
);
1108 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1109 * @gh: the holder structure
1111 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1113 * Returns: 0, GLR_TRYFAILED, or errno on failure
1116 int gfs2_glock_nq(struct gfs2_holder
*gh
)
1118 struct gfs2_glock
*gl
= gh
->gh_gl
;
1119 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1123 if (unlikely(test_bit(SDF_SHUTDOWN
, &sdp
->sd_flags
))) {
1124 set_bit(HIF_ABORTED
, &gh
->gh_iflags
);
1128 set_bit(HIF_PROMOTE
, &gh
->gh_iflags
);
1130 spin_lock(&gl
->gl_spin
);
1133 spin_unlock(&gl
->gl_spin
);
1135 if (!(gh
->gh_flags
& GL_ASYNC
)) {
1136 error
= glock_wait_internal(gh
);
1137 if (error
== GLR_CANCELED
) {
1147 * gfs2_glock_poll - poll to see if an async request has been completed
1150 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1153 int gfs2_glock_poll(struct gfs2_holder
*gh
)
1155 struct gfs2_glock
*gl
= gh
->gh_gl
;
1158 spin_lock(&gl
->gl_spin
);
1160 if (test_bit(HIF_HOLDER
, &gh
->gh_iflags
))
1162 else if (list_empty(&gh
->gh_list
)) {
1163 if (gh
->gh_error
== GLR_CANCELED
) {
1164 spin_unlock(&gl
->gl_spin
);
1166 if (gfs2_glock_nq(gh
))
1173 spin_unlock(&gl
->gl_spin
);
1179 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1180 * @gh: the holder structure
1182 * Returns: 0, GLR_TRYFAILED, or errno on failure
1185 int gfs2_glock_wait(struct gfs2_holder
*gh
)
1189 error
= glock_wait_internal(gh
);
1190 if (error
== GLR_CANCELED
) {
1192 gh
->gh_flags
&= ~GL_ASYNC
;
1193 error
= gfs2_glock_nq(gh
);
1200 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1201 * @gh: the glock holder
1205 void gfs2_glock_dq(struct gfs2_holder
*gh
)
1207 struct gfs2_glock
*gl
= gh
->gh_gl
;
1208 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1210 if (gh
->gh_flags
& GL_NOCACHE
)
1211 handle_callback(gl
, LM_ST_UNLOCKED
, 0);
1213 gfs2_glmutex_lock(gl
);
1215 spin_lock(&gl
->gl_spin
);
1216 list_del_init(&gh
->gh_list
);
1218 if (list_empty(&gl
->gl_holders
)) {
1219 spin_unlock(&gl
->gl_spin
);
1221 if (glops
->go_unlock
)
1222 glops
->go_unlock(gh
);
1224 spin_lock(&gl
->gl_spin
);
1225 gl
->gl_stamp
= jiffies
;
1228 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
1230 spin_unlock(&gl
->gl_spin
);
1233 void gfs2_glock_dq_wait(struct gfs2_holder
*gh
)
1235 struct gfs2_glock
*gl
= gh
->gh_gl
;
1241 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1242 * @gh: the holder structure
1246 void gfs2_glock_dq_uninit(struct gfs2_holder
*gh
)
1249 gfs2_holder_uninit(gh
);
1253 * gfs2_glock_nq_num - acquire a glock based on lock number
1254 * @sdp: the filesystem
1255 * @number: the lock number
1256 * @glops: the glock operations for the type of glock
1257 * @state: the state to acquire the glock in
1258 * @flags: modifier flags for the aquisition
1259 * @gh: the struct gfs2_holder
1264 int gfs2_glock_nq_num(struct gfs2_sbd
*sdp
, u64 number
,
1265 const struct gfs2_glock_operations
*glops
,
1266 unsigned int state
, int flags
, struct gfs2_holder
*gh
)
1268 struct gfs2_glock
*gl
;
1271 error
= gfs2_glock_get(sdp
, number
, glops
, CREATE
, &gl
);
1273 error
= gfs2_glock_nq_init(gl
, state
, flags
, gh
);
1281 * glock_compare - Compare two struct gfs2_glock structures for sorting
1282 * @arg_a: the first structure
1283 * @arg_b: the second structure
1287 static int glock_compare(const void *arg_a
, const void *arg_b
)
1289 const struct gfs2_holder
*gh_a
= *(const struct gfs2_holder
**)arg_a
;
1290 const struct gfs2_holder
*gh_b
= *(const struct gfs2_holder
**)arg_b
;
1291 const struct lm_lockname
*a
= &gh_a
->gh_gl
->gl_name
;
1292 const struct lm_lockname
*b
= &gh_b
->gh_gl
->gl_name
;
1294 if (a
->ln_number
> b
->ln_number
)
1296 if (a
->ln_number
< b
->ln_number
)
1298 BUG_ON(gh_a
->gh_gl
->gl_ops
->go_type
== gh_b
->gh_gl
->gl_ops
->go_type
);
1303 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1304 * @num_gh: the number of structures
1305 * @ghs: an array of struct gfs2_holder structures
1307 * Returns: 0 on success (all glocks acquired),
1308 * errno on failure (no glocks acquired)
1311 static int nq_m_sync(unsigned int num_gh
, struct gfs2_holder
*ghs
,
1312 struct gfs2_holder
**p
)
1317 for (x
= 0; x
< num_gh
; x
++)
1320 sort(p
, num_gh
, sizeof(struct gfs2_holder
*), glock_compare
, NULL
);
1322 for (x
= 0; x
< num_gh
; x
++) {
1323 p
[x
]->gh_flags
&= ~(LM_FLAG_TRY
| GL_ASYNC
);
1325 error
= gfs2_glock_nq(p
[x
]);
1328 gfs2_glock_dq(p
[x
]);
1337 * gfs2_glock_nq_m - acquire multiple glocks
1338 * @num_gh: the number of structures
1339 * @ghs: an array of struct gfs2_holder structures
1342 * Returns: 0 on success (all glocks acquired),
1343 * errno on failure (no glocks acquired)
1346 int gfs2_glock_nq_m(unsigned int num_gh
, struct gfs2_holder
*ghs
)
1348 struct gfs2_holder
*tmp
[4];
1349 struct gfs2_holder
**pph
= tmp
;
1356 ghs
->gh_flags
&= ~(LM_FLAG_TRY
| GL_ASYNC
);
1357 return gfs2_glock_nq(ghs
);
1361 pph
= kmalloc(num_gh
* sizeof(struct gfs2_holder
*), GFP_NOFS
);
1366 error
= nq_m_sync(num_gh
, ghs
, pph
);
1375 * gfs2_glock_dq_m - release multiple glocks
1376 * @num_gh: the number of structures
1377 * @ghs: an array of struct gfs2_holder structures
1381 void gfs2_glock_dq_m(unsigned int num_gh
, struct gfs2_holder
*ghs
)
1385 for (x
= 0; x
< num_gh
; x
++)
1386 gfs2_glock_dq(&ghs
[x
]);
1390 * gfs2_glock_dq_uninit_m - release multiple glocks
1391 * @num_gh: the number of structures
1392 * @ghs: an array of struct gfs2_holder structures
1396 void gfs2_glock_dq_uninit_m(unsigned int num_gh
, struct gfs2_holder
*ghs
)
1400 for (x
= 0; x
< num_gh
; x
++)
1401 gfs2_glock_dq_uninit(&ghs
[x
]);
1405 * gfs2_lvb_hold - attach a LVB from a glock
1406 * @gl: The glock in question
1410 int gfs2_lvb_hold(struct gfs2_glock
*gl
)
1414 gfs2_glmutex_lock(gl
);
1416 if (!atomic_read(&gl
->gl_lvb_count
)) {
1417 error
= gfs2_lm_hold_lvb(gl
->gl_sbd
, gl
->gl_lock
, &gl
->gl_lvb
);
1419 gfs2_glmutex_unlock(gl
);
1422 gfs2_glock_hold(gl
);
1424 atomic_inc(&gl
->gl_lvb_count
);
1426 gfs2_glmutex_unlock(gl
);
1432 * gfs2_lvb_unhold - detach a LVB from a glock
1433 * @gl: The glock in question
1437 void gfs2_lvb_unhold(struct gfs2_glock
*gl
)
1439 gfs2_glock_hold(gl
);
1440 gfs2_glmutex_lock(gl
);
1442 gfs2_assert(gl
->gl_sbd
, atomic_read(&gl
->gl_lvb_count
) > 0);
1443 if (atomic_dec_and_test(&gl
->gl_lvb_count
)) {
1444 gfs2_lm_unhold_lvb(gl
->gl_sbd
, gl
->gl_lock
, gl
->gl_lvb
);
1449 gfs2_glmutex_unlock(gl
);
1453 static void blocking_cb(struct gfs2_sbd
*sdp
, struct lm_lockname
*name
,
1456 struct gfs2_glock
*gl
;
1458 gl
= gfs2_glock_find(sdp
, name
);
1462 handle_callback(gl
, state
, 1);
1464 spin_lock(&gl
->gl_spin
);
1466 spin_unlock(&gl
->gl_spin
);
1472 * gfs2_glock_cb - Callback used by locking module
1473 * @sdp: Pointer to the superblock
1474 * @type: Type of callback
1475 * @data: Type dependent data pointer
1477 * Called by the locking module when it wants to tell us something.
1478 * Either we need to drop a lock, one of our ASYNC requests completed, or
1479 * a journal from another client needs to be recovered.
1482 void gfs2_glock_cb(void *cb_data
, unsigned int type
, void *data
)
1484 struct gfs2_sbd
*sdp
= cb_data
;
1488 blocking_cb(sdp
, data
, LM_ST_UNLOCKED
);
1492 blocking_cb(sdp
, data
, LM_ST_DEFERRED
);
1496 blocking_cb(sdp
, data
, LM_ST_SHARED
);
1500 struct lm_async_cb
*async
= data
;
1501 struct gfs2_glock
*gl
;
1503 down_read(&gfs2_umount_flush_sem
);
1504 gl
= gfs2_glock_find(sdp
, &async
->lc_name
);
1505 if (gfs2_assert_warn(sdp
, gl
))
1507 if (!gfs2_assert_warn(sdp
, gl
->gl_req_bh
))
1508 gl
->gl_req_bh(gl
, async
->lc_ret
);
1510 up_read(&gfs2_umount_flush_sem
);
1514 case LM_CB_NEED_RECOVERY
:
1515 gfs2_jdesc_make_dirty(sdp
, *(unsigned int *)data
);
1516 if (sdp
->sd_recoverd_process
)
1517 wake_up_process(sdp
->sd_recoverd_process
);
1520 case LM_CB_DROPLOCKS
:
1521 gfs2_gl_hash_clear(sdp
, NO_WAIT
);
1522 gfs2_quota_scan(sdp
);
1526 gfs2_assert_warn(sdp
, 0);
1532 * demote_ok - Check to see if it's ok to unlock a glock
1535 * Returns: 1 if it's ok
1538 static int demote_ok(struct gfs2_glock
*gl
)
1540 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1543 if (test_bit(GLF_STICKY
, &gl
->gl_flags
))
1545 else if (glops
->go_demote_ok
)
1546 demote
= glops
->go_demote_ok(gl
);
1552 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1557 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock
*gl
)
1559 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1561 spin_lock(&sdp
->sd_reclaim_lock
);
1562 if (list_empty(&gl
->gl_reclaim
)) {
1563 gfs2_glock_hold(gl
);
1564 list_add(&gl
->gl_reclaim
, &sdp
->sd_reclaim_list
);
1565 atomic_inc(&sdp
->sd_reclaim_count
);
1567 spin_unlock(&sdp
->sd_reclaim_lock
);
1569 wake_up(&sdp
->sd_reclaim_wq
);
1573 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1574 * @sdp: the filesystem
1576 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1577 * different glock and we notice that there are a lot of glocks in the
1582 void gfs2_reclaim_glock(struct gfs2_sbd
*sdp
)
1584 struct gfs2_glock
*gl
;
1586 spin_lock(&sdp
->sd_reclaim_lock
);
1587 if (list_empty(&sdp
->sd_reclaim_list
)) {
1588 spin_unlock(&sdp
->sd_reclaim_lock
);
1591 gl
= list_entry(sdp
->sd_reclaim_list
.next
,
1592 struct gfs2_glock
, gl_reclaim
);
1593 list_del_init(&gl
->gl_reclaim
);
1594 spin_unlock(&sdp
->sd_reclaim_lock
);
1596 atomic_dec(&sdp
->sd_reclaim_count
);
1597 atomic_inc(&sdp
->sd_reclaimed
);
1599 if (gfs2_glmutex_trylock(gl
)) {
1600 if (list_empty(&gl
->gl_holders
) &&
1601 gl
->gl_state
!= LM_ST_UNLOCKED
&& demote_ok(gl
))
1602 handle_callback(gl
, LM_ST_UNLOCKED
, 0);
1603 gfs2_glmutex_unlock(gl
);
1610 * examine_bucket - Call a function for glock in a hash bucket
1611 * @examiner: the function
1612 * @sdp: the filesystem
1613 * @bucket: the bucket
1615 * Returns: 1 if the bucket has entries
1618 static int examine_bucket(glock_examiner examiner
, struct gfs2_sbd
*sdp
,
1621 struct gfs2_glock
*gl
, *prev
= NULL
;
1622 int has_entries
= 0;
1623 struct hlist_head
*head
= &gl_hash_table
[hash
].hb_list
;
1625 read_lock(gl_lock_addr(hash
));
1626 /* Can't use hlist_for_each_entry - don't want prefetch here */
1627 if (hlist_empty(head
))
1629 gl
= list_entry(head
->first
, struct gfs2_glock
, gl_list
);
1631 if (gl
->gl_sbd
== sdp
) {
1632 gfs2_glock_hold(gl
);
1633 read_unlock(gl_lock_addr(hash
));
1635 gfs2_glock_put(prev
);
1639 read_lock(gl_lock_addr(hash
));
1641 if (gl
->gl_list
.next
== NULL
)
1643 gl
= list_entry(gl
->gl_list
.next
, struct gfs2_glock
, gl_list
);
1646 read_unlock(gl_lock_addr(hash
));
1648 gfs2_glock_put(prev
);
1653 * scan_glock - look at a glock and see if we can reclaim it
1654 * @gl: the glock to look at
1658 static void scan_glock(struct gfs2_glock
*gl
)
1660 if (gl
->gl_ops
== &gfs2_inode_glops
&& gl
->gl_object
)
1663 if (gfs2_glmutex_trylock(gl
)) {
1664 if (list_empty(&gl
->gl_holders
) &&
1665 gl
->gl_state
!= LM_ST_UNLOCKED
&& demote_ok(gl
))
1667 gfs2_glmutex_unlock(gl
);
1672 gfs2_glmutex_unlock(gl
);
1673 gfs2_glock_schedule_for_reclaim(gl
);
1677 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
1678 * @sdp: the filesystem
1682 void gfs2_scand_internal(struct gfs2_sbd
*sdp
)
1686 for (x
= 0; x
< GFS2_GL_HASH_SIZE
; x
++)
1687 examine_bucket(scan_glock
, sdp
, x
);
1691 * clear_glock - look at a glock and see if we can free it from glock cache
1692 * @gl: the glock to look at
1696 static void clear_glock(struct gfs2_glock
*gl
)
1698 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1701 spin_lock(&sdp
->sd_reclaim_lock
);
1702 if (!list_empty(&gl
->gl_reclaim
)) {
1703 list_del_init(&gl
->gl_reclaim
);
1704 atomic_dec(&sdp
->sd_reclaim_count
);
1705 spin_unlock(&sdp
->sd_reclaim_lock
);
1706 released
= gfs2_glock_put(gl
);
1707 gfs2_assert(sdp
, !released
);
1709 spin_unlock(&sdp
->sd_reclaim_lock
);
1712 if (gfs2_glmutex_trylock(gl
)) {
1713 if (list_empty(&gl
->gl_holders
) &&
1714 gl
->gl_state
!= LM_ST_UNLOCKED
)
1715 handle_callback(gl
, LM_ST_UNLOCKED
, 0);
1716 gfs2_glmutex_unlock(gl
);
1721 * gfs2_gl_hash_clear - Empty out the glock hash table
1722 * @sdp: the filesystem
1723 * @wait: wait until it's all gone
1725 * Called when unmounting the filesystem, or when inter-node lock manager
1726 * requests DROPLOCKS because it is running out of capacity.
1729 void gfs2_gl_hash_clear(struct gfs2_sbd
*sdp
, int wait
)
1739 for (x
= 0; x
< GFS2_GL_HASH_SIZE
; x
++) {
1740 if (examine_bucket(clear_glock
, sdp
, x
))
1747 if (time_after_eq(jiffies
,
1748 t
+ gfs2_tune_get(sdp
, gt_stall_secs
) * HZ
)) {
1749 fs_warn(sdp
, "Unmount seems to be stalled. "
1750 "Dumping lock state...\n");
1751 gfs2_dump_lockstate(sdp
);
1755 down_write(&gfs2_umount_flush_sem
);
1756 invalidate_inodes(sdp
->sd_vfs
);
1757 up_write(&gfs2_umount_flush_sem
);
1763 * Diagnostic routines to help debug distributed deadlock
1766 static void gfs2_print_symbol(struct glock_iter
*gi
, const char *fmt
,
1767 unsigned long address
)
1769 char buffer
[KSYM_SYMBOL_LEN
];
1771 sprint_symbol(buffer
, address
);
1772 print_dbg(gi
, fmt
, buffer
);
1776 * dump_holder - print information about a glock holder
1777 * @str: a string naming the type of holder
1778 * @gh: the glock holder
1780 * Returns: 0 on success, -ENOBUFS when we run out of space
1783 static int dump_holder(struct glock_iter
*gi
, char *str
,
1784 struct gfs2_holder
*gh
)
1787 struct task_struct
*gh_owner
;
1789 print_dbg(gi
, " %s\n", str
);
1790 if (gh
->gh_owner_pid
) {
1791 print_dbg(gi
, " owner = %ld ", (long)gh
->gh_owner_pid
);
1792 gh_owner
= find_task_by_pid(gh
->gh_owner_pid
);
1794 print_dbg(gi
, "(%s)\n", gh_owner
->comm
);
1796 print_dbg(gi
, "(ended)\n");
1798 print_dbg(gi
, " owner = -1\n");
1799 print_dbg(gi
, " gh_state = %u\n", gh
->gh_state
);
1800 print_dbg(gi
, " gh_flags =");
1801 for (x
= 0; x
< 32; x
++)
1802 if (gh
->gh_flags
& (1 << x
))
1803 print_dbg(gi
, " %u", x
);
1804 print_dbg(gi
, " \n");
1805 print_dbg(gi
, " error = %d\n", gh
->gh_error
);
1806 print_dbg(gi
, " gh_iflags =");
1807 for (x
= 0; x
< 32; x
++)
1808 if (test_bit(x
, &gh
->gh_iflags
))
1809 print_dbg(gi
, " %u", x
);
1810 print_dbg(gi
, " \n");
1811 gfs2_print_symbol(gi
, " initialized at: %s\n", gh
->gh_ip
);
1817 * dump_inode - print information about an inode
1820 * Returns: 0 on success, -ENOBUFS when we run out of space
1823 static int dump_inode(struct glock_iter
*gi
, struct gfs2_inode
*ip
)
1827 print_dbg(gi
, " Inode:\n");
1828 print_dbg(gi
, " num = %llu/%llu\n",
1829 (unsigned long long)ip
->i_no_formal_ino
,
1830 (unsigned long long)ip
->i_no_addr
);
1831 print_dbg(gi
, " type = %u\n", IF2DT(ip
->i_inode
.i_mode
));
1832 print_dbg(gi
, " i_flags =");
1833 for (x
= 0; x
< 32; x
++)
1834 if (test_bit(x
, &ip
->i_flags
))
1835 print_dbg(gi
, " %u", x
);
1836 print_dbg(gi
, " \n");
1841 * dump_glock - print information about a glock
1843 * @count: where we are in the buffer
1845 * Returns: 0 on success, -ENOBUFS when we run out of space
1848 static int dump_glock(struct glock_iter
*gi
, struct gfs2_glock
*gl
)
1850 struct gfs2_holder
*gh
;
1852 int error
= -ENOBUFS
;
1853 struct task_struct
*gl_owner
;
1855 spin_lock(&gl
->gl_spin
);
1857 print_dbg(gi
, "Glock 0x%p (%u, %llu)\n", gl
, gl
->gl_name
.ln_type
,
1858 (unsigned long long)gl
->gl_name
.ln_number
);
1859 print_dbg(gi
, " gl_flags =");
1860 for (x
= 0; x
< 32; x
++) {
1861 if (test_bit(x
, &gl
->gl_flags
))
1862 print_dbg(gi
, " %u", x
);
1864 if (!test_bit(GLF_LOCK
, &gl
->gl_flags
))
1865 print_dbg(gi
, " (unlocked)");
1866 print_dbg(gi
, " \n");
1867 print_dbg(gi
, " gl_ref = %d\n", atomic_read(&gl
->gl_ref
));
1868 print_dbg(gi
, " gl_state = %u\n", gl
->gl_state
);
1869 if (gl
->gl_owner_pid
) {
1870 gl_owner
= find_task_by_pid(gl
->gl_owner_pid
);
1872 print_dbg(gi
, " gl_owner = pid %d (%s)\n",
1873 gl
->gl_owner_pid
, gl_owner
->comm
);
1875 print_dbg(gi
, " gl_owner = %d (ended)\n",
1878 print_dbg(gi
, " gl_owner = -1\n");
1879 print_dbg(gi
, " gl_ip = %lu\n", gl
->gl_ip
);
1880 print_dbg(gi
, " req_gh = %s\n", (gl
->gl_req_gh
) ? "yes" : "no");
1881 print_dbg(gi
, " req_bh = %s\n", (gl
->gl_req_bh
) ? "yes" : "no");
1882 print_dbg(gi
, " lvb_count = %d\n", atomic_read(&gl
->gl_lvb_count
));
1883 print_dbg(gi
, " object = %s\n", (gl
->gl_object
) ? "yes" : "no");
1884 print_dbg(gi
, " le = %s\n",
1885 (list_empty(&gl
->gl_le
.le_list
)) ? "no" : "yes");
1886 print_dbg(gi
, " reclaim = %s\n",
1887 (list_empty(&gl
->gl_reclaim
)) ? "no" : "yes");
1889 print_dbg(gi
, " aspace = 0x%p nrpages = %lu\n", gl
->gl_aspace
,
1890 gl
->gl_aspace
->i_mapping
->nrpages
);
1892 print_dbg(gi
, " aspace = no\n");
1893 print_dbg(gi
, " ail = %d\n", atomic_read(&gl
->gl_ail_count
));
1894 if (gl
->gl_req_gh
) {
1895 error
= dump_holder(gi
, "Request", gl
->gl_req_gh
);
1899 list_for_each_entry(gh
, &gl
->gl_holders
, gh_list
) {
1900 error
= dump_holder(gi
, "Holder", gh
);
1904 list_for_each_entry(gh
, &gl
->gl_waiters1
, gh_list
) {
1905 error
= dump_holder(gi
, "Waiter1", gh
);
1909 list_for_each_entry(gh
, &gl
->gl_waiters3
, gh_list
) {
1910 error
= dump_holder(gi
, "Waiter3", gh
);
1914 if (test_bit(GLF_DEMOTE
, &gl
->gl_flags
)) {
1915 print_dbg(gi
, " Demotion req to state %u (%llu uS ago)\n",
1916 gl
->gl_demote_state
, (unsigned long long)
1917 (jiffies
- gl
->gl_demote_time
)*(1000000/HZ
));
1919 if (gl
->gl_ops
== &gfs2_inode_glops
&& gl
->gl_object
) {
1920 if (!test_bit(GLF_LOCK
, &gl
->gl_flags
) &&
1921 list_empty(&gl
->gl_holders
)) {
1922 error
= dump_inode(gi
, gl
->gl_object
);
1927 print_dbg(gi
, " Inode: busy\n");
1934 spin_unlock(&gl
->gl_spin
);
1939 * gfs2_dump_lockstate - print out the current lockstate
1940 * @sdp: the filesystem
1941 * @ub: the buffer to copy the information into
1943 * If @ub is NULL, dump the lockstate to the console.
1947 static int gfs2_dump_lockstate(struct gfs2_sbd
*sdp
)
1949 struct gfs2_glock
*gl
;
1950 struct hlist_node
*h
;
1954 for (x
= 0; x
< GFS2_GL_HASH_SIZE
; x
++) {
1956 read_lock(gl_lock_addr(x
));
1958 hlist_for_each_entry(gl
, h
, &gl_hash_table
[x
].hb_list
, gl_list
) {
1959 if (gl
->gl_sbd
!= sdp
)
1962 error
= dump_glock(NULL
, gl
);
1967 read_unlock(gl_lock_addr(x
));
1977 int __init
gfs2_glock_init(void)
1980 for(i
= 0; i
< GFS2_GL_HASH_SIZE
; i
++) {
1981 INIT_HLIST_HEAD(&gl_hash_table
[i
].hb_list
);
1983 #ifdef GL_HASH_LOCK_SZ
1984 for(i
= 0; i
< GL_HASH_LOCK_SZ
; i
++) {
1985 rwlock_init(&gl_hash_locks
[i
]);
1991 static int gfs2_glock_iter_next(struct glock_iter
*gi
)
1993 read_lock(gl_lock_addr(gi
->hash
));
1995 if (!gi
->hb_list
) { /* If we don't have a hash bucket yet */
1996 gi
->hb_list
= &gl_hash_table
[gi
->hash
].hb_list
;
1997 if (hlist_empty(gi
->hb_list
)) {
1998 read_unlock(gl_lock_addr(gi
->hash
));
2000 read_lock(gl_lock_addr(gi
->hash
));
2002 if (gi
->hash
>= GFS2_GL_HASH_SIZE
) {
2003 read_unlock(gl_lock_addr(gi
->hash
));
2009 if (!hlist_empty(gi
->hb_list
)) {
2010 gi
->gl
= list_entry(gi
->hb_list
->first
,
2015 if (gi
->gl
->gl_list
.next
== NULL
) {
2016 read_unlock(gl_lock_addr(gi
->hash
));
2018 read_lock(gl_lock_addr(gi
->hash
));
2022 gi
->gl
= list_entry(gi
->gl
->gl_list
.next
,
2023 struct gfs2_glock
, gl_list
);
2028 read_unlock(gl_lock_addr(gi
->hash
));
2032 static void gfs2_glock_iter_free(struct glock_iter
*gi
)
2037 static struct glock_iter
*gfs2_glock_iter_init(struct gfs2_sbd
*sdp
)
2039 struct glock_iter
*gi
;
2041 gi
= kmalloc(sizeof (*gi
), GFP_KERNEL
);
2050 memset(gi
->string
, 0, sizeof(gi
->string
));
2052 if (gfs2_glock_iter_next(gi
)) {
2053 gfs2_glock_iter_free(gi
);
2060 static void *gfs2_glock_seq_start(struct seq_file
*file
, loff_t
*pos
)
2062 struct glock_iter
*gi
;
2065 gi
= gfs2_glock_iter_init(file
->private);
2070 if (gfs2_glock_iter_next(gi
)) {
2071 gfs2_glock_iter_free(gi
);
2079 static void *gfs2_glock_seq_next(struct seq_file
*file
, void *iter_ptr
,
2082 struct glock_iter
*gi
= iter_ptr
;
2086 if (gfs2_glock_iter_next(gi
)) {
2087 gfs2_glock_iter_free(gi
);
2094 static void gfs2_glock_seq_stop(struct seq_file
*file
, void *iter_ptr
)
2096 /* nothing for now */
2099 static int gfs2_glock_seq_show(struct seq_file
*file
, void *iter_ptr
)
2101 struct glock_iter
*gi
= iter_ptr
;
2104 dump_glock(gi
, gi
->gl
);
2109 static struct seq_operations gfs2_glock_seq_ops
= {
2110 .start
= gfs2_glock_seq_start
,
2111 .next
= gfs2_glock_seq_next
,
2112 .stop
= gfs2_glock_seq_stop
,
2113 .show
= gfs2_glock_seq_show
,
2116 static int gfs2_debugfs_open(struct inode
*inode
, struct file
*file
)
2118 struct seq_file
*seq
;
2121 ret
= seq_open(file
, &gfs2_glock_seq_ops
);
2125 seq
= file
->private_data
;
2126 seq
->private = inode
->i_private
;
2131 static const struct file_operations gfs2_debug_fops
= {
2132 .owner
= THIS_MODULE
,
2133 .open
= gfs2_debugfs_open
,
2135 .llseek
= seq_lseek
,
2136 .release
= seq_release
2139 int gfs2_create_debugfs_file(struct gfs2_sbd
*sdp
)
2141 sdp
->debugfs_dir
= debugfs_create_dir(sdp
->sd_table_name
, gfs2_root
);
2142 if (!sdp
->debugfs_dir
)
2144 sdp
->debugfs_dentry_glocks
= debugfs_create_file("glocks",
2146 sdp
->debugfs_dir
, sdp
,
2148 if (!sdp
->debugfs_dentry_glocks
)
2154 void gfs2_delete_debugfs_file(struct gfs2_sbd
*sdp
)
2156 if (sdp
&& sdp
->debugfs_dir
) {
2157 if (sdp
->debugfs_dentry_glocks
) {
2158 debugfs_remove(sdp
->debugfs_dentry_glocks
);
2159 sdp
->debugfs_dentry_glocks
= NULL
;
2161 debugfs_remove(sdp
->debugfs_dir
);
2162 sdp
->debugfs_dir
= NULL
;
2166 int gfs2_register_debugfs(void)
2168 gfs2_root
= debugfs_create_dir("gfs2", NULL
);
2169 return gfs2_root
? 0 : -ENOMEM
;
2172 void gfs2_unregister_debugfs(void)
2174 debugfs_remove(gfs2_root
);