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 <asm/semaphore.h>
20 #include <asm/uaccess.h>
32 /* Must be kept in sync with the beginning of struct gfs2_glock */
34 struct list_head gl_list
;
35 unsigned long gl_flags
;
39 struct gfs2_holder gr_gh
;
40 struct work_struct gr_work
;
43 typedef void (*glock_examiner
) (struct gfs2_glock
* gl
);
46 * relaxed_state_ok - is a requested lock compatible with the current lock mode?
47 * @actual: the current state of the lock
48 * @requested: the lock state that was requested by the caller
49 * @flags: the modifier flags passed in by the caller
51 * Returns: 1 if the locks are compatible, 0 otherwise
54 static inline int relaxed_state_ok(unsigned int actual
, unsigned requested
,
57 if (actual
== requested
)
63 if (actual
== LM_ST_EXCLUSIVE
&& requested
== LM_ST_SHARED
)
66 if (actual
!= LM_ST_UNLOCKED
&& (flags
& LM_FLAG_ANY
))
73 * gl_hash() - Turn glock number into hash bucket number
74 * @lock: The glock number
76 * Returns: The number of the corresponding hash bucket
79 static unsigned int gl_hash(struct lm_lockname
*name
)
83 h
= jhash(&name
->ln_number
, sizeof(uint64_t), 0);
84 h
= jhash(&name
->ln_type
, sizeof(unsigned int), h
);
85 h
&= GFS2_GL_HASH_MASK
;
91 * glock_free() - Perform a few checks and then release struct gfs2_glock
92 * @gl: The glock to release
94 * Also calls lock module to release its internal structure for this glock.
98 static void glock_free(struct gfs2_glock
*gl
)
100 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
101 struct inode
*aspace
= gl
->gl_aspace
;
103 gfs2_lm_put_lock(sdp
, gl
->gl_lock
);
106 gfs2_aspace_put(aspace
);
108 kmem_cache_free(gfs2_glock_cachep
, gl
);
110 atomic_dec(&sdp
->sd_glock_count
);
114 * gfs2_glock_hold() - increment reference count on glock
115 * @gl: The glock to hold
119 void gfs2_glock_hold(struct gfs2_glock
*gl
)
121 kref_get(&gl
->gl_ref
);
124 /* All work is done after the return from kref_put() so we
125 can release the write_lock before the free. */
127 static void kill_glock(struct kref
*kref
)
129 struct gfs2_glock
*gl
= container_of(kref
, struct gfs2_glock
, gl_ref
);
130 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
132 gfs2_assert(sdp
, gl
->gl_state
== LM_ST_UNLOCKED
);
133 gfs2_assert(sdp
, list_empty(&gl
->gl_reclaim
));
134 gfs2_assert(sdp
, list_empty(&gl
->gl_holders
));
135 gfs2_assert(sdp
, list_empty(&gl
->gl_waiters1
));
136 gfs2_assert(sdp
, list_empty(&gl
->gl_waiters2
));
137 gfs2_assert(sdp
, list_empty(&gl
->gl_waiters3
));
141 * gfs2_glock_put() - Decrement reference count on glock
142 * @gl: The glock to put
146 int gfs2_glock_put(struct gfs2_glock
*gl
)
148 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
149 struct gfs2_gl_hash_bucket
*bucket
= gl
->gl_bucket
;
152 down(&sdp
->sd_invalidate_inodes_mutex
);
154 write_lock(&bucket
->hb_lock
);
155 if (kref_put(&gl
->gl_ref
, kill_glock
)) {
156 list_del_init(&gl
->gl_list
);
157 write_unlock(&bucket
->hb_lock
);
162 write_unlock(&bucket
->hb_lock
);
164 up(&sdp
->sd_invalidate_inodes_mutex
);
169 * queue_empty - check to see if a glock's queue is empty
171 * @head: the head of the queue to check
173 * This function protects the list in the event that a process already
174 * has a holder on the list and is adding a second holder for itself.
175 * The glmutex lock is what generally prevents processes from working
176 * on the same glock at once, but the special case of adding a second
177 * holder for yourself ("recursive" locking) doesn't involve locking
178 * glmutex, making the spin lock necessary.
180 * Returns: 1 if the queue is empty
183 static inline int queue_empty(struct gfs2_glock
*gl
, struct list_head
*head
)
186 spin_lock(&gl
->gl_spin
);
187 empty
= list_empty(head
);
188 spin_unlock(&gl
->gl_spin
);
193 * search_bucket() - Find struct gfs2_glock by lock number
194 * @bucket: the bucket to search
195 * @name: The lock name
197 * Returns: NULL, or the struct gfs2_glock with the requested number
200 static struct gfs2_glock
*search_bucket(struct gfs2_gl_hash_bucket
*bucket
,
201 struct lm_lockname
*name
)
203 struct gfs2_glock
*gl
;
205 list_for_each_entry(gl
, &bucket
->hb_list
, gl_list
) {
206 if (test_bit(GLF_PLUG
, &gl
->gl_flags
))
208 if (!lm_name_equal(&gl
->gl_name
, name
))
211 kref_get(&gl
->gl_ref
);
220 * gfs2_glock_find() - Find glock by lock number
221 * @sdp: The GFS2 superblock
222 * @name: The lock name
224 * Returns: NULL, or the struct gfs2_glock with the requested number
227 struct gfs2_glock
*gfs2_glock_find(struct gfs2_sbd
*sdp
,
228 struct lm_lockname
*name
)
230 struct gfs2_gl_hash_bucket
*bucket
= &sdp
->sd_gl_hash
[gl_hash(name
)];
231 struct gfs2_glock
*gl
;
233 read_lock(&bucket
->hb_lock
);
234 gl
= search_bucket(bucket
, name
);
235 read_unlock(&bucket
->hb_lock
);
241 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
242 * @sdp: The GFS2 superblock
243 * @number: the lock number
244 * @glops: The glock_operations to use
245 * @create: If 0, don't create the glock if it doesn't exist
246 * @glp: the glock is returned here
248 * This does not lock a glock, just finds/creates structures for one.
253 int gfs2_glock_get(struct gfs2_sbd
*sdp
, uint64_t number
,
254 struct gfs2_glock_operations
*glops
, int create
,
255 struct gfs2_glock
**glp
)
257 struct lm_lockname name
;
258 struct gfs2_glock
*gl
, *tmp
;
259 struct gfs2_gl_hash_bucket
*bucket
;
262 name
.ln_number
= number
;
263 name
.ln_type
= glops
->go_type
;
264 bucket
= &sdp
->sd_gl_hash
[gl_hash(&name
)];
266 read_lock(&bucket
->hb_lock
);
267 gl
= search_bucket(bucket
, &name
);
268 read_unlock(&bucket
->hb_lock
);
275 gl
= kmem_cache_alloc(gfs2_glock_cachep
, GFP_KERNEL
);
279 memset(gl
, 0, sizeof(struct gfs2_glock
));
281 INIT_LIST_HEAD(&gl
->gl_list
);
283 kref_init(&gl
->gl_ref
);
285 spin_lock_init(&gl
->gl_spin
);
287 gl
->gl_state
= LM_ST_UNLOCKED
;
288 INIT_LIST_HEAD(&gl
->gl_holders
);
289 INIT_LIST_HEAD(&gl
->gl_waiters1
);
290 INIT_LIST_HEAD(&gl
->gl_waiters2
);
291 INIT_LIST_HEAD(&gl
->gl_waiters3
);
295 gl
->gl_bucket
= bucket
;
296 INIT_LIST_HEAD(&gl
->gl_reclaim
);
300 lops_init_le(&gl
->gl_le
, &gfs2_glock_lops
);
301 INIT_LIST_HEAD(&gl
->gl_ail_list
);
303 /* If this glock protects actual on-disk data or metadata blocks,
304 create a VFS inode to manage the pages/buffers holding them. */
305 if (glops
== &gfs2_inode_glops
||
306 glops
== &gfs2_rgrp_glops
||
307 glops
== &gfs2_meta_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 atomic_inc(&sdp
->sd_glock_count
);
321 write_lock(&bucket
->hb_lock
);
322 tmp
= search_bucket(bucket
, &name
);
324 write_unlock(&bucket
->hb_lock
);
328 list_add_tail(&gl
->gl_list
, &bucket
->hb_list
);
329 write_unlock(&bucket
->hb_lock
);
338 gfs2_aspace_put(gl
->gl_aspace
);
341 kmem_cache_free(gfs2_glock_cachep
, gl
);
347 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
349 * @state: the state we're requesting
350 * @flags: the modifier flags
351 * @gh: the holder structure
355 void gfs2_holder_init(struct gfs2_glock
*gl
, unsigned int state
, int flags
,
356 struct gfs2_holder
*gh
)
358 INIT_LIST_HEAD(&gh
->gh_list
);
360 gh
->gh_owner
= (flags
& GL_NEVER_RECURSE
) ? NULL
: current
;
361 gh
->gh_state
= state
;
362 gh
->gh_flags
= flags
;
365 init_completion(&gh
->gh_wait
);
367 if (gh
->gh_state
== LM_ST_EXCLUSIVE
)
368 gh
->gh_flags
|= GL_LOCAL_EXCL
;
374 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
375 * @state: the state we're requesting
376 * @flags: the modifier flags
377 * @gh: the holder structure
379 * Don't mess with the glock.
383 void gfs2_holder_reinit(unsigned int state
, int flags
, struct gfs2_holder
*gh
)
385 gh
->gh_state
= state
;
386 gh
->gh_flags
= flags
;
387 if (gh
->gh_state
== LM_ST_EXCLUSIVE
)
388 gh
->gh_flags
|= GL_LOCAL_EXCL
;
390 gh
->gh_iflags
&= 1 << HIF_ALLOCED
;
394 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
395 * @gh: the holder structure
399 void gfs2_holder_uninit(struct gfs2_holder
*gh
)
401 gfs2_glock_put(gh
->gh_gl
);
406 * gfs2_holder_get - get a struct gfs2_holder structure
408 * @state: the state we're requesting
409 * @flags: the modifier flags
410 * @gfp_flags: __GFP_NOFAIL
412 * Figure out how big an impact this function has. Either:
413 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
414 * 2) Leave it like it is
416 * Returns: the holder structure, NULL on ENOMEM
419 struct gfs2_holder
*gfs2_holder_get(struct gfs2_glock
*gl
, unsigned int state
,
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
);
435 * gfs2_holder_put - get rid of a struct gfs2_holder structure
436 * @gh: the holder structure
440 void gfs2_holder_put(struct gfs2_holder
*gh
)
442 gfs2_holder_uninit(gh
);
447 * handle_recurse - put other holder structures (marked recursive)
448 * into the holders list
449 * @gh: the holder structure
453 static void handle_recurse(struct gfs2_holder
*gh
)
455 struct gfs2_glock
*gl
= gh
->gh_gl
;
456 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
457 struct gfs2_holder
*tmp_gh
, *safe
;
460 if (gfs2_assert_warn(sdp
, gh
->gh_owner
))
463 list_for_each_entry_safe(tmp_gh
, safe
, &gl
->gl_waiters3
, gh_list
) {
464 if (tmp_gh
->gh_owner
!= gh
->gh_owner
)
467 gfs2_assert_warn(sdp
,
468 test_bit(HIF_RECURSE
, &tmp_gh
->gh_iflags
));
470 list_move_tail(&tmp_gh
->gh_list
, &gl
->gl_holders
);
471 tmp_gh
->gh_error
= 0;
472 set_bit(HIF_HOLDER
, &tmp_gh
->gh_iflags
);
474 complete(&tmp_gh
->gh_wait
);
479 gfs2_assert_warn(sdp
, found
);
483 * do_unrecurse - a recursive holder was just dropped of the waiters3 list
486 * If there is only one other recursive holder, clear its HIF_RECURSE bit.
487 * If there is more than one, leave them alone.
491 static void do_unrecurse(struct gfs2_holder
*gh
)
493 struct gfs2_glock
*gl
= gh
->gh_gl
;
494 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
495 struct gfs2_holder
*tmp_gh
, *last_gh
= NULL
;
498 if (gfs2_assert_warn(sdp
, gh
->gh_owner
))
501 list_for_each_entry(tmp_gh
, &gl
->gl_waiters3
, gh_list
) {
502 if (tmp_gh
->gh_owner
!= gh
->gh_owner
)
505 gfs2_assert_warn(sdp
,
506 test_bit(HIF_RECURSE
, &tmp_gh
->gh_iflags
));
515 if (!gfs2_assert_warn(sdp
, found
))
516 clear_bit(HIF_RECURSE
, &last_gh
->gh_iflags
);
520 * rq_mutex - process a mutex request in the queue
521 * @gh: the glock holder
523 * Returns: 1 if the queue is blocked
526 static int rq_mutex(struct gfs2_holder
*gh
)
528 struct gfs2_glock
*gl
= gh
->gh_gl
;
530 list_del_init(&gh
->gh_list
);
531 /* gh->gh_error never examined. */
532 set_bit(GLF_LOCK
, &gl
->gl_flags
);
533 complete(&gh
->gh_wait
);
539 * rq_promote - process a promote request in the queue
540 * @gh: the glock holder
542 * Acquire a new inter-node lock, or change a lock state to more restrictive.
544 * Returns: 1 if the queue is blocked
547 static int rq_promote(struct gfs2_holder
*gh
)
549 struct gfs2_glock
*gl
= gh
->gh_gl
;
550 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
551 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
554 if (!relaxed_state_ok(gl
->gl_state
, gh
->gh_state
, gh
->gh_flags
)) {
555 if (list_empty(&gl
->gl_holders
)) {
557 set_bit(GLF_LOCK
, &gl
->gl_flags
);
558 spin_unlock(&gl
->gl_spin
);
560 if (atomic_read(&sdp
->sd_reclaim_count
) >
561 gfs2_tune_get(sdp
, gt_reclaim_limit
) &&
562 !(gh
->gh_flags
& LM_FLAG_PRIORITY
)) {
563 gfs2_reclaim_glock(sdp
);
564 gfs2_reclaim_glock(sdp
);
567 glops
->go_xmote_th(gl
, gh
->gh_state
,
570 spin_lock(&gl
->gl_spin
);
575 if (list_empty(&gl
->gl_holders
)) {
576 set_bit(HIF_FIRST
, &gh
->gh_iflags
);
577 set_bit(GLF_LOCK
, &gl
->gl_flags
);
580 struct gfs2_holder
*next_gh
;
581 if (gh
->gh_flags
& GL_LOCAL_EXCL
)
583 next_gh
= list_entry(gl
->gl_holders
.next
, struct gfs2_holder
,
585 if (next_gh
->gh_flags
& GL_LOCAL_EXCL
)
587 recurse
= test_bit(HIF_RECURSE
, &gh
->gh_iflags
);
590 list_move_tail(&gh
->gh_list
, &gl
->gl_holders
);
592 set_bit(HIF_HOLDER
, &gh
->gh_iflags
);
597 complete(&gh
->gh_wait
);
603 * rq_demote - process a demote request in the queue
604 * @gh: the glock holder
606 * Returns: 1 if the queue is blocked
609 static int rq_demote(struct gfs2_holder
*gh
)
611 struct gfs2_glock
*gl
= gh
->gh_gl
;
612 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
614 if (!list_empty(&gl
->gl_holders
))
617 if (gl
->gl_state
== gh
->gh_state
|| gl
->gl_state
== LM_ST_UNLOCKED
) {
618 list_del_init(&gh
->gh_list
);
620 spin_unlock(&gl
->gl_spin
);
621 if (test_bit(HIF_DEALLOC
, &gh
->gh_iflags
))
624 complete(&gh
->gh_wait
);
625 spin_lock(&gl
->gl_spin
);
628 set_bit(GLF_LOCK
, &gl
->gl_flags
);
629 spin_unlock(&gl
->gl_spin
);
631 if (gh
->gh_state
== LM_ST_UNLOCKED
||
632 gl
->gl_state
!= LM_ST_EXCLUSIVE
)
633 glops
->go_drop_th(gl
);
635 glops
->go_xmote_th(gl
, gh
->gh_state
, gh
->gh_flags
);
637 spin_lock(&gl
->gl_spin
);
644 * rq_greedy - process a queued request to drop greedy status
645 * @gh: the glock holder
647 * Returns: 1 if the queue is blocked
650 static int rq_greedy(struct gfs2_holder
*gh
)
652 struct gfs2_glock
*gl
= gh
->gh_gl
;
654 list_del_init(&gh
->gh_list
);
655 /* gh->gh_error never examined. */
656 clear_bit(GLF_GREEDY
, &gl
->gl_flags
);
657 spin_unlock(&gl
->gl_spin
);
659 gfs2_holder_uninit(gh
);
660 kfree(container_of(gh
, struct greedy
, gr_gh
));
662 spin_lock(&gl
->gl_spin
);
668 * run_queue - process holder structures on a glock
673 static void run_queue(struct gfs2_glock
*gl
)
675 struct gfs2_holder
*gh
;
679 if (test_bit(GLF_LOCK
, &gl
->gl_flags
))
682 if (!list_empty(&gl
->gl_waiters1
)) {
683 gh
= list_entry(gl
->gl_waiters1
.next
,
684 struct gfs2_holder
, gh_list
);
686 if (test_bit(HIF_MUTEX
, &gh
->gh_iflags
))
687 blocked
= rq_mutex(gh
);
689 gfs2_assert_warn(gl
->gl_sbd
, 0);
691 } else if (!list_empty(&gl
->gl_waiters2
) &&
692 !test_bit(GLF_SKIP_WAITERS2
, &gl
->gl_flags
)) {
693 gh
= list_entry(gl
->gl_waiters2
.next
,
694 struct gfs2_holder
, gh_list
);
696 if (test_bit(HIF_DEMOTE
, &gh
->gh_iflags
))
697 blocked
= rq_demote(gh
);
698 else if (test_bit(HIF_GREEDY
, &gh
->gh_iflags
))
699 blocked
= rq_greedy(gh
);
701 gfs2_assert_warn(gl
->gl_sbd
, 0);
703 } else if (!list_empty(&gl
->gl_waiters3
)) {
704 gh
= list_entry(gl
->gl_waiters3
.next
,
705 struct gfs2_holder
, gh_list
);
707 if (test_bit(HIF_PROMOTE
, &gh
->gh_iflags
))
708 blocked
= rq_promote(gh
);
710 gfs2_assert_warn(gl
->gl_sbd
, 0);
721 * gfs2_glmutex_lock - acquire a local lock on a glock
724 * Gives caller exclusive access to manipulate a glock structure.
727 void gfs2_glmutex_lock(struct gfs2_glock
*gl
)
729 struct gfs2_holder gh
;
731 gfs2_holder_init(gl
, 0, 0, &gh
);
732 set_bit(HIF_MUTEX
, &gh
.gh_iflags
);
734 spin_lock(&gl
->gl_spin
);
735 if (test_and_set_bit(GLF_LOCK
, &gl
->gl_flags
))
736 list_add_tail(&gh
.gh_list
, &gl
->gl_waiters1
);
738 complete(&gh
.gh_wait
);
739 spin_unlock(&gl
->gl_spin
);
741 wait_for_completion(&gh
.gh_wait
);
742 gfs2_holder_uninit(&gh
);
746 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
749 * Returns: 1 if the glock is acquired
752 int gfs2_glmutex_trylock(struct gfs2_glock
*gl
)
756 spin_lock(&gl
->gl_spin
);
757 if (test_and_set_bit(GLF_LOCK
, &gl
->gl_flags
))
759 spin_unlock(&gl
->gl_spin
);
765 * gfs2_glmutex_unlock - release a local lock on a glock
770 void gfs2_glmutex_unlock(struct gfs2_glock
*gl
)
772 spin_lock(&gl
->gl_spin
);
773 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
775 spin_unlock(&gl
->gl_spin
);
779 * handle_callback - add a demote request to a lock's queue
781 * @state: the state the caller wants us to change to
785 static void handle_callback(struct gfs2_glock
*gl
, unsigned int state
)
787 struct gfs2_holder
*gh
, *new_gh
= NULL
;
790 spin_lock(&gl
->gl_spin
);
792 list_for_each_entry(gh
, &gl
->gl_waiters2
, gh_list
) {
793 if (test_bit(HIF_DEMOTE
, &gh
->gh_iflags
) &&
794 gl
->gl_req_gh
!= gh
) {
795 if (gh
->gh_state
!= state
)
796 gh
->gh_state
= LM_ST_UNLOCKED
;
802 list_add_tail(&new_gh
->gh_list
, &gl
->gl_waiters2
);
805 spin_unlock(&gl
->gl_spin
);
807 new_gh
= gfs2_holder_get(gl
, state
,
808 LM_FLAG_TRY
| GL_NEVER_RECURSE
,
809 GFP_KERNEL
| __GFP_NOFAIL
),
810 set_bit(HIF_DEMOTE
, &new_gh
->gh_iflags
);
811 set_bit(HIF_DEALLOC
, &new_gh
->gh_iflags
);
817 spin_unlock(&gl
->gl_spin
);
820 gfs2_holder_put(new_gh
);
824 * state_change - record that the glock is now in a different state
826 * @new_state the new state
830 static void state_change(struct gfs2_glock
*gl
, unsigned int new_state
)
832 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
835 held1
= (gl
->gl_state
!= LM_ST_UNLOCKED
);
836 held2
= (new_state
!= LM_ST_UNLOCKED
);
838 if (held1
!= held2
) {
840 atomic_inc(&sdp
->sd_glock_held_count
);
843 atomic_dec(&sdp
->sd_glock_held_count
);
848 gl
->gl_state
= new_state
;
852 * xmote_bh - Called after the lock module is done acquiring a lock
853 * @gl: The glock in question
854 * @ret: the int returned from the lock module
858 static void xmote_bh(struct gfs2_glock
*gl
, unsigned int ret
)
860 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
861 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
862 struct gfs2_holder
*gh
= gl
->gl_req_gh
;
863 int prev_state
= gl
->gl_state
;
866 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
867 gfs2_assert_warn(sdp
, queue_empty(gl
, &gl
->gl_holders
));
868 gfs2_assert_warn(sdp
, !(ret
& LM_OUT_ASYNC
));
870 state_change(gl
, ret
& LM_OUT_ST_MASK
);
872 if (prev_state
!= LM_ST_UNLOCKED
&& !(ret
& LM_OUT_CACHEABLE
)) {
874 glops
->go_inval(gl
, DIO_METADATA
| DIO_DATA
);
875 } else if (gl
->gl_state
== LM_ST_DEFERRED
) {
876 /* We might not want to do this here.
877 Look at moving to the inode glops. */
879 glops
->go_inval(gl
, DIO_DATA
);
882 /* Deal with each possible exit condition */
885 gl
->gl_stamp
= jiffies
;
887 else if (unlikely(test_bit(SDF_SHUTDOWN
, &sdp
->sd_flags
))) {
888 spin_lock(&gl
->gl_spin
);
889 list_del_init(&gh
->gh_list
);
891 if (test_bit(HIF_RECURSE
, &gh
->gh_iflags
))
893 spin_unlock(&gl
->gl_spin
);
895 } else if (test_bit(HIF_DEMOTE
, &gh
->gh_iflags
)) {
896 spin_lock(&gl
->gl_spin
);
897 list_del_init(&gh
->gh_list
);
898 if (gl
->gl_state
== gh
->gh_state
||
899 gl
->gl_state
== LM_ST_UNLOCKED
)
902 if (gfs2_assert_warn(sdp
, gh
->gh_flags
&
903 (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
)) == -1)
904 fs_warn(sdp
, "ret = 0x%.8X\n", ret
);
905 gh
->gh_error
= GLR_TRYFAILED
;
907 spin_unlock(&gl
->gl_spin
);
909 if (ret
& LM_OUT_CANCELED
)
910 handle_callback(gl
, LM_ST_UNLOCKED
); /* Lame */
912 } else if (ret
& LM_OUT_CANCELED
) {
913 spin_lock(&gl
->gl_spin
);
914 list_del_init(&gh
->gh_list
);
915 gh
->gh_error
= GLR_CANCELED
;
916 if (test_bit(HIF_RECURSE
, &gh
->gh_iflags
))
918 spin_unlock(&gl
->gl_spin
);
920 } else if (relaxed_state_ok(gl
->gl_state
, gh
->gh_state
, gh
->gh_flags
)) {
921 spin_lock(&gl
->gl_spin
);
922 list_move_tail(&gh
->gh_list
, &gl
->gl_holders
);
924 set_bit(HIF_HOLDER
, &gh
->gh_iflags
);
925 spin_unlock(&gl
->gl_spin
);
927 set_bit(HIF_FIRST
, &gh
->gh_iflags
);
931 } else if (gh
->gh_flags
& (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
)) {
932 spin_lock(&gl
->gl_spin
);
933 list_del_init(&gh
->gh_list
);
934 gh
->gh_error
= GLR_TRYFAILED
;
935 if (test_bit(HIF_RECURSE
, &gh
->gh_iflags
))
937 spin_unlock(&gl
->gl_spin
);
940 if (gfs2_assert_withdraw(sdp
, 0) == -1)
941 fs_err(sdp
, "ret = 0x%.8X\n", ret
);
944 if (glops
->go_xmote_bh
)
945 glops
->go_xmote_bh(gl
);
948 spin_lock(&gl
->gl_spin
);
949 gl
->gl_req_gh
= NULL
;
950 gl
->gl_req_bh
= NULL
;
951 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
953 spin_unlock(&gl
->gl_spin
);
959 if (test_bit(HIF_DEALLOC
, &gh
->gh_iflags
))
962 complete(&gh
->gh_wait
);
967 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
968 * @gl: The glock in question
969 * @state: the requested state
970 * @flags: modifier flags to the lock call
974 void gfs2_glock_xmote_th(struct gfs2_glock
*gl
, unsigned int state
, int flags
)
976 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
977 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
978 int lck_flags
= flags
& (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
|
979 LM_FLAG_NOEXP
| LM_FLAG_ANY
|
981 unsigned int lck_ret
;
983 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
984 gfs2_assert_warn(sdp
, queue_empty(gl
, &gl
->gl_holders
));
985 gfs2_assert_warn(sdp
, state
!= LM_ST_UNLOCKED
);
986 gfs2_assert_warn(sdp
, state
!= gl
->gl_state
);
988 if (gl
->gl_state
== LM_ST_EXCLUSIVE
) {
991 DIO_METADATA
| DIO_DATA
| DIO_RELEASE
);
995 gl
->gl_req_bh
= xmote_bh
;
997 atomic_inc(&sdp
->sd_lm_lock_calls
);
999 lck_ret
= gfs2_lm_lock(sdp
, gl
->gl_lock
, gl
->gl_state
, state
,
1002 if (gfs2_assert_withdraw(sdp
, !(lck_ret
& LM_OUT_ERROR
)))
1005 if (lck_ret
& LM_OUT_ASYNC
)
1006 gfs2_assert_warn(sdp
, lck_ret
== LM_OUT_ASYNC
);
1008 xmote_bh(gl
, lck_ret
);
1012 * drop_bh - Called after a lock module unlock completes
1014 * @ret: the return status
1016 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
1017 * Doesn't drop the reference on the glock the top half took out
1021 static void drop_bh(struct gfs2_glock
*gl
, unsigned int ret
)
1023 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1024 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1025 struct gfs2_holder
*gh
= gl
->gl_req_gh
;
1027 clear_bit(GLF_PREFETCH
, &gl
->gl_flags
);
1029 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
1030 gfs2_assert_warn(sdp
, queue_empty(gl
, &gl
->gl_holders
));
1031 gfs2_assert_warn(sdp
, !ret
);
1033 state_change(gl
, LM_ST_UNLOCKED
);
1035 if (glops
->go_inval
)
1036 glops
->go_inval(gl
, DIO_METADATA
| DIO_DATA
);
1039 spin_lock(&gl
->gl_spin
);
1040 list_del_init(&gh
->gh_list
);
1042 spin_unlock(&gl
->gl_spin
);
1045 if (glops
->go_drop_bh
)
1046 glops
->go_drop_bh(gl
);
1048 spin_lock(&gl
->gl_spin
);
1049 gl
->gl_req_gh
= NULL
;
1050 gl
->gl_req_bh
= NULL
;
1051 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
1053 spin_unlock(&gl
->gl_spin
);
1058 if (test_bit(HIF_DEALLOC
, &gh
->gh_iflags
))
1059 gfs2_holder_put(gh
);
1061 complete(&gh
->gh_wait
);
1066 * gfs2_glock_drop_th - call into the lock module to unlock a lock
1071 void gfs2_glock_drop_th(struct gfs2_glock
*gl
)
1073 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1074 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1077 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
1078 gfs2_assert_warn(sdp
, queue_empty(gl
, &gl
->gl_holders
));
1079 gfs2_assert_warn(sdp
, gl
->gl_state
!= LM_ST_UNLOCKED
);
1081 if (gl
->gl_state
== LM_ST_EXCLUSIVE
) {
1084 DIO_METADATA
| DIO_DATA
| DIO_RELEASE
);
1087 gfs2_glock_hold(gl
);
1088 gl
->gl_req_bh
= drop_bh
;
1090 atomic_inc(&sdp
->sd_lm_unlock_calls
);
1092 ret
= gfs2_lm_unlock(sdp
, gl
->gl_lock
, gl
->gl_state
);
1094 if (gfs2_assert_withdraw(sdp
, !(ret
& LM_OUT_ERROR
)))
1100 gfs2_assert_warn(sdp
, ret
== LM_OUT_ASYNC
);
1104 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1105 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1107 * Don't cancel GL_NOCANCEL requests.
1110 static void do_cancels(struct gfs2_holder
*gh
)
1112 struct gfs2_glock
*gl
= gh
->gh_gl
;
1114 spin_lock(&gl
->gl_spin
);
1116 while (gl
->gl_req_gh
!= gh
&&
1117 !test_bit(HIF_HOLDER
, &gh
->gh_iflags
) &&
1118 !list_empty(&gh
->gh_list
)) {
1119 if (gl
->gl_req_bh
&&
1121 (gl
->gl_req_gh
->gh_flags
& GL_NOCANCEL
))) {
1122 spin_unlock(&gl
->gl_spin
);
1123 gfs2_lm_cancel(gl
->gl_sbd
, gl
->gl_lock
);
1125 spin_lock(&gl
->gl_spin
);
1127 spin_unlock(&gl
->gl_spin
);
1129 spin_lock(&gl
->gl_spin
);
1133 spin_unlock(&gl
->gl_spin
);
1137 * glock_wait_internal - wait on a glock acquisition
1138 * @gh: the glock holder
1140 * Returns: 0 on success
1143 static int glock_wait_internal(struct gfs2_holder
*gh
)
1145 struct gfs2_glock
*gl
= gh
->gh_gl
;
1146 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1147 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1149 if (test_bit(HIF_ABORTED
, &gh
->gh_iflags
))
1152 if (gh
->gh_flags
& (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
)) {
1153 spin_lock(&gl
->gl_spin
);
1154 if (gl
->gl_req_gh
!= gh
&&
1155 !test_bit(HIF_HOLDER
, &gh
->gh_iflags
) &&
1156 !list_empty(&gh
->gh_list
)) {
1157 list_del_init(&gh
->gh_list
);
1158 gh
->gh_error
= GLR_TRYFAILED
;
1159 if (test_bit(HIF_RECURSE
, &gh
->gh_iflags
))
1162 spin_unlock(&gl
->gl_spin
);
1163 return gh
->gh_error
;
1165 spin_unlock(&gl
->gl_spin
);
1168 if (gh
->gh_flags
& LM_FLAG_PRIORITY
)
1171 wait_for_completion(&gh
->gh_wait
);
1174 return gh
->gh_error
;
1176 gfs2_assert_withdraw(sdp
, test_bit(HIF_HOLDER
, &gh
->gh_iflags
));
1177 gfs2_assert_withdraw(sdp
, relaxed_state_ok(gl
->gl_state
,
1181 if (test_bit(HIF_FIRST
, &gh
->gh_iflags
)) {
1182 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
1184 if (glops
->go_lock
) {
1185 gh
->gh_error
= glops
->go_lock(gh
);
1187 spin_lock(&gl
->gl_spin
);
1188 list_del_init(&gh
->gh_list
);
1189 if (test_and_clear_bit(HIF_RECURSE
,
1192 spin_unlock(&gl
->gl_spin
);
1196 spin_lock(&gl
->gl_spin
);
1197 gl
->gl_req_gh
= NULL
;
1198 gl
->gl_req_bh
= NULL
;
1199 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
1200 if (test_bit(HIF_RECURSE
, &gh
->gh_iflags
))
1203 spin_unlock(&gl
->gl_spin
);
1206 return gh
->gh_error
;
1209 static inline struct gfs2_holder
*
1210 find_holder_by_owner(struct list_head
*head
, struct task_struct
*owner
)
1212 struct gfs2_holder
*gh
;
1214 list_for_each_entry(gh
, head
, gh_list
) {
1215 if (gh
->gh_owner
== owner
)
1225 * Make sure the new holder is compatible with the pre-existing one.
1229 static int recurse_check(struct gfs2_holder
*existing
, struct gfs2_holder
*new,
1232 struct gfs2_sbd
*sdp
= existing
->gh_gl
->gl_sbd
;
1234 if (gfs2_assert_warn(sdp
, (new->gh_flags
& LM_FLAG_ANY
) ||
1235 !(existing
->gh_flags
& LM_FLAG_ANY
)))
1238 if (gfs2_assert_warn(sdp
, (existing
->gh_flags
& GL_LOCAL_EXCL
) ||
1239 !(new->gh_flags
& GL_LOCAL_EXCL
)))
1242 if (gfs2_assert_warn(sdp
, relaxed_state_ok(state
, new->gh_state
,
1249 set_bit(HIF_ABORTED
, &new->gh_iflags
);
1254 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1255 * @gh: the holder structure to add
1259 static void add_to_queue(struct gfs2_holder
*gh
)
1261 struct gfs2_glock
*gl
= gh
->gh_gl
;
1262 struct gfs2_holder
*existing
;
1267 existing
= find_holder_by_owner(&gl
->gl_holders
, gh
->gh_owner
);
1269 if (recurse_check(existing
, gh
, gl
->gl_state
))
1272 list_add_tail(&gh
->gh_list
, &gl
->gl_holders
);
1273 set_bit(HIF_HOLDER
, &gh
->gh_iflags
);
1276 complete(&gh
->gh_wait
);
1281 existing
= find_holder_by_owner(&gl
->gl_waiters3
, gh
->gh_owner
);
1283 if (recurse_check(existing
, gh
, existing
->gh_state
))
1286 set_bit(HIF_RECURSE
, &gh
->gh_iflags
);
1287 set_bit(HIF_RECURSE
, &existing
->gh_iflags
);
1289 list_add_tail(&gh
->gh_list
, &gl
->gl_waiters3
);
1295 if (gh
->gh_flags
& LM_FLAG_PRIORITY
)
1296 list_add(&gh
->gh_list
, &gl
->gl_waiters3
);
1298 list_add_tail(&gh
->gh_list
, &gl
->gl_waiters3
);
1302 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1303 * @gh: the holder structure
1305 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1307 * Returns: 0, GLR_TRYFAILED, or errno on failure
1310 int gfs2_glock_nq(struct gfs2_holder
*gh
)
1312 struct gfs2_glock
*gl
= gh
->gh_gl
;
1313 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1316 atomic_inc(&sdp
->sd_glock_nq_calls
);
1319 if (unlikely(test_bit(SDF_SHUTDOWN
, &sdp
->sd_flags
))) {
1320 set_bit(HIF_ABORTED
, &gh
->gh_iflags
);
1324 set_bit(HIF_PROMOTE
, &gh
->gh_iflags
);
1326 spin_lock(&gl
->gl_spin
);
1329 spin_unlock(&gl
->gl_spin
);
1331 if (!(gh
->gh_flags
& GL_ASYNC
)) {
1332 error
= glock_wait_internal(gh
);
1333 if (error
== GLR_CANCELED
) {
1339 clear_bit(GLF_PREFETCH
, &gl
->gl_flags
);
1345 * gfs2_glock_poll - poll to see if an async request has been completed
1348 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1351 int gfs2_glock_poll(struct gfs2_holder
*gh
)
1353 struct gfs2_glock
*gl
= gh
->gh_gl
;
1356 spin_lock(&gl
->gl_spin
);
1358 if (test_bit(HIF_HOLDER
, &gh
->gh_iflags
))
1360 else if (list_empty(&gh
->gh_list
)) {
1361 if (gh
->gh_error
== GLR_CANCELED
) {
1362 spin_unlock(&gl
->gl_spin
);
1364 if (gfs2_glock_nq(gh
))
1371 spin_unlock(&gl
->gl_spin
);
1377 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1378 * @gh: the holder structure
1380 * Returns: 0, GLR_TRYFAILED, or errno on failure
1383 int gfs2_glock_wait(struct gfs2_holder
*gh
)
1387 error
= glock_wait_internal(gh
);
1388 if (error
== GLR_CANCELED
) {
1390 gh
->gh_flags
&= ~GL_ASYNC
;
1391 error
= gfs2_glock_nq(gh
);
1398 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1399 * @gh: the glock holder
1403 void gfs2_glock_dq(struct gfs2_holder
*gh
)
1405 struct gfs2_glock
*gl
= gh
->gh_gl
;
1406 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1407 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1409 atomic_inc(&sdp
->sd_glock_dq_calls
);
1411 if (gh
->gh_flags
& GL_SYNC
)
1412 set_bit(GLF_SYNC
, &gl
->gl_flags
);
1414 if (gh
->gh_flags
& GL_NOCACHE
)
1415 handle_callback(gl
, LM_ST_UNLOCKED
);
1417 gfs2_glmutex_lock(gl
);
1419 spin_lock(&gl
->gl_spin
);
1420 list_del_init(&gh
->gh_list
);
1422 if (list_empty(&gl
->gl_holders
)) {
1423 spin_unlock(&gl
->gl_spin
);
1425 if (glops
->go_unlock
)
1426 glops
->go_unlock(gh
);
1428 if (test_bit(GLF_SYNC
, &gl
->gl_flags
)) {
1430 glops
->go_sync(gl
, DIO_METADATA
| DIO_DATA
);
1433 gl
->gl_stamp
= jiffies
;
1435 spin_lock(&gl
->gl_spin
);
1438 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
1440 spin_unlock(&gl
->gl_spin
);
1444 * gfs2_glock_prefetch - Try to prefetch a glock
1446 * @state: the state to prefetch in
1447 * @flags: flags passed to go_xmote_th()
1451 void gfs2_glock_prefetch(struct gfs2_glock
*gl
, unsigned int state
, int flags
)
1453 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1455 spin_lock(&gl
->gl_spin
);
1457 if (test_bit(GLF_LOCK
, &gl
->gl_flags
) ||
1458 !list_empty(&gl
->gl_holders
) ||
1459 !list_empty(&gl
->gl_waiters1
) ||
1460 !list_empty(&gl
->gl_waiters2
) ||
1461 !list_empty(&gl
->gl_waiters3
) ||
1462 relaxed_state_ok(gl
->gl_state
, state
, flags
)) {
1463 spin_unlock(&gl
->gl_spin
);
1467 set_bit(GLF_PREFETCH
, &gl
->gl_flags
);
1468 set_bit(GLF_LOCK
, &gl
->gl_flags
);
1469 spin_unlock(&gl
->gl_spin
);
1471 glops
->go_xmote_th(gl
, state
, flags
);
1473 atomic_inc(&gl
->gl_sbd
->sd_glock_prefetch_calls
);
1477 * gfs2_glock_force_drop - Force a glock to be uncached
1482 void gfs2_glock_force_drop(struct gfs2_glock
*gl
)
1484 struct gfs2_holder gh
;
1486 gfs2_holder_init(gl
, LM_ST_UNLOCKED
, GL_NEVER_RECURSE
, &gh
);
1487 set_bit(HIF_DEMOTE
, &gh
.gh_iflags
);
1489 spin_lock(&gl
->gl_spin
);
1490 list_add_tail(&gh
.gh_list
, &gl
->gl_waiters2
);
1492 spin_unlock(&gl
->gl_spin
);
1494 wait_for_completion(&gh
.gh_wait
);
1495 gfs2_holder_uninit(&gh
);
1498 static void greedy_work(void *data
)
1500 struct greedy
*gr
= (struct greedy
*)data
;
1501 struct gfs2_holder
*gh
= &gr
->gr_gh
;
1502 struct gfs2_glock
*gl
= gh
->gh_gl
;
1503 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1505 clear_bit(GLF_SKIP_WAITERS2
, &gl
->gl_flags
);
1507 if (glops
->go_greedy
)
1508 glops
->go_greedy(gl
);
1510 spin_lock(&gl
->gl_spin
);
1512 if (list_empty(&gl
->gl_waiters2
)) {
1513 clear_bit(GLF_GREEDY
, &gl
->gl_flags
);
1514 spin_unlock(&gl
->gl_spin
);
1515 gfs2_holder_uninit(gh
);
1518 gfs2_glock_hold(gl
);
1519 list_add_tail(&gh
->gh_list
, &gl
->gl_waiters2
);
1521 spin_unlock(&gl
->gl_spin
);
1527 * gfs2_glock_be_greedy -
1531 * Returns: 0 if go_greedy will be called, 1 otherwise
1534 int gfs2_glock_be_greedy(struct gfs2_glock
*gl
, unsigned int time
)
1537 struct gfs2_holder
*gh
;
1540 gl
->gl_sbd
->sd_args
.ar_localcaching
||
1541 test_and_set_bit(GLF_GREEDY
, &gl
->gl_flags
))
1544 gr
= kmalloc(sizeof(struct greedy
), GFP_KERNEL
);
1546 clear_bit(GLF_GREEDY
, &gl
->gl_flags
);
1551 gfs2_holder_init(gl
, 0, GL_NEVER_RECURSE
, gh
);
1552 set_bit(HIF_GREEDY
, &gh
->gh_iflags
);
1553 INIT_WORK(&gr
->gr_work
, greedy_work
, gr
);
1555 set_bit(GLF_SKIP_WAITERS2
, &gl
->gl_flags
);
1556 schedule_delayed_work(&gr
->gr_work
, time
);
1562 * gfs2_glock_nq_init - intialize a holder and enqueue it on a glock
1564 * @state: the state we're requesting
1565 * @flags: the modifier flags
1566 * @gh: the holder structure
1568 * Returns: 0, GLR_*, or errno
1571 int gfs2_glock_nq_init(struct gfs2_glock
*gl
, unsigned int state
, int flags
,
1572 struct gfs2_holder
*gh
)
1576 gfs2_holder_init(gl
, state
, flags
, gh
);
1578 error
= gfs2_glock_nq(gh
);
1580 gfs2_holder_uninit(gh
);
1586 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1587 * @gh: the holder structure
1591 void gfs2_glock_dq_uninit(struct gfs2_holder
*gh
)
1594 gfs2_holder_uninit(gh
);
1598 * gfs2_glock_nq_num - acquire a glock based on lock number
1599 * @sdp: the filesystem
1600 * @number: the lock number
1601 * @glops: the glock operations for the type of glock
1602 * @state: the state to acquire the glock in
1603 * @flags: modifier flags for the aquisition
1604 * @gh: the struct gfs2_holder
1609 int gfs2_glock_nq_num(struct gfs2_sbd
*sdp
, uint64_t number
,
1610 struct gfs2_glock_operations
*glops
, unsigned int state
,
1611 int flags
, struct gfs2_holder
*gh
)
1613 struct gfs2_glock
*gl
;
1616 error
= gfs2_glock_get(sdp
, number
, glops
, CREATE
, &gl
);
1618 error
= gfs2_glock_nq_init(gl
, state
, flags
, gh
);
1626 * glock_compare - Compare two struct gfs2_glock structures for sorting
1627 * @arg_a: the first structure
1628 * @arg_b: the second structure
1632 static int glock_compare(const void *arg_a
, const void *arg_b
)
1634 struct gfs2_holder
*gh_a
= *(struct gfs2_holder
**)arg_a
;
1635 struct gfs2_holder
*gh_b
= *(struct gfs2_holder
**)arg_b
;
1636 struct lm_lockname
*a
= &gh_a
->gh_gl
->gl_name
;
1637 struct lm_lockname
*b
= &gh_b
->gh_gl
->gl_name
;
1640 if (a
->ln_number
> b
->ln_number
)
1642 else if (a
->ln_number
< b
->ln_number
)
1645 if (gh_a
->gh_state
== LM_ST_SHARED
&&
1646 gh_b
->gh_state
== LM_ST_EXCLUSIVE
)
1648 else if (!(gh_a
->gh_flags
& GL_LOCAL_EXCL
) &&
1649 (gh_b
->gh_flags
& GL_LOCAL_EXCL
))
1657 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1658 * @num_gh: the number of structures
1659 * @ghs: an array of struct gfs2_holder structures
1661 * Returns: 0 on success (all glocks acquired),
1662 * errno on failure (no glocks acquired)
1665 static int nq_m_sync(unsigned int num_gh
, struct gfs2_holder
*ghs
,
1666 struct gfs2_holder
**p
)
1671 for (x
= 0; x
< num_gh
; x
++)
1674 sort(p
, num_gh
, sizeof(struct gfs2_holder
*), glock_compare
, NULL
);
1676 for (x
= 0; x
< num_gh
; x
++) {
1677 p
[x
]->gh_flags
&= ~(LM_FLAG_TRY
| GL_ASYNC
);
1679 error
= gfs2_glock_nq(p
[x
]);
1682 gfs2_glock_dq(p
[x
]);
1691 * gfs2_glock_nq_m - acquire multiple glocks
1692 * @num_gh: the number of structures
1693 * @ghs: an array of struct gfs2_holder structures
1695 * Figure out how big an impact this function has. Either:
1696 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1697 * 2) Forget async stuff and just call nq_m_sync()
1698 * 3) Leave it like it is
1700 * Returns: 0 on success (all glocks acquired),
1701 * errno on failure (no glocks acquired)
1704 int gfs2_glock_nq_m(unsigned int num_gh
, struct gfs2_holder
*ghs
)
1708 int borked
= 0, serious
= 0;
1715 ghs
->gh_flags
&= ~(LM_FLAG_TRY
| GL_ASYNC
);
1716 return gfs2_glock_nq(ghs
);
1719 e
= kcalloc(num_gh
, sizeof(struct gfs2_holder
*), GFP_KERNEL
);
1723 for (x
= 0; x
< num_gh
; x
++) {
1724 ghs
[x
].gh_flags
|= LM_FLAG_TRY
| GL_ASYNC
;
1725 error
= gfs2_glock_nq(&ghs
[x
]);
1734 for (x
= 0; x
< num_gh
; x
++) {
1735 error
= e
[x
] = glock_wait_internal(&ghs
[x
]);
1738 if (error
!= GLR_TRYFAILED
&& error
!= GLR_CANCELED
)
1748 for (x
= 0; x
< num_gh
; x
++)
1750 gfs2_glock_dq(&ghs
[x
]);
1755 for (x
= 0; x
< num_gh
; x
++)
1756 gfs2_holder_reinit(ghs
[x
].gh_state
, ghs
[x
].gh_flags
,
1758 error
= nq_m_sync(num_gh
, ghs
, (struct gfs2_holder
**)e
);
1767 * gfs2_glock_dq_m - release multiple glocks
1768 * @num_gh: the number of structures
1769 * @ghs: an array of struct gfs2_holder structures
1773 void gfs2_glock_dq_m(unsigned int num_gh
, struct gfs2_holder
*ghs
)
1777 for (x
= 0; x
< num_gh
; x
++)
1778 gfs2_glock_dq(&ghs
[x
]);
1782 * gfs2_glock_dq_uninit_m - release multiple glocks
1783 * @num_gh: the number of structures
1784 * @ghs: an array of struct gfs2_holder structures
1788 void gfs2_glock_dq_uninit_m(unsigned int num_gh
, struct gfs2_holder
*ghs
)
1792 for (x
= 0; x
< num_gh
; x
++)
1793 gfs2_glock_dq_uninit(&ghs
[x
]);
1797 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1798 * @sdp: the filesystem
1799 * @number: the lock number
1800 * @glops: the glock operations for the type of glock
1801 * @state: the state to acquire the glock in
1802 * @flags: modifier flags for the aquisition
1807 void gfs2_glock_prefetch_num(struct gfs2_sbd
*sdp
, uint64_t number
,
1808 struct gfs2_glock_operations
*glops
,
1809 unsigned int state
, int flags
)
1811 struct gfs2_glock
*gl
;
1814 if (atomic_read(&sdp
->sd_reclaim_count
) <
1815 gfs2_tune_get(sdp
, gt_reclaim_limit
)) {
1816 error
= gfs2_glock_get(sdp
, number
, glops
, CREATE
, &gl
);
1818 gfs2_glock_prefetch(gl
, state
, flags
);
1825 * gfs2_lvb_hold - attach a LVB from a glock
1826 * @gl: The glock in question
1830 int gfs2_lvb_hold(struct gfs2_glock
*gl
)
1834 gfs2_glmutex_lock(gl
);
1836 if (!atomic_read(&gl
->gl_lvb_count
)) {
1837 error
= gfs2_lm_hold_lvb(gl
->gl_sbd
, gl
->gl_lock
, &gl
->gl_lvb
);
1839 gfs2_glmutex_unlock(gl
);
1842 gfs2_glock_hold(gl
);
1844 atomic_inc(&gl
->gl_lvb_count
);
1846 gfs2_glmutex_unlock(gl
);
1852 * gfs2_lvb_unhold - detach a LVB from a glock
1853 * @gl: The glock in question
1857 void gfs2_lvb_unhold(struct gfs2_glock
*gl
)
1859 gfs2_glock_hold(gl
);
1860 gfs2_glmutex_lock(gl
);
1862 gfs2_assert(gl
->gl_sbd
, atomic_read(&gl
->gl_lvb_count
) > 0);
1863 if (atomic_dec_and_test(&gl
->gl_lvb_count
)) {
1864 gfs2_lm_unhold_lvb(gl
->gl_sbd
, gl
->gl_lock
, gl
->gl_lvb
);
1869 gfs2_glmutex_unlock(gl
);
1873 void gfs2_lvb_sync(struct gfs2_glock
*gl
)
1875 gfs2_glmutex_lock(gl
);
1877 gfs2_assert(gl
->gl_sbd
, atomic_read(&gl
->gl_lvb_count
));
1878 if (!gfs2_assert_warn(gl
->gl_sbd
, gfs2_glock_is_held_excl(gl
)))
1879 gfs2_lm_sync_lvb(gl
->gl_sbd
, gl
->gl_lock
, gl
->gl_lvb
);
1881 gfs2_glmutex_unlock(gl
);
1884 static void blocking_cb(struct gfs2_sbd
*sdp
, struct lm_lockname
*name
,
1887 struct gfs2_glock
*gl
;
1889 gl
= gfs2_glock_find(sdp
, name
);
1893 if (gl
->gl_ops
->go_callback
)
1894 gl
->gl_ops
->go_callback(gl
, state
);
1895 handle_callback(gl
, state
);
1897 spin_lock(&gl
->gl_spin
);
1899 spin_unlock(&gl
->gl_spin
);
1905 * gfs2_glock_cb - Callback used by locking module
1906 * @fsdata: Pointer to the superblock
1907 * @type: Type of callback
1908 * @data: Type dependent data pointer
1910 * Called by the locking module when it wants to tell us something.
1911 * Either we need to drop a lock, one of our ASYNC requests completed, or
1912 * a journal from another client needs to be recovered.
1915 void gfs2_glock_cb(lm_fsdata_t
*fsdata
, unsigned int type
, void *data
)
1917 struct gfs2_sbd
*sdp
= (struct gfs2_sbd
*)fsdata
;
1919 atomic_inc(&sdp
->sd_lm_callbacks
);
1923 blocking_cb(sdp
, (struct lm_lockname
*)data
, LM_ST_UNLOCKED
);
1927 blocking_cb(sdp
, (struct lm_lockname
*)data
, LM_ST_DEFERRED
);
1931 blocking_cb(sdp
, (struct lm_lockname
*)data
, LM_ST_SHARED
);
1935 struct lm_async_cb
*async
= (struct lm_async_cb
*)data
;
1936 struct gfs2_glock
*gl
;
1938 gl
= gfs2_glock_find(sdp
, &async
->lc_name
);
1939 if (gfs2_assert_warn(sdp
, gl
))
1941 if (!gfs2_assert_warn(sdp
, gl
->gl_req_bh
))
1942 gl
->gl_req_bh(gl
, async
->lc_ret
);
1948 case LM_CB_NEED_RECOVERY
:
1949 gfs2_jdesc_make_dirty(sdp
, *(unsigned int *)data
);
1950 if (sdp
->sd_recoverd_process
)
1951 wake_up_process(sdp
->sd_recoverd_process
);
1954 case LM_CB_DROPLOCKS
:
1955 gfs2_gl_hash_clear(sdp
, NO_WAIT
);
1956 gfs2_quota_scan(sdp
);
1960 gfs2_assert_warn(sdp
, 0);
1966 * gfs2_try_toss_inode - try to remove a particular inode struct from cache
1967 * sdp: the filesystem
1968 * inum: the inode number
1972 void gfs2_try_toss_inode(struct gfs2_sbd
*sdp
, struct gfs2_inum
*inum
)
1974 struct gfs2_glock
*gl
;
1975 struct gfs2_inode
*ip
;
1978 error
= gfs2_glock_get(sdp
, inum
->no_addr
, &gfs2_inode_glops
,
1983 if (!gfs2_glmutex_trylock(gl
))
1990 if (atomic_read(&ip
->i_count
))
1993 gfs2_inode_destroy(ip
);
1996 gfs2_glmutex_unlock(gl
);
2003 * gfs2_iopen_go_callback - Try to kick the inode/vnode associated with an
2004 * iopen glock from memory
2005 * @io_gl: the iopen glock
2006 * @state: the state into which the glock should be put
2010 void gfs2_iopen_go_callback(struct gfs2_glock
*io_gl
, unsigned int state
)
2012 struct gfs2_glock
*i_gl
;
2014 if (state
!= LM_ST_UNLOCKED
)
2017 spin_lock(&io_gl
->gl_spin
);
2018 i_gl
= get_gl2gl(io_gl
);
2020 gfs2_glock_hold(i_gl
);
2021 spin_unlock(&io_gl
->gl_spin
);
2023 spin_unlock(&io_gl
->gl_spin
);
2027 if (gfs2_glmutex_trylock(i_gl
)) {
2028 struct gfs2_inode
*ip
= get_gl2ip(i_gl
);
2030 gfs2_try_toss_vnode(ip
);
2031 gfs2_glmutex_unlock(i_gl
);
2032 gfs2_glock_schedule_for_reclaim(i_gl
);
2035 gfs2_glmutex_unlock(i_gl
);
2039 gfs2_glock_put(i_gl
);
2043 * demote_ok - Check to see if it's ok to unlock a glock
2046 * Returns: 1 if it's ok
2049 static int demote_ok(struct gfs2_glock
*gl
)
2051 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
2052 struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
2055 if (test_bit(GLF_STICKY
, &gl
->gl_flags
))
2057 else if (test_bit(GLF_PREFETCH
, &gl
->gl_flags
))
2058 demote
= time_after_eq(jiffies
,
2060 gfs2_tune_get(sdp
, gt_prefetch_secs
) * HZ
);
2061 else if (glops
->go_demote_ok
)
2062 demote
= glops
->go_demote_ok(gl
);
2068 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
2073 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock
*gl
)
2075 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
2077 spin_lock(&sdp
->sd_reclaim_lock
);
2078 if (list_empty(&gl
->gl_reclaim
)) {
2079 gfs2_glock_hold(gl
);
2080 list_add(&gl
->gl_reclaim
, &sdp
->sd_reclaim_list
);
2081 atomic_inc(&sdp
->sd_reclaim_count
);
2083 spin_unlock(&sdp
->sd_reclaim_lock
);
2085 wake_up(&sdp
->sd_reclaim_wq
);
2089 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
2090 * @sdp: the filesystem
2092 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
2093 * different glock and we notice that there are a lot of glocks in the
2098 void gfs2_reclaim_glock(struct gfs2_sbd
*sdp
)
2100 struct gfs2_glock
*gl
;
2102 spin_lock(&sdp
->sd_reclaim_lock
);
2103 if (list_empty(&sdp
->sd_reclaim_list
)) {
2104 spin_unlock(&sdp
->sd_reclaim_lock
);
2107 gl
= list_entry(sdp
->sd_reclaim_list
.next
,
2108 struct gfs2_glock
, gl_reclaim
);
2109 list_del_init(&gl
->gl_reclaim
);
2110 spin_unlock(&sdp
->sd_reclaim_lock
);
2112 atomic_dec(&sdp
->sd_reclaim_count
);
2113 atomic_inc(&sdp
->sd_reclaimed
);
2115 if (gfs2_glmutex_trylock(gl
)) {
2116 if (gl
->gl_ops
== &gfs2_inode_glops
) {
2117 struct gfs2_inode
*ip
= get_gl2ip(gl
);
2118 if (ip
&& !atomic_read(&ip
->i_count
))
2119 gfs2_inode_destroy(ip
);
2121 if (queue_empty(gl
, &gl
->gl_holders
) &&
2122 gl
->gl_state
!= LM_ST_UNLOCKED
&&
2124 handle_callback(gl
, LM_ST_UNLOCKED
);
2125 gfs2_glmutex_unlock(gl
);
2132 * examine_bucket - Call a function for glock in a hash bucket
2133 * @examiner: the function
2134 * @sdp: the filesystem
2135 * @bucket: the bucket
2137 * Returns: 1 if the bucket has entries
2140 static int examine_bucket(glock_examiner examiner
, struct gfs2_sbd
*sdp
,
2141 struct gfs2_gl_hash_bucket
*bucket
)
2143 struct glock_plug plug
;
2144 struct list_head
*tmp
;
2145 struct gfs2_glock
*gl
;
2148 /* Add "plug" to end of bucket list, work back up list from there */
2149 memset(&plug
.gl_flags
, 0, sizeof(unsigned long));
2150 set_bit(GLF_PLUG
, &plug
.gl_flags
);
2152 write_lock(&bucket
->hb_lock
);
2153 list_add(&plug
.gl_list
, &bucket
->hb_list
);
2154 write_unlock(&bucket
->hb_lock
);
2157 write_lock(&bucket
->hb_lock
);
2160 tmp
= plug
.gl_list
.next
;
2162 if (tmp
== &bucket
->hb_list
) {
2163 list_del(&plug
.gl_list
);
2164 entries
= !list_empty(&bucket
->hb_list
);
2165 write_unlock(&bucket
->hb_lock
);
2168 gl
= list_entry(tmp
, struct gfs2_glock
, gl_list
);
2170 /* Move plug up list */
2171 list_move(&plug
.gl_list
, &gl
->gl_list
);
2173 if (test_bit(GLF_PLUG
, &gl
->gl_flags
))
2176 /* examiner() must glock_put() */
2177 gfs2_glock_hold(gl
);
2182 write_unlock(&bucket
->hb_lock
);
2189 * scan_glock - look at a glock and see if we can reclaim it
2190 * @gl: the glock to look at
2194 static void scan_glock(struct gfs2_glock
*gl
)
2196 if (gfs2_glmutex_trylock(gl
)) {
2197 if (gl
->gl_ops
== &gfs2_inode_glops
) {
2198 struct gfs2_inode
*ip
= get_gl2ip(gl
);
2199 if (ip
&& !atomic_read(&ip
->i_count
))
2202 if (queue_empty(gl
, &gl
->gl_holders
) &&
2203 gl
->gl_state
!= LM_ST_UNLOCKED
&&
2207 gfs2_glmutex_unlock(gl
);
2215 gfs2_glmutex_unlock(gl
);
2216 gfs2_glock_schedule_for_reclaim(gl
);
2221 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
2222 * @sdp: the filesystem
2226 void gfs2_scand_internal(struct gfs2_sbd
*sdp
)
2230 for (x
= 0; x
< GFS2_GL_HASH_SIZE
; x
++) {
2231 examine_bucket(scan_glock
, sdp
, &sdp
->sd_gl_hash
[x
]);
2237 * clear_glock - look at a glock and see if we can free it from glock cache
2238 * @gl: the glock to look at
2242 static void clear_glock(struct gfs2_glock
*gl
)
2244 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
2247 spin_lock(&sdp
->sd_reclaim_lock
);
2248 if (!list_empty(&gl
->gl_reclaim
)) {
2249 list_del_init(&gl
->gl_reclaim
);
2250 atomic_dec(&sdp
->sd_reclaim_count
);
2251 released
= gfs2_glock_put(gl
);
2252 gfs2_assert(sdp
, !released
);
2254 spin_unlock(&sdp
->sd_reclaim_lock
);
2256 if (gfs2_glmutex_trylock(gl
)) {
2257 if (gl
->gl_ops
== &gfs2_inode_glops
) {
2258 struct gfs2_inode
*ip
= get_gl2ip(gl
);
2259 if (ip
&& !atomic_read(&ip
->i_count
))
2260 gfs2_inode_destroy(ip
);
2262 if (queue_empty(gl
, &gl
->gl_holders
) &&
2263 gl
->gl_state
!= LM_ST_UNLOCKED
)
2264 handle_callback(gl
, LM_ST_UNLOCKED
);
2266 gfs2_glmutex_unlock(gl
);
2273 * gfs2_gl_hash_clear - Empty out the glock hash table
2274 * @sdp: the filesystem
2275 * @wait: wait until it's all gone
2277 * Called when unmounting the filesystem, or when inter-node lock manager
2278 * requests DROPLOCKS because it is running out of capacity.
2281 void gfs2_gl_hash_clear(struct gfs2_sbd
*sdp
, int wait
)
2292 for (x
= 0; x
< GFS2_GL_HASH_SIZE
; x
++)
2293 if (examine_bucket(clear_glock
, sdp
,
2294 &sdp
->sd_gl_hash
[x
]))
2300 if (time_after_eq(jiffies
,
2301 t
+ gfs2_tune_get(sdp
, gt_stall_secs
) * HZ
)) {
2302 fs_warn(sdp
, "Unmount seems to be stalled. "
2303 "Dumping lock state...\n");
2304 gfs2_dump_lockstate(sdp
);
2308 /* invalidate_inodes() requires that the sb inodes list
2309 not change, but an async completion callback for an
2310 unlock can occur which does glock_put() which
2311 can call iput() which will change the sb inodes list.
2312 invalidate_inodes_mutex prevents glock_put()'s during
2313 an invalidate_inodes() */
2315 down(&sdp
->sd_invalidate_inodes_mutex
);
2316 invalidate_inodes(sdp
->sd_vfs
);
2317 up(&sdp
->sd_invalidate_inodes_mutex
);
2323 * Diagnostic routines to help debug distributed deadlock
2327 * dump_holder - print information about a glock holder
2328 * @str: a string naming the type of holder
2329 * @gh: the glock holder
2331 * Returns: 0 on success, -ENOBUFS when we run out of space
2334 static int dump_holder(char *str
, struct gfs2_holder
*gh
)
2337 int error
= -ENOBUFS
;
2339 printk(" %s\n", str
);
2340 printk(" owner = %ld\n",
2341 (gh
->gh_owner
) ? (long)gh
->gh_owner
->pid
: -1);
2342 printk(" gh_state = %u\n", gh
->gh_state
);
2343 printk(" gh_flags =");
2344 for (x
= 0; x
< 32; x
++)
2345 if (gh
->gh_flags
& (1 << x
))
2348 printk(" error = %d\n", gh
->gh_error
);
2349 printk(" gh_iflags =");
2350 for (x
= 0; x
< 32; x
++)
2351 if (test_bit(x
, &gh
->gh_iflags
))
2361 * dump_inode - print information about an inode
2364 * Returns: 0 on success, -ENOBUFS when we run out of space
2367 static int dump_inode(struct gfs2_inode
*ip
)
2370 int error
= -ENOBUFS
;
2372 printk(" Inode:\n");
2373 printk(" num = %llu %llu\n",
2374 ip
->i_num
.no_formal_ino
, ip
->i_num
.no_addr
);
2375 printk(" type = %u\n", IF2DT(ip
->i_di
.di_mode
));
2376 printk(" i_count = %d\n", atomic_read(&ip
->i_count
));
2377 printk(" i_flags =");
2378 for (x
= 0; x
< 32; x
++)
2379 if (test_bit(x
, &ip
->i_flags
))
2382 printk(" vnode = %s\n", (ip
->i_vnode
) ? "yes" : "no");
2390 * dump_glock - print information about a glock
2392 * @count: where we are in the buffer
2394 * Returns: 0 on success, -ENOBUFS when we run out of space
2397 static int dump_glock(struct gfs2_glock
*gl
)
2399 struct gfs2_holder
*gh
;
2401 int error
= -ENOBUFS
;
2403 spin_lock(&gl
->gl_spin
);
2405 printk("Glock (%u, %llu)\n",
2406 gl
->gl_name
.ln_type
,
2407 gl
->gl_name
.ln_number
);
2408 printk(" gl_flags =");
2409 for (x
= 0; x
< 32; x
++)
2410 if (test_bit(x
, &gl
->gl_flags
))
2413 printk(" gl_ref = %d\n", atomic_read(&gl
->gl_ref
.refcount
));
2414 printk(" gl_state = %u\n", gl
->gl_state
);
2415 printk(" req_gh = %s\n", (gl
->gl_req_gh
) ? "yes" : "no");
2416 printk(" req_bh = %s\n", (gl
->gl_req_bh
) ? "yes" : "no");
2417 printk(" lvb_count = %d\n", atomic_read(&gl
->gl_lvb_count
));
2418 printk(" object = %s\n", (gl
->gl_object
) ? "yes" : "no");
2419 printk(" le = %s\n",
2420 (list_empty(&gl
->gl_le
.le_list
)) ? "no" : "yes");
2421 printk(" reclaim = %s\n",
2422 (list_empty(&gl
->gl_reclaim
)) ? "no" : "yes");
2424 printk(" aspace = %lu\n",
2425 gl
->gl_aspace
->i_mapping
->nrpages
);
2427 printk(" aspace = no\n");
2428 printk(" ail = %d\n", atomic_read(&gl
->gl_ail_count
));
2429 if (gl
->gl_req_gh
) {
2430 error
= dump_holder("Request", gl
->gl_req_gh
);
2434 list_for_each_entry(gh
, &gl
->gl_holders
, gh_list
) {
2435 error
= dump_holder("Holder", gh
);
2439 list_for_each_entry(gh
, &gl
->gl_waiters1
, gh_list
) {
2440 error
= dump_holder("Waiter1", gh
);
2444 list_for_each_entry(gh
, &gl
->gl_waiters2
, gh_list
) {
2445 error
= dump_holder("Waiter2", gh
);
2449 list_for_each_entry(gh
, &gl
->gl_waiters3
, gh_list
) {
2450 error
= dump_holder("Waiter3", gh
);
2454 if (gl
->gl_ops
== &gfs2_inode_glops
&& get_gl2ip(gl
)) {
2455 if (!test_bit(GLF_LOCK
, &gl
->gl_flags
) &&
2456 list_empty(&gl
->gl_holders
)) {
2457 error
= dump_inode(get_gl2ip(gl
));
2462 printk(" Inode: busy\n");
2469 spin_unlock(&gl
->gl_spin
);
2475 * gfs2_dump_lockstate - print out the current lockstate
2476 * @sdp: the filesystem
2477 * @ub: the buffer to copy the information into
2479 * If @ub is NULL, dump the lockstate to the console.
2483 int gfs2_dump_lockstate(struct gfs2_sbd
*sdp
)
2485 struct gfs2_gl_hash_bucket
*bucket
;
2486 struct gfs2_glock
*gl
;
2490 for (x
= 0; x
< GFS2_GL_HASH_SIZE
; x
++) {
2491 bucket
= &sdp
->sd_gl_hash
[x
];
2493 read_lock(&bucket
->hb_lock
);
2495 list_for_each_entry(gl
, &bucket
->hb_list
, gl_list
) {
2496 if (test_bit(GLF_PLUG
, &gl
->gl_flags
))
2499 error
= dump_glock(gl
);
2504 read_unlock(&bucket
->hb_lock
);