2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License v.2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/delay.h>
16 #include <linux/sort.h>
17 #include <linux/jhash.h>
18 #include <linux/kref.h>
19 #include <linux/kallsyms.h>
20 #include <linux/gfs2_ondisk.h>
21 #include <asm/semaphore.h>
22 #include <asm/uaccess.h>
25 #include "lm_interface.h"
37 /* Must be kept in sync with the beginning of struct gfs2_glock */
39 struct list_head gl_list;
40 unsigned long gl_flags;
44 struct gfs2_holder gr_gh;
45 struct work_struct gr_work;
48 typedef void (*glock_examiner) (struct gfs2_glock * gl);
51 * relaxed_state_ok - is a requested lock compatible with the current lock mode?
52 * @actual: the current state of the lock
53 * @requested: the lock state that was requested by the caller
54 * @flags: the modifier flags passed in by the caller
56 * Returns: 1 if the locks are compatible, 0 otherwise
59 static inline int relaxed_state_ok(unsigned int actual, unsigned requested,
62 if (actual == requested)
68 if (actual == LM_ST_EXCLUSIVE && requested == LM_ST_SHARED)
71 if (actual != LM_ST_UNLOCKED && (flags & LM_FLAG_ANY))
78 * gl_hash() - Turn glock number into hash bucket number
79 * @lock: The glock number
81 * Returns: The number of the corresponding hash bucket
84 static unsigned int gl_hash(struct lm_lockname *name)
88 h = jhash(&name->ln_number, sizeof(uint64_t), 0);
89 h = jhash(&name->ln_type, sizeof(unsigned int), h);
90 h &= GFS2_GL_HASH_MASK;
96 * glock_free() - Perform a few checks and then release struct gfs2_glock
97 * @gl: The glock to release
99 * Also calls lock module to release its internal structure for this glock.
103 static void glock_free(struct gfs2_glock *gl)
105 struct gfs2_sbd *sdp = gl->gl_sbd;
106 struct inode *aspace = gl->gl_aspace;
108 gfs2_lm_put_lock(sdp, gl->gl_lock);
111 gfs2_aspace_put(aspace);
113 kmem_cache_free(gfs2_glock_cachep, gl);
117 * gfs2_glock_hold() - increment reference count on glock
118 * @gl: The glock to hold
122 void gfs2_glock_hold(struct gfs2_glock *gl)
124 kref_get(&gl->gl_ref);
127 /* All work is done after the return from kref_put() so we
128 can release the write_lock before the free. */
130 static void kill_glock(struct kref *kref)
132 struct gfs2_glock *gl = container_of(kref, struct gfs2_glock, gl_ref);
133 struct gfs2_sbd *sdp = gl->gl_sbd;
135 gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED);
136 gfs2_assert(sdp, list_empty(&gl->gl_reclaim));
137 gfs2_assert(sdp, list_empty(&gl->gl_holders));
138 gfs2_assert(sdp, list_empty(&gl->gl_waiters1));
139 gfs2_assert(sdp, list_empty(&gl->gl_waiters2));
140 gfs2_assert(sdp, list_empty(&gl->gl_waiters3));
144 * gfs2_glock_put() - Decrement reference count on glock
145 * @gl: The glock to put
149 int gfs2_glock_put(struct gfs2_glock *gl)
151 struct gfs2_sbd *sdp = gl->gl_sbd;
152 struct gfs2_gl_hash_bucket *bucket = gl->gl_bucket;
155 mutex_lock(&sdp->sd_invalidate_inodes_mutex);
157 write_lock(&bucket->hb_lock);
158 if (kref_put(&gl->gl_ref, kill_glock)) {
159 list_del_init(&gl->gl_list);
160 write_unlock(&bucket->hb_lock);
161 BUG_ON(spin_is_locked(&gl->gl_spin));
166 write_unlock(&bucket->hb_lock);
168 mutex_unlock(&sdp->sd_invalidate_inodes_mutex);
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 struct lm_lockname *name)
207 struct gfs2_glock *gl;
209 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
210 if (test_bit(GLF_PLUG, &gl->gl_flags))
212 if (!lm_name_equal(&gl->gl_name, name))
215 kref_get(&gl->gl_ref);
224 * gfs2_glock_find() - Find glock by lock number
225 * @sdp: The GFS2 superblock
226 * @name: The lock name
228 * Returns: NULL, or the struct gfs2_glock with the requested number
231 struct gfs2_glock *gfs2_glock_find(struct gfs2_sbd *sdp,
232 struct lm_lockname *name)
234 struct gfs2_gl_hash_bucket *bucket = &sdp->sd_gl_hash[gl_hash(name)];
235 struct gfs2_glock *gl;
237 read_lock(&bucket->hb_lock);
238 gl = search_bucket(bucket, name);
239 read_unlock(&bucket->hb_lock);
245 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
246 * @sdp: The GFS2 superblock
247 * @number: the lock number
248 * @glops: The glock_operations to use
249 * @create: If 0, don't create the glock if it doesn't exist
250 * @glp: the glock is returned here
252 * This does not lock a glock, just finds/creates structures for one.
257 int gfs2_glock_get(struct gfs2_sbd *sdp, uint64_t number,
258 struct gfs2_glock_operations *glops, int create,
259 struct gfs2_glock **glp)
261 struct lm_lockname name;
262 struct gfs2_glock *gl, *tmp;
263 struct gfs2_gl_hash_bucket *bucket;
266 name.ln_number = number;
267 name.ln_type = glops->go_type;
268 bucket = &sdp->sd_gl_hash[gl_hash(&name)];
270 read_lock(&bucket->hb_lock);
271 gl = search_bucket(bucket, &name);
272 read_unlock(&bucket->hb_lock);
279 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
283 memset(gl, 0, sizeof(struct gfs2_glock));
285 INIT_LIST_HEAD(&gl->gl_list);
287 kref_init(&gl->gl_ref);
289 spin_lock_init(&gl->gl_spin);
291 gl->gl_state = LM_ST_UNLOCKED;
292 INIT_LIST_HEAD(&gl->gl_holders);
293 INIT_LIST_HEAD(&gl->gl_waiters1);
294 INIT_LIST_HEAD(&gl->gl_waiters2);
295 INIT_LIST_HEAD(&gl->gl_waiters3);
299 gl->gl_bucket = bucket;
300 INIT_LIST_HEAD(&gl->gl_reclaim);
304 lops_init_le(&gl->gl_le, &gfs2_glock_lops);
305 INIT_LIST_HEAD(&gl->gl_ail_list);
307 /* If this glock protects actual on-disk data or metadata blocks,
308 create a VFS inode to manage the pages/buffers holding them. */
309 if (glops == &gfs2_inode_glops ||
310 glops == &gfs2_rgrp_glops ||
311 glops == &gfs2_meta_glops) {
312 gl->gl_aspace = gfs2_aspace_get(sdp);
313 if (!gl->gl_aspace) {
319 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
323 write_lock(&bucket->hb_lock);
324 tmp = search_bucket(bucket, &name);
326 write_unlock(&bucket->hb_lock);
330 list_add_tail(&gl->gl_list, &bucket->hb_list);
331 write_unlock(&bucket->hb_lock);
340 gfs2_aspace_put(gl->gl_aspace);
343 kmem_cache_free(gfs2_glock_cachep, gl);
349 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
351 * @state: the state we're requesting
352 * @flags: the modifier flags
353 * @gh: the holder structure
357 void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
358 struct gfs2_holder *gh)
360 INIT_LIST_HEAD(&gh->gh_list);
362 gh->gh_ip = (unsigned long)__builtin_return_address(0);
363 gh->gh_owner = current;
364 gh->gh_state = state;
365 gh->gh_flags = flags;
368 init_completion(&gh->gh_wait);
370 if (gh->gh_state == LM_ST_EXCLUSIVE)
371 gh->gh_flags |= GL_LOCAL_EXCL;
377 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
378 * @state: the state we're requesting
379 * @flags: the modifier flags
380 * @gh: the holder structure
382 * Don't mess with the glock.
386 void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
388 gh->gh_state = state;
389 gh->gh_flags = flags;
390 if (gh->gh_state == LM_ST_EXCLUSIVE)
391 gh->gh_flags |= GL_LOCAL_EXCL;
393 gh->gh_iflags &= 1 << HIF_ALLOCED;
394 gh->gh_ip = (unsigned long)__builtin_return_address(0);
398 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
399 * @gh: the holder structure
403 void gfs2_holder_uninit(struct gfs2_holder *gh)
405 gfs2_glock_put(gh->gh_gl);
411 * gfs2_holder_get - get a struct gfs2_holder structure
413 * @state: the state we're requesting
414 * @flags: the modifier flags
415 * @gfp_flags: __GFP_NOFAIL
417 * Figure out how big an impact this function has. Either:
418 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
419 * 2) Leave it like it is
421 * Returns: the holder structure, NULL on ENOMEM
424 struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl, unsigned int state,
425 int flags, gfp_t gfp_flags)
427 struct gfs2_holder *gh;
429 gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags);
433 gfs2_holder_init(gl, state, flags, gh);
434 set_bit(HIF_ALLOCED, &gh->gh_iflags);
435 gh->gh_ip = (unsigned long)__builtin_return_address(0);
440 * gfs2_holder_put - get rid of a struct gfs2_holder structure
441 * @gh: the holder structure
445 void gfs2_holder_put(struct gfs2_holder *gh)
447 gfs2_holder_uninit(gh);
452 * rq_mutex - process a mutex request in the queue
453 * @gh: the glock holder
455 * Returns: 1 if the queue is blocked
458 static int rq_mutex(struct gfs2_holder *gh)
460 struct gfs2_glock *gl = gh->gh_gl;
462 list_del_init(&gh->gh_list);
463 /* gh->gh_error never examined. */
464 set_bit(GLF_LOCK, &gl->gl_flags);
465 complete(&gh->gh_wait);
471 * rq_promote - process a promote request in the queue
472 * @gh: the glock holder
474 * Acquire a new inter-node lock, or change a lock state to more restrictive.
476 * Returns: 1 if the queue is blocked
479 static int rq_promote(struct gfs2_holder *gh)
481 struct gfs2_glock *gl = gh->gh_gl;
482 struct gfs2_sbd *sdp = gl->gl_sbd;
483 struct gfs2_glock_operations *glops = gl->gl_ops;
485 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
486 if (list_empty(&gl->gl_holders)) {
488 set_bit(GLF_LOCK, &gl->gl_flags);
489 spin_unlock(&gl->gl_spin);
491 if (atomic_read(&sdp->sd_reclaim_count) >
492 gfs2_tune_get(sdp, gt_reclaim_limit) &&
493 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
494 gfs2_reclaim_glock(sdp);
495 gfs2_reclaim_glock(sdp);
498 glops->go_xmote_th(gl, gh->gh_state,
501 spin_lock(&gl->gl_spin);
506 if (list_empty(&gl->gl_holders)) {
507 set_bit(HIF_FIRST, &gh->gh_iflags);
508 set_bit(GLF_LOCK, &gl->gl_flags);
510 struct gfs2_holder *next_gh;
511 if (gh->gh_flags & GL_LOCAL_EXCL)
513 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
515 if (next_gh->gh_flags & GL_LOCAL_EXCL)
519 list_move_tail(&gh->gh_list, &gl->gl_holders);
521 set_bit(HIF_HOLDER, &gh->gh_iflags);
523 complete(&gh->gh_wait);
529 * rq_demote - process a demote request in the queue
530 * @gh: the glock holder
532 * Returns: 1 if the queue is blocked
535 static int rq_demote(struct gfs2_holder *gh)
537 struct gfs2_glock *gl = gh->gh_gl;
538 struct gfs2_glock_operations *glops = gl->gl_ops;
540 if (!list_empty(&gl->gl_holders))
543 if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) {
544 list_del_init(&gh->gh_list);
546 spin_unlock(&gl->gl_spin);
547 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
550 complete(&gh->gh_wait);
551 spin_lock(&gl->gl_spin);
554 set_bit(GLF_LOCK, &gl->gl_flags);
555 spin_unlock(&gl->gl_spin);
557 if (gh->gh_state == LM_ST_UNLOCKED ||
558 gl->gl_state != LM_ST_EXCLUSIVE)
559 glops->go_drop_th(gl);
561 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
563 spin_lock(&gl->gl_spin);
570 * rq_greedy - process a queued request to drop greedy status
571 * @gh: the glock holder
573 * Returns: 1 if the queue is blocked
576 static int rq_greedy(struct gfs2_holder *gh)
578 struct gfs2_glock *gl = gh->gh_gl;
580 list_del_init(&gh->gh_list);
581 /* gh->gh_error never examined. */
582 clear_bit(GLF_GREEDY, &gl->gl_flags);
583 spin_unlock(&gl->gl_spin);
585 gfs2_holder_uninit(gh);
586 kfree(container_of(gh, struct greedy, gr_gh));
588 spin_lock(&gl->gl_spin);
594 * run_queue - process holder structures on a glock
598 static void run_queue(struct gfs2_glock *gl)
600 struct gfs2_holder *gh;
604 if (test_bit(GLF_LOCK, &gl->gl_flags))
607 if (!list_empty(&gl->gl_waiters1)) {
608 gh = list_entry(gl->gl_waiters1.next,
609 struct gfs2_holder, gh_list);
611 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
612 blocked = rq_mutex(gh);
614 gfs2_assert_warn(gl->gl_sbd, 0);
616 } else if (!list_empty(&gl->gl_waiters2) &&
617 !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) {
618 gh = list_entry(gl->gl_waiters2.next,
619 struct gfs2_holder, gh_list);
621 if (test_bit(HIF_DEMOTE, &gh->gh_iflags))
622 blocked = rq_demote(gh);
623 else if (test_bit(HIF_GREEDY, &gh->gh_iflags))
624 blocked = rq_greedy(gh);
626 gfs2_assert_warn(gl->gl_sbd, 0);
628 } else if (!list_empty(&gl->gl_waiters3)) {
629 gh = list_entry(gl->gl_waiters3.next,
630 struct gfs2_holder, gh_list);
632 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
633 blocked = rq_promote(gh);
635 gfs2_assert_warn(gl->gl_sbd, 0);
646 * gfs2_glmutex_lock - acquire a local lock on a glock
649 * Gives caller exclusive access to manipulate a glock structure.
652 void gfs2_glmutex_lock(struct gfs2_glock *gl)
654 struct gfs2_holder gh;
656 gfs2_holder_init(gl, 0, 0, &gh);
657 set_bit(HIF_MUTEX, &gh.gh_iflags);
659 spin_lock(&gl->gl_spin);
660 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
661 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
663 complete(&gh.gh_wait);
664 spin_unlock(&gl->gl_spin);
666 wait_for_completion(&gh.gh_wait);
667 gfs2_holder_uninit(&gh);
671 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
674 * Returns: 1 if the glock is acquired
677 int gfs2_glmutex_trylock(struct gfs2_glock *gl)
681 spin_lock(&gl->gl_spin);
682 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
684 spin_unlock(&gl->gl_spin);
690 * gfs2_glmutex_unlock - release a local lock on a glock
695 void gfs2_glmutex_unlock(struct gfs2_glock *gl)
697 spin_lock(&gl->gl_spin);
698 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
711 static void handle_callback(struct gfs2_glock *gl, unsigned int state)
713 struct gfs2_holder *gh, *new_gh = NULL;
716 spin_lock(&gl->gl_spin);
718 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
719 if (test_bit(HIF_DEMOTE, &gh->gh_iflags) &&
720 gl->gl_req_gh != gh) {
721 if (gh->gh_state != state)
722 gh->gh_state = LM_ST_UNLOCKED;
728 list_add_tail(&new_gh->gh_list, &gl->gl_waiters2);
731 spin_unlock(&gl->gl_spin);
733 new_gh = gfs2_holder_get(gl, state, LM_FLAG_TRY,
734 GFP_KERNEL | __GFP_NOFAIL),
735 set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
736 set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
742 spin_unlock(&gl->gl_spin);
745 gfs2_holder_put(new_gh);
749 * state_change - record that the glock is now in a different state
751 * @new_state the new state
755 static void state_change(struct gfs2_glock *gl, unsigned int new_state)
759 held1 = (gl->gl_state != LM_ST_UNLOCKED);
760 held2 = (new_state != LM_ST_UNLOCKED);
762 if (held1 != held2) {
769 gl->gl_state = new_state;
773 * xmote_bh - Called after the lock module is done acquiring a lock
774 * @gl: The glock in question
775 * @ret: the int returned from the lock module
779 static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
781 struct gfs2_sbd *sdp = gl->gl_sbd;
782 struct gfs2_glock_operations *glops = gl->gl_ops;
783 struct gfs2_holder *gh = gl->gl_req_gh;
784 int prev_state = gl->gl_state;
787 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
788 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
789 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
791 state_change(gl, ret & LM_OUT_ST_MASK);
793 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
795 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
796 } else if (gl->gl_state == LM_ST_DEFERRED) {
797 /* We might not want to do this here.
798 Look at moving to the inode glops. */
800 glops->go_inval(gl, DIO_DATA);
803 /* Deal with each possible exit condition */
806 gl->gl_stamp = jiffies;
808 else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
809 spin_lock(&gl->gl_spin);
810 list_del_init(&gh->gh_list);
812 spin_unlock(&gl->gl_spin);
814 } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
815 spin_lock(&gl->gl_spin);
816 list_del_init(&gh->gh_list);
817 if (gl->gl_state == gh->gh_state ||
818 gl->gl_state == LM_ST_UNLOCKED)
821 if (gfs2_assert_warn(sdp, gh->gh_flags &
822 (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
823 fs_warn(sdp, "ret = 0x%.8X\n", ret);
824 gh->gh_error = GLR_TRYFAILED;
826 spin_unlock(&gl->gl_spin);
828 if (ret & LM_OUT_CANCELED)
829 handle_callback(gl, LM_ST_UNLOCKED); /* Lame */
831 } else if (ret & LM_OUT_CANCELED) {
832 spin_lock(&gl->gl_spin);
833 list_del_init(&gh->gh_list);
834 gh->gh_error = GLR_CANCELED;
835 spin_unlock(&gl->gl_spin);
837 } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
838 spin_lock(&gl->gl_spin);
839 list_move_tail(&gh->gh_list, &gl->gl_holders);
841 set_bit(HIF_HOLDER, &gh->gh_iflags);
842 spin_unlock(&gl->gl_spin);
844 set_bit(HIF_FIRST, &gh->gh_iflags);
848 } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
849 spin_lock(&gl->gl_spin);
850 list_del_init(&gh->gh_list);
851 gh->gh_error = GLR_TRYFAILED;
852 spin_unlock(&gl->gl_spin);
855 if (gfs2_assert_withdraw(sdp, 0) == -1)
856 fs_err(sdp, "ret = 0x%.8X\n", ret);
859 if (glops->go_xmote_bh)
860 glops->go_xmote_bh(gl);
863 spin_lock(&gl->gl_spin);
864 gl->gl_req_gh = NULL;
865 gl->gl_req_bh = NULL;
866 clear_bit(GLF_LOCK, &gl->gl_flags);
868 spin_unlock(&gl->gl_spin);
874 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
877 complete(&gh->gh_wait);
882 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
883 * @gl: The glock in question
884 * @state: the requested state
885 * @flags: modifier flags to the lock call
889 void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags)
891 struct gfs2_sbd *sdp = gl->gl_sbd;
892 struct gfs2_glock_operations *glops = gl->gl_ops;
893 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
894 LM_FLAG_NOEXP | LM_FLAG_ANY |
896 unsigned int lck_ret;
898 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
899 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
900 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
901 gfs2_assert_warn(sdp, state != gl->gl_state);
903 if (gl->gl_state == LM_ST_EXCLUSIVE) {
906 DIO_METADATA | DIO_DATA | DIO_RELEASE);
910 gl->gl_req_bh = xmote_bh;
912 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state,
915 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
918 if (lck_ret & LM_OUT_ASYNC)
919 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
921 xmote_bh(gl, lck_ret);
925 * drop_bh - Called after a lock module unlock completes
927 * @ret: the return status
929 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
930 * Doesn't drop the reference on the glock the top half took out
934 static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
936 struct gfs2_sbd *sdp = gl->gl_sbd;
937 struct gfs2_glock_operations *glops = gl->gl_ops;
938 struct gfs2_holder *gh = gl->gl_req_gh;
940 clear_bit(GLF_PREFETCH, &gl->gl_flags);
942 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
943 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
944 gfs2_assert_warn(sdp, !ret);
946 state_change(gl, LM_ST_UNLOCKED);
949 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
952 spin_lock(&gl->gl_spin);
953 list_del_init(&gh->gh_list);
955 spin_unlock(&gl->gl_spin);
958 if (glops->go_drop_bh)
959 glops->go_drop_bh(gl);
961 spin_lock(&gl->gl_spin);
962 gl->gl_req_gh = NULL;
963 gl->gl_req_bh = NULL;
964 clear_bit(GLF_LOCK, &gl->gl_flags);
966 spin_unlock(&gl->gl_spin);
971 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
974 complete(&gh->gh_wait);
979 * gfs2_glock_drop_th - call into the lock module to unlock a lock
984 void gfs2_glock_drop_th(struct gfs2_glock *gl)
986 struct gfs2_sbd *sdp = gl->gl_sbd;
987 struct gfs2_glock_operations *glops = gl->gl_ops;
990 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
991 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
992 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
994 if (gl->gl_state == LM_ST_EXCLUSIVE) {
997 DIO_METADATA | DIO_DATA | DIO_RELEASE);
1000 gfs2_glock_hold(gl);
1001 gl->gl_req_bh = drop_bh;
1003 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
1005 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
1011 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
1015 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1016 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1018 * Don't cancel GL_NOCANCEL requests.
1021 static void do_cancels(struct gfs2_holder *gh)
1023 struct gfs2_glock *gl = gh->gh_gl;
1025 spin_lock(&gl->gl_spin);
1027 while (gl->gl_req_gh != gh &&
1028 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1029 !list_empty(&gh->gh_list)) {
1030 if (gl->gl_req_bh &&
1032 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
1033 spin_unlock(&gl->gl_spin);
1034 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
1036 spin_lock(&gl->gl_spin);
1038 spin_unlock(&gl->gl_spin);
1040 spin_lock(&gl->gl_spin);
1044 spin_unlock(&gl->gl_spin);
1048 * glock_wait_internal - wait on a glock acquisition
1049 * @gh: the glock holder
1051 * Returns: 0 on success
1054 static int glock_wait_internal(struct gfs2_holder *gh)
1056 struct gfs2_glock *gl = gh->gh_gl;
1057 struct gfs2_sbd *sdp = gl->gl_sbd;
1058 struct gfs2_glock_operations *glops = gl->gl_ops;
1060 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1063 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1064 spin_lock(&gl->gl_spin);
1065 if (gl->gl_req_gh != gh &&
1066 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1067 !list_empty(&gh->gh_list)) {
1068 list_del_init(&gh->gh_list);
1069 gh->gh_error = GLR_TRYFAILED;
1071 spin_unlock(&gl->gl_spin);
1072 return gh->gh_error;
1074 spin_unlock(&gl->gl_spin);
1077 if (gh->gh_flags & LM_FLAG_PRIORITY)
1080 wait_for_completion(&gh->gh_wait);
1083 return gh->gh_error;
1085 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
1086 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state,
1090 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1091 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1093 if (glops->go_lock) {
1094 gh->gh_error = glops->go_lock(gh);
1096 spin_lock(&gl->gl_spin);
1097 list_del_init(&gh->gh_list);
1098 spin_unlock(&gl->gl_spin);
1102 spin_lock(&gl->gl_spin);
1103 gl->gl_req_gh = NULL;
1104 gl->gl_req_bh = NULL;
1105 clear_bit(GLF_LOCK, &gl->gl_flags);
1107 spin_unlock(&gl->gl_spin);
1110 return gh->gh_error;
1113 static inline struct gfs2_holder *
1114 find_holder_by_owner(struct list_head *head, struct task_struct *owner)
1116 struct gfs2_holder *gh;
1118 list_for_each_entry(gh, head, gh_list) {
1119 if (gh->gh_owner == owner)
1127 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1128 * @gh: the holder structure to add
1132 static void add_to_queue(struct gfs2_holder *gh)
1134 struct gfs2_glock *gl = gh->gh_gl;
1135 struct gfs2_holder *existing;
1137 BUG_ON(!gh->gh_owner);
1139 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
1141 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1142 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1146 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
1148 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1149 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1153 if (gh->gh_flags & LM_FLAG_PRIORITY)
1154 list_add(&gh->gh_list, &gl->gl_waiters3);
1156 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1160 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1161 * @gh: the holder structure
1163 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1165 * Returns: 0, GLR_TRYFAILED, or errno on failure
1168 int gfs2_glock_nq(struct gfs2_holder *gh)
1170 struct gfs2_glock *gl = gh->gh_gl;
1171 struct gfs2_sbd *sdp = gl->gl_sbd;
1175 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1176 set_bit(HIF_ABORTED, &gh->gh_iflags);
1180 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1182 spin_lock(&gl->gl_spin);
1185 spin_unlock(&gl->gl_spin);
1187 if (!(gh->gh_flags & GL_ASYNC)) {
1188 error = glock_wait_internal(gh);
1189 if (error == GLR_CANCELED) {
1195 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1201 * gfs2_glock_poll - poll to see if an async request has been completed
1204 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1207 int gfs2_glock_poll(struct gfs2_holder *gh)
1209 struct gfs2_glock *gl = gh->gh_gl;
1212 spin_lock(&gl->gl_spin);
1214 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1216 else if (list_empty(&gh->gh_list)) {
1217 if (gh->gh_error == GLR_CANCELED) {
1218 spin_unlock(&gl->gl_spin);
1220 if (gfs2_glock_nq(gh))
1227 spin_unlock(&gl->gl_spin);
1233 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1234 * @gh: the holder structure
1236 * Returns: 0, GLR_TRYFAILED, or errno on failure
1239 int gfs2_glock_wait(struct gfs2_holder *gh)
1243 error = glock_wait_internal(gh);
1244 if (error == GLR_CANCELED) {
1246 gh->gh_flags &= ~GL_ASYNC;
1247 error = gfs2_glock_nq(gh);
1254 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1255 * @gh: the glock holder
1259 void gfs2_glock_dq(struct gfs2_holder *gh)
1261 struct gfs2_glock *gl = gh->gh_gl;
1262 struct gfs2_glock_operations *glops = gl->gl_ops;
1264 if (gh->gh_flags & GL_SYNC)
1265 set_bit(GLF_SYNC, &gl->gl_flags);
1267 if (gh->gh_flags & GL_NOCACHE)
1268 handle_callback(gl, LM_ST_UNLOCKED);
1270 gfs2_glmutex_lock(gl);
1272 spin_lock(&gl->gl_spin);
1273 list_del_init(&gh->gh_list);
1275 if (list_empty(&gl->gl_holders)) {
1276 spin_unlock(&gl->gl_spin);
1278 if (glops->go_unlock)
1279 glops->go_unlock(gh);
1281 if (test_bit(GLF_SYNC, &gl->gl_flags)) {
1283 glops->go_sync(gl, DIO_METADATA | DIO_DATA);
1286 gl->gl_stamp = jiffies;
1288 spin_lock(&gl->gl_spin);
1291 clear_bit(GLF_LOCK, &gl->gl_flags);
1293 spin_unlock(&gl->gl_spin);
1297 * gfs2_glock_prefetch - Try to prefetch a glock
1299 * @state: the state to prefetch in
1300 * @flags: flags passed to go_xmote_th()
1304 void gfs2_glock_prefetch(struct gfs2_glock *gl, unsigned int state, int flags)
1306 struct gfs2_glock_operations *glops = gl->gl_ops;
1308 spin_lock(&gl->gl_spin);
1310 if (test_bit(GLF_LOCK, &gl->gl_flags) ||
1311 !list_empty(&gl->gl_holders) ||
1312 !list_empty(&gl->gl_waiters1) ||
1313 !list_empty(&gl->gl_waiters2) ||
1314 !list_empty(&gl->gl_waiters3) ||
1315 relaxed_state_ok(gl->gl_state, state, flags)) {
1316 spin_unlock(&gl->gl_spin);
1320 set_bit(GLF_PREFETCH, &gl->gl_flags);
1321 set_bit(GLF_LOCK, &gl->gl_flags);
1322 spin_unlock(&gl->gl_spin);
1324 glops->go_xmote_th(gl, state, flags);
1328 * gfs2_glock_force_drop - Force a glock to be uncached
1333 void gfs2_glock_force_drop(struct gfs2_glock *gl)
1335 struct gfs2_holder gh;
1337 gfs2_holder_init(gl, LM_ST_UNLOCKED, 0, &gh);
1338 set_bit(HIF_DEMOTE, &gh.gh_iflags);
1340 spin_lock(&gl->gl_spin);
1341 list_add_tail(&gh.gh_list, &gl->gl_waiters2);
1343 spin_unlock(&gl->gl_spin);
1345 wait_for_completion(&gh.gh_wait);
1346 gfs2_holder_uninit(&gh);
1349 static void greedy_work(void *data)
1351 struct greedy *gr = data;
1352 struct gfs2_holder *gh = &gr->gr_gh;
1353 struct gfs2_glock *gl = gh->gh_gl;
1354 struct gfs2_glock_operations *glops = gl->gl_ops;
1356 clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1358 if (glops->go_greedy)
1359 glops->go_greedy(gl);
1361 spin_lock(&gl->gl_spin);
1363 if (list_empty(&gl->gl_waiters2)) {
1364 clear_bit(GLF_GREEDY, &gl->gl_flags);
1365 spin_unlock(&gl->gl_spin);
1366 gfs2_holder_uninit(gh);
1369 gfs2_glock_hold(gl);
1370 list_add_tail(&gh->gh_list, &gl->gl_waiters2);
1372 spin_unlock(&gl->gl_spin);
1378 * gfs2_glock_be_greedy -
1382 * Returns: 0 if go_greedy will be called, 1 otherwise
1385 int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time)
1388 struct gfs2_holder *gh;
1391 gl->gl_sbd->sd_args.ar_localcaching ||
1392 test_and_set_bit(GLF_GREEDY, &gl->gl_flags))
1395 gr = kmalloc(sizeof(struct greedy), GFP_KERNEL);
1397 clear_bit(GLF_GREEDY, &gl->gl_flags);
1402 gfs2_holder_init(gl, 0, 0, gh);
1403 set_bit(HIF_GREEDY, &gh->gh_iflags);
1404 INIT_WORK(&gr->gr_work, greedy_work, gr);
1406 set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1407 schedule_delayed_work(&gr->gr_work, time);
1413 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1414 * @gh: the holder structure
1418 void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1421 gfs2_holder_uninit(gh);
1425 * gfs2_glock_nq_num - acquire a glock based on lock number
1426 * @sdp: the filesystem
1427 * @number: the lock number
1428 * @glops: the glock operations for the type of glock
1429 * @state: the state to acquire the glock in
1430 * @flags: modifier flags for the aquisition
1431 * @gh: the struct gfs2_holder
1436 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, uint64_t number,
1437 struct gfs2_glock_operations *glops, unsigned int state,
1438 int flags, struct gfs2_holder *gh)
1440 struct gfs2_glock *gl;
1443 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1445 error = gfs2_glock_nq_init(gl, state, flags, gh);
1453 * glock_compare - Compare two struct gfs2_glock structures for sorting
1454 * @arg_a: the first structure
1455 * @arg_b: the second structure
1459 static int glock_compare(const void *arg_a, const void *arg_b)
1461 struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
1462 struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
1463 struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1464 struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1467 if (a->ln_number > b->ln_number)
1469 else if (a->ln_number < b->ln_number)
1472 if (gh_a->gh_state == LM_ST_SHARED &&
1473 gh_b->gh_state == LM_ST_EXCLUSIVE)
1475 else if (!(gh_a->gh_flags & GL_LOCAL_EXCL) &&
1476 (gh_b->gh_flags & GL_LOCAL_EXCL))
1484 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1485 * @num_gh: the number of structures
1486 * @ghs: an array of struct gfs2_holder structures
1488 * Returns: 0 on success (all glocks acquired),
1489 * errno on failure (no glocks acquired)
1492 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1493 struct gfs2_holder **p)
1498 for (x = 0; x < num_gh; x++)
1501 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1503 for (x = 0; x < num_gh; x++) {
1504 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1506 error = gfs2_glock_nq(p[x]);
1509 gfs2_glock_dq(p[x]);
1518 * gfs2_glock_nq_m - acquire multiple glocks
1519 * @num_gh: the number of structures
1520 * @ghs: an array of struct gfs2_holder structures
1522 * Figure out how big an impact this function has. Either:
1523 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1524 * 2) Forget async stuff and just call nq_m_sync()
1525 * 3) Leave it like it is
1527 * Returns: 0 on success (all glocks acquired),
1528 * errno on failure (no glocks acquired)
1531 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1535 int borked = 0, serious = 0;
1542 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1543 return gfs2_glock_nq(ghs);
1546 e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1550 for (x = 0; x < num_gh; x++) {
1551 ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
1552 error = gfs2_glock_nq(&ghs[x]);
1561 for (x = 0; x < num_gh; x++) {
1562 error = e[x] = glock_wait_internal(&ghs[x]);
1565 if (error != GLR_TRYFAILED && error != GLR_CANCELED)
1575 for (x = 0; x < num_gh; x++)
1577 gfs2_glock_dq(&ghs[x]);
1582 for (x = 0; x < num_gh; x++)
1583 gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
1585 error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
1594 * gfs2_glock_dq_m - release multiple glocks
1595 * @num_gh: the number of structures
1596 * @ghs: an array of struct gfs2_holder structures
1600 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1604 for (x = 0; x < num_gh; x++)
1605 gfs2_glock_dq(&ghs[x]);
1609 * gfs2_glock_dq_uninit_m - release multiple glocks
1610 * @num_gh: the number of structures
1611 * @ghs: an array of struct gfs2_holder structures
1615 void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1619 for (x = 0; x < num_gh; x++)
1620 gfs2_glock_dq_uninit(&ghs[x]);
1624 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1625 * @sdp: the filesystem
1626 * @number: the lock number
1627 * @glops: the glock operations for the type of glock
1628 * @state: the state to acquire the glock in
1629 * @flags: modifier flags for the aquisition
1634 void gfs2_glock_prefetch_num(struct gfs2_sbd *sdp, uint64_t number,
1635 struct gfs2_glock_operations *glops,
1636 unsigned int state, int flags)
1638 struct gfs2_glock *gl;
1641 if (atomic_read(&sdp->sd_reclaim_count) <
1642 gfs2_tune_get(sdp, gt_reclaim_limit)) {
1643 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1645 gfs2_glock_prefetch(gl, state, flags);
1652 * gfs2_lvb_hold - attach a LVB from a glock
1653 * @gl: The glock in question
1657 int gfs2_lvb_hold(struct gfs2_glock *gl)
1661 gfs2_glmutex_lock(gl);
1663 if (!atomic_read(&gl->gl_lvb_count)) {
1664 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1666 gfs2_glmutex_unlock(gl);
1669 gfs2_glock_hold(gl);
1671 atomic_inc(&gl->gl_lvb_count);
1673 gfs2_glmutex_unlock(gl);
1679 * gfs2_lvb_unhold - detach a LVB from a glock
1680 * @gl: The glock in question
1684 void gfs2_lvb_unhold(struct gfs2_glock *gl)
1686 gfs2_glock_hold(gl);
1687 gfs2_glmutex_lock(gl);
1689 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1690 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1691 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1696 gfs2_glmutex_unlock(gl);
1700 void gfs2_lvb_sync(struct gfs2_glock *gl)
1702 gfs2_glmutex_lock(gl);
1704 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count));
1705 if (!gfs2_assert_warn(gl->gl_sbd, gfs2_glock_is_held_excl(gl)))
1706 gfs2_lm_sync_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1708 gfs2_glmutex_unlock(gl);
1711 static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1714 struct gfs2_glock *gl;
1716 gl = gfs2_glock_find(sdp, name);
1720 if (gl->gl_ops->go_callback)
1721 gl->gl_ops->go_callback(gl, state);
1722 handle_callback(gl, state);
1724 spin_lock(&gl->gl_spin);
1726 spin_unlock(&gl->gl_spin);
1732 * gfs2_glock_cb - Callback used by locking module
1733 * @fsdata: Pointer to the superblock
1734 * @type: Type of callback
1735 * @data: Type dependent data pointer
1737 * Called by the locking module when it wants to tell us something.
1738 * Either we need to drop a lock, one of our ASYNC requests completed, or
1739 * a journal from another client needs to be recovered.
1742 void gfs2_glock_cb(lm_fsdata_t *fsdata, unsigned int type, void *data)
1744 struct gfs2_sbd *sdp = (struct gfs2_sbd *)fsdata;
1748 blocking_cb(sdp, data, LM_ST_UNLOCKED);
1752 blocking_cb(sdp, data, LM_ST_DEFERRED);
1756 blocking_cb(sdp, data, LM_ST_SHARED);
1760 struct lm_async_cb *async = data;
1761 struct gfs2_glock *gl;
1763 gl = gfs2_glock_find(sdp, &async->lc_name);
1764 if (gfs2_assert_warn(sdp, gl))
1766 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1767 gl->gl_req_bh(gl, async->lc_ret);
1772 case LM_CB_NEED_RECOVERY:
1773 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1774 if (sdp->sd_recoverd_process)
1775 wake_up_process(sdp->sd_recoverd_process);
1778 case LM_CB_DROPLOCKS:
1779 gfs2_gl_hash_clear(sdp, NO_WAIT);
1780 gfs2_quota_scan(sdp);
1784 gfs2_assert_warn(sdp, 0);
1790 * gfs2_try_toss_inode - try to remove a particular inode struct from cache
1791 * sdp: the filesystem
1792 * inum: the inode number
1796 void gfs2_try_toss_inode(struct gfs2_sbd *sdp, struct gfs2_inum *inum)
1798 struct gfs2_glock *gl;
1799 struct gfs2_inode *ip;
1802 error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_inode_glops,
1807 if (!gfs2_glmutex_trylock(gl))
1814 if (atomic_read(&ip->i_count))
1817 gfs2_inode_destroy(ip, 1);
1820 gfs2_glmutex_unlock(gl);
1827 * gfs2_iopen_go_callback - Try to kick the inode/vnode associated with an
1828 * iopen glock from memory
1829 * @io_gl: the iopen glock
1830 * @state: the state into which the glock should be put
1834 void gfs2_iopen_go_callback(struct gfs2_glock *io_gl, unsigned int state)
1836 struct gfs2_glock *i_gl;
1838 if (state != LM_ST_UNLOCKED)
1841 spin_lock(&io_gl->gl_spin);
1842 i_gl = io_gl->gl_object;
1844 gfs2_glock_hold(i_gl);
1845 spin_unlock(&io_gl->gl_spin);
1847 spin_unlock(&io_gl->gl_spin);
1851 if (gfs2_glmutex_trylock(i_gl)) {
1852 struct gfs2_inode *ip = i_gl->gl_object;
1854 gfs2_try_toss_vnode(ip);
1855 gfs2_glmutex_unlock(i_gl);
1856 gfs2_glock_schedule_for_reclaim(i_gl);
1859 gfs2_glmutex_unlock(i_gl);
1863 gfs2_glock_put(i_gl);
1867 * demote_ok - Check to see if it's ok to unlock a glock
1870 * Returns: 1 if it's ok
1873 static int demote_ok(struct gfs2_glock *gl)
1875 struct gfs2_sbd *sdp = gl->gl_sbd;
1876 struct gfs2_glock_operations *glops = gl->gl_ops;
1879 if (test_bit(GLF_STICKY, &gl->gl_flags))
1881 else if (test_bit(GLF_PREFETCH, &gl->gl_flags))
1882 demote = time_after_eq(jiffies,
1884 gfs2_tune_get(sdp, gt_prefetch_secs) * HZ);
1885 else if (glops->go_demote_ok)
1886 demote = glops->go_demote_ok(gl);
1892 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1897 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1899 struct gfs2_sbd *sdp = gl->gl_sbd;
1901 spin_lock(&sdp->sd_reclaim_lock);
1902 if (list_empty(&gl->gl_reclaim)) {
1903 gfs2_glock_hold(gl);
1904 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1905 atomic_inc(&sdp->sd_reclaim_count);
1907 spin_unlock(&sdp->sd_reclaim_lock);
1909 wake_up(&sdp->sd_reclaim_wq);
1913 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1914 * @sdp: the filesystem
1916 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1917 * different glock and we notice that there are a lot of glocks in the
1922 void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1924 struct gfs2_glock *gl;
1926 spin_lock(&sdp->sd_reclaim_lock);
1927 if (list_empty(&sdp->sd_reclaim_list)) {
1928 spin_unlock(&sdp->sd_reclaim_lock);
1931 gl = list_entry(sdp->sd_reclaim_list.next,
1932 struct gfs2_glock, gl_reclaim);
1933 list_del_init(&gl->gl_reclaim);
1934 spin_unlock(&sdp->sd_reclaim_lock);
1936 atomic_dec(&sdp->sd_reclaim_count);
1937 atomic_inc(&sdp->sd_reclaimed);
1939 if (gfs2_glmutex_trylock(gl)) {
1940 if (gl->gl_ops == &gfs2_inode_glops) {
1941 struct gfs2_inode *ip = gl->gl_object;
1942 if (ip && !atomic_read(&ip->i_count))
1943 gfs2_inode_destroy(ip, 1);
1945 if (queue_empty(gl, &gl->gl_holders) &&
1946 gl->gl_state != LM_ST_UNLOCKED &&
1948 handle_callback(gl, LM_ST_UNLOCKED);
1949 gfs2_glmutex_unlock(gl);
1956 * examine_bucket - Call a function for glock in a hash bucket
1957 * @examiner: the function
1958 * @sdp: the filesystem
1959 * @bucket: the bucket
1961 * Returns: 1 if the bucket has entries
1964 static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
1965 struct gfs2_gl_hash_bucket *bucket)
1967 struct glock_plug plug;
1968 struct list_head *tmp;
1969 struct gfs2_glock *gl;
1972 /* Add "plug" to end of bucket list, work back up list from there */
1973 memset(&plug.gl_flags, 0, sizeof(unsigned long));
1974 set_bit(GLF_PLUG, &plug.gl_flags);
1976 write_lock(&bucket->hb_lock);
1977 list_add(&plug.gl_list, &bucket->hb_list);
1978 write_unlock(&bucket->hb_lock);
1981 write_lock(&bucket->hb_lock);
1984 tmp = plug.gl_list.next;
1986 if (tmp == &bucket->hb_list) {
1987 list_del(&plug.gl_list);
1988 entries = !list_empty(&bucket->hb_list);
1989 write_unlock(&bucket->hb_lock);
1992 gl = list_entry(tmp, struct gfs2_glock, gl_list);
1994 /* Move plug up list */
1995 list_move(&plug.gl_list, &gl->gl_list);
1997 if (test_bit(GLF_PLUG, &gl->gl_flags))
2000 /* examiner() must glock_put() */
2001 gfs2_glock_hold(gl);
2006 write_unlock(&bucket->hb_lock);
2013 * scan_glock - look at a glock and see if we can reclaim it
2014 * @gl: the glock to look at
2018 static void scan_glock(struct gfs2_glock *gl)
2020 if (gfs2_glmutex_trylock(gl)) {
2021 if (gl->gl_ops == &gfs2_inode_glops) {
2022 struct gfs2_inode *ip = gl->gl_object;
2023 if (ip && !atomic_read(&ip->i_count))
2026 if (queue_empty(gl, &gl->gl_holders) &&
2027 gl->gl_state != LM_ST_UNLOCKED &&
2031 gfs2_glmutex_unlock(gl);
2039 gfs2_glmutex_unlock(gl);
2040 gfs2_glock_schedule_for_reclaim(gl);
2045 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
2046 * @sdp: the filesystem
2050 void gfs2_scand_internal(struct gfs2_sbd *sdp)
2054 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2055 examine_bucket(scan_glock, sdp, &sdp->sd_gl_hash[x]);
2061 * clear_glock - look at a glock and see if we can free it from glock cache
2062 * @gl: the glock to look at
2066 static void clear_glock(struct gfs2_glock *gl)
2068 struct gfs2_sbd *sdp = gl->gl_sbd;
2071 spin_lock(&sdp->sd_reclaim_lock);
2072 if (!list_empty(&gl->gl_reclaim)) {
2073 list_del_init(&gl->gl_reclaim);
2074 atomic_dec(&sdp->sd_reclaim_count);
2075 spin_unlock(&sdp->sd_reclaim_lock);
2076 released = gfs2_glock_put(gl);
2077 gfs2_assert(sdp, !released);
2079 spin_unlock(&sdp->sd_reclaim_lock);
2082 if (gfs2_glmutex_trylock(gl)) {
2083 if (gl->gl_ops == &gfs2_inode_glops) {
2084 struct gfs2_inode *ip = gl->gl_object;
2085 if (ip && !atomic_read(&ip->i_count))
2086 gfs2_inode_destroy(ip, 1);
2088 if (queue_empty(gl, &gl->gl_holders) &&
2089 gl->gl_state != LM_ST_UNLOCKED)
2090 handle_callback(gl, LM_ST_UNLOCKED);
2092 gfs2_glmutex_unlock(gl);
2099 * gfs2_gl_hash_clear - Empty out the glock hash table
2100 * @sdp: the filesystem
2101 * @wait: wait until it's all gone
2103 * Called when unmounting the filesystem, or when inter-node lock manager
2104 * requests DROPLOCKS because it is running out of capacity.
2107 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
2118 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
2119 if (examine_bucket(clear_glock, sdp,
2120 &sdp->sd_gl_hash[x]))
2126 if (time_after_eq(jiffies,
2127 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
2128 fs_warn(sdp, "Unmount seems to be stalled. "
2129 "Dumping lock state...\n");
2130 gfs2_dump_lockstate(sdp);
2134 /* invalidate_inodes() requires that the sb inodes list
2135 not change, but an async completion callback for an
2136 unlock can occur which does glock_put() which
2137 can call iput() which will change the sb inodes list.
2138 invalidate_inodes_mutex prevents glock_put()'s during
2139 an invalidate_inodes() */
2141 mutex_lock(&sdp->sd_invalidate_inodes_mutex);
2142 invalidate_inodes(sdp->sd_vfs);
2143 mutex_unlock(&sdp->sd_invalidate_inodes_mutex);
2149 * Diagnostic routines to help debug distributed deadlock
2153 * dump_holder - print information about a glock holder
2154 * @str: a string naming the type of holder
2155 * @gh: the glock holder
2157 * Returns: 0 on success, -ENOBUFS when we run out of space
2160 static int dump_holder(char *str, struct gfs2_holder *gh)
2163 int error = -ENOBUFS;
2165 printk(KERN_INFO " %s\n", str);
2166 printk(KERN_INFO " owner = %ld\n",
2167 (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
2168 printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
2169 printk(KERN_INFO " gh_flags =");
2170 for (x = 0; x < 32; x++)
2171 if (gh->gh_flags & (1 << x))
2174 printk(KERN_INFO " error = %d\n", gh->gh_error);
2175 printk(KERN_INFO " gh_iflags =");
2176 for (x = 0; x < 32; x++)
2177 if (test_bit(x, &gh->gh_iflags))
2180 print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip);
2188 * dump_inode - print information about an inode
2191 * Returns: 0 on success, -ENOBUFS when we run out of space
2194 static int dump_inode(struct gfs2_inode *ip)
2197 int error = -ENOBUFS;
2199 printk(KERN_INFO " Inode:\n");
2200 printk(KERN_INFO " num = %llu %llu\n",
2201 ip->i_num.no_formal_ino, ip->i_num.no_addr);
2202 printk(KERN_INFO " type = %u\n", IF2DT(ip->i_di.di_mode));
2203 printk(KERN_INFO " i_count = %d\n", atomic_read(&ip->i_count));
2204 printk(KERN_INFO " i_flags =");
2205 for (x = 0; x < 32; x++)
2206 if (test_bit(x, &ip->i_flags))
2209 printk(KERN_INFO " vnode = %s\n", (ip->i_vnode) ? "yes" : "no");
2217 * dump_glock - print information about a glock
2219 * @count: where we are in the buffer
2221 * Returns: 0 on success, -ENOBUFS when we run out of space
2224 static int dump_glock(struct gfs2_glock *gl)
2226 struct gfs2_holder *gh;
2228 int error = -ENOBUFS;
2230 spin_lock(&gl->gl_spin);
2232 printk(KERN_INFO "Glock (%u, %llu)\n",
2233 gl->gl_name.ln_type,
2234 gl->gl_name.ln_number);
2235 printk(KERN_INFO " gl_flags =");
2236 for (x = 0; x < 32; x++)
2237 if (test_bit(x, &gl->gl_flags))
2240 printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref.refcount));
2241 printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
2242 printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
2243 printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
2244 printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
2245 printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
2246 printk(KERN_INFO " le = %s\n",
2247 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
2248 printk(KERN_INFO " reclaim = %s\n",
2249 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
2251 printk(KERN_INFO " aspace = %lu\n",
2252 gl->gl_aspace->i_mapping->nrpages);
2254 printk(KERN_INFO " aspace = no\n");
2255 printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
2256 if (gl->gl_req_gh) {
2257 error = dump_holder("Request", gl->gl_req_gh);
2261 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
2262 error = dump_holder("Holder", gh);
2266 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
2267 error = dump_holder("Waiter1", gh);
2271 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
2272 error = dump_holder("Waiter2", gh);
2276 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
2277 error = dump_holder("Waiter3", gh);
2281 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
2282 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
2283 list_empty(&gl->gl_holders)) {
2284 error = dump_inode(gl->gl_object);
2289 printk(KERN_INFO " Inode: busy\n");
2296 spin_unlock(&gl->gl_spin);
2302 * gfs2_dump_lockstate - print out the current lockstate
2303 * @sdp: the filesystem
2304 * @ub: the buffer to copy the information into
2306 * If @ub is NULL, dump the lockstate to the console.
2310 int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
2312 struct gfs2_gl_hash_bucket *bucket;
2313 struct gfs2_glock *gl;
2317 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2318 bucket = &sdp->sd_gl_hash[x];
2320 read_lock(&bucket->hb_lock);
2322 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
2323 if (test_bit(GLF_PLUG, &gl->gl_flags))
2326 error = dump_glock(gl);
2331 read_unlock(&bucket->hb_lock);