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/kref.h>
19 #include <linux/kallsyms.h>
20 #include <linux/gfs2_ondisk.h>
21 #include <asm/uaccess.h>
24 #include "lm_interface.h"
36 /* Must be kept in sync with the beginning of struct gfs2_glock */
38 struct list_head gl_list
;
39 unsigned long gl_flags
;
43 struct gfs2_holder gr_gh
;
44 struct work_struct gr_work
;
47 typedef void (*glock_examiner
) (struct gfs2_glock
* gl
);
49 static int gfs2_dump_lockstate(struct gfs2_sbd
*sdp
);
50 static int dump_glock(struct gfs2_glock
*gl
);
52 static struct gfs2_gl_hash_bucket gl_hash_table
[GFS2_GL_HASH_SIZE
];
55 * relaxed_state_ok - is a requested lock compatible with the current lock mode?
56 * @actual: the current state of the lock
57 * @requested: the lock state that was requested by the caller
58 * @flags: the modifier flags passed in by the caller
60 * Returns: 1 if the locks are compatible, 0 otherwise
63 static inline int relaxed_state_ok(unsigned int actual
, unsigned requested
,
66 if (actual
== requested
)
72 if (actual
== LM_ST_EXCLUSIVE
&& requested
== LM_ST_SHARED
)
75 if (actual
!= LM_ST_UNLOCKED
&& (flags
& LM_FLAG_ANY
))
82 * gl_hash() - Turn glock number into hash bucket number
83 * @lock: The glock number
85 * Returns: The number of the corresponding hash bucket
88 static unsigned int gl_hash(const struct gfs2_sbd
*sdp
,
89 const struct lm_lockname
*name
)
93 h
= jhash(&name
->ln_number
, sizeof(u64
), 0);
94 h
= jhash(&name
->ln_type
, sizeof(unsigned int), h
);
95 h
= jhash(&sdp
, sizeof(struct gfs2_sbd
*), h
);
96 h
&= GFS2_GL_HASH_MASK
;
102 * glock_free() - Perform a few checks and then release struct gfs2_glock
103 * @gl: The glock to release
105 * Also calls lock module to release its internal structure for this glock.
109 static void glock_free(struct gfs2_glock
*gl
)
111 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
112 struct inode
*aspace
= gl
->gl_aspace
;
114 gfs2_lm_put_lock(sdp
, gl
->gl_lock
);
117 gfs2_aspace_put(aspace
);
119 kmem_cache_free(gfs2_glock_cachep
, gl
);
123 * gfs2_glock_hold() - increment reference count on glock
124 * @gl: The glock to hold
128 void gfs2_glock_hold(struct gfs2_glock
*gl
)
130 kref_get(&gl
->gl_ref
);
133 /* All work is done after the return from kref_put() so we
134 can release the write_lock before the free. */
136 static void kill_glock(struct kref
*kref
)
138 struct gfs2_glock
*gl
= container_of(kref
, struct gfs2_glock
, gl_ref
);
139 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
141 gfs2_assert(sdp
, gl
->gl_state
== LM_ST_UNLOCKED
);
142 gfs2_assert(sdp
, list_empty(&gl
->gl_reclaim
));
143 gfs2_assert(sdp
, list_empty(&gl
->gl_holders
));
144 gfs2_assert(sdp
, list_empty(&gl
->gl_waiters1
));
145 gfs2_assert(sdp
, list_empty(&gl
->gl_waiters2
));
146 gfs2_assert(sdp
, list_empty(&gl
->gl_waiters3
));
150 * gfs2_glock_put() - Decrement reference count on glock
151 * @gl: The glock to put
155 int gfs2_glock_put(struct gfs2_glock
*gl
)
157 struct gfs2_gl_hash_bucket
*bucket
= gl
->gl_bucket
;
160 write_lock(&bucket
->hb_lock
);
161 if (kref_put(&gl
->gl_ref
, kill_glock
)) {
162 list_del_init(&gl
->gl_list
);
163 write_unlock(&bucket
->hb_lock
);
164 BUG_ON(spin_is_locked(&gl
->gl_spin
));
169 write_unlock(&bucket
->hb_lock
);
175 * queue_empty - check to see if a glock's queue is empty
177 * @head: the head of the queue to check
179 * This function protects the list in the event that a process already
180 * has a holder on the list and is adding a second holder for itself.
181 * The glmutex lock is what generally prevents processes from working
182 * on the same glock at once, but the special case of adding a second
183 * holder for yourself ("recursive" locking) doesn't involve locking
184 * glmutex, making the spin lock necessary.
186 * Returns: 1 if the queue is empty
189 static inline int queue_empty(struct gfs2_glock
*gl
, struct list_head
*head
)
192 spin_lock(&gl
->gl_spin
);
193 empty
= list_empty(head
);
194 spin_unlock(&gl
->gl_spin
);
199 * search_bucket() - Find struct gfs2_glock by lock number
200 * @bucket: the bucket to search
201 * @name: The lock name
203 * Returns: NULL, or the struct gfs2_glock with the requested number
206 static struct gfs2_glock
*search_bucket(struct gfs2_gl_hash_bucket
*bucket
,
207 const struct gfs2_sbd
*sdp
,
208 const struct lm_lockname
*name
)
210 struct gfs2_glock
*gl
;
212 list_for_each_entry(gl
, &bucket
->hb_list
, gl_list
) {
213 if (test_bit(GLF_PLUG
, &gl
->gl_flags
))
215 if (!lm_name_equal(&gl
->gl_name
, name
))
217 if (gl
->gl_sbd
!= sdp
)
220 kref_get(&gl
->gl_ref
);
229 * gfs2_glock_find() - Find glock by lock number
230 * @sdp: The GFS2 superblock
231 * @name: The lock name
233 * Returns: NULL, or the struct gfs2_glock with the requested number
236 static struct gfs2_glock
*gfs2_glock_find(const struct gfs2_sbd
*sdp
,
237 const struct lm_lockname
*name
)
239 struct gfs2_gl_hash_bucket
*bucket
= &gl_hash_table
[gl_hash(sdp
, name
)];
240 struct gfs2_glock
*gl
;
242 read_lock(&bucket
->hb_lock
);
243 gl
= search_bucket(bucket
, sdp
, name
);
244 read_unlock(&bucket
->hb_lock
);
250 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
251 * @sdp: The GFS2 superblock
252 * @number: the lock number
253 * @glops: The glock_operations to use
254 * @create: If 0, don't create the glock if it doesn't exist
255 * @glp: the glock is returned here
257 * This does not lock a glock, just finds/creates structures for one.
262 int gfs2_glock_get(struct gfs2_sbd
*sdp
, u64 number
,
263 const struct gfs2_glock_operations
*glops
, int create
,
264 struct gfs2_glock
**glp
)
266 struct lm_lockname name
;
267 struct gfs2_glock
*gl
, *tmp
;
268 struct gfs2_gl_hash_bucket
*bucket
;
271 name
.ln_number
= number
;
272 name
.ln_type
= glops
->go_type
;
273 bucket
= &gl_hash_table
[gl_hash(sdp
, &name
)];
275 read_lock(&bucket
->hb_lock
);
276 gl
= search_bucket(bucket
, sdp
, &name
);
277 read_unlock(&bucket
->hb_lock
);
284 gl
= kmem_cache_alloc(gfs2_glock_cachep
, GFP_KERNEL
);
290 kref_init(&gl
->gl_ref
);
291 gl
->gl_state
= LM_ST_UNLOCKED
;
295 gl
->gl_req_gh
= NULL
;
296 gl
->gl_req_bh
= NULL
;
298 gl
->gl_stamp
= jiffies
;
299 gl
->gl_object
= NULL
;
300 gl
->gl_bucket
= bucket
;
302 gl
->gl_aspace
= NULL
;
303 lops_init_le(&gl
->gl_le
, &gfs2_glock_lops
);
305 /* If this glock protects actual on-disk data or metadata blocks,
306 create a VFS inode to manage the pages/buffers holding them. */
307 if (glops
== &gfs2_inode_glops
|| glops
== &gfs2_rgrp_glops
) {
308 gl
->gl_aspace
= gfs2_aspace_get(sdp
);
309 if (!gl
->gl_aspace
) {
315 error
= gfs2_lm_get_lock(sdp
, &name
, &gl
->gl_lock
);
319 write_lock(&bucket
->hb_lock
);
320 tmp
= search_bucket(bucket
, sdp
, &name
);
322 write_unlock(&bucket
->hb_lock
);
326 list_add_tail(&gl
->gl_list
, &bucket
->hb_list
);
327 write_unlock(&bucket
->hb_lock
);
336 gfs2_aspace_put(gl
->gl_aspace
);
338 kmem_cache_free(gfs2_glock_cachep
, gl
);
343 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
345 * @state: the state we're requesting
346 * @flags: the modifier flags
347 * @gh: the holder structure
351 void gfs2_holder_init(struct gfs2_glock
*gl
, unsigned int state
, unsigned flags
,
352 struct gfs2_holder
*gh
)
354 INIT_LIST_HEAD(&gh
->gh_list
);
356 gh
->gh_ip
= (unsigned long)__builtin_return_address(0);
357 gh
->gh_owner
= current
;
358 gh
->gh_state
= state
;
359 gh
->gh_flags
= flags
;
362 init_completion(&gh
->gh_wait
);
364 if (gh
->gh_state
== LM_ST_EXCLUSIVE
)
365 gh
->gh_flags
|= GL_LOCAL_EXCL
;
371 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
372 * @state: the state we're requesting
373 * @flags: the modifier flags
374 * @gh: the holder structure
376 * Don't mess with the glock.
380 void gfs2_holder_reinit(unsigned int state
, unsigned flags
, struct gfs2_holder
*gh
)
382 gh
->gh_state
= state
;
383 gh
->gh_flags
= flags
;
384 if (gh
->gh_state
== LM_ST_EXCLUSIVE
)
385 gh
->gh_flags
|= GL_LOCAL_EXCL
;
387 gh
->gh_iflags
&= 1 << HIF_ALLOCED
;
388 gh
->gh_ip
= (unsigned long)__builtin_return_address(0);
392 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
393 * @gh: the holder structure
397 void gfs2_holder_uninit(struct gfs2_holder
*gh
)
399 gfs2_glock_put(gh
->gh_gl
);
405 * gfs2_holder_get - get a struct gfs2_holder structure
407 * @state: the state we're requesting
408 * @flags: the modifier flags
411 * Figure out how big an impact this function has. Either:
412 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
413 * 2) Leave it like it is
415 * Returns: the holder structure, NULL on ENOMEM
418 static struct gfs2_holder
*gfs2_holder_get(struct gfs2_glock
*gl
,
420 int flags
, gfp_t gfp_flags
)
422 struct gfs2_holder
*gh
;
424 gh
= kmalloc(sizeof(struct gfs2_holder
), gfp_flags
);
428 gfs2_holder_init(gl
, state
, flags
, gh
);
429 set_bit(HIF_ALLOCED
, &gh
->gh_iflags
);
430 gh
->gh_ip
= (unsigned long)__builtin_return_address(0);
435 * gfs2_holder_put - get rid of a struct gfs2_holder structure
436 * @gh: the holder structure
440 static void gfs2_holder_put(struct gfs2_holder
*gh
)
442 gfs2_holder_uninit(gh
);
447 * rq_mutex - process a mutex request in the queue
448 * @gh: the glock holder
450 * Returns: 1 if the queue is blocked
453 static int rq_mutex(struct gfs2_holder
*gh
)
455 struct gfs2_glock
*gl
= gh
->gh_gl
;
457 list_del_init(&gh
->gh_list
);
458 /* gh->gh_error never examined. */
459 set_bit(GLF_LOCK
, &gl
->gl_flags
);
460 complete(&gh
->gh_wait
);
466 * rq_promote - process a promote request in the queue
467 * @gh: the glock holder
469 * Acquire a new inter-node lock, or change a lock state to more restrictive.
471 * Returns: 1 if the queue is blocked
474 static int rq_promote(struct gfs2_holder
*gh
)
476 struct gfs2_glock
*gl
= gh
->gh_gl
;
477 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
478 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
480 if (!relaxed_state_ok(gl
->gl_state
, gh
->gh_state
, gh
->gh_flags
)) {
481 if (list_empty(&gl
->gl_holders
)) {
483 set_bit(GLF_LOCK
, &gl
->gl_flags
);
484 spin_unlock(&gl
->gl_spin
);
486 if (atomic_read(&sdp
->sd_reclaim_count
) >
487 gfs2_tune_get(sdp
, gt_reclaim_limit
) &&
488 !(gh
->gh_flags
& LM_FLAG_PRIORITY
)) {
489 gfs2_reclaim_glock(sdp
);
490 gfs2_reclaim_glock(sdp
);
493 glops
->go_xmote_th(gl
, gh
->gh_state
, gh
->gh_flags
);
494 spin_lock(&gl
->gl_spin
);
499 if (list_empty(&gl
->gl_holders
)) {
500 set_bit(HIF_FIRST
, &gh
->gh_iflags
);
501 set_bit(GLF_LOCK
, &gl
->gl_flags
);
503 struct gfs2_holder
*next_gh
;
504 if (gh
->gh_flags
& GL_LOCAL_EXCL
)
506 next_gh
= list_entry(gl
->gl_holders
.next
, struct gfs2_holder
,
508 if (next_gh
->gh_flags
& GL_LOCAL_EXCL
)
512 list_move_tail(&gh
->gh_list
, &gl
->gl_holders
);
514 set_bit(HIF_HOLDER
, &gh
->gh_iflags
);
516 complete(&gh
->gh_wait
);
522 * rq_demote - process a demote request in the queue
523 * @gh: the glock holder
525 * Returns: 1 if the queue is blocked
528 static int rq_demote(struct gfs2_holder
*gh
)
530 struct gfs2_glock
*gl
= gh
->gh_gl
;
531 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
533 if (!list_empty(&gl
->gl_holders
))
536 if (gl
->gl_state
== gh
->gh_state
|| gl
->gl_state
== LM_ST_UNLOCKED
) {
537 list_del_init(&gh
->gh_list
);
539 spin_unlock(&gl
->gl_spin
);
540 if (test_bit(HIF_DEALLOC
, &gh
->gh_iflags
))
543 complete(&gh
->gh_wait
);
544 spin_lock(&gl
->gl_spin
);
547 set_bit(GLF_LOCK
, &gl
->gl_flags
);
548 spin_unlock(&gl
->gl_spin
);
550 if (gh
->gh_state
== LM_ST_UNLOCKED
||
551 gl
->gl_state
!= LM_ST_EXCLUSIVE
)
552 glops
->go_drop_th(gl
);
554 glops
->go_xmote_th(gl
, gh
->gh_state
, gh
->gh_flags
);
556 spin_lock(&gl
->gl_spin
);
563 * rq_greedy - process a queued request to drop greedy status
564 * @gh: the glock holder
566 * Returns: 1 if the queue is blocked
569 static int rq_greedy(struct gfs2_holder
*gh
)
571 struct gfs2_glock
*gl
= gh
->gh_gl
;
573 list_del_init(&gh
->gh_list
);
574 /* gh->gh_error never examined. */
575 clear_bit(GLF_GREEDY
, &gl
->gl_flags
);
576 spin_unlock(&gl
->gl_spin
);
578 gfs2_holder_uninit(gh
);
579 kfree(container_of(gh
, struct greedy
, gr_gh
));
581 spin_lock(&gl
->gl_spin
);
587 * run_queue - process holder structures on a glock
591 static void run_queue(struct gfs2_glock
*gl
)
593 struct gfs2_holder
*gh
;
597 if (test_bit(GLF_LOCK
, &gl
->gl_flags
))
600 if (!list_empty(&gl
->gl_waiters1
)) {
601 gh
= list_entry(gl
->gl_waiters1
.next
,
602 struct gfs2_holder
, gh_list
);
604 if (test_bit(HIF_MUTEX
, &gh
->gh_iflags
))
605 blocked
= rq_mutex(gh
);
607 gfs2_assert_warn(gl
->gl_sbd
, 0);
609 } else if (!list_empty(&gl
->gl_waiters2
) &&
610 !test_bit(GLF_SKIP_WAITERS2
, &gl
->gl_flags
)) {
611 gh
= list_entry(gl
->gl_waiters2
.next
,
612 struct gfs2_holder
, gh_list
);
614 if (test_bit(HIF_DEMOTE
, &gh
->gh_iflags
))
615 blocked
= rq_demote(gh
);
616 else if (test_bit(HIF_GREEDY
, &gh
->gh_iflags
))
617 blocked
= rq_greedy(gh
);
619 gfs2_assert_warn(gl
->gl_sbd
, 0);
621 } else if (!list_empty(&gl
->gl_waiters3
)) {
622 gh
= list_entry(gl
->gl_waiters3
.next
,
623 struct gfs2_holder
, gh_list
);
625 if (test_bit(HIF_PROMOTE
, &gh
->gh_iflags
))
626 blocked
= rq_promote(gh
);
628 gfs2_assert_warn(gl
->gl_sbd
, 0);
639 * gfs2_glmutex_lock - acquire a local lock on a glock
642 * Gives caller exclusive access to manipulate a glock structure.
645 static void gfs2_glmutex_lock(struct gfs2_glock
*gl
)
647 struct gfs2_holder gh
;
649 gfs2_holder_init(gl
, 0, 0, &gh
);
650 set_bit(HIF_MUTEX
, &gh
.gh_iflags
);
652 spin_lock(&gl
->gl_spin
);
653 if (test_and_set_bit(GLF_LOCK
, &gl
->gl_flags
)) {
654 list_add_tail(&gh
.gh_list
, &gl
->gl_waiters1
);
656 gl
->gl_owner
= current
;
657 gl
->gl_ip
= (unsigned long)__builtin_return_address(0);
658 complete(&gh
.gh_wait
);
660 spin_unlock(&gl
->gl_spin
);
662 wait_for_completion(&gh
.gh_wait
);
663 gfs2_holder_uninit(&gh
);
667 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
670 * Returns: 1 if the glock is acquired
673 static int gfs2_glmutex_trylock(struct gfs2_glock
*gl
)
677 spin_lock(&gl
->gl_spin
);
678 if (test_and_set_bit(GLF_LOCK
, &gl
->gl_flags
)) {
681 gl
->gl_owner
= current
;
682 gl
->gl_ip
= (unsigned long)__builtin_return_address(0);
684 spin_unlock(&gl
->gl_spin
);
690 * gfs2_glmutex_unlock - release a local lock on a glock
695 static void gfs2_glmutex_unlock(struct gfs2_glock
*gl
)
697 spin_lock(&gl
->gl_spin
);
698 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
702 BUG_ON(!spin_is_locked(&gl
->gl_spin
));
703 spin_unlock(&gl
->gl_spin
);
707 * handle_callback - add a demote request to a lock's queue
709 * @state: the state the caller wants us to change to
711 * Note: This may fail sliently if we are out of memory.
714 static void handle_callback(struct gfs2_glock
*gl
, unsigned int state
)
716 struct gfs2_holder
*gh
, *new_gh
= NULL
;
719 spin_lock(&gl
->gl_spin
);
721 list_for_each_entry(gh
, &gl
->gl_waiters2
, gh_list
) {
722 if (test_bit(HIF_DEMOTE
, &gh
->gh_iflags
) &&
723 gl
->gl_req_gh
!= gh
) {
724 if (gh
->gh_state
!= state
)
725 gh
->gh_state
= LM_ST_UNLOCKED
;
731 list_add_tail(&new_gh
->gh_list
, &gl
->gl_waiters2
);
734 spin_unlock(&gl
->gl_spin
);
736 new_gh
= gfs2_holder_get(gl
, state
, LM_FLAG_TRY
, GFP_KERNEL
);
739 set_bit(HIF_DEMOTE
, &new_gh
->gh_iflags
);
740 set_bit(HIF_DEALLOC
, &new_gh
->gh_iflags
);
746 spin_unlock(&gl
->gl_spin
);
749 gfs2_holder_put(new_gh
);
752 void gfs2_glock_inode_squish(struct inode
*inode
)
754 struct gfs2_holder gh
;
755 struct gfs2_glock
*gl
= GFS2_I(inode
)->i_gl
;
756 gfs2_holder_init(gl
, LM_ST_UNLOCKED
, 0, &gh
);
757 set_bit(HIF_DEMOTE
, &gh
.gh_iflags
);
758 spin_lock(&gl
->gl_spin
);
759 gfs2_assert(inode
->i_sb
->s_fs_info
, list_empty(&gl
->gl_holders
));
760 list_add_tail(&gh
.gh_list
, &gl
->gl_waiters2
);
762 spin_unlock(&gl
->gl_spin
);
763 wait_for_completion(&gh
.gh_wait
);
764 gfs2_holder_uninit(&gh
);
768 * state_change - record that the glock is now in a different state
770 * @new_state the new state
774 static void state_change(struct gfs2_glock
*gl
, unsigned int new_state
)
778 held1
= (gl
->gl_state
!= LM_ST_UNLOCKED
);
779 held2
= (new_state
!= LM_ST_UNLOCKED
);
781 if (held1
!= held2
) {
788 gl
->gl_state
= new_state
;
792 * xmote_bh - Called after the lock module is done acquiring a lock
793 * @gl: The glock in question
794 * @ret: the int returned from the lock module
798 static void xmote_bh(struct gfs2_glock
*gl
, unsigned int ret
)
800 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
801 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
802 struct gfs2_holder
*gh
= gl
->gl_req_gh
;
803 int prev_state
= gl
->gl_state
;
806 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
807 gfs2_assert_warn(sdp
, queue_empty(gl
, &gl
->gl_holders
));
808 gfs2_assert_warn(sdp
, !(ret
& LM_OUT_ASYNC
));
810 state_change(gl
, ret
& LM_OUT_ST_MASK
);
812 if (prev_state
!= LM_ST_UNLOCKED
&& !(ret
& LM_OUT_CACHEABLE
)) {
814 glops
->go_inval(gl
, DIO_METADATA
| DIO_DATA
);
815 } else if (gl
->gl_state
== LM_ST_DEFERRED
) {
816 /* We might not want to do this here.
817 Look at moving to the inode glops. */
819 glops
->go_inval(gl
, DIO_DATA
);
822 /* Deal with each possible exit condition */
825 gl
->gl_stamp
= jiffies
;
826 else if (unlikely(test_bit(SDF_SHUTDOWN
, &sdp
->sd_flags
))) {
827 spin_lock(&gl
->gl_spin
);
828 list_del_init(&gh
->gh_list
);
830 spin_unlock(&gl
->gl_spin
);
831 } else if (test_bit(HIF_DEMOTE
, &gh
->gh_iflags
)) {
832 spin_lock(&gl
->gl_spin
);
833 list_del_init(&gh
->gh_list
);
834 if (gl
->gl_state
== gh
->gh_state
||
835 gl
->gl_state
== LM_ST_UNLOCKED
) {
838 if (gfs2_assert_warn(sdp
, gh
->gh_flags
&
839 (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
)) == -1)
840 fs_warn(sdp
, "ret = 0x%.8X\n", ret
);
841 gh
->gh_error
= GLR_TRYFAILED
;
843 spin_unlock(&gl
->gl_spin
);
845 if (ret
& LM_OUT_CANCELED
)
846 handle_callback(gl
, LM_ST_UNLOCKED
);
848 } else if (ret
& LM_OUT_CANCELED
) {
849 spin_lock(&gl
->gl_spin
);
850 list_del_init(&gh
->gh_list
);
851 gh
->gh_error
= GLR_CANCELED
;
852 spin_unlock(&gl
->gl_spin
);
854 } else if (relaxed_state_ok(gl
->gl_state
, gh
->gh_state
, gh
->gh_flags
)) {
855 spin_lock(&gl
->gl_spin
);
856 list_move_tail(&gh
->gh_list
, &gl
->gl_holders
);
858 set_bit(HIF_HOLDER
, &gh
->gh_iflags
);
859 spin_unlock(&gl
->gl_spin
);
861 set_bit(HIF_FIRST
, &gh
->gh_iflags
);
865 } else if (gh
->gh_flags
& (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
)) {
866 spin_lock(&gl
->gl_spin
);
867 list_del_init(&gh
->gh_list
);
868 gh
->gh_error
= GLR_TRYFAILED
;
869 spin_unlock(&gl
->gl_spin
);
872 if (gfs2_assert_withdraw(sdp
, 0) == -1)
873 fs_err(sdp
, "ret = 0x%.8X\n", ret
);
876 if (glops
->go_xmote_bh
)
877 glops
->go_xmote_bh(gl
);
880 spin_lock(&gl
->gl_spin
);
881 gl
->gl_req_gh
= NULL
;
882 gl
->gl_req_bh
= NULL
;
883 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
885 spin_unlock(&gl
->gl_spin
);
891 if (test_bit(HIF_DEALLOC
, &gh
->gh_iflags
))
894 complete(&gh
->gh_wait
);
899 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
900 * @gl: The glock in question
901 * @state: the requested state
902 * @flags: modifier flags to the lock call
906 void gfs2_glock_xmote_th(struct gfs2_glock
*gl
, unsigned int state
, int flags
)
908 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
909 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
910 int lck_flags
= flags
& (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
|
911 LM_FLAG_NOEXP
| LM_FLAG_ANY
|
913 unsigned int lck_ret
;
915 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
916 gfs2_assert_warn(sdp
, queue_empty(gl
, &gl
->gl_holders
));
917 gfs2_assert_warn(sdp
, state
!= LM_ST_UNLOCKED
);
918 gfs2_assert_warn(sdp
, state
!= gl
->gl_state
);
920 if (gl
->gl_state
== LM_ST_EXCLUSIVE
&& glops
->go_sync
)
921 glops
->go_sync(gl
, DIO_METADATA
| DIO_DATA
| DIO_RELEASE
);
924 gl
->gl_req_bh
= xmote_bh
;
926 lck_ret
= gfs2_lm_lock(sdp
, gl
->gl_lock
, gl
->gl_state
, state
, lck_flags
);
928 if (gfs2_assert_withdraw(sdp
, !(lck_ret
& LM_OUT_ERROR
)))
931 if (lck_ret
& LM_OUT_ASYNC
)
932 gfs2_assert_warn(sdp
, lck_ret
== LM_OUT_ASYNC
);
934 xmote_bh(gl
, lck_ret
);
938 * drop_bh - Called after a lock module unlock completes
940 * @ret: the return status
942 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
943 * Doesn't drop the reference on the glock the top half took out
947 static void drop_bh(struct gfs2_glock
*gl
, unsigned int ret
)
949 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
950 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
951 struct gfs2_holder
*gh
= gl
->gl_req_gh
;
953 clear_bit(GLF_PREFETCH
, &gl
->gl_flags
);
955 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
956 gfs2_assert_warn(sdp
, queue_empty(gl
, &gl
->gl_holders
));
957 gfs2_assert_warn(sdp
, !ret
);
959 state_change(gl
, LM_ST_UNLOCKED
);
962 glops
->go_inval(gl
, DIO_METADATA
| DIO_DATA
);
965 spin_lock(&gl
->gl_spin
);
966 list_del_init(&gh
->gh_list
);
968 spin_unlock(&gl
->gl_spin
);
971 if (glops
->go_drop_bh
)
972 glops
->go_drop_bh(gl
);
974 spin_lock(&gl
->gl_spin
);
975 gl
->gl_req_gh
= NULL
;
976 gl
->gl_req_bh
= NULL
;
977 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
979 spin_unlock(&gl
->gl_spin
);
984 if (test_bit(HIF_DEALLOC
, &gh
->gh_iflags
))
987 complete(&gh
->gh_wait
);
992 * gfs2_glock_drop_th - call into the lock module to unlock a lock
997 void gfs2_glock_drop_th(struct gfs2_glock
*gl
)
999 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1000 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1003 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
1004 gfs2_assert_warn(sdp
, queue_empty(gl
, &gl
->gl_holders
));
1005 gfs2_assert_warn(sdp
, gl
->gl_state
!= LM_ST_UNLOCKED
);
1007 if (gl
->gl_state
== LM_ST_EXCLUSIVE
&& glops
->go_sync
)
1008 glops
->go_sync(gl
, DIO_METADATA
| DIO_DATA
| DIO_RELEASE
);
1010 gfs2_glock_hold(gl
);
1011 gl
->gl_req_bh
= drop_bh
;
1013 ret
= gfs2_lm_unlock(sdp
, gl
->gl_lock
, gl
->gl_state
);
1015 if (gfs2_assert_withdraw(sdp
, !(ret
& LM_OUT_ERROR
)))
1021 gfs2_assert_warn(sdp
, ret
== LM_OUT_ASYNC
);
1025 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1026 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1028 * Don't cancel GL_NOCANCEL requests.
1031 static void do_cancels(struct gfs2_holder
*gh
)
1033 struct gfs2_glock
*gl
= gh
->gh_gl
;
1035 spin_lock(&gl
->gl_spin
);
1037 while (gl
->gl_req_gh
!= gh
&&
1038 !test_bit(HIF_HOLDER
, &gh
->gh_iflags
) &&
1039 !list_empty(&gh
->gh_list
)) {
1040 if (gl
->gl_req_bh
&& !(gl
->gl_req_gh
&&
1041 (gl
->gl_req_gh
->gh_flags
& GL_NOCANCEL
))) {
1042 spin_unlock(&gl
->gl_spin
);
1043 gfs2_lm_cancel(gl
->gl_sbd
, gl
->gl_lock
);
1045 spin_lock(&gl
->gl_spin
);
1047 spin_unlock(&gl
->gl_spin
);
1049 spin_lock(&gl
->gl_spin
);
1053 spin_unlock(&gl
->gl_spin
);
1057 * glock_wait_internal - wait on a glock acquisition
1058 * @gh: the glock holder
1060 * Returns: 0 on success
1063 static int glock_wait_internal(struct gfs2_holder
*gh
)
1065 struct gfs2_glock
*gl
= gh
->gh_gl
;
1066 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1067 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1069 if (test_bit(HIF_ABORTED
, &gh
->gh_iflags
))
1072 if (gh
->gh_flags
& (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
)) {
1073 spin_lock(&gl
->gl_spin
);
1074 if (gl
->gl_req_gh
!= gh
&&
1075 !test_bit(HIF_HOLDER
, &gh
->gh_iflags
) &&
1076 !list_empty(&gh
->gh_list
)) {
1077 list_del_init(&gh
->gh_list
);
1078 gh
->gh_error
= GLR_TRYFAILED
;
1080 spin_unlock(&gl
->gl_spin
);
1081 return gh
->gh_error
;
1083 spin_unlock(&gl
->gl_spin
);
1086 if (gh
->gh_flags
& LM_FLAG_PRIORITY
)
1089 wait_for_completion(&gh
->gh_wait
);
1092 return gh
->gh_error
;
1094 gfs2_assert_withdraw(sdp
, test_bit(HIF_HOLDER
, &gh
->gh_iflags
));
1095 gfs2_assert_withdraw(sdp
, relaxed_state_ok(gl
->gl_state
, gh
->gh_state
,
1098 if (test_bit(HIF_FIRST
, &gh
->gh_iflags
)) {
1099 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
1101 if (glops
->go_lock
) {
1102 gh
->gh_error
= glops
->go_lock(gh
);
1104 spin_lock(&gl
->gl_spin
);
1105 list_del_init(&gh
->gh_list
);
1106 spin_unlock(&gl
->gl_spin
);
1110 spin_lock(&gl
->gl_spin
);
1111 gl
->gl_req_gh
= NULL
;
1112 gl
->gl_req_bh
= NULL
;
1113 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
1115 spin_unlock(&gl
->gl_spin
);
1118 return gh
->gh_error
;
1121 static inline struct gfs2_holder
*
1122 find_holder_by_owner(struct list_head
*head
, struct task_struct
*owner
)
1124 struct gfs2_holder
*gh
;
1126 list_for_each_entry(gh
, head
, gh_list
) {
1127 if (gh
->gh_owner
== owner
)
1135 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1136 * @gh: the holder structure to add
1140 static void add_to_queue(struct gfs2_holder
*gh
)
1142 struct gfs2_glock
*gl
= gh
->gh_gl
;
1143 struct gfs2_holder
*existing
;
1145 BUG_ON(!gh
->gh_owner
);
1147 existing
= find_holder_by_owner(&gl
->gl_holders
, gh
->gh_owner
);
1149 print_symbol(KERN_WARNING
"original: %s\n", existing
->gh_ip
);
1150 printk(KERN_INFO
"pid : %d\n", existing
->gh_owner
->pid
);
1151 printk(KERN_INFO
"lock type : %d lock state : %d\n",
1152 existing
->gh_gl
->gl_name
.ln_type
, existing
->gh_gl
->gl_state
);
1153 print_symbol(KERN_WARNING
"new: %s\n", gh
->gh_ip
);
1154 printk(KERN_INFO
"pid : %d\n", gh
->gh_owner
->pid
);
1155 printk(KERN_INFO
"lock type : %d lock state : %d\n",
1156 gl
->gl_name
.ln_type
, gl
->gl_state
);
1160 existing
= find_holder_by_owner(&gl
->gl_waiters3
, gh
->gh_owner
);
1162 print_symbol(KERN_WARNING
"original: %s\n", existing
->gh_ip
);
1163 print_symbol(KERN_WARNING
"new: %s\n", gh
->gh_ip
);
1167 if (gh
->gh_flags
& LM_FLAG_PRIORITY
)
1168 list_add(&gh
->gh_list
, &gl
->gl_waiters3
);
1170 list_add_tail(&gh
->gh_list
, &gl
->gl_waiters3
);
1174 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1175 * @gh: the holder structure
1177 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1179 * Returns: 0, GLR_TRYFAILED, or errno on failure
1182 int gfs2_glock_nq(struct gfs2_holder
*gh
)
1184 struct gfs2_glock
*gl
= gh
->gh_gl
;
1185 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1189 if (unlikely(test_bit(SDF_SHUTDOWN
, &sdp
->sd_flags
))) {
1190 set_bit(HIF_ABORTED
, &gh
->gh_iflags
);
1194 set_bit(HIF_PROMOTE
, &gh
->gh_iflags
);
1196 spin_lock(&gl
->gl_spin
);
1199 spin_unlock(&gl
->gl_spin
);
1201 if (!(gh
->gh_flags
& GL_ASYNC
)) {
1202 error
= glock_wait_internal(gh
);
1203 if (error
== GLR_CANCELED
) {
1209 clear_bit(GLF_PREFETCH
, &gl
->gl_flags
);
1211 if (error
== GLR_TRYFAILED
&& (gh
->gh_flags
& GL_DUMP
))
1218 * gfs2_glock_poll - poll to see if an async request has been completed
1221 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1224 int gfs2_glock_poll(struct gfs2_holder
*gh
)
1226 struct gfs2_glock
*gl
= gh
->gh_gl
;
1229 spin_lock(&gl
->gl_spin
);
1231 if (test_bit(HIF_HOLDER
, &gh
->gh_iflags
))
1233 else if (list_empty(&gh
->gh_list
)) {
1234 if (gh
->gh_error
== GLR_CANCELED
) {
1235 spin_unlock(&gl
->gl_spin
);
1237 if (gfs2_glock_nq(gh
))
1244 spin_unlock(&gl
->gl_spin
);
1250 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1251 * @gh: the holder structure
1253 * Returns: 0, GLR_TRYFAILED, or errno on failure
1256 int gfs2_glock_wait(struct gfs2_holder
*gh
)
1260 error
= glock_wait_internal(gh
);
1261 if (error
== GLR_CANCELED
) {
1263 gh
->gh_flags
&= ~GL_ASYNC
;
1264 error
= gfs2_glock_nq(gh
);
1271 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1272 * @gh: the glock holder
1276 void gfs2_glock_dq(struct gfs2_holder
*gh
)
1278 struct gfs2_glock
*gl
= gh
->gh_gl
;
1279 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1281 if (gh
->gh_flags
& GL_NOCACHE
)
1282 handle_callback(gl
, LM_ST_UNLOCKED
);
1284 gfs2_glmutex_lock(gl
);
1286 spin_lock(&gl
->gl_spin
);
1287 list_del_init(&gh
->gh_list
);
1289 if (list_empty(&gl
->gl_holders
)) {
1290 spin_unlock(&gl
->gl_spin
);
1292 if (glops
->go_unlock
)
1293 glops
->go_unlock(gh
);
1295 gl
->gl_stamp
= jiffies
;
1297 spin_lock(&gl
->gl_spin
);
1300 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
1302 spin_unlock(&gl
->gl_spin
);
1306 * gfs2_glock_prefetch - Try to prefetch a glock
1308 * @state: the state to prefetch in
1309 * @flags: flags passed to go_xmote_th()
1313 static void gfs2_glock_prefetch(struct gfs2_glock
*gl
, unsigned int state
,
1316 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1318 spin_lock(&gl
->gl_spin
);
1320 if (test_bit(GLF_LOCK
, &gl
->gl_flags
) || !list_empty(&gl
->gl_holders
) ||
1321 !list_empty(&gl
->gl_waiters1
) || !list_empty(&gl
->gl_waiters2
) ||
1322 !list_empty(&gl
->gl_waiters3
) ||
1323 relaxed_state_ok(gl
->gl_state
, state
, flags
)) {
1324 spin_unlock(&gl
->gl_spin
);
1328 set_bit(GLF_PREFETCH
, &gl
->gl_flags
);
1329 set_bit(GLF_LOCK
, &gl
->gl_flags
);
1330 spin_unlock(&gl
->gl_spin
);
1332 glops
->go_xmote_th(gl
, state
, flags
);
1335 static void greedy_work(void *data
)
1337 struct greedy
*gr
= data
;
1338 struct gfs2_holder
*gh
= &gr
->gr_gh
;
1339 struct gfs2_glock
*gl
= gh
->gh_gl
;
1340 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1342 clear_bit(GLF_SKIP_WAITERS2
, &gl
->gl_flags
);
1344 if (glops
->go_greedy
)
1345 glops
->go_greedy(gl
);
1347 spin_lock(&gl
->gl_spin
);
1349 if (list_empty(&gl
->gl_waiters2
)) {
1350 clear_bit(GLF_GREEDY
, &gl
->gl_flags
);
1351 spin_unlock(&gl
->gl_spin
);
1352 gfs2_holder_uninit(gh
);
1355 gfs2_glock_hold(gl
);
1356 list_add_tail(&gh
->gh_list
, &gl
->gl_waiters2
);
1358 spin_unlock(&gl
->gl_spin
);
1364 * gfs2_glock_be_greedy -
1368 * Returns: 0 if go_greedy will be called, 1 otherwise
1371 int gfs2_glock_be_greedy(struct gfs2_glock
*gl
, unsigned int time
)
1374 struct gfs2_holder
*gh
;
1376 if (!time
|| gl
->gl_sbd
->sd_args
.ar_localcaching
||
1377 test_and_set_bit(GLF_GREEDY
, &gl
->gl_flags
))
1380 gr
= kmalloc(sizeof(struct greedy
), GFP_KERNEL
);
1382 clear_bit(GLF_GREEDY
, &gl
->gl_flags
);
1387 gfs2_holder_init(gl
, 0, 0, gh
);
1388 set_bit(HIF_GREEDY
, &gh
->gh_iflags
);
1389 INIT_WORK(&gr
->gr_work
, greedy_work
, gr
);
1391 set_bit(GLF_SKIP_WAITERS2
, &gl
->gl_flags
);
1392 schedule_delayed_work(&gr
->gr_work
, time
);
1398 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1399 * @gh: the holder structure
1403 void gfs2_glock_dq_uninit(struct gfs2_holder
*gh
)
1406 gfs2_holder_uninit(gh
);
1410 * gfs2_glock_nq_num - acquire a glock based on lock number
1411 * @sdp: the filesystem
1412 * @number: the lock number
1413 * @glops: the glock operations for the type of glock
1414 * @state: the state to acquire the glock in
1415 * @flags: modifier flags for the aquisition
1416 * @gh: the struct gfs2_holder
1421 int gfs2_glock_nq_num(struct gfs2_sbd
*sdp
, u64 number
,
1422 const struct gfs2_glock_operations
*glops
,
1423 unsigned int state
, int flags
, struct gfs2_holder
*gh
)
1425 struct gfs2_glock
*gl
;
1428 error
= gfs2_glock_get(sdp
, number
, glops
, CREATE
, &gl
);
1430 error
= gfs2_glock_nq_init(gl
, state
, flags
, gh
);
1438 * glock_compare - Compare two struct gfs2_glock structures for sorting
1439 * @arg_a: the first structure
1440 * @arg_b: the second structure
1444 static int glock_compare(const void *arg_a
, const void *arg_b
)
1446 struct gfs2_holder
*gh_a
= *(struct gfs2_holder
**)arg_a
;
1447 struct gfs2_holder
*gh_b
= *(struct gfs2_holder
**)arg_b
;
1448 struct lm_lockname
*a
= &gh_a
->gh_gl
->gl_name
;
1449 struct lm_lockname
*b
= &gh_b
->gh_gl
->gl_name
;
1452 if (a
->ln_number
> b
->ln_number
)
1454 else if (a
->ln_number
< b
->ln_number
)
1457 if (gh_a
->gh_state
== LM_ST_SHARED
&&
1458 gh_b
->gh_state
== LM_ST_EXCLUSIVE
)
1460 else if (!(gh_a
->gh_flags
& GL_LOCAL_EXCL
) &&
1461 (gh_b
->gh_flags
& GL_LOCAL_EXCL
))
1469 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1470 * @num_gh: the number of structures
1471 * @ghs: an array of struct gfs2_holder structures
1473 * Returns: 0 on success (all glocks acquired),
1474 * errno on failure (no glocks acquired)
1477 static int nq_m_sync(unsigned int num_gh
, struct gfs2_holder
*ghs
,
1478 struct gfs2_holder
**p
)
1483 for (x
= 0; x
< num_gh
; x
++)
1486 sort(p
, num_gh
, sizeof(struct gfs2_holder
*), glock_compare
, NULL
);
1488 for (x
= 0; x
< num_gh
; x
++) {
1489 p
[x
]->gh_flags
&= ~(LM_FLAG_TRY
| GL_ASYNC
);
1491 error
= gfs2_glock_nq(p
[x
]);
1494 gfs2_glock_dq(p
[x
]);
1503 * gfs2_glock_nq_m - acquire multiple glocks
1504 * @num_gh: the number of structures
1505 * @ghs: an array of struct gfs2_holder structures
1507 * Figure out how big an impact this function has. Either:
1508 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1509 * 2) Forget async stuff and just call nq_m_sync()
1510 * 3) Leave it like it is
1512 * Returns: 0 on success (all glocks acquired),
1513 * errno on failure (no glocks acquired)
1516 int gfs2_glock_nq_m(unsigned int num_gh
, struct gfs2_holder
*ghs
)
1520 int borked
= 0, serious
= 0;
1527 ghs
->gh_flags
&= ~(LM_FLAG_TRY
| GL_ASYNC
);
1528 return gfs2_glock_nq(ghs
);
1531 e
= kcalloc(num_gh
, sizeof(struct gfs2_holder
*), GFP_KERNEL
);
1535 for (x
= 0; x
< num_gh
; x
++) {
1536 ghs
[x
].gh_flags
|= LM_FLAG_TRY
| GL_ASYNC
;
1537 error
= gfs2_glock_nq(&ghs
[x
]);
1546 for (x
= 0; x
< num_gh
; x
++) {
1547 error
= e
[x
] = glock_wait_internal(&ghs
[x
]);
1550 if (error
!= GLR_TRYFAILED
&& error
!= GLR_CANCELED
)
1560 for (x
= 0; x
< num_gh
; x
++)
1562 gfs2_glock_dq(&ghs
[x
]);
1567 for (x
= 0; x
< num_gh
; x
++)
1568 gfs2_holder_reinit(ghs
[x
].gh_state
, ghs
[x
].gh_flags
,
1570 error
= nq_m_sync(num_gh
, ghs
, (struct gfs2_holder
**)e
);
1579 * gfs2_glock_dq_m - release multiple glocks
1580 * @num_gh: the number of structures
1581 * @ghs: an array of struct gfs2_holder structures
1585 void gfs2_glock_dq_m(unsigned int num_gh
, struct gfs2_holder
*ghs
)
1589 for (x
= 0; x
< num_gh
; x
++)
1590 gfs2_glock_dq(&ghs
[x
]);
1594 * gfs2_glock_dq_uninit_m - release multiple glocks
1595 * @num_gh: the number of structures
1596 * @ghs: an array of struct gfs2_holder structures
1600 void gfs2_glock_dq_uninit_m(unsigned int num_gh
, struct gfs2_holder
*ghs
)
1604 for (x
= 0; x
< num_gh
; x
++)
1605 gfs2_glock_dq_uninit(&ghs
[x
]);
1609 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1610 * @sdp: the filesystem
1611 * @number: the lock number
1612 * @glops: the glock operations for the type of glock
1613 * @state: the state to acquire the glock in
1614 * @flags: modifier flags for the aquisition
1619 void gfs2_glock_prefetch_num(struct gfs2_sbd
*sdp
, u64 number
,
1620 const struct gfs2_glock_operations
*glops
,
1621 unsigned int state
, int flags
)
1623 struct gfs2_glock
*gl
;
1626 if (atomic_read(&sdp
->sd_reclaim_count
) <
1627 gfs2_tune_get(sdp
, gt_reclaim_limit
)) {
1628 error
= gfs2_glock_get(sdp
, number
, glops
, CREATE
, &gl
);
1630 gfs2_glock_prefetch(gl
, state
, flags
);
1637 * gfs2_lvb_hold - attach a LVB from a glock
1638 * @gl: The glock in question
1642 int gfs2_lvb_hold(struct gfs2_glock
*gl
)
1646 gfs2_glmutex_lock(gl
);
1648 if (!atomic_read(&gl
->gl_lvb_count
)) {
1649 error
= gfs2_lm_hold_lvb(gl
->gl_sbd
, gl
->gl_lock
, &gl
->gl_lvb
);
1651 gfs2_glmutex_unlock(gl
);
1654 gfs2_glock_hold(gl
);
1656 atomic_inc(&gl
->gl_lvb_count
);
1658 gfs2_glmutex_unlock(gl
);
1664 * gfs2_lvb_unhold - detach a LVB from a glock
1665 * @gl: The glock in question
1669 void gfs2_lvb_unhold(struct gfs2_glock
*gl
)
1671 gfs2_glock_hold(gl
);
1672 gfs2_glmutex_lock(gl
);
1674 gfs2_assert(gl
->gl_sbd
, atomic_read(&gl
->gl_lvb_count
) > 0);
1675 if (atomic_dec_and_test(&gl
->gl_lvb_count
)) {
1676 gfs2_lm_unhold_lvb(gl
->gl_sbd
, gl
->gl_lock
, gl
->gl_lvb
);
1681 gfs2_glmutex_unlock(gl
);
1685 static void blocking_cb(struct gfs2_sbd
*sdp
, struct lm_lockname
*name
,
1688 struct gfs2_glock
*gl
;
1690 gl
= gfs2_glock_find(sdp
, name
);
1694 if (gl
->gl_ops
->go_callback
)
1695 gl
->gl_ops
->go_callback(gl
, state
);
1696 handle_callback(gl
, state
);
1698 spin_lock(&gl
->gl_spin
);
1700 spin_unlock(&gl
->gl_spin
);
1706 * gfs2_glock_cb - Callback used by locking module
1707 * @sdp: Pointer to the superblock
1708 * @type: Type of callback
1709 * @data: Type dependent data pointer
1711 * Called by the locking module when it wants to tell us something.
1712 * Either we need to drop a lock, one of our ASYNC requests completed, or
1713 * a journal from another client needs to be recovered.
1716 void gfs2_glock_cb(struct gfs2_sbd
*sdp
, unsigned int type
, void *data
)
1721 blocking_cb(sdp
, data
, LM_ST_UNLOCKED
);
1725 blocking_cb(sdp
, data
, LM_ST_DEFERRED
);
1729 blocking_cb(sdp
, data
, LM_ST_SHARED
);
1733 struct lm_async_cb
*async
= data
;
1734 struct gfs2_glock
*gl
;
1736 gl
= gfs2_glock_find(sdp
, &async
->lc_name
);
1737 if (gfs2_assert_warn(sdp
, gl
))
1739 if (!gfs2_assert_warn(sdp
, gl
->gl_req_bh
))
1740 gl
->gl_req_bh(gl
, async
->lc_ret
);
1745 case LM_CB_NEED_RECOVERY
:
1746 gfs2_jdesc_make_dirty(sdp
, *(unsigned int *)data
);
1747 if (sdp
->sd_recoverd_process
)
1748 wake_up_process(sdp
->sd_recoverd_process
);
1751 case LM_CB_DROPLOCKS
:
1752 gfs2_gl_hash_clear(sdp
, NO_WAIT
);
1753 gfs2_quota_scan(sdp
);
1757 gfs2_assert_warn(sdp
, 0);
1763 * gfs2_iopen_go_callback - Try to kick the inode/vnode associated with an
1764 * iopen glock from memory
1765 * @io_gl: the iopen glock
1766 * @state: the state into which the glock should be put
1770 void gfs2_iopen_go_callback(struct gfs2_glock
*io_gl
, unsigned int state
)
1773 if (state
!= LM_ST_UNLOCKED
)
1775 /* FIXME: remove this? */
1779 * demote_ok - Check to see if it's ok to unlock a glock
1782 * Returns: 1 if it's ok
1785 static int demote_ok(struct gfs2_glock
*gl
)
1787 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1788 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1791 if (test_bit(GLF_STICKY
, &gl
->gl_flags
))
1793 else if (test_bit(GLF_PREFETCH
, &gl
->gl_flags
))
1794 demote
= time_after_eq(jiffies
, gl
->gl_stamp
+
1795 gfs2_tune_get(sdp
, gt_prefetch_secs
) * HZ
);
1796 else if (glops
->go_demote_ok
)
1797 demote
= glops
->go_demote_ok(gl
);
1803 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1808 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock
*gl
)
1810 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1812 spin_lock(&sdp
->sd_reclaim_lock
);
1813 if (list_empty(&gl
->gl_reclaim
)) {
1814 gfs2_glock_hold(gl
);
1815 list_add(&gl
->gl_reclaim
, &sdp
->sd_reclaim_list
);
1816 atomic_inc(&sdp
->sd_reclaim_count
);
1818 spin_unlock(&sdp
->sd_reclaim_lock
);
1820 wake_up(&sdp
->sd_reclaim_wq
);
1824 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1825 * @sdp: the filesystem
1827 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1828 * different glock and we notice that there are a lot of glocks in the
1833 void gfs2_reclaim_glock(struct gfs2_sbd
*sdp
)
1835 struct gfs2_glock
*gl
;
1837 spin_lock(&sdp
->sd_reclaim_lock
);
1838 if (list_empty(&sdp
->sd_reclaim_list
)) {
1839 spin_unlock(&sdp
->sd_reclaim_lock
);
1842 gl
= list_entry(sdp
->sd_reclaim_list
.next
,
1843 struct gfs2_glock
, gl_reclaim
);
1844 list_del_init(&gl
->gl_reclaim
);
1845 spin_unlock(&sdp
->sd_reclaim_lock
);
1847 atomic_dec(&sdp
->sd_reclaim_count
);
1848 atomic_inc(&sdp
->sd_reclaimed
);
1850 if (gfs2_glmutex_trylock(gl
)) {
1851 if (queue_empty(gl
, &gl
->gl_holders
) &&
1852 gl
->gl_state
!= LM_ST_UNLOCKED
&& demote_ok(gl
))
1853 handle_callback(gl
, LM_ST_UNLOCKED
);
1854 gfs2_glmutex_unlock(gl
);
1861 * examine_bucket - Call a function for glock in a hash bucket
1862 * @examiner: the function
1863 * @sdp: the filesystem
1864 * @bucket: the bucket
1866 * Returns: 1 if the bucket has entries
1869 static int examine_bucket(glock_examiner examiner
, struct gfs2_sbd
*sdp
,
1870 struct gfs2_gl_hash_bucket
*bucket
)
1872 struct glock_plug plug
;
1873 struct list_head
*tmp
;
1874 struct gfs2_glock
*gl
;
1877 /* Add "plug" to end of bucket list, work back up list from there */
1878 memset(&plug
.gl_flags
, 0, sizeof(unsigned long));
1879 set_bit(GLF_PLUG
, &plug
.gl_flags
);
1881 write_lock(&bucket
->hb_lock
);
1882 list_add(&plug
.gl_list
, &bucket
->hb_list
);
1883 write_unlock(&bucket
->hb_lock
);
1886 write_lock(&bucket
->hb_lock
);
1889 tmp
= plug
.gl_list
.next
;
1891 if (tmp
== &bucket
->hb_list
) {
1892 list_del(&plug
.gl_list
);
1893 entries
= !list_empty(&bucket
->hb_list
);
1894 write_unlock(&bucket
->hb_lock
);
1897 gl
= list_entry(tmp
, struct gfs2_glock
, gl_list
);
1899 /* Move plug up list */
1900 list_move(&plug
.gl_list
, &gl
->gl_list
);
1902 if (test_bit(GLF_PLUG
, &gl
->gl_flags
))
1904 if (gl
->gl_sbd
!= sdp
)
1907 /* examiner() must glock_put() */
1908 gfs2_glock_hold(gl
);
1913 write_unlock(&bucket
->hb_lock
);
1920 * scan_glock - look at a glock and see if we can reclaim it
1921 * @gl: the glock to look at
1925 static void scan_glock(struct gfs2_glock
*gl
)
1927 if (gl
->gl_ops
== &gfs2_inode_glops
)
1930 if (gfs2_glmutex_trylock(gl
)) {
1931 if (queue_empty(gl
, &gl
->gl_holders
) &&
1932 gl
->gl_state
!= LM_ST_UNLOCKED
&&
1935 gfs2_glmutex_unlock(gl
);
1942 gfs2_glmutex_unlock(gl
);
1943 gfs2_glock_schedule_for_reclaim(gl
);
1948 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
1949 * @sdp: the filesystem
1953 void gfs2_scand_internal(struct gfs2_sbd
*sdp
)
1957 for (x
= 0; x
< GFS2_GL_HASH_SIZE
; x
++) {
1958 examine_bucket(scan_glock
, sdp
, &gl_hash_table
[x
]);
1964 * clear_glock - look at a glock and see if we can free it from glock cache
1965 * @gl: the glock to look at
1969 static void clear_glock(struct gfs2_glock
*gl
)
1971 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1974 spin_lock(&sdp
->sd_reclaim_lock
);
1975 if (!list_empty(&gl
->gl_reclaim
)) {
1976 list_del_init(&gl
->gl_reclaim
);
1977 atomic_dec(&sdp
->sd_reclaim_count
);
1978 spin_unlock(&sdp
->sd_reclaim_lock
);
1979 released
= gfs2_glock_put(gl
);
1980 gfs2_assert(sdp
, !released
);
1982 spin_unlock(&sdp
->sd_reclaim_lock
);
1985 if (gfs2_glmutex_trylock(gl
)) {
1986 if (queue_empty(gl
, &gl
->gl_holders
) &&
1987 gl
->gl_state
!= LM_ST_UNLOCKED
)
1988 handle_callback(gl
, LM_ST_UNLOCKED
);
1990 gfs2_glmutex_unlock(gl
);
1997 * gfs2_gl_hash_clear - Empty out the glock hash table
1998 * @sdp: the filesystem
1999 * @wait: wait until it's all gone
2001 * Called when unmounting the filesystem, or when inter-node lock manager
2002 * requests DROPLOCKS because it is running out of capacity.
2005 void gfs2_gl_hash_clear(struct gfs2_sbd
*sdp
, int wait
)
2016 for (x
= 0; x
< GFS2_GL_HASH_SIZE
; x
++)
2017 if (examine_bucket(clear_glock
, sdp
, &gl_hash_table
[x
]))
2023 if (time_after_eq(jiffies
,
2024 t
+ gfs2_tune_get(sdp
, gt_stall_secs
) * HZ
)) {
2025 fs_warn(sdp
, "Unmount seems to be stalled. "
2026 "Dumping lock state...\n");
2027 gfs2_dump_lockstate(sdp
);
2031 invalidate_inodes(sdp
->sd_vfs
);
2037 * Diagnostic routines to help debug distributed deadlock
2041 * dump_holder - print information about a glock holder
2042 * @str: a string naming the type of holder
2043 * @gh: the glock holder
2045 * Returns: 0 on success, -ENOBUFS when we run out of space
2048 static int dump_holder(char *str
, struct gfs2_holder
*gh
)
2051 int error
= -ENOBUFS
;
2053 printk(KERN_INFO
" %s\n", str
);
2054 printk(KERN_INFO
" owner = %ld\n",
2055 (gh
->gh_owner
) ? (long)gh
->gh_owner
->pid
: -1);
2056 printk(KERN_INFO
" gh_state = %u\n", gh
->gh_state
);
2057 printk(KERN_INFO
" gh_flags =");
2058 for (x
= 0; x
< 32; x
++)
2059 if (gh
->gh_flags
& (1 << x
))
2062 printk(KERN_INFO
" error = %d\n", gh
->gh_error
);
2063 printk(KERN_INFO
" gh_iflags =");
2064 for (x
= 0; x
< 32; x
++)
2065 if (test_bit(x
, &gh
->gh_iflags
))
2068 print_symbol(KERN_INFO
" initialized at: %s\n", gh
->gh_ip
);
2076 * dump_inode - print information about an inode
2079 * Returns: 0 on success, -ENOBUFS when we run out of space
2082 static int dump_inode(struct gfs2_inode
*ip
)
2085 int error
= -ENOBUFS
;
2087 printk(KERN_INFO
" Inode:\n");
2088 printk(KERN_INFO
" num = %llu %llu\n",
2089 (unsigned long long)ip
->i_num
.no_formal_ino
,
2090 (unsigned long long)ip
->i_num
.no_addr
);
2091 printk(KERN_INFO
" type = %u\n", IF2DT(ip
->i_di
.di_mode
));
2092 printk(KERN_INFO
" i_flags =");
2093 for (x
= 0; x
< 32; x
++)
2094 if (test_bit(x
, &ip
->i_flags
))
2104 * dump_glock - print information about a glock
2106 * @count: where we are in the buffer
2108 * Returns: 0 on success, -ENOBUFS when we run out of space
2111 static int dump_glock(struct gfs2_glock
*gl
)
2113 struct gfs2_holder
*gh
;
2115 int error
= -ENOBUFS
;
2117 spin_lock(&gl
->gl_spin
);
2119 printk(KERN_INFO
"Glock 0x%p (%u, %llu)\n", gl
, gl
->gl_name
.ln_type
,
2120 (unsigned long long)gl
->gl_name
.ln_number
);
2121 printk(KERN_INFO
" gl_flags =");
2122 for (x
= 0; x
< 32; x
++) {
2123 if (test_bit(x
, &gl
->gl_flags
))
2127 printk(KERN_INFO
" gl_ref = %d\n", atomic_read(&gl
->gl_ref
.refcount
));
2128 printk(KERN_INFO
" gl_state = %u\n", gl
->gl_state
);
2129 printk(KERN_INFO
" gl_owner = %s\n", gl
->gl_owner
->comm
);
2130 print_symbol(KERN_INFO
" gl_ip = %s\n", gl
->gl_ip
);
2131 printk(KERN_INFO
" req_gh = %s\n", (gl
->gl_req_gh
) ? "yes" : "no");
2132 printk(KERN_INFO
" req_bh = %s\n", (gl
->gl_req_bh
) ? "yes" : "no");
2133 printk(KERN_INFO
" lvb_count = %d\n", atomic_read(&gl
->gl_lvb_count
));
2134 printk(KERN_INFO
" object = %s\n", (gl
->gl_object
) ? "yes" : "no");
2135 printk(KERN_INFO
" le = %s\n",
2136 (list_empty(&gl
->gl_le
.le_list
)) ? "no" : "yes");
2137 printk(KERN_INFO
" reclaim = %s\n",
2138 (list_empty(&gl
->gl_reclaim
)) ? "no" : "yes");
2140 printk(KERN_INFO
" aspace = 0x%p nrpages = %lu\n", gl
->gl_aspace
,
2141 gl
->gl_aspace
->i_mapping
->nrpages
);
2143 printk(KERN_INFO
" aspace = no\n");
2144 printk(KERN_INFO
" ail = %d\n", atomic_read(&gl
->gl_ail_count
));
2145 if (gl
->gl_req_gh
) {
2146 error
= dump_holder("Request", gl
->gl_req_gh
);
2150 list_for_each_entry(gh
, &gl
->gl_holders
, gh_list
) {
2151 error
= dump_holder("Holder", gh
);
2155 list_for_each_entry(gh
, &gl
->gl_waiters1
, gh_list
) {
2156 error
= dump_holder("Waiter1", gh
);
2160 list_for_each_entry(gh
, &gl
->gl_waiters2
, gh_list
) {
2161 error
= dump_holder("Waiter2", gh
);
2165 list_for_each_entry(gh
, &gl
->gl_waiters3
, gh_list
) {
2166 error
= dump_holder("Waiter3", gh
);
2170 if (gl
->gl_ops
== &gfs2_inode_glops
&& gl
->gl_object
) {
2171 if (!test_bit(GLF_LOCK
, &gl
->gl_flags
) &&
2172 list_empty(&gl
->gl_holders
)) {
2173 error
= dump_inode(gl
->gl_object
);
2178 printk(KERN_INFO
" Inode: busy\n");
2185 spin_unlock(&gl
->gl_spin
);
2190 * gfs2_dump_lockstate - print out the current lockstate
2191 * @sdp: the filesystem
2192 * @ub: the buffer to copy the information into
2194 * If @ub is NULL, dump the lockstate to the console.
2198 static int gfs2_dump_lockstate(struct gfs2_sbd
*sdp
)
2200 struct gfs2_gl_hash_bucket
*bucket
;
2201 struct gfs2_glock
*gl
;
2205 for (x
= 0; x
< GFS2_GL_HASH_SIZE
; x
++) {
2206 bucket
= &gl_hash_table
[x
];
2208 read_lock(&bucket
->hb_lock
);
2210 list_for_each_entry(gl
, &bucket
->hb_list
, gl_list
) {
2211 if (test_bit(GLF_PLUG
, &gl
->gl_flags
))
2213 if (gl
->gl_sbd
!= sdp
)
2216 error
= dump_glock(gl
);
2221 read_unlock(&bucket
->hb_lock
);
2231 int __init
gfs2_glock_init(void)
2234 for(i
= 0; i
< GFS2_GL_HASH_SIZE
; i
++) {
2235 struct gfs2_gl_hash_bucket
*hb
= &gl_hash_table
[i
];
2236 rwlock_init(&hb
->hb_lock
);
2237 INIT_LIST_HEAD(&hb
->hb_list
);