2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/delay.h>
16 #include <linux/sort.h>
17 #include <linux/jhash.h>
18 #include <linux/kref.h>
19 #include <linux/kallsyms.h>
20 #include <linux/gfs2_ondisk.h>
21 #include <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(const struct gfs2_sbd *sdp,
87 const struct lm_lockname *name)
91 h = jhash(&name->ln_number, sizeof(u64), 0);
92 h = jhash(&name->ln_type, sizeof(unsigned int), h);
93 h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
94 h &= GFS2_GL_HASH_MASK;
100 * glock_free() - Perform a few checks and then release struct gfs2_glock
101 * @gl: The glock to release
103 * Also calls lock module to release its internal structure for this glock.
107 static void glock_free(struct gfs2_glock *gl)
109 struct gfs2_sbd *sdp = gl->gl_sbd;
110 struct inode *aspace = gl->gl_aspace;
112 gfs2_lm_put_lock(sdp, gl->gl_lock);
115 gfs2_aspace_put(aspace);
117 kmem_cache_free(gfs2_glock_cachep, gl);
121 * gfs2_glock_hold() - increment reference count on glock
122 * @gl: The glock to hold
126 void gfs2_glock_hold(struct gfs2_glock *gl)
128 kref_get(&gl->gl_ref);
131 /* All work is done after the return from kref_put() so we
132 can release the write_lock before the free. */
134 static void kill_glock(struct kref *kref)
136 struct gfs2_glock *gl = container_of(kref, struct gfs2_glock, gl_ref);
137 struct gfs2_sbd *sdp = gl->gl_sbd;
139 gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED);
140 gfs2_assert(sdp, list_empty(&gl->gl_reclaim));
141 gfs2_assert(sdp, list_empty(&gl->gl_holders));
142 gfs2_assert(sdp, list_empty(&gl->gl_waiters1));
143 gfs2_assert(sdp, list_empty(&gl->gl_waiters2));
144 gfs2_assert(sdp, list_empty(&gl->gl_waiters3));
148 * gfs2_glock_put() - Decrement reference count on glock
149 * @gl: The glock to put
153 int gfs2_glock_put(struct gfs2_glock *gl)
155 struct gfs2_gl_hash_bucket *bucket = gl->gl_bucket;
158 write_lock(&bucket->hb_lock);
159 if (kref_put(&gl->gl_ref, kill_glock)) {
160 list_del_init(&gl->gl_list);
161 write_unlock(&bucket->hb_lock);
162 BUG_ON(spin_is_locked(&gl->gl_spin));
167 write_unlock(&bucket->hb_lock);
173 * queue_empty - check to see if a glock's queue is empty
175 * @head: the head of the queue to check
177 * This function protects the list in the event that a process already
178 * has a holder on the list and is adding a second holder for itself.
179 * The glmutex lock is what generally prevents processes from working
180 * on the same glock at once, but the special case of adding a second
181 * holder for yourself ("recursive" locking) doesn't involve locking
182 * glmutex, making the spin lock necessary.
184 * Returns: 1 if the queue is empty
187 static inline int queue_empty(struct gfs2_glock *gl, struct list_head *head)
190 spin_lock(&gl->gl_spin);
191 empty = list_empty(head);
192 spin_unlock(&gl->gl_spin);
197 * search_bucket() - Find struct gfs2_glock by lock number
198 * @bucket: the bucket to search
199 * @name: The lock name
201 * Returns: NULL, or the struct gfs2_glock with the requested number
204 static struct gfs2_glock *search_bucket(struct gfs2_gl_hash_bucket *bucket,
205 const struct gfs2_sbd *sdp,
206 const struct lm_lockname *name)
208 struct gfs2_glock *gl;
210 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
211 if (test_bit(GLF_PLUG, &gl->gl_flags))
213 if (!lm_name_equal(&gl->gl_name, name))
215 if (gl->gl_sbd != sdp)
218 kref_get(&gl->gl_ref);
227 * gfs2_glock_find() - Find glock by lock number
228 * @sdp: The GFS2 superblock
229 * @name: The lock name
231 * Returns: NULL, or the struct gfs2_glock with the requested number
234 static struct gfs2_glock *gfs2_glock_find(struct gfs2_sbd *sdp,
235 const struct lm_lockname *name)
237 struct gfs2_gl_hash_bucket *bucket = &sdp->sd_gl_hash[gl_hash(sdp, name)];
238 struct gfs2_glock *gl;
240 read_lock(&bucket->hb_lock);
241 gl = search_bucket(bucket, sdp, name);
242 read_unlock(&bucket->hb_lock);
248 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
249 * @sdp: The GFS2 superblock
250 * @number: the lock number
251 * @glops: The glock_operations to use
252 * @create: If 0, don't create the glock if it doesn't exist
253 * @glp: the glock is returned here
255 * This does not lock a glock, just finds/creates structures for one.
260 int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
261 const struct gfs2_glock_operations *glops, int create,
262 struct gfs2_glock **glp)
264 struct lm_lockname name;
265 struct gfs2_glock *gl, *tmp;
266 struct gfs2_gl_hash_bucket *bucket;
269 name.ln_number = number;
270 name.ln_type = glops->go_type;
271 bucket = &sdp->sd_gl_hash[gl_hash(sdp, &name)];
273 read_lock(&bucket->hb_lock);
274 gl = search_bucket(bucket, sdp, &name);
275 read_unlock(&bucket->hb_lock);
282 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
288 kref_init(&gl->gl_ref);
289 gl->gl_state = LM_ST_UNLOCKED;
293 gl->gl_req_gh = NULL;
294 gl->gl_req_bh = NULL;
296 gl->gl_stamp = jiffies;
297 gl->gl_object = NULL;
298 gl->gl_bucket = bucket;
300 gl->gl_aspace = NULL;
301 lops_init_le(&gl->gl_le, &gfs2_glock_lops);
303 /* If this glock protects actual on-disk data or metadata blocks,
304 create a VFS inode to manage the pages/buffers holding them. */
305 if (glops == &gfs2_inode_glops || glops == &gfs2_rgrp_glops) {
306 gl->gl_aspace = gfs2_aspace_get(sdp);
307 if (!gl->gl_aspace) {
313 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
317 write_lock(&bucket->hb_lock);
318 tmp = search_bucket(bucket, sdp, &name);
320 write_unlock(&bucket->hb_lock);
324 list_add_tail(&gl->gl_list, &bucket->hb_list);
325 write_unlock(&bucket->hb_lock);
334 gfs2_aspace_put(gl->gl_aspace);
336 kmem_cache_free(gfs2_glock_cachep, gl);
341 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
343 * @state: the state we're requesting
344 * @flags: the modifier flags
345 * @gh: the holder structure
349 void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
350 struct gfs2_holder *gh)
352 INIT_LIST_HEAD(&gh->gh_list);
354 gh->gh_ip = (unsigned long)__builtin_return_address(0);
355 gh->gh_owner = current;
356 gh->gh_state = state;
357 gh->gh_flags = flags;
360 init_completion(&gh->gh_wait);
362 if (gh->gh_state == LM_ST_EXCLUSIVE)
363 gh->gh_flags |= GL_LOCAL_EXCL;
369 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
370 * @state: the state we're requesting
371 * @flags: the modifier flags
372 * @gh: the holder structure
374 * Don't mess with the glock.
378 void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
380 gh->gh_state = state;
381 gh->gh_flags = flags;
382 if (gh->gh_state == LM_ST_EXCLUSIVE)
383 gh->gh_flags |= GL_LOCAL_EXCL;
385 gh->gh_iflags &= 1 << HIF_ALLOCED;
386 gh->gh_ip = (unsigned long)__builtin_return_address(0);
390 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
391 * @gh: the holder structure
395 void gfs2_holder_uninit(struct gfs2_holder *gh)
397 gfs2_glock_put(gh->gh_gl);
403 * gfs2_holder_get - get a struct gfs2_holder structure
405 * @state: the state we're requesting
406 * @flags: the modifier flags
409 * Figure out how big an impact this function has. Either:
410 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
411 * 2) Leave it like it is
413 * Returns: the holder structure, NULL on ENOMEM
416 static struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl,
418 int flags, gfp_t gfp_flags)
420 struct gfs2_holder *gh;
422 gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags);
426 gfs2_holder_init(gl, state, flags, gh);
427 set_bit(HIF_ALLOCED, &gh->gh_iflags);
428 gh->gh_ip = (unsigned long)__builtin_return_address(0);
433 * gfs2_holder_put - get rid of a struct gfs2_holder structure
434 * @gh: the holder structure
438 static void gfs2_holder_put(struct gfs2_holder *gh)
440 gfs2_holder_uninit(gh);
445 * rq_mutex - process a mutex request in the queue
446 * @gh: the glock holder
448 * Returns: 1 if the queue is blocked
451 static int rq_mutex(struct gfs2_holder *gh)
453 struct gfs2_glock *gl = gh->gh_gl;
455 list_del_init(&gh->gh_list);
456 /* gh->gh_error never examined. */
457 set_bit(GLF_LOCK, &gl->gl_flags);
458 complete(&gh->gh_wait);
464 * rq_promote - process a promote request in the queue
465 * @gh: the glock holder
467 * Acquire a new inter-node lock, or change a lock state to more restrictive.
469 * Returns: 1 if the queue is blocked
472 static int rq_promote(struct gfs2_holder *gh)
474 struct gfs2_glock *gl = gh->gh_gl;
475 struct gfs2_sbd *sdp = gl->gl_sbd;
476 const struct gfs2_glock_operations *glops = gl->gl_ops;
478 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
479 if (list_empty(&gl->gl_holders)) {
481 set_bit(GLF_LOCK, &gl->gl_flags);
482 spin_unlock(&gl->gl_spin);
484 if (atomic_read(&sdp->sd_reclaim_count) >
485 gfs2_tune_get(sdp, gt_reclaim_limit) &&
486 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
487 gfs2_reclaim_glock(sdp);
488 gfs2_reclaim_glock(sdp);
491 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
492 spin_lock(&gl->gl_spin);
497 if (list_empty(&gl->gl_holders)) {
498 set_bit(HIF_FIRST, &gh->gh_iflags);
499 set_bit(GLF_LOCK, &gl->gl_flags);
501 struct gfs2_holder *next_gh;
502 if (gh->gh_flags & GL_LOCAL_EXCL)
504 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
506 if (next_gh->gh_flags & GL_LOCAL_EXCL)
510 list_move_tail(&gh->gh_list, &gl->gl_holders);
512 set_bit(HIF_HOLDER, &gh->gh_iflags);
514 complete(&gh->gh_wait);
520 * rq_demote - process a demote request in the queue
521 * @gh: the glock holder
523 * Returns: 1 if the queue is blocked
526 static int rq_demote(struct gfs2_holder *gh)
528 struct gfs2_glock *gl = gh->gh_gl;
529 const struct gfs2_glock_operations *glops = gl->gl_ops;
531 if (!list_empty(&gl->gl_holders))
534 if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) {
535 list_del_init(&gh->gh_list);
537 spin_unlock(&gl->gl_spin);
538 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
541 complete(&gh->gh_wait);
542 spin_lock(&gl->gl_spin);
545 set_bit(GLF_LOCK, &gl->gl_flags);
546 spin_unlock(&gl->gl_spin);
548 if (gh->gh_state == LM_ST_UNLOCKED ||
549 gl->gl_state != LM_ST_EXCLUSIVE)
550 glops->go_drop_th(gl);
552 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
554 spin_lock(&gl->gl_spin);
561 * rq_greedy - process a queued request to drop greedy status
562 * @gh: the glock holder
564 * Returns: 1 if the queue is blocked
567 static int rq_greedy(struct gfs2_holder *gh)
569 struct gfs2_glock *gl = gh->gh_gl;
571 list_del_init(&gh->gh_list);
572 /* gh->gh_error never examined. */
573 clear_bit(GLF_GREEDY, &gl->gl_flags);
574 spin_unlock(&gl->gl_spin);
576 gfs2_holder_uninit(gh);
577 kfree(container_of(gh, struct greedy, gr_gh));
579 spin_lock(&gl->gl_spin);
585 * run_queue - process holder structures on a glock
589 static void run_queue(struct gfs2_glock *gl)
591 struct gfs2_holder *gh;
595 if (test_bit(GLF_LOCK, &gl->gl_flags))
598 if (!list_empty(&gl->gl_waiters1)) {
599 gh = list_entry(gl->gl_waiters1.next,
600 struct gfs2_holder, gh_list);
602 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
603 blocked = rq_mutex(gh);
605 gfs2_assert_warn(gl->gl_sbd, 0);
607 } else if (!list_empty(&gl->gl_waiters2) &&
608 !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) {
609 gh = list_entry(gl->gl_waiters2.next,
610 struct gfs2_holder, gh_list);
612 if (test_bit(HIF_DEMOTE, &gh->gh_iflags))
613 blocked = rq_demote(gh);
614 else if (test_bit(HIF_GREEDY, &gh->gh_iflags))
615 blocked = rq_greedy(gh);
617 gfs2_assert_warn(gl->gl_sbd, 0);
619 } else if (!list_empty(&gl->gl_waiters3)) {
620 gh = list_entry(gl->gl_waiters3.next,
621 struct gfs2_holder, gh_list);
623 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
624 blocked = rq_promote(gh);
626 gfs2_assert_warn(gl->gl_sbd, 0);
637 * gfs2_glmutex_lock - acquire a local lock on a glock
640 * Gives caller exclusive access to manipulate a glock structure.
643 static void gfs2_glmutex_lock(struct gfs2_glock *gl)
645 struct gfs2_holder gh;
647 gfs2_holder_init(gl, 0, 0, &gh);
648 set_bit(HIF_MUTEX, &gh.gh_iflags);
650 spin_lock(&gl->gl_spin);
651 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
652 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
654 gl->gl_owner = current;
655 gl->gl_ip = (unsigned long)__builtin_return_address(0);
656 complete(&gh.gh_wait);
658 spin_unlock(&gl->gl_spin);
660 wait_for_completion(&gh.gh_wait);
661 gfs2_holder_uninit(&gh);
665 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
668 * Returns: 1 if the glock is acquired
671 static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
675 spin_lock(&gl->gl_spin);
676 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
679 gl->gl_owner = current;
680 gl->gl_ip = (unsigned long)__builtin_return_address(0);
682 spin_unlock(&gl->gl_spin);
688 * gfs2_glmutex_unlock - release a local lock on a glock
693 static void gfs2_glmutex_unlock(struct gfs2_glock *gl)
695 spin_lock(&gl->gl_spin);
696 clear_bit(GLF_LOCK, &gl->gl_flags);
700 BUG_ON(!spin_is_locked(&gl->gl_spin));
701 spin_unlock(&gl->gl_spin);
705 * handle_callback - add a demote request to a lock's queue
707 * @state: the state the caller wants us to change to
709 * Note: This may fail sliently if we are out of memory.
712 static void handle_callback(struct gfs2_glock *gl, unsigned int state)
714 struct gfs2_holder *gh, *new_gh = NULL;
717 spin_lock(&gl->gl_spin);
719 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
720 if (test_bit(HIF_DEMOTE, &gh->gh_iflags) &&
721 gl->gl_req_gh != gh) {
722 if (gh->gh_state != state)
723 gh->gh_state = LM_ST_UNLOCKED;
729 list_add_tail(&new_gh->gh_list, &gl->gl_waiters2);
732 spin_unlock(&gl->gl_spin);
734 new_gh = gfs2_holder_get(gl, state, LM_FLAG_TRY, GFP_KERNEL);
737 set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
738 set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
744 spin_unlock(&gl->gl_spin);
747 gfs2_holder_put(new_gh);
750 void gfs2_glock_inode_squish(struct inode *inode)
752 struct gfs2_holder gh;
753 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
754 gfs2_holder_init(gl, LM_ST_UNLOCKED, 0, &gh);
755 set_bit(HIF_DEMOTE, &gh.gh_iflags);
756 spin_lock(&gl->gl_spin);
757 gfs2_assert(inode->i_sb->s_fs_info, list_empty(&gl->gl_holders));
758 list_add_tail(&gh.gh_list, &gl->gl_waiters2);
760 spin_unlock(&gl->gl_spin);
761 wait_for_completion(&gh.gh_wait);
762 gfs2_holder_uninit(&gh);
766 * state_change - record that the glock is now in a different state
768 * @new_state the new state
772 static void state_change(struct gfs2_glock *gl, unsigned int new_state)
776 held1 = (gl->gl_state != LM_ST_UNLOCKED);
777 held2 = (new_state != LM_ST_UNLOCKED);
779 if (held1 != held2) {
786 gl->gl_state = new_state;
790 * xmote_bh - Called after the lock module is done acquiring a lock
791 * @gl: The glock in question
792 * @ret: the int returned from the lock module
796 static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
798 struct gfs2_sbd *sdp = gl->gl_sbd;
799 const struct gfs2_glock_operations *glops = gl->gl_ops;
800 struct gfs2_holder *gh = gl->gl_req_gh;
801 int prev_state = gl->gl_state;
804 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
805 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
806 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
808 state_change(gl, ret & LM_OUT_ST_MASK);
810 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
812 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
813 } else if (gl->gl_state == LM_ST_DEFERRED) {
814 /* We might not want to do this here.
815 Look at moving to the inode glops. */
817 glops->go_inval(gl, DIO_DATA);
820 /* Deal with each possible exit condition */
823 gl->gl_stamp = jiffies;
824 else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
825 spin_lock(&gl->gl_spin);
826 list_del_init(&gh->gh_list);
828 spin_unlock(&gl->gl_spin);
829 } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
830 spin_lock(&gl->gl_spin);
831 list_del_init(&gh->gh_list);
832 if (gl->gl_state == gh->gh_state ||
833 gl->gl_state == LM_ST_UNLOCKED)
836 if (gfs2_assert_warn(sdp, gh->gh_flags &
837 (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
838 fs_warn(sdp, "ret = 0x%.8X\n", ret);
839 gh->gh_error = GLR_TRYFAILED;
841 spin_unlock(&gl->gl_spin);
843 if (ret & LM_OUT_CANCELED)
844 handle_callback(gl, LM_ST_UNLOCKED);
846 } else if (ret & LM_OUT_CANCELED) {
847 spin_lock(&gl->gl_spin);
848 list_del_init(&gh->gh_list);
849 gh->gh_error = GLR_CANCELED;
850 spin_unlock(&gl->gl_spin);
852 } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
853 spin_lock(&gl->gl_spin);
854 list_move_tail(&gh->gh_list, &gl->gl_holders);
856 set_bit(HIF_HOLDER, &gh->gh_iflags);
857 spin_unlock(&gl->gl_spin);
859 set_bit(HIF_FIRST, &gh->gh_iflags);
863 } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
864 spin_lock(&gl->gl_spin);
865 list_del_init(&gh->gh_list);
866 gh->gh_error = GLR_TRYFAILED;
867 spin_unlock(&gl->gl_spin);
870 if (gfs2_assert_withdraw(sdp, 0) == -1)
871 fs_err(sdp, "ret = 0x%.8X\n", ret);
874 if (glops->go_xmote_bh)
875 glops->go_xmote_bh(gl);
878 spin_lock(&gl->gl_spin);
879 gl->gl_req_gh = NULL;
880 gl->gl_req_bh = NULL;
881 clear_bit(GLF_LOCK, &gl->gl_flags);
883 spin_unlock(&gl->gl_spin);
889 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
892 complete(&gh->gh_wait);
897 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
898 * @gl: The glock in question
899 * @state: the requested state
900 * @flags: modifier flags to the lock call
904 void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags)
906 struct gfs2_sbd *sdp = gl->gl_sbd;
907 const struct gfs2_glock_operations *glops = gl->gl_ops;
908 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
909 LM_FLAG_NOEXP | LM_FLAG_ANY |
911 unsigned int lck_ret;
913 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
914 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
915 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
916 gfs2_assert_warn(sdp, state != gl->gl_state);
918 if (gl->gl_state == LM_ST_EXCLUSIVE && glops->go_sync)
919 glops->go_sync(gl, DIO_METADATA | DIO_DATA | DIO_RELEASE);
922 gl->gl_req_bh = xmote_bh;
924 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state, lck_flags);
926 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
929 if (lck_ret & LM_OUT_ASYNC)
930 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
932 xmote_bh(gl, lck_ret);
936 * drop_bh - Called after a lock module unlock completes
938 * @ret: the return status
940 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
941 * Doesn't drop the reference on the glock the top half took out
945 static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
947 struct gfs2_sbd *sdp = gl->gl_sbd;
948 const struct gfs2_glock_operations *glops = gl->gl_ops;
949 struct gfs2_holder *gh = gl->gl_req_gh;
951 clear_bit(GLF_PREFETCH, &gl->gl_flags);
953 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
954 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
955 gfs2_assert_warn(sdp, !ret);
957 state_change(gl, LM_ST_UNLOCKED);
960 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
963 spin_lock(&gl->gl_spin);
964 list_del_init(&gh->gh_list);
966 spin_unlock(&gl->gl_spin);
969 if (glops->go_drop_bh)
970 glops->go_drop_bh(gl);
972 spin_lock(&gl->gl_spin);
973 gl->gl_req_gh = NULL;
974 gl->gl_req_bh = NULL;
975 clear_bit(GLF_LOCK, &gl->gl_flags);
977 spin_unlock(&gl->gl_spin);
982 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
985 complete(&gh->gh_wait);
990 * gfs2_glock_drop_th - call into the lock module to unlock a lock
995 void gfs2_glock_drop_th(struct gfs2_glock *gl)
997 struct gfs2_sbd *sdp = gl->gl_sbd;
998 const struct gfs2_glock_operations *glops = gl->gl_ops;
1001 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1002 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
1003 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
1005 if (gl->gl_state == LM_ST_EXCLUSIVE && glops->go_sync)
1006 glops->go_sync(gl, DIO_METADATA | DIO_DATA | DIO_RELEASE);
1008 gfs2_glock_hold(gl);
1009 gl->gl_req_bh = drop_bh;
1011 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
1013 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
1019 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
1023 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1024 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1026 * Don't cancel GL_NOCANCEL requests.
1029 static void do_cancels(struct gfs2_holder *gh)
1031 struct gfs2_glock *gl = gh->gh_gl;
1033 spin_lock(&gl->gl_spin);
1035 while (gl->gl_req_gh != gh &&
1036 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1037 !list_empty(&gh->gh_list)) {
1038 if (gl->gl_req_bh && !(gl->gl_req_gh &&
1039 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
1040 spin_unlock(&gl->gl_spin);
1041 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
1043 spin_lock(&gl->gl_spin);
1045 spin_unlock(&gl->gl_spin);
1047 spin_lock(&gl->gl_spin);
1051 spin_unlock(&gl->gl_spin);
1055 * glock_wait_internal - wait on a glock acquisition
1056 * @gh: the glock holder
1058 * Returns: 0 on success
1061 static int glock_wait_internal(struct gfs2_holder *gh)
1063 struct gfs2_glock *gl = gh->gh_gl;
1064 struct gfs2_sbd *sdp = gl->gl_sbd;
1065 const struct gfs2_glock_operations *glops = gl->gl_ops;
1067 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1070 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1071 spin_lock(&gl->gl_spin);
1072 if (gl->gl_req_gh != gh &&
1073 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1074 !list_empty(&gh->gh_list)) {
1075 list_del_init(&gh->gh_list);
1076 gh->gh_error = GLR_TRYFAILED;
1078 spin_unlock(&gl->gl_spin);
1079 return gh->gh_error;
1081 spin_unlock(&gl->gl_spin);
1084 if (gh->gh_flags & LM_FLAG_PRIORITY)
1087 wait_for_completion(&gh->gh_wait);
1090 return gh->gh_error;
1092 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
1093 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state,
1097 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1098 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1100 if (glops->go_lock) {
1101 gh->gh_error = glops->go_lock(gh);
1103 spin_lock(&gl->gl_spin);
1104 list_del_init(&gh->gh_list);
1105 spin_unlock(&gl->gl_spin);
1109 spin_lock(&gl->gl_spin);
1110 gl->gl_req_gh = NULL;
1111 gl->gl_req_bh = NULL;
1112 clear_bit(GLF_LOCK, &gl->gl_flags);
1114 spin_unlock(&gl->gl_spin);
1117 return gh->gh_error;
1120 static inline struct gfs2_holder *
1121 find_holder_by_owner(struct list_head *head, struct task_struct *owner)
1123 struct gfs2_holder *gh;
1125 list_for_each_entry(gh, head, gh_list) {
1126 if (gh->gh_owner == owner)
1134 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1135 * @gh: the holder structure to add
1139 static void add_to_queue(struct gfs2_holder *gh)
1141 struct gfs2_glock *gl = gh->gh_gl;
1142 struct gfs2_holder *existing;
1144 BUG_ON(!gh->gh_owner);
1146 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
1148 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1149 printk(KERN_INFO "pid : %d\n", existing->gh_owner->pid);
1150 printk(KERN_INFO "lock type : %d lock state : %d\n",
1151 existing->gh_gl->gl_name.ln_type, existing->gh_gl->gl_state);
1152 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1153 printk(KERN_INFO "pid : %d\n", gh->gh_owner->pid);
1154 printk(KERN_INFO "lock type : %d lock state : %d\n",
1155 gl->gl_name.ln_type, gl->gl_state);
1159 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
1161 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1162 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1166 if (gh->gh_flags & LM_FLAG_PRIORITY)
1167 list_add(&gh->gh_list, &gl->gl_waiters3);
1169 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1173 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1174 * @gh: the holder structure
1176 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1178 * Returns: 0, GLR_TRYFAILED, or errno on failure
1181 int gfs2_glock_nq(struct gfs2_holder *gh)
1183 struct gfs2_glock *gl = gh->gh_gl;
1184 struct gfs2_sbd *sdp = gl->gl_sbd;
1188 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1189 set_bit(HIF_ABORTED, &gh->gh_iflags);
1193 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1195 spin_lock(&gl->gl_spin);
1198 spin_unlock(&gl->gl_spin);
1200 if (!(gh->gh_flags & GL_ASYNC)) {
1201 error = glock_wait_internal(gh);
1202 if (error == GLR_CANCELED) {
1208 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1210 if (error == GLR_TRYFAILED && (gh->gh_flags & GL_DUMP))
1217 * gfs2_glock_poll - poll to see if an async request has been completed
1220 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1223 int gfs2_glock_poll(struct gfs2_holder *gh)
1225 struct gfs2_glock *gl = gh->gh_gl;
1228 spin_lock(&gl->gl_spin);
1230 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1232 else if (list_empty(&gh->gh_list)) {
1233 if (gh->gh_error == GLR_CANCELED) {
1234 spin_unlock(&gl->gl_spin);
1236 if (gfs2_glock_nq(gh))
1243 spin_unlock(&gl->gl_spin);
1249 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1250 * @gh: the holder structure
1252 * Returns: 0, GLR_TRYFAILED, or errno on failure
1255 int gfs2_glock_wait(struct gfs2_holder *gh)
1259 error = glock_wait_internal(gh);
1260 if (error == GLR_CANCELED) {
1262 gh->gh_flags &= ~GL_ASYNC;
1263 error = gfs2_glock_nq(gh);
1270 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1271 * @gh: the glock holder
1275 void gfs2_glock_dq(struct gfs2_holder *gh)
1277 struct gfs2_glock *gl = gh->gh_gl;
1278 const struct gfs2_glock_operations *glops = gl->gl_ops;
1280 if (gh->gh_flags & GL_NOCACHE)
1281 handle_callback(gl, LM_ST_UNLOCKED);
1283 gfs2_glmutex_lock(gl);
1285 spin_lock(&gl->gl_spin);
1286 list_del_init(&gh->gh_list);
1288 if (list_empty(&gl->gl_holders)) {
1289 spin_unlock(&gl->gl_spin);
1291 if (glops->go_unlock)
1292 glops->go_unlock(gh);
1294 gl->gl_stamp = jiffies;
1296 spin_lock(&gl->gl_spin);
1299 clear_bit(GLF_LOCK, &gl->gl_flags);
1301 spin_unlock(&gl->gl_spin);
1305 * gfs2_glock_prefetch - Try to prefetch a glock
1307 * @state: the state to prefetch in
1308 * @flags: flags passed to go_xmote_th()
1312 static void gfs2_glock_prefetch(struct gfs2_glock *gl, unsigned int state,
1315 const struct gfs2_glock_operations *glops = gl->gl_ops;
1317 spin_lock(&gl->gl_spin);
1319 if (test_bit(GLF_LOCK, &gl->gl_flags) || !list_empty(&gl->gl_holders) ||
1320 !list_empty(&gl->gl_waiters1) || !list_empty(&gl->gl_waiters2) ||
1321 !list_empty(&gl->gl_waiters3) ||
1322 relaxed_state_ok(gl->gl_state, state, flags)) {
1323 spin_unlock(&gl->gl_spin);
1327 set_bit(GLF_PREFETCH, &gl->gl_flags);
1328 set_bit(GLF_LOCK, &gl->gl_flags);
1329 spin_unlock(&gl->gl_spin);
1331 glops->go_xmote_th(gl, state, flags);
1334 static void greedy_work(void *data)
1336 struct greedy *gr = data;
1337 struct gfs2_holder *gh = &gr->gr_gh;
1338 struct gfs2_glock *gl = gh->gh_gl;
1339 const struct gfs2_glock_operations *glops = gl->gl_ops;
1341 clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1343 if (glops->go_greedy)
1344 glops->go_greedy(gl);
1346 spin_lock(&gl->gl_spin);
1348 if (list_empty(&gl->gl_waiters2)) {
1349 clear_bit(GLF_GREEDY, &gl->gl_flags);
1350 spin_unlock(&gl->gl_spin);
1351 gfs2_holder_uninit(gh);
1354 gfs2_glock_hold(gl);
1355 list_add_tail(&gh->gh_list, &gl->gl_waiters2);
1357 spin_unlock(&gl->gl_spin);
1363 * gfs2_glock_be_greedy -
1367 * Returns: 0 if go_greedy will be called, 1 otherwise
1370 int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time)
1373 struct gfs2_holder *gh;
1375 if (!time || gl->gl_sbd->sd_args.ar_localcaching ||
1376 test_and_set_bit(GLF_GREEDY, &gl->gl_flags))
1379 gr = kmalloc(sizeof(struct greedy), GFP_KERNEL);
1381 clear_bit(GLF_GREEDY, &gl->gl_flags);
1386 gfs2_holder_init(gl, 0, 0, gh);
1387 set_bit(HIF_GREEDY, &gh->gh_iflags);
1388 INIT_WORK(&gr->gr_work, greedy_work, gr);
1390 set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1391 schedule_delayed_work(&gr->gr_work, time);
1397 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1398 * @gh: the holder structure
1402 void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1405 gfs2_holder_uninit(gh);
1409 * gfs2_glock_nq_num - acquire a glock based on lock number
1410 * @sdp: the filesystem
1411 * @number: the lock number
1412 * @glops: the glock operations for the type of glock
1413 * @state: the state to acquire the glock in
1414 * @flags: modifier flags for the aquisition
1415 * @gh: the struct gfs2_holder
1420 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
1421 const struct gfs2_glock_operations *glops,
1422 unsigned int state, int flags, struct gfs2_holder *gh)
1424 struct gfs2_glock *gl;
1427 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1429 error = gfs2_glock_nq_init(gl, state, flags, gh);
1437 * glock_compare - Compare two struct gfs2_glock structures for sorting
1438 * @arg_a: the first structure
1439 * @arg_b: the second structure
1443 static int glock_compare(const void *arg_a, const void *arg_b)
1445 struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
1446 struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
1447 struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1448 struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1451 if (a->ln_number > b->ln_number)
1453 else if (a->ln_number < b->ln_number)
1456 if (gh_a->gh_state == LM_ST_SHARED &&
1457 gh_b->gh_state == LM_ST_EXCLUSIVE)
1459 else if (!(gh_a->gh_flags & GL_LOCAL_EXCL) &&
1460 (gh_b->gh_flags & GL_LOCAL_EXCL))
1468 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1469 * @num_gh: the number of structures
1470 * @ghs: an array of struct gfs2_holder structures
1472 * Returns: 0 on success (all glocks acquired),
1473 * errno on failure (no glocks acquired)
1476 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1477 struct gfs2_holder **p)
1482 for (x = 0; x < num_gh; x++)
1485 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1487 for (x = 0; x < num_gh; x++) {
1488 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1490 error = gfs2_glock_nq(p[x]);
1493 gfs2_glock_dq(p[x]);
1502 * gfs2_glock_nq_m - acquire multiple glocks
1503 * @num_gh: the number of structures
1504 * @ghs: an array of struct gfs2_holder structures
1506 * Figure out how big an impact this function has. Either:
1507 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1508 * 2) Forget async stuff and just call nq_m_sync()
1509 * 3) Leave it like it is
1511 * Returns: 0 on success (all glocks acquired),
1512 * errno on failure (no glocks acquired)
1515 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1519 int borked = 0, serious = 0;
1526 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1527 return gfs2_glock_nq(ghs);
1530 e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1534 for (x = 0; x < num_gh; x++) {
1535 ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
1536 error = gfs2_glock_nq(&ghs[x]);
1545 for (x = 0; x < num_gh; x++) {
1546 error = e[x] = glock_wait_internal(&ghs[x]);
1549 if (error != GLR_TRYFAILED && error != GLR_CANCELED)
1559 for (x = 0; x < num_gh; x++)
1561 gfs2_glock_dq(&ghs[x]);
1566 for (x = 0; x < num_gh; x++)
1567 gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
1569 error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
1578 * gfs2_glock_dq_m - release multiple glocks
1579 * @num_gh: the number of structures
1580 * @ghs: an array of struct gfs2_holder structures
1584 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1588 for (x = 0; x < num_gh; x++)
1589 gfs2_glock_dq(&ghs[x]);
1593 * gfs2_glock_dq_uninit_m - release multiple glocks
1594 * @num_gh: the number of structures
1595 * @ghs: an array of struct gfs2_holder structures
1599 void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1603 for (x = 0; x < num_gh; x++)
1604 gfs2_glock_dq_uninit(&ghs[x]);
1608 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1609 * @sdp: the filesystem
1610 * @number: the lock number
1611 * @glops: the glock operations for the type of glock
1612 * @state: the state to acquire the glock in
1613 * @flags: modifier flags for the aquisition
1618 void gfs2_glock_prefetch_num(struct gfs2_sbd *sdp, u64 number,
1619 const struct gfs2_glock_operations *glops,
1620 unsigned int state, int flags)
1622 struct gfs2_glock *gl;
1625 if (atomic_read(&sdp->sd_reclaim_count) <
1626 gfs2_tune_get(sdp, gt_reclaim_limit)) {
1627 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1629 gfs2_glock_prefetch(gl, state, flags);
1636 * gfs2_lvb_hold - attach a LVB from a glock
1637 * @gl: The glock in question
1641 int gfs2_lvb_hold(struct gfs2_glock *gl)
1645 gfs2_glmutex_lock(gl);
1647 if (!atomic_read(&gl->gl_lvb_count)) {
1648 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1650 gfs2_glmutex_unlock(gl);
1653 gfs2_glock_hold(gl);
1655 atomic_inc(&gl->gl_lvb_count);
1657 gfs2_glmutex_unlock(gl);
1663 * gfs2_lvb_unhold - detach a LVB from a glock
1664 * @gl: The glock in question
1668 void gfs2_lvb_unhold(struct gfs2_glock *gl)
1670 gfs2_glock_hold(gl);
1671 gfs2_glmutex_lock(gl);
1673 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1674 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1675 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1680 gfs2_glmutex_unlock(gl);
1684 static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1687 struct gfs2_glock *gl;
1689 gl = gfs2_glock_find(sdp, name);
1693 if (gl->gl_ops->go_callback)
1694 gl->gl_ops->go_callback(gl, state);
1695 handle_callback(gl, state);
1697 spin_lock(&gl->gl_spin);
1699 spin_unlock(&gl->gl_spin);
1705 * gfs2_glock_cb - Callback used by locking module
1706 * @fsdata: Pointer to the superblock
1707 * @type: Type of callback
1708 * @data: Type dependent data pointer
1710 * Called by the locking module when it wants to tell us something.
1711 * Either we need to drop a lock, one of our ASYNC requests completed, or
1712 * a journal from another client needs to be recovered.
1715 void gfs2_glock_cb(lm_fsdata_t *fsdata, unsigned int type, void *data)
1717 struct gfs2_sbd *sdp = (struct gfs2_sbd *)fsdata;
1721 blocking_cb(sdp, data, LM_ST_UNLOCKED);
1725 blocking_cb(sdp, data, LM_ST_DEFERRED);
1729 blocking_cb(sdp, data, LM_ST_SHARED);
1733 struct lm_async_cb *async = data;
1734 struct gfs2_glock *gl;
1736 gl = gfs2_glock_find(sdp, &async->lc_name);
1737 if (gfs2_assert_warn(sdp, gl))
1739 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1740 gl->gl_req_bh(gl, async->lc_ret);
1745 case LM_CB_NEED_RECOVERY:
1746 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1747 if (sdp->sd_recoverd_process)
1748 wake_up_process(sdp->sd_recoverd_process);
1751 case LM_CB_DROPLOCKS:
1752 gfs2_gl_hash_clear(sdp, NO_WAIT);
1753 gfs2_quota_scan(sdp);
1757 gfs2_assert_warn(sdp, 0);
1763 * gfs2_iopen_go_callback - Try to kick the inode/vnode associated with an
1764 * iopen glock from memory
1765 * @io_gl: the iopen glock
1766 * @state: the state into which the glock should be put
1770 void gfs2_iopen_go_callback(struct gfs2_glock *io_gl, unsigned int state)
1773 if (state != LM_ST_UNLOCKED)
1775 /* FIXME: remove this? */
1779 * demote_ok - Check to see if it's ok to unlock a glock
1782 * Returns: 1 if it's ok
1785 static int demote_ok(struct gfs2_glock *gl)
1787 struct gfs2_sbd *sdp = gl->gl_sbd;
1788 const struct gfs2_glock_operations *glops = gl->gl_ops;
1791 if (test_bit(GLF_STICKY, &gl->gl_flags))
1793 else if (test_bit(GLF_PREFETCH, &gl->gl_flags))
1794 demote = time_after_eq(jiffies, gl->gl_stamp +
1795 gfs2_tune_get(sdp, gt_prefetch_secs) * HZ);
1796 else if (glops->go_demote_ok)
1797 demote = glops->go_demote_ok(gl);
1803 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1808 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1810 struct gfs2_sbd *sdp = gl->gl_sbd;
1812 spin_lock(&sdp->sd_reclaim_lock);
1813 if (list_empty(&gl->gl_reclaim)) {
1814 gfs2_glock_hold(gl);
1815 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1816 atomic_inc(&sdp->sd_reclaim_count);
1818 spin_unlock(&sdp->sd_reclaim_lock);
1820 wake_up(&sdp->sd_reclaim_wq);
1824 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1825 * @sdp: the filesystem
1827 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1828 * different glock and we notice that there are a lot of glocks in the
1833 void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1835 struct gfs2_glock *gl;
1837 spin_lock(&sdp->sd_reclaim_lock);
1838 if (list_empty(&sdp->sd_reclaim_list)) {
1839 spin_unlock(&sdp->sd_reclaim_lock);
1842 gl = list_entry(sdp->sd_reclaim_list.next,
1843 struct gfs2_glock, gl_reclaim);
1844 list_del_init(&gl->gl_reclaim);
1845 spin_unlock(&sdp->sd_reclaim_lock);
1847 atomic_dec(&sdp->sd_reclaim_count);
1848 atomic_inc(&sdp->sd_reclaimed);
1850 if (gfs2_glmutex_trylock(gl)) {
1851 if (queue_empty(gl, &gl->gl_holders) &&
1852 gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
1853 handle_callback(gl, LM_ST_UNLOCKED);
1854 gfs2_glmutex_unlock(gl);
1861 * examine_bucket - Call a function for glock in a hash bucket
1862 * @examiner: the function
1863 * @sdp: the filesystem
1864 * @bucket: the bucket
1866 * Returns: 1 if the bucket has entries
1869 static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
1870 struct gfs2_gl_hash_bucket *bucket)
1872 struct glock_plug plug;
1873 struct list_head *tmp;
1874 struct gfs2_glock *gl;
1877 /* Add "plug" to end of bucket list, work back up list from there */
1878 memset(&plug.gl_flags, 0, sizeof(unsigned long));
1879 set_bit(GLF_PLUG, &plug.gl_flags);
1881 write_lock(&bucket->hb_lock);
1882 list_add(&plug.gl_list, &bucket->hb_list);
1883 write_unlock(&bucket->hb_lock);
1886 write_lock(&bucket->hb_lock);
1889 tmp = plug.gl_list.next;
1891 if (tmp == &bucket->hb_list) {
1892 list_del(&plug.gl_list);
1893 entries = !list_empty(&bucket->hb_list);
1894 write_unlock(&bucket->hb_lock);
1897 gl = list_entry(tmp, struct gfs2_glock, gl_list);
1899 /* Move plug up list */
1900 list_move(&plug.gl_list, &gl->gl_list);
1902 if (test_bit(GLF_PLUG, &gl->gl_flags))
1905 /* examiner() must glock_put() */
1906 gfs2_glock_hold(gl);
1911 write_unlock(&bucket->hb_lock);
1918 * scan_glock - look at a glock and see if we can reclaim it
1919 * @gl: the glock to look at
1923 static void scan_glock(struct gfs2_glock *gl)
1925 if (gl->gl_ops == &gfs2_inode_glops)
1928 if (gfs2_glmutex_trylock(gl)) {
1929 if (queue_empty(gl, &gl->gl_holders) &&
1930 gl->gl_state != LM_ST_UNLOCKED &&
1933 gfs2_glmutex_unlock(gl);
1940 gfs2_glmutex_unlock(gl);
1941 gfs2_glock_schedule_for_reclaim(gl);
1946 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
1947 * @sdp: the filesystem
1951 void gfs2_scand_internal(struct gfs2_sbd *sdp)
1955 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
1956 examine_bucket(scan_glock, sdp, &sdp->sd_gl_hash[x]);
1962 * clear_glock - look at a glock and see if we can free it from glock cache
1963 * @gl: the glock to look at
1967 static void clear_glock(struct gfs2_glock *gl)
1969 struct gfs2_sbd *sdp = gl->gl_sbd;
1972 spin_lock(&sdp->sd_reclaim_lock);
1973 if (!list_empty(&gl->gl_reclaim)) {
1974 list_del_init(&gl->gl_reclaim);
1975 atomic_dec(&sdp->sd_reclaim_count);
1976 spin_unlock(&sdp->sd_reclaim_lock);
1977 released = gfs2_glock_put(gl);
1978 gfs2_assert(sdp, !released);
1980 spin_unlock(&sdp->sd_reclaim_lock);
1983 if (gfs2_glmutex_trylock(gl)) {
1984 if (queue_empty(gl, &gl->gl_holders) &&
1985 gl->gl_state != LM_ST_UNLOCKED)
1986 handle_callback(gl, LM_ST_UNLOCKED);
1988 gfs2_glmutex_unlock(gl);
1995 * gfs2_gl_hash_clear - Empty out the glock hash table
1996 * @sdp: the filesystem
1997 * @wait: wait until it's all gone
1999 * Called when unmounting the filesystem, or when inter-node lock manager
2000 * requests DROPLOCKS because it is running out of capacity.
2003 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
2014 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
2015 if (examine_bucket(clear_glock, sdp, &sdp->sd_gl_hash[x]))
2021 if (time_after_eq(jiffies,
2022 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
2023 fs_warn(sdp, "Unmount seems to be stalled. "
2024 "Dumping lock state...\n");
2025 gfs2_dump_lockstate(sdp);
2029 invalidate_inodes(sdp->sd_vfs);
2035 * Diagnostic routines to help debug distributed deadlock
2039 * dump_holder - print information about a glock holder
2040 * @str: a string naming the type of holder
2041 * @gh: the glock holder
2043 * Returns: 0 on success, -ENOBUFS when we run out of space
2046 static int dump_holder(char *str, struct gfs2_holder *gh)
2049 int error = -ENOBUFS;
2051 printk(KERN_INFO " %s\n", str);
2052 printk(KERN_INFO " owner = %ld\n",
2053 (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
2054 printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
2055 printk(KERN_INFO " gh_flags =");
2056 for (x = 0; x < 32; x++)
2057 if (gh->gh_flags & (1 << x))
2060 printk(KERN_INFO " error = %d\n", gh->gh_error);
2061 printk(KERN_INFO " gh_iflags =");
2062 for (x = 0; x < 32; x++)
2063 if (test_bit(x, &gh->gh_iflags))
2066 print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip);
2074 * dump_inode - print information about an inode
2077 * Returns: 0 on success, -ENOBUFS when we run out of space
2080 static int dump_inode(struct gfs2_inode *ip)
2083 int error = -ENOBUFS;
2085 printk(KERN_INFO " Inode:\n");
2086 printk(KERN_INFO " num = %llu %llu\n",
2087 (unsigned long long)ip->i_num.no_formal_ino,
2088 (unsigned long long)ip->i_num.no_addr);
2089 printk(KERN_INFO " type = %u\n", IF2DT(ip->i_di.di_mode));
2090 printk(KERN_INFO " i_flags =");
2091 for (x = 0; x < 32; x++)
2092 if (test_bit(x, &ip->i_flags))
2102 * dump_glock - print information about a glock
2104 * @count: where we are in the buffer
2106 * Returns: 0 on success, -ENOBUFS when we run out of space
2109 static int dump_glock(struct gfs2_glock *gl)
2111 struct gfs2_holder *gh;
2113 int error = -ENOBUFS;
2115 spin_lock(&gl->gl_spin);
2117 printk(KERN_INFO "Glock 0x%p (%u, %llu)\n",
2119 gl->gl_name.ln_type,
2120 (unsigned long long)gl->gl_name.ln_number);
2121 printk(KERN_INFO " gl_flags =");
2122 for (x = 0; x < 32; x++)
2123 if (test_bit(x, &gl->gl_flags))
2126 printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref.refcount));
2127 printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
2128 printk(KERN_INFO " gl_owner = %s\n", gl->gl_owner->comm);
2129 print_symbol(KERN_INFO " gl_ip = %s\n", gl->gl_ip);
2130 printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
2131 printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
2132 printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
2133 printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
2134 printk(KERN_INFO " le = %s\n",
2135 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
2136 printk(KERN_INFO " reclaim = %s\n",
2137 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
2139 printk(KERN_INFO " aspace = 0x%p nrpages = %lu\n",
2141 gl->gl_aspace->i_mapping->nrpages);
2143 printk(KERN_INFO " aspace = no\n");
2144 printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
2145 if (gl->gl_req_gh) {
2146 error = dump_holder("Request", gl->gl_req_gh);
2150 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
2151 error = dump_holder("Holder", gh);
2155 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
2156 error = dump_holder("Waiter1", gh);
2160 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
2161 error = dump_holder("Waiter2", gh);
2165 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
2166 error = dump_holder("Waiter3", gh);
2170 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
2171 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
2172 list_empty(&gl->gl_holders)) {
2173 error = dump_inode(gl->gl_object);
2178 printk(KERN_INFO " Inode: busy\n");
2185 spin_unlock(&gl->gl_spin);
2190 * gfs2_dump_lockstate - print out the current lockstate
2191 * @sdp: the filesystem
2192 * @ub: the buffer to copy the information into
2194 * If @ub is NULL, dump the lockstate to the console.
2198 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
2200 struct gfs2_gl_hash_bucket *bucket;
2201 struct gfs2_glock *gl;
2205 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2206 bucket = &sdp->sd_gl_hash[x];
2208 read_lock(&bucket->hb_lock);
2210 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
2211 if (test_bit(GLF_PLUG, &gl->gl_flags))
2214 error = dump_glock(gl);
2219 read_unlock(&bucket->hb_lock);