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 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/uaccess.h>
24 #include "lm_interface.h"
36 /* Must be kept in sync with the beginning of struct gfs2_glock */
38 struct list_head gl_list;
39 unsigned long gl_flags;
43 struct gfs2_holder gr_gh;
44 struct work_struct gr_work;
47 typedef void (*glock_examiner) (struct gfs2_glock * gl);
49 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
50 static int dump_glock(struct gfs2_glock *gl);
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;
296 INIT_LIST_HEAD(&gl->gl_holders);
297 INIT_LIST_HEAD(&gl->gl_waiters1);
298 INIT_LIST_HEAD(&gl->gl_waiters2);
299 INIT_LIST_HEAD(&gl->gl_waiters3);
303 gl->gl_bucket = bucket;
304 INIT_LIST_HEAD(&gl->gl_reclaim);
308 lops_init_le(&gl->gl_le, &gfs2_glock_lops);
309 INIT_LIST_HEAD(&gl->gl_ail_list);
311 /* If this glock protects actual on-disk data or metadata blocks,
312 create a VFS inode to manage the pages/buffers holding them. */
313 if (glops == &gfs2_inode_glops ||
314 glops == &gfs2_rgrp_glops ||
315 glops == &gfs2_meta_glops) {
316 gl->gl_aspace = gfs2_aspace_get(sdp);
317 if (!gl->gl_aspace) {
323 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
327 write_lock(&bucket->hb_lock);
328 tmp = search_bucket(bucket, &name);
330 write_unlock(&bucket->hb_lock);
334 list_add_tail(&gl->gl_list, &bucket->hb_list);
335 write_unlock(&bucket->hb_lock);
344 gfs2_aspace_put(gl->gl_aspace);
347 kmem_cache_free(gfs2_glock_cachep, gl);
353 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
355 * @state: the state we're requesting
356 * @flags: the modifier flags
357 * @gh: the holder structure
361 void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
362 struct gfs2_holder *gh)
364 INIT_LIST_HEAD(&gh->gh_list);
366 gh->gh_ip = (unsigned long)__builtin_return_address(0);
367 gh->gh_owner = current;
368 gh->gh_state = state;
369 gh->gh_flags = flags;
372 init_completion(&gh->gh_wait);
374 if (gh->gh_state == LM_ST_EXCLUSIVE)
375 gh->gh_flags |= GL_LOCAL_EXCL;
381 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
382 * @state: the state we're requesting
383 * @flags: the modifier flags
384 * @gh: the holder structure
386 * Don't mess with the glock.
390 void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
392 gh->gh_state = state;
393 gh->gh_flags = flags;
394 if (gh->gh_state == LM_ST_EXCLUSIVE)
395 gh->gh_flags |= GL_LOCAL_EXCL;
397 gh->gh_iflags &= 1 << HIF_ALLOCED;
398 gh->gh_ip = (unsigned long)__builtin_return_address(0);
402 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
403 * @gh: the holder structure
407 void gfs2_holder_uninit(struct gfs2_holder *gh)
409 gfs2_glock_put(gh->gh_gl);
415 * gfs2_holder_get - get a struct gfs2_holder structure
417 * @state: the state we're requesting
418 * @flags: the modifier flags
421 * Figure out how big an impact this function has. Either:
422 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
423 * 2) Leave it like it is
425 * Returns: the holder structure, NULL on ENOMEM
428 static struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl,
430 int flags, gfp_t gfp_flags)
432 struct gfs2_holder *gh;
434 gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags);
438 gfs2_holder_init(gl, state, flags, gh);
439 set_bit(HIF_ALLOCED, &gh->gh_iflags);
440 gh->gh_ip = (unsigned long)__builtin_return_address(0);
445 * gfs2_holder_put - get rid of a struct gfs2_holder structure
446 * @gh: the holder structure
450 static void gfs2_holder_put(struct gfs2_holder *gh)
452 gfs2_holder_uninit(gh);
457 * rq_mutex - process a mutex request in the queue
458 * @gh: the glock holder
460 * Returns: 1 if the queue is blocked
463 static int rq_mutex(struct gfs2_holder *gh)
465 struct gfs2_glock *gl = gh->gh_gl;
467 list_del_init(&gh->gh_list);
468 /* gh->gh_error never examined. */
469 set_bit(GLF_LOCK, &gl->gl_flags);
470 complete(&gh->gh_wait);
476 * rq_promote - process a promote request in the queue
477 * @gh: the glock holder
479 * Acquire a new inter-node lock, or change a lock state to more restrictive.
481 * Returns: 1 if the queue is blocked
484 static int rq_promote(struct gfs2_holder *gh)
486 struct gfs2_glock *gl = gh->gh_gl;
487 struct gfs2_sbd *sdp = gl->gl_sbd;
488 struct gfs2_glock_operations *glops = gl->gl_ops;
490 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
491 if (list_empty(&gl->gl_holders)) {
493 set_bit(GLF_LOCK, &gl->gl_flags);
494 spin_unlock(&gl->gl_spin);
496 if (atomic_read(&sdp->sd_reclaim_count) >
497 gfs2_tune_get(sdp, gt_reclaim_limit) &&
498 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
499 gfs2_reclaim_glock(sdp);
500 gfs2_reclaim_glock(sdp);
503 glops->go_xmote_th(gl, gh->gh_state,
506 spin_lock(&gl->gl_spin);
511 if (list_empty(&gl->gl_holders)) {
512 set_bit(HIF_FIRST, &gh->gh_iflags);
513 set_bit(GLF_LOCK, &gl->gl_flags);
515 struct gfs2_holder *next_gh;
516 if (gh->gh_flags & GL_LOCAL_EXCL)
518 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
520 if (next_gh->gh_flags & GL_LOCAL_EXCL)
524 list_move_tail(&gh->gh_list, &gl->gl_holders);
526 set_bit(HIF_HOLDER, &gh->gh_iflags);
528 complete(&gh->gh_wait);
534 * rq_demote - process a demote request in the queue
535 * @gh: the glock holder
537 * Returns: 1 if the queue is blocked
540 static int rq_demote(struct gfs2_holder *gh)
542 struct gfs2_glock *gl = gh->gh_gl;
543 struct gfs2_glock_operations *glops = gl->gl_ops;
545 if (!list_empty(&gl->gl_holders))
548 if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) {
549 list_del_init(&gh->gh_list);
551 spin_unlock(&gl->gl_spin);
552 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
555 complete(&gh->gh_wait);
556 spin_lock(&gl->gl_spin);
559 set_bit(GLF_LOCK, &gl->gl_flags);
560 spin_unlock(&gl->gl_spin);
562 if (gh->gh_state == LM_ST_UNLOCKED ||
563 gl->gl_state != LM_ST_EXCLUSIVE)
564 glops->go_drop_th(gl);
566 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
568 spin_lock(&gl->gl_spin);
575 * rq_greedy - process a queued request to drop greedy status
576 * @gh: the glock holder
578 * Returns: 1 if the queue is blocked
581 static int rq_greedy(struct gfs2_holder *gh)
583 struct gfs2_glock *gl = gh->gh_gl;
585 list_del_init(&gh->gh_list);
586 /* gh->gh_error never examined. */
587 clear_bit(GLF_GREEDY, &gl->gl_flags);
588 spin_unlock(&gl->gl_spin);
590 gfs2_holder_uninit(gh);
591 kfree(container_of(gh, struct greedy, gr_gh));
593 spin_lock(&gl->gl_spin);
599 * run_queue - process holder structures on a glock
603 static void run_queue(struct gfs2_glock *gl)
605 struct gfs2_holder *gh;
609 if (test_bit(GLF_LOCK, &gl->gl_flags))
612 if (!list_empty(&gl->gl_waiters1)) {
613 gh = list_entry(gl->gl_waiters1.next,
614 struct gfs2_holder, gh_list);
616 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
617 blocked = rq_mutex(gh);
619 gfs2_assert_warn(gl->gl_sbd, 0);
621 } else if (!list_empty(&gl->gl_waiters2) &&
622 !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) {
623 gh = list_entry(gl->gl_waiters2.next,
624 struct gfs2_holder, gh_list);
626 if (test_bit(HIF_DEMOTE, &gh->gh_iflags))
627 blocked = rq_demote(gh);
628 else if (test_bit(HIF_GREEDY, &gh->gh_iflags))
629 blocked = rq_greedy(gh);
631 gfs2_assert_warn(gl->gl_sbd, 0);
633 } else if (!list_empty(&gl->gl_waiters3)) {
634 gh = list_entry(gl->gl_waiters3.next,
635 struct gfs2_holder, gh_list);
637 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
638 blocked = rq_promote(gh);
640 gfs2_assert_warn(gl->gl_sbd, 0);
651 * gfs2_glmutex_lock - acquire a local lock on a glock
654 * Gives caller exclusive access to manipulate a glock structure.
657 static void gfs2_glmutex_lock(struct gfs2_glock *gl)
659 struct gfs2_holder gh;
661 gfs2_holder_init(gl, 0, 0, &gh);
662 set_bit(HIF_MUTEX, &gh.gh_iflags);
664 spin_lock(&gl->gl_spin);
665 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
666 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
668 gl->gl_owner = current;
669 gl->gl_ip = (unsigned long)__builtin_return_address(0);
670 complete(&gh.gh_wait);
672 spin_unlock(&gl->gl_spin);
674 wait_for_completion(&gh.gh_wait);
675 gfs2_holder_uninit(&gh);
679 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
682 * Returns: 1 if the glock is acquired
685 static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
689 spin_lock(&gl->gl_spin);
690 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
693 gl->gl_owner = current;
694 gl->gl_ip = (unsigned long)__builtin_return_address(0);
696 spin_unlock(&gl->gl_spin);
702 * gfs2_glmutex_unlock - release a local lock on a glock
707 static void gfs2_glmutex_unlock(struct gfs2_glock *gl)
709 spin_lock(&gl->gl_spin);
710 clear_bit(GLF_LOCK, &gl->gl_flags);
714 BUG_ON(!spin_is_locked(&gl->gl_spin));
715 spin_unlock(&gl->gl_spin);
719 * handle_callback - add a demote request to a lock's queue
721 * @state: the state the caller wants us to change to
723 * Note: This may fail sliently if we are out of memory.
726 static void handle_callback(struct gfs2_glock *gl, unsigned int state)
728 struct gfs2_holder *gh, *new_gh = NULL;
731 spin_lock(&gl->gl_spin);
733 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
734 if (test_bit(HIF_DEMOTE, &gh->gh_iflags) &&
735 gl->gl_req_gh != gh) {
736 if (gh->gh_state != state)
737 gh->gh_state = LM_ST_UNLOCKED;
743 list_add_tail(&new_gh->gh_list, &gl->gl_waiters2);
746 spin_unlock(&gl->gl_spin);
748 new_gh = gfs2_holder_get(gl, state, LM_FLAG_TRY, GFP_KERNEL);
751 set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
752 set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
758 spin_unlock(&gl->gl_spin);
761 gfs2_holder_put(new_gh);
764 void gfs2_glock_inode_squish(struct inode *inode)
766 struct gfs2_holder gh;
767 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
768 gfs2_holder_init(gl, LM_ST_UNLOCKED, 0, &gh);
769 set_bit(HIF_DEMOTE, &gh.gh_iflags);
770 spin_lock(&gl->gl_spin);
771 gfs2_assert(inode->i_sb->s_fs_info, list_empty(&gl->gl_holders));
772 list_add_tail(&gh.gh_list, &gl->gl_waiters2);
774 spin_unlock(&gl->gl_spin);
775 gfs2_holder_uninit(&gh);
779 * state_change - record that the glock is now in a different state
781 * @new_state the new state
785 static void state_change(struct gfs2_glock *gl, unsigned int new_state)
789 held1 = (gl->gl_state != LM_ST_UNLOCKED);
790 held2 = (new_state != LM_ST_UNLOCKED);
792 if (held1 != held2) {
799 gl->gl_state = new_state;
803 * xmote_bh - Called after the lock module is done acquiring a lock
804 * @gl: The glock in question
805 * @ret: the int returned from the lock module
809 static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
811 struct gfs2_sbd *sdp = gl->gl_sbd;
812 struct gfs2_glock_operations *glops = gl->gl_ops;
813 struct gfs2_holder *gh = gl->gl_req_gh;
814 int prev_state = gl->gl_state;
817 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
818 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
819 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
821 state_change(gl, ret & LM_OUT_ST_MASK);
823 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
825 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
826 } else if (gl->gl_state == LM_ST_DEFERRED) {
827 /* We might not want to do this here.
828 Look at moving to the inode glops. */
830 glops->go_inval(gl, DIO_DATA);
833 /* Deal with each possible exit condition */
836 gl->gl_stamp = jiffies;
838 else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
839 spin_lock(&gl->gl_spin);
840 list_del_init(&gh->gh_list);
842 spin_unlock(&gl->gl_spin);
844 } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
845 spin_lock(&gl->gl_spin);
846 list_del_init(&gh->gh_list);
847 if (gl->gl_state == gh->gh_state ||
848 gl->gl_state == LM_ST_UNLOCKED)
851 if (gfs2_assert_warn(sdp, gh->gh_flags &
852 (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
853 fs_warn(sdp, "ret = 0x%.8X\n", ret);
854 gh->gh_error = GLR_TRYFAILED;
856 spin_unlock(&gl->gl_spin);
858 if (ret & LM_OUT_CANCELED)
859 handle_callback(gl, LM_ST_UNLOCKED); /* Lame */
861 } else if (ret & LM_OUT_CANCELED) {
862 spin_lock(&gl->gl_spin);
863 list_del_init(&gh->gh_list);
864 gh->gh_error = GLR_CANCELED;
865 spin_unlock(&gl->gl_spin);
867 } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
868 spin_lock(&gl->gl_spin);
869 list_move_tail(&gh->gh_list, &gl->gl_holders);
871 set_bit(HIF_HOLDER, &gh->gh_iflags);
872 spin_unlock(&gl->gl_spin);
874 set_bit(HIF_FIRST, &gh->gh_iflags);
878 } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
879 spin_lock(&gl->gl_spin);
880 list_del_init(&gh->gh_list);
881 gh->gh_error = GLR_TRYFAILED;
882 spin_unlock(&gl->gl_spin);
885 if (gfs2_assert_withdraw(sdp, 0) == -1)
886 fs_err(sdp, "ret = 0x%.8X\n", ret);
889 if (glops->go_xmote_bh)
890 glops->go_xmote_bh(gl);
893 spin_lock(&gl->gl_spin);
894 gl->gl_req_gh = NULL;
895 gl->gl_req_bh = NULL;
896 clear_bit(GLF_LOCK, &gl->gl_flags);
898 spin_unlock(&gl->gl_spin);
904 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
907 complete(&gh->gh_wait);
912 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
913 * @gl: The glock in question
914 * @state: the requested state
915 * @flags: modifier flags to the lock call
919 void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags)
921 struct gfs2_sbd *sdp = gl->gl_sbd;
922 struct gfs2_glock_operations *glops = gl->gl_ops;
923 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
924 LM_FLAG_NOEXP | LM_FLAG_ANY |
926 unsigned int lck_ret;
928 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
929 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
930 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
931 gfs2_assert_warn(sdp, state != gl->gl_state);
933 if (gl->gl_state == LM_ST_EXCLUSIVE) {
936 DIO_METADATA | DIO_DATA | DIO_RELEASE);
940 gl->gl_req_bh = xmote_bh;
942 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state,
945 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
948 if (lck_ret & LM_OUT_ASYNC)
949 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
951 xmote_bh(gl, lck_ret);
955 * drop_bh - Called after a lock module unlock completes
957 * @ret: the return status
959 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
960 * Doesn't drop the reference on the glock the top half took out
964 static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
966 struct gfs2_sbd *sdp = gl->gl_sbd;
967 struct gfs2_glock_operations *glops = gl->gl_ops;
968 struct gfs2_holder *gh = gl->gl_req_gh;
970 clear_bit(GLF_PREFETCH, &gl->gl_flags);
972 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
973 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
974 gfs2_assert_warn(sdp, !ret);
976 state_change(gl, LM_ST_UNLOCKED);
979 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
982 spin_lock(&gl->gl_spin);
983 list_del_init(&gh->gh_list);
985 spin_unlock(&gl->gl_spin);
988 if (glops->go_drop_bh)
989 glops->go_drop_bh(gl);
991 spin_lock(&gl->gl_spin);
992 gl->gl_req_gh = NULL;
993 gl->gl_req_bh = NULL;
994 clear_bit(GLF_LOCK, &gl->gl_flags);
996 spin_unlock(&gl->gl_spin);
1001 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
1002 gfs2_holder_put(gh);
1004 complete(&gh->gh_wait);
1009 * gfs2_glock_drop_th - call into the lock module to unlock a lock
1014 void gfs2_glock_drop_th(struct gfs2_glock *gl)
1016 struct gfs2_sbd *sdp = gl->gl_sbd;
1017 struct gfs2_glock_operations *glops = gl->gl_ops;
1020 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1021 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
1022 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
1024 if (gl->gl_state == LM_ST_EXCLUSIVE) {
1027 DIO_METADATA | DIO_DATA | DIO_RELEASE);
1030 gfs2_glock_hold(gl);
1031 gl->gl_req_bh = drop_bh;
1033 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
1035 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
1041 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
1045 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1046 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1048 * Don't cancel GL_NOCANCEL requests.
1051 static void do_cancels(struct gfs2_holder *gh)
1053 struct gfs2_glock *gl = gh->gh_gl;
1055 spin_lock(&gl->gl_spin);
1057 while (gl->gl_req_gh != gh &&
1058 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1059 !list_empty(&gh->gh_list)) {
1060 if (gl->gl_req_bh &&
1062 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
1063 spin_unlock(&gl->gl_spin);
1064 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
1066 spin_lock(&gl->gl_spin);
1068 spin_unlock(&gl->gl_spin);
1070 spin_lock(&gl->gl_spin);
1074 spin_unlock(&gl->gl_spin);
1078 * glock_wait_internal - wait on a glock acquisition
1079 * @gh: the glock holder
1081 * Returns: 0 on success
1084 static int glock_wait_internal(struct gfs2_holder *gh)
1086 struct gfs2_glock *gl = gh->gh_gl;
1087 struct gfs2_sbd *sdp = gl->gl_sbd;
1088 struct gfs2_glock_operations *glops = gl->gl_ops;
1090 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1093 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1094 spin_lock(&gl->gl_spin);
1095 if (gl->gl_req_gh != gh &&
1096 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1097 !list_empty(&gh->gh_list)) {
1098 list_del_init(&gh->gh_list);
1099 gh->gh_error = GLR_TRYFAILED;
1101 spin_unlock(&gl->gl_spin);
1102 return gh->gh_error;
1104 spin_unlock(&gl->gl_spin);
1107 if (gh->gh_flags & LM_FLAG_PRIORITY)
1110 wait_for_completion(&gh->gh_wait);
1113 return gh->gh_error;
1115 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
1116 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state,
1120 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1121 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1123 if (glops->go_lock) {
1124 gh->gh_error = glops->go_lock(gh);
1126 spin_lock(&gl->gl_spin);
1127 list_del_init(&gh->gh_list);
1128 spin_unlock(&gl->gl_spin);
1132 spin_lock(&gl->gl_spin);
1133 gl->gl_req_gh = NULL;
1134 gl->gl_req_bh = NULL;
1135 clear_bit(GLF_LOCK, &gl->gl_flags);
1137 spin_unlock(&gl->gl_spin);
1140 return gh->gh_error;
1143 static inline struct gfs2_holder *
1144 find_holder_by_owner(struct list_head *head, struct task_struct *owner)
1146 struct gfs2_holder *gh;
1148 list_for_each_entry(gh, head, gh_list) {
1149 if (gh->gh_owner == owner)
1157 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1158 * @gh: the holder structure to add
1162 static void add_to_queue(struct gfs2_holder *gh)
1164 struct gfs2_glock *gl = gh->gh_gl;
1165 struct gfs2_holder *existing;
1167 BUG_ON(!gh->gh_owner);
1169 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
1171 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1172 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1176 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
1178 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1179 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1183 if (gh->gh_flags & LM_FLAG_PRIORITY)
1184 list_add(&gh->gh_list, &gl->gl_waiters3);
1186 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1190 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1191 * @gh: the holder structure
1193 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1195 * Returns: 0, GLR_TRYFAILED, or errno on failure
1198 int gfs2_glock_nq(struct gfs2_holder *gh)
1200 struct gfs2_glock *gl = gh->gh_gl;
1201 struct gfs2_sbd *sdp = gl->gl_sbd;
1205 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1206 set_bit(HIF_ABORTED, &gh->gh_iflags);
1210 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1212 spin_lock(&gl->gl_spin);
1215 spin_unlock(&gl->gl_spin);
1217 if (!(gh->gh_flags & GL_ASYNC)) {
1218 error = glock_wait_internal(gh);
1219 if (error == GLR_CANCELED) {
1225 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1227 if (error == GLR_TRYFAILED && (gh->gh_flags & GL_DUMP))
1234 * gfs2_glock_poll - poll to see if an async request has been completed
1237 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1240 int gfs2_glock_poll(struct gfs2_holder *gh)
1242 struct gfs2_glock *gl = gh->gh_gl;
1245 spin_lock(&gl->gl_spin);
1247 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1249 else if (list_empty(&gh->gh_list)) {
1250 if (gh->gh_error == GLR_CANCELED) {
1251 spin_unlock(&gl->gl_spin);
1253 if (gfs2_glock_nq(gh))
1260 spin_unlock(&gl->gl_spin);
1266 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1267 * @gh: the holder structure
1269 * Returns: 0, GLR_TRYFAILED, or errno on failure
1272 int gfs2_glock_wait(struct gfs2_holder *gh)
1276 error = glock_wait_internal(gh);
1277 if (error == GLR_CANCELED) {
1279 gh->gh_flags &= ~GL_ASYNC;
1280 error = gfs2_glock_nq(gh);
1287 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1288 * @gh: the glock holder
1292 void gfs2_glock_dq(struct gfs2_holder *gh)
1294 struct gfs2_glock *gl = gh->gh_gl;
1295 struct gfs2_glock_operations *glops = gl->gl_ops;
1297 if (gh->gh_flags & GL_SYNC)
1298 set_bit(GLF_SYNC, &gl->gl_flags);
1300 if (gh->gh_flags & GL_NOCACHE)
1301 handle_callback(gl, LM_ST_UNLOCKED);
1303 gfs2_glmutex_lock(gl);
1305 spin_lock(&gl->gl_spin);
1306 list_del_init(&gh->gh_list);
1308 if (list_empty(&gl->gl_holders)) {
1309 spin_unlock(&gl->gl_spin);
1311 if (glops->go_unlock)
1312 glops->go_unlock(gh);
1314 if (test_bit(GLF_SYNC, &gl->gl_flags)) {
1316 glops->go_sync(gl, DIO_METADATA | DIO_DATA);
1319 gl->gl_stamp = jiffies;
1321 spin_lock(&gl->gl_spin);
1324 clear_bit(GLF_LOCK, &gl->gl_flags);
1326 spin_unlock(&gl->gl_spin);
1330 * gfs2_glock_prefetch - Try to prefetch a glock
1332 * @state: the state to prefetch in
1333 * @flags: flags passed to go_xmote_th()
1337 static void gfs2_glock_prefetch(struct gfs2_glock *gl, unsigned int state,
1340 struct gfs2_glock_operations *glops = gl->gl_ops;
1342 spin_lock(&gl->gl_spin);
1344 if (test_bit(GLF_LOCK, &gl->gl_flags) ||
1345 !list_empty(&gl->gl_holders) ||
1346 !list_empty(&gl->gl_waiters1) ||
1347 !list_empty(&gl->gl_waiters2) ||
1348 !list_empty(&gl->gl_waiters3) ||
1349 relaxed_state_ok(gl->gl_state, state, flags)) {
1350 spin_unlock(&gl->gl_spin);
1354 set_bit(GLF_PREFETCH, &gl->gl_flags);
1355 set_bit(GLF_LOCK, &gl->gl_flags);
1356 spin_unlock(&gl->gl_spin);
1358 glops->go_xmote_th(gl, state, flags);
1361 static void greedy_work(void *data)
1363 struct greedy *gr = data;
1364 struct gfs2_holder *gh = &gr->gr_gh;
1365 struct gfs2_glock *gl = gh->gh_gl;
1366 struct gfs2_glock_operations *glops = gl->gl_ops;
1368 clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1370 if (glops->go_greedy)
1371 glops->go_greedy(gl);
1373 spin_lock(&gl->gl_spin);
1375 if (list_empty(&gl->gl_waiters2)) {
1376 clear_bit(GLF_GREEDY, &gl->gl_flags);
1377 spin_unlock(&gl->gl_spin);
1378 gfs2_holder_uninit(gh);
1381 gfs2_glock_hold(gl);
1382 list_add_tail(&gh->gh_list, &gl->gl_waiters2);
1384 spin_unlock(&gl->gl_spin);
1390 * gfs2_glock_be_greedy -
1394 * Returns: 0 if go_greedy will be called, 1 otherwise
1397 int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time)
1400 struct gfs2_holder *gh;
1402 if (!time || gl->gl_sbd->sd_args.ar_localcaching ||
1403 test_and_set_bit(GLF_GREEDY, &gl->gl_flags))
1406 gr = kmalloc(sizeof(struct greedy), GFP_KERNEL);
1408 clear_bit(GLF_GREEDY, &gl->gl_flags);
1413 gfs2_holder_init(gl, 0, 0, gh);
1414 set_bit(HIF_GREEDY, &gh->gh_iflags);
1415 INIT_WORK(&gr->gr_work, greedy_work, gr);
1417 set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1418 schedule_delayed_work(&gr->gr_work, time);
1424 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1425 * @gh: the holder structure
1429 void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1432 gfs2_holder_uninit(gh);
1436 * gfs2_glock_nq_num - acquire a glock based on lock number
1437 * @sdp: the filesystem
1438 * @number: the lock number
1439 * @glops: the glock operations for the type of glock
1440 * @state: the state to acquire the glock in
1441 * @flags: modifier flags for the aquisition
1442 * @gh: the struct gfs2_holder
1447 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, uint64_t number,
1448 struct gfs2_glock_operations *glops, unsigned int state,
1449 int flags, struct gfs2_holder *gh)
1451 struct gfs2_glock *gl;
1454 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1456 error = gfs2_glock_nq_init(gl, state, flags, gh);
1464 * glock_compare - Compare two struct gfs2_glock structures for sorting
1465 * @arg_a: the first structure
1466 * @arg_b: the second structure
1470 static int glock_compare(const void *arg_a, const void *arg_b)
1472 struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
1473 struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
1474 struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1475 struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1478 if (a->ln_number > b->ln_number)
1480 else if (a->ln_number < b->ln_number)
1483 if (gh_a->gh_state == LM_ST_SHARED &&
1484 gh_b->gh_state == LM_ST_EXCLUSIVE)
1486 else if (!(gh_a->gh_flags & GL_LOCAL_EXCL) &&
1487 (gh_b->gh_flags & GL_LOCAL_EXCL))
1495 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1496 * @num_gh: the number of structures
1497 * @ghs: an array of struct gfs2_holder structures
1499 * Returns: 0 on success (all glocks acquired),
1500 * errno on failure (no glocks acquired)
1503 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1504 struct gfs2_holder **p)
1509 for (x = 0; x < num_gh; x++)
1512 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1514 for (x = 0; x < num_gh; x++) {
1515 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1517 error = gfs2_glock_nq(p[x]);
1520 gfs2_glock_dq(p[x]);
1529 * gfs2_glock_nq_m - acquire multiple glocks
1530 * @num_gh: the number of structures
1531 * @ghs: an array of struct gfs2_holder structures
1533 * Figure out how big an impact this function has. Either:
1534 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1535 * 2) Forget async stuff and just call nq_m_sync()
1536 * 3) Leave it like it is
1538 * Returns: 0 on success (all glocks acquired),
1539 * errno on failure (no glocks acquired)
1542 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1546 int borked = 0, serious = 0;
1553 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1554 return gfs2_glock_nq(ghs);
1557 e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1561 for (x = 0; x < num_gh; x++) {
1562 ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
1563 error = gfs2_glock_nq(&ghs[x]);
1572 for (x = 0; x < num_gh; x++) {
1573 error = e[x] = glock_wait_internal(&ghs[x]);
1576 if (error != GLR_TRYFAILED && error != GLR_CANCELED)
1586 for (x = 0; x < num_gh; x++)
1588 gfs2_glock_dq(&ghs[x]);
1593 for (x = 0; x < num_gh; x++)
1594 gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
1596 error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
1605 * gfs2_glock_dq_m - release multiple glocks
1606 * @num_gh: the number of structures
1607 * @ghs: an array of struct gfs2_holder structures
1611 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1615 for (x = 0; x < num_gh; x++)
1616 gfs2_glock_dq(&ghs[x]);
1620 * gfs2_glock_dq_uninit_m - release multiple glocks
1621 * @num_gh: the number of structures
1622 * @ghs: an array of struct gfs2_holder structures
1626 void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1630 for (x = 0; x < num_gh; x++)
1631 gfs2_glock_dq_uninit(&ghs[x]);
1635 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1636 * @sdp: the filesystem
1637 * @number: the lock number
1638 * @glops: the glock operations for the type of glock
1639 * @state: the state to acquire the glock in
1640 * @flags: modifier flags for the aquisition
1645 void gfs2_glock_prefetch_num(struct gfs2_sbd *sdp, uint64_t number,
1646 struct gfs2_glock_operations *glops,
1647 unsigned int state, int flags)
1649 struct gfs2_glock *gl;
1652 if (atomic_read(&sdp->sd_reclaim_count) <
1653 gfs2_tune_get(sdp, gt_reclaim_limit)) {
1654 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1656 gfs2_glock_prefetch(gl, state, flags);
1663 * gfs2_lvb_hold - attach a LVB from a glock
1664 * @gl: The glock in question
1668 int gfs2_lvb_hold(struct gfs2_glock *gl)
1672 gfs2_glmutex_lock(gl);
1674 if (!atomic_read(&gl->gl_lvb_count)) {
1675 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1677 gfs2_glmutex_unlock(gl);
1680 gfs2_glock_hold(gl);
1682 atomic_inc(&gl->gl_lvb_count);
1684 gfs2_glmutex_unlock(gl);
1690 * gfs2_lvb_unhold - detach a LVB from a glock
1691 * @gl: The glock in question
1695 void gfs2_lvb_unhold(struct gfs2_glock *gl)
1697 gfs2_glock_hold(gl);
1698 gfs2_glmutex_lock(gl);
1700 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1701 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1702 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1707 gfs2_glmutex_unlock(gl);
1712 void gfs2_lvb_sync(struct gfs2_glock *gl)
1714 gfs2_glmutex_lock(gl);
1716 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count));
1717 if (!gfs2_assert_warn(gl->gl_sbd, gfs2_glock_is_held_excl(gl)))
1718 gfs2_lm_sync_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1720 gfs2_glmutex_unlock(gl);
1724 static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1727 struct gfs2_glock *gl;
1729 gl = gfs2_glock_find(sdp, name);
1733 if (gl->gl_ops->go_callback)
1734 gl->gl_ops->go_callback(gl, state);
1735 handle_callback(gl, state);
1737 spin_lock(&gl->gl_spin);
1739 spin_unlock(&gl->gl_spin);
1745 * gfs2_glock_cb - Callback used by locking module
1746 * @fsdata: Pointer to the superblock
1747 * @type: Type of callback
1748 * @data: Type dependent data pointer
1750 * Called by the locking module when it wants to tell us something.
1751 * Either we need to drop a lock, one of our ASYNC requests completed, or
1752 * a journal from another client needs to be recovered.
1755 void gfs2_glock_cb(lm_fsdata_t *fsdata, unsigned int type, void *data)
1757 struct gfs2_sbd *sdp = (struct gfs2_sbd *)fsdata;
1761 blocking_cb(sdp, data, LM_ST_UNLOCKED);
1765 blocking_cb(sdp, data, LM_ST_DEFERRED);
1769 blocking_cb(sdp, data, LM_ST_SHARED);
1773 struct lm_async_cb *async = data;
1774 struct gfs2_glock *gl;
1776 gl = gfs2_glock_find(sdp, &async->lc_name);
1777 if (gfs2_assert_warn(sdp, gl))
1779 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1780 gl->gl_req_bh(gl, async->lc_ret);
1785 case LM_CB_NEED_RECOVERY:
1786 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1787 if (sdp->sd_recoverd_process)
1788 wake_up_process(sdp->sd_recoverd_process);
1791 case LM_CB_DROPLOCKS:
1792 gfs2_gl_hash_clear(sdp, NO_WAIT);
1793 gfs2_quota_scan(sdp);
1797 gfs2_assert_warn(sdp, 0);
1803 * gfs2_iopen_go_callback - Try to kick the inode/vnode associated with an
1804 * iopen glock from memory
1805 * @io_gl: the iopen glock
1806 * @state: the state into which the glock should be put
1810 void gfs2_iopen_go_callback(struct gfs2_glock *io_gl, unsigned int state)
1813 if (state != LM_ST_UNLOCKED)
1815 /* FIXME: remove this? */
1819 * demote_ok - Check to see if it's ok to unlock a glock
1822 * Returns: 1 if it's ok
1825 static int demote_ok(struct gfs2_glock *gl)
1827 struct gfs2_sbd *sdp = gl->gl_sbd;
1828 struct gfs2_glock_operations *glops = gl->gl_ops;
1831 if (test_bit(GLF_STICKY, &gl->gl_flags))
1833 else if (test_bit(GLF_PREFETCH, &gl->gl_flags))
1834 demote = time_after_eq(jiffies,
1836 gfs2_tune_get(sdp, gt_prefetch_secs) * HZ);
1837 else if (glops->go_demote_ok)
1838 demote = glops->go_demote_ok(gl);
1844 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1849 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1851 struct gfs2_sbd *sdp = gl->gl_sbd;
1853 spin_lock(&sdp->sd_reclaim_lock);
1854 if (list_empty(&gl->gl_reclaim)) {
1855 gfs2_glock_hold(gl);
1856 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1857 atomic_inc(&sdp->sd_reclaim_count);
1859 spin_unlock(&sdp->sd_reclaim_lock);
1861 wake_up(&sdp->sd_reclaim_wq);
1865 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1866 * @sdp: the filesystem
1868 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1869 * different glock and we notice that there are a lot of glocks in the
1874 void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1876 struct gfs2_glock *gl;
1878 spin_lock(&sdp->sd_reclaim_lock);
1879 if (list_empty(&sdp->sd_reclaim_list)) {
1880 spin_unlock(&sdp->sd_reclaim_lock);
1883 gl = list_entry(sdp->sd_reclaim_list.next,
1884 struct gfs2_glock, gl_reclaim);
1885 list_del_init(&gl->gl_reclaim);
1886 spin_unlock(&sdp->sd_reclaim_lock);
1888 atomic_dec(&sdp->sd_reclaim_count);
1889 atomic_inc(&sdp->sd_reclaimed);
1891 if (gfs2_glmutex_trylock(gl)) {
1892 if (queue_empty(gl, &gl->gl_holders) &&
1893 gl->gl_state != LM_ST_UNLOCKED &&
1895 handle_callback(gl, LM_ST_UNLOCKED);
1896 gfs2_glmutex_unlock(gl);
1903 * examine_bucket - Call a function for glock in a hash bucket
1904 * @examiner: the function
1905 * @sdp: the filesystem
1906 * @bucket: the bucket
1908 * Returns: 1 if the bucket has entries
1911 static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
1912 struct gfs2_gl_hash_bucket *bucket)
1914 struct glock_plug plug;
1915 struct list_head *tmp;
1916 struct gfs2_glock *gl;
1919 /* Add "plug" to end of bucket list, work back up list from there */
1920 memset(&plug.gl_flags, 0, sizeof(unsigned long));
1921 set_bit(GLF_PLUG, &plug.gl_flags);
1923 write_lock(&bucket->hb_lock);
1924 list_add(&plug.gl_list, &bucket->hb_list);
1925 write_unlock(&bucket->hb_lock);
1928 write_lock(&bucket->hb_lock);
1931 tmp = plug.gl_list.next;
1933 if (tmp == &bucket->hb_list) {
1934 list_del(&plug.gl_list);
1935 entries = !list_empty(&bucket->hb_list);
1936 write_unlock(&bucket->hb_lock);
1939 gl = list_entry(tmp, struct gfs2_glock, gl_list);
1941 /* Move plug up list */
1942 list_move(&plug.gl_list, &gl->gl_list);
1944 if (test_bit(GLF_PLUG, &gl->gl_flags))
1947 /* examiner() must glock_put() */
1948 gfs2_glock_hold(gl);
1953 write_unlock(&bucket->hb_lock);
1960 * scan_glock - look at a glock and see if we can reclaim it
1961 * @gl: the glock to look at
1965 static void scan_glock(struct gfs2_glock *gl)
1967 if (gfs2_glmutex_trylock(gl)) {
1968 if (gl->gl_ops == &gfs2_inode_glops)
1970 if (queue_empty(gl, &gl->gl_holders) &&
1971 gl->gl_state != LM_ST_UNLOCKED &&
1975 gfs2_glmutex_unlock(gl);
1983 gfs2_glmutex_unlock(gl);
1984 gfs2_glock_schedule_for_reclaim(gl);
1989 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
1990 * @sdp: the filesystem
1994 void gfs2_scand_internal(struct gfs2_sbd *sdp)
1998 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
1999 examine_bucket(scan_glock, sdp, &sdp->sd_gl_hash[x]);
2005 * clear_glock - look at a glock and see if we can free it from glock cache
2006 * @gl: the glock to look at
2010 static void clear_glock(struct gfs2_glock *gl)
2012 struct gfs2_sbd *sdp = gl->gl_sbd;
2015 spin_lock(&sdp->sd_reclaim_lock);
2016 if (!list_empty(&gl->gl_reclaim)) {
2017 list_del_init(&gl->gl_reclaim);
2018 atomic_dec(&sdp->sd_reclaim_count);
2019 spin_unlock(&sdp->sd_reclaim_lock);
2020 released = gfs2_glock_put(gl);
2021 gfs2_assert(sdp, !released);
2023 spin_unlock(&sdp->sd_reclaim_lock);
2026 if (gfs2_glmutex_trylock(gl)) {
2027 if (queue_empty(gl, &gl->gl_holders) &&
2028 gl->gl_state != LM_ST_UNLOCKED)
2029 handle_callback(gl, LM_ST_UNLOCKED);
2031 gfs2_glmutex_unlock(gl);
2038 * gfs2_gl_hash_clear - Empty out the glock hash table
2039 * @sdp: the filesystem
2040 * @wait: wait until it's all gone
2042 * Called when unmounting the filesystem, or when inter-node lock manager
2043 * requests DROPLOCKS because it is running out of capacity.
2046 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
2057 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
2058 if (examine_bucket(clear_glock, sdp,
2059 &sdp->sd_gl_hash[x]))
2065 if (time_after_eq(jiffies,
2066 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
2067 fs_warn(sdp, "Unmount seems to be stalled. "
2068 "Dumping lock state...\n");
2069 gfs2_dump_lockstate(sdp);
2073 /* invalidate_inodes() requires that the sb inodes list
2074 not change, but an async completion callback for an
2075 unlock can occur which does glock_put() which
2076 can call iput() which will change the sb inodes list.
2077 invalidate_inodes_mutex prevents glock_put()'s during
2078 an invalidate_inodes() */
2080 mutex_lock(&sdp->sd_invalidate_inodes_mutex);
2081 invalidate_inodes(sdp->sd_vfs);
2082 mutex_unlock(&sdp->sd_invalidate_inodes_mutex);
2088 * Diagnostic routines to help debug distributed deadlock
2092 * dump_holder - print information about a glock holder
2093 * @str: a string naming the type of holder
2094 * @gh: the glock holder
2096 * Returns: 0 on success, -ENOBUFS when we run out of space
2099 static int dump_holder(char *str, struct gfs2_holder *gh)
2102 int error = -ENOBUFS;
2104 printk(KERN_INFO " %s\n", str);
2105 printk(KERN_INFO " owner = %ld\n",
2106 (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
2107 printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
2108 printk(KERN_INFO " gh_flags =");
2109 for (x = 0; x < 32; x++)
2110 if (gh->gh_flags & (1 << x))
2113 printk(KERN_INFO " error = %d\n", gh->gh_error);
2114 printk(KERN_INFO " gh_iflags =");
2115 for (x = 0; x < 32; x++)
2116 if (test_bit(x, &gh->gh_iflags))
2119 print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip);
2127 * dump_inode - print information about an inode
2130 * Returns: 0 on success, -ENOBUFS when we run out of space
2133 static int dump_inode(struct gfs2_inode *ip)
2136 int error = -ENOBUFS;
2138 printk(KERN_INFO " Inode:\n");
2139 printk(KERN_INFO " num = %llu %llu\n",
2140 (unsigned long long)ip->i_num.no_formal_ino,
2141 (unsigned long long)ip->i_num.no_addr);
2142 printk(KERN_INFO " type = %u\n", IF2DT(ip->i_di.di_mode));
2143 printk(KERN_INFO " i_flags =");
2144 for (x = 0; x < 32; x++)
2145 if (test_bit(x, &ip->i_flags))
2155 * dump_glock - print information about a glock
2157 * @count: where we are in the buffer
2159 * Returns: 0 on success, -ENOBUFS when we run out of space
2162 static int dump_glock(struct gfs2_glock *gl)
2164 struct gfs2_holder *gh;
2166 int error = -ENOBUFS;
2168 spin_lock(&gl->gl_spin);
2170 printk(KERN_INFO "Glock (%u, %llu)\n", gl->gl_name.ln_type,
2171 (unsigned long long)gl->gl_name.ln_number);
2172 printk(KERN_INFO " gl_flags =");
2173 for (x = 0; x < 32; x++)
2174 if (test_bit(x, &gl->gl_flags))
2177 printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref.refcount));
2178 printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
2179 printk(KERN_INFO " gl_owner = %s\n", gl->gl_owner->comm);
2180 print_symbol(KERN_INFO " gl_ip = %s\n", gl->gl_ip);
2181 printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
2182 printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
2183 printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
2184 printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
2185 printk(KERN_INFO " le = %s\n",
2186 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
2187 printk(KERN_INFO " reclaim = %s\n",
2188 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
2190 printk(KERN_INFO " aspace = %lu\n",
2191 gl->gl_aspace->i_mapping->nrpages);
2193 printk(KERN_INFO " aspace = no\n");
2194 printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
2195 if (gl->gl_req_gh) {
2196 error = dump_holder("Request", gl->gl_req_gh);
2200 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
2201 error = dump_holder("Holder", gh);
2205 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
2206 error = dump_holder("Waiter1", gh);
2210 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
2211 error = dump_holder("Waiter2", gh);
2215 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
2216 error = dump_holder("Waiter3", gh);
2220 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
2221 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
2222 list_empty(&gl->gl_holders)) {
2223 error = dump_inode(gl->gl_object);
2228 printk(KERN_INFO " Inode: busy\n");
2235 spin_unlock(&gl->gl_spin);
2241 * gfs2_dump_lockstate - print out the current lockstate
2242 * @sdp: the filesystem
2243 * @ub: the buffer to copy the information into
2245 * If @ub is NULL, dump the lockstate to the console.
2249 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
2251 struct gfs2_gl_hash_bucket *bucket;
2252 struct gfs2_glock *gl;
2256 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2257 bucket = &sdp->sd_gl_hash[x];
2259 read_lock(&bucket->hb_lock);
2261 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
2262 if (test_bit(GLF_PLUG, &gl->gl_flags))
2265 error = dump_glock(gl);
2270 read_unlock(&bucket->hb_lock);