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 <linux/wait.h>
23 #include <asm/uaccess.h>
38 struct gfs2_holder gr_gh;
39 struct delayed_work 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(unsigned int 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 atomic_inc(&gl->gl_ref);
186 * gfs2_glock_put() - Decrement reference count on glock
187 * @gl: The glock to put
191 int gfs2_glock_put(struct gfs2_glock *gl)
194 struct gfs2_sbd *sdp = gl->gl_sbd;
196 write_lock(gl_lock_addr(gl->gl_hash));
197 if (atomic_dec_and_test(&gl->gl_ref)) {
198 hlist_del(&gl->gl_list);
199 write_unlock(gl_lock_addr(gl->gl_hash));
200 BUG_ON(spin_is_locked(&gl->gl_spin));
201 gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED);
202 gfs2_assert(sdp, list_empty(&gl->gl_reclaim));
203 gfs2_assert(sdp, list_empty(&gl->gl_holders));
204 gfs2_assert(sdp, list_empty(&gl->gl_waiters1));
205 gfs2_assert(sdp, list_empty(&gl->gl_waiters2));
206 gfs2_assert(sdp, list_empty(&gl->gl_waiters3));
211 write_unlock(gl_lock_addr(gl->gl_hash));
217 * queue_empty - check to see if a glock's queue is empty
219 * @head: the head of the queue to check
221 * This function protects the list in the event that a process already
222 * has a holder on the list and is adding a second holder for itself.
223 * The glmutex lock is what generally prevents processes from working
224 * on the same glock at once, but the special case of adding a second
225 * holder for yourself ("recursive" locking) doesn't involve locking
226 * glmutex, making the spin lock necessary.
228 * Returns: 1 if the queue is empty
231 static inline int queue_empty(struct gfs2_glock *gl, struct list_head *head)
234 spin_lock(&gl->gl_spin);
235 empty = list_empty(head);
236 spin_unlock(&gl->gl_spin);
241 * search_bucket() - Find struct gfs2_glock by lock number
242 * @bucket: the bucket to search
243 * @name: The lock name
245 * Returns: NULL, or the struct gfs2_glock with the requested number
248 static struct gfs2_glock *search_bucket(unsigned int hash,
249 const struct gfs2_sbd *sdp,
250 const struct lm_lockname *name)
252 struct gfs2_glock *gl;
253 struct hlist_node *h;
255 hlist_for_each_entry(gl, h, &gl_hash_table[hash].hb_list, gl_list) {
256 if (!lm_name_equal(&gl->gl_name, name))
258 if (gl->gl_sbd != sdp)
261 atomic_inc(&gl->gl_ref);
270 * gfs2_glock_find() - Find glock by lock number
271 * @sdp: The GFS2 superblock
272 * @name: The lock name
274 * Returns: NULL, or the struct gfs2_glock with the requested number
277 static struct gfs2_glock *gfs2_glock_find(const struct gfs2_sbd *sdp,
278 const struct lm_lockname *name)
280 unsigned int hash = gl_hash(sdp, name);
281 struct gfs2_glock *gl;
283 read_lock(gl_lock_addr(hash));
284 gl = search_bucket(hash, sdp, name);
285 read_unlock(gl_lock_addr(hash));
291 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
292 * @sdp: The GFS2 superblock
293 * @number: the lock number
294 * @glops: The glock_operations to use
295 * @create: If 0, don't create the glock if it doesn't exist
296 * @glp: the glock is returned here
298 * This does not lock a glock, just finds/creates structures for one.
303 int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
304 const struct gfs2_glock_operations *glops, int create,
305 struct gfs2_glock **glp)
307 struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
308 struct gfs2_glock *gl, *tmp;
309 unsigned int hash = gl_hash(sdp, &name);
312 read_lock(gl_lock_addr(hash));
313 gl = search_bucket(hash, sdp, &name);
314 read_unlock(gl_lock_addr(hash));
321 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
327 atomic_set(&gl->gl_ref, 1);
328 gl->gl_state = LM_ST_UNLOCKED;
333 gl->gl_req_gh = NULL;
334 gl->gl_req_bh = NULL;
336 gl->gl_stamp = jiffies;
337 gl->gl_object = NULL;
339 gl->gl_aspace = NULL;
340 lops_init_le(&gl->gl_le, &gfs2_glock_lops);
342 /* If this glock protects actual on-disk data or metadata blocks,
343 create a VFS inode to manage the pages/buffers holding them. */
344 if (glops == &gfs2_inode_glops || glops == &gfs2_rgrp_glops) {
345 gl->gl_aspace = gfs2_aspace_get(sdp);
346 if (!gl->gl_aspace) {
352 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
356 write_lock(gl_lock_addr(hash));
357 tmp = search_bucket(hash, sdp, &name);
359 write_unlock(gl_lock_addr(hash));
363 hlist_add_head(&gl->gl_list, &gl_hash_table[hash].hb_list);
364 write_unlock(gl_lock_addr(hash));
373 gfs2_aspace_put(gl->gl_aspace);
375 kmem_cache_free(gfs2_glock_cachep, gl);
380 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
382 * @state: the state we're requesting
383 * @flags: the modifier flags
384 * @gh: the holder structure
388 void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
389 struct gfs2_holder *gh)
391 INIT_LIST_HEAD(&gh->gh_list);
393 gh->gh_ip = (unsigned long)__builtin_return_address(0);
394 gh->gh_owner = current;
395 gh->gh_state = state;
396 gh->gh_flags = flags;
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);
482 static void gfs2_holder_dispose_or_wake(struct gfs2_holder *gh)
484 if (test_bit(HIF_DEALLOC, &gh->gh_iflags)) {
488 clear_bit(HIF_WAIT, &gh->gh_iflags);
490 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
493 static int holder_wait(void *word)
499 static void wait_on_holder(struct gfs2_holder *gh)
502 wait_on_bit(&gh->gh_iflags, HIF_WAIT, holder_wait, TASK_UNINTERRUPTIBLE);
506 * rq_mutex - process a mutex request in the queue
507 * @gh: the glock holder
509 * Returns: 1 if the queue is blocked
512 static int rq_mutex(struct gfs2_holder *gh)
514 struct gfs2_glock *gl = gh->gh_gl;
516 list_del_init(&gh->gh_list);
517 /* gh->gh_error never examined. */
518 set_bit(GLF_LOCK, &gl->gl_flags);
519 clear_bit(HIF_WAIT, &gh->gh_flags);
521 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
527 * rq_promote - process a promote request in the queue
528 * @gh: the glock holder
530 * Acquire a new inter-node lock, or change a lock state to more restrictive.
532 * Returns: 1 if the queue is blocked
535 static int rq_promote(struct gfs2_holder *gh)
537 struct gfs2_glock *gl = gh->gh_gl;
538 struct gfs2_sbd *sdp = gl->gl_sbd;
539 const struct gfs2_glock_operations *glops = gl->gl_ops;
541 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
542 if (list_empty(&gl->gl_holders)) {
544 set_bit(GLF_LOCK, &gl->gl_flags);
545 spin_unlock(&gl->gl_spin);
547 if (atomic_read(&sdp->sd_reclaim_count) >
548 gfs2_tune_get(sdp, gt_reclaim_limit) &&
549 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
550 gfs2_reclaim_glock(sdp);
551 gfs2_reclaim_glock(sdp);
554 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
555 spin_lock(&gl->gl_spin);
560 if (list_empty(&gl->gl_holders)) {
561 set_bit(HIF_FIRST, &gh->gh_iflags);
562 set_bit(GLF_LOCK, &gl->gl_flags);
564 struct gfs2_holder *next_gh;
565 if (gh->gh_flags & GL_LOCAL_EXCL)
567 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
569 if (next_gh->gh_flags & GL_LOCAL_EXCL)
573 list_move_tail(&gh->gh_list, &gl->gl_holders);
575 set_bit(HIF_HOLDER, &gh->gh_iflags);
577 gfs2_holder_dispose_or_wake(gh);
583 * rq_demote - process a demote request in the queue
584 * @gh: the glock holder
586 * Returns: 1 if the queue is blocked
589 static int rq_demote(struct gfs2_holder *gh)
591 struct gfs2_glock *gl = gh->gh_gl;
592 const struct gfs2_glock_operations *glops = gl->gl_ops;
594 if (!list_empty(&gl->gl_holders))
597 if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) {
598 list_del_init(&gh->gh_list);
600 spin_unlock(&gl->gl_spin);
601 gfs2_holder_dispose_or_wake(gh);
602 spin_lock(&gl->gl_spin);
605 set_bit(GLF_LOCK, &gl->gl_flags);
606 spin_unlock(&gl->gl_spin);
608 if (gh->gh_state == LM_ST_UNLOCKED ||
609 gl->gl_state != LM_ST_EXCLUSIVE)
610 glops->go_drop_th(gl);
612 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
614 spin_lock(&gl->gl_spin);
621 * rq_greedy - process a queued request to drop greedy status
622 * @gh: the glock holder
624 * Returns: 1 if the queue is blocked
627 static int rq_greedy(struct gfs2_holder *gh)
629 struct gfs2_glock *gl = gh->gh_gl;
631 list_del_init(&gh->gh_list);
632 /* gh->gh_error never examined. */
633 clear_bit(GLF_GREEDY, &gl->gl_flags);
634 spin_unlock(&gl->gl_spin);
636 gfs2_holder_uninit(gh);
637 kfree(container_of(gh, struct greedy, gr_gh));
639 spin_lock(&gl->gl_spin);
645 * run_queue - process holder structures on a glock
649 static void run_queue(struct gfs2_glock *gl)
651 struct gfs2_holder *gh;
655 if (test_bit(GLF_LOCK, &gl->gl_flags))
658 if (!list_empty(&gl->gl_waiters1)) {
659 gh = list_entry(gl->gl_waiters1.next,
660 struct gfs2_holder, gh_list);
662 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
663 blocked = rq_mutex(gh);
665 gfs2_assert_warn(gl->gl_sbd, 0);
667 } else if (!list_empty(&gl->gl_waiters2) &&
668 !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) {
669 gh = list_entry(gl->gl_waiters2.next,
670 struct gfs2_holder, gh_list);
672 if (test_bit(HIF_DEMOTE, &gh->gh_iflags))
673 blocked = rq_demote(gh);
674 else if (test_bit(HIF_GREEDY, &gh->gh_iflags))
675 blocked = rq_greedy(gh);
677 gfs2_assert_warn(gl->gl_sbd, 0);
679 } else if (!list_empty(&gl->gl_waiters3)) {
680 gh = list_entry(gl->gl_waiters3.next,
681 struct gfs2_holder, gh_list);
683 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
684 blocked = rq_promote(gh);
686 gfs2_assert_warn(gl->gl_sbd, 0);
697 * gfs2_glmutex_lock - acquire a local lock on a glock
700 * Gives caller exclusive access to manipulate a glock structure.
703 static void gfs2_glmutex_lock(struct gfs2_glock *gl)
705 struct gfs2_holder gh;
707 gfs2_holder_init(gl, 0, 0, &gh);
708 set_bit(HIF_MUTEX, &gh.gh_iflags);
709 if (test_and_set_bit(HIF_WAIT, &gh.gh_iflags))
712 spin_lock(&gl->gl_spin);
713 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
714 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
716 gl->gl_owner = current;
717 gl->gl_ip = (unsigned long)__builtin_return_address(0);
718 clear_bit(HIF_WAIT, &gh.gh_iflags);
720 wake_up_bit(&gh.gh_iflags, HIF_WAIT);
722 spin_unlock(&gl->gl_spin);
725 gfs2_holder_uninit(&gh);
729 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
732 * Returns: 1 if the glock is acquired
735 static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
739 spin_lock(&gl->gl_spin);
740 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
743 gl->gl_owner = current;
744 gl->gl_ip = (unsigned long)__builtin_return_address(0);
746 spin_unlock(&gl->gl_spin);
752 * gfs2_glmutex_unlock - release a local lock on a glock
757 static void gfs2_glmutex_unlock(struct gfs2_glock *gl)
759 spin_lock(&gl->gl_spin);
760 clear_bit(GLF_LOCK, &gl->gl_flags);
764 BUG_ON(!spin_is_locked(&gl->gl_spin));
765 spin_unlock(&gl->gl_spin);
769 * handle_callback - add a demote request to a lock's queue
771 * @state: the state the caller wants us to change to
773 * Note: This may fail sliently if we are out of memory.
776 static void handle_callback(struct gfs2_glock *gl, unsigned int state)
778 struct gfs2_holder *gh, *new_gh = NULL;
781 spin_lock(&gl->gl_spin);
783 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
784 if (test_bit(HIF_DEMOTE, &gh->gh_iflags) &&
785 gl->gl_req_gh != gh) {
786 if (gh->gh_state != state)
787 gh->gh_state = LM_ST_UNLOCKED;
793 list_add_tail(&new_gh->gh_list, &gl->gl_waiters2);
796 spin_unlock(&gl->gl_spin);
798 new_gh = gfs2_holder_get(gl, state, LM_FLAG_TRY, GFP_NOFS);
801 set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
802 set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
803 set_bit(HIF_WAIT, &new_gh->gh_iflags);
809 spin_unlock(&gl->gl_spin);
812 gfs2_holder_put(new_gh);
816 * state_change - record that the glock is now in a different state
818 * @new_state the new state
822 static void state_change(struct gfs2_glock *gl, unsigned int new_state)
826 held1 = (gl->gl_state != LM_ST_UNLOCKED);
827 held2 = (new_state != LM_ST_UNLOCKED);
829 if (held1 != held2) {
836 gl->gl_state = new_state;
840 * xmote_bh - Called after the lock module is done acquiring a lock
841 * @gl: The glock in question
842 * @ret: the int returned from the lock module
846 static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
848 struct gfs2_sbd *sdp = gl->gl_sbd;
849 const struct gfs2_glock_operations *glops = gl->gl_ops;
850 struct gfs2_holder *gh = gl->gl_req_gh;
851 int prev_state = gl->gl_state;
854 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
855 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
856 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
858 state_change(gl, ret & LM_OUT_ST_MASK);
860 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
862 glops->go_inval(gl, DIO_METADATA);
863 } else if (gl->gl_state == LM_ST_DEFERRED) {
864 /* We might not want to do this here.
865 Look at moving to the inode glops. */
867 glops->go_inval(gl, 0);
870 /* Deal with each possible exit condition */
873 gl->gl_stamp = jiffies;
874 else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
875 spin_lock(&gl->gl_spin);
876 list_del_init(&gh->gh_list);
878 spin_unlock(&gl->gl_spin);
879 } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
880 spin_lock(&gl->gl_spin);
881 list_del_init(&gh->gh_list);
882 if (gl->gl_state == gh->gh_state ||
883 gl->gl_state == LM_ST_UNLOCKED) {
886 if (gfs2_assert_warn(sdp, gh->gh_flags &
887 (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
888 fs_warn(sdp, "ret = 0x%.8X\n", ret);
889 gh->gh_error = GLR_TRYFAILED;
891 spin_unlock(&gl->gl_spin);
893 if (ret & LM_OUT_CANCELED)
894 handle_callback(gl, LM_ST_UNLOCKED);
896 } else if (ret & LM_OUT_CANCELED) {
897 spin_lock(&gl->gl_spin);
898 list_del_init(&gh->gh_list);
899 gh->gh_error = GLR_CANCELED;
900 spin_unlock(&gl->gl_spin);
902 } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
903 spin_lock(&gl->gl_spin);
904 list_move_tail(&gh->gh_list, &gl->gl_holders);
906 set_bit(HIF_HOLDER, &gh->gh_iflags);
907 spin_unlock(&gl->gl_spin);
909 set_bit(HIF_FIRST, &gh->gh_iflags);
913 } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
914 spin_lock(&gl->gl_spin);
915 list_del_init(&gh->gh_list);
916 gh->gh_error = GLR_TRYFAILED;
917 spin_unlock(&gl->gl_spin);
920 if (gfs2_assert_withdraw(sdp, 0) == -1)
921 fs_err(sdp, "ret = 0x%.8X\n", ret);
924 if (glops->go_xmote_bh)
925 glops->go_xmote_bh(gl);
928 spin_lock(&gl->gl_spin);
929 gl->gl_req_gh = NULL;
930 gl->gl_req_bh = NULL;
931 clear_bit(GLF_LOCK, &gl->gl_flags);
933 spin_unlock(&gl->gl_spin);
939 gfs2_holder_dispose_or_wake(gh);
943 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
944 * @gl: The glock in question
945 * @state: the requested state
946 * @flags: modifier flags to the lock call
950 void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags)
952 struct gfs2_sbd *sdp = gl->gl_sbd;
953 const struct gfs2_glock_operations *glops = gl->gl_ops;
954 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
955 LM_FLAG_NOEXP | LM_FLAG_ANY |
957 unsigned int lck_ret;
959 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
960 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
961 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
962 gfs2_assert_warn(sdp, state != gl->gl_state);
964 if (gl->gl_state == LM_ST_EXCLUSIVE && glops->go_sync)
968 gl->gl_req_bh = xmote_bh;
970 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state, lck_flags);
972 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
975 if (lck_ret & LM_OUT_ASYNC)
976 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
978 xmote_bh(gl, lck_ret);
982 * drop_bh - Called after a lock module unlock completes
984 * @ret: the return status
986 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
987 * Doesn't drop the reference on the glock the top half took out
991 static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
993 struct gfs2_sbd *sdp = gl->gl_sbd;
994 const struct gfs2_glock_operations *glops = gl->gl_ops;
995 struct gfs2_holder *gh = gl->gl_req_gh;
997 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
998 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
999 gfs2_assert_warn(sdp, !ret);
1001 state_change(gl, LM_ST_UNLOCKED);
1003 if (glops->go_inval)
1004 glops->go_inval(gl, DIO_METADATA);
1007 spin_lock(&gl->gl_spin);
1008 list_del_init(&gh->gh_list);
1010 spin_unlock(&gl->gl_spin);
1013 if (glops->go_drop_bh)
1014 glops->go_drop_bh(gl);
1016 spin_lock(&gl->gl_spin);
1017 gl->gl_req_gh = NULL;
1018 gl->gl_req_bh = NULL;
1019 clear_bit(GLF_LOCK, &gl->gl_flags);
1021 spin_unlock(&gl->gl_spin);
1026 gfs2_holder_dispose_or_wake(gh);
1030 * gfs2_glock_drop_th - call into the lock module to unlock a lock
1035 void gfs2_glock_drop_th(struct gfs2_glock *gl)
1037 struct gfs2_sbd *sdp = gl->gl_sbd;
1038 const struct gfs2_glock_operations *glops = gl->gl_ops;
1041 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1042 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
1043 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
1045 if (gl->gl_state == LM_ST_EXCLUSIVE && glops->go_sync)
1048 gfs2_glock_hold(gl);
1049 gl->gl_req_bh = drop_bh;
1051 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
1053 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
1059 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
1063 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1064 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1066 * Don't cancel GL_NOCANCEL requests.
1069 static void do_cancels(struct gfs2_holder *gh)
1071 struct gfs2_glock *gl = gh->gh_gl;
1073 spin_lock(&gl->gl_spin);
1075 while (gl->gl_req_gh != gh &&
1076 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1077 !list_empty(&gh->gh_list)) {
1078 if (gl->gl_req_bh && !(gl->gl_req_gh &&
1079 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
1080 spin_unlock(&gl->gl_spin);
1081 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
1083 spin_lock(&gl->gl_spin);
1085 spin_unlock(&gl->gl_spin);
1087 spin_lock(&gl->gl_spin);
1091 spin_unlock(&gl->gl_spin);
1095 * glock_wait_internal - wait on a glock acquisition
1096 * @gh: the glock holder
1098 * Returns: 0 on success
1101 static int glock_wait_internal(struct gfs2_holder *gh)
1103 struct gfs2_glock *gl = gh->gh_gl;
1104 struct gfs2_sbd *sdp = gl->gl_sbd;
1105 const struct gfs2_glock_operations *glops = gl->gl_ops;
1107 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1110 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1111 spin_lock(&gl->gl_spin);
1112 if (gl->gl_req_gh != gh &&
1113 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1114 !list_empty(&gh->gh_list)) {
1115 list_del_init(&gh->gh_list);
1116 gh->gh_error = GLR_TRYFAILED;
1118 spin_unlock(&gl->gl_spin);
1119 return gh->gh_error;
1121 spin_unlock(&gl->gl_spin);
1124 if (gh->gh_flags & LM_FLAG_PRIORITY)
1129 return gh->gh_error;
1131 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
1132 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state, gh->gh_state,
1135 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1136 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1138 if (glops->go_lock) {
1139 gh->gh_error = glops->go_lock(gh);
1141 spin_lock(&gl->gl_spin);
1142 list_del_init(&gh->gh_list);
1143 spin_unlock(&gl->gl_spin);
1147 spin_lock(&gl->gl_spin);
1148 gl->gl_req_gh = NULL;
1149 gl->gl_req_bh = NULL;
1150 clear_bit(GLF_LOCK, &gl->gl_flags);
1152 spin_unlock(&gl->gl_spin);
1155 return gh->gh_error;
1158 static inline struct gfs2_holder *
1159 find_holder_by_owner(struct list_head *head, struct task_struct *owner)
1161 struct gfs2_holder *gh;
1163 list_for_each_entry(gh, head, gh_list) {
1164 if (gh->gh_owner == owner)
1172 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1173 * @gh: the holder structure to add
1177 static void add_to_queue(struct gfs2_holder *gh)
1179 struct gfs2_glock *gl = gh->gh_gl;
1180 struct gfs2_holder *existing;
1182 BUG_ON(!gh->gh_owner);
1183 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
1186 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
1188 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1189 printk(KERN_INFO "pid : %d\n", existing->gh_owner->pid);
1190 printk(KERN_INFO "lock type : %d lock state : %d\n",
1191 existing->gh_gl->gl_name.ln_type, existing->gh_gl->gl_state);
1192 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1193 printk(KERN_INFO "pid : %d\n", gh->gh_owner->pid);
1194 printk(KERN_INFO "lock type : %d lock state : %d\n",
1195 gl->gl_name.ln_type, gl->gl_state);
1199 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
1201 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1202 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1206 if (gh->gh_flags & LM_FLAG_PRIORITY)
1207 list_add(&gh->gh_list, &gl->gl_waiters3);
1209 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1213 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1214 * @gh: the holder structure
1216 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1218 * Returns: 0, GLR_TRYFAILED, or errno on failure
1221 int gfs2_glock_nq(struct gfs2_holder *gh)
1223 struct gfs2_glock *gl = gh->gh_gl;
1224 struct gfs2_sbd *sdp = gl->gl_sbd;
1228 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1229 set_bit(HIF_ABORTED, &gh->gh_iflags);
1233 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1235 spin_lock(&gl->gl_spin);
1238 spin_unlock(&gl->gl_spin);
1240 if (!(gh->gh_flags & GL_ASYNC)) {
1241 error = glock_wait_internal(gh);
1242 if (error == GLR_CANCELED) {
1252 * gfs2_glock_poll - poll to see if an async request has been completed
1255 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1258 int gfs2_glock_poll(struct gfs2_holder *gh)
1260 struct gfs2_glock *gl = gh->gh_gl;
1263 spin_lock(&gl->gl_spin);
1265 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1267 else if (list_empty(&gh->gh_list)) {
1268 if (gh->gh_error == GLR_CANCELED) {
1269 spin_unlock(&gl->gl_spin);
1271 if (gfs2_glock_nq(gh))
1278 spin_unlock(&gl->gl_spin);
1284 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1285 * @gh: the holder structure
1287 * Returns: 0, GLR_TRYFAILED, or errno on failure
1290 int gfs2_glock_wait(struct gfs2_holder *gh)
1294 error = glock_wait_internal(gh);
1295 if (error == GLR_CANCELED) {
1297 gh->gh_flags &= ~GL_ASYNC;
1298 error = gfs2_glock_nq(gh);
1305 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1306 * @gh: the glock holder
1310 void gfs2_glock_dq(struct gfs2_holder *gh)
1312 struct gfs2_glock *gl = gh->gh_gl;
1313 const struct gfs2_glock_operations *glops = gl->gl_ops;
1315 if (gh->gh_flags & GL_NOCACHE)
1316 handle_callback(gl, LM_ST_UNLOCKED);
1318 gfs2_glmutex_lock(gl);
1320 spin_lock(&gl->gl_spin);
1321 list_del_init(&gh->gh_list);
1323 if (list_empty(&gl->gl_holders)) {
1324 spin_unlock(&gl->gl_spin);
1326 if (glops->go_unlock)
1327 glops->go_unlock(gh);
1329 gl->gl_stamp = jiffies;
1331 spin_lock(&gl->gl_spin);
1334 clear_bit(GLF_LOCK, &gl->gl_flags);
1336 spin_unlock(&gl->gl_spin);
1339 static void greedy_work(struct work_struct *work)
1341 struct greedy *gr = container_of(work, struct greedy, gr_work.work);
1342 struct gfs2_holder *gh = &gr->gr_gh;
1343 struct gfs2_glock *gl = gh->gh_gl;
1344 const struct gfs2_glock_operations *glops = gl->gl_ops;
1346 clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1348 if (glops->go_greedy)
1349 glops->go_greedy(gl);
1351 spin_lock(&gl->gl_spin);
1353 if (list_empty(&gl->gl_waiters2)) {
1354 clear_bit(GLF_GREEDY, &gl->gl_flags);
1355 spin_unlock(&gl->gl_spin);
1356 gfs2_holder_uninit(gh);
1359 gfs2_glock_hold(gl);
1360 list_add_tail(&gh->gh_list, &gl->gl_waiters2);
1362 spin_unlock(&gl->gl_spin);
1368 * gfs2_glock_be_greedy -
1372 * Returns: 0 if go_greedy will be called, 1 otherwise
1375 int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time)
1378 struct gfs2_holder *gh;
1380 if (!time || gl->gl_sbd->sd_args.ar_localcaching ||
1381 test_and_set_bit(GLF_GREEDY, &gl->gl_flags))
1384 gr = kmalloc(sizeof(struct greedy), GFP_KERNEL);
1386 clear_bit(GLF_GREEDY, &gl->gl_flags);
1391 gfs2_holder_init(gl, 0, 0, gh);
1392 set_bit(HIF_GREEDY, &gh->gh_iflags);
1393 INIT_DELAYED_WORK(&gr->gr_work, greedy_work);
1395 set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1396 schedule_delayed_work(&gr->gr_work, time);
1402 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1403 * @gh: the holder structure
1407 void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1410 gfs2_holder_uninit(gh);
1414 * gfs2_glock_nq_num - acquire a glock based on lock number
1415 * @sdp: the filesystem
1416 * @number: the lock number
1417 * @glops: the glock operations for the type of glock
1418 * @state: the state to acquire the glock in
1419 * @flags: modifier flags for the aquisition
1420 * @gh: the struct gfs2_holder
1425 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
1426 const struct gfs2_glock_operations *glops,
1427 unsigned int state, int flags, struct gfs2_holder *gh)
1429 struct gfs2_glock *gl;
1432 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1434 error = gfs2_glock_nq_init(gl, state, flags, gh);
1442 * glock_compare - Compare two struct gfs2_glock structures for sorting
1443 * @arg_a: the first structure
1444 * @arg_b: the second structure
1448 static int glock_compare(const void *arg_a, const void *arg_b)
1450 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1451 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1452 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1453 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1455 if (a->ln_number > b->ln_number)
1457 if (a->ln_number < b->ln_number)
1459 if (gh_a->gh_state == LM_ST_SHARED && gh_b->gh_state == LM_ST_EXCLUSIVE)
1461 if (!(gh_a->gh_flags & GL_LOCAL_EXCL) && (gh_b->gh_flags & GL_LOCAL_EXCL))
1467 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1468 * @num_gh: the number of structures
1469 * @ghs: an array of struct gfs2_holder structures
1471 * Returns: 0 on success (all glocks acquired),
1472 * errno on failure (no glocks acquired)
1475 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1476 struct gfs2_holder **p)
1481 for (x = 0; x < num_gh; x++)
1484 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1486 for (x = 0; x < num_gh; x++) {
1487 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1489 error = gfs2_glock_nq(p[x]);
1492 gfs2_glock_dq(p[x]);
1501 * gfs2_glock_nq_m - acquire multiple glocks
1502 * @num_gh: the number of structures
1503 * @ghs: an array of struct gfs2_holder structures
1505 * Figure out how big an impact this function has. Either:
1506 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1507 * 2) Forget async stuff and just call nq_m_sync()
1508 * 3) Leave it like it is
1510 * Returns: 0 on success (all glocks acquired),
1511 * errno on failure (no glocks acquired)
1514 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1518 int borked = 0, serious = 0;
1525 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1526 return gfs2_glock_nq(ghs);
1529 e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1533 for (x = 0; x < num_gh; x++) {
1534 ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
1535 error = gfs2_glock_nq(&ghs[x]);
1544 for (x = 0; x < num_gh; x++) {
1545 error = e[x] = glock_wait_internal(&ghs[x]);
1548 if (error != GLR_TRYFAILED && error != GLR_CANCELED)
1558 for (x = 0; x < num_gh; x++)
1560 gfs2_glock_dq(&ghs[x]);
1565 for (x = 0; x < num_gh; x++)
1566 gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
1568 error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
1577 * gfs2_glock_dq_m - release multiple glocks
1578 * @num_gh: the number of structures
1579 * @ghs: an array of struct gfs2_holder structures
1583 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1587 for (x = 0; x < num_gh; x++)
1588 gfs2_glock_dq(&ghs[x]);
1592 * gfs2_glock_dq_uninit_m - release multiple glocks
1593 * @num_gh: the number of structures
1594 * @ghs: an array of struct gfs2_holder structures
1598 void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1602 for (x = 0; x < num_gh; x++)
1603 gfs2_glock_dq_uninit(&ghs[x]);
1607 * gfs2_lvb_hold - attach a LVB from a glock
1608 * @gl: The glock in question
1612 int gfs2_lvb_hold(struct gfs2_glock *gl)
1616 gfs2_glmutex_lock(gl);
1618 if (!atomic_read(&gl->gl_lvb_count)) {
1619 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1621 gfs2_glmutex_unlock(gl);
1624 gfs2_glock_hold(gl);
1626 atomic_inc(&gl->gl_lvb_count);
1628 gfs2_glmutex_unlock(gl);
1634 * gfs2_lvb_unhold - detach a LVB from a glock
1635 * @gl: The glock in question
1639 void gfs2_lvb_unhold(struct gfs2_glock *gl)
1641 gfs2_glock_hold(gl);
1642 gfs2_glmutex_lock(gl);
1644 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1645 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1646 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1651 gfs2_glmutex_unlock(gl);
1655 static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1658 struct gfs2_glock *gl;
1660 gl = gfs2_glock_find(sdp, name);
1664 if (gl->gl_ops->go_callback)
1665 gl->gl_ops->go_callback(gl, state);
1666 handle_callback(gl, state);
1668 spin_lock(&gl->gl_spin);
1670 spin_unlock(&gl->gl_spin);
1676 * gfs2_glock_cb - Callback used by locking module
1677 * @sdp: Pointer to the superblock
1678 * @type: Type of callback
1679 * @data: Type dependent data pointer
1681 * Called by the locking module when it wants to tell us something.
1682 * Either we need to drop a lock, one of our ASYNC requests completed, or
1683 * a journal from another client needs to be recovered.
1686 void gfs2_glock_cb(void *cb_data, unsigned int type, void *data)
1688 struct gfs2_sbd *sdp = cb_data;
1692 blocking_cb(sdp, data, LM_ST_UNLOCKED);
1696 blocking_cb(sdp, data, LM_ST_DEFERRED);
1700 blocking_cb(sdp, data, LM_ST_SHARED);
1704 struct lm_async_cb *async = data;
1705 struct gfs2_glock *gl;
1707 gl = gfs2_glock_find(sdp, &async->lc_name);
1708 if (gfs2_assert_warn(sdp, gl))
1710 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1711 gl->gl_req_bh(gl, async->lc_ret);
1716 case LM_CB_NEED_RECOVERY:
1717 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1718 if (sdp->sd_recoverd_process)
1719 wake_up_process(sdp->sd_recoverd_process);
1722 case LM_CB_DROPLOCKS:
1723 gfs2_gl_hash_clear(sdp, NO_WAIT);
1724 gfs2_quota_scan(sdp);
1728 gfs2_assert_warn(sdp, 0);
1734 * demote_ok - Check to see if it's ok to unlock a glock
1737 * Returns: 1 if it's ok
1740 static int demote_ok(struct gfs2_glock *gl)
1742 const struct gfs2_glock_operations *glops = gl->gl_ops;
1745 if (test_bit(GLF_STICKY, &gl->gl_flags))
1747 else if (glops->go_demote_ok)
1748 demote = glops->go_demote_ok(gl);
1754 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1759 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1761 struct gfs2_sbd *sdp = gl->gl_sbd;
1763 spin_lock(&sdp->sd_reclaim_lock);
1764 if (list_empty(&gl->gl_reclaim)) {
1765 gfs2_glock_hold(gl);
1766 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1767 atomic_inc(&sdp->sd_reclaim_count);
1769 spin_unlock(&sdp->sd_reclaim_lock);
1771 wake_up(&sdp->sd_reclaim_wq);
1775 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1776 * @sdp: the filesystem
1778 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1779 * different glock and we notice that there are a lot of glocks in the
1784 void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1786 struct gfs2_glock *gl;
1788 spin_lock(&sdp->sd_reclaim_lock);
1789 if (list_empty(&sdp->sd_reclaim_list)) {
1790 spin_unlock(&sdp->sd_reclaim_lock);
1793 gl = list_entry(sdp->sd_reclaim_list.next,
1794 struct gfs2_glock, gl_reclaim);
1795 list_del_init(&gl->gl_reclaim);
1796 spin_unlock(&sdp->sd_reclaim_lock);
1798 atomic_dec(&sdp->sd_reclaim_count);
1799 atomic_inc(&sdp->sd_reclaimed);
1801 if (gfs2_glmutex_trylock(gl)) {
1802 if (queue_empty(gl, &gl->gl_holders) &&
1803 gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
1804 handle_callback(gl, LM_ST_UNLOCKED);
1805 gfs2_glmutex_unlock(gl);
1812 * examine_bucket - Call a function for glock in a hash bucket
1813 * @examiner: the function
1814 * @sdp: the filesystem
1815 * @bucket: the bucket
1817 * Returns: 1 if the bucket has entries
1820 static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
1823 struct gfs2_glock *gl, *prev = NULL;
1824 int has_entries = 0;
1825 struct hlist_head *head = &gl_hash_table[hash].hb_list;
1827 read_lock(gl_lock_addr(hash));
1828 /* Can't use hlist_for_each_entry - don't want prefetch here */
1829 if (hlist_empty(head))
1831 gl = list_entry(head->first, struct gfs2_glock, gl_list);
1833 if (gl->gl_sbd == sdp) {
1834 gfs2_glock_hold(gl);
1835 read_unlock(gl_lock_addr(hash));
1837 gfs2_glock_put(prev);
1841 read_lock(gl_lock_addr(hash));
1843 if (gl->gl_list.next == NULL)
1845 gl = list_entry(gl->gl_list.next, struct gfs2_glock, gl_list);
1848 read_unlock(gl_lock_addr(hash));
1850 gfs2_glock_put(prev);
1855 * scan_glock - look at a glock and see if we can reclaim it
1856 * @gl: the glock to look at
1860 static void scan_glock(struct gfs2_glock *gl)
1862 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object)
1865 if (gfs2_glmutex_trylock(gl)) {
1866 if (queue_empty(gl, &gl->gl_holders) &&
1867 gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
1869 gfs2_glmutex_unlock(gl);
1874 gfs2_glmutex_unlock(gl);
1875 gfs2_glock_schedule_for_reclaim(gl);
1879 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
1880 * @sdp: the filesystem
1884 void gfs2_scand_internal(struct gfs2_sbd *sdp)
1888 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
1889 examine_bucket(scan_glock, sdp, x);
1893 * clear_glock - look at a glock and see if we can free it from glock cache
1894 * @gl: the glock to look at
1898 static void clear_glock(struct gfs2_glock *gl)
1900 struct gfs2_sbd *sdp = gl->gl_sbd;
1903 spin_lock(&sdp->sd_reclaim_lock);
1904 if (!list_empty(&gl->gl_reclaim)) {
1905 list_del_init(&gl->gl_reclaim);
1906 atomic_dec(&sdp->sd_reclaim_count);
1907 spin_unlock(&sdp->sd_reclaim_lock);
1908 released = gfs2_glock_put(gl);
1909 gfs2_assert(sdp, !released);
1911 spin_unlock(&sdp->sd_reclaim_lock);
1914 if (gfs2_glmutex_trylock(gl)) {
1915 if (queue_empty(gl, &gl->gl_holders) &&
1916 gl->gl_state != LM_ST_UNLOCKED)
1917 handle_callback(gl, LM_ST_UNLOCKED);
1918 gfs2_glmutex_unlock(gl);
1923 * gfs2_gl_hash_clear - Empty out the glock hash table
1924 * @sdp: the filesystem
1925 * @wait: wait until it's all gone
1927 * Called when unmounting the filesystem, or when inter-node lock manager
1928 * requests DROPLOCKS because it is running out of capacity.
1931 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
1941 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
1942 if (examine_bucket(clear_glock, sdp, x))
1949 if (time_after_eq(jiffies,
1950 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
1951 fs_warn(sdp, "Unmount seems to be stalled. "
1952 "Dumping lock state...\n");
1953 gfs2_dump_lockstate(sdp);
1957 invalidate_inodes(sdp->sd_vfs);
1963 * Diagnostic routines to help debug distributed deadlock
1967 * dump_holder - print information about a glock holder
1968 * @str: a string naming the type of holder
1969 * @gh: the glock holder
1971 * Returns: 0 on success, -ENOBUFS when we run out of space
1974 static int dump_holder(char *str, struct gfs2_holder *gh)
1977 int error = -ENOBUFS;
1979 printk(KERN_INFO " %s\n", str);
1980 printk(KERN_INFO " owner = %ld\n",
1981 (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
1982 printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
1983 printk(KERN_INFO " gh_flags =");
1984 for (x = 0; x < 32; x++)
1985 if (gh->gh_flags & (1 << x))
1988 printk(KERN_INFO " error = %d\n", gh->gh_error);
1989 printk(KERN_INFO " gh_iflags =");
1990 for (x = 0; x < 32; x++)
1991 if (test_bit(x, &gh->gh_iflags))
1994 print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip);
2002 * dump_inode - print information about an inode
2005 * Returns: 0 on success, -ENOBUFS when we run out of space
2008 static int dump_inode(struct gfs2_inode *ip)
2011 int error = -ENOBUFS;
2013 printk(KERN_INFO " Inode:\n");
2014 printk(KERN_INFO " num = %llu %llu\n",
2015 (unsigned long long)ip->i_num.no_formal_ino,
2016 (unsigned long long)ip->i_num.no_addr);
2017 printk(KERN_INFO " type = %u\n", IF2DT(ip->i_inode.i_mode));
2018 printk(KERN_INFO " i_flags =");
2019 for (x = 0; x < 32; x++)
2020 if (test_bit(x, &ip->i_flags))
2030 * dump_glock - print information about a glock
2032 * @count: where we are in the buffer
2034 * Returns: 0 on success, -ENOBUFS when we run out of space
2037 static int dump_glock(struct gfs2_glock *gl)
2039 struct gfs2_holder *gh;
2041 int error = -ENOBUFS;
2043 spin_lock(&gl->gl_spin);
2045 printk(KERN_INFO "Glock 0x%p (%u, %llu)\n", gl, gl->gl_name.ln_type,
2046 (unsigned long long)gl->gl_name.ln_number);
2047 printk(KERN_INFO " gl_flags =");
2048 for (x = 0; x < 32; x++) {
2049 if (test_bit(x, &gl->gl_flags))
2053 printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref));
2054 printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
2055 printk(KERN_INFO " gl_owner = %s\n", gl->gl_owner->comm);
2056 print_symbol(KERN_INFO " gl_ip = %s\n", gl->gl_ip);
2057 printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
2058 printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
2059 printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
2060 printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
2061 printk(KERN_INFO " le = %s\n",
2062 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
2063 printk(KERN_INFO " reclaim = %s\n",
2064 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
2066 printk(KERN_INFO " aspace = 0x%p nrpages = %lu\n", gl->gl_aspace,
2067 gl->gl_aspace->i_mapping->nrpages);
2069 printk(KERN_INFO " aspace = no\n");
2070 printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
2071 if (gl->gl_req_gh) {
2072 error = dump_holder("Request", gl->gl_req_gh);
2076 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
2077 error = dump_holder("Holder", gh);
2081 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
2082 error = dump_holder("Waiter1", gh);
2086 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
2087 error = dump_holder("Waiter2", gh);
2091 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
2092 error = dump_holder("Waiter3", gh);
2096 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
2097 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
2098 list_empty(&gl->gl_holders)) {
2099 error = dump_inode(gl->gl_object);
2104 printk(KERN_INFO " Inode: busy\n");
2111 spin_unlock(&gl->gl_spin);
2116 * gfs2_dump_lockstate - print out the current lockstate
2117 * @sdp: the filesystem
2118 * @ub: the buffer to copy the information into
2120 * If @ub is NULL, dump the lockstate to the console.
2124 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
2126 struct gfs2_glock *gl;
2127 struct hlist_node *h;
2131 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2133 read_lock(gl_lock_addr(x));
2135 hlist_for_each_entry(gl, h, &gl_hash_table[x].hb_list, gl_list) {
2136 if (gl->gl_sbd != sdp)
2139 error = dump_glock(gl);
2144 read_unlock(gl_lock_addr(x));
2154 int __init gfs2_glock_init(void)
2157 for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
2158 INIT_HLIST_HEAD(&gl_hash_table[i].hb_list);
2160 #ifdef GL_HASH_LOCK_SZ
2161 for(i = 0; i < GL_HASH_LOCK_SZ; i++) {
2162 rwlock_init(&gl_hash_locks[i]);