2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/delay.h>
16 #include <linux/sort.h>
17 #include <linux/jhash.h>
18 #include <linux/kref.h>
19 #include <linux/kallsyms.h>
20 #include <linux/gfs2_ondisk.h>
21 #include <linux/list.h>
22 #include <asm/uaccess.h>
25 #include "lm_interface.h"
38 struct gfs2_holder gr_gh;
39 struct work_struct gr_work;
42 struct gfs2_gl_hash_bucket {
43 struct hlist_head hb_list;
46 typedef void (*glock_examiner) (struct gfs2_glock * gl);
48 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
49 static int dump_glock(struct gfs2_glock *gl);
50 static int dump_inode(struct gfs2_inode *ip);
52 #define GFS2_GL_HASH_SHIFT 15
53 #define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT)
54 #define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1)
56 static struct gfs2_gl_hash_bucket gl_hash_table[GFS2_GL_HASH_SIZE];
59 * Despite what you might think, the numbers below are not arbitrary :-)
60 * They are taken from the ipv4 routing hash code, which is well tested
61 * and thus should be nearly optimal. Later on we might tweek the numbers
62 * but for now this should be fine.
64 * The reason for putting the locks in a separate array from the list heads
65 * is that we can have fewer locks than list heads and save memory. We use
66 * the same hash function for both, but with a different hash mask.
68 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
69 defined(CONFIG_PROVE_LOCKING)
72 # define GL_HASH_LOCK_SZ 256
75 # define GL_HASH_LOCK_SZ 4096
77 # define GL_HASH_LOCK_SZ 2048
79 # define GL_HASH_LOCK_SZ 1024
81 # define GL_HASH_LOCK_SZ 512
83 # define GL_HASH_LOCK_SZ 256
87 /* We never want more locks than chains */
88 #if GFS2_GL_HASH_SIZE < GL_HASH_LOCK_SZ
89 # undef GL_HASH_LOCK_SZ
90 # define GL_HASH_LOCK_SZ GFS2_GL_HASH_SIZE
93 static rwlock_t gl_hash_locks[GL_HASH_LOCK_SZ];
95 static inline rwlock_t *gl_lock_addr(unsigned int x)
97 return &gl_hash_locks[x & (GL_HASH_LOCK_SZ-1)];
99 #else /* not SMP, so no spinlocks required */
100 static inline rwlock_t *gl_lock_addr(x)
107 * relaxed_state_ok - is a requested lock compatible with the current lock mode?
108 * @actual: the current state of the lock
109 * @requested: the lock state that was requested by the caller
110 * @flags: the modifier flags passed in by the caller
112 * Returns: 1 if the locks are compatible, 0 otherwise
115 static inline int relaxed_state_ok(unsigned int actual, unsigned requested,
118 if (actual == requested)
121 if (flags & GL_EXACT)
124 if (actual == LM_ST_EXCLUSIVE && requested == LM_ST_SHARED)
127 if (actual != LM_ST_UNLOCKED && (flags & LM_FLAG_ANY))
134 * gl_hash() - Turn glock number into hash bucket number
135 * @lock: The glock number
137 * Returns: The number of the corresponding hash bucket
140 static unsigned int gl_hash(const struct gfs2_sbd *sdp,
141 const struct lm_lockname *name)
145 h = jhash(&name->ln_number, sizeof(u64), 0);
146 h = jhash(&name->ln_type, sizeof(unsigned int), h);
147 h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
148 h &= GFS2_GL_HASH_MASK;
154 * glock_free() - Perform a few checks and then release struct gfs2_glock
155 * @gl: The glock to release
157 * Also calls lock module to release its internal structure for this glock.
161 static void glock_free(struct gfs2_glock *gl)
163 struct gfs2_sbd *sdp = gl->gl_sbd;
164 struct inode *aspace = gl->gl_aspace;
166 gfs2_lm_put_lock(sdp, gl->gl_lock);
169 gfs2_aspace_put(aspace);
171 kmem_cache_free(gfs2_glock_cachep, gl);
175 * gfs2_glock_hold() - increment reference count on glock
176 * @gl: The glock to hold
180 void gfs2_glock_hold(struct gfs2_glock *gl)
182 kref_get(&gl->gl_ref);
185 /* All work is done after the return from kref_put() so we
186 can release the write_lock before the free. */
188 static void kill_glock(struct kref *kref)
190 struct gfs2_glock *gl = container_of(kref, struct gfs2_glock, gl_ref);
191 struct gfs2_sbd *sdp = gl->gl_sbd;
193 gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED);
194 gfs2_assert(sdp, list_empty(&gl->gl_reclaim));
195 gfs2_assert(sdp, list_empty(&gl->gl_holders));
196 gfs2_assert(sdp, list_empty(&gl->gl_waiters1));
197 gfs2_assert(sdp, list_empty(&gl->gl_waiters2));
198 gfs2_assert(sdp, list_empty(&gl->gl_waiters3));
202 * gfs2_glock_put() - Decrement reference count on glock
203 * @gl: The glock to put
207 int gfs2_glock_put(struct gfs2_glock *gl)
211 write_lock(gl_lock_addr(gl->gl_hash));
212 if (kref_put(&gl->gl_ref, kill_glock)) {
213 hlist_del(&gl->gl_list);
214 write_unlock(gl_lock_addr(gl->gl_hash));
215 BUG_ON(spin_is_locked(&gl->gl_spin));
220 write_unlock(gl_lock_addr(gl->gl_hash));
226 * queue_empty - check to see if a glock's queue is empty
228 * @head: the head of the queue to check
230 * This function protects the list in the event that a process already
231 * has a holder on the list and is adding a second holder for itself.
232 * The glmutex lock is what generally prevents processes from working
233 * on the same glock at once, but the special case of adding a second
234 * holder for yourself ("recursive" locking) doesn't involve locking
235 * glmutex, making the spin lock necessary.
237 * Returns: 1 if the queue is empty
240 static inline int queue_empty(struct gfs2_glock *gl, struct list_head *head)
243 spin_lock(&gl->gl_spin);
244 empty = list_empty(head);
245 spin_unlock(&gl->gl_spin);
250 * search_bucket() - Find struct gfs2_glock by lock number
251 * @bucket: the bucket to search
252 * @name: The lock name
254 * Returns: NULL, or the struct gfs2_glock with the requested number
257 static struct gfs2_glock *search_bucket(unsigned int hash,
258 const struct gfs2_sbd *sdp,
259 const struct lm_lockname *name)
261 struct gfs2_glock *gl;
262 struct hlist_node *h;
264 hlist_for_each_entry(gl, h, &gl_hash_table[hash].hb_list, gl_list) {
265 if (!lm_name_equal(&gl->gl_name, name))
267 if (gl->gl_sbd != sdp)
270 kref_get(&gl->gl_ref);
279 * gfs2_glock_find() - Find glock by lock number
280 * @sdp: The GFS2 superblock
281 * @name: The lock name
283 * Returns: NULL, or the struct gfs2_glock with the requested number
286 static struct gfs2_glock *gfs2_glock_find(const struct gfs2_sbd *sdp,
287 const struct lm_lockname *name)
289 unsigned int hash = gl_hash(sdp, name);
290 struct gfs2_glock *gl;
292 read_lock(gl_lock_addr(hash));
293 gl = search_bucket(hash, sdp, name);
294 read_unlock(gl_lock_addr(hash));
300 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
301 * @sdp: The GFS2 superblock
302 * @number: the lock number
303 * @glops: The glock_operations to use
304 * @create: If 0, don't create the glock if it doesn't exist
305 * @glp: the glock is returned here
307 * This does not lock a glock, just finds/creates structures for one.
312 int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
313 const struct gfs2_glock_operations *glops, int create,
314 struct gfs2_glock **glp)
316 struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
317 struct gfs2_glock *gl, *tmp;
318 unsigned int hash = gl_hash(sdp, &name);
321 read_lock(gl_lock_addr(hash));
322 gl = search_bucket(hash, sdp, &name);
323 read_unlock(gl_lock_addr(hash));
330 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
336 kref_init(&gl->gl_ref);
337 gl->gl_state = LM_ST_UNLOCKED;
342 gl->gl_req_gh = NULL;
343 gl->gl_req_bh = NULL;
345 gl->gl_stamp = jiffies;
346 gl->gl_object = NULL;
348 gl->gl_aspace = NULL;
349 lops_init_le(&gl->gl_le, &gfs2_glock_lops);
351 /* If this glock protects actual on-disk data or metadata blocks,
352 create a VFS inode to manage the pages/buffers holding them. */
353 if (glops == &gfs2_inode_glops || glops == &gfs2_rgrp_glops) {
354 gl->gl_aspace = gfs2_aspace_get(sdp);
355 if (!gl->gl_aspace) {
361 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
365 write_lock(gl_lock_addr(hash));
366 tmp = search_bucket(hash, sdp, &name);
368 write_unlock(gl_lock_addr(hash));
372 hlist_add_head(&gl->gl_list, &gl_hash_table[hash].hb_list);
373 write_unlock(gl_lock_addr(hash));
382 gfs2_aspace_put(gl->gl_aspace);
384 kmem_cache_free(gfs2_glock_cachep, gl);
389 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
391 * @state: the state we're requesting
392 * @flags: the modifier flags
393 * @gh: the holder structure
397 void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
398 struct gfs2_holder *gh)
400 INIT_LIST_HEAD(&gh->gh_list);
402 gh->gh_ip = (unsigned long)__builtin_return_address(0);
403 gh->gh_owner = current;
404 gh->gh_state = state;
405 gh->gh_flags = flags;
408 init_completion(&gh->gh_wait);
410 if (gh->gh_state == LM_ST_EXCLUSIVE)
411 gh->gh_flags |= GL_LOCAL_EXCL;
417 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
418 * @state: the state we're requesting
419 * @flags: the modifier flags
420 * @gh: the holder structure
422 * Don't mess with the glock.
426 void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
428 gh->gh_state = state;
429 gh->gh_flags = flags;
430 if (gh->gh_state == LM_ST_EXCLUSIVE)
431 gh->gh_flags |= GL_LOCAL_EXCL;
433 gh->gh_iflags &= 1 << HIF_ALLOCED;
434 gh->gh_ip = (unsigned long)__builtin_return_address(0);
438 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
439 * @gh: the holder structure
443 void gfs2_holder_uninit(struct gfs2_holder *gh)
445 gfs2_glock_put(gh->gh_gl);
451 * gfs2_holder_get - get a struct gfs2_holder structure
453 * @state: the state we're requesting
454 * @flags: the modifier flags
457 * Figure out how big an impact this function has. Either:
458 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
459 * 2) Leave it like it is
461 * Returns: the holder structure, NULL on ENOMEM
464 static struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl,
466 int flags, gfp_t gfp_flags)
468 struct gfs2_holder *gh;
470 gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags);
474 gfs2_holder_init(gl, state, flags, gh);
475 set_bit(HIF_ALLOCED, &gh->gh_iflags);
476 gh->gh_ip = (unsigned long)__builtin_return_address(0);
481 * gfs2_holder_put - get rid of a struct gfs2_holder structure
482 * @gh: the holder structure
486 static void gfs2_holder_put(struct gfs2_holder *gh)
488 gfs2_holder_uninit(gh);
493 * rq_mutex - process a mutex request in the queue
494 * @gh: the glock holder
496 * Returns: 1 if the queue is blocked
499 static int rq_mutex(struct gfs2_holder *gh)
501 struct gfs2_glock *gl = gh->gh_gl;
503 list_del_init(&gh->gh_list);
504 /* gh->gh_error never examined. */
505 set_bit(GLF_LOCK, &gl->gl_flags);
506 complete(&gh->gh_wait);
512 * rq_promote - process a promote request in the queue
513 * @gh: the glock holder
515 * Acquire a new inter-node lock, or change a lock state to more restrictive.
517 * Returns: 1 if the queue is blocked
520 static int rq_promote(struct gfs2_holder *gh)
522 struct gfs2_glock *gl = gh->gh_gl;
523 struct gfs2_sbd *sdp = gl->gl_sbd;
524 const struct gfs2_glock_operations *glops = gl->gl_ops;
526 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
527 if (list_empty(&gl->gl_holders)) {
529 set_bit(GLF_LOCK, &gl->gl_flags);
530 spin_unlock(&gl->gl_spin);
532 if (atomic_read(&sdp->sd_reclaim_count) >
533 gfs2_tune_get(sdp, gt_reclaim_limit) &&
534 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
535 gfs2_reclaim_glock(sdp);
536 gfs2_reclaim_glock(sdp);
539 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
540 spin_lock(&gl->gl_spin);
545 if (list_empty(&gl->gl_holders)) {
546 set_bit(HIF_FIRST, &gh->gh_iflags);
547 set_bit(GLF_LOCK, &gl->gl_flags);
549 struct gfs2_holder *next_gh;
550 if (gh->gh_flags & GL_LOCAL_EXCL)
552 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
554 if (next_gh->gh_flags & GL_LOCAL_EXCL)
558 list_move_tail(&gh->gh_list, &gl->gl_holders);
560 set_bit(HIF_HOLDER, &gh->gh_iflags);
562 complete(&gh->gh_wait);
568 * rq_demote - process a demote request in the queue
569 * @gh: the glock holder
571 * Returns: 1 if the queue is blocked
574 static int rq_demote(struct gfs2_holder *gh)
576 struct gfs2_glock *gl = gh->gh_gl;
577 const struct gfs2_glock_operations *glops = gl->gl_ops;
579 if (!list_empty(&gl->gl_holders))
582 if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) {
583 list_del_init(&gh->gh_list);
585 spin_unlock(&gl->gl_spin);
586 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
589 complete(&gh->gh_wait);
590 spin_lock(&gl->gl_spin);
593 set_bit(GLF_LOCK, &gl->gl_flags);
594 spin_unlock(&gl->gl_spin);
596 if (gh->gh_state == LM_ST_UNLOCKED ||
597 gl->gl_state != LM_ST_EXCLUSIVE)
598 glops->go_drop_th(gl);
600 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
602 spin_lock(&gl->gl_spin);
609 * rq_greedy - process a queued request to drop greedy status
610 * @gh: the glock holder
612 * Returns: 1 if the queue is blocked
615 static int rq_greedy(struct gfs2_holder *gh)
617 struct gfs2_glock *gl = gh->gh_gl;
619 list_del_init(&gh->gh_list);
620 /* gh->gh_error never examined. */
621 clear_bit(GLF_GREEDY, &gl->gl_flags);
622 spin_unlock(&gl->gl_spin);
624 gfs2_holder_uninit(gh);
625 kfree(container_of(gh, struct greedy, gr_gh));
627 spin_lock(&gl->gl_spin);
633 * run_queue - process holder structures on a glock
637 static void run_queue(struct gfs2_glock *gl)
639 struct gfs2_holder *gh;
643 if (test_bit(GLF_LOCK, &gl->gl_flags))
646 if (!list_empty(&gl->gl_waiters1)) {
647 gh = list_entry(gl->gl_waiters1.next,
648 struct gfs2_holder, gh_list);
650 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
651 blocked = rq_mutex(gh);
653 gfs2_assert_warn(gl->gl_sbd, 0);
655 } else if (!list_empty(&gl->gl_waiters2) &&
656 !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) {
657 gh = list_entry(gl->gl_waiters2.next,
658 struct gfs2_holder, gh_list);
660 if (test_bit(HIF_DEMOTE, &gh->gh_iflags))
661 blocked = rq_demote(gh);
662 else if (test_bit(HIF_GREEDY, &gh->gh_iflags))
663 blocked = rq_greedy(gh);
665 gfs2_assert_warn(gl->gl_sbd, 0);
667 } else if (!list_empty(&gl->gl_waiters3)) {
668 gh = list_entry(gl->gl_waiters3.next,
669 struct gfs2_holder, gh_list);
671 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
672 blocked = rq_promote(gh);
674 gfs2_assert_warn(gl->gl_sbd, 0);
685 * gfs2_glmutex_lock - acquire a local lock on a glock
688 * Gives caller exclusive access to manipulate a glock structure.
691 static void gfs2_glmutex_lock(struct gfs2_glock *gl)
693 struct gfs2_holder gh;
695 gfs2_holder_init(gl, 0, 0, &gh);
696 set_bit(HIF_MUTEX, &gh.gh_iflags);
698 spin_lock(&gl->gl_spin);
699 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
700 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
702 gl->gl_owner = current;
703 gl->gl_ip = (unsigned long)__builtin_return_address(0);
704 complete(&gh.gh_wait);
706 spin_unlock(&gl->gl_spin);
708 wait_for_completion(&gh.gh_wait);
709 gfs2_holder_uninit(&gh);
713 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
716 * Returns: 1 if the glock is acquired
719 static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
723 spin_lock(&gl->gl_spin);
724 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
727 gl->gl_owner = current;
728 gl->gl_ip = (unsigned long)__builtin_return_address(0);
730 spin_unlock(&gl->gl_spin);
736 * gfs2_glmutex_unlock - release a local lock on a glock
741 static void gfs2_glmutex_unlock(struct gfs2_glock *gl)
743 spin_lock(&gl->gl_spin);
744 clear_bit(GLF_LOCK, &gl->gl_flags);
748 BUG_ON(!spin_is_locked(&gl->gl_spin));
749 spin_unlock(&gl->gl_spin);
753 * handle_callback - add a demote request to a lock's queue
755 * @state: the state the caller wants us to change to
757 * Note: This may fail sliently if we are out of memory.
760 static void handle_callback(struct gfs2_glock *gl, unsigned int state)
762 struct gfs2_holder *gh, *new_gh = NULL;
765 spin_lock(&gl->gl_spin);
767 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
768 if (test_bit(HIF_DEMOTE, &gh->gh_iflags) &&
769 gl->gl_req_gh != gh) {
770 if (gh->gh_state != state)
771 gh->gh_state = LM_ST_UNLOCKED;
777 list_add_tail(&new_gh->gh_list, &gl->gl_waiters2);
780 spin_unlock(&gl->gl_spin);
782 new_gh = gfs2_holder_get(gl, state, LM_FLAG_TRY, GFP_KERNEL);
785 set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
786 set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
792 spin_unlock(&gl->gl_spin);
795 gfs2_holder_put(new_gh);
798 void gfs2_glock_inode_squish(struct inode *inode)
800 struct gfs2_holder gh;
801 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
802 gfs2_holder_init(gl, LM_ST_UNLOCKED, 0, &gh);
803 set_bit(HIF_DEMOTE, &gh.gh_iflags);
804 spin_lock(&gl->gl_spin);
805 gfs2_assert(inode->i_sb->s_fs_info, list_empty(&gl->gl_holders));
806 list_add_tail(&gh.gh_list, &gl->gl_waiters2);
808 spin_unlock(&gl->gl_spin);
809 wait_for_completion(&gh.gh_wait);
810 gfs2_holder_uninit(&gh);
814 * state_change - record that the glock is now in a different state
816 * @new_state the new state
820 static void state_change(struct gfs2_glock *gl, unsigned int new_state)
824 held1 = (gl->gl_state != LM_ST_UNLOCKED);
825 held2 = (new_state != LM_ST_UNLOCKED);
827 if (held1 != held2) {
834 gl->gl_state = new_state;
838 * xmote_bh - Called after the lock module is done acquiring a lock
839 * @gl: The glock in question
840 * @ret: the int returned from the lock module
844 static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
846 struct gfs2_sbd *sdp = gl->gl_sbd;
847 const struct gfs2_glock_operations *glops = gl->gl_ops;
848 struct gfs2_holder *gh = gl->gl_req_gh;
849 int prev_state = gl->gl_state;
852 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
853 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
854 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
856 state_change(gl, ret & LM_OUT_ST_MASK);
858 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
860 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
861 } else if (gl->gl_state == LM_ST_DEFERRED) {
862 /* We might not want to do this here.
863 Look at moving to the inode glops. */
865 glops->go_inval(gl, DIO_DATA);
868 /* Deal with each possible exit condition */
871 gl->gl_stamp = jiffies;
872 else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
873 spin_lock(&gl->gl_spin);
874 list_del_init(&gh->gh_list);
876 spin_unlock(&gl->gl_spin);
877 } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
878 spin_lock(&gl->gl_spin);
879 list_del_init(&gh->gh_list);
880 if (gl->gl_state == gh->gh_state ||
881 gl->gl_state == LM_ST_UNLOCKED) {
884 if (gfs2_assert_warn(sdp, gh->gh_flags &
885 (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
886 fs_warn(sdp, "ret = 0x%.8X\n", ret);
887 gh->gh_error = GLR_TRYFAILED;
889 spin_unlock(&gl->gl_spin);
891 if (ret & LM_OUT_CANCELED)
892 handle_callback(gl, LM_ST_UNLOCKED);
894 } else if (ret & LM_OUT_CANCELED) {
895 spin_lock(&gl->gl_spin);
896 list_del_init(&gh->gh_list);
897 gh->gh_error = GLR_CANCELED;
898 spin_unlock(&gl->gl_spin);
900 } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
901 spin_lock(&gl->gl_spin);
902 list_move_tail(&gh->gh_list, &gl->gl_holders);
904 set_bit(HIF_HOLDER, &gh->gh_iflags);
905 spin_unlock(&gl->gl_spin);
907 set_bit(HIF_FIRST, &gh->gh_iflags);
911 } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
912 spin_lock(&gl->gl_spin);
913 list_del_init(&gh->gh_list);
914 gh->gh_error = GLR_TRYFAILED;
915 spin_unlock(&gl->gl_spin);
918 if (gfs2_assert_withdraw(sdp, 0) == -1)
919 fs_err(sdp, "ret = 0x%.8X\n", ret);
922 if (glops->go_xmote_bh)
923 glops->go_xmote_bh(gl);
926 spin_lock(&gl->gl_spin);
927 gl->gl_req_gh = NULL;
928 gl->gl_req_bh = NULL;
929 clear_bit(GLF_LOCK, &gl->gl_flags);
931 spin_unlock(&gl->gl_spin);
937 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
940 complete(&gh->gh_wait);
945 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
946 * @gl: The glock in question
947 * @state: the requested state
948 * @flags: modifier flags to the lock call
952 void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags)
954 struct gfs2_sbd *sdp = gl->gl_sbd;
955 const struct gfs2_glock_operations *glops = gl->gl_ops;
956 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
957 LM_FLAG_NOEXP | LM_FLAG_ANY |
959 unsigned int lck_ret;
961 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
962 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
963 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
964 gfs2_assert_warn(sdp, state != gl->gl_state);
966 if (gl->gl_state == LM_ST_EXCLUSIVE && glops->go_sync)
967 glops->go_sync(gl, DIO_METADATA | DIO_DATA | DIO_RELEASE);
970 gl->gl_req_bh = xmote_bh;
972 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state, lck_flags);
974 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
977 if (lck_ret & LM_OUT_ASYNC)
978 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
980 xmote_bh(gl, lck_ret);
984 * drop_bh - Called after a lock module unlock completes
986 * @ret: the return status
988 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
989 * Doesn't drop the reference on the glock the top half took out
993 static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
995 struct gfs2_sbd *sdp = gl->gl_sbd;
996 const struct gfs2_glock_operations *glops = gl->gl_ops;
997 struct gfs2_holder *gh = gl->gl_req_gh;
999 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1001 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1002 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
1003 gfs2_assert_warn(sdp, !ret);
1005 state_change(gl, LM_ST_UNLOCKED);
1007 if (glops->go_inval)
1008 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
1011 spin_lock(&gl->gl_spin);
1012 list_del_init(&gh->gh_list);
1014 spin_unlock(&gl->gl_spin);
1017 if (glops->go_drop_bh)
1018 glops->go_drop_bh(gl);
1020 spin_lock(&gl->gl_spin);
1021 gl->gl_req_gh = NULL;
1022 gl->gl_req_bh = NULL;
1023 clear_bit(GLF_LOCK, &gl->gl_flags);
1025 spin_unlock(&gl->gl_spin);
1030 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
1031 gfs2_holder_put(gh);
1033 complete(&gh->gh_wait);
1038 * gfs2_glock_drop_th - call into the lock module to unlock a lock
1043 void gfs2_glock_drop_th(struct gfs2_glock *gl)
1045 struct gfs2_sbd *sdp = gl->gl_sbd;
1046 const struct gfs2_glock_operations *glops = gl->gl_ops;
1049 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1050 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
1051 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
1053 if (gl->gl_state == LM_ST_EXCLUSIVE && glops->go_sync)
1054 glops->go_sync(gl, DIO_METADATA | DIO_DATA | DIO_RELEASE);
1056 gfs2_glock_hold(gl);
1057 gl->gl_req_bh = drop_bh;
1059 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
1061 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
1067 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
1071 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1072 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1074 * Don't cancel GL_NOCANCEL requests.
1077 static void do_cancels(struct gfs2_holder *gh)
1079 struct gfs2_glock *gl = gh->gh_gl;
1081 spin_lock(&gl->gl_spin);
1083 while (gl->gl_req_gh != gh &&
1084 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1085 !list_empty(&gh->gh_list)) {
1086 if (gl->gl_req_bh && !(gl->gl_req_gh &&
1087 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
1088 spin_unlock(&gl->gl_spin);
1089 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
1091 spin_lock(&gl->gl_spin);
1093 spin_unlock(&gl->gl_spin);
1095 spin_lock(&gl->gl_spin);
1099 spin_unlock(&gl->gl_spin);
1103 * glock_wait_internal - wait on a glock acquisition
1104 * @gh: the glock holder
1106 * Returns: 0 on success
1109 static int glock_wait_internal(struct gfs2_holder *gh)
1111 struct gfs2_glock *gl = gh->gh_gl;
1112 struct gfs2_sbd *sdp = gl->gl_sbd;
1113 const struct gfs2_glock_operations *glops = gl->gl_ops;
1115 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1118 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1119 spin_lock(&gl->gl_spin);
1120 if (gl->gl_req_gh != gh &&
1121 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1122 !list_empty(&gh->gh_list)) {
1123 list_del_init(&gh->gh_list);
1124 gh->gh_error = GLR_TRYFAILED;
1126 spin_unlock(&gl->gl_spin);
1127 return gh->gh_error;
1129 spin_unlock(&gl->gl_spin);
1132 if (gh->gh_flags & LM_FLAG_PRIORITY)
1135 wait_for_completion(&gh->gh_wait);
1138 return gh->gh_error;
1140 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
1141 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state, gh->gh_state,
1144 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1145 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1147 if (glops->go_lock) {
1148 gh->gh_error = glops->go_lock(gh);
1150 spin_lock(&gl->gl_spin);
1151 list_del_init(&gh->gh_list);
1152 spin_unlock(&gl->gl_spin);
1156 spin_lock(&gl->gl_spin);
1157 gl->gl_req_gh = NULL;
1158 gl->gl_req_bh = NULL;
1159 clear_bit(GLF_LOCK, &gl->gl_flags);
1161 spin_unlock(&gl->gl_spin);
1164 return gh->gh_error;
1167 static inline struct gfs2_holder *
1168 find_holder_by_owner(struct list_head *head, struct task_struct *owner)
1170 struct gfs2_holder *gh;
1172 list_for_each_entry(gh, head, gh_list) {
1173 if (gh->gh_owner == owner)
1181 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1182 * @gh: the holder structure to add
1186 static void add_to_queue(struct gfs2_holder *gh)
1188 struct gfs2_glock *gl = gh->gh_gl;
1189 struct gfs2_holder *existing;
1191 BUG_ON(!gh->gh_owner);
1193 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
1195 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1196 printk(KERN_INFO "pid : %d\n", existing->gh_owner->pid);
1197 printk(KERN_INFO "lock type : %d lock state : %d\n",
1198 existing->gh_gl->gl_name.ln_type, existing->gh_gl->gl_state);
1199 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1200 printk(KERN_INFO "pid : %d\n", gh->gh_owner->pid);
1201 printk(KERN_INFO "lock type : %d lock state : %d\n",
1202 gl->gl_name.ln_type, gl->gl_state);
1206 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
1208 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1209 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1213 if (gh->gh_flags & LM_FLAG_PRIORITY)
1214 list_add(&gh->gh_list, &gl->gl_waiters3);
1216 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1220 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1221 * @gh: the holder structure
1223 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1225 * Returns: 0, GLR_TRYFAILED, or errno on failure
1228 int gfs2_glock_nq(struct gfs2_holder *gh)
1230 struct gfs2_glock *gl = gh->gh_gl;
1231 struct gfs2_sbd *sdp = gl->gl_sbd;
1235 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1236 set_bit(HIF_ABORTED, &gh->gh_iflags);
1240 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1242 spin_lock(&gl->gl_spin);
1245 spin_unlock(&gl->gl_spin);
1247 if (!(gh->gh_flags & GL_ASYNC)) {
1248 error = glock_wait_internal(gh);
1249 if (error == GLR_CANCELED) {
1255 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1257 if (error == GLR_TRYFAILED && (gh->gh_flags & GL_DUMP))
1264 * gfs2_glock_poll - poll to see if an async request has been completed
1267 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1270 int gfs2_glock_poll(struct gfs2_holder *gh)
1272 struct gfs2_glock *gl = gh->gh_gl;
1275 spin_lock(&gl->gl_spin);
1277 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1279 else if (list_empty(&gh->gh_list)) {
1280 if (gh->gh_error == GLR_CANCELED) {
1281 spin_unlock(&gl->gl_spin);
1283 if (gfs2_glock_nq(gh))
1290 spin_unlock(&gl->gl_spin);
1296 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1297 * @gh: the holder structure
1299 * Returns: 0, GLR_TRYFAILED, or errno on failure
1302 int gfs2_glock_wait(struct gfs2_holder *gh)
1306 error = glock_wait_internal(gh);
1307 if (error == GLR_CANCELED) {
1309 gh->gh_flags &= ~GL_ASYNC;
1310 error = gfs2_glock_nq(gh);
1317 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1318 * @gh: the glock holder
1322 void gfs2_glock_dq(struct gfs2_holder *gh)
1324 struct gfs2_glock *gl = gh->gh_gl;
1325 const struct gfs2_glock_operations *glops = gl->gl_ops;
1327 if (gh->gh_flags & GL_NOCACHE)
1328 handle_callback(gl, LM_ST_UNLOCKED);
1330 gfs2_glmutex_lock(gl);
1332 spin_lock(&gl->gl_spin);
1333 list_del_init(&gh->gh_list);
1335 if (list_empty(&gl->gl_holders)) {
1336 spin_unlock(&gl->gl_spin);
1338 if (glops->go_unlock)
1339 glops->go_unlock(gh);
1341 gl->gl_stamp = jiffies;
1343 spin_lock(&gl->gl_spin);
1346 clear_bit(GLF_LOCK, &gl->gl_flags);
1348 spin_unlock(&gl->gl_spin);
1352 * gfs2_glock_prefetch - Try to prefetch a glock
1354 * @state: the state to prefetch in
1355 * @flags: flags passed to go_xmote_th()
1359 static void gfs2_glock_prefetch(struct gfs2_glock *gl, unsigned int state,
1362 const struct gfs2_glock_operations *glops = gl->gl_ops;
1364 spin_lock(&gl->gl_spin);
1366 if (test_bit(GLF_LOCK, &gl->gl_flags) || !list_empty(&gl->gl_holders) ||
1367 !list_empty(&gl->gl_waiters1) || !list_empty(&gl->gl_waiters2) ||
1368 !list_empty(&gl->gl_waiters3) ||
1369 relaxed_state_ok(gl->gl_state, state, flags)) {
1370 spin_unlock(&gl->gl_spin);
1374 set_bit(GLF_PREFETCH, &gl->gl_flags);
1375 set_bit(GLF_LOCK, &gl->gl_flags);
1376 spin_unlock(&gl->gl_spin);
1378 glops->go_xmote_th(gl, state, flags);
1381 static void greedy_work(void *data)
1383 struct greedy *gr = data;
1384 struct gfs2_holder *gh = &gr->gr_gh;
1385 struct gfs2_glock *gl = gh->gh_gl;
1386 const struct gfs2_glock_operations *glops = gl->gl_ops;
1388 clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1390 if (glops->go_greedy)
1391 glops->go_greedy(gl);
1393 spin_lock(&gl->gl_spin);
1395 if (list_empty(&gl->gl_waiters2)) {
1396 clear_bit(GLF_GREEDY, &gl->gl_flags);
1397 spin_unlock(&gl->gl_spin);
1398 gfs2_holder_uninit(gh);
1401 gfs2_glock_hold(gl);
1402 list_add_tail(&gh->gh_list, &gl->gl_waiters2);
1404 spin_unlock(&gl->gl_spin);
1410 * gfs2_glock_be_greedy -
1414 * Returns: 0 if go_greedy will be called, 1 otherwise
1417 int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time)
1420 struct gfs2_holder *gh;
1422 if (!time || gl->gl_sbd->sd_args.ar_localcaching ||
1423 test_and_set_bit(GLF_GREEDY, &gl->gl_flags))
1426 gr = kmalloc(sizeof(struct greedy), GFP_KERNEL);
1428 clear_bit(GLF_GREEDY, &gl->gl_flags);
1433 gfs2_holder_init(gl, 0, 0, gh);
1434 set_bit(HIF_GREEDY, &gh->gh_iflags);
1435 INIT_WORK(&gr->gr_work, greedy_work, gr);
1437 set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1438 schedule_delayed_work(&gr->gr_work, time);
1444 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1445 * @gh: the holder structure
1449 void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1452 gfs2_holder_uninit(gh);
1456 * gfs2_glock_nq_num - acquire a glock based on lock number
1457 * @sdp: the filesystem
1458 * @number: the lock number
1459 * @glops: the glock operations for the type of glock
1460 * @state: the state to acquire the glock in
1461 * @flags: modifier flags for the aquisition
1462 * @gh: the struct gfs2_holder
1467 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
1468 const struct gfs2_glock_operations *glops,
1469 unsigned int state, int flags, struct gfs2_holder *gh)
1471 struct gfs2_glock *gl;
1474 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1476 error = gfs2_glock_nq_init(gl, state, flags, gh);
1484 * glock_compare - Compare two struct gfs2_glock structures for sorting
1485 * @arg_a: the first structure
1486 * @arg_b: the second structure
1490 static int glock_compare(const void *arg_a, const void *arg_b)
1492 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1493 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1494 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1495 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1497 if (a->ln_number > b->ln_number)
1499 if (a->ln_number < b->ln_number)
1501 if (gh_a->gh_state == LM_ST_SHARED && gh_b->gh_state == LM_ST_EXCLUSIVE)
1503 if (!(gh_a->gh_flags & GL_LOCAL_EXCL) && (gh_b->gh_flags & GL_LOCAL_EXCL))
1509 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1510 * @num_gh: the number of structures
1511 * @ghs: an array of struct gfs2_holder structures
1513 * Returns: 0 on success (all glocks acquired),
1514 * errno on failure (no glocks acquired)
1517 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1518 struct gfs2_holder **p)
1523 for (x = 0; x < num_gh; x++)
1526 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1528 for (x = 0; x < num_gh; x++) {
1529 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1531 error = gfs2_glock_nq(p[x]);
1534 gfs2_glock_dq(p[x]);
1543 * gfs2_glock_nq_m - acquire multiple glocks
1544 * @num_gh: the number of structures
1545 * @ghs: an array of struct gfs2_holder structures
1547 * Figure out how big an impact this function has. Either:
1548 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1549 * 2) Forget async stuff and just call nq_m_sync()
1550 * 3) Leave it like it is
1552 * Returns: 0 on success (all glocks acquired),
1553 * errno on failure (no glocks acquired)
1556 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1560 int borked = 0, serious = 0;
1567 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1568 return gfs2_glock_nq(ghs);
1571 e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1575 for (x = 0; x < num_gh; x++) {
1576 ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
1577 error = gfs2_glock_nq(&ghs[x]);
1586 for (x = 0; x < num_gh; x++) {
1587 error = e[x] = glock_wait_internal(&ghs[x]);
1590 if (error != GLR_TRYFAILED && error != GLR_CANCELED)
1600 for (x = 0; x < num_gh; x++)
1602 gfs2_glock_dq(&ghs[x]);
1607 for (x = 0; x < num_gh; x++)
1608 gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
1610 error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
1619 * gfs2_glock_dq_m - release multiple glocks
1620 * @num_gh: the number of structures
1621 * @ghs: an array of struct gfs2_holder structures
1625 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1629 for (x = 0; x < num_gh; x++)
1630 gfs2_glock_dq(&ghs[x]);
1634 * gfs2_glock_dq_uninit_m - release multiple glocks
1635 * @num_gh: the number of structures
1636 * @ghs: an array of struct gfs2_holder structures
1640 void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1644 for (x = 0; x < num_gh; x++)
1645 gfs2_glock_dq_uninit(&ghs[x]);
1649 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1650 * @sdp: the filesystem
1651 * @number: the lock number
1652 * @glops: the glock operations for the type of glock
1653 * @state: the state to acquire the glock in
1654 * @flags: modifier flags for the aquisition
1659 void gfs2_glock_prefetch_num(struct gfs2_sbd *sdp, u64 number,
1660 const struct gfs2_glock_operations *glops,
1661 unsigned int state, int flags)
1663 struct gfs2_glock *gl;
1666 if (atomic_read(&sdp->sd_reclaim_count) <
1667 gfs2_tune_get(sdp, gt_reclaim_limit)) {
1668 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1670 gfs2_glock_prefetch(gl, state, flags);
1677 * gfs2_lvb_hold - attach a LVB from a glock
1678 * @gl: The glock in question
1682 int gfs2_lvb_hold(struct gfs2_glock *gl)
1686 gfs2_glmutex_lock(gl);
1688 if (!atomic_read(&gl->gl_lvb_count)) {
1689 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1691 gfs2_glmutex_unlock(gl);
1694 gfs2_glock_hold(gl);
1696 atomic_inc(&gl->gl_lvb_count);
1698 gfs2_glmutex_unlock(gl);
1704 * gfs2_lvb_unhold - detach a LVB from a glock
1705 * @gl: The glock in question
1709 void gfs2_lvb_unhold(struct gfs2_glock *gl)
1711 gfs2_glock_hold(gl);
1712 gfs2_glmutex_lock(gl);
1714 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1715 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1716 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1721 gfs2_glmutex_unlock(gl);
1725 static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1728 struct gfs2_glock *gl;
1730 gl = gfs2_glock_find(sdp, name);
1734 if (gl->gl_ops->go_callback)
1735 gl->gl_ops->go_callback(gl, state);
1736 handle_callback(gl, state);
1738 spin_lock(&gl->gl_spin);
1740 spin_unlock(&gl->gl_spin);
1746 * gfs2_glock_cb - Callback used by locking module
1747 * @sdp: Pointer to the superblock
1748 * @type: Type of callback
1749 * @data: Type dependent data pointer
1751 * Called by the locking module when it wants to tell us something.
1752 * Either we need to drop a lock, one of our ASYNC requests completed, or
1753 * a journal from another client needs to be recovered.
1756 void gfs2_glock_cb(void *cb_data, unsigned int type, void *data)
1758 struct gfs2_sbd *sdp = cb_data;
1762 blocking_cb(sdp, data, LM_ST_UNLOCKED);
1766 blocking_cb(sdp, data, LM_ST_DEFERRED);
1770 blocking_cb(sdp, data, LM_ST_SHARED);
1774 struct lm_async_cb *async = data;
1775 struct gfs2_glock *gl;
1777 gl = gfs2_glock_find(sdp, &async->lc_name);
1778 if (gfs2_assert_warn(sdp, gl))
1780 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1781 gl->gl_req_bh(gl, async->lc_ret);
1786 case LM_CB_NEED_RECOVERY:
1787 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1788 if (sdp->sd_recoverd_process)
1789 wake_up_process(sdp->sd_recoverd_process);
1792 case LM_CB_DROPLOCKS:
1793 gfs2_gl_hash_clear(sdp, NO_WAIT);
1794 gfs2_quota_scan(sdp);
1798 gfs2_assert_warn(sdp, 0);
1804 * demote_ok - Check to see if it's ok to unlock a glock
1807 * Returns: 1 if it's ok
1810 static int demote_ok(struct gfs2_glock *gl)
1812 struct gfs2_sbd *sdp = gl->gl_sbd;
1813 const struct gfs2_glock_operations *glops = gl->gl_ops;
1816 if (test_bit(GLF_STICKY, &gl->gl_flags))
1818 else if (test_bit(GLF_PREFETCH, &gl->gl_flags))
1819 demote = time_after_eq(jiffies, gl->gl_stamp +
1820 gfs2_tune_get(sdp, gt_prefetch_secs) * HZ);
1821 else if (glops->go_demote_ok)
1822 demote = glops->go_demote_ok(gl);
1828 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1833 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1835 struct gfs2_sbd *sdp = gl->gl_sbd;
1837 spin_lock(&sdp->sd_reclaim_lock);
1838 if (list_empty(&gl->gl_reclaim)) {
1839 gfs2_glock_hold(gl);
1840 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1841 atomic_inc(&sdp->sd_reclaim_count);
1843 spin_unlock(&sdp->sd_reclaim_lock);
1845 wake_up(&sdp->sd_reclaim_wq);
1849 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1850 * @sdp: the filesystem
1852 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1853 * different glock and we notice that there are a lot of glocks in the
1858 void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1860 struct gfs2_glock *gl;
1862 spin_lock(&sdp->sd_reclaim_lock);
1863 if (list_empty(&sdp->sd_reclaim_list)) {
1864 spin_unlock(&sdp->sd_reclaim_lock);
1867 gl = list_entry(sdp->sd_reclaim_list.next,
1868 struct gfs2_glock, gl_reclaim);
1869 list_del_init(&gl->gl_reclaim);
1870 spin_unlock(&sdp->sd_reclaim_lock);
1872 atomic_dec(&sdp->sd_reclaim_count);
1873 atomic_inc(&sdp->sd_reclaimed);
1875 if (gfs2_glmutex_trylock(gl)) {
1876 if (queue_empty(gl, &gl->gl_holders) &&
1877 gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
1878 handle_callback(gl, LM_ST_UNLOCKED);
1879 gfs2_glmutex_unlock(gl);
1886 * examine_bucket - Call a function for glock in a hash bucket
1887 * @examiner: the function
1888 * @sdp: the filesystem
1889 * @bucket: the bucket
1891 * Returns: 1 if the bucket has entries
1894 static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
1897 struct gfs2_glock *gl, *prev = NULL;
1898 int has_entries = 0;
1899 struct hlist_head *head = &gl_hash_table[hash].hb_list;
1901 read_lock(gl_lock_addr(hash));
1902 /* Can't use hlist_for_each_entry - don't want prefetch here */
1903 if (hlist_empty(head))
1906 gl = list_entry(head->first, struct gfs2_glock, gl_list);
1908 if (gl->gl_sbd == sdp) {
1909 gfs2_glock_hold(gl);
1910 read_unlock(gl_lock_addr(hash));
1912 gfs2_glock_put(prev);
1915 read_lock(gl_lock_addr(hash));
1917 if (gl->gl_list.next == NULL)
1919 gl = list_entry(gl->gl_list.next, struct gfs2_glock, gl_list);
1922 read_unlock(gl_lock_addr(hash));
1924 gfs2_glock_put(prev);
1929 * scan_glock - look at a glock and see if we can reclaim it
1930 * @gl: the glock to look at
1934 static void scan_glock(struct gfs2_glock *gl)
1936 if (gl->gl_ops == &gfs2_inode_glops)
1939 if (gfs2_glmutex_trylock(gl)) {
1940 if (queue_empty(gl, &gl->gl_holders) &&
1941 gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
1943 gfs2_glmutex_unlock(gl);
1948 gfs2_glmutex_unlock(gl);
1949 gfs2_glock_schedule_for_reclaim(gl);
1953 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
1954 * @sdp: the filesystem
1958 void gfs2_scand_internal(struct gfs2_sbd *sdp)
1962 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
1963 examine_bucket(scan_glock, sdp, x);
1967 * clear_glock - look at a glock and see if we can free it from glock cache
1968 * @gl: the glock to look at
1972 static void clear_glock(struct gfs2_glock *gl)
1974 struct gfs2_sbd *sdp = gl->gl_sbd;
1977 spin_lock(&sdp->sd_reclaim_lock);
1978 if (!list_empty(&gl->gl_reclaim)) {
1979 list_del_init(&gl->gl_reclaim);
1980 atomic_dec(&sdp->sd_reclaim_count);
1981 spin_unlock(&sdp->sd_reclaim_lock);
1982 released = gfs2_glock_put(gl);
1983 gfs2_assert(sdp, !released);
1985 spin_unlock(&sdp->sd_reclaim_lock);
1988 if (gfs2_glmutex_trylock(gl)) {
1989 if (queue_empty(gl, &gl->gl_holders) &&
1990 gl->gl_state != LM_ST_UNLOCKED)
1991 handle_callback(gl, LM_ST_UNLOCKED);
1992 gfs2_glmutex_unlock(gl);
1997 * gfs2_gl_hash_clear - Empty out the glock hash table
1998 * @sdp: the filesystem
1999 * @wait: wait until it's all gone
2001 * Called when unmounting the filesystem, or when inter-node lock manager
2002 * requests DROPLOCKS because it is running out of capacity.
2005 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
2015 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2016 if (examine_bucket(clear_glock, sdp, x))
2023 if (time_after_eq(jiffies,
2024 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
2025 fs_warn(sdp, "Unmount seems to be stalled. "
2026 "Dumping lock state...\n");
2027 gfs2_dump_lockstate(sdp);
2031 invalidate_inodes(sdp->sd_vfs);
2037 * Diagnostic routines to help debug distributed deadlock
2041 * dump_holder - print information about a glock holder
2042 * @str: a string naming the type of holder
2043 * @gh: the glock holder
2045 * Returns: 0 on success, -ENOBUFS when we run out of space
2048 static int dump_holder(char *str, struct gfs2_holder *gh)
2051 int error = -ENOBUFS;
2053 printk(KERN_INFO " %s\n", str);
2054 printk(KERN_INFO " owner = %ld\n",
2055 (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
2056 printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
2057 printk(KERN_INFO " gh_flags =");
2058 for (x = 0; x < 32; x++)
2059 if (gh->gh_flags & (1 << x))
2062 printk(KERN_INFO " error = %d\n", gh->gh_error);
2063 printk(KERN_INFO " gh_iflags =");
2064 for (x = 0; x < 32; x++)
2065 if (test_bit(x, &gh->gh_iflags))
2068 print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip);
2076 * dump_inode - print information about an inode
2079 * Returns: 0 on success, -ENOBUFS when we run out of space
2082 static int dump_inode(struct gfs2_inode *ip)
2085 int error = -ENOBUFS;
2087 printk(KERN_INFO " Inode:\n");
2088 printk(KERN_INFO " num = %llu %llu\n",
2089 (unsigned long long)ip->i_num.no_formal_ino,
2090 (unsigned long long)ip->i_num.no_addr);
2091 printk(KERN_INFO " type = %u\n", IF2DT(ip->i_di.di_mode));
2092 printk(KERN_INFO " i_flags =");
2093 for (x = 0; x < 32; x++)
2094 if (test_bit(x, &ip->i_flags))
2104 * dump_glock - print information about a glock
2106 * @count: where we are in the buffer
2108 * Returns: 0 on success, -ENOBUFS when we run out of space
2111 static int dump_glock(struct gfs2_glock *gl)
2113 struct gfs2_holder *gh;
2115 int error = -ENOBUFS;
2117 spin_lock(&gl->gl_spin);
2119 printk(KERN_INFO "Glock 0x%p (%u, %llu)\n", gl, gl->gl_name.ln_type,
2120 (unsigned long long)gl->gl_name.ln_number);
2121 printk(KERN_INFO " gl_flags =");
2122 for (x = 0; x < 32; x++) {
2123 if (test_bit(x, &gl->gl_flags))
2127 printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref.refcount));
2128 printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
2129 printk(KERN_INFO " gl_owner = %s\n", gl->gl_owner->comm);
2130 print_symbol(KERN_INFO " gl_ip = %s\n", gl->gl_ip);
2131 printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
2132 printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
2133 printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
2134 printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
2135 printk(KERN_INFO " le = %s\n",
2136 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
2137 printk(KERN_INFO " reclaim = %s\n",
2138 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
2140 printk(KERN_INFO " aspace = 0x%p nrpages = %lu\n", gl->gl_aspace,
2141 gl->gl_aspace->i_mapping->nrpages);
2143 printk(KERN_INFO " aspace = no\n");
2144 printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
2145 if (gl->gl_req_gh) {
2146 error = dump_holder("Request", gl->gl_req_gh);
2150 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
2151 error = dump_holder("Holder", gh);
2155 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
2156 error = dump_holder("Waiter1", gh);
2160 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
2161 error = dump_holder("Waiter2", gh);
2165 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
2166 error = dump_holder("Waiter3", gh);
2170 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
2171 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
2172 list_empty(&gl->gl_holders)) {
2173 error = dump_inode(gl->gl_object);
2178 printk(KERN_INFO " Inode: busy\n");
2185 spin_unlock(&gl->gl_spin);
2190 * gfs2_dump_lockstate - print out the current lockstate
2191 * @sdp: the filesystem
2192 * @ub: the buffer to copy the information into
2194 * If @ub is NULL, dump the lockstate to the console.
2198 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
2200 struct gfs2_glock *gl;
2201 struct hlist_node *h;
2205 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2207 read_lock(gl_lock_addr(x));
2209 hlist_for_each_entry(gl, h, &gl_hash_table[x].hb_list, gl_list) {
2210 if (gl->gl_sbd != sdp)
2213 error = dump_glock(gl);
2218 read_unlock(gl_lock_addr(x));
2228 int __init gfs2_glock_init(void)
2231 for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
2232 INIT_HLIST_HEAD(&gl_hash_table[i].hb_list);
2234 #ifdef GL_HASH_LOCK_SZ
2235 for(i = 0; i < GL_HASH_LOCK_SZ; i++) {
2236 rwlock_init(&gl_hash_locks[i]);