2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License v.2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/delay.h>
16 #include <linux/sort.h>
17 #include <linux/jhash.h>
18 #include <linux/kref.h>
19 #include <linux/kallsyms.h>
20 #include <linux/gfs2_ondisk.h>
21 #include <asm/semaphore.h>
22 #include <asm/uaccess.h>
25 #include "lm_interface.h"
37 /* Must be kept in sync with the beginning of struct gfs2_glock */
39 struct list_head gl_list;
40 unsigned long gl_flags;
44 struct gfs2_holder gr_gh;
45 struct work_struct gr_work;
48 typedef void (*glock_examiner) (struct gfs2_glock * gl);
50 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
53 * relaxed_state_ok - is a requested lock compatible with the current lock mode?
54 * @actual: the current state of the lock
55 * @requested: the lock state that was requested by the caller
56 * @flags: the modifier flags passed in by the caller
58 * Returns: 1 if the locks are compatible, 0 otherwise
61 static inline int relaxed_state_ok(unsigned int actual, unsigned requested,
64 if (actual == requested)
70 if (actual == LM_ST_EXCLUSIVE && requested == LM_ST_SHARED)
73 if (actual != LM_ST_UNLOCKED && (flags & LM_FLAG_ANY))
80 * gl_hash() - Turn glock number into hash bucket number
81 * @lock: The glock number
83 * Returns: The number of the corresponding hash bucket
86 static unsigned int gl_hash(struct lm_lockname *name)
90 h = jhash(&name->ln_number, sizeof(uint64_t), 0);
91 h = jhash(&name->ln_type, sizeof(unsigned int), h);
92 h &= GFS2_GL_HASH_MASK;
98 * glock_free() - Perform a few checks and then release struct gfs2_glock
99 * @gl: The glock to release
101 * Also calls lock module to release its internal structure for this glock.
105 static void glock_free(struct gfs2_glock *gl)
107 struct gfs2_sbd *sdp = gl->gl_sbd;
108 struct inode *aspace = gl->gl_aspace;
110 gfs2_lm_put_lock(sdp, gl->gl_lock);
113 gfs2_aspace_put(aspace);
115 kmem_cache_free(gfs2_glock_cachep, gl);
119 * gfs2_glock_hold() - increment reference count on glock
120 * @gl: The glock to hold
124 void gfs2_glock_hold(struct gfs2_glock *gl)
126 kref_get(&gl->gl_ref);
129 /* All work is done after the return from kref_put() so we
130 can release the write_lock before the free. */
132 static void kill_glock(struct kref *kref)
134 struct gfs2_glock *gl = container_of(kref, struct gfs2_glock, gl_ref);
135 struct gfs2_sbd *sdp = gl->gl_sbd;
137 gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED);
138 gfs2_assert(sdp, list_empty(&gl->gl_reclaim));
139 gfs2_assert(sdp, list_empty(&gl->gl_holders));
140 gfs2_assert(sdp, list_empty(&gl->gl_waiters1));
141 gfs2_assert(sdp, list_empty(&gl->gl_waiters2));
142 gfs2_assert(sdp, list_empty(&gl->gl_waiters3));
146 * gfs2_glock_put() - Decrement reference count on glock
147 * @gl: The glock to put
151 int gfs2_glock_put(struct gfs2_glock *gl)
153 struct gfs2_sbd *sdp = gl->gl_sbd;
154 struct gfs2_gl_hash_bucket *bucket = gl->gl_bucket;
157 mutex_lock(&sdp->sd_invalidate_inodes_mutex);
159 write_lock(&bucket->hb_lock);
160 if (kref_put(&gl->gl_ref, kill_glock)) {
161 list_del_init(&gl->gl_list);
162 write_unlock(&bucket->hb_lock);
163 BUG_ON(spin_is_locked(&gl->gl_spin));
168 write_unlock(&bucket->hb_lock);
170 mutex_unlock(&sdp->sd_invalidate_inodes_mutex);
175 * queue_empty - check to see if a glock's queue is empty
177 * @head: the head of the queue to check
179 * This function protects the list in the event that a process already
180 * has a holder on the list and is adding a second holder for itself.
181 * The glmutex lock is what generally prevents processes from working
182 * on the same glock at once, but the special case of adding a second
183 * holder for yourself ("recursive" locking) doesn't involve locking
184 * glmutex, making the spin lock necessary.
186 * Returns: 1 if the queue is empty
189 static inline int queue_empty(struct gfs2_glock *gl, struct list_head *head)
192 spin_lock(&gl->gl_spin);
193 empty = list_empty(head);
194 spin_unlock(&gl->gl_spin);
199 * search_bucket() - Find struct gfs2_glock by lock number
200 * @bucket: the bucket to search
201 * @name: The lock name
203 * Returns: NULL, or the struct gfs2_glock with the requested number
206 static struct gfs2_glock *search_bucket(struct gfs2_gl_hash_bucket *bucket,
207 struct lm_lockname *name)
209 struct gfs2_glock *gl;
211 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
212 if (test_bit(GLF_PLUG, &gl->gl_flags))
214 if (!lm_name_equal(&gl->gl_name, name))
217 kref_get(&gl->gl_ref);
226 * gfs2_glock_find() - Find glock by lock number
227 * @sdp: The GFS2 superblock
228 * @name: The lock name
230 * Returns: NULL, or the struct gfs2_glock with the requested number
233 static struct gfs2_glock *gfs2_glock_find(struct gfs2_sbd *sdp,
234 struct lm_lockname *name)
236 struct gfs2_gl_hash_bucket *bucket = &sdp->sd_gl_hash[gl_hash(name)];
237 struct gfs2_glock *gl;
239 read_lock(&bucket->hb_lock);
240 gl = search_bucket(bucket, name);
241 read_unlock(&bucket->hb_lock);
247 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
248 * @sdp: The GFS2 superblock
249 * @number: the lock number
250 * @glops: The glock_operations to use
251 * @create: If 0, don't create the glock if it doesn't exist
252 * @glp: the glock is returned here
254 * This does not lock a glock, just finds/creates structures for one.
259 int gfs2_glock_get(struct gfs2_sbd *sdp, uint64_t number,
260 struct gfs2_glock_operations *glops, int create,
261 struct gfs2_glock **glp)
263 struct lm_lockname name;
264 struct gfs2_glock *gl, *tmp;
265 struct gfs2_gl_hash_bucket *bucket;
268 name.ln_number = number;
269 name.ln_type = glops->go_type;
270 bucket = &sdp->sd_gl_hash[gl_hash(&name)];
272 read_lock(&bucket->hb_lock);
273 gl = search_bucket(bucket, &name);
274 read_unlock(&bucket->hb_lock);
281 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
285 memset(gl, 0, sizeof(struct gfs2_glock));
287 INIT_LIST_HEAD(&gl->gl_list);
289 kref_init(&gl->gl_ref);
291 spin_lock_init(&gl->gl_spin);
293 gl->gl_state = LM_ST_UNLOCKED;
294 INIT_LIST_HEAD(&gl->gl_holders);
295 INIT_LIST_HEAD(&gl->gl_waiters1);
296 INIT_LIST_HEAD(&gl->gl_waiters2);
297 INIT_LIST_HEAD(&gl->gl_waiters3);
301 gl->gl_bucket = bucket;
302 INIT_LIST_HEAD(&gl->gl_reclaim);
306 lops_init_le(&gl->gl_le, &gfs2_glock_lops);
307 INIT_LIST_HEAD(&gl->gl_ail_list);
309 /* If this glock protects actual on-disk data or metadata blocks,
310 create a VFS inode to manage the pages/buffers holding them. */
311 if (glops == &gfs2_inode_glops ||
312 glops == &gfs2_rgrp_glops ||
313 glops == &gfs2_meta_glops) {
314 gl->gl_aspace = gfs2_aspace_get(sdp);
315 if (!gl->gl_aspace) {
321 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
325 write_lock(&bucket->hb_lock);
326 tmp = search_bucket(bucket, &name);
328 write_unlock(&bucket->hb_lock);
332 list_add_tail(&gl->gl_list, &bucket->hb_list);
333 write_unlock(&bucket->hb_lock);
342 gfs2_aspace_put(gl->gl_aspace);
345 kmem_cache_free(gfs2_glock_cachep, gl);
351 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
353 * @state: the state we're requesting
354 * @flags: the modifier flags
355 * @gh: the holder structure
359 void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
360 struct gfs2_holder *gh)
362 INIT_LIST_HEAD(&gh->gh_list);
364 gh->gh_ip = (unsigned long)__builtin_return_address(0);
365 gh->gh_owner = current;
366 gh->gh_state = state;
367 gh->gh_flags = flags;
370 init_completion(&gh->gh_wait);
372 if (gh->gh_state == LM_ST_EXCLUSIVE)
373 gh->gh_flags |= GL_LOCAL_EXCL;
379 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
380 * @state: the state we're requesting
381 * @flags: the modifier flags
382 * @gh: the holder structure
384 * Don't mess with the glock.
388 void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
390 gh->gh_state = state;
391 gh->gh_flags = flags;
392 if (gh->gh_state == LM_ST_EXCLUSIVE)
393 gh->gh_flags |= GL_LOCAL_EXCL;
395 gh->gh_iflags &= 1 << HIF_ALLOCED;
396 gh->gh_ip = (unsigned long)__builtin_return_address(0);
400 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
401 * @gh: the holder structure
405 void gfs2_holder_uninit(struct gfs2_holder *gh)
407 gfs2_glock_put(gh->gh_gl);
413 * gfs2_holder_get - get a struct gfs2_holder structure
415 * @state: the state we're requesting
416 * @flags: the modifier flags
417 * @gfp_flags: __GFP_NOFAIL
419 * Figure out how big an impact this function has. Either:
420 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
421 * 2) Leave it like it is
423 * Returns: the holder structure, NULL on ENOMEM
426 static struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl,
428 int flags, gfp_t gfp_flags)
430 struct gfs2_holder *gh;
432 gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags);
436 gfs2_holder_init(gl, state, flags, gh);
437 set_bit(HIF_ALLOCED, &gh->gh_iflags);
438 gh->gh_ip = (unsigned long)__builtin_return_address(0);
443 * gfs2_holder_put - get rid of a struct gfs2_holder structure
444 * @gh: the holder structure
448 static void gfs2_holder_put(struct gfs2_holder *gh)
450 gfs2_holder_uninit(gh);
455 * rq_mutex - process a mutex request in the queue
456 * @gh: the glock holder
458 * Returns: 1 if the queue is blocked
461 static int rq_mutex(struct gfs2_holder *gh)
463 struct gfs2_glock *gl = gh->gh_gl;
465 list_del_init(&gh->gh_list);
466 /* gh->gh_error never examined. */
467 set_bit(GLF_LOCK, &gl->gl_flags);
468 complete(&gh->gh_wait);
474 * rq_promote - process a promote request in the queue
475 * @gh: the glock holder
477 * Acquire a new inter-node lock, or change a lock state to more restrictive.
479 * Returns: 1 if the queue is blocked
482 static int rq_promote(struct gfs2_holder *gh)
484 struct gfs2_glock *gl = gh->gh_gl;
485 struct gfs2_sbd *sdp = gl->gl_sbd;
486 struct gfs2_glock_operations *glops = gl->gl_ops;
488 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
489 if (list_empty(&gl->gl_holders)) {
491 set_bit(GLF_LOCK, &gl->gl_flags);
492 spin_unlock(&gl->gl_spin);
494 if (atomic_read(&sdp->sd_reclaim_count) >
495 gfs2_tune_get(sdp, gt_reclaim_limit) &&
496 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
497 gfs2_reclaim_glock(sdp);
498 gfs2_reclaim_glock(sdp);
501 glops->go_xmote_th(gl, gh->gh_state,
504 spin_lock(&gl->gl_spin);
509 if (list_empty(&gl->gl_holders)) {
510 set_bit(HIF_FIRST, &gh->gh_iflags);
511 set_bit(GLF_LOCK, &gl->gl_flags);
513 struct gfs2_holder *next_gh;
514 if (gh->gh_flags & GL_LOCAL_EXCL)
516 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
518 if (next_gh->gh_flags & GL_LOCAL_EXCL)
522 list_move_tail(&gh->gh_list, &gl->gl_holders);
524 set_bit(HIF_HOLDER, &gh->gh_iflags);
526 complete(&gh->gh_wait);
532 * rq_demote - process a demote request in the queue
533 * @gh: the glock holder
535 * Returns: 1 if the queue is blocked
538 static int rq_demote(struct gfs2_holder *gh)
540 struct gfs2_glock *gl = gh->gh_gl;
541 struct gfs2_glock_operations *glops = gl->gl_ops;
543 if (!list_empty(&gl->gl_holders))
546 if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) {
547 list_del_init(&gh->gh_list);
549 spin_unlock(&gl->gl_spin);
550 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
553 complete(&gh->gh_wait);
554 spin_lock(&gl->gl_spin);
557 set_bit(GLF_LOCK, &gl->gl_flags);
558 spin_unlock(&gl->gl_spin);
560 if (gh->gh_state == LM_ST_UNLOCKED ||
561 gl->gl_state != LM_ST_EXCLUSIVE)
562 glops->go_drop_th(gl);
564 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
566 spin_lock(&gl->gl_spin);
573 * rq_greedy - process a queued request to drop greedy status
574 * @gh: the glock holder
576 * Returns: 1 if the queue is blocked
579 static int rq_greedy(struct gfs2_holder *gh)
581 struct gfs2_glock *gl = gh->gh_gl;
583 list_del_init(&gh->gh_list);
584 /* gh->gh_error never examined. */
585 clear_bit(GLF_GREEDY, &gl->gl_flags);
586 spin_unlock(&gl->gl_spin);
588 gfs2_holder_uninit(gh);
589 kfree(container_of(gh, struct greedy, gr_gh));
591 spin_lock(&gl->gl_spin);
597 * run_queue - process holder structures on a glock
601 static void run_queue(struct gfs2_glock *gl)
603 struct gfs2_holder *gh;
607 if (test_bit(GLF_LOCK, &gl->gl_flags))
610 if (!list_empty(&gl->gl_waiters1)) {
611 gh = list_entry(gl->gl_waiters1.next,
612 struct gfs2_holder, gh_list);
614 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
615 blocked = rq_mutex(gh);
617 gfs2_assert_warn(gl->gl_sbd, 0);
619 } else if (!list_empty(&gl->gl_waiters2) &&
620 !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) {
621 gh = list_entry(gl->gl_waiters2.next,
622 struct gfs2_holder, gh_list);
624 if (test_bit(HIF_DEMOTE, &gh->gh_iflags))
625 blocked = rq_demote(gh);
626 else if (test_bit(HIF_GREEDY, &gh->gh_iflags))
627 blocked = rq_greedy(gh);
629 gfs2_assert_warn(gl->gl_sbd, 0);
631 } else if (!list_empty(&gl->gl_waiters3)) {
632 gh = list_entry(gl->gl_waiters3.next,
633 struct gfs2_holder, gh_list);
635 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
636 blocked = rq_promote(gh);
638 gfs2_assert_warn(gl->gl_sbd, 0);
649 * gfs2_glmutex_lock - acquire a local lock on a glock
652 * Gives caller exclusive access to manipulate a glock structure.
655 void gfs2_glmutex_lock(struct gfs2_glock *gl)
657 struct gfs2_holder gh;
659 gfs2_holder_init(gl, 0, 0, &gh);
660 set_bit(HIF_MUTEX, &gh.gh_iflags);
662 spin_lock(&gl->gl_spin);
663 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
664 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
666 complete(&gh.gh_wait);
667 spin_unlock(&gl->gl_spin);
669 wait_for_completion(&gh.gh_wait);
670 gfs2_holder_uninit(&gh);
674 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
677 * Returns: 1 if the glock is acquired
680 static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
684 spin_lock(&gl->gl_spin);
685 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
687 spin_unlock(&gl->gl_spin);
693 * gfs2_glmutex_unlock - release a local lock on a glock
698 void gfs2_glmutex_unlock(struct gfs2_glock *gl)
700 spin_lock(&gl->gl_spin);
701 clear_bit(GLF_LOCK, &gl->gl_flags);
703 BUG_ON(!spin_is_locked(&gl->gl_spin));
704 spin_unlock(&gl->gl_spin);
708 * handle_callback - add a demote request to a lock's queue
710 * @state: the state the caller wants us to change to
714 static void handle_callback(struct gfs2_glock *gl, unsigned int state)
716 struct gfs2_holder *gh, *new_gh = NULL;
719 spin_lock(&gl->gl_spin);
721 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
722 if (test_bit(HIF_DEMOTE, &gh->gh_iflags) &&
723 gl->gl_req_gh != gh) {
724 if (gh->gh_state != state)
725 gh->gh_state = LM_ST_UNLOCKED;
731 list_add_tail(&new_gh->gh_list, &gl->gl_waiters2);
734 spin_unlock(&gl->gl_spin);
736 new_gh = gfs2_holder_get(gl, state, LM_FLAG_TRY,
737 GFP_KERNEL | __GFP_NOFAIL),
738 set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
739 set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
745 spin_unlock(&gl->gl_spin);
748 gfs2_holder_put(new_gh);
752 * state_change - record that the glock is now in a different state
754 * @new_state the new state
758 static void state_change(struct gfs2_glock *gl, unsigned int new_state)
762 held1 = (gl->gl_state != LM_ST_UNLOCKED);
763 held2 = (new_state != LM_ST_UNLOCKED);
765 if (held1 != held2) {
772 gl->gl_state = new_state;
776 * xmote_bh - Called after the lock module is done acquiring a lock
777 * @gl: The glock in question
778 * @ret: the int returned from the lock module
782 static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
784 struct gfs2_sbd *sdp = gl->gl_sbd;
785 struct gfs2_glock_operations *glops = gl->gl_ops;
786 struct gfs2_holder *gh = gl->gl_req_gh;
787 int prev_state = gl->gl_state;
790 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
791 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
792 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
794 state_change(gl, ret & LM_OUT_ST_MASK);
796 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
798 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
799 } else if (gl->gl_state == LM_ST_DEFERRED) {
800 /* We might not want to do this here.
801 Look at moving to the inode glops. */
803 glops->go_inval(gl, DIO_DATA);
806 /* Deal with each possible exit condition */
809 gl->gl_stamp = jiffies;
811 else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
812 spin_lock(&gl->gl_spin);
813 list_del_init(&gh->gh_list);
815 spin_unlock(&gl->gl_spin);
817 } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
818 spin_lock(&gl->gl_spin);
819 list_del_init(&gh->gh_list);
820 if (gl->gl_state == gh->gh_state ||
821 gl->gl_state == LM_ST_UNLOCKED)
824 if (gfs2_assert_warn(sdp, gh->gh_flags &
825 (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
826 fs_warn(sdp, "ret = 0x%.8X\n", ret);
827 gh->gh_error = GLR_TRYFAILED;
829 spin_unlock(&gl->gl_spin);
831 if (ret & LM_OUT_CANCELED)
832 handle_callback(gl, LM_ST_UNLOCKED); /* Lame */
834 } else if (ret & LM_OUT_CANCELED) {
835 spin_lock(&gl->gl_spin);
836 list_del_init(&gh->gh_list);
837 gh->gh_error = GLR_CANCELED;
838 spin_unlock(&gl->gl_spin);
840 } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
841 spin_lock(&gl->gl_spin);
842 list_move_tail(&gh->gh_list, &gl->gl_holders);
844 set_bit(HIF_HOLDER, &gh->gh_iflags);
845 spin_unlock(&gl->gl_spin);
847 set_bit(HIF_FIRST, &gh->gh_iflags);
851 } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
852 spin_lock(&gl->gl_spin);
853 list_del_init(&gh->gh_list);
854 gh->gh_error = GLR_TRYFAILED;
855 spin_unlock(&gl->gl_spin);
858 if (gfs2_assert_withdraw(sdp, 0) == -1)
859 fs_err(sdp, "ret = 0x%.8X\n", ret);
862 if (glops->go_xmote_bh)
863 glops->go_xmote_bh(gl);
866 spin_lock(&gl->gl_spin);
867 gl->gl_req_gh = NULL;
868 gl->gl_req_bh = NULL;
869 clear_bit(GLF_LOCK, &gl->gl_flags);
871 spin_unlock(&gl->gl_spin);
877 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
880 complete(&gh->gh_wait);
885 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
886 * @gl: The glock in question
887 * @state: the requested state
888 * @flags: modifier flags to the lock call
892 void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags)
894 struct gfs2_sbd *sdp = gl->gl_sbd;
895 struct gfs2_glock_operations *glops = gl->gl_ops;
896 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
897 LM_FLAG_NOEXP | LM_FLAG_ANY |
899 unsigned int lck_ret;
901 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
902 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
903 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
904 gfs2_assert_warn(sdp, state != gl->gl_state);
906 if (gl->gl_state == LM_ST_EXCLUSIVE) {
909 DIO_METADATA | DIO_DATA | DIO_RELEASE);
913 gl->gl_req_bh = xmote_bh;
915 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state,
918 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
921 if (lck_ret & LM_OUT_ASYNC)
922 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
924 xmote_bh(gl, lck_ret);
928 * drop_bh - Called after a lock module unlock completes
930 * @ret: the return status
932 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
933 * Doesn't drop the reference on the glock the top half took out
937 static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
939 struct gfs2_sbd *sdp = gl->gl_sbd;
940 struct gfs2_glock_operations *glops = gl->gl_ops;
941 struct gfs2_holder *gh = gl->gl_req_gh;
943 clear_bit(GLF_PREFETCH, &gl->gl_flags);
945 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
946 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
947 gfs2_assert_warn(sdp, !ret);
949 state_change(gl, LM_ST_UNLOCKED);
952 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
955 spin_lock(&gl->gl_spin);
956 list_del_init(&gh->gh_list);
958 spin_unlock(&gl->gl_spin);
961 if (glops->go_drop_bh)
962 glops->go_drop_bh(gl);
964 spin_lock(&gl->gl_spin);
965 gl->gl_req_gh = NULL;
966 gl->gl_req_bh = NULL;
967 clear_bit(GLF_LOCK, &gl->gl_flags);
969 spin_unlock(&gl->gl_spin);
974 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
977 complete(&gh->gh_wait);
982 * gfs2_glock_drop_th - call into the lock module to unlock a lock
987 void gfs2_glock_drop_th(struct gfs2_glock *gl)
989 struct gfs2_sbd *sdp = gl->gl_sbd;
990 struct gfs2_glock_operations *glops = gl->gl_ops;
993 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
994 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
995 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
997 if (gl->gl_state == LM_ST_EXCLUSIVE) {
1000 DIO_METADATA | DIO_DATA | DIO_RELEASE);
1003 gfs2_glock_hold(gl);
1004 gl->gl_req_bh = drop_bh;
1006 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
1008 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
1014 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
1018 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1019 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1021 * Don't cancel GL_NOCANCEL requests.
1024 static void do_cancels(struct gfs2_holder *gh)
1026 struct gfs2_glock *gl = gh->gh_gl;
1028 spin_lock(&gl->gl_spin);
1030 while (gl->gl_req_gh != gh &&
1031 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1032 !list_empty(&gh->gh_list)) {
1033 if (gl->gl_req_bh &&
1035 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
1036 spin_unlock(&gl->gl_spin);
1037 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
1039 spin_lock(&gl->gl_spin);
1041 spin_unlock(&gl->gl_spin);
1043 spin_lock(&gl->gl_spin);
1047 spin_unlock(&gl->gl_spin);
1051 * glock_wait_internal - wait on a glock acquisition
1052 * @gh: the glock holder
1054 * Returns: 0 on success
1057 static int glock_wait_internal(struct gfs2_holder *gh)
1059 struct gfs2_glock *gl = gh->gh_gl;
1060 struct gfs2_sbd *sdp = gl->gl_sbd;
1061 struct gfs2_glock_operations *glops = gl->gl_ops;
1063 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1066 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1067 spin_lock(&gl->gl_spin);
1068 if (gl->gl_req_gh != gh &&
1069 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1070 !list_empty(&gh->gh_list)) {
1071 list_del_init(&gh->gh_list);
1072 gh->gh_error = GLR_TRYFAILED;
1074 spin_unlock(&gl->gl_spin);
1075 return gh->gh_error;
1077 spin_unlock(&gl->gl_spin);
1080 if (gh->gh_flags & LM_FLAG_PRIORITY)
1083 wait_for_completion(&gh->gh_wait);
1086 return gh->gh_error;
1088 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
1089 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state,
1093 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1094 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1096 if (glops->go_lock) {
1097 gh->gh_error = glops->go_lock(gh);
1099 spin_lock(&gl->gl_spin);
1100 list_del_init(&gh->gh_list);
1101 spin_unlock(&gl->gl_spin);
1105 spin_lock(&gl->gl_spin);
1106 gl->gl_req_gh = NULL;
1107 gl->gl_req_bh = NULL;
1108 clear_bit(GLF_LOCK, &gl->gl_flags);
1110 spin_unlock(&gl->gl_spin);
1113 return gh->gh_error;
1116 static inline struct gfs2_holder *
1117 find_holder_by_owner(struct list_head *head, struct task_struct *owner)
1119 struct gfs2_holder *gh;
1121 list_for_each_entry(gh, head, gh_list) {
1122 if (gh->gh_owner == owner)
1130 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1131 * @gh: the holder structure to add
1135 static void add_to_queue(struct gfs2_holder *gh)
1137 struct gfs2_glock *gl = gh->gh_gl;
1138 struct gfs2_holder *existing;
1140 BUG_ON(!gh->gh_owner);
1142 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
1144 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1145 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1149 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
1151 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1152 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1156 if (gh->gh_flags & LM_FLAG_PRIORITY)
1157 list_add(&gh->gh_list, &gl->gl_waiters3);
1159 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1163 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1164 * @gh: the holder structure
1166 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1168 * Returns: 0, GLR_TRYFAILED, or errno on failure
1171 int gfs2_glock_nq(struct gfs2_holder *gh)
1173 struct gfs2_glock *gl = gh->gh_gl;
1174 struct gfs2_sbd *sdp = gl->gl_sbd;
1178 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1179 set_bit(HIF_ABORTED, &gh->gh_iflags);
1183 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1185 spin_lock(&gl->gl_spin);
1188 spin_unlock(&gl->gl_spin);
1190 if (!(gh->gh_flags & GL_ASYNC)) {
1191 error = glock_wait_internal(gh);
1192 if (error == GLR_CANCELED) {
1198 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1204 * gfs2_glock_poll - poll to see if an async request has been completed
1207 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1210 int gfs2_glock_poll(struct gfs2_holder *gh)
1212 struct gfs2_glock *gl = gh->gh_gl;
1215 spin_lock(&gl->gl_spin);
1217 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1219 else if (list_empty(&gh->gh_list)) {
1220 if (gh->gh_error == GLR_CANCELED) {
1221 spin_unlock(&gl->gl_spin);
1223 if (gfs2_glock_nq(gh))
1230 spin_unlock(&gl->gl_spin);
1236 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1237 * @gh: the holder structure
1239 * Returns: 0, GLR_TRYFAILED, or errno on failure
1242 int gfs2_glock_wait(struct gfs2_holder *gh)
1246 error = glock_wait_internal(gh);
1247 if (error == GLR_CANCELED) {
1249 gh->gh_flags &= ~GL_ASYNC;
1250 error = gfs2_glock_nq(gh);
1257 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1258 * @gh: the glock holder
1262 void gfs2_glock_dq(struct gfs2_holder *gh)
1264 struct gfs2_glock *gl = gh->gh_gl;
1265 struct gfs2_glock_operations *glops = gl->gl_ops;
1267 if (gh->gh_flags & GL_SYNC)
1268 set_bit(GLF_SYNC, &gl->gl_flags);
1270 if (gh->gh_flags & GL_NOCACHE)
1271 handle_callback(gl, LM_ST_UNLOCKED);
1273 gfs2_glmutex_lock(gl);
1275 spin_lock(&gl->gl_spin);
1276 list_del_init(&gh->gh_list);
1278 if (list_empty(&gl->gl_holders)) {
1279 spin_unlock(&gl->gl_spin);
1281 if (glops->go_unlock)
1282 glops->go_unlock(gh);
1284 if (test_bit(GLF_SYNC, &gl->gl_flags)) {
1286 glops->go_sync(gl, DIO_METADATA | DIO_DATA);
1289 gl->gl_stamp = jiffies;
1291 spin_lock(&gl->gl_spin);
1294 clear_bit(GLF_LOCK, &gl->gl_flags);
1296 spin_unlock(&gl->gl_spin);
1300 * gfs2_glock_prefetch - Try to prefetch a glock
1302 * @state: the state to prefetch in
1303 * @flags: flags passed to go_xmote_th()
1307 static void gfs2_glock_prefetch(struct gfs2_glock *gl, unsigned int state,
1310 struct gfs2_glock_operations *glops = gl->gl_ops;
1312 spin_lock(&gl->gl_spin);
1314 if (test_bit(GLF_LOCK, &gl->gl_flags) ||
1315 !list_empty(&gl->gl_holders) ||
1316 !list_empty(&gl->gl_waiters1) ||
1317 !list_empty(&gl->gl_waiters2) ||
1318 !list_empty(&gl->gl_waiters3) ||
1319 relaxed_state_ok(gl->gl_state, state, flags)) {
1320 spin_unlock(&gl->gl_spin);
1324 set_bit(GLF_PREFETCH, &gl->gl_flags);
1325 set_bit(GLF_LOCK, &gl->gl_flags);
1326 spin_unlock(&gl->gl_spin);
1328 glops->go_xmote_th(gl, state, flags);
1332 * gfs2_glock_force_drop - Force a glock to be uncached
1337 void gfs2_glock_force_drop(struct gfs2_glock *gl)
1339 struct gfs2_holder gh;
1341 gfs2_holder_init(gl, LM_ST_UNLOCKED, 0, &gh);
1342 set_bit(HIF_DEMOTE, &gh.gh_iflags);
1344 spin_lock(&gl->gl_spin);
1345 list_add_tail(&gh.gh_list, &gl->gl_waiters2);
1347 spin_unlock(&gl->gl_spin);
1349 wait_for_completion(&gh.gh_wait);
1350 gfs2_holder_uninit(&gh);
1354 static void greedy_work(void *data)
1356 struct greedy *gr = data;
1357 struct gfs2_holder *gh = &gr->gr_gh;
1358 struct gfs2_glock *gl = gh->gh_gl;
1359 struct gfs2_glock_operations *glops = gl->gl_ops;
1361 clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1363 if (glops->go_greedy)
1364 glops->go_greedy(gl);
1366 spin_lock(&gl->gl_spin);
1368 if (list_empty(&gl->gl_waiters2)) {
1369 clear_bit(GLF_GREEDY, &gl->gl_flags);
1370 spin_unlock(&gl->gl_spin);
1371 gfs2_holder_uninit(gh);
1374 gfs2_glock_hold(gl);
1375 list_add_tail(&gh->gh_list, &gl->gl_waiters2);
1377 spin_unlock(&gl->gl_spin);
1383 * gfs2_glock_be_greedy -
1387 * Returns: 0 if go_greedy will be called, 1 otherwise
1390 int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time)
1393 struct gfs2_holder *gh;
1396 gl->gl_sbd->sd_args.ar_localcaching ||
1397 test_and_set_bit(GLF_GREEDY, &gl->gl_flags))
1400 gr = kmalloc(sizeof(struct greedy), GFP_KERNEL);
1402 clear_bit(GLF_GREEDY, &gl->gl_flags);
1407 gfs2_holder_init(gl, 0, 0, gh);
1408 set_bit(HIF_GREEDY, &gh->gh_iflags);
1409 INIT_WORK(&gr->gr_work, greedy_work, gr);
1411 set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1412 schedule_delayed_work(&gr->gr_work, time);
1418 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1419 * @gh: the holder structure
1423 void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1426 gfs2_holder_uninit(gh);
1430 * gfs2_glock_nq_num - acquire a glock based on lock number
1431 * @sdp: the filesystem
1432 * @number: the lock number
1433 * @glops: the glock operations for the type of glock
1434 * @state: the state to acquire the glock in
1435 * @flags: modifier flags for the aquisition
1436 * @gh: the struct gfs2_holder
1441 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, uint64_t number,
1442 struct gfs2_glock_operations *glops, unsigned int state,
1443 int flags, struct gfs2_holder *gh)
1445 struct gfs2_glock *gl;
1448 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1450 error = gfs2_glock_nq_init(gl, state, flags, gh);
1458 * glock_compare - Compare two struct gfs2_glock structures for sorting
1459 * @arg_a: the first structure
1460 * @arg_b: the second structure
1464 static int glock_compare(const void *arg_a, const void *arg_b)
1466 struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
1467 struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
1468 struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1469 struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1472 if (a->ln_number > b->ln_number)
1474 else if (a->ln_number < b->ln_number)
1477 if (gh_a->gh_state == LM_ST_SHARED &&
1478 gh_b->gh_state == LM_ST_EXCLUSIVE)
1480 else if (!(gh_a->gh_flags & GL_LOCAL_EXCL) &&
1481 (gh_b->gh_flags & GL_LOCAL_EXCL))
1489 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1490 * @num_gh: the number of structures
1491 * @ghs: an array of struct gfs2_holder structures
1493 * Returns: 0 on success (all glocks acquired),
1494 * errno on failure (no glocks acquired)
1497 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1498 struct gfs2_holder **p)
1503 for (x = 0; x < num_gh; x++)
1506 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1508 for (x = 0; x < num_gh; x++) {
1509 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1511 error = gfs2_glock_nq(p[x]);
1514 gfs2_glock_dq(p[x]);
1523 * gfs2_glock_nq_m - acquire multiple glocks
1524 * @num_gh: the number of structures
1525 * @ghs: an array of struct gfs2_holder structures
1527 * Figure out how big an impact this function has. Either:
1528 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1529 * 2) Forget async stuff and just call nq_m_sync()
1530 * 3) Leave it like it is
1532 * Returns: 0 on success (all glocks acquired),
1533 * errno on failure (no glocks acquired)
1536 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1540 int borked = 0, serious = 0;
1547 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1548 return gfs2_glock_nq(ghs);
1551 e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1555 for (x = 0; x < num_gh; x++) {
1556 ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
1557 error = gfs2_glock_nq(&ghs[x]);
1566 for (x = 0; x < num_gh; x++) {
1567 error = e[x] = glock_wait_internal(&ghs[x]);
1570 if (error != GLR_TRYFAILED && error != GLR_CANCELED)
1580 for (x = 0; x < num_gh; x++)
1582 gfs2_glock_dq(&ghs[x]);
1587 for (x = 0; x < num_gh; x++)
1588 gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
1590 error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
1599 * gfs2_glock_dq_m - release multiple glocks
1600 * @num_gh: the number of structures
1601 * @ghs: an array of struct gfs2_holder structures
1605 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1609 for (x = 0; x < num_gh; x++)
1610 gfs2_glock_dq(&ghs[x]);
1614 * gfs2_glock_dq_uninit_m - release multiple glocks
1615 * @num_gh: the number of structures
1616 * @ghs: an array of struct gfs2_holder structures
1620 void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1624 for (x = 0; x < num_gh; x++)
1625 gfs2_glock_dq_uninit(&ghs[x]);
1629 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1630 * @sdp: the filesystem
1631 * @number: the lock number
1632 * @glops: the glock operations for the type of glock
1633 * @state: the state to acquire the glock in
1634 * @flags: modifier flags for the aquisition
1639 void gfs2_glock_prefetch_num(struct gfs2_sbd *sdp, uint64_t number,
1640 struct gfs2_glock_operations *glops,
1641 unsigned int state, int flags)
1643 struct gfs2_glock *gl;
1646 if (atomic_read(&sdp->sd_reclaim_count) <
1647 gfs2_tune_get(sdp, gt_reclaim_limit)) {
1648 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1650 gfs2_glock_prefetch(gl, state, flags);
1657 * gfs2_lvb_hold - attach a LVB from a glock
1658 * @gl: The glock in question
1662 int gfs2_lvb_hold(struct gfs2_glock *gl)
1666 gfs2_glmutex_lock(gl);
1668 if (!atomic_read(&gl->gl_lvb_count)) {
1669 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1671 gfs2_glmutex_unlock(gl);
1674 gfs2_glock_hold(gl);
1676 atomic_inc(&gl->gl_lvb_count);
1678 gfs2_glmutex_unlock(gl);
1684 * gfs2_lvb_unhold - detach a LVB from a glock
1685 * @gl: The glock in question
1689 void gfs2_lvb_unhold(struct gfs2_glock *gl)
1691 gfs2_glock_hold(gl);
1692 gfs2_glmutex_lock(gl);
1694 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1695 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1696 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1701 gfs2_glmutex_unlock(gl);
1706 void gfs2_lvb_sync(struct gfs2_glock *gl)
1708 gfs2_glmutex_lock(gl);
1710 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count));
1711 if (!gfs2_assert_warn(gl->gl_sbd, gfs2_glock_is_held_excl(gl)))
1712 gfs2_lm_sync_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1714 gfs2_glmutex_unlock(gl);
1718 static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1721 struct gfs2_glock *gl;
1723 gl = gfs2_glock_find(sdp, name);
1727 if (gl->gl_ops->go_callback)
1728 gl->gl_ops->go_callback(gl, state);
1729 handle_callback(gl, state);
1731 spin_lock(&gl->gl_spin);
1733 spin_unlock(&gl->gl_spin);
1739 * gfs2_glock_cb - Callback used by locking module
1740 * @fsdata: Pointer to the superblock
1741 * @type: Type of callback
1742 * @data: Type dependent data pointer
1744 * Called by the locking module when it wants to tell us something.
1745 * Either we need to drop a lock, one of our ASYNC requests completed, or
1746 * a journal from another client needs to be recovered.
1749 void gfs2_glock_cb(lm_fsdata_t *fsdata, unsigned int type, void *data)
1751 struct gfs2_sbd *sdp = (struct gfs2_sbd *)fsdata;
1755 blocking_cb(sdp, data, LM_ST_UNLOCKED);
1759 blocking_cb(sdp, data, LM_ST_DEFERRED);
1763 blocking_cb(sdp, data, LM_ST_SHARED);
1767 struct lm_async_cb *async = data;
1768 struct gfs2_glock *gl;
1770 gl = gfs2_glock_find(sdp, &async->lc_name);
1771 if (gfs2_assert_warn(sdp, gl))
1773 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1774 gl->gl_req_bh(gl, async->lc_ret);
1779 case LM_CB_NEED_RECOVERY:
1780 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1781 if (sdp->sd_recoverd_process)
1782 wake_up_process(sdp->sd_recoverd_process);
1785 case LM_CB_DROPLOCKS:
1786 gfs2_gl_hash_clear(sdp, NO_WAIT);
1787 gfs2_quota_scan(sdp);
1791 gfs2_assert_warn(sdp, 0);
1797 * gfs2_try_toss_inode - try to remove a particular inode struct from cache
1798 * sdp: the filesystem
1799 * inum: the inode number
1803 void gfs2_try_toss_inode(struct gfs2_sbd *sdp, struct gfs2_inum *inum)
1805 struct gfs2_glock *gl;
1806 struct gfs2_inode *ip;
1809 error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_inode_glops,
1814 if (!gfs2_glmutex_trylock(gl))
1821 if (atomic_read(&ip->i_count))
1824 gfs2_inode_destroy(ip, 1);
1827 gfs2_glmutex_unlock(gl);
1834 * gfs2_iopen_go_callback - Try to kick the inode/vnode associated with an
1835 * iopen glock from memory
1836 * @io_gl: the iopen glock
1837 * @state: the state into which the glock should be put
1841 void gfs2_iopen_go_callback(struct gfs2_glock *io_gl, unsigned int state)
1843 struct gfs2_glock *i_gl;
1845 if (state != LM_ST_UNLOCKED)
1848 spin_lock(&io_gl->gl_spin);
1849 i_gl = io_gl->gl_object;
1851 gfs2_glock_hold(i_gl);
1852 spin_unlock(&io_gl->gl_spin);
1854 spin_unlock(&io_gl->gl_spin);
1858 if (gfs2_glmutex_trylock(i_gl)) {
1859 struct gfs2_inode *ip = i_gl->gl_object;
1861 gfs2_try_toss_vnode(ip);
1862 gfs2_glmutex_unlock(i_gl);
1863 gfs2_glock_schedule_for_reclaim(i_gl);
1866 gfs2_glmutex_unlock(i_gl);
1870 gfs2_glock_put(i_gl);
1874 * demote_ok - Check to see if it's ok to unlock a glock
1877 * Returns: 1 if it's ok
1880 static int demote_ok(struct gfs2_glock *gl)
1882 struct gfs2_sbd *sdp = gl->gl_sbd;
1883 struct gfs2_glock_operations *glops = gl->gl_ops;
1886 if (test_bit(GLF_STICKY, &gl->gl_flags))
1888 else if (test_bit(GLF_PREFETCH, &gl->gl_flags))
1889 demote = time_after_eq(jiffies,
1891 gfs2_tune_get(sdp, gt_prefetch_secs) * HZ);
1892 else if (glops->go_demote_ok)
1893 demote = glops->go_demote_ok(gl);
1899 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1904 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1906 struct gfs2_sbd *sdp = gl->gl_sbd;
1908 spin_lock(&sdp->sd_reclaim_lock);
1909 if (list_empty(&gl->gl_reclaim)) {
1910 gfs2_glock_hold(gl);
1911 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1912 atomic_inc(&sdp->sd_reclaim_count);
1914 spin_unlock(&sdp->sd_reclaim_lock);
1916 wake_up(&sdp->sd_reclaim_wq);
1920 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1921 * @sdp: the filesystem
1923 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1924 * different glock and we notice that there are a lot of glocks in the
1929 void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1931 struct gfs2_glock *gl;
1933 spin_lock(&sdp->sd_reclaim_lock);
1934 if (list_empty(&sdp->sd_reclaim_list)) {
1935 spin_unlock(&sdp->sd_reclaim_lock);
1938 gl = list_entry(sdp->sd_reclaim_list.next,
1939 struct gfs2_glock, gl_reclaim);
1940 list_del_init(&gl->gl_reclaim);
1941 spin_unlock(&sdp->sd_reclaim_lock);
1943 atomic_dec(&sdp->sd_reclaim_count);
1944 atomic_inc(&sdp->sd_reclaimed);
1946 if (gfs2_glmutex_trylock(gl)) {
1947 if (gl->gl_ops == &gfs2_inode_glops) {
1948 struct gfs2_inode *ip = gl->gl_object;
1949 if (ip && !atomic_read(&ip->i_count))
1950 gfs2_inode_destroy(ip, 1);
1952 if (queue_empty(gl, &gl->gl_holders) &&
1953 gl->gl_state != LM_ST_UNLOCKED &&
1955 handle_callback(gl, LM_ST_UNLOCKED);
1956 gfs2_glmutex_unlock(gl);
1963 * examine_bucket - Call a function for glock in a hash bucket
1964 * @examiner: the function
1965 * @sdp: the filesystem
1966 * @bucket: the bucket
1968 * Returns: 1 if the bucket has entries
1971 static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
1972 struct gfs2_gl_hash_bucket *bucket)
1974 struct glock_plug plug;
1975 struct list_head *tmp;
1976 struct gfs2_glock *gl;
1979 /* Add "plug" to end of bucket list, work back up list from there */
1980 memset(&plug.gl_flags, 0, sizeof(unsigned long));
1981 set_bit(GLF_PLUG, &plug.gl_flags);
1983 write_lock(&bucket->hb_lock);
1984 list_add(&plug.gl_list, &bucket->hb_list);
1985 write_unlock(&bucket->hb_lock);
1988 write_lock(&bucket->hb_lock);
1991 tmp = plug.gl_list.next;
1993 if (tmp == &bucket->hb_list) {
1994 list_del(&plug.gl_list);
1995 entries = !list_empty(&bucket->hb_list);
1996 write_unlock(&bucket->hb_lock);
1999 gl = list_entry(tmp, struct gfs2_glock, gl_list);
2001 /* Move plug up list */
2002 list_move(&plug.gl_list, &gl->gl_list);
2004 if (test_bit(GLF_PLUG, &gl->gl_flags))
2007 /* examiner() must glock_put() */
2008 gfs2_glock_hold(gl);
2013 write_unlock(&bucket->hb_lock);
2020 * scan_glock - look at a glock and see if we can reclaim it
2021 * @gl: the glock to look at
2025 static void scan_glock(struct gfs2_glock *gl)
2027 if (gfs2_glmutex_trylock(gl)) {
2028 if (gl->gl_ops == &gfs2_inode_glops) {
2029 struct gfs2_inode *ip = gl->gl_object;
2030 if (ip && !atomic_read(&ip->i_count))
2033 if (queue_empty(gl, &gl->gl_holders) &&
2034 gl->gl_state != LM_ST_UNLOCKED &&
2038 gfs2_glmutex_unlock(gl);
2046 gfs2_glmutex_unlock(gl);
2047 gfs2_glock_schedule_for_reclaim(gl);
2052 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
2053 * @sdp: the filesystem
2057 void gfs2_scand_internal(struct gfs2_sbd *sdp)
2061 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2062 examine_bucket(scan_glock, sdp, &sdp->sd_gl_hash[x]);
2068 * clear_glock - look at a glock and see if we can free it from glock cache
2069 * @gl: the glock to look at
2073 static void clear_glock(struct gfs2_glock *gl)
2075 struct gfs2_sbd *sdp = gl->gl_sbd;
2078 spin_lock(&sdp->sd_reclaim_lock);
2079 if (!list_empty(&gl->gl_reclaim)) {
2080 list_del_init(&gl->gl_reclaim);
2081 atomic_dec(&sdp->sd_reclaim_count);
2082 spin_unlock(&sdp->sd_reclaim_lock);
2083 released = gfs2_glock_put(gl);
2084 gfs2_assert(sdp, !released);
2086 spin_unlock(&sdp->sd_reclaim_lock);
2089 if (gfs2_glmutex_trylock(gl)) {
2090 if (gl->gl_ops == &gfs2_inode_glops) {
2091 struct gfs2_inode *ip = gl->gl_object;
2092 if (ip && !atomic_read(&ip->i_count))
2093 gfs2_inode_destroy(ip, 1);
2095 if (queue_empty(gl, &gl->gl_holders) &&
2096 gl->gl_state != LM_ST_UNLOCKED)
2097 handle_callback(gl, LM_ST_UNLOCKED);
2099 gfs2_glmutex_unlock(gl);
2106 * gfs2_gl_hash_clear - Empty out the glock hash table
2107 * @sdp: the filesystem
2108 * @wait: wait until it's all gone
2110 * Called when unmounting the filesystem, or when inter-node lock manager
2111 * requests DROPLOCKS because it is running out of capacity.
2114 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
2125 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
2126 if (examine_bucket(clear_glock, sdp,
2127 &sdp->sd_gl_hash[x]))
2133 if (time_after_eq(jiffies,
2134 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
2135 fs_warn(sdp, "Unmount seems to be stalled. "
2136 "Dumping lock state...\n");
2137 gfs2_dump_lockstate(sdp);
2141 /* invalidate_inodes() requires that the sb inodes list
2142 not change, but an async completion callback for an
2143 unlock can occur which does glock_put() which
2144 can call iput() which will change the sb inodes list.
2145 invalidate_inodes_mutex prevents glock_put()'s during
2146 an invalidate_inodes() */
2148 mutex_lock(&sdp->sd_invalidate_inodes_mutex);
2149 invalidate_inodes(sdp->sd_vfs);
2150 mutex_unlock(&sdp->sd_invalidate_inodes_mutex);
2156 * Diagnostic routines to help debug distributed deadlock
2160 * dump_holder - print information about a glock holder
2161 * @str: a string naming the type of holder
2162 * @gh: the glock holder
2164 * Returns: 0 on success, -ENOBUFS when we run out of space
2167 static int dump_holder(char *str, struct gfs2_holder *gh)
2170 int error = -ENOBUFS;
2172 printk(KERN_INFO " %s\n", str);
2173 printk(KERN_INFO " owner = %ld\n",
2174 (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
2175 printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
2176 printk(KERN_INFO " gh_flags =");
2177 for (x = 0; x < 32; x++)
2178 if (gh->gh_flags & (1 << x))
2181 printk(KERN_INFO " error = %d\n", gh->gh_error);
2182 printk(KERN_INFO " gh_iflags =");
2183 for (x = 0; x < 32; x++)
2184 if (test_bit(x, &gh->gh_iflags))
2187 print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip);
2195 * dump_inode - print information about an inode
2198 * Returns: 0 on success, -ENOBUFS when we run out of space
2201 static int dump_inode(struct gfs2_inode *ip)
2204 int error = -ENOBUFS;
2206 printk(KERN_INFO " Inode:\n");
2207 printk(KERN_INFO " num = %llu %llu\n",
2208 ip->i_num.no_formal_ino, ip->i_num.no_addr);
2209 printk(KERN_INFO " type = %u\n", IF2DT(ip->i_di.di_mode));
2210 printk(KERN_INFO " i_count = %d\n", atomic_read(&ip->i_count));
2211 printk(KERN_INFO " i_flags =");
2212 for (x = 0; x < 32; x++)
2213 if (test_bit(x, &ip->i_flags))
2216 printk(KERN_INFO " vnode = %s\n", (ip->i_vnode) ? "yes" : "no");
2224 * dump_glock - print information about a glock
2226 * @count: where we are in the buffer
2228 * Returns: 0 on success, -ENOBUFS when we run out of space
2231 static int dump_glock(struct gfs2_glock *gl)
2233 struct gfs2_holder *gh;
2235 int error = -ENOBUFS;
2237 spin_lock(&gl->gl_spin);
2239 printk(KERN_INFO "Glock (%u, %llu)\n",
2240 gl->gl_name.ln_type,
2241 gl->gl_name.ln_number);
2242 printk(KERN_INFO " gl_flags =");
2243 for (x = 0; x < 32; x++)
2244 if (test_bit(x, &gl->gl_flags))
2247 printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref.refcount));
2248 printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
2249 printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
2250 printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
2251 printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
2252 printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
2253 printk(KERN_INFO " le = %s\n",
2254 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
2255 printk(KERN_INFO " reclaim = %s\n",
2256 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
2258 printk(KERN_INFO " aspace = %lu\n",
2259 gl->gl_aspace->i_mapping->nrpages);
2261 printk(KERN_INFO " aspace = no\n");
2262 printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
2263 if (gl->gl_req_gh) {
2264 error = dump_holder("Request", gl->gl_req_gh);
2268 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
2269 error = dump_holder("Holder", gh);
2273 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
2274 error = dump_holder("Waiter1", gh);
2278 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
2279 error = dump_holder("Waiter2", gh);
2283 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
2284 error = dump_holder("Waiter3", gh);
2288 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
2289 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
2290 list_empty(&gl->gl_holders)) {
2291 error = dump_inode(gl->gl_object);
2296 printk(KERN_INFO " Inode: busy\n");
2303 spin_unlock(&gl->gl_spin);
2309 * gfs2_dump_lockstate - print out the current lockstate
2310 * @sdp: the filesystem
2311 * @ub: the buffer to copy the information into
2313 * If @ub is NULL, dump the lockstate to the console.
2317 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
2319 struct gfs2_gl_hash_bucket *bucket;
2320 struct gfs2_glock *gl;
2324 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2325 bucket = &sdp->sd_gl_hash[x];
2327 read_lock(&bucket->hb_lock);
2329 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
2330 if (test_bit(GLF_PLUG, &gl->gl_flags))
2333 error = dump_glock(gl);
2338 read_unlock(&bucket->hb_lock);