2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/delay.h>
16 #include <linux/sort.h>
17 #include <linux/jhash.h>
18 #include <linux/kallsyms.h>
19 #include <linux/gfs2_ondisk.h>
20 #include <linux/list.h>
21 #include <linux/lm_interface.h>
22 #include <asm/uaccess.h>
37 struct gfs2_holder gr_gh
;
38 struct work_struct gr_work
;
41 struct gfs2_gl_hash_bucket
{
42 struct hlist_head hb_list
;
45 typedef void (*glock_examiner
) (struct gfs2_glock
* gl
);
47 static int gfs2_dump_lockstate(struct gfs2_sbd
*sdp
);
48 static int dump_glock(struct gfs2_glock
*gl
);
49 static int dump_inode(struct gfs2_inode
*ip
);
51 #define GFS2_GL_HASH_SHIFT 15
52 #define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT)
53 #define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1)
55 static struct gfs2_gl_hash_bucket gl_hash_table
[GFS2_GL_HASH_SIZE
];
58 * Despite what you might think, the numbers below are not arbitrary :-)
59 * They are taken from the ipv4 routing hash code, which is well tested
60 * and thus should be nearly optimal. Later on we might tweek the numbers
61 * but for now this should be fine.
63 * The reason for putting the locks in a separate array from the list heads
64 * is that we can have fewer locks than list heads and save memory. We use
65 * the same hash function for both, but with a different hash mask.
67 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
68 defined(CONFIG_PROVE_LOCKING)
71 # define GL_HASH_LOCK_SZ 256
74 # define GL_HASH_LOCK_SZ 4096
76 # define GL_HASH_LOCK_SZ 2048
78 # define GL_HASH_LOCK_SZ 1024
80 # define GL_HASH_LOCK_SZ 512
82 # define GL_HASH_LOCK_SZ 256
86 /* We never want more locks than chains */
87 #if GFS2_GL_HASH_SIZE < GL_HASH_LOCK_SZ
88 # undef GL_HASH_LOCK_SZ
89 # define GL_HASH_LOCK_SZ GFS2_GL_HASH_SIZE
92 static rwlock_t gl_hash_locks
[GL_HASH_LOCK_SZ
];
94 static inline rwlock_t
*gl_lock_addr(unsigned int x
)
96 return &gl_hash_locks
[x
& (GL_HASH_LOCK_SZ
-1)];
98 #else /* not SMP, so no spinlocks required */
99 static inline rwlock_t
*gl_lock_addr(x
)
106 * relaxed_state_ok - is a requested lock compatible with the current lock mode?
107 * @actual: the current state of the lock
108 * @requested: the lock state that was requested by the caller
109 * @flags: the modifier flags passed in by the caller
111 * Returns: 1 if the locks are compatible, 0 otherwise
114 static inline int relaxed_state_ok(unsigned int actual
, unsigned requested
,
117 if (actual
== requested
)
120 if (flags
& GL_EXACT
)
123 if (actual
== LM_ST_EXCLUSIVE
&& requested
== LM_ST_SHARED
)
126 if (actual
!= LM_ST_UNLOCKED
&& (flags
& LM_FLAG_ANY
))
133 * gl_hash() - Turn glock number into hash bucket number
134 * @lock: The glock number
136 * Returns: The number of the corresponding hash bucket
139 static unsigned int gl_hash(const struct gfs2_sbd
*sdp
,
140 const struct lm_lockname
*name
)
144 h
= jhash(&name
->ln_number
, sizeof(u64
), 0);
145 h
= jhash(&name
->ln_type
, sizeof(unsigned int), h
);
146 h
= jhash(&sdp
, sizeof(struct gfs2_sbd
*), h
);
147 h
&= GFS2_GL_HASH_MASK
;
153 * glock_free() - Perform a few checks and then release struct gfs2_glock
154 * @gl: The glock to release
156 * Also calls lock module to release its internal structure for this glock.
160 static void glock_free(struct gfs2_glock
*gl
)
162 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
163 struct inode
*aspace
= gl
->gl_aspace
;
165 gfs2_lm_put_lock(sdp
, gl
->gl_lock
);
168 gfs2_aspace_put(aspace
);
170 kmem_cache_free(gfs2_glock_cachep
, gl
);
174 * gfs2_glock_hold() - increment reference count on glock
175 * @gl: The glock to hold
179 void gfs2_glock_hold(struct gfs2_glock
*gl
)
181 atomic_inc(&gl
->gl_ref
);
185 * gfs2_glock_put() - Decrement reference count on glock
186 * @gl: The glock to put
190 int gfs2_glock_put(struct gfs2_glock
*gl
)
193 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
195 write_lock(gl_lock_addr(gl
->gl_hash
));
196 if (atomic_dec_and_test(&gl
->gl_ref
)) {
197 hlist_del(&gl
->gl_list
);
198 write_unlock(gl_lock_addr(gl
->gl_hash
));
199 BUG_ON(spin_is_locked(&gl
->gl_spin
));
200 gfs2_assert(sdp
, gl
->gl_state
== LM_ST_UNLOCKED
);
201 gfs2_assert(sdp
, list_empty(&gl
->gl_reclaim
));
202 gfs2_assert(sdp
, list_empty(&gl
->gl_holders
));
203 gfs2_assert(sdp
, list_empty(&gl
->gl_waiters1
));
204 gfs2_assert(sdp
, list_empty(&gl
->gl_waiters2
));
205 gfs2_assert(sdp
, list_empty(&gl
->gl_waiters3
));
210 write_unlock(gl_lock_addr(gl
->gl_hash
));
216 * queue_empty - check to see if a glock's queue is empty
218 * @head: the head of the queue to check
220 * This function protects the list in the event that a process already
221 * has a holder on the list and is adding a second holder for itself.
222 * The glmutex lock is what generally prevents processes from working
223 * on the same glock at once, but the special case of adding a second
224 * holder for yourself ("recursive" locking) doesn't involve locking
225 * glmutex, making the spin lock necessary.
227 * Returns: 1 if the queue is empty
230 static inline int queue_empty(struct gfs2_glock
*gl
, struct list_head
*head
)
233 spin_lock(&gl
->gl_spin
);
234 empty
= list_empty(head
);
235 spin_unlock(&gl
->gl_spin
);
240 * search_bucket() - Find struct gfs2_glock by lock number
241 * @bucket: the bucket to search
242 * @name: The lock name
244 * Returns: NULL, or the struct gfs2_glock with the requested number
247 static struct gfs2_glock
*search_bucket(unsigned int hash
,
248 const struct gfs2_sbd
*sdp
,
249 const struct lm_lockname
*name
)
251 struct gfs2_glock
*gl
;
252 struct hlist_node
*h
;
254 hlist_for_each_entry(gl
, h
, &gl_hash_table
[hash
].hb_list
, gl_list
) {
255 if (!lm_name_equal(&gl
->gl_name
, name
))
257 if (gl
->gl_sbd
!= sdp
)
260 atomic_inc(&gl
->gl_ref
);
269 * gfs2_glock_find() - Find glock by lock number
270 * @sdp: The GFS2 superblock
271 * @name: The lock name
273 * Returns: NULL, or the struct gfs2_glock with the requested number
276 static struct gfs2_glock
*gfs2_glock_find(const struct gfs2_sbd
*sdp
,
277 const struct lm_lockname
*name
)
279 unsigned int hash
= gl_hash(sdp
, name
);
280 struct gfs2_glock
*gl
;
282 read_lock(gl_lock_addr(hash
));
283 gl
= search_bucket(hash
, sdp
, name
);
284 read_unlock(gl_lock_addr(hash
));
290 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
291 * @sdp: The GFS2 superblock
292 * @number: the lock number
293 * @glops: The glock_operations to use
294 * @create: If 0, don't create the glock if it doesn't exist
295 * @glp: the glock is returned here
297 * This does not lock a glock, just finds/creates structures for one.
302 int gfs2_glock_get(struct gfs2_sbd
*sdp
, u64 number
,
303 const struct gfs2_glock_operations
*glops
, int create
,
304 struct gfs2_glock
**glp
)
306 struct lm_lockname name
= { .ln_number
= number
, .ln_type
= glops
->go_type
};
307 struct gfs2_glock
*gl
, *tmp
;
308 unsigned int hash
= gl_hash(sdp
, &name
);
311 read_lock(gl_lock_addr(hash
));
312 gl
= search_bucket(hash
, sdp
, &name
);
313 read_unlock(gl_lock_addr(hash
));
320 gl
= kmem_cache_alloc(gfs2_glock_cachep
, GFP_KERNEL
);
326 atomic_set(&gl
->gl_ref
, 1);
327 gl
->gl_state
= LM_ST_UNLOCKED
;
332 gl
->gl_req_gh
= NULL
;
333 gl
->gl_req_bh
= NULL
;
335 gl
->gl_stamp
= jiffies
;
336 gl
->gl_object
= NULL
;
338 gl
->gl_aspace
= NULL
;
339 lops_init_le(&gl
->gl_le
, &gfs2_glock_lops
);
341 /* If this glock protects actual on-disk data or metadata blocks,
342 create a VFS inode to manage the pages/buffers holding them. */
343 if (glops
== &gfs2_inode_glops
|| glops
== &gfs2_rgrp_glops
) {
344 gl
->gl_aspace
= gfs2_aspace_get(sdp
);
345 if (!gl
->gl_aspace
) {
351 error
= gfs2_lm_get_lock(sdp
, &name
, &gl
->gl_lock
);
355 write_lock(gl_lock_addr(hash
));
356 tmp
= search_bucket(hash
, sdp
, &name
);
358 write_unlock(gl_lock_addr(hash
));
362 hlist_add_head(&gl
->gl_list
, &gl_hash_table
[hash
].hb_list
);
363 write_unlock(gl_lock_addr(hash
));
372 gfs2_aspace_put(gl
->gl_aspace
);
374 kmem_cache_free(gfs2_glock_cachep
, gl
);
379 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
381 * @state: the state we're requesting
382 * @flags: the modifier flags
383 * @gh: the holder structure
387 void gfs2_holder_init(struct gfs2_glock
*gl
, unsigned int state
, unsigned flags
,
388 struct gfs2_holder
*gh
)
390 INIT_LIST_HEAD(&gh
->gh_list
);
392 gh
->gh_ip
= (unsigned long)__builtin_return_address(0);
393 gh
->gh_owner
= current
;
394 gh
->gh_state
= state
;
395 gh
->gh_flags
= flags
;
398 init_completion(&gh
->gh_wait
);
400 if (gh
->gh_state
== LM_ST_EXCLUSIVE
)
401 gh
->gh_flags
|= GL_LOCAL_EXCL
;
407 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
408 * @state: the state we're requesting
409 * @flags: the modifier flags
410 * @gh: the holder structure
412 * Don't mess with the glock.
416 void gfs2_holder_reinit(unsigned int state
, unsigned flags
, struct gfs2_holder
*gh
)
418 gh
->gh_state
= state
;
419 gh
->gh_flags
= flags
;
420 if (gh
->gh_state
== LM_ST_EXCLUSIVE
)
421 gh
->gh_flags
|= GL_LOCAL_EXCL
;
423 gh
->gh_iflags
&= 1 << HIF_ALLOCED
;
424 gh
->gh_ip
= (unsigned long)__builtin_return_address(0);
428 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
429 * @gh: the holder structure
433 void gfs2_holder_uninit(struct gfs2_holder
*gh
)
435 gfs2_glock_put(gh
->gh_gl
);
441 * gfs2_holder_get - get a struct gfs2_holder structure
443 * @state: the state we're requesting
444 * @flags: the modifier flags
447 * Figure out how big an impact this function has. Either:
448 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
449 * 2) Leave it like it is
451 * Returns: the holder structure, NULL on ENOMEM
454 static struct gfs2_holder
*gfs2_holder_get(struct gfs2_glock
*gl
,
456 int flags
, gfp_t gfp_flags
)
458 struct gfs2_holder
*gh
;
460 gh
= kmalloc(sizeof(struct gfs2_holder
), gfp_flags
);
464 gfs2_holder_init(gl
, state
, flags
, gh
);
465 set_bit(HIF_ALLOCED
, &gh
->gh_iflags
);
466 gh
->gh_ip
= (unsigned long)__builtin_return_address(0);
471 * gfs2_holder_put - get rid of a struct gfs2_holder structure
472 * @gh: the holder structure
476 static void gfs2_holder_put(struct gfs2_holder
*gh
)
478 gfs2_holder_uninit(gh
);
483 * rq_mutex - process a mutex request in the queue
484 * @gh: the glock holder
486 * Returns: 1 if the queue is blocked
489 static int rq_mutex(struct gfs2_holder
*gh
)
491 struct gfs2_glock
*gl
= gh
->gh_gl
;
493 list_del_init(&gh
->gh_list
);
494 /* gh->gh_error never examined. */
495 set_bit(GLF_LOCK
, &gl
->gl_flags
);
496 complete(&gh
->gh_wait
);
502 * rq_promote - process a promote request in the queue
503 * @gh: the glock holder
505 * Acquire a new inter-node lock, or change a lock state to more restrictive.
507 * Returns: 1 if the queue is blocked
510 static int rq_promote(struct gfs2_holder
*gh
)
512 struct gfs2_glock
*gl
= gh
->gh_gl
;
513 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
514 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
516 if (!relaxed_state_ok(gl
->gl_state
, gh
->gh_state
, gh
->gh_flags
)) {
517 if (list_empty(&gl
->gl_holders
)) {
519 set_bit(GLF_LOCK
, &gl
->gl_flags
);
520 spin_unlock(&gl
->gl_spin
);
522 if (atomic_read(&sdp
->sd_reclaim_count
) >
523 gfs2_tune_get(sdp
, gt_reclaim_limit
) &&
524 !(gh
->gh_flags
& LM_FLAG_PRIORITY
)) {
525 gfs2_reclaim_glock(sdp
);
526 gfs2_reclaim_glock(sdp
);
529 glops
->go_xmote_th(gl
, gh
->gh_state
, gh
->gh_flags
);
530 spin_lock(&gl
->gl_spin
);
535 if (list_empty(&gl
->gl_holders
)) {
536 set_bit(HIF_FIRST
, &gh
->gh_iflags
);
537 set_bit(GLF_LOCK
, &gl
->gl_flags
);
539 struct gfs2_holder
*next_gh
;
540 if (gh
->gh_flags
& GL_LOCAL_EXCL
)
542 next_gh
= list_entry(gl
->gl_holders
.next
, struct gfs2_holder
,
544 if (next_gh
->gh_flags
& GL_LOCAL_EXCL
)
548 list_move_tail(&gh
->gh_list
, &gl
->gl_holders
);
550 set_bit(HIF_HOLDER
, &gh
->gh_iflags
);
552 complete(&gh
->gh_wait
);
558 * rq_demote - process a demote request in the queue
559 * @gh: the glock holder
561 * Returns: 1 if the queue is blocked
564 static int rq_demote(struct gfs2_holder
*gh
)
566 struct gfs2_glock
*gl
= gh
->gh_gl
;
567 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
569 if (!list_empty(&gl
->gl_holders
))
572 if (gl
->gl_state
== gh
->gh_state
|| gl
->gl_state
== LM_ST_UNLOCKED
) {
573 list_del_init(&gh
->gh_list
);
575 spin_unlock(&gl
->gl_spin
);
576 if (test_bit(HIF_DEALLOC
, &gh
->gh_iflags
))
579 complete(&gh
->gh_wait
);
580 spin_lock(&gl
->gl_spin
);
583 set_bit(GLF_LOCK
, &gl
->gl_flags
);
584 spin_unlock(&gl
->gl_spin
);
586 if (gh
->gh_state
== LM_ST_UNLOCKED
||
587 gl
->gl_state
!= LM_ST_EXCLUSIVE
)
588 glops
->go_drop_th(gl
);
590 glops
->go_xmote_th(gl
, gh
->gh_state
, gh
->gh_flags
);
592 spin_lock(&gl
->gl_spin
);
599 * rq_greedy - process a queued request to drop greedy status
600 * @gh: the glock holder
602 * Returns: 1 if the queue is blocked
605 static int rq_greedy(struct gfs2_holder
*gh
)
607 struct gfs2_glock
*gl
= gh
->gh_gl
;
609 list_del_init(&gh
->gh_list
);
610 /* gh->gh_error never examined. */
611 clear_bit(GLF_GREEDY
, &gl
->gl_flags
);
612 spin_unlock(&gl
->gl_spin
);
614 gfs2_holder_uninit(gh
);
615 kfree(container_of(gh
, struct greedy
, gr_gh
));
617 spin_lock(&gl
->gl_spin
);
623 * run_queue - process holder structures on a glock
627 static void run_queue(struct gfs2_glock
*gl
)
629 struct gfs2_holder
*gh
;
633 if (test_bit(GLF_LOCK
, &gl
->gl_flags
))
636 if (!list_empty(&gl
->gl_waiters1
)) {
637 gh
= list_entry(gl
->gl_waiters1
.next
,
638 struct gfs2_holder
, gh_list
);
640 if (test_bit(HIF_MUTEX
, &gh
->gh_iflags
))
641 blocked
= rq_mutex(gh
);
643 gfs2_assert_warn(gl
->gl_sbd
, 0);
645 } else if (!list_empty(&gl
->gl_waiters2
) &&
646 !test_bit(GLF_SKIP_WAITERS2
, &gl
->gl_flags
)) {
647 gh
= list_entry(gl
->gl_waiters2
.next
,
648 struct gfs2_holder
, gh_list
);
650 if (test_bit(HIF_DEMOTE
, &gh
->gh_iflags
))
651 blocked
= rq_demote(gh
);
652 else if (test_bit(HIF_GREEDY
, &gh
->gh_iflags
))
653 blocked
= rq_greedy(gh
);
655 gfs2_assert_warn(gl
->gl_sbd
, 0);
657 } else if (!list_empty(&gl
->gl_waiters3
)) {
658 gh
= list_entry(gl
->gl_waiters3
.next
,
659 struct gfs2_holder
, gh_list
);
661 if (test_bit(HIF_PROMOTE
, &gh
->gh_iflags
))
662 blocked
= rq_promote(gh
);
664 gfs2_assert_warn(gl
->gl_sbd
, 0);
675 * gfs2_glmutex_lock - acquire a local lock on a glock
678 * Gives caller exclusive access to manipulate a glock structure.
681 static void gfs2_glmutex_lock(struct gfs2_glock
*gl
)
683 struct gfs2_holder gh
;
685 gfs2_holder_init(gl
, 0, 0, &gh
);
686 set_bit(HIF_MUTEX
, &gh
.gh_iflags
);
688 spin_lock(&gl
->gl_spin
);
689 if (test_and_set_bit(GLF_LOCK
, &gl
->gl_flags
)) {
690 list_add_tail(&gh
.gh_list
, &gl
->gl_waiters1
);
692 gl
->gl_owner
= current
;
693 gl
->gl_ip
= (unsigned long)__builtin_return_address(0);
694 complete(&gh
.gh_wait
);
696 spin_unlock(&gl
->gl_spin
);
698 wait_for_completion(&gh
.gh_wait
);
699 gfs2_holder_uninit(&gh
);
703 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
706 * Returns: 1 if the glock is acquired
709 static int gfs2_glmutex_trylock(struct gfs2_glock
*gl
)
713 spin_lock(&gl
->gl_spin
);
714 if (test_and_set_bit(GLF_LOCK
, &gl
->gl_flags
)) {
717 gl
->gl_owner
= current
;
718 gl
->gl_ip
= (unsigned long)__builtin_return_address(0);
720 spin_unlock(&gl
->gl_spin
);
726 * gfs2_glmutex_unlock - release a local lock on a glock
731 static void gfs2_glmutex_unlock(struct gfs2_glock
*gl
)
733 spin_lock(&gl
->gl_spin
);
734 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
738 BUG_ON(!spin_is_locked(&gl
->gl_spin
));
739 spin_unlock(&gl
->gl_spin
);
743 * handle_callback - add a demote request to a lock's queue
745 * @state: the state the caller wants us to change to
747 * Note: This may fail sliently if we are out of memory.
750 static void handle_callback(struct gfs2_glock
*gl
, unsigned int state
)
752 struct gfs2_holder
*gh
, *new_gh
= NULL
;
755 spin_lock(&gl
->gl_spin
);
757 list_for_each_entry(gh
, &gl
->gl_waiters2
, gh_list
) {
758 if (test_bit(HIF_DEMOTE
, &gh
->gh_iflags
) &&
759 gl
->gl_req_gh
!= gh
) {
760 if (gh
->gh_state
!= state
)
761 gh
->gh_state
= LM_ST_UNLOCKED
;
767 list_add_tail(&new_gh
->gh_list
, &gl
->gl_waiters2
);
770 spin_unlock(&gl
->gl_spin
);
772 new_gh
= gfs2_holder_get(gl
, state
, LM_FLAG_TRY
, GFP_NOFS
);
775 set_bit(HIF_DEMOTE
, &new_gh
->gh_iflags
);
776 set_bit(HIF_DEALLOC
, &new_gh
->gh_iflags
);
782 spin_unlock(&gl
->gl_spin
);
785 gfs2_holder_put(new_gh
);
788 void gfs2_glock_inode_squish(struct inode
*inode
)
790 struct gfs2_holder gh
;
791 struct gfs2_glock
*gl
= GFS2_I(inode
)->i_gl
;
792 gfs2_holder_init(gl
, LM_ST_UNLOCKED
, 0, &gh
);
793 set_bit(HIF_DEMOTE
, &gh
.gh_iflags
);
794 spin_lock(&gl
->gl_spin
);
795 gfs2_assert(inode
->i_sb
->s_fs_info
, list_empty(&gl
->gl_holders
));
796 list_add_tail(&gh
.gh_list
, &gl
->gl_waiters2
);
798 spin_unlock(&gl
->gl_spin
);
799 wait_for_completion(&gh
.gh_wait
);
800 gfs2_holder_uninit(&gh
);
804 * state_change - record that the glock is now in a different state
806 * @new_state the new state
810 static void state_change(struct gfs2_glock
*gl
, unsigned int new_state
)
814 held1
= (gl
->gl_state
!= LM_ST_UNLOCKED
);
815 held2
= (new_state
!= LM_ST_UNLOCKED
);
817 if (held1
!= held2
) {
824 gl
->gl_state
= new_state
;
828 * xmote_bh - Called after the lock module is done acquiring a lock
829 * @gl: The glock in question
830 * @ret: the int returned from the lock module
834 static void xmote_bh(struct gfs2_glock
*gl
, unsigned int ret
)
836 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
837 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
838 struct gfs2_holder
*gh
= gl
->gl_req_gh
;
839 int prev_state
= gl
->gl_state
;
842 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
843 gfs2_assert_warn(sdp
, queue_empty(gl
, &gl
->gl_holders
));
844 gfs2_assert_warn(sdp
, !(ret
& LM_OUT_ASYNC
));
846 state_change(gl
, ret
& LM_OUT_ST_MASK
);
848 if (prev_state
!= LM_ST_UNLOCKED
&& !(ret
& LM_OUT_CACHEABLE
)) {
850 glops
->go_inval(gl
, DIO_METADATA
);
851 } else if (gl
->gl_state
== LM_ST_DEFERRED
) {
852 /* We might not want to do this here.
853 Look at moving to the inode glops. */
855 glops
->go_inval(gl
, 0);
858 /* Deal with each possible exit condition */
861 gl
->gl_stamp
= jiffies
;
862 else if (unlikely(test_bit(SDF_SHUTDOWN
, &sdp
->sd_flags
))) {
863 spin_lock(&gl
->gl_spin
);
864 list_del_init(&gh
->gh_list
);
866 spin_unlock(&gl
->gl_spin
);
867 } else if (test_bit(HIF_DEMOTE
, &gh
->gh_iflags
)) {
868 spin_lock(&gl
->gl_spin
);
869 list_del_init(&gh
->gh_list
);
870 if (gl
->gl_state
== gh
->gh_state
||
871 gl
->gl_state
== LM_ST_UNLOCKED
) {
874 if (gfs2_assert_warn(sdp
, gh
->gh_flags
&
875 (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
)) == -1)
876 fs_warn(sdp
, "ret = 0x%.8X\n", ret
);
877 gh
->gh_error
= GLR_TRYFAILED
;
879 spin_unlock(&gl
->gl_spin
);
881 if (ret
& LM_OUT_CANCELED
)
882 handle_callback(gl
, LM_ST_UNLOCKED
);
884 } else if (ret
& LM_OUT_CANCELED
) {
885 spin_lock(&gl
->gl_spin
);
886 list_del_init(&gh
->gh_list
);
887 gh
->gh_error
= GLR_CANCELED
;
888 spin_unlock(&gl
->gl_spin
);
890 } else if (relaxed_state_ok(gl
->gl_state
, gh
->gh_state
, gh
->gh_flags
)) {
891 spin_lock(&gl
->gl_spin
);
892 list_move_tail(&gh
->gh_list
, &gl
->gl_holders
);
894 set_bit(HIF_HOLDER
, &gh
->gh_iflags
);
895 spin_unlock(&gl
->gl_spin
);
897 set_bit(HIF_FIRST
, &gh
->gh_iflags
);
901 } else if (gh
->gh_flags
& (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
)) {
902 spin_lock(&gl
->gl_spin
);
903 list_del_init(&gh
->gh_list
);
904 gh
->gh_error
= GLR_TRYFAILED
;
905 spin_unlock(&gl
->gl_spin
);
908 if (gfs2_assert_withdraw(sdp
, 0) == -1)
909 fs_err(sdp
, "ret = 0x%.8X\n", ret
);
912 if (glops
->go_xmote_bh
)
913 glops
->go_xmote_bh(gl
);
916 spin_lock(&gl
->gl_spin
);
917 gl
->gl_req_gh
= NULL
;
918 gl
->gl_req_bh
= NULL
;
919 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
921 spin_unlock(&gl
->gl_spin
);
927 if (test_bit(HIF_DEALLOC
, &gh
->gh_iflags
))
930 complete(&gh
->gh_wait
);
935 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
936 * @gl: The glock in question
937 * @state: the requested state
938 * @flags: modifier flags to the lock call
942 void gfs2_glock_xmote_th(struct gfs2_glock
*gl
, unsigned int state
, int flags
)
944 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
945 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
946 int lck_flags
= flags
& (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
|
947 LM_FLAG_NOEXP
| LM_FLAG_ANY
|
949 unsigned int lck_ret
;
951 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
952 gfs2_assert_warn(sdp
, queue_empty(gl
, &gl
->gl_holders
));
953 gfs2_assert_warn(sdp
, state
!= LM_ST_UNLOCKED
);
954 gfs2_assert_warn(sdp
, state
!= gl
->gl_state
);
956 if (gl
->gl_state
== LM_ST_EXCLUSIVE
&& glops
->go_sync
)
960 gl
->gl_req_bh
= xmote_bh
;
962 lck_ret
= gfs2_lm_lock(sdp
, gl
->gl_lock
, gl
->gl_state
, state
, lck_flags
);
964 if (gfs2_assert_withdraw(sdp
, !(lck_ret
& LM_OUT_ERROR
)))
967 if (lck_ret
& LM_OUT_ASYNC
)
968 gfs2_assert_warn(sdp
, lck_ret
== LM_OUT_ASYNC
);
970 xmote_bh(gl
, lck_ret
);
974 * drop_bh - Called after a lock module unlock completes
976 * @ret: the return status
978 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
979 * Doesn't drop the reference on the glock the top half took out
983 static void drop_bh(struct gfs2_glock
*gl
, unsigned int ret
)
985 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
986 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
987 struct gfs2_holder
*gh
= gl
->gl_req_gh
;
989 clear_bit(GLF_PREFETCH
, &gl
->gl_flags
);
991 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
992 gfs2_assert_warn(sdp
, queue_empty(gl
, &gl
->gl_holders
));
993 gfs2_assert_warn(sdp
, !ret
);
995 state_change(gl
, LM_ST_UNLOCKED
);
998 glops
->go_inval(gl
, DIO_METADATA
);
1001 spin_lock(&gl
->gl_spin
);
1002 list_del_init(&gh
->gh_list
);
1004 spin_unlock(&gl
->gl_spin
);
1007 if (glops
->go_drop_bh
)
1008 glops
->go_drop_bh(gl
);
1010 spin_lock(&gl
->gl_spin
);
1011 gl
->gl_req_gh
= NULL
;
1012 gl
->gl_req_bh
= NULL
;
1013 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
1015 spin_unlock(&gl
->gl_spin
);
1020 if (test_bit(HIF_DEALLOC
, &gh
->gh_iflags
))
1021 gfs2_holder_put(gh
);
1023 complete(&gh
->gh_wait
);
1028 * gfs2_glock_drop_th - call into the lock module to unlock a lock
1033 void gfs2_glock_drop_th(struct gfs2_glock
*gl
)
1035 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1036 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1039 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
1040 gfs2_assert_warn(sdp
, queue_empty(gl
, &gl
->gl_holders
));
1041 gfs2_assert_warn(sdp
, gl
->gl_state
!= LM_ST_UNLOCKED
);
1043 if (gl
->gl_state
== LM_ST_EXCLUSIVE
&& glops
->go_sync
)
1046 gfs2_glock_hold(gl
);
1047 gl
->gl_req_bh
= drop_bh
;
1049 ret
= gfs2_lm_unlock(sdp
, gl
->gl_lock
, gl
->gl_state
);
1051 if (gfs2_assert_withdraw(sdp
, !(ret
& LM_OUT_ERROR
)))
1057 gfs2_assert_warn(sdp
, ret
== LM_OUT_ASYNC
);
1061 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1062 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1064 * Don't cancel GL_NOCANCEL requests.
1067 static void do_cancels(struct gfs2_holder
*gh
)
1069 struct gfs2_glock
*gl
= gh
->gh_gl
;
1071 spin_lock(&gl
->gl_spin
);
1073 while (gl
->gl_req_gh
!= gh
&&
1074 !test_bit(HIF_HOLDER
, &gh
->gh_iflags
) &&
1075 !list_empty(&gh
->gh_list
)) {
1076 if (gl
->gl_req_bh
&& !(gl
->gl_req_gh
&&
1077 (gl
->gl_req_gh
->gh_flags
& GL_NOCANCEL
))) {
1078 spin_unlock(&gl
->gl_spin
);
1079 gfs2_lm_cancel(gl
->gl_sbd
, gl
->gl_lock
);
1081 spin_lock(&gl
->gl_spin
);
1083 spin_unlock(&gl
->gl_spin
);
1085 spin_lock(&gl
->gl_spin
);
1089 spin_unlock(&gl
->gl_spin
);
1093 * glock_wait_internal - wait on a glock acquisition
1094 * @gh: the glock holder
1096 * Returns: 0 on success
1099 static int glock_wait_internal(struct gfs2_holder
*gh
)
1101 struct gfs2_glock
*gl
= gh
->gh_gl
;
1102 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1103 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1105 if (test_bit(HIF_ABORTED
, &gh
->gh_iflags
))
1108 if (gh
->gh_flags
& (LM_FLAG_TRY
| LM_FLAG_TRY_1CB
)) {
1109 spin_lock(&gl
->gl_spin
);
1110 if (gl
->gl_req_gh
!= gh
&&
1111 !test_bit(HIF_HOLDER
, &gh
->gh_iflags
) &&
1112 !list_empty(&gh
->gh_list
)) {
1113 list_del_init(&gh
->gh_list
);
1114 gh
->gh_error
= GLR_TRYFAILED
;
1116 spin_unlock(&gl
->gl_spin
);
1117 return gh
->gh_error
;
1119 spin_unlock(&gl
->gl_spin
);
1122 if (gh
->gh_flags
& LM_FLAG_PRIORITY
)
1125 wait_for_completion(&gh
->gh_wait
);
1128 return gh
->gh_error
;
1130 gfs2_assert_withdraw(sdp
, test_bit(HIF_HOLDER
, &gh
->gh_iflags
));
1131 gfs2_assert_withdraw(sdp
, relaxed_state_ok(gl
->gl_state
, gh
->gh_state
,
1134 if (test_bit(HIF_FIRST
, &gh
->gh_iflags
)) {
1135 gfs2_assert_warn(sdp
, test_bit(GLF_LOCK
, &gl
->gl_flags
));
1137 if (glops
->go_lock
) {
1138 gh
->gh_error
= glops
->go_lock(gh
);
1140 spin_lock(&gl
->gl_spin
);
1141 list_del_init(&gh
->gh_list
);
1142 spin_unlock(&gl
->gl_spin
);
1146 spin_lock(&gl
->gl_spin
);
1147 gl
->gl_req_gh
= NULL
;
1148 gl
->gl_req_bh
= NULL
;
1149 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
1151 spin_unlock(&gl
->gl_spin
);
1154 return gh
->gh_error
;
1157 static inline struct gfs2_holder
*
1158 find_holder_by_owner(struct list_head
*head
, struct task_struct
*owner
)
1160 struct gfs2_holder
*gh
;
1162 list_for_each_entry(gh
, head
, gh_list
) {
1163 if (gh
->gh_owner
== owner
)
1171 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1172 * @gh: the holder structure to add
1176 static void add_to_queue(struct gfs2_holder
*gh
)
1178 struct gfs2_glock
*gl
= gh
->gh_gl
;
1179 struct gfs2_holder
*existing
;
1181 BUG_ON(!gh
->gh_owner
);
1183 existing
= find_holder_by_owner(&gl
->gl_holders
, gh
->gh_owner
);
1185 print_symbol(KERN_WARNING
"original: %s\n", existing
->gh_ip
);
1186 printk(KERN_INFO
"pid : %d\n", existing
->gh_owner
->pid
);
1187 printk(KERN_INFO
"lock type : %d lock state : %d\n",
1188 existing
->gh_gl
->gl_name
.ln_type
, existing
->gh_gl
->gl_state
);
1189 print_symbol(KERN_WARNING
"new: %s\n", gh
->gh_ip
);
1190 printk(KERN_INFO
"pid : %d\n", gh
->gh_owner
->pid
);
1191 printk(KERN_INFO
"lock type : %d lock state : %d\n",
1192 gl
->gl_name
.ln_type
, gl
->gl_state
);
1196 existing
= find_holder_by_owner(&gl
->gl_waiters3
, gh
->gh_owner
);
1198 print_symbol(KERN_WARNING
"original: %s\n", existing
->gh_ip
);
1199 print_symbol(KERN_WARNING
"new: %s\n", gh
->gh_ip
);
1203 if (gh
->gh_flags
& LM_FLAG_PRIORITY
)
1204 list_add(&gh
->gh_list
, &gl
->gl_waiters3
);
1206 list_add_tail(&gh
->gh_list
, &gl
->gl_waiters3
);
1210 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1211 * @gh: the holder structure
1213 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1215 * Returns: 0, GLR_TRYFAILED, or errno on failure
1218 int gfs2_glock_nq(struct gfs2_holder
*gh
)
1220 struct gfs2_glock
*gl
= gh
->gh_gl
;
1221 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1225 if (unlikely(test_bit(SDF_SHUTDOWN
, &sdp
->sd_flags
))) {
1226 set_bit(HIF_ABORTED
, &gh
->gh_iflags
);
1230 set_bit(HIF_PROMOTE
, &gh
->gh_iflags
);
1232 spin_lock(&gl
->gl_spin
);
1235 spin_unlock(&gl
->gl_spin
);
1237 if (!(gh
->gh_flags
& GL_ASYNC
)) {
1238 error
= glock_wait_internal(gh
);
1239 if (error
== GLR_CANCELED
) {
1245 clear_bit(GLF_PREFETCH
, &gl
->gl_flags
);
1251 * gfs2_glock_poll - poll to see if an async request has been completed
1254 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1257 int gfs2_glock_poll(struct gfs2_holder
*gh
)
1259 struct gfs2_glock
*gl
= gh
->gh_gl
;
1262 spin_lock(&gl
->gl_spin
);
1264 if (test_bit(HIF_HOLDER
, &gh
->gh_iflags
))
1266 else if (list_empty(&gh
->gh_list
)) {
1267 if (gh
->gh_error
== GLR_CANCELED
) {
1268 spin_unlock(&gl
->gl_spin
);
1270 if (gfs2_glock_nq(gh
))
1277 spin_unlock(&gl
->gl_spin
);
1283 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1284 * @gh: the holder structure
1286 * Returns: 0, GLR_TRYFAILED, or errno on failure
1289 int gfs2_glock_wait(struct gfs2_holder
*gh
)
1293 error
= glock_wait_internal(gh
);
1294 if (error
== GLR_CANCELED
) {
1296 gh
->gh_flags
&= ~GL_ASYNC
;
1297 error
= gfs2_glock_nq(gh
);
1304 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1305 * @gh: the glock holder
1309 void gfs2_glock_dq(struct gfs2_holder
*gh
)
1311 struct gfs2_glock
*gl
= gh
->gh_gl
;
1312 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1314 if (gh
->gh_flags
& GL_NOCACHE
)
1315 handle_callback(gl
, LM_ST_UNLOCKED
);
1317 gfs2_glmutex_lock(gl
);
1319 spin_lock(&gl
->gl_spin
);
1320 list_del_init(&gh
->gh_list
);
1322 if (list_empty(&gl
->gl_holders
)) {
1323 spin_unlock(&gl
->gl_spin
);
1325 if (glops
->go_unlock
)
1326 glops
->go_unlock(gh
);
1328 gl
->gl_stamp
= jiffies
;
1330 spin_lock(&gl
->gl_spin
);
1333 clear_bit(GLF_LOCK
, &gl
->gl_flags
);
1335 spin_unlock(&gl
->gl_spin
);
1339 * gfs2_glock_prefetch - Try to prefetch a glock
1341 * @state: the state to prefetch in
1342 * @flags: flags passed to go_xmote_th()
1346 static void gfs2_glock_prefetch(struct gfs2_glock
*gl
, unsigned int state
,
1349 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1351 spin_lock(&gl
->gl_spin
);
1353 if (test_bit(GLF_LOCK
, &gl
->gl_flags
) || !list_empty(&gl
->gl_holders
) ||
1354 !list_empty(&gl
->gl_waiters1
) || !list_empty(&gl
->gl_waiters2
) ||
1355 !list_empty(&gl
->gl_waiters3
) ||
1356 relaxed_state_ok(gl
->gl_state
, state
, flags
)) {
1357 spin_unlock(&gl
->gl_spin
);
1361 set_bit(GLF_PREFETCH
, &gl
->gl_flags
);
1362 set_bit(GLF_LOCK
, &gl
->gl_flags
);
1363 spin_unlock(&gl
->gl_spin
);
1365 glops
->go_xmote_th(gl
, state
, flags
);
1368 static void greedy_work(void *data
)
1370 struct greedy
*gr
= data
;
1371 struct gfs2_holder
*gh
= &gr
->gr_gh
;
1372 struct gfs2_glock
*gl
= gh
->gh_gl
;
1373 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1375 clear_bit(GLF_SKIP_WAITERS2
, &gl
->gl_flags
);
1377 if (glops
->go_greedy
)
1378 glops
->go_greedy(gl
);
1380 spin_lock(&gl
->gl_spin
);
1382 if (list_empty(&gl
->gl_waiters2
)) {
1383 clear_bit(GLF_GREEDY
, &gl
->gl_flags
);
1384 spin_unlock(&gl
->gl_spin
);
1385 gfs2_holder_uninit(gh
);
1388 gfs2_glock_hold(gl
);
1389 list_add_tail(&gh
->gh_list
, &gl
->gl_waiters2
);
1391 spin_unlock(&gl
->gl_spin
);
1397 * gfs2_glock_be_greedy -
1401 * Returns: 0 if go_greedy will be called, 1 otherwise
1404 int gfs2_glock_be_greedy(struct gfs2_glock
*gl
, unsigned int time
)
1407 struct gfs2_holder
*gh
;
1409 if (!time
|| gl
->gl_sbd
->sd_args
.ar_localcaching
||
1410 test_and_set_bit(GLF_GREEDY
, &gl
->gl_flags
))
1413 gr
= kmalloc(sizeof(struct greedy
), GFP_KERNEL
);
1415 clear_bit(GLF_GREEDY
, &gl
->gl_flags
);
1420 gfs2_holder_init(gl
, 0, 0, gh
);
1421 set_bit(HIF_GREEDY
, &gh
->gh_iflags
);
1422 INIT_WORK(&gr
->gr_work
, greedy_work
, gr
);
1424 set_bit(GLF_SKIP_WAITERS2
, &gl
->gl_flags
);
1425 schedule_delayed_work(&gr
->gr_work
, time
);
1431 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1432 * @gh: the holder structure
1436 void gfs2_glock_dq_uninit(struct gfs2_holder
*gh
)
1439 gfs2_holder_uninit(gh
);
1443 * gfs2_glock_nq_num - acquire a glock based on lock number
1444 * @sdp: the filesystem
1445 * @number: the lock number
1446 * @glops: the glock operations for the type of glock
1447 * @state: the state to acquire the glock in
1448 * @flags: modifier flags for the aquisition
1449 * @gh: the struct gfs2_holder
1454 int gfs2_glock_nq_num(struct gfs2_sbd
*sdp
, u64 number
,
1455 const struct gfs2_glock_operations
*glops
,
1456 unsigned int state
, int flags
, struct gfs2_holder
*gh
)
1458 struct gfs2_glock
*gl
;
1461 error
= gfs2_glock_get(sdp
, number
, glops
, CREATE
, &gl
);
1463 error
= gfs2_glock_nq_init(gl
, state
, flags
, gh
);
1471 * glock_compare - Compare two struct gfs2_glock structures for sorting
1472 * @arg_a: the first structure
1473 * @arg_b: the second structure
1477 static int glock_compare(const void *arg_a
, const void *arg_b
)
1479 const struct gfs2_holder
*gh_a
= *(const struct gfs2_holder
**)arg_a
;
1480 const struct gfs2_holder
*gh_b
= *(const struct gfs2_holder
**)arg_b
;
1481 const struct lm_lockname
*a
= &gh_a
->gh_gl
->gl_name
;
1482 const struct lm_lockname
*b
= &gh_b
->gh_gl
->gl_name
;
1484 if (a
->ln_number
> b
->ln_number
)
1486 if (a
->ln_number
< b
->ln_number
)
1488 if (gh_a
->gh_state
== LM_ST_SHARED
&& gh_b
->gh_state
== LM_ST_EXCLUSIVE
)
1490 if (!(gh_a
->gh_flags
& GL_LOCAL_EXCL
) && (gh_b
->gh_flags
& GL_LOCAL_EXCL
))
1496 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1497 * @num_gh: the number of structures
1498 * @ghs: an array of struct gfs2_holder structures
1500 * Returns: 0 on success (all glocks acquired),
1501 * errno on failure (no glocks acquired)
1504 static int nq_m_sync(unsigned int num_gh
, struct gfs2_holder
*ghs
,
1505 struct gfs2_holder
**p
)
1510 for (x
= 0; x
< num_gh
; x
++)
1513 sort(p
, num_gh
, sizeof(struct gfs2_holder
*), glock_compare
, NULL
);
1515 for (x
= 0; x
< num_gh
; x
++) {
1516 p
[x
]->gh_flags
&= ~(LM_FLAG_TRY
| GL_ASYNC
);
1518 error
= gfs2_glock_nq(p
[x
]);
1521 gfs2_glock_dq(p
[x
]);
1530 * gfs2_glock_nq_m - acquire multiple glocks
1531 * @num_gh: the number of structures
1532 * @ghs: an array of struct gfs2_holder structures
1534 * Figure out how big an impact this function has. Either:
1535 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1536 * 2) Forget async stuff and just call nq_m_sync()
1537 * 3) Leave it like it is
1539 * Returns: 0 on success (all glocks acquired),
1540 * errno on failure (no glocks acquired)
1543 int gfs2_glock_nq_m(unsigned int num_gh
, struct gfs2_holder
*ghs
)
1547 int borked
= 0, serious
= 0;
1554 ghs
->gh_flags
&= ~(LM_FLAG_TRY
| GL_ASYNC
);
1555 return gfs2_glock_nq(ghs
);
1558 e
= kcalloc(num_gh
, sizeof(struct gfs2_holder
*), GFP_KERNEL
);
1562 for (x
= 0; x
< num_gh
; x
++) {
1563 ghs
[x
].gh_flags
|= LM_FLAG_TRY
| GL_ASYNC
;
1564 error
= gfs2_glock_nq(&ghs
[x
]);
1573 for (x
= 0; x
< num_gh
; x
++) {
1574 error
= e
[x
] = glock_wait_internal(&ghs
[x
]);
1577 if (error
!= GLR_TRYFAILED
&& error
!= GLR_CANCELED
)
1587 for (x
= 0; x
< num_gh
; x
++)
1589 gfs2_glock_dq(&ghs
[x
]);
1594 for (x
= 0; x
< num_gh
; x
++)
1595 gfs2_holder_reinit(ghs
[x
].gh_state
, ghs
[x
].gh_flags
,
1597 error
= nq_m_sync(num_gh
, ghs
, (struct gfs2_holder
**)e
);
1606 * gfs2_glock_dq_m - release multiple glocks
1607 * @num_gh: the number of structures
1608 * @ghs: an array of struct gfs2_holder structures
1612 void gfs2_glock_dq_m(unsigned int num_gh
, struct gfs2_holder
*ghs
)
1616 for (x
= 0; x
< num_gh
; x
++)
1617 gfs2_glock_dq(&ghs
[x
]);
1621 * gfs2_glock_dq_uninit_m - release multiple glocks
1622 * @num_gh: the number of structures
1623 * @ghs: an array of struct gfs2_holder structures
1627 void gfs2_glock_dq_uninit_m(unsigned int num_gh
, struct gfs2_holder
*ghs
)
1631 for (x
= 0; x
< num_gh
; x
++)
1632 gfs2_glock_dq_uninit(&ghs
[x
]);
1636 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1637 * @sdp: the filesystem
1638 * @number: the lock number
1639 * @glops: the glock operations for the type of glock
1640 * @state: the state to acquire the glock in
1641 * @flags: modifier flags for the aquisition
1646 void gfs2_glock_prefetch_num(struct gfs2_sbd
*sdp
, u64 number
,
1647 const struct gfs2_glock_operations
*glops
,
1648 unsigned int state
, int flags
)
1650 struct gfs2_glock
*gl
;
1653 if (atomic_read(&sdp
->sd_reclaim_count
) <
1654 gfs2_tune_get(sdp
, gt_reclaim_limit
)) {
1655 error
= gfs2_glock_get(sdp
, number
, glops
, CREATE
, &gl
);
1657 gfs2_glock_prefetch(gl
, state
, flags
);
1664 * gfs2_lvb_hold - attach a LVB from a glock
1665 * @gl: The glock in question
1669 int gfs2_lvb_hold(struct gfs2_glock
*gl
)
1673 gfs2_glmutex_lock(gl
);
1675 if (!atomic_read(&gl
->gl_lvb_count
)) {
1676 error
= gfs2_lm_hold_lvb(gl
->gl_sbd
, gl
->gl_lock
, &gl
->gl_lvb
);
1678 gfs2_glmutex_unlock(gl
);
1681 gfs2_glock_hold(gl
);
1683 atomic_inc(&gl
->gl_lvb_count
);
1685 gfs2_glmutex_unlock(gl
);
1691 * gfs2_lvb_unhold - detach a LVB from a glock
1692 * @gl: The glock in question
1696 void gfs2_lvb_unhold(struct gfs2_glock
*gl
)
1698 gfs2_glock_hold(gl
);
1699 gfs2_glmutex_lock(gl
);
1701 gfs2_assert(gl
->gl_sbd
, atomic_read(&gl
->gl_lvb_count
) > 0);
1702 if (atomic_dec_and_test(&gl
->gl_lvb_count
)) {
1703 gfs2_lm_unhold_lvb(gl
->gl_sbd
, gl
->gl_lock
, gl
->gl_lvb
);
1708 gfs2_glmutex_unlock(gl
);
1712 static void blocking_cb(struct gfs2_sbd
*sdp
, struct lm_lockname
*name
,
1715 struct gfs2_glock
*gl
;
1717 gl
= gfs2_glock_find(sdp
, name
);
1721 if (gl
->gl_ops
->go_callback
)
1722 gl
->gl_ops
->go_callback(gl
, state
);
1723 handle_callback(gl
, state
);
1725 spin_lock(&gl
->gl_spin
);
1727 spin_unlock(&gl
->gl_spin
);
1733 * gfs2_glock_cb - Callback used by locking module
1734 * @sdp: Pointer to the superblock
1735 * @type: Type of callback
1736 * @data: Type dependent data pointer
1738 * Called by the locking module when it wants to tell us something.
1739 * Either we need to drop a lock, one of our ASYNC requests completed, or
1740 * a journal from another client needs to be recovered.
1743 void gfs2_glock_cb(void *cb_data
, unsigned int type
, void *data
)
1745 struct gfs2_sbd
*sdp
= cb_data
;
1749 blocking_cb(sdp
, data
, LM_ST_UNLOCKED
);
1753 blocking_cb(sdp
, data
, LM_ST_DEFERRED
);
1757 blocking_cb(sdp
, data
, LM_ST_SHARED
);
1761 struct lm_async_cb
*async
= data
;
1762 struct gfs2_glock
*gl
;
1764 gl
= gfs2_glock_find(sdp
, &async
->lc_name
);
1765 if (gfs2_assert_warn(sdp
, gl
))
1767 if (!gfs2_assert_warn(sdp
, gl
->gl_req_bh
))
1768 gl
->gl_req_bh(gl
, async
->lc_ret
);
1773 case LM_CB_NEED_RECOVERY
:
1774 gfs2_jdesc_make_dirty(sdp
, *(unsigned int *)data
);
1775 if (sdp
->sd_recoverd_process
)
1776 wake_up_process(sdp
->sd_recoverd_process
);
1779 case LM_CB_DROPLOCKS
:
1780 gfs2_gl_hash_clear(sdp
, NO_WAIT
);
1781 gfs2_quota_scan(sdp
);
1785 gfs2_assert_warn(sdp
, 0);
1791 * demote_ok - Check to see if it's ok to unlock a glock
1794 * Returns: 1 if it's ok
1797 static int demote_ok(struct gfs2_glock
*gl
)
1799 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1800 const struct gfs2_glock_operations
*glops
= gl
->gl_ops
;
1803 if (test_bit(GLF_STICKY
, &gl
->gl_flags
))
1805 else if (test_bit(GLF_PREFETCH
, &gl
->gl_flags
))
1806 demote
= time_after_eq(jiffies
, gl
->gl_stamp
+
1807 gfs2_tune_get(sdp
, gt_prefetch_secs
) * HZ
);
1808 else if (glops
->go_demote_ok
)
1809 demote
= glops
->go_demote_ok(gl
);
1815 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1820 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock
*gl
)
1822 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1824 spin_lock(&sdp
->sd_reclaim_lock
);
1825 if (list_empty(&gl
->gl_reclaim
)) {
1826 gfs2_glock_hold(gl
);
1827 list_add(&gl
->gl_reclaim
, &sdp
->sd_reclaim_list
);
1828 atomic_inc(&sdp
->sd_reclaim_count
);
1830 spin_unlock(&sdp
->sd_reclaim_lock
);
1832 wake_up(&sdp
->sd_reclaim_wq
);
1836 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1837 * @sdp: the filesystem
1839 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1840 * different glock and we notice that there are a lot of glocks in the
1845 void gfs2_reclaim_glock(struct gfs2_sbd
*sdp
)
1847 struct gfs2_glock
*gl
;
1849 spin_lock(&sdp
->sd_reclaim_lock
);
1850 if (list_empty(&sdp
->sd_reclaim_list
)) {
1851 spin_unlock(&sdp
->sd_reclaim_lock
);
1854 gl
= list_entry(sdp
->sd_reclaim_list
.next
,
1855 struct gfs2_glock
, gl_reclaim
);
1856 list_del_init(&gl
->gl_reclaim
);
1857 spin_unlock(&sdp
->sd_reclaim_lock
);
1859 atomic_dec(&sdp
->sd_reclaim_count
);
1860 atomic_inc(&sdp
->sd_reclaimed
);
1862 if (gfs2_glmutex_trylock(gl
)) {
1863 if (queue_empty(gl
, &gl
->gl_holders
) &&
1864 gl
->gl_state
!= LM_ST_UNLOCKED
&& demote_ok(gl
))
1865 handle_callback(gl
, LM_ST_UNLOCKED
);
1866 gfs2_glmutex_unlock(gl
);
1873 * examine_bucket - Call a function for glock in a hash bucket
1874 * @examiner: the function
1875 * @sdp: the filesystem
1876 * @bucket: the bucket
1878 * Returns: 1 if the bucket has entries
1881 static int examine_bucket(glock_examiner examiner
, struct gfs2_sbd
*sdp
,
1884 struct gfs2_glock
*gl
, *prev
= NULL
;
1885 int has_entries
= 0;
1886 struct hlist_head
*head
= &gl_hash_table
[hash
].hb_list
;
1888 read_lock(gl_lock_addr(hash
));
1889 /* Can't use hlist_for_each_entry - don't want prefetch here */
1890 if (hlist_empty(head
))
1892 gl
= list_entry(head
->first
, struct gfs2_glock
, gl_list
);
1894 if (gl
->gl_sbd
== sdp
) {
1895 gfs2_glock_hold(gl
);
1896 read_unlock(gl_lock_addr(hash
));
1898 gfs2_glock_put(prev
);
1902 read_lock(gl_lock_addr(hash
));
1904 if (gl
->gl_list
.next
== NULL
)
1906 gl
= list_entry(gl
->gl_list
.next
, struct gfs2_glock
, gl_list
);
1909 read_unlock(gl_lock_addr(hash
));
1911 gfs2_glock_put(prev
);
1916 * scan_glock - look at a glock and see if we can reclaim it
1917 * @gl: the glock to look at
1921 static void scan_glock(struct gfs2_glock
*gl
)
1923 if (gl
->gl_ops
== &gfs2_inode_glops
)
1926 if (gfs2_glmutex_trylock(gl
)) {
1927 if (queue_empty(gl
, &gl
->gl_holders
) &&
1928 gl
->gl_state
!= LM_ST_UNLOCKED
&& demote_ok(gl
))
1930 gfs2_glmutex_unlock(gl
);
1935 gfs2_glmutex_unlock(gl
);
1936 gfs2_glock_schedule_for_reclaim(gl
);
1940 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
1941 * @sdp: the filesystem
1945 void gfs2_scand_internal(struct gfs2_sbd
*sdp
)
1949 for (x
= 0; x
< GFS2_GL_HASH_SIZE
; x
++)
1950 examine_bucket(scan_glock
, sdp
, x
);
1954 * clear_glock - look at a glock and see if we can free it from glock cache
1955 * @gl: the glock to look at
1959 static void clear_glock(struct gfs2_glock
*gl
)
1961 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
1964 spin_lock(&sdp
->sd_reclaim_lock
);
1965 if (!list_empty(&gl
->gl_reclaim
)) {
1966 list_del_init(&gl
->gl_reclaim
);
1967 atomic_dec(&sdp
->sd_reclaim_count
);
1968 spin_unlock(&sdp
->sd_reclaim_lock
);
1969 released
= gfs2_glock_put(gl
);
1970 gfs2_assert(sdp
, !released
);
1972 spin_unlock(&sdp
->sd_reclaim_lock
);
1975 if (gfs2_glmutex_trylock(gl
)) {
1976 if (queue_empty(gl
, &gl
->gl_holders
) &&
1977 gl
->gl_state
!= LM_ST_UNLOCKED
)
1978 handle_callback(gl
, LM_ST_UNLOCKED
);
1979 gfs2_glmutex_unlock(gl
);
1984 * gfs2_gl_hash_clear - Empty out the glock hash table
1985 * @sdp: the filesystem
1986 * @wait: wait until it's all gone
1988 * Called when unmounting the filesystem, or when inter-node lock manager
1989 * requests DROPLOCKS because it is running out of capacity.
1992 void gfs2_gl_hash_clear(struct gfs2_sbd
*sdp
, int wait
)
2002 for (x
= 0; x
< GFS2_GL_HASH_SIZE
; x
++) {
2003 if (examine_bucket(clear_glock
, sdp
, x
))
2010 if (time_after_eq(jiffies
,
2011 t
+ gfs2_tune_get(sdp
, gt_stall_secs
) * HZ
)) {
2012 fs_warn(sdp
, "Unmount seems to be stalled. "
2013 "Dumping lock state...\n");
2014 gfs2_dump_lockstate(sdp
);
2018 invalidate_inodes(sdp
->sd_vfs
);
2024 * Diagnostic routines to help debug distributed deadlock
2028 * dump_holder - print information about a glock holder
2029 * @str: a string naming the type of holder
2030 * @gh: the glock holder
2032 * Returns: 0 on success, -ENOBUFS when we run out of space
2035 static int dump_holder(char *str
, struct gfs2_holder
*gh
)
2038 int error
= -ENOBUFS
;
2040 printk(KERN_INFO
" %s\n", str
);
2041 printk(KERN_INFO
" owner = %ld\n",
2042 (gh
->gh_owner
) ? (long)gh
->gh_owner
->pid
: -1);
2043 printk(KERN_INFO
" gh_state = %u\n", gh
->gh_state
);
2044 printk(KERN_INFO
" gh_flags =");
2045 for (x
= 0; x
< 32; x
++)
2046 if (gh
->gh_flags
& (1 << x
))
2049 printk(KERN_INFO
" error = %d\n", gh
->gh_error
);
2050 printk(KERN_INFO
" gh_iflags =");
2051 for (x
= 0; x
< 32; x
++)
2052 if (test_bit(x
, &gh
->gh_iflags
))
2055 print_symbol(KERN_INFO
" initialized at: %s\n", gh
->gh_ip
);
2063 * dump_inode - print information about an inode
2066 * Returns: 0 on success, -ENOBUFS when we run out of space
2069 static int dump_inode(struct gfs2_inode
*ip
)
2072 int error
= -ENOBUFS
;
2074 printk(KERN_INFO
" Inode:\n");
2075 printk(KERN_INFO
" num = %llu %llu\n",
2076 (unsigned long long)ip
->i_num
.no_formal_ino
,
2077 (unsigned long long)ip
->i_num
.no_addr
);
2078 printk(KERN_INFO
" type = %u\n", IF2DT(ip
->i_inode
.i_mode
));
2079 printk(KERN_INFO
" i_flags =");
2080 for (x
= 0; x
< 32; x
++)
2081 if (test_bit(x
, &ip
->i_flags
))
2091 * dump_glock - print information about a glock
2093 * @count: where we are in the buffer
2095 * Returns: 0 on success, -ENOBUFS when we run out of space
2098 static int dump_glock(struct gfs2_glock
*gl
)
2100 struct gfs2_holder
*gh
;
2102 int error
= -ENOBUFS
;
2104 spin_lock(&gl
->gl_spin
);
2106 printk(KERN_INFO
"Glock 0x%p (%u, %llu)\n", gl
, gl
->gl_name
.ln_type
,
2107 (unsigned long long)gl
->gl_name
.ln_number
);
2108 printk(KERN_INFO
" gl_flags =");
2109 for (x
= 0; x
< 32; x
++) {
2110 if (test_bit(x
, &gl
->gl_flags
))
2114 printk(KERN_INFO
" gl_ref = %d\n", atomic_read(&gl
->gl_ref
));
2115 printk(KERN_INFO
" gl_state = %u\n", gl
->gl_state
);
2116 printk(KERN_INFO
" gl_owner = %s\n", gl
->gl_owner
->comm
);
2117 print_symbol(KERN_INFO
" gl_ip = %s\n", gl
->gl_ip
);
2118 printk(KERN_INFO
" req_gh = %s\n", (gl
->gl_req_gh
) ? "yes" : "no");
2119 printk(KERN_INFO
" req_bh = %s\n", (gl
->gl_req_bh
) ? "yes" : "no");
2120 printk(KERN_INFO
" lvb_count = %d\n", atomic_read(&gl
->gl_lvb_count
));
2121 printk(KERN_INFO
" object = %s\n", (gl
->gl_object
) ? "yes" : "no");
2122 printk(KERN_INFO
" le = %s\n",
2123 (list_empty(&gl
->gl_le
.le_list
)) ? "no" : "yes");
2124 printk(KERN_INFO
" reclaim = %s\n",
2125 (list_empty(&gl
->gl_reclaim
)) ? "no" : "yes");
2127 printk(KERN_INFO
" aspace = 0x%p nrpages = %lu\n", gl
->gl_aspace
,
2128 gl
->gl_aspace
->i_mapping
->nrpages
);
2130 printk(KERN_INFO
" aspace = no\n");
2131 printk(KERN_INFO
" ail = %d\n", atomic_read(&gl
->gl_ail_count
));
2132 if (gl
->gl_req_gh
) {
2133 error
= dump_holder("Request", gl
->gl_req_gh
);
2137 list_for_each_entry(gh
, &gl
->gl_holders
, gh_list
) {
2138 error
= dump_holder("Holder", gh
);
2142 list_for_each_entry(gh
, &gl
->gl_waiters1
, gh_list
) {
2143 error
= dump_holder("Waiter1", gh
);
2147 list_for_each_entry(gh
, &gl
->gl_waiters2
, gh_list
) {
2148 error
= dump_holder("Waiter2", gh
);
2152 list_for_each_entry(gh
, &gl
->gl_waiters3
, gh_list
) {
2153 error
= dump_holder("Waiter3", gh
);
2157 if (gl
->gl_ops
== &gfs2_inode_glops
&& gl
->gl_object
) {
2158 if (!test_bit(GLF_LOCK
, &gl
->gl_flags
) &&
2159 list_empty(&gl
->gl_holders
)) {
2160 error
= dump_inode(gl
->gl_object
);
2165 printk(KERN_INFO
" Inode: busy\n");
2172 spin_unlock(&gl
->gl_spin
);
2177 * gfs2_dump_lockstate - print out the current lockstate
2178 * @sdp: the filesystem
2179 * @ub: the buffer to copy the information into
2181 * If @ub is NULL, dump the lockstate to the console.
2185 static int gfs2_dump_lockstate(struct gfs2_sbd
*sdp
)
2187 struct gfs2_glock
*gl
;
2188 struct hlist_node
*h
;
2192 for (x
= 0; x
< GFS2_GL_HASH_SIZE
; x
++) {
2194 read_lock(gl_lock_addr(x
));
2196 hlist_for_each_entry(gl
, h
, &gl_hash_table
[x
].hb_list
, gl_list
) {
2197 if (gl
->gl_sbd
!= sdp
)
2200 error
= dump_glock(gl
);
2205 read_unlock(gl_lock_addr(x
));
2215 int __init
gfs2_glock_init(void)
2218 for(i
= 0; i
< GFS2_GL_HASH_SIZE
; i
++) {
2219 INIT_HLIST_HEAD(&gl_hash_table
[i
].hb_list
);
2221 #ifdef GL_HASH_LOCK_SZ
2222 for(i
= 0; i
< GL_HASH_LOCK_SZ
; i
++) {
2223 rwlock_init(&gl_hash_locks
[i
]);