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);
165 write_unlock(&bucket->hb_lock);
167 mutex_unlock(&sdp->sd_invalidate_inodes_mutex);
172 * queue_empty - check to see if a glock's queue is empty
174 * @head: the head of the queue to check
176 * This function protects the list in the event that a process already
177 * has a holder on the list and is adding a second holder for itself.
178 * The glmutex lock is what generally prevents processes from working
179 * on the same glock at once, but the special case of adding a second
180 * holder for yourself ("recursive" locking) doesn't involve locking
181 * glmutex, making the spin lock necessary.
183 * Returns: 1 if the queue is empty
186 static inline int queue_empty(struct gfs2_glock *gl, struct list_head *head)
189 spin_lock(&gl->gl_spin);
190 empty = list_empty(head);
191 spin_unlock(&gl->gl_spin);
196 * search_bucket() - Find struct gfs2_glock by lock number
197 * @bucket: the bucket to search
198 * @name: The lock name
200 * Returns: NULL, or the struct gfs2_glock with the requested number
203 static struct gfs2_glock *search_bucket(struct gfs2_gl_hash_bucket *bucket,
204 struct lm_lockname *name)
206 struct gfs2_glock *gl;
208 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
209 if (test_bit(GLF_PLUG, &gl->gl_flags))
211 if (!lm_name_equal(&gl->gl_name, name))
214 kref_get(&gl->gl_ref);
223 * gfs2_glock_find() - Find glock by lock number
224 * @sdp: The GFS2 superblock
225 * @name: The lock name
227 * Returns: NULL, or the struct gfs2_glock with the requested number
230 struct gfs2_glock *gfs2_glock_find(struct gfs2_sbd *sdp,
231 struct lm_lockname *name)
233 struct gfs2_gl_hash_bucket *bucket = &sdp->sd_gl_hash[gl_hash(name)];
234 struct gfs2_glock *gl;
236 read_lock(&bucket->hb_lock);
237 gl = search_bucket(bucket, name);
238 read_unlock(&bucket->hb_lock);
244 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
245 * @sdp: The GFS2 superblock
246 * @number: the lock number
247 * @glops: The glock_operations to use
248 * @create: If 0, don't create the glock if it doesn't exist
249 * @glp: the glock is returned here
251 * This does not lock a glock, just finds/creates structures for one.
256 int gfs2_glock_get(struct gfs2_sbd *sdp, uint64_t number,
257 struct gfs2_glock_operations *glops, int create,
258 struct gfs2_glock **glp)
260 struct lm_lockname name;
261 struct gfs2_glock *gl, *tmp;
262 struct gfs2_gl_hash_bucket *bucket;
265 name.ln_number = number;
266 name.ln_type = glops->go_type;
267 bucket = &sdp->sd_gl_hash[gl_hash(&name)];
269 read_lock(&bucket->hb_lock);
270 gl = search_bucket(bucket, &name);
271 read_unlock(&bucket->hb_lock);
278 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
282 memset(gl, 0, sizeof(struct gfs2_glock));
284 INIT_LIST_HEAD(&gl->gl_list);
286 kref_init(&gl->gl_ref);
288 spin_lock_init(&gl->gl_spin);
290 gl->gl_state = LM_ST_UNLOCKED;
291 INIT_LIST_HEAD(&gl->gl_holders);
292 INIT_LIST_HEAD(&gl->gl_waiters1);
293 INIT_LIST_HEAD(&gl->gl_waiters2);
294 INIT_LIST_HEAD(&gl->gl_waiters3);
298 gl->gl_bucket = bucket;
299 INIT_LIST_HEAD(&gl->gl_reclaim);
303 lops_init_le(&gl->gl_le, &gfs2_glock_lops);
304 INIT_LIST_HEAD(&gl->gl_ail_list);
306 /* If this glock protects actual on-disk data or metadata blocks,
307 create a VFS inode to manage the pages/buffers holding them. */
308 if (glops == &gfs2_inode_glops ||
309 glops == &gfs2_rgrp_glops ||
310 glops == &gfs2_meta_glops) {
311 gl->gl_aspace = gfs2_aspace_get(sdp);
312 if (!gl->gl_aspace) {
318 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
322 write_lock(&bucket->hb_lock);
323 tmp = search_bucket(bucket, &name);
325 write_unlock(&bucket->hb_lock);
329 list_add_tail(&gl->gl_list, &bucket->hb_list);
330 write_unlock(&bucket->hb_lock);
339 gfs2_aspace_put(gl->gl_aspace);
342 kmem_cache_free(gfs2_glock_cachep, gl);
348 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
350 * @state: the state we're requesting
351 * @flags: the modifier flags
352 * @gh: the holder structure
356 void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, int flags,
357 struct gfs2_holder *gh)
359 INIT_LIST_HEAD(&gh->gh_list);
361 gh->gh_ip = (unsigned long)__builtin_return_address(0);
362 gh->gh_owner = (flags & GL_NEVER_RECURSE) ? NULL : current;
363 gh->gh_state = state;
364 gh->gh_flags = flags;
367 init_completion(&gh->gh_wait);
369 if (gh->gh_state == LM_ST_EXCLUSIVE)
370 gh->gh_flags |= GL_LOCAL_EXCL;
376 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
377 * @state: the state we're requesting
378 * @flags: the modifier flags
379 * @gh: the holder structure
381 * Don't mess with the glock.
385 void gfs2_holder_reinit(unsigned int state, int flags, struct gfs2_holder *gh)
387 gh->gh_state = state;
388 gh->gh_flags = flags;
389 if (gh->gh_state == LM_ST_EXCLUSIVE)
390 gh->gh_flags |= GL_LOCAL_EXCL;
392 gh->gh_iflags &= 1 << HIF_ALLOCED;
393 gh->gh_ip = (unsigned long)__builtin_return_address(0);
397 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
398 * @gh: the holder structure
402 void gfs2_holder_uninit(struct gfs2_holder *gh)
404 gfs2_glock_put(gh->gh_gl);
410 * gfs2_holder_get - get a struct gfs2_holder structure
412 * @state: the state we're requesting
413 * @flags: the modifier flags
414 * @gfp_flags: __GFP_NOFAIL
416 * Figure out how big an impact this function has. Either:
417 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
418 * 2) Leave it like it is
420 * Returns: the holder structure, NULL on ENOMEM
423 struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl, unsigned int state,
424 int flags, gfp_t gfp_flags)
426 struct gfs2_holder *gh;
428 gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags);
432 gfs2_holder_init(gl, state, flags, gh);
433 set_bit(HIF_ALLOCED, &gh->gh_iflags);
434 gh->gh_ip = (unsigned long)__builtin_return_address(0);
439 * gfs2_holder_put - get rid of a struct gfs2_holder structure
440 * @gh: the holder structure
444 void gfs2_holder_put(struct gfs2_holder *gh)
446 gfs2_holder_uninit(gh);
451 * handle_recurse - put other holder structures (marked recursive)
452 * into the holders list
453 * @gh: the holder structure
457 static void handle_recurse(struct gfs2_holder *gh)
459 struct gfs2_glock *gl = gh->gh_gl;
460 struct gfs2_sbd *sdp = gl->gl_sbd;
461 struct gfs2_holder *tmp_gh, *safe;
464 printk(KERN_INFO "recursion %016llx, %u\n", gl->gl_name.ln_number,
465 gl->gl_name.ln_type);
467 if (gfs2_assert_warn(sdp, gh->gh_owner))
470 list_for_each_entry_safe(tmp_gh, safe, &gl->gl_waiters3, gh_list) {
471 if (tmp_gh->gh_owner != gh->gh_owner)
474 gfs2_assert_warn(sdp,
475 test_bit(HIF_RECURSE, &tmp_gh->gh_iflags));
477 list_move_tail(&tmp_gh->gh_list, &gl->gl_holders);
478 tmp_gh->gh_error = 0;
479 set_bit(HIF_HOLDER, &tmp_gh->gh_iflags);
481 complete(&tmp_gh->gh_wait);
486 gfs2_assert_warn(sdp, found);
490 * do_unrecurse - a recursive holder was just dropped of the waiters3 list
493 * If there is only one other recursive holder, clear its HIF_RECURSE bit.
494 * If there is more than one, leave them alone.
498 static void do_unrecurse(struct gfs2_holder *gh)
500 struct gfs2_glock *gl = gh->gh_gl;
501 struct gfs2_sbd *sdp = gl->gl_sbd;
502 struct gfs2_holder *tmp_gh, *last_gh = NULL;
505 if (gfs2_assert_warn(sdp, gh->gh_owner))
508 list_for_each_entry(tmp_gh, &gl->gl_waiters3, gh_list) {
509 if (tmp_gh->gh_owner != gh->gh_owner)
512 gfs2_assert_warn(sdp,
513 test_bit(HIF_RECURSE, &tmp_gh->gh_iflags));
522 if (!gfs2_assert_warn(sdp, found))
523 clear_bit(HIF_RECURSE, &last_gh->gh_iflags);
527 * rq_mutex - process a mutex request in the queue
528 * @gh: the glock holder
530 * Returns: 1 if the queue is blocked
533 static int rq_mutex(struct gfs2_holder *gh)
535 struct gfs2_glock *gl = gh->gh_gl;
537 list_del_init(&gh->gh_list);
538 /* gh->gh_error never examined. */
539 set_bit(GLF_LOCK, &gl->gl_flags);
540 complete(&gh->gh_wait);
546 * rq_promote - process a promote request in the queue
547 * @gh: the glock holder
549 * Acquire a new inter-node lock, or change a lock state to more restrictive.
551 * Returns: 1 if the queue is blocked
554 static int rq_promote(struct gfs2_holder *gh)
556 struct gfs2_glock *gl = gh->gh_gl;
557 struct gfs2_sbd *sdp = gl->gl_sbd;
558 struct gfs2_glock_operations *glops = gl->gl_ops;
561 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
562 if (list_empty(&gl->gl_holders)) {
564 set_bit(GLF_LOCK, &gl->gl_flags);
565 spin_unlock(&gl->gl_spin);
567 if (atomic_read(&sdp->sd_reclaim_count) >
568 gfs2_tune_get(sdp, gt_reclaim_limit) &&
569 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
570 gfs2_reclaim_glock(sdp);
571 gfs2_reclaim_glock(sdp);
574 glops->go_xmote_th(gl, gh->gh_state,
577 spin_lock(&gl->gl_spin);
582 if (list_empty(&gl->gl_holders)) {
583 set_bit(HIF_FIRST, &gh->gh_iflags);
584 set_bit(GLF_LOCK, &gl->gl_flags);
587 struct gfs2_holder *next_gh;
588 if (gh->gh_flags & GL_LOCAL_EXCL)
590 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
592 if (next_gh->gh_flags & GL_LOCAL_EXCL)
594 recurse = test_bit(HIF_RECURSE, &gh->gh_iflags);
597 list_move_tail(&gh->gh_list, &gl->gl_holders);
599 set_bit(HIF_HOLDER, &gh->gh_iflags);
604 complete(&gh->gh_wait);
610 * rq_demote - process a demote request in the queue
611 * @gh: the glock holder
613 * Returns: 1 if the queue is blocked
616 static int rq_demote(struct gfs2_holder *gh)
618 struct gfs2_glock *gl = gh->gh_gl;
619 struct gfs2_glock_operations *glops = gl->gl_ops;
621 if (!list_empty(&gl->gl_holders))
624 if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) {
625 list_del_init(&gh->gh_list);
627 spin_unlock(&gl->gl_spin);
628 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
631 complete(&gh->gh_wait);
632 spin_lock(&gl->gl_spin);
635 set_bit(GLF_LOCK, &gl->gl_flags);
636 spin_unlock(&gl->gl_spin);
638 if (gh->gh_state == LM_ST_UNLOCKED ||
639 gl->gl_state != LM_ST_EXCLUSIVE)
640 glops->go_drop_th(gl);
642 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
644 spin_lock(&gl->gl_spin);
651 * rq_greedy - process a queued request to drop greedy status
652 * @gh: the glock holder
654 * Returns: 1 if the queue is blocked
657 static int rq_greedy(struct gfs2_holder *gh)
659 struct gfs2_glock *gl = gh->gh_gl;
661 list_del_init(&gh->gh_list);
662 /* gh->gh_error never examined. */
663 clear_bit(GLF_GREEDY, &gl->gl_flags);
664 spin_unlock(&gl->gl_spin);
666 gfs2_holder_uninit(gh);
667 kfree(container_of(gh, struct greedy, gr_gh));
669 spin_lock(&gl->gl_spin);
675 * run_queue - process holder structures on a glock
680 static void run_queue(struct gfs2_glock *gl)
682 struct gfs2_holder *gh;
686 if (test_bit(GLF_LOCK, &gl->gl_flags))
689 if (!list_empty(&gl->gl_waiters1)) {
690 gh = list_entry(gl->gl_waiters1.next,
691 struct gfs2_holder, gh_list);
693 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
694 blocked = rq_mutex(gh);
696 gfs2_assert_warn(gl->gl_sbd, 0);
698 } else if (!list_empty(&gl->gl_waiters2) &&
699 !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) {
700 gh = list_entry(gl->gl_waiters2.next,
701 struct gfs2_holder, gh_list);
703 if (test_bit(HIF_DEMOTE, &gh->gh_iflags))
704 blocked = rq_demote(gh);
705 else if (test_bit(HIF_GREEDY, &gh->gh_iflags))
706 blocked = rq_greedy(gh);
708 gfs2_assert_warn(gl->gl_sbd, 0);
710 } else if (!list_empty(&gl->gl_waiters3)) {
711 gh = list_entry(gl->gl_waiters3.next,
712 struct gfs2_holder, gh_list);
714 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
715 blocked = rq_promote(gh);
717 gfs2_assert_warn(gl->gl_sbd, 0);
728 * gfs2_glmutex_lock - acquire a local lock on a glock
731 * Gives caller exclusive access to manipulate a glock structure.
734 void gfs2_glmutex_lock(struct gfs2_glock *gl)
736 struct gfs2_holder gh;
738 gfs2_holder_init(gl, 0, 0, &gh);
739 set_bit(HIF_MUTEX, &gh.gh_iflags);
741 spin_lock(&gl->gl_spin);
742 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
743 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
745 complete(&gh.gh_wait);
746 spin_unlock(&gl->gl_spin);
748 wait_for_completion(&gh.gh_wait);
749 gfs2_holder_uninit(&gh);
753 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
756 * Returns: 1 if the glock is acquired
759 int gfs2_glmutex_trylock(struct gfs2_glock *gl)
763 spin_lock(&gl->gl_spin);
764 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
766 spin_unlock(&gl->gl_spin);
772 * gfs2_glmutex_unlock - release a local lock on a glock
777 void gfs2_glmutex_unlock(struct gfs2_glock *gl)
779 spin_lock(&gl->gl_spin);
780 clear_bit(GLF_LOCK, &gl->gl_flags);
782 spin_unlock(&gl->gl_spin);
786 * handle_callback - add a demote request to a lock's queue
788 * @state: the state the caller wants us to change to
792 static void handle_callback(struct gfs2_glock *gl, unsigned int state)
794 struct gfs2_holder *gh, *new_gh = NULL;
797 spin_lock(&gl->gl_spin);
799 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
800 if (test_bit(HIF_DEMOTE, &gh->gh_iflags) &&
801 gl->gl_req_gh != gh) {
802 if (gh->gh_state != state)
803 gh->gh_state = LM_ST_UNLOCKED;
809 list_add_tail(&new_gh->gh_list, &gl->gl_waiters2);
812 spin_unlock(&gl->gl_spin);
814 new_gh = gfs2_holder_get(gl, state,
815 LM_FLAG_TRY | GL_NEVER_RECURSE,
816 GFP_KERNEL | __GFP_NOFAIL),
817 set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
818 set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
824 spin_unlock(&gl->gl_spin);
827 gfs2_holder_put(new_gh);
831 * state_change - record that the glock is now in a different state
833 * @new_state the new state
837 static void state_change(struct gfs2_glock *gl, unsigned int new_state)
841 held1 = (gl->gl_state != LM_ST_UNLOCKED);
842 held2 = (new_state != LM_ST_UNLOCKED);
844 if (held1 != held2) {
851 gl->gl_state = new_state;
855 * xmote_bh - Called after the lock module is done acquiring a lock
856 * @gl: The glock in question
857 * @ret: the int returned from the lock module
861 static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
863 struct gfs2_sbd *sdp = gl->gl_sbd;
864 struct gfs2_glock_operations *glops = gl->gl_ops;
865 struct gfs2_holder *gh = gl->gl_req_gh;
866 int prev_state = gl->gl_state;
869 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
870 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
871 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
873 state_change(gl, ret & LM_OUT_ST_MASK);
875 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
877 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
878 } else if (gl->gl_state == LM_ST_DEFERRED) {
879 /* We might not want to do this here.
880 Look at moving to the inode glops. */
882 glops->go_inval(gl, DIO_DATA);
885 /* Deal with each possible exit condition */
888 gl->gl_stamp = jiffies;
890 else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
891 spin_lock(&gl->gl_spin);
892 list_del_init(&gh->gh_list);
894 if (test_bit(HIF_RECURSE, &gh->gh_iflags))
896 spin_unlock(&gl->gl_spin);
898 } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
899 spin_lock(&gl->gl_spin);
900 list_del_init(&gh->gh_list);
901 if (gl->gl_state == gh->gh_state ||
902 gl->gl_state == LM_ST_UNLOCKED)
905 if (gfs2_assert_warn(sdp, gh->gh_flags &
906 (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
907 fs_warn(sdp, "ret = 0x%.8X\n", ret);
908 gh->gh_error = GLR_TRYFAILED;
910 spin_unlock(&gl->gl_spin);
912 if (ret & LM_OUT_CANCELED)
913 handle_callback(gl, LM_ST_UNLOCKED); /* Lame */
915 } else if (ret & LM_OUT_CANCELED) {
916 spin_lock(&gl->gl_spin);
917 list_del_init(&gh->gh_list);
918 gh->gh_error = GLR_CANCELED;
919 if (test_bit(HIF_RECURSE, &gh->gh_iflags))
921 spin_unlock(&gl->gl_spin);
923 } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
924 spin_lock(&gl->gl_spin);
925 list_move_tail(&gh->gh_list, &gl->gl_holders);
927 set_bit(HIF_HOLDER, &gh->gh_iflags);
928 spin_unlock(&gl->gl_spin);
930 set_bit(HIF_FIRST, &gh->gh_iflags);
934 } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
935 spin_lock(&gl->gl_spin);
936 list_del_init(&gh->gh_list);
937 gh->gh_error = GLR_TRYFAILED;
938 if (test_bit(HIF_RECURSE, &gh->gh_iflags))
940 spin_unlock(&gl->gl_spin);
943 if (gfs2_assert_withdraw(sdp, 0) == -1)
944 fs_err(sdp, "ret = 0x%.8X\n", ret);
947 if (glops->go_xmote_bh)
948 glops->go_xmote_bh(gl);
951 spin_lock(&gl->gl_spin);
952 gl->gl_req_gh = NULL;
953 gl->gl_req_bh = NULL;
954 clear_bit(GLF_LOCK, &gl->gl_flags);
956 spin_unlock(&gl->gl_spin);
962 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
965 complete(&gh->gh_wait);
970 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
971 * @gl: The glock in question
972 * @state: the requested state
973 * @flags: modifier flags to the lock call
977 void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags)
979 struct gfs2_sbd *sdp = gl->gl_sbd;
980 struct gfs2_glock_operations *glops = gl->gl_ops;
981 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
982 LM_FLAG_NOEXP | LM_FLAG_ANY |
984 unsigned int lck_ret;
986 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
987 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
988 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
989 gfs2_assert_warn(sdp, state != gl->gl_state);
991 if (gl->gl_state == LM_ST_EXCLUSIVE) {
994 DIO_METADATA | DIO_DATA | DIO_RELEASE);
998 gl->gl_req_bh = xmote_bh;
1000 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state,
1003 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
1006 if (lck_ret & LM_OUT_ASYNC)
1007 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
1009 xmote_bh(gl, lck_ret);
1013 * drop_bh - Called after a lock module unlock completes
1015 * @ret: the return status
1017 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
1018 * Doesn't drop the reference on the glock the top half took out
1022 static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
1024 struct gfs2_sbd *sdp = gl->gl_sbd;
1025 struct gfs2_glock_operations *glops = gl->gl_ops;
1026 struct gfs2_holder *gh = gl->gl_req_gh;
1028 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1030 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1031 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
1032 gfs2_assert_warn(sdp, !ret);
1034 state_change(gl, LM_ST_UNLOCKED);
1036 if (glops->go_inval)
1037 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
1040 spin_lock(&gl->gl_spin);
1041 list_del_init(&gh->gh_list);
1043 spin_unlock(&gl->gl_spin);
1046 if (glops->go_drop_bh)
1047 glops->go_drop_bh(gl);
1049 spin_lock(&gl->gl_spin);
1050 gl->gl_req_gh = NULL;
1051 gl->gl_req_bh = NULL;
1052 clear_bit(GLF_LOCK, &gl->gl_flags);
1054 spin_unlock(&gl->gl_spin);
1059 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
1060 gfs2_holder_put(gh);
1062 complete(&gh->gh_wait);
1067 * gfs2_glock_drop_th - call into the lock module to unlock a lock
1072 void gfs2_glock_drop_th(struct gfs2_glock *gl)
1074 struct gfs2_sbd *sdp = gl->gl_sbd;
1075 struct gfs2_glock_operations *glops = gl->gl_ops;
1078 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1079 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
1080 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
1082 if (gl->gl_state == LM_ST_EXCLUSIVE) {
1085 DIO_METADATA | DIO_DATA | DIO_RELEASE);
1088 gfs2_glock_hold(gl);
1089 gl->gl_req_bh = drop_bh;
1091 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
1093 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
1099 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
1103 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1104 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1106 * Don't cancel GL_NOCANCEL requests.
1109 static void do_cancels(struct gfs2_holder *gh)
1111 struct gfs2_glock *gl = gh->gh_gl;
1113 spin_lock(&gl->gl_spin);
1115 while (gl->gl_req_gh != gh &&
1116 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1117 !list_empty(&gh->gh_list)) {
1118 if (gl->gl_req_bh &&
1120 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
1121 spin_unlock(&gl->gl_spin);
1122 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
1124 spin_lock(&gl->gl_spin);
1126 spin_unlock(&gl->gl_spin);
1128 spin_lock(&gl->gl_spin);
1132 spin_unlock(&gl->gl_spin);
1136 * glock_wait_internal - wait on a glock acquisition
1137 * @gh: the glock holder
1139 * Returns: 0 on success
1142 static int glock_wait_internal(struct gfs2_holder *gh)
1144 struct gfs2_glock *gl = gh->gh_gl;
1145 struct gfs2_sbd *sdp = gl->gl_sbd;
1146 struct gfs2_glock_operations *glops = gl->gl_ops;
1148 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1151 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1152 spin_lock(&gl->gl_spin);
1153 if (gl->gl_req_gh != gh &&
1154 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1155 !list_empty(&gh->gh_list)) {
1156 list_del_init(&gh->gh_list);
1157 gh->gh_error = GLR_TRYFAILED;
1158 if (test_bit(HIF_RECURSE, &gh->gh_iflags))
1161 spin_unlock(&gl->gl_spin);
1162 return gh->gh_error;
1164 spin_unlock(&gl->gl_spin);
1167 if (gh->gh_flags & LM_FLAG_PRIORITY)
1170 wait_for_completion(&gh->gh_wait);
1173 return gh->gh_error;
1175 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
1176 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state,
1180 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1181 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1183 if (glops->go_lock) {
1184 gh->gh_error = glops->go_lock(gh);
1186 spin_lock(&gl->gl_spin);
1187 list_del_init(&gh->gh_list);
1188 if (test_and_clear_bit(HIF_RECURSE,
1191 spin_unlock(&gl->gl_spin);
1195 spin_lock(&gl->gl_spin);
1196 gl->gl_req_gh = NULL;
1197 gl->gl_req_bh = NULL;
1198 clear_bit(GLF_LOCK, &gl->gl_flags);
1199 if (test_bit(HIF_RECURSE, &gh->gh_iflags))
1202 spin_unlock(&gl->gl_spin);
1205 return gh->gh_error;
1208 static inline struct gfs2_holder *
1209 find_holder_by_owner(struct list_head *head, struct task_struct *owner)
1211 struct gfs2_holder *gh;
1213 list_for_each_entry(gh, head, gh_list) {
1214 if (gh->gh_owner == owner)
1224 * Make sure the new holder is compatible with the pre-existing one.
1228 static int recurse_check(struct gfs2_holder *existing, struct gfs2_holder *new,
1231 struct gfs2_sbd *sdp = existing->gh_gl->gl_sbd;
1233 if (gfs2_assert_warn(sdp, (new->gh_flags & LM_FLAG_ANY) ||
1234 !(existing->gh_flags & LM_FLAG_ANY)))
1237 if (gfs2_assert_warn(sdp, (existing->gh_flags & GL_LOCAL_EXCL) ||
1238 !(new->gh_flags & GL_LOCAL_EXCL)))
1241 if (gfs2_assert_warn(sdp, relaxed_state_ok(state, new->gh_state,
1248 print_symbol(KERN_WARNING "GFS2: Existing holder from %s\n",
1250 print_symbol(KERN_WARNING "GFS2: New holder from %s\n", new->gh_ip);
1251 set_bit(HIF_ABORTED, &new->gh_iflags);
1256 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1257 * @gh: the holder structure to add
1261 static void add_to_queue(struct gfs2_holder *gh)
1263 struct gfs2_glock *gl = gh->gh_gl;
1264 struct gfs2_holder *existing;
1269 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
1271 if (recurse_check(existing, gh, gl->gl_state))
1274 list_add_tail(&gh->gh_list, &gl->gl_holders);
1275 set_bit(HIF_HOLDER, &gh->gh_iflags);
1278 complete(&gh->gh_wait);
1283 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
1285 if (recurse_check(existing, gh, existing->gh_state))
1288 set_bit(HIF_RECURSE, &gh->gh_iflags);
1289 set_bit(HIF_RECURSE, &existing->gh_iflags);
1291 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1297 if (gh->gh_flags & LM_FLAG_PRIORITY)
1298 list_add(&gh->gh_list, &gl->gl_waiters3);
1300 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1304 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1305 * @gh: the holder structure
1307 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1309 * Returns: 0, GLR_TRYFAILED, or errno on failure
1312 int gfs2_glock_nq(struct gfs2_holder *gh)
1314 struct gfs2_glock *gl = gh->gh_gl;
1315 struct gfs2_sbd *sdp = gl->gl_sbd;
1319 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1320 set_bit(HIF_ABORTED, &gh->gh_iflags);
1324 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1326 spin_lock(&gl->gl_spin);
1329 spin_unlock(&gl->gl_spin);
1331 if (!(gh->gh_flags & GL_ASYNC)) {
1332 error = glock_wait_internal(gh);
1333 if (error == GLR_CANCELED) {
1339 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1345 * gfs2_glock_poll - poll to see if an async request has been completed
1348 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1351 int gfs2_glock_poll(struct gfs2_holder *gh)
1353 struct gfs2_glock *gl = gh->gh_gl;
1356 spin_lock(&gl->gl_spin);
1358 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1360 else if (list_empty(&gh->gh_list)) {
1361 if (gh->gh_error == GLR_CANCELED) {
1362 spin_unlock(&gl->gl_spin);
1364 if (gfs2_glock_nq(gh))
1371 spin_unlock(&gl->gl_spin);
1377 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1378 * @gh: the holder structure
1380 * Returns: 0, GLR_TRYFAILED, or errno on failure
1383 int gfs2_glock_wait(struct gfs2_holder *gh)
1387 error = glock_wait_internal(gh);
1388 if (error == GLR_CANCELED) {
1390 gh->gh_flags &= ~GL_ASYNC;
1391 error = gfs2_glock_nq(gh);
1398 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1399 * @gh: the glock holder
1403 void gfs2_glock_dq(struct gfs2_holder *gh)
1405 struct gfs2_glock *gl = gh->gh_gl;
1406 struct gfs2_glock_operations *glops = gl->gl_ops;
1408 if (gh->gh_flags & GL_SYNC)
1409 set_bit(GLF_SYNC, &gl->gl_flags);
1411 if (gh->gh_flags & GL_NOCACHE)
1412 handle_callback(gl, LM_ST_UNLOCKED);
1414 gfs2_glmutex_lock(gl);
1416 spin_lock(&gl->gl_spin);
1417 list_del_init(&gh->gh_list);
1419 if (list_empty(&gl->gl_holders)) {
1420 spin_unlock(&gl->gl_spin);
1422 if (glops->go_unlock)
1423 glops->go_unlock(gh);
1425 if (test_bit(GLF_SYNC, &gl->gl_flags)) {
1427 glops->go_sync(gl, DIO_METADATA | DIO_DATA);
1430 gl->gl_stamp = jiffies;
1432 spin_lock(&gl->gl_spin);
1435 clear_bit(GLF_LOCK, &gl->gl_flags);
1437 spin_unlock(&gl->gl_spin);
1441 * gfs2_glock_prefetch - Try to prefetch a glock
1443 * @state: the state to prefetch in
1444 * @flags: flags passed to go_xmote_th()
1448 void gfs2_glock_prefetch(struct gfs2_glock *gl, unsigned int state, int flags)
1450 struct gfs2_glock_operations *glops = gl->gl_ops;
1452 spin_lock(&gl->gl_spin);
1454 if (test_bit(GLF_LOCK, &gl->gl_flags) ||
1455 !list_empty(&gl->gl_holders) ||
1456 !list_empty(&gl->gl_waiters1) ||
1457 !list_empty(&gl->gl_waiters2) ||
1458 !list_empty(&gl->gl_waiters3) ||
1459 relaxed_state_ok(gl->gl_state, state, flags)) {
1460 spin_unlock(&gl->gl_spin);
1464 set_bit(GLF_PREFETCH, &gl->gl_flags);
1465 set_bit(GLF_LOCK, &gl->gl_flags);
1466 spin_unlock(&gl->gl_spin);
1468 glops->go_xmote_th(gl, state, flags);
1472 * gfs2_glock_force_drop - Force a glock to be uncached
1477 void gfs2_glock_force_drop(struct gfs2_glock *gl)
1479 struct gfs2_holder gh;
1481 gfs2_holder_init(gl, LM_ST_UNLOCKED, GL_NEVER_RECURSE, &gh);
1482 set_bit(HIF_DEMOTE, &gh.gh_iflags);
1484 spin_lock(&gl->gl_spin);
1485 list_add_tail(&gh.gh_list, &gl->gl_waiters2);
1487 spin_unlock(&gl->gl_spin);
1489 wait_for_completion(&gh.gh_wait);
1490 gfs2_holder_uninit(&gh);
1493 static void greedy_work(void *data)
1495 struct greedy *gr = (struct greedy *)data;
1496 struct gfs2_holder *gh = &gr->gr_gh;
1497 struct gfs2_glock *gl = gh->gh_gl;
1498 struct gfs2_glock_operations *glops = gl->gl_ops;
1500 clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1502 if (glops->go_greedy)
1503 glops->go_greedy(gl);
1505 spin_lock(&gl->gl_spin);
1507 if (list_empty(&gl->gl_waiters2)) {
1508 clear_bit(GLF_GREEDY, &gl->gl_flags);
1509 spin_unlock(&gl->gl_spin);
1510 gfs2_holder_uninit(gh);
1513 gfs2_glock_hold(gl);
1514 list_add_tail(&gh->gh_list, &gl->gl_waiters2);
1516 spin_unlock(&gl->gl_spin);
1522 * gfs2_glock_be_greedy -
1526 * Returns: 0 if go_greedy will be called, 1 otherwise
1529 int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time)
1532 struct gfs2_holder *gh;
1535 gl->gl_sbd->sd_args.ar_localcaching ||
1536 test_and_set_bit(GLF_GREEDY, &gl->gl_flags))
1539 gr = kmalloc(sizeof(struct greedy), GFP_KERNEL);
1541 clear_bit(GLF_GREEDY, &gl->gl_flags);
1546 gfs2_holder_init(gl, 0, GL_NEVER_RECURSE, gh);
1547 set_bit(HIF_GREEDY, &gh->gh_iflags);
1548 INIT_WORK(&gr->gr_work, greedy_work, gr);
1550 set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1551 schedule_delayed_work(&gr->gr_work, time);
1557 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1558 * @gh: the holder structure
1562 void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1565 gfs2_holder_uninit(gh);
1569 * gfs2_glock_nq_num - acquire a glock based on lock number
1570 * @sdp: the filesystem
1571 * @number: the lock number
1572 * @glops: the glock operations for the type of glock
1573 * @state: the state to acquire the glock in
1574 * @flags: modifier flags for the aquisition
1575 * @gh: the struct gfs2_holder
1580 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, uint64_t number,
1581 struct gfs2_glock_operations *glops, unsigned int state,
1582 int flags, struct gfs2_holder *gh)
1584 struct gfs2_glock *gl;
1587 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1589 error = gfs2_glock_nq_init(gl, state, flags, gh);
1597 * glock_compare - Compare two struct gfs2_glock structures for sorting
1598 * @arg_a: the first structure
1599 * @arg_b: the second structure
1603 static int glock_compare(const void *arg_a, const void *arg_b)
1605 struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
1606 struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
1607 struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1608 struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1611 if (a->ln_number > b->ln_number)
1613 else if (a->ln_number < b->ln_number)
1616 if (gh_a->gh_state == LM_ST_SHARED &&
1617 gh_b->gh_state == LM_ST_EXCLUSIVE)
1619 else if (!(gh_a->gh_flags & GL_LOCAL_EXCL) &&
1620 (gh_b->gh_flags & GL_LOCAL_EXCL))
1628 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1629 * @num_gh: the number of structures
1630 * @ghs: an array of struct gfs2_holder structures
1632 * Returns: 0 on success (all glocks acquired),
1633 * errno on failure (no glocks acquired)
1636 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1637 struct gfs2_holder **p)
1642 for (x = 0; x < num_gh; x++)
1645 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1647 for (x = 0; x < num_gh; x++) {
1648 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1650 error = gfs2_glock_nq(p[x]);
1653 gfs2_glock_dq(p[x]);
1662 * gfs2_glock_nq_m - acquire multiple glocks
1663 * @num_gh: the number of structures
1664 * @ghs: an array of struct gfs2_holder structures
1666 * Figure out how big an impact this function has. Either:
1667 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1668 * 2) Forget async stuff and just call nq_m_sync()
1669 * 3) Leave it like it is
1671 * Returns: 0 on success (all glocks acquired),
1672 * errno on failure (no glocks acquired)
1675 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1679 int borked = 0, serious = 0;
1686 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1687 return gfs2_glock_nq(ghs);
1690 e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1694 for (x = 0; x < num_gh; x++) {
1695 ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
1696 error = gfs2_glock_nq(&ghs[x]);
1705 for (x = 0; x < num_gh; x++) {
1706 error = e[x] = glock_wait_internal(&ghs[x]);
1709 if (error != GLR_TRYFAILED && error != GLR_CANCELED)
1719 for (x = 0; x < num_gh; x++)
1721 gfs2_glock_dq(&ghs[x]);
1726 for (x = 0; x < num_gh; x++)
1727 gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
1729 error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
1738 * gfs2_glock_dq_m - release multiple glocks
1739 * @num_gh: the number of structures
1740 * @ghs: an array of struct gfs2_holder structures
1744 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1748 for (x = 0; x < num_gh; x++)
1749 gfs2_glock_dq(&ghs[x]);
1753 * gfs2_glock_dq_uninit_m - release multiple glocks
1754 * @num_gh: the number of structures
1755 * @ghs: an array of struct gfs2_holder structures
1759 void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1763 for (x = 0; x < num_gh; x++)
1764 gfs2_glock_dq_uninit(&ghs[x]);
1768 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1769 * @sdp: the filesystem
1770 * @number: the lock number
1771 * @glops: the glock operations for the type of glock
1772 * @state: the state to acquire the glock in
1773 * @flags: modifier flags for the aquisition
1778 void gfs2_glock_prefetch_num(struct gfs2_sbd *sdp, uint64_t number,
1779 struct gfs2_glock_operations *glops,
1780 unsigned int state, int flags)
1782 struct gfs2_glock *gl;
1785 if (atomic_read(&sdp->sd_reclaim_count) <
1786 gfs2_tune_get(sdp, gt_reclaim_limit)) {
1787 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1789 gfs2_glock_prefetch(gl, state, flags);
1796 * gfs2_lvb_hold - attach a LVB from a glock
1797 * @gl: The glock in question
1801 int gfs2_lvb_hold(struct gfs2_glock *gl)
1805 gfs2_glmutex_lock(gl);
1807 if (!atomic_read(&gl->gl_lvb_count)) {
1808 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1810 gfs2_glmutex_unlock(gl);
1813 gfs2_glock_hold(gl);
1815 atomic_inc(&gl->gl_lvb_count);
1817 gfs2_glmutex_unlock(gl);
1823 * gfs2_lvb_unhold - detach a LVB from a glock
1824 * @gl: The glock in question
1828 void gfs2_lvb_unhold(struct gfs2_glock *gl)
1830 gfs2_glock_hold(gl);
1831 gfs2_glmutex_lock(gl);
1833 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1834 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1835 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1840 gfs2_glmutex_unlock(gl);
1844 void gfs2_lvb_sync(struct gfs2_glock *gl)
1846 gfs2_glmutex_lock(gl);
1848 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count));
1849 if (!gfs2_assert_warn(gl->gl_sbd, gfs2_glock_is_held_excl(gl)))
1850 gfs2_lm_sync_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1852 gfs2_glmutex_unlock(gl);
1855 static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1858 struct gfs2_glock *gl;
1860 gl = gfs2_glock_find(sdp, name);
1864 if (gl->gl_ops->go_callback)
1865 gl->gl_ops->go_callback(gl, state);
1866 handle_callback(gl, state);
1868 spin_lock(&gl->gl_spin);
1870 spin_unlock(&gl->gl_spin);
1876 * gfs2_glock_cb - Callback used by locking module
1877 * @fsdata: Pointer to the superblock
1878 * @type: Type of callback
1879 * @data: Type dependent data pointer
1881 * Called by the locking module when it wants to tell us something.
1882 * Either we need to drop a lock, one of our ASYNC requests completed, or
1883 * a journal from another client needs to be recovered.
1886 void gfs2_glock_cb(lm_fsdata_t *fsdata, unsigned int type, void *data)
1888 struct gfs2_sbd *sdp = (struct gfs2_sbd *)fsdata;
1892 blocking_cb(sdp, (struct lm_lockname *)data, LM_ST_UNLOCKED);
1896 blocking_cb(sdp, (struct lm_lockname *)data, LM_ST_DEFERRED);
1900 blocking_cb(sdp, (struct lm_lockname *)data, LM_ST_SHARED);
1904 struct lm_async_cb *async = (struct lm_async_cb *)data;
1905 struct gfs2_glock *gl;
1907 gl = gfs2_glock_find(sdp, &async->lc_name);
1908 if (gfs2_assert_warn(sdp, gl))
1910 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1911 gl->gl_req_bh(gl, async->lc_ret);
1917 case LM_CB_NEED_RECOVERY:
1918 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1919 if (sdp->sd_recoverd_process)
1920 wake_up_process(sdp->sd_recoverd_process);
1923 case LM_CB_DROPLOCKS:
1924 gfs2_gl_hash_clear(sdp, NO_WAIT);
1925 gfs2_quota_scan(sdp);
1929 gfs2_assert_warn(sdp, 0);
1935 * gfs2_try_toss_inode - try to remove a particular inode struct from cache
1936 * sdp: the filesystem
1937 * inum: the inode number
1941 void gfs2_try_toss_inode(struct gfs2_sbd *sdp, struct gfs2_inum *inum)
1943 struct gfs2_glock *gl;
1944 struct gfs2_inode *ip;
1947 error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_inode_glops,
1952 if (!gfs2_glmutex_trylock(gl))
1959 if (atomic_read(&ip->i_count))
1962 gfs2_inode_destroy(ip);
1965 gfs2_glmutex_unlock(gl);
1972 * gfs2_iopen_go_callback - Try to kick the inode/vnode associated with an
1973 * iopen glock from memory
1974 * @io_gl: the iopen glock
1975 * @state: the state into which the glock should be put
1979 void gfs2_iopen_go_callback(struct gfs2_glock *io_gl, unsigned int state)
1981 struct gfs2_glock *i_gl;
1983 if (state != LM_ST_UNLOCKED)
1986 spin_lock(&io_gl->gl_spin);
1987 i_gl = io_gl->gl_object;
1989 gfs2_glock_hold(i_gl);
1990 spin_unlock(&io_gl->gl_spin);
1992 spin_unlock(&io_gl->gl_spin);
1996 if (gfs2_glmutex_trylock(i_gl)) {
1997 struct gfs2_inode *ip = i_gl->gl_object;
1999 gfs2_try_toss_vnode(ip);
2000 gfs2_glmutex_unlock(i_gl);
2001 gfs2_glock_schedule_for_reclaim(i_gl);
2004 gfs2_glmutex_unlock(i_gl);
2008 gfs2_glock_put(i_gl);
2012 * demote_ok - Check to see if it's ok to unlock a glock
2015 * Returns: 1 if it's ok
2018 static int demote_ok(struct gfs2_glock *gl)
2020 struct gfs2_sbd *sdp = gl->gl_sbd;
2021 struct gfs2_glock_operations *glops = gl->gl_ops;
2024 if (test_bit(GLF_STICKY, &gl->gl_flags))
2026 else if (test_bit(GLF_PREFETCH, &gl->gl_flags))
2027 demote = time_after_eq(jiffies,
2029 gfs2_tune_get(sdp, gt_prefetch_secs) * HZ);
2030 else if (glops->go_demote_ok)
2031 demote = glops->go_demote_ok(gl);
2037 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
2042 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
2044 struct gfs2_sbd *sdp = gl->gl_sbd;
2046 spin_lock(&sdp->sd_reclaim_lock);
2047 if (list_empty(&gl->gl_reclaim)) {
2048 gfs2_glock_hold(gl);
2049 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
2050 atomic_inc(&sdp->sd_reclaim_count);
2052 spin_unlock(&sdp->sd_reclaim_lock);
2054 wake_up(&sdp->sd_reclaim_wq);
2058 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
2059 * @sdp: the filesystem
2061 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
2062 * different glock and we notice that there are a lot of glocks in the
2067 void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
2069 struct gfs2_glock *gl;
2071 spin_lock(&sdp->sd_reclaim_lock);
2072 if (list_empty(&sdp->sd_reclaim_list)) {
2073 spin_unlock(&sdp->sd_reclaim_lock);
2076 gl = list_entry(sdp->sd_reclaim_list.next,
2077 struct gfs2_glock, gl_reclaim);
2078 list_del_init(&gl->gl_reclaim);
2079 spin_unlock(&sdp->sd_reclaim_lock);
2081 atomic_dec(&sdp->sd_reclaim_count);
2082 atomic_inc(&sdp->sd_reclaimed);
2084 if (gfs2_glmutex_trylock(gl)) {
2085 if (gl->gl_ops == &gfs2_inode_glops) {
2086 struct gfs2_inode *ip = gl->gl_object;
2087 if (ip && !atomic_read(&ip->i_count))
2088 gfs2_inode_destroy(ip);
2090 if (queue_empty(gl, &gl->gl_holders) &&
2091 gl->gl_state != LM_ST_UNLOCKED &&
2093 handle_callback(gl, LM_ST_UNLOCKED);
2094 gfs2_glmutex_unlock(gl);
2101 * examine_bucket - Call a function for glock in a hash bucket
2102 * @examiner: the function
2103 * @sdp: the filesystem
2104 * @bucket: the bucket
2106 * Returns: 1 if the bucket has entries
2109 static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
2110 struct gfs2_gl_hash_bucket *bucket)
2112 struct glock_plug plug;
2113 struct list_head *tmp;
2114 struct gfs2_glock *gl;
2117 /* Add "plug" to end of bucket list, work back up list from there */
2118 memset(&plug.gl_flags, 0, sizeof(unsigned long));
2119 set_bit(GLF_PLUG, &plug.gl_flags);
2121 write_lock(&bucket->hb_lock);
2122 list_add(&plug.gl_list, &bucket->hb_list);
2123 write_unlock(&bucket->hb_lock);
2126 write_lock(&bucket->hb_lock);
2129 tmp = plug.gl_list.next;
2131 if (tmp == &bucket->hb_list) {
2132 list_del(&plug.gl_list);
2133 entries = !list_empty(&bucket->hb_list);
2134 write_unlock(&bucket->hb_lock);
2137 gl = list_entry(tmp, struct gfs2_glock, gl_list);
2139 /* Move plug up list */
2140 list_move(&plug.gl_list, &gl->gl_list);
2142 if (test_bit(GLF_PLUG, &gl->gl_flags))
2145 /* examiner() must glock_put() */
2146 gfs2_glock_hold(gl);
2151 write_unlock(&bucket->hb_lock);
2158 * scan_glock - look at a glock and see if we can reclaim it
2159 * @gl: the glock to look at
2163 static void scan_glock(struct gfs2_glock *gl)
2165 if (gfs2_glmutex_trylock(gl)) {
2166 if (gl->gl_ops == &gfs2_inode_glops) {
2167 struct gfs2_inode *ip = gl->gl_object;
2168 if (ip && !atomic_read(&ip->i_count))
2171 if (queue_empty(gl, &gl->gl_holders) &&
2172 gl->gl_state != LM_ST_UNLOCKED &&
2176 gfs2_glmutex_unlock(gl);
2184 gfs2_glmutex_unlock(gl);
2185 gfs2_glock_schedule_for_reclaim(gl);
2190 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
2191 * @sdp: the filesystem
2195 void gfs2_scand_internal(struct gfs2_sbd *sdp)
2199 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2200 examine_bucket(scan_glock, sdp, &sdp->sd_gl_hash[x]);
2206 * clear_glock - look at a glock and see if we can free it from glock cache
2207 * @gl: the glock to look at
2211 static void clear_glock(struct gfs2_glock *gl)
2213 struct gfs2_sbd *sdp = gl->gl_sbd;
2216 spin_lock(&sdp->sd_reclaim_lock);
2217 if (!list_empty(&gl->gl_reclaim)) {
2218 list_del_init(&gl->gl_reclaim);
2219 atomic_dec(&sdp->sd_reclaim_count);
2220 released = gfs2_glock_put(gl);
2221 gfs2_assert(sdp, !released);
2223 spin_unlock(&sdp->sd_reclaim_lock);
2225 if (gfs2_glmutex_trylock(gl)) {
2226 if (gl->gl_ops == &gfs2_inode_glops) {
2227 struct gfs2_inode *ip = gl->gl_object;
2228 if (ip && !atomic_read(&ip->i_count))
2229 gfs2_inode_destroy(ip);
2231 if (queue_empty(gl, &gl->gl_holders) &&
2232 gl->gl_state != LM_ST_UNLOCKED)
2233 handle_callback(gl, LM_ST_UNLOCKED);
2235 gfs2_glmutex_unlock(gl);
2242 * gfs2_gl_hash_clear - Empty out the glock hash table
2243 * @sdp: the filesystem
2244 * @wait: wait until it's all gone
2246 * Called when unmounting the filesystem, or when inter-node lock manager
2247 * requests DROPLOCKS because it is running out of capacity.
2250 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
2261 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
2262 if (examine_bucket(clear_glock, sdp,
2263 &sdp->sd_gl_hash[x]))
2269 if (time_after_eq(jiffies,
2270 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
2271 fs_warn(sdp, "Unmount seems to be stalled. "
2272 "Dumping lock state...\n");
2273 gfs2_dump_lockstate(sdp);
2277 /* invalidate_inodes() requires that the sb inodes list
2278 not change, but an async completion callback for an
2279 unlock can occur which does glock_put() which
2280 can call iput() which will change the sb inodes list.
2281 invalidate_inodes_mutex prevents glock_put()'s during
2282 an invalidate_inodes() */
2284 mutex_lock(&sdp->sd_invalidate_inodes_mutex);
2285 invalidate_inodes(sdp->sd_vfs);
2286 mutex_unlock(&sdp->sd_invalidate_inodes_mutex);
2292 * Diagnostic routines to help debug distributed deadlock
2296 * dump_holder - print information about a glock holder
2297 * @str: a string naming the type of holder
2298 * @gh: the glock holder
2300 * Returns: 0 on success, -ENOBUFS when we run out of space
2303 static int dump_holder(char *str, struct gfs2_holder *gh)
2306 int error = -ENOBUFS;
2308 printk(KERN_INFO " %s\n", str);
2309 printk(KERN_INFO " owner = %ld\n",
2310 (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
2311 printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
2312 printk(KERN_INFO " gh_flags =");
2313 for (x = 0; x < 32; x++)
2314 if (gh->gh_flags & (1 << x))
2317 printk(KERN_INFO " error = %d\n", gh->gh_error);
2318 printk(KERN_INFO " gh_iflags =");
2319 for (x = 0; x < 32; x++)
2320 if (test_bit(x, &gh->gh_iflags))
2323 print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip);
2331 * dump_inode - print information about an inode
2334 * Returns: 0 on success, -ENOBUFS when we run out of space
2337 static int dump_inode(struct gfs2_inode *ip)
2340 int error = -ENOBUFS;
2342 printk(KERN_INFO " Inode:\n");
2343 printk(KERN_INFO " num = %llu %llu\n",
2344 ip->i_num.no_formal_ino, ip->i_num.no_addr);
2345 printk(KERN_INFO " type = %u\n", IF2DT(ip->i_di.di_mode));
2346 printk(KERN_INFO " i_count = %d\n", atomic_read(&ip->i_count));
2347 printk(KERN_INFO " i_flags =");
2348 for (x = 0; x < 32; x++)
2349 if (test_bit(x, &ip->i_flags))
2352 printk(KERN_INFO " vnode = %s\n", (ip->i_vnode) ? "yes" : "no");
2360 * dump_glock - print information about a glock
2362 * @count: where we are in the buffer
2364 * Returns: 0 on success, -ENOBUFS when we run out of space
2367 static int dump_glock(struct gfs2_glock *gl)
2369 struct gfs2_holder *gh;
2371 int error = -ENOBUFS;
2373 spin_lock(&gl->gl_spin);
2375 printk(KERN_INFO "Glock (%u, %llu)\n",
2376 gl->gl_name.ln_type,
2377 gl->gl_name.ln_number);
2378 printk(KERN_INFO " gl_flags =");
2379 for (x = 0; x < 32; x++)
2380 if (test_bit(x, &gl->gl_flags))
2383 printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref.refcount));
2384 printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
2385 printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
2386 printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
2387 printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
2388 printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
2389 printk(KERN_INFO " le = %s\n",
2390 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
2391 printk(KERN_INFO " reclaim = %s\n",
2392 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
2394 printk(KERN_INFO " aspace = %lu\n",
2395 gl->gl_aspace->i_mapping->nrpages);
2397 printk(KERN_INFO " aspace = no\n");
2398 printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
2399 if (gl->gl_req_gh) {
2400 error = dump_holder("Request", gl->gl_req_gh);
2404 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
2405 error = dump_holder("Holder", gh);
2409 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
2410 error = dump_holder("Waiter1", gh);
2414 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
2415 error = dump_holder("Waiter2", gh);
2419 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
2420 error = dump_holder("Waiter3", gh);
2424 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
2425 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
2426 list_empty(&gl->gl_holders)) {
2427 error = dump_inode(gl->gl_object);
2432 printk(KERN_INFO " Inode: busy\n");
2439 spin_unlock(&gl->gl_spin);
2445 * gfs2_dump_lockstate - print out the current lockstate
2446 * @sdp: the filesystem
2447 * @ub: the buffer to copy the information into
2449 * If @ub is NULL, dump the lockstate to the console.
2453 int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
2455 struct gfs2_gl_hash_bucket *bucket;
2456 struct gfs2_glock *gl;
2460 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2461 bucket = &sdp->sd_gl_hash[x];
2463 read_lock(&bucket->hb_lock);
2465 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
2466 if (test_bit(GLF_PLUG, &gl->gl_flags))
2469 error = dump_glock(gl);
2474 read_unlock(&bucket->hb_lock);