2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 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 v.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/semaphore.h>
22 #include <asm/uaccess.h>
25 #include "lm_interface.h"
37 /* Must be kept in sync with the beginning of struct gfs2_glock */
39 struct list_head gl_list
;
40 unsigned long gl_flags
;
44 struct gfs2_holder gr_gh
;
45 struct work_struct gr_work
;
48 typedef void (*glock_examiner
) (struct gfs2_glock
* gl
);
51 * relaxed_state_ok - is a requested lock compatible with the current lock mode?
52 * @actual: the current state of the lock
53 * @requested: the lock state that was requested by the caller
54 * @flags: the modifier flags passed in by the caller
56 * Returns: 1 if the locks are compatible, 0 otherwise
59 static inline int relaxed_state_ok(unsigned int actual
, unsigned requested
,
62 if (actual
== requested
)
68 if (actual
== LM_ST_EXCLUSIVE
&& requested
== LM_ST_SHARED
)
71 if (actual
!= LM_ST_UNLOCKED
&& (flags
& LM_FLAG_ANY
))
78 * gl_hash() - Turn glock number into hash bucket number
79 * @lock: The glock number
81 * Returns: The number of the corresponding hash bucket
84 static unsigned int gl_hash(struct lm_lockname
*name
)
88 h
= jhash(&name
->ln_number
, sizeof(uint64_t), 0);
89 h
= jhash(&name
->ln_type
, sizeof(unsigned int), h
);
90 h
&= GFS2_GL_HASH_MASK
;
96 * glock_free() - Perform a few checks and then release struct gfs2_glock
97 * @gl: The glock to release
99 * Also calls lock module to release its internal structure for this glock.
103 static void glock_free(struct gfs2_glock
*gl
)
105 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
106 struct inode
*aspace
= gl
->gl_aspace
;
108 gfs2_lm_put_lock(sdp
, gl
->gl_lock
);
111 gfs2_aspace_put(aspace
);
113 kmem_cache_free(gfs2_glock_cachep
, gl
);
117 * gfs2_glock_hold() - increment reference count on glock
118 * @gl: The glock to hold
122 void gfs2_glock_hold(struct gfs2_glock
*gl
)
124 kref_get(&gl
->gl_ref
);
127 /* All work is done after the return from kref_put() so we
128 can release the write_lock before the free. */
130 static void kill_glock(struct kref
*kref
)
132 struct gfs2_glock
*gl
= container_of(kref
, struct gfs2_glock
, gl_ref
);
133 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
135 gfs2_assert(sdp
, gl
->gl_state
== LM_ST_UNLOCKED
);
136 gfs2_assert(sdp
, list_empty(&gl
->gl_reclaim
));
137 gfs2_assert(sdp
, list_empty(&gl
->gl_holders
));
138 gfs2_assert(sdp
, list_empty(&gl
->gl_waiters1
));
139 gfs2_assert(sdp
, list_empty(&gl
->gl_waiters2
));
140 gfs2_assert(sdp
, list_empty(&gl
->gl_waiters3
));
144 * gfs2_glock_put() - Decrement reference count on glock
145 * @gl: The glock to put
149 int gfs2_glock_put(struct gfs2_glock
*gl
)
151 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
152 struct gfs2_gl_hash_bucket
*bucket
= gl
->gl_bucket
;
155 mutex_lock(&sdp
->sd_invalidate_inodes_mutex
);
157 write_lock(&bucket
->hb_lock
);
158 if (kref_put(&gl
->gl_ref
, kill_glock
)) {
159 list_del_init(&gl
->gl_list
);
160 write_unlock(&bucket
->hb_lock
);
161 BUG_ON(spin_is_locked(&gl
->gl_spin
));
166 write_unlock(&bucket
->hb_lock
);
168 mutex_unlock(&sdp
->sd_invalidate_inodes_mutex
);
173 * queue_empty - check to see if a glock's queue is empty
175 * @head: the head of the queue to check
177 * This function protects the list in the event that a process already
178 * has a holder on the list and is adding a second holder for itself.
179 * The glmutex lock is what generally prevents processes from working
180 * on the same glock at once, but the special case of adding a second
181 * holder for yourself ("recursive" locking) doesn't involve locking
182 * glmutex, making the spin lock necessary.
184 * Returns: 1 if the queue is empty
187 static inline int queue_empty(struct gfs2_glock
*gl
, struct list_head
*head
)
190 spin_lock(&gl
->gl_spin
);
191 empty
= list_empty(head
);
192 spin_unlock(&gl
->gl_spin
);
197 * search_bucket() - Find struct gfs2_glock by lock number
198 * @bucket: the bucket to search
199 * @name: The lock name
201 * Returns: NULL, or the struct gfs2_glock with the requested number
204 static struct gfs2_glock
*search_bucket(struct gfs2_gl_hash_bucket
*bucket
,
205 struct lm_lockname
*name
)
207 struct gfs2_glock
*gl
;
209 list_for_each_entry(gl
, &bucket
->hb_list
, gl_list
) {
210 if (test_bit(GLF_PLUG
, &gl
->gl_flags
))
212 if (!lm_name_equal(&gl
->gl_name
, name
))
215 kref_get(&gl
->gl_ref
);
224 * gfs2_glock_find() - Find glock by lock number
225 * @sdp: The GFS2 superblock
226 * @name: The lock name
228 * Returns: NULL, or the struct gfs2_glock with the requested number
231 struct gfs2_glock
*gfs2_glock_find(struct gfs2_sbd
*sdp
,
232 struct lm_lockname
*name
)
234 struct gfs2_gl_hash_bucket
*bucket
= &sdp
->sd_gl_hash
[gl_hash(name
)];
235 struct gfs2_glock
*gl
;
237 read_lock(&bucket
->hb_lock
);
238 gl
= search_bucket(bucket
, name
);
239 read_unlock(&bucket
->hb_lock
);
245 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
246 * @sdp: The GFS2 superblock
247 * @number: the lock number
248 * @glops: The glock_operations to use
249 * @create: If 0, don't create the glock if it doesn't exist
250 * @glp: the glock is returned here
252 * This does not lock a glock, just finds/creates structures for one.
257 int gfs2_glock_get(struct gfs2_sbd
*sdp
, uint64_t number
,
258 struct gfs2_glock_operations
*glops
, int create
,
259 struct gfs2_glock
**glp
)
261 struct lm_lockname name
;
262 struct gfs2_glock
*gl
, *tmp
;
263 struct gfs2_gl_hash_bucket
*bucket
;
266 name
.ln_number
= number
;
267 name
.ln_type
= glops
->go_type
;
268 bucket
= &sdp
->sd_gl_hash
[gl_hash(&name
)];
270 read_lock(&bucket
->hb_lock
);
271 gl
= search_bucket(bucket
, &name
);
272 read_unlock(&bucket
->hb_lock
);
279 gl
= kmem_cache_alloc(gfs2_glock_cachep
, GFP_KERNEL
);
283 memset(gl
, 0, sizeof(struct gfs2_glock
));
285 INIT_LIST_HEAD(&gl
->gl_list
);
287 kref_init(&gl
->gl_ref
);
289 spin_lock_init(&gl
->gl_spin
);
291 gl
->gl_state
= LM_ST_UNLOCKED
;
292 INIT_LIST_HEAD(&gl
->gl_holders
);
293 INIT_LIST_HEAD(&gl
->gl_waiters1
);
294 INIT_LIST_HEAD(&gl
->gl_waiters2
);
295 INIT_LIST_HEAD(&gl
->gl_waiters3
);
299 gl
->gl_bucket
= bucket
;
300 INIT_LIST_HEAD(&gl
->gl_reclaim
);
304 lops_init_le(&gl
->gl_le
, &gfs2_glock_lops
);
305 INIT_LIST_HEAD(&gl
->gl_ail_list
);
307 /* If this glock protects actual on-disk data or metadata blocks,
308 create a VFS inode to manage the pages/buffers holding them. */
309 if (glops
== &gfs2_inode_glops
||
310 glops
== &gfs2_rgrp_glops
||
311 glops
== &gfs2_meta_glops
) {
312 gl
->gl_aspace
= gfs2_aspace_get(sdp
);
313 if (!gl
->gl_aspace
) {
319 error
= gfs2_lm_get_lock(sdp
, &name
, &gl
->gl_lock
);
323 write_lock(&bucket
->hb_lock
);
324 tmp
= search_bucket(bucket
, &name
);
326 write_unlock(&bucket
->hb_lock
);
330 list_add_tail(&gl
->gl_list
, &bucket
->hb_list
);
331 write_unlock(&bucket
->hb_lock
);
340 gfs2_aspace_put(gl
->gl_aspace
);
343 kmem_cache_free(gfs2_glock_cachep
, gl
);
349 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
351 * @state: the state we're requesting
352 * @flags: the modifier flags
353 * @gh: the holder structure
357 void gfs2_holder_init(struct gfs2_glock
*gl
, unsigned int state
, unsigned flags
,
358 struct gfs2_holder
*gh
)
360 flags
|= GL_NEVER_RECURSE
;
361 INIT_LIST_HEAD(&gh
->gh_list
);
363 gh
->gh_ip
= (unsigned long)__builtin_return_address(0);
364 gh
->gh_owner
= current
;
365 gh
->gh_state
= state
;
366 gh
->gh_flags
= flags
;
369 init_completion(&gh
->gh_wait
);
371 if (gh
->gh_state
== LM_ST_EXCLUSIVE
)
372 gh
->gh_flags
|= GL_LOCAL_EXCL
;
378 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
379 * @state: the state we're requesting
380 * @flags: the modifier flags
381 * @gh: the holder structure
383 * Don't mess with the glock.
387 void gfs2_holder_reinit(unsigned int state
, unsigned flags
, struct gfs2_holder
*gh
)
389 gh
->gh_state
= state
;
390 gh
->gh_flags
= flags
| GL_NEVER_RECURSE
;
391 if (gh
->gh_state
== LM_ST_EXCLUSIVE
)
392 gh
->gh_flags
|= GL_LOCAL_EXCL
;
394 gh
->gh_iflags
&= 1 << HIF_ALLOCED
;
395 gh
->gh_ip
= (unsigned long)__builtin_return_address(0);
399 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
400 * @gh: the holder structure
404 void gfs2_holder_uninit(struct gfs2_holder
*gh
)
406 gfs2_glock_put(gh
->gh_gl
);
412 * gfs2_holder_get - get a struct gfs2_holder structure
414 * @state: the state we're requesting
415 * @flags: the modifier flags
416 * @gfp_flags: __GFP_NOFAIL
418 * Figure out how big an impact this function has. Either:
419 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
420 * 2) Leave it like it is
422 * Returns: the holder structure, NULL on ENOMEM
425 struct gfs2_holder
*gfs2_holder_get(struct gfs2_glock
*gl
, unsigned int state
,
426 int flags
, gfp_t gfp_flags
)
428 struct gfs2_holder
*gh
;
430 gh
= kmalloc(sizeof(struct gfs2_holder
), gfp_flags
);
434 gfs2_holder_init(gl
, state
, flags
, gh
);
435 set_bit(HIF_ALLOCED
, &gh
->gh_iflags
);
436 gh
->gh_ip
= (unsigned long)__builtin_return_address(0);
441 * gfs2_holder_put - get rid of a struct gfs2_holder structure
442 * @gh: the holder structure
446 void gfs2_holder_put(struct gfs2_holder
*gh
)
448 gfs2_holder_uninit(gh
);
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 complete(&gh
->gh_wait
);
472 * rq_promote - process a promote request in the queue
473 * @gh: the glock holder
475 * Acquire a new inter-node lock, or change a lock state to more restrictive.
477 * Returns: 1 if the queue is blocked
480 static int rq_promote(struct gfs2_holder
*gh
)
482 struct gfs2_glock
*gl
= gh
->gh_gl
;
483 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
484 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
486 if (!relaxed_state_ok(gl
->gl_state
, gh
->gh_state
, gh
->gh_flags
)) {
487 if (list_empty(&gl
->gl_holders
)) {
489 set_bit(GLF_LOCK
, &gl
->gl_flags
);
490 spin_unlock(&gl
->gl_spin
);
492 if (atomic_read(&sdp
->sd_reclaim_count
) >
493 gfs2_tune_get(sdp
, gt_reclaim_limit
) &&
494 !(gh
->gh_flags
& LM_FLAG_PRIORITY
)) {
495 gfs2_reclaim_glock(sdp
);
496 gfs2_reclaim_glock(sdp
);
499 glops
->go_xmote_th(gl
, gh
->gh_state
,
502 spin_lock(&gl
->gl_spin
);
507 if (list_empty(&gl
->gl_holders
)) {
508 set_bit(HIF_FIRST
, &gh
->gh_iflags
);
509 set_bit(GLF_LOCK
, &gl
->gl_flags
);
511 struct gfs2_holder
*next_gh
;
512 if (gh
->gh_flags
& GL_LOCAL_EXCL
)
514 next_gh
= list_entry(gl
->gl_holders
.next
, struct gfs2_holder
,
516 if (next_gh
->gh_flags
& GL_LOCAL_EXCL
)
520 list_move_tail(&gh
->gh_list
, &gl
->gl_holders
);
522 set_bit(HIF_HOLDER
, &gh
->gh_iflags
);
524 complete(&gh
->gh_wait
);
530 * rq_demote - process a demote request in the queue
531 * @gh: the glock holder
533 * Returns: 1 if the queue is blocked
536 static int rq_demote(struct gfs2_holder
*gh
)
538 struct gfs2_glock
*gl
= gh
->gh_gl
;
539 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
541 if (!list_empty(&gl
->gl_holders
))
544 if (gl
->gl_state
== gh
->gh_state
|| gl
->gl_state
== LM_ST_UNLOCKED
) {
545 list_del_init(&gh
->gh_list
);
547 spin_unlock(&gl
->gl_spin
);
548 if (test_bit(HIF_DEALLOC
, &gh
->gh_iflags
))
551 complete(&gh
->gh_wait
);
552 spin_lock(&gl
->gl_spin
);
555 set_bit(GLF_LOCK
, &gl
->gl_flags
);
556 spin_unlock(&gl
->gl_spin
);
558 if (gh
->gh_state
== LM_ST_UNLOCKED
||
559 gl
->gl_state
!= LM_ST_EXCLUSIVE
)
560 glops
->go_drop_th(gl
);
562 glops
->go_xmote_th(gl
, gh
->gh_state
, gh
->gh_flags
);
564 spin_lock(&gl
->gl_spin
);
571 * rq_greedy - process a queued request to drop greedy status
572 * @gh: the glock holder
574 * Returns: 1 if the queue is blocked
577 static int rq_greedy(struct gfs2_holder
*gh
)
579 struct gfs2_glock
*gl
= gh
->gh_gl
;
581 list_del_init(&gh
->gh_list
);
582 /* gh->gh_error never examined. */
583 clear_bit(GLF_GREEDY
, &gl
->gl_flags
);
584 spin_unlock(&gl
->gl_spin
);
586 gfs2_holder_uninit(gh
);
587 kfree(container_of(gh
, struct greedy
, gr_gh
));
589 spin_lock(&gl
->gl_spin
);
595 * run_queue - process holder structures on a glock
599 static void run_queue(struct gfs2_glock
*gl
)
601 struct gfs2_holder
*gh
;
605 if (test_bit(GLF_LOCK
, &gl
->gl_flags
))
608 if (!list_empty(&gl
->gl_waiters1
)) {
609 gh
= list_entry(gl
->gl_waiters1
.next
,
610 struct gfs2_holder
, gh_list
);
612 if (test_bit(HIF_MUTEX
, &gh
->gh_iflags
))
613 blocked
= rq_mutex(gh
);
615 gfs2_assert_warn(gl
->gl_sbd
, 0);
617 } else if (!list_empty(&gl
->gl_waiters2
) &&
618 !test_bit(GLF_SKIP_WAITERS2
, &gl
->gl_flags
)) {
619 gh
= list_entry(gl
->gl_waiters2
.next
,
620 struct gfs2_holder
, gh_list
);
622 if (test_bit(HIF_DEMOTE
, &gh
->gh_iflags
))
623 blocked
= rq_demote(gh
);
624 else if (test_bit(HIF_GREEDY
, &gh
->gh_iflags
))
625 blocked
= rq_greedy(gh
);
627 gfs2_assert_warn(gl
->gl_sbd
, 0);
629 } else if (!list_empty(&gl
->gl_waiters3
)) {
630 gh
= list_entry(gl
->gl_waiters3
.next
,
631 struct gfs2_holder
, gh_list
);
633 if (test_bit(HIF_PROMOTE
, &gh
->gh_iflags
))
634 blocked
= rq_promote(gh
);
636 gfs2_assert_warn(gl
->gl_sbd
, 0);
647 * gfs2_glmutex_lock - acquire a local lock on a glock
650 * Gives caller exclusive access to manipulate a glock structure.
653 void gfs2_glmutex_lock(struct gfs2_glock
*gl
)
655 struct gfs2_holder gh
;
657 gfs2_holder_init(gl
, 0, 0, &gh
);
658 set_bit(HIF_MUTEX
, &gh
.gh_iflags
);
660 spin_lock(&gl
->gl_spin
);
661 if (test_and_set_bit(GLF_LOCK
, &gl
->gl_flags
))
662 list_add_tail(&gh
.gh_list
, &gl
->gl_waiters1
);
664 complete(&gh
.gh_wait
);
665 spin_unlock(&gl
->gl_spin
);
667 wait_for_completion(&gh
.gh_wait
);
668 gfs2_holder_uninit(&gh
);
672 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
675 * Returns: 1 if the glock is acquired
678 int gfs2_glmutex_trylock(struct gfs2_glock
*gl
)
682 spin_lock(&gl
->gl_spin
);
683 if (test_and_set_bit(GLF_LOCK
, &gl
->gl_flags
))
685 spin_unlock(&gl
->gl_spin
);
691 * gfs2_glmutex_unlock - release a local lock on a glock
696 void gfs2_glmutex_unlock(struct gfs2_glock
*gl
)
698 spin_lock(&gl
->gl_spin
);
699 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
701 BUG_ON(!spin_is_locked(&gl
->gl_spin
));
702 spin_unlock(&gl
->gl_spin
);
706 * handle_callback - add a demote request to a lock's queue
708 * @state: the state the caller wants us to change to
712 static void handle_callback(struct gfs2_glock
*gl
, unsigned int state
)
714 struct gfs2_holder
*gh
, *new_gh
= NULL
;
717 spin_lock(&gl
->gl_spin
);
719 list_for_each_entry(gh
, &gl
->gl_waiters2
, gh_list
) {
720 if (test_bit(HIF_DEMOTE
, &gh
->gh_iflags
) &&
721 gl
->gl_req_gh
!= gh
) {
722 if (gh
->gh_state
!= state
)
723 gh
->gh_state
= LM_ST_UNLOCKED
;
729 list_add_tail(&new_gh
->gh_list
, &gl
->gl_waiters2
);
732 spin_unlock(&gl
->gl_spin
);
734 new_gh
= gfs2_holder_get(gl
, state
,
735 LM_FLAG_TRY
| GL_NEVER_RECURSE
,
736 GFP_KERNEL
| __GFP_NOFAIL
),
737 set_bit(HIF_DEMOTE
, &new_gh
->gh_iflags
);
738 set_bit(HIF_DEALLOC
, &new_gh
->gh_iflags
);
744 spin_unlock(&gl
->gl_spin
);
747 gfs2_holder_put(new_gh
);
751 * state_change - record that the glock is now in a different state
753 * @new_state the new state
757 static void state_change(struct gfs2_glock
*gl
, unsigned int new_state
)
761 held1
= (gl
->gl_state
!= LM_ST_UNLOCKED
);
762 held2
= (new_state
!= LM_ST_UNLOCKED
);
764 if (held1
!= held2
) {
771 gl
->gl_state
= new_state
;
775 * xmote_bh - Called after the lock module is done acquiring a lock
776 * @gl: The glock in question
777 * @ret: the int returned from the lock module
781 static void xmote_bh(struct gfs2_glock
*gl
, unsigned int ret
)
783 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
784 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
785 struct gfs2_holder
*gh
= gl
->gl_req_gh
;
786 int prev_state
= gl
->gl_state
;
789 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
790 gfs2_assert_warn(sdp
, queue_empty(gl
, &gl
->gl_holders
));
791 gfs2_assert_warn(sdp
, !(ret
& LM_OUT_ASYNC
));
793 state_change(gl
, ret
& LM_OUT_ST_MASK
);
795 if (prev_state
!= LM_ST_UNLOCKED
&& !(ret
& LM_OUT_CACHEABLE
)) {
797 glops
->go_inval(gl
, DIO_METADATA
| DIO_DATA
);
798 } else if (gl
->gl_state
== LM_ST_DEFERRED
) {
799 /* We might not want to do this here.
800 Look at moving to the inode glops. */
802 glops
->go_inval(gl
, DIO_DATA
);
805 /* Deal with each possible exit condition */
808 gl
->gl_stamp
= jiffies
;
810 else if (unlikely(test_bit(SDF_SHUTDOWN
, &sdp
->sd_flags
))) {
811 spin_lock(&gl
->gl_spin
);
812 list_del_init(&gh
->gh_list
);
814 spin_unlock(&gl
->gl_spin
);
816 } else if (test_bit(HIF_DEMOTE
, &gh
->gh_iflags
)) {
817 spin_lock(&gl
->gl_spin
);
818 list_del_init(&gh
->gh_list
);
819 if (gl
->gl_state
== gh
->gh_state
||
820 gl
->gl_state
== LM_ST_UNLOCKED
)
823 if (gfs2_assert_warn(sdp
, gh
->gh_flags
&
824 (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
)) == -1)
825 fs_warn(sdp
, "ret = 0x%.8X\n", ret
);
826 gh
->gh_error
= GLR_TRYFAILED
;
828 spin_unlock(&gl
->gl_spin
);
830 if (ret
& LM_OUT_CANCELED
)
831 handle_callback(gl
, LM_ST_UNLOCKED
); /* Lame */
833 } else if (ret
& LM_OUT_CANCELED
) {
834 spin_lock(&gl
->gl_spin
);
835 list_del_init(&gh
->gh_list
);
836 gh
->gh_error
= GLR_CANCELED
;
837 spin_unlock(&gl
->gl_spin
);
839 } else if (relaxed_state_ok(gl
->gl_state
, gh
->gh_state
, gh
->gh_flags
)) {
840 spin_lock(&gl
->gl_spin
);
841 list_move_tail(&gh
->gh_list
, &gl
->gl_holders
);
843 set_bit(HIF_HOLDER
, &gh
->gh_iflags
);
844 spin_unlock(&gl
->gl_spin
);
846 set_bit(HIF_FIRST
, &gh
->gh_iflags
);
850 } else if (gh
->gh_flags
& (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
)) {
851 spin_lock(&gl
->gl_spin
);
852 list_del_init(&gh
->gh_list
);
853 gh
->gh_error
= GLR_TRYFAILED
;
854 spin_unlock(&gl
->gl_spin
);
857 if (gfs2_assert_withdraw(sdp
, 0) == -1)
858 fs_err(sdp
, "ret = 0x%.8X\n", ret
);
861 if (glops
->go_xmote_bh
)
862 glops
->go_xmote_bh(gl
);
865 spin_lock(&gl
->gl_spin
);
866 gl
->gl_req_gh
= NULL
;
867 gl
->gl_req_bh
= NULL
;
868 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
870 spin_unlock(&gl
->gl_spin
);
876 if (test_bit(HIF_DEALLOC
, &gh
->gh_iflags
))
879 complete(&gh
->gh_wait
);
884 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
885 * @gl: The glock in question
886 * @state: the requested state
887 * @flags: modifier flags to the lock call
891 void gfs2_glock_xmote_th(struct gfs2_glock
*gl
, unsigned int state
, int flags
)
893 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
894 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
895 int lck_flags
= flags
& (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
|
896 LM_FLAG_NOEXP
| LM_FLAG_ANY
|
898 unsigned int lck_ret
;
900 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
901 gfs2_assert_warn(sdp
, queue_empty(gl
, &gl
->gl_holders
));
902 gfs2_assert_warn(sdp
, state
!= LM_ST_UNLOCKED
);
903 gfs2_assert_warn(sdp
, state
!= gl
->gl_state
);
905 if (gl
->gl_state
== LM_ST_EXCLUSIVE
) {
908 DIO_METADATA
| DIO_DATA
| DIO_RELEASE
);
912 gl
->gl_req_bh
= xmote_bh
;
914 lck_ret
= gfs2_lm_lock(sdp
, gl
->gl_lock
, gl
->gl_state
, state
,
917 if (gfs2_assert_withdraw(sdp
, !(lck_ret
& LM_OUT_ERROR
)))
920 if (lck_ret
& LM_OUT_ASYNC
)
921 gfs2_assert_warn(sdp
, lck_ret
== LM_OUT_ASYNC
);
923 xmote_bh(gl
, lck_ret
);
927 * drop_bh - Called after a lock module unlock completes
929 * @ret: the return status
931 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
932 * Doesn't drop the reference on the glock the top half took out
936 static void drop_bh(struct gfs2_glock
*gl
, unsigned int ret
)
938 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
939 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
940 struct gfs2_holder
*gh
= gl
->gl_req_gh
;
942 clear_bit(GLF_PREFETCH
, &gl
->gl_flags
);
944 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
945 gfs2_assert_warn(sdp
, queue_empty(gl
, &gl
->gl_holders
));
946 gfs2_assert_warn(sdp
, !ret
);
948 state_change(gl
, LM_ST_UNLOCKED
);
951 glops
->go_inval(gl
, DIO_METADATA
| DIO_DATA
);
954 spin_lock(&gl
->gl_spin
);
955 list_del_init(&gh
->gh_list
);
957 spin_unlock(&gl
->gl_spin
);
960 if (glops
->go_drop_bh
)
961 glops
->go_drop_bh(gl
);
963 spin_lock(&gl
->gl_spin
);
964 gl
->gl_req_gh
= NULL
;
965 gl
->gl_req_bh
= NULL
;
966 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
968 spin_unlock(&gl
->gl_spin
);
973 if (test_bit(HIF_DEALLOC
, &gh
->gh_iflags
))
976 complete(&gh
->gh_wait
);
981 * gfs2_glock_drop_th - call into the lock module to unlock a lock
986 void gfs2_glock_drop_th(struct gfs2_glock
*gl
)
988 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
989 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
992 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
993 gfs2_assert_warn(sdp
, queue_empty(gl
, &gl
->gl_holders
));
994 gfs2_assert_warn(sdp
, gl
->gl_state
!= LM_ST_UNLOCKED
);
996 if (gl
->gl_state
== LM_ST_EXCLUSIVE
) {
999 DIO_METADATA
| DIO_DATA
| DIO_RELEASE
);
1002 gfs2_glock_hold(gl
);
1003 gl
->gl_req_bh
= drop_bh
;
1005 ret
= gfs2_lm_unlock(sdp
, gl
->gl_lock
, gl
->gl_state
);
1007 if (gfs2_assert_withdraw(sdp
, !(ret
& LM_OUT_ERROR
)))
1013 gfs2_assert_warn(sdp
, ret
== LM_OUT_ASYNC
);
1017 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1018 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1020 * Don't cancel GL_NOCANCEL requests.
1023 static void do_cancels(struct gfs2_holder
*gh
)
1025 struct gfs2_glock
*gl
= gh
->gh_gl
;
1027 spin_lock(&gl
->gl_spin
);
1029 while (gl
->gl_req_gh
!= gh
&&
1030 !test_bit(HIF_HOLDER
, &gh
->gh_iflags
) &&
1031 !list_empty(&gh
->gh_list
)) {
1032 if (gl
->gl_req_bh
&&
1034 (gl
->gl_req_gh
->gh_flags
& GL_NOCANCEL
))) {
1035 spin_unlock(&gl
->gl_spin
);
1036 gfs2_lm_cancel(gl
->gl_sbd
, gl
->gl_lock
);
1038 spin_lock(&gl
->gl_spin
);
1040 spin_unlock(&gl
->gl_spin
);
1042 spin_lock(&gl
->gl_spin
);
1046 spin_unlock(&gl
->gl_spin
);
1050 * glock_wait_internal - wait on a glock acquisition
1051 * @gh: the glock holder
1053 * Returns: 0 on success
1056 static int glock_wait_internal(struct gfs2_holder
*gh
)
1058 struct gfs2_glock
*gl
= gh
->gh_gl
;
1059 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1060 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1062 if (test_bit(HIF_ABORTED
, &gh
->gh_iflags
))
1065 if (gh
->gh_flags
& (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
)) {
1066 spin_lock(&gl
->gl_spin
);
1067 if (gl
->gl_req_gh
!= gh
&&
1068 !test_bit(HIF_HOLDER
, &gh
->gh_iflags
) &&
1069 !list_empty(&gh
->gh_list
)) {
1070 list_del_init(&gh
->gh_list
);
1071 gh
->gh_error
= GLR_TRYFAILED
;
1073 spin_unlock(&gl
->gl_spin
);
1074 return gh
->gh_error
;
1076 spin_unlock(&gl
->gl_spin
);
1079 if (gh
->gh_flags
& LM_FLAG_PRIORITY
)
1082 wait_for_completion(&gh
->gh_wait
);
1085 return gh
->gh_error
;
1087 gfs2_assert_withdraw(sdp
, test_bit(HIF_HOLDER
, &gh
->gh_iflags
));
1088 gfs2_assert_withdraw(sdp
, relaxed_state_ok(gl
->gl_state
,
1092 if (test_bit(HIF_FIRST
, &gh
->gh_iflags
)) {
1093 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
1095 if (glops
->go_lock
) {
1096 gh
->gh_error
= glops
->go_lock(gh
);
1098 spin_lock(&gl
->gl_spin
);
1099 list_del_init(&gh
->gh_list
);
1100 spin_unlock(&gl
->gl_spin
);
1104 spin_lock(&gl
->gl_spin
);
1105 gl
->gl_req_gh
= NULL
;
1106 gl
->gl_req_bh
= NULL
;
1107 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
1109 spin_unlock(&gl
->gl_spin
);
1112 return gh
->gh_error
;
1115 static inline struct gfs2_holder
*
1116 find_holder_by_owner(struct list_head
*head
, struct task_struct
*owner
)
1118 struct gfs2_holder
*gh
;
1120 list_for_each_entry(gh
, head
, gh_list
) {
1121 if (gh
->gh_owner
== owner
)
1129 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1130 * @gh: the holder structure to add
1134 static void add_to_queue(struct gfs2_holder
*gh
)
1136 struct gfs2_glock
*gl
= gh
->gh_gl
;
1137 struct gfs2_holder
*existing
;
1139 BUG_ON(!gh
->gh_owner
);
1141 existing
= find_holder_by_owner(&gl
->gl_holders
, gh
->gh_owner
);
1143 print_symbol(KERN_WARNING
"original: %s\n", existing
->gh_ip
);
1144 print_symbol(KERN_WARNING
"new: %s\n", gh
->gh_ip
);
1148 existing
= find_holder_by_owner(&gl
->gl_waiters3
, gh
->gh_owner
);
1150 print_symbol(KERN_WARNING
"original: %s\n", existing
->gh_ip
);
1151 print_symbol(KERN_WARNING
"new: %s\n", gh
->gh_ip
);
1155 if (gh
->gh_flags
& LM_FLAG_PRIORITY
)
1156 list_add(&gh
->gh_list
, &gl
->gl_waiters3
);
1158 list_add_tail(&gh
->gh_list
, &gl
->gl_waiters3
);
1162 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1163 * @gh: the holder structure
1165 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1167 * Returns: 0, GLR_TRYFAILED, or errno on failure
1170 int gfs2_glock_nq(struct gfs2_holder
*gh
)
1172 struct gfs2_glock
*gl
= gh
->gh_gl
;
1173 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1177 if (unlikely(test_bit(SDF_SHUTDOWN
, &sdp
->sd_flags
))) {
1178 set_bit(HIF_ABORTED
, &gh
->gh_iflags
);
1182 set_bit(HIF_PROMOTE
, &gh
->gh_iflags
);
1184 spin_lock(&gl
->gl_spin
);
1187 spin_unlock(&gl
->gl_spin
);
1189 if (!(gh
->gh_flags
& GL_ASYNC
)) {
1190 error
= glock_wait_internal(gh
);
1191 if (error
== GLR_CANCELED
) {
1197 clear_bit(GLF_PREFETCH
, &gl
->gl_flags
);
1203 * gfs2_glock_poll - poll to see if an async request has been completed
1206 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1209 int gfs2_glock_poll(struct gfs2_holder
*gh
)
1211 struct gfs2_glock
*gl
= gh
->gh_gl
;
1214 spin_lock(&gl
->gl_spin
);
1216 if (test_bit(HIF_HOLDER
, &gh
->gh_iflags
))
1218 else if (list_empty(&gh
->gh_list
)) {
1219 if (gh
->gh_error
== GLR_CANCELED
) {
1220 spin_unlock(&gl
->gl_spin
);
1222 if (gfs2_glock_nq(gh
))
1229 spin_unlock(&gl
->gl_spin
);
1235 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1236 * @gh: the holder structure
1238 * Returns: 0, GLR_TRYFAILED, or errno on failure
1241 int gfs2_glock_wait(struct gfs2_holder
*gh
)
1245 error
= glock_wait_internal(gh
);
1246 if (error
== GLR_CANCELED
) {
1248 gh
->gh_flags
&= ~GL_ASYNC
;
1249 error
= gfs2_glock_nq(gh
);
1256 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1257 * @gh: the glock holder
1261 void gfs2_glock_dq(struct gfs2_holder
*gh
)
1263 struct gfs2_glock
*gl
= gh
->gh_gl
;
1264 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1266 if (gh
->gh_flags
& GL_SYNC
)
1267 set_bit(GLF_SYNC
, &gl
->gl_flags
);
1269 if (gh
->gh_flags
& GL_NOCACHE
)
1270 handle_callback(gl
, LM_ST_UNLOCKED
);
1272 gfs2_glmutex_lock(gl
);
1274 spin_lock(&gl
->gl_spin
);
1275 list_del_init(&gh
->gh_list
);
1277 if (list_empty(&gl
->gl_holders
)) {
1278 spin_unlock(&gl
->gl_spin
);
1280 if (glops
->go_unlock
)
1281 glops
->go_unlock(gh
);
1283 if (test_bit(GLF_SYNC
, &gl
->gl_flags
)) {
1285 glops
->go_sync(gl
, DIO_METADATA
| DIO_DATA
);
1288 gl
->gl_stamp
= jiffies
;
1290 spin_lock(&gl
->gl_spin
);
1293 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
1295 spin_unlock(&gl
->gl_spin
);
1299 * gfs2_glock_prefetch - Try to prefetch a glock
1301 * @state: the state to prefetch in
1302 * @flags: flags passed to go_xmote_th()
1306 void gfs2_glock_prefetch(struct gfs2_glock
*gl
, unsigned int state
, int flags
)
1308 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1310 spin_lock(&gl
->gl_spin
);
1312 if (test_bit(GLF_LOCK
, &gl
->gl_flags
) ||
1313 !list_empty(&gl
->gl_holders
) ||
1314 !list_empty(&gl
->gl_waiters1
) ||
1315 !list_empty(&gl
->gl_waiters2
) ||
1316 !list_empty(&gl
->gl_waiters3
) ||
1317 relaxed_state_ok(gl
->gl_state
, state
, flags
)) {
1318 spin_unlock(&gl
->gl_spin
);
1322 set_bit(GLF_PREFETCH
, &gl
->gl_flags
);
1323 set_bit(GLF_LOCK
, &gl
->gl_flags
);
1324 spin_unlock(&gl
->gl_spin
);
1326 glops
->go_xmote_th(gl
, state
, flags
);
1330 * gfs2_glock_force_drop - Force a glock to be uncached
1335 void gfs2_glock_force_drop(struct gfs2_glock
*gl
)
1337 struct gfs2_holder gh
;
1339 gfs2_holder_init(gl
, LM_ST_UNLOCKED
, GL_NEVER_RECURSE
, &gh
);
1340 set_bit(HIF_DEMOTE
, &gh
.gh_iflags
);
1342 spin_lock(&gl
->gl_spin
);
1343 list_add_tail(&gh
.gh_list
, &gl
->gl_waiters2
);
1345 spin_unlock(&gl
->gl_spin
);
1347 wait_for_completion(&gh
.gh_wait
);
1348 gfs2_holder_uninit(&gh
);
1351 static void greedy_work(void *data
)
1353 struct greedy
*gr
= (struct greedy
*)data
;
1354 struct gfs2_holder
*gh
= &gr
->gr_gh
;
1355 struct gfs2_glock
*gl
= gh
->gh_gl
;
1356 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1358 clear_bit(GLF_SKIP_WAITERS2
, &gl
->gl_flags
);
1360 if (glops
->go_greedy
)
1361 glops
->go_greedy(gl
);
1363 spin_lock(&gl
->gl_spin
);
1365 if (list_empty(&gl
->gl_waiters2
)) {
1366 clear_bit(GLF_GREEDY
, &gl
->gl_flags
);
1367 spin_unlock(&gl
->gl_spin
);
1368 gfs2_holder_uninit(gh
);
1371 gfs2_glock_hold(gl
);
1372 list_add_tail(&gh
->gh_list
, &gl
->gl_waiters2
);
1374 spin_unlock(&gl
->gl_spin
);
1380 * gfs2_glock_be_greedy -
1384 * Returns: 0 if go_greedy will be called, 1 otherwise
1387 int gfs2_glock_be_greedy(struct gfs2_glock
*gl
, unsigned int time
)
1390 struct gfs2_holder
*gh
;
1393 gl
->gl_sbd
->sd_args
.ar_localcaching
||
1394 test_and_set_bit(GLF_GREEDY
, &gl
->gl_flags
))
1397 gr
= kmalloc(sizeof(struct greedy
), GFP_KERNEL
);
1399 clear_bit(GLF_GREEDY
, &gl
->gl_flags
);
1404 gfs2_holder_init(gl
, 0, GL_NEVER_RECURSE
, gh
);
1405 set_bit(HIF_GREEDY
, &gh
->gh_iflags
);
1406 INIT_WORK(&gr
->gr_work
, greedy_work
, gr
);
1408 set_bit(GLF_SKIP_WAITERS2
, &gl
->gl_flags
);
1409 schedule_delayed_work(&gr
->gr_work
, time
);
1415 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1416 * @gh: the holder structure
1420 void gfs2_glock_dq_uninit(struct gfs2_holder
*gh
)
1423 gfs2_holder_uninit(gh
);
1427 * gfs2_glock_nq_num - acquire a glock based on lock number
1428 * @sdp: the filesystem
1429 * @number: the lock number
1430 * @glops: the glock operations for the type of glock
1431 * @state: the state to acquire the glock in
1432 * @flags: modifier flags for the aquisition
1433 * @gh: the struct gfs2_holder
1438 int gfs2_glock_nq_num(struct gfs2_sbd
*sdp
, uint64_t number
,
1439 struct gfs2_glock_operations
*glops
, unsigned int state
,
1440 int flags
, struct gfs2_holder
*gh
)
1442 struct gfs2_glock
*gl
;
1445 error
= gfs2_glock_get(sdp
, number
, glops
, CREATE
, &gl
);
1447 error
= gfs2_glock_nq_init(gl
, state
, flags
, gh
);
1455 * glock_compare - Compare two struct gfs2_glock structures for sorting
1456 * @arg_a: the first structure
1457 * @arg_b: the second structure
1461 static int glock_compare(const void *arg_a
, const void *arg_b
)
1463 struct gfs2_holder
*gh_a
= *(struct gfs2_holder
**)arg_a
;
1464 struct gfs2_holder
*gh_b
= *(struct gfs2_holder
**)arg_b
;
1465 struct lm_lockname
*a
= &gh_a
->gh_gl
->gl_name
;
1466 struct lm_lockname
*b
= &gh_b
->gh_gl
->gl_name
;
1469 if (a
->ln_number
> b
->ln_number
)
1471 else if (a
->ln_number
< b
->ln_number
)
1474 if (gh_a
->gh_state
== LM_ST_SHARED
&&
1475 gh_b
->gh_state
== LM_ST_EXCLUSIVE
)
1477 else if (!(gh_a
->gh_flags
& GL_LOCAL_EXCL
) &&
1478 (gh_b
->gh_flags
& GL_LOCAL_EXCL
))
1486 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1487 * @num_gh: the number of structures
1488 * @ghs: an array of struct gfs2_holder structures
1490 * Returns: 0 on success (all glocks acquired),
1491 * errno on failure (no glocks acquired)
1494 static int nq_m_sync(unsigned int num_gh
, struct gfs2_holder
*ghs
,
1495 struct gfs2_holder
**p
)
1500 for (x
= 0; x
< num_gh
; x
++)
1503 sort(p
, num_gh
, sizeof(struct gfs2_holder
*), glock_compare
, NULL
);
1505 for (x
= 0; x
< num_gh
; x
++) {
1506 p
[x
]->gh_flags
&= ~(LM_FLAG_TRY
| GL_ASYNC
);
1508 error
= gfs2_glock_nq(p
[x
]);
1511 gfs2_glock_dq(p
[x
]);
1520 * gfs2_glock_nq_m - acquire multiple glocks
1521 * @num_gh: the number of structures
1522 * @ghs: an array of struct gfs2_holder structures
1524 * Figure out how big an impact this function has. Either:
1525 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1526 * 2) Forget async stuff and just call nq_m_sync()
1527 * 3) Leave it like it is
1529 * Returns: 0 on success (all glocks acquired),
1530 * errno on failure (no glocks acquired)
1533 int gfs2_glock_nq_m(unsigned int num_gh
, struct gfs2_holder
*ghs
)
1537 int borked
= 0, serious
= 0;
1544 ghs
->gh_flags
&= ~(LM_FLAG_TRY
| GL_ASYNC
);
1545 return gfs2_glock_nq(ghs
);
1548 e
= kcalloc(num_gh
, sizeof(struct gfs2_holder
*), GFP_KERNEL
);
1552 for (x
= 0; x
< num_gh
; x
++) {
1553 ghs
[x
].gh_flags
|= LM_FLAG_TRY
| GL_ASYNC
;
1554 error
= gfs2_glock_nq(&ghs
[x
]);
1563 for (x
= 0; x
< num_gh
; x
++) {
1564 error
= e
[x
] = glock_wait_internal(&ghs
[x
]);
1567 if (error
!= GLR_TRYFAILED
&& error
!= GLR_CANCELED
)
1577 for (x
= 0; x
< num_gh
; x
++)
1579 gfs2_glock_dq(&ghs
[x
]);
1584 for (x
= 0; x
< num_gh
; x
++)
1585 gfs2_holder_reinit(ghs
[x
].gh_state
, ghs
[x
].gh_flags
,
1587 error
= nq_m_sync(num_gh
, ghs
, (struct gfs2_holder
**)e
);
1596 * gfs2_glock_dq_m - release multiple glocks
1597 * @num_gh: the number of structures
1598 * @ghs: an array of struct gfs2_holder structures
1602 void gfs2_glock_dq_m(unsigned int num_gh
, struct gfs2_holder
*ghs
)
1606 for (x
= 0; x
< num_gh
; x
++)
1607 gfs2_glock_dq(&ghs
[x
]);
1611 * gfs2_glock_dq_uninit_m - release multiple glocks
1612 * @num_gh: the number of structures
1613 * @ghs: an array of struct gfs2_holder structures
1617 void gfs2_glock_dq_uninit_m(unsigned int num_gh
, struct gfs2_holder
*ghs
)
1621 for (x
= 0; x
< num_gh
; x
++)
1622 gfs2_glock_dq_uninit(&ghs
[x
]);
1626 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1627 * @sdp: the filesystem
1628 * @number: the lock number
1629 * @glops: the glock operations for the type of glock
1630 * @state: the state to acquire the glock in
1631 * @flags: modifier flags for the aquisition
1636 void gfs2_glock_prefetch_num(struct gfs2_sbd
*sdp
, uint64_t number
,
1637 struct gfs2_glock_operations
*glops
,
1638 unsigned int state
, int flags
)
1640 struct gfs2_glock
*gl
;
1643 if (atomic_read(&sdp
->sd_reclaim_count
) <
1644 gfs2_tune_get(sdp
, gt_reclaim_limit
)) {
1645 error
= gfs2_glock_get(sdp
, number
, glops
, CREATE
, &gl
);
1647 gfs2_glock_prefetch(gl
, state
, flags
);
1654 * gfs2_lvb_hold - attach a LVB from a glock
1655 * @gl: The glock in question
1659 int gfs2_lvb_hold(struct gfs2_glock
*gl
)
1663 gfs2_glmutex_lock(gl
);
1665 if (!atomic_read(&gl
->gl_lvb_count
)) {
1666 error
= gfs2_lm_hold_lvb(gl
->gl_sbd
, gl
->gl_lock
, &gl
->gl_lvb
);
1668 gfs2_glmutex_unlock(gl
);
1671 gfs2_glock_hold(gl
);
1673 atomic_inc(&gl
->gl_lvb_count
);
1675 gfs2_glmutex_unlock(gl
);
1681 * gfs2_lvb_unhold - detach a LVB from a glock
1682 * @gl: The glock in question
1686 void gfs2_lvb_unhold(struct gfs2_glock
*gl
)
1688 gfs2_glock_hold(gl
);
1689 gfs2_glmutex_lock(gl
);
1691 gfs2_assert(gl
->gl_sbd
, atomic_read(&gl
->gl_lvb_count
) > 0);
1692 if (atomic_dec_and_test(&gl
->gl_lvb_count
)) {
1693 gfs2_lm_unhold_lvb(gl
->gl_sbd
, gl
->gl_lock
, gl
->gl_lvb
);
1698 gfs2_glmutex_unlock(gl
);
1702 void gfs2_lvb_sync(struct gfs2_glock
*gl
)
1704 gfs2_glmutex_lock(gl
);
1706 gfs2_assert(gl
->gl_sbd
, atomic_read(&gl
->gl_lvb_count
));
1707 if (!gfs2_assert_warn(gl
->gl_sbd
, gfs2_glock_is_held_excl(gl
)))
1708 gfs2_lm_sync_lvb(gl
->gl_sbd
, gl
->gl_lock
, gl
->gl_lvb
);
1710 gfs2_glmutex_unlock(gl
);
1713 static void blocking_cb(struct gfs2_sbd
*sdp
, struct lm_lockname
*name
,
1716 struct gfs2_glock
*gl
;
1718 gl
= gfs2_glock_find(sdp
, name
);
1722 if (gl
->gl_ops
->go_callback
)
1723 gl
->gl_ops
->go_callback(gl
, state
);
1724 handle_callback(gl
, state
);
1726 spin_lock(&gl
->gl_spin
);
1728 spin_unlock(&gl
->gl_spin
);
1734 * gfs2_glock_cb - Callback used by locking module
1735 * @fsdata: Pointer to the superblock
1736 * @type: Type of callback
1737 * @data: Type dependent data pointer
1739 * Called by the locking module when it wants to tell us something.
1740 * Either we need to drop a lock, one of our ASYNC requests completed, or
1741 * a journal from another client needs to be recovered.
1744 void gfs2_glock_cb(lm_fsdata_t
*fsdata
, unsigned int type
, void *data
)
1746 struct gfs2_sbd
*sdp
= (struct gfs2_sbd
*)fsdata
;
1750 blocking_cb(sdp
, (struct lm_lockname
*)data
, LM_ST_UNLOCKED
);
1754 blocking_cb(sdp
, (struct lm_lockname
*)data
, LM_ST_DEFERRED
);
1758 blocking_cb(sdp
, (struct lm_lockname
*)data
, LM_ST_SHARED
);
1762 struct lm_async_cb
*async
= (struct lm_async_cb
*)data
;
1763 struct gfs2_glock
*gl
;
1765 gl
= gfs2_glock_find(sdp
, &async
->lc_name
);
1766 if (gfs2_assert_warn(sdp
, gl
))
1768 if (!gfs2_assert_warn(sdp
, gl
->gl_req_bh
))
1769 gl
->gl_req_bh(gl
, async
->lc_ret
);
1775 case LM_CB_NEED_RECOVERY
:
1776 gfs2_jdesc_make_dirty(sdp
, *(unsigned int *)data
);
1777 if (sdp
->sd_recoverd_process
)
1778 wake_up_process(sdp
->sd_recoverd_process
);
1781 case LM_CB_DROPLOCKS
:
1782 gfs2_gl_hash_clear(sdp
, NO_WAIT
);
1783 gfs2_quota_scan(sdp
);
1787 gfs2_assert_warn(sdp
, 0);
1793 * gfs2_try_toss_inode - try to remove a particular inode struct from cache
1794 * sdp: the filesystem
1795 * inum: the inode number
1799 void gfs2_try_toss_inode(struct gfs2_sbd
*sdp
, struct gfs2_inum
*inum
)
1801 struct gfs2_glock
*gl
;
1802 struct gfs2_inode
*ip
;
1805 error
= gfs2_glock_get(sdp
, inum
->no_addr
, &gfs2_inode_glops
,
1810 if (!gfs2_glmutex_trylock(gl
))
1817 if (atomic_read(&ip
->i_count
))
1820 gfs2_inode_destroy(ip
);
1823 gfs2_glmutex_unlock(gl
);
1830 * gfs2_iopen_go_callback - Try to kick the inode/vnode associated with an
1831 * iopen glock from memory
1832 * @io_gl: the iopen glock
1833 * @state: the state into which the glock should be put
1837 void gfs2_iopen_go_callback(struct gfs2_glock
*io_gl
, unsigned int state
)
1839 struct gfs2_glock
*i_gl
;
1841 if (state
!= LM_ST_UNLOCKED
)
1844 spin_lock(&io_gl
->gl_spin
);
1845 i_gl
= io_gl
->gl_object
;
1847 gfs2_glock_hold(i_gl
);
1848 spin_unlock(&io_gl
->gl_spin
);
1850 spin_unlock(&io_gl
->gl_spin
);
1854 if (gfs2_glmutex_trylock(i_gl
)) {
1855 struct gfs2_inode
*ip
= i_gl
->gl_object
;
1857 gfs2_try_toss_vnode(ip
);
1858 gfs2_glmutex_unlock(i_gl
);
1859 gfs2_glock_schedule_for_reclaim(i_gl
);
1862 gfs2_glmutex_unlock(i_gl
);
1866 gfs2_glock_put(i_gl
);
1870 * demote_ok - Check to see if it's ok to unlock a glock
1873 * Returns: 1 if it's ok
1876 static int demote_ok(struct gfs2_glock
*gl
)
1878 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1879 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1882 if (test_bit(GLF_STICKY
, &gl
->gl_flags
))
1884 else if (test_bit(GLF_PREFETCH
, &gl
->gl_flags
))
1885 demote
= time_after_eq(jiffies
,
1887 gfs2_tune_get(sdp
, gt_prefetch_secs
) * HZ
);
1888 else if (glops
->go_demote_ok
)
1889 demote
= glops
->go_demote_ok(gl
);
1895 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1900 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock
*gl
)
1902 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1904 spin_lock(&sdp
->sd_reclaim_lock
);
1905 if (list_empty(&gl
->gl_reclaim
)) {
1906 gfs2_glock_hold(gl
);
1907 list_add(&gl
->gl_reclaim
, &sdp
->sd_reclaim_list
);
1908 atomic_inc(&sdp
->sd_reclaim_count
);
1910 spin_unlock(&sdp
->sd_reclaim_lock
);
1912 wake_up(&sdp
->sd_reclaim_wq
);
1916 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1917 * @sdp: the filesystem
1919 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1920 * different glock and we notice that there are a lot of glocks in the
1925 void gfs2_reclaim_glock(struct gfs2_sbd
*sdp
)
1927 struct gfs2_glock
*gl
;
1929 spin_lock(&sdp
->sd_reclaim_lock
);
1930 if (list_empty(&sdp
->sd_reclaim_list
)) {
1931 spin_unlock(&sdp
->sd_reclaim_lock
);
1934 gl
= list_entry(sdp
->sd_reclaim_list
.next
,
1935 struct gfs2_glock
, gl_reclaim
);
1936 list_del_init(&gl
->gl_reclaim
);
1937 spin_unlock(&sdp
->sd_reclaim_lock
);
1939 atomic_dec(&sdp
->sd_reclaim_count
);
1940 atomic_inc(&sdp
->sd_reclaimed
);
1942 if (gfs2_glmutex_trylock(gl
)) {
1943 if (gl
->gl_ops
== &gfs2_inode_glops
) {
1944 struct gfs2_inode
*ip
= gl
->gl_object
;
1945 if (ip
&& !atomic_read(&ip
->i_count
))
1946 gfs2_inode_destroy(ip
);
1948 if (queue_empty(gl
, &gl
->gl_holders
) &&
1949 gl
->gl_state
!= LM_ST_UNLOCKED
&&
1951 handle_callback(gl
, LM_ST_UNLOCKED
);
1952 gfs2_glmutex_unlock(gl
);
1959 * examine_bucket - Call a function for glock in a hash bucket
1960 * @examiner: the function
1961 * @sdp: the filesystem
1962 * @bucket: the bucket
1964 * Returns: 1 if the bucket has entries
1967 static int examine_bucket(glock_examiner examiner
, struct gfs2_sbd
*sdp
,
1968 struct gfs2_gl_hash_bucket
*bucket
)
1970 struct glock_plug plug
;
1971 struct list_head
*tmp
;
1972 struct gfs2_glock
*gl
;
1975 /* Add "plug" to end of bucket list, work back up list from there */
1976 memset(&plug
.gl_flags
, 0, sizeof(unsigned long));
1977 set_bit(GLF_PLUG
, &plug
.gl_flags
);
1979 write_lock(&bucket
->hb_lock
);
1980 list_add(&plug
.gl_list
, &bucket
->hb_list
);
1981 write_unlock(&bucket
->hb_lock
);
1984 write_lock(&bucket
->hb_lock
);
1987 tmp
= plug
.gl_list
.next
;
1989 if (tmp
== &bucket
->hb_list
) {
1990 list_del(&plug
.gl_list
);
1991 entries
= !list_empty(&bucket
->hb_list
);
1992 write_unlock(&bucket
->hb_lock
);
1995 gl
= list_entry(tmp
, struct gfs2_glock
, gl_list
);
1997 /* Move plug up list */
1998 list_move(&plug
.gl_list
, &gl
->gl_list
);
2000 if (test_bit(GLF_PLUG
, &gl
->gl_flags
))
2003 /* examiner() must glock_put() */
2004 gfs2_glock_hold(gl
);
2009 write_unlock(&bucket
->hb_lock
);
2016 * scan_glock - look at a glock and see if we can reclaim it
2017 * @gl: the glock to look at
2021 static void scan_glock(struct gfs2_glock
*gl
)
2023 if (gfs2_glmutex_trylock(gl
)) {
2024 if (gl
->gl_ops
== &gfs2_inode_glops
) {
2025 struct gfs2_inode
*ip
= gl
->gl_object
;
2026 if (ip
&& !atomic_read(&ip
->i_count
))
2029 if (queue_empty(gl
, &gl
->gl_holders
) &&
2030 gl
->gl_state
!= LM_ST_UNLOCKED
&&
2034 gfs2_glmutex_unlock(gl
);
2042 gfs2_glmutex_unlock(gl
);
2043 gfs2_glock_schedule_for_reclaim(gl
);
2048 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
2049 * @sdp: the filesystem
2053 void gfs2_scand_internal(struct gfs2_sbd
*sdp
)
2057 for (x
= 0; x
< GFS2_GL_HASH_SIZE
; x
++) {
2058 examine_bucket(scan_glock
, sdp
, &sdp
->sd_gl_hash
[x
]);
2064 * clear_glock - look at a glock and see if we can free it from glock cache
2065 * @gl: the glock to look at
2069 static void clear_glock(struct gfs2_glock
*gl
)
2071 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
2074 spin_lock(&sdp
->sd_reclaim_lock
);
2075 if (!list_empty(&gl
->gl_reclaim
)) {
2076 list_del_init(&gl
->gl_reclaim
);
2077 atomic_dec(&sdp
->sd_reclaim_count
);
2078 spin_unlock(&sdp
->sd_reclaim_lock
);
2079 released
= gfs2_glock_put(gl
);
2080 gfs2_assert(sdp
, !released
);
2082 spin_unlock(&sdp
->sd_reclaim_lock
);
2085 if (gfs2_glmutex_trylock(gl
)) {
2086 if (gl
->gl_ops
== &gfs2_inode_glops
) {
2087 struct gfs2_inode
*ip
= gl
->gl_object
;
2088 if (ip
&& !atomic_read(&ip
->i_count
))
2089 gfs2_inode_destroy(ip
);
2091 if (queue_empty(gl
, &gl
->gl_holders
) &&
2092 gl
->gl_state
!= LM_ST_UNLOCKED
)
2093 handle_callback(gl
, LM_ST_UNLOCKED
);
2095 gfs2_glmutex_unlock(gl
);
2102 * gfs2_gl_hash_clear - Empty out the glock hash table
2103 * @sdp: the filesystem
2104 * @wait: wait until it's all gone
2106 * Called when unmounting the filesystem, or when inter-node lock manager
2107 * requests DROPLOCKS because it is running out of capacity.
2110 void gfs2_gl_hash_clear(struct gfs2_sbd
*sdp
, int wait
)
2121 for (x
= 0; x
< GFS2_GL_HASH_SIZE
; x
++)
2122 if (examine_bucket(clear_glock
, sdp
,
2123 &sdp
->sd_gl_hash
[x
]))
2129 if (time_after_eq(jiffies
,
2130 t
+ gfs2_tune_get(sdp
, gt_stall_secs
) * HZ
)) {
2131 fs_warn(sdp
, "Unmount seems to be stalled. "
2132 "Dumping lock state...\n");
2133 gfs2_dump_lockstate(sdp
);
2137 /* invalidate_inodes() requires that the sb inodes list
2138 not change, but an async completion callback for an
2139 unlock can occur which does glock_put() which
2140 can call iput() which will change the sb inodes list.
2141 invalidate_inodes_mutex prevents glock_put()'s during
2142 an invalidate_inodes() */
2144 mutex_lock(&sdp
->sd_invalidate_inodes_mutex
);
2145 invalidate_inodes(sdp
->sd_vfs
);
2146 mutex_unlock(&sdp
->sd_invalidate_inodes_mutex
);
2152 * Diagnostic routines to help debug distributed deadlock
2156 * dump_holder - print information about a glock holder
2157 * @str: a string naming the type of holder
2158 * @gh: the glock holder
2160 * Returns: 0 on success, -ENOBUFS when we run out of space
2163 static int dump_holder(char *str
, struct gfs2_holder
*gh
)
2166 int error
= -ENOBUFS
;
2168 printk(KERN_INFO
" %s\n", str
);
2169 printk(KERN_INFO
" owner = %ld\n",
2170 (gh
->gh_owner
) ? (long)gh
->gh_owner
->pid
: -1);
2171 printk(KERN_INFO
" gh_state = %u\n", gh
->gh_state
);
2172 printk(KERN_INFO
" gh_flags =");
2173 for (x
= 0; x
< 32; x
++)
2174 if (gh
->gh_flags
& (1 << x
))
2177 printk(KERN_INFO
" error = %d\n", gh
->gh_error
);
2178 printk(KERN_INFO
" gh_iflags =");
2179 for (x
= 0; x
< 32; x
++)
2180 if (test_bit(x
, &gh
->gh_iflags
))
2183 print_symbol(KERN_INFO
" initialized at: %s\n", gh
->gh_ip
);
2191 * dump_inode - print information about an inode
2194 * Returns: 0 on success, -ENOBUFS when we run out of space
2197 static int dump_inode(struct gfs2_inode
*ip
)
2200 int error
= -ENOBUFS
;
2202 printk(KERN_INFO
" Inode:\n");
2203 printk(KERN_INFO
" num = %llu %llu\n",
2204 ip
->i_num
.no_formal_ino
, ip
->i_num
.no_addr
);
2205 printk(KERN_INFO
" type = %u\n", IF2DT(ip
->i_di
.di_mode
));
2206 printk(KERN_INFO
" i_count = %d\n", atomic_read(&ip
->i_count
));
2207 printk(KERN_INFO
" i_flags =");
2208 for (x
= 0; x
< 32; x
++)
2209 if (test_bit(x
, &ip
->i_flags
))
2212 printk(KERN_INFO
" vnode = %s\n", (ip
->i_vnode
) ? "yes" : "no");
2220 * dump_glock - print information about a glock
2222 * @count: where we are in the buffer
2224 * Returns: 0 on success, -ENOBUFS when we run out of space
2227 static int dump_glock(struct gfs2_glock
*gl
)
2229 struct gfs2_holder
*gh
;
2231 int error
= -ENOBUFS
;
2233 spin_lock(&gl
->gl_spin
);
2235 printk(KERN_INFO
"Glock (%u, %llu)\n",
2236 gl
->gl_name
.ln_type
,
2237 gl
->gl_name
.ln_number
);
2238 printk(KERN_INFO
" gl_flags =");
2239 for (x
= 0; x
< 32; x
++)
2240 if (test_bit(x
, &gl
->gl_flags
))
2243 printk(KERN_INFO
" gl_ref = %d\n", atomic_read(&gl
->gl_ref
.refcount
));
2244 printk(KERN_INFO
" gl_state = %u\n", gl
->gl_state
);
2245 printk(KERN_INFO
" req_gh = %s\n", (gl
->gl_req_gh
) ? "yes" : "no");
2246 printk(KERN_INFO
" req_bh = %s\n", (gl
->gl_req_bh
) ? "yes" : "no");
2247 printk(KERN_INFO
" lvb_count = %d\n", atomic_read(&gl
->gl_lvb_count
));
2248 printk(KERN_INFO
" object = %s\n", (gl
->gl_object
) ? "yes" : "no");
2249 printk(KERN_INFO
" le = %s\n",
2250 (list_empty(&gl
->gl_le
.le_list
)) ? "no" : "yes");
2251 printk(KERN_INFO
" reclaim = %s\n",
2252 (list_empty(&gl
->gl_reclaim
)) ? "no" : "yes");
2254 printk(KERN_INFO
" aspace = %lu\n",
2255 gl
->gl_aspace
->i_mapping
->nrpages
);
2257 printk(KERN_INFO
" aspace = no\n");
2258 printk(KERN_INFO
" ail = %d\n", atomic_read(&gl
->gl_ail_count
));
2259 if (gl
->gl_req_gh
) {
2260 error
= dump_holder("Request", gl
->gl_req_gh
);
2264 list_for_each_entry(gh
, &gl
->gl_holders
, gh_list
) {
2265 error
= dump_holder("Holder", gh
);
2269 list_for_each_entry(gh
, &gl
->gl_waiters1
, gh_list
) {
2270 error
= dump_holder("Waiter1", gh
);
2274 list_for_each_entry(gh
, &gl
->gl_waiters2
, gh_list
) {
2275 error
= dump_holder("Waiter2", gh
);
2279 list_for_each_entry(gh
, &gl
->gl_waiters3
, gh_list
) {
2280 error
= dump_holder("Waiter3", gh
);
2284 if (gl
->gl_ops
== &gfs2_inode_glops
&& gl
->gl_object
) {
2285 if (!test_bit(GLF_LOCK
, &gl
->gl_flags
) &&
2286 list_empty(&gl
->gl_holders
)) {
2287 error
= dump_inode(gl
->gl_object
);
2292 printk(KERN_INFO
" Inode: busy\n");
2299 spin_unlock(&gl
->gl_spin
);
2305 * gfs2_dump_lockstate - print out the current lockstate
2306 * @sdp: the filesystem
2307 * @ub: the buffer to copy the information into
2309 * If @ub is NULL, dump the lockstate to the console.
2313 int gfs2_dump_lockstate(struct gfs2_sbd
*sdp
)
2315 struct gfs2_gl_hash_bucket
*bucket
;
2316 struct gfs2_glock
*gl
;
2320 for (x
= 0; x
< GFS2_GL_HASH_SIZE
; x
++) {
2321 bucket
= &sdp
->sd_gl_hash
[x
];
2323 read_lock(&bucket
->hb_lock
);
2325 list_for_each_entry(gl
, &bucket
->hb_list
, gl_list
) {
2326 if (test_bit(GLF_PLUG
, &gl
->gl_flags
))
2329 error
= dump_glock(gl
);
2334 read_unlock(&bucket
->hb_lock
);