2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License v.2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/delay.h>
16 #include <linux/sort.h>
17 #include <linux/jhash.h>
18 #include <linux/kref.h>
19 #include <linux/kallsyms.h>
20 #include <linux/gfs2_ondisk.h>
21 #include <asm/uaccess.h>
24 #include "lm_interface.h"
36 /* Must be kept in sync with the beginning of struct gfs2_glock */
38 struct list_head gl_list;
39 unsigned long gl_flags;
43 struct gfs2_holder gr_gh;
44 struct work_struct gr_work;
47 typedef void (*glock_examiner) (struct gfs2_glock * gl);
49 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
50 static int dump_glock(struct gfs2_glock *gl);
53 * relaxed_state_ok - is a requested lock compatible with the current lock mode?
54 * @actual: the current state of the lock
55 * @requested: the lock state that was requested by the caller
56 * @flags: the modifier flags passed in by the caller
58 * Returns: 1 if the locks are compatible, 0 otherwise
61 static inline int relaxed_state_ok(unsigned int actual, unsigned requested,
64 if (actual == requested)
70 if (actual == LM_ST_EXCLUSIVE && requested == LM_ST_SHARED)
73 if (actual != LM_ST_UNLOCKED && (flags & LM_FLAG_ANY))
80 * gl_hash() - Turn glock number into hash bucket number
81 * @lock: The glock number
83 * Returns: The number of the corresponding hash bucket
86 static unsigned int gl_hash(struct lm_lockname *name)
90 h = jhash(&name->ln_number, sizeof(uint64_t), 0);
91 h = jhash(&name->ln_type, sizeof(unsigned int), h);
92 h &= GFS2_GL_HASH_MASK;
98 * glock_free() - Perform a few checks and then release struct gfs2_glock
99 * @gl: The glock to release
101 * Also calls lock module to release its internal structure for this glock.
105 static void glock_free(struct gfs2_glock *gl)
107 struct gfs2_sbd *sdp = gl->gl_sbd;
108 struct inode *aspace = gl->gl_aspace;
110 gfs2_lm_put_lock(sdp, gl->gl_lock);
113 gfs2_aspace_put(aspace);
115 kmem_cache_free(gfs2_glock_cachep, gl);
119 * gfs2_glock_hold() - increment reference count on glock
120 * @gl: The glock to hold
124 void gfs2_glock_hold(struct gfs2_glock *gl)
126 kref_get(&gl->gl_ref);
129 /* All work is done after the return from kref_put() so we
130 can release the write_lock before the free. */
132 static void kill_glock(struct kref *kref)
134 struct gfs2_glock *gl = container_of(kref, struct gfs2_glock, gl_ref);
135 struct gfs2_sbd *sdp = gl->gl_sbd;
137 gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED);
138 gfs2_assert(sdp, list_empty(&gl->gl_reclaim));
139 gfs2_assert(sdp, list_empty(&gl->gl_holders));
140 gfs2_assert(sdp, list_empty(&gl->gl_waiters1));
141 gfs2_assert(sdp, list_empty(&gl->gl_waiters2));
142 gfs2_assert(sdp, list_empty(&gl->gl_waiters3));
146 * gfs2_glock_put() - Decrement reference count on glock
147 * @gl: The glock to put
151 int gfs2_glock_put(struct gfs2_glock *gl)
153 struct gfs2_gl_hash_bucket *bucket = gl->gl_bucket;
156 write_lock(&bucket->hb_lock);
157 if (kref_put(&gl->gl_ref, kill_glock)) {
158 list_del_init(&gl->gl_list);
159 write_unlock(&bucket->hb_lock);
160 BUG_ON(spin_is_locked(&gl->gl_spin));
165 write_unlock(&bucket->hb_lock);
171 * queue_empty - check to see if a glock's queue is empty
173 * @head: the head of the queue to check
175 * This function protects the list in the event that a process already
176 * has a holder on the list and is adding a second holder for itself.
177 * The glmutex lock is what generally prevents processes from working
178 * on the same glock at once, but the special case of adding a second
179 * holder for yourself ("recursive" locking) doesn't involve locking
180 * glmutex, making the spin lock necessary.
182 * Returns: 1 if the queue is empty
185 static inline int queue_empty(struct gfs2_glock *gl, struct list_head *head)
188 spin_lock(&gl->gl_spin);
189 empty = list_empty(head);
190 spin_unlock(&gl->gl_spin);
195 * search_bucket() - Find struct gfs2_glock by lock number
196 * @bucket: the bucket to search
197 * @name: The lock name
199 * Returns: NULL, or the struct gfs2_glock with the requested number
202 static struct gfs2_glock *search_bucket(struct gfs2_gl_hash_bucket *bucket,
203 struct lm_lockname *name)
205 struct gfs2_glock *gl;
207 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
208 if (test_bit(GLF_PLUG, &gl->gl_flags))
210 if (!lm_name_equal(&gl->gl_name, name))
213 kref_get(&gl->gl_ref);
222 * gfs2_glock_find() - Find glock by lock number
223 * @sdp: The GFS2 superblock
224 * @name: The lock name
226 * Returns: NULL, or the struct gfs2_glock with the requested number
229 static struct gfs2_glock *gfs2_glock_find(struct gfs2_sbd *sdp,
230 struct lm_lockname *name)
232 struct gfs2_gl_hash_bucket *bucket = &sdp->sd_gl_hash[gl_hash(name)];
233 struct gfs2_glock *gl;
235 read_lock(&bucket->hb_lock);
236 gl = search_bucket(bucket, name);
237 read_unlock(&bucket->hb_lock);
243 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
244 * @sdp: The GFS2 superblock
245 * @number: the lock number
246 * @glops: The glock_operations to use
247 * @create: If 0, don't create the glock if it doesn't exist
248 * @glp: the glock is returned here
250 * This does not lock a glock, just finds/creates structures for one.
255 int gfs2_glock_get(struct gfs2_sbd *sdp, uint64_t number,
256 struct gfs2_glock_operations *glops, int create,
257 struct gfs2_glock **glp)
259 struct lm_lockname name;
260 struct gfs2_glock *gl, *tmp;
261 struct gfs2_gl_hash_bucket *bucket;
264 name.ln_number = number;
265 name.ln_type = glops->go_type;
266 bucket = &sdp->sd_gl_hash[gl_hash(&name)];
268 read_lock(&bucket->hb_lock);
269 gl = search_bucket(bucket, &name);
270 read_unlock(&bucket->hb_lock);
277 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
281 memset(gl, 0, sizeof(struct gfs2_glock));
283 INIT_LIST_HEAD(&gl->gl_list);
285 kref_init(&gl->gl_ref);
287 spin_lock_init(&gl->gl_spin);
289 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 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, unsigned 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 = 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, unsigned 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
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 static struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl,
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 static 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 static 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 gl->gl_owner = current;
664 gl->gl_ip = (unsigned long)__builtin_return_address(0);
665 complete(&gh.gh_wait);
667 spin_unlock(&gl->gl_spin);
669 wait_for_completion(&gh.gh_wait);
670 gfs2_holder_uninit(&gh);
674 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
677 * Returns: 1 if the glock is acquired
680 static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
684 spin_lock(&gl->gl_spin);
685 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
688 gl->gl_owner = current;
689 gl->gl_ip = (unsigned long)__builtin_return_address(0);
691 spin_unlock(&gl->gl_spin);
697 * gfs2_glmutex_unlock - release a local lock on a glock
702 static void gfs2_glmutex_unlock(struct gfs2_glock *gl)
704 spin_lock(&gl->gl_spin);
705 clear_bit(GLF_LOCK, &gl->gl_flags);
709 BUG_ON(!spin_is_locked(&gl->gl_spin));
710 spin_unlock(&gl->gl_spin);
714 * handle_callback - add a demote request to a lock's queue
716 * @state: the state the caller wants us to change to
718 * Note: This may fail sliently if we are out of memory.
721 static void handle_callback(struct gfs2_glock *gl, unsigned int state)
723 struct gfs2_holder *gh, *new_gh = NULL;
726 spin_lock(&gl->gl_spin);
728 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
729 if (test_bit(HIF_DEMOTE, &gh->gh_iflags) &&
730 gl->gl_req_gh != gh) {
731 if (gh->gh_state != state)
732 gh->gh_state = LM_ST_UNLOCKED;
738 list_add_tail(&new_gh->gh_list, &gl->gl_waiters2);
741 spin_unlock(&gl->gl_spin);
743 new_gh = gfs2_holder_get(gl, state, LM_FLAG_TRY, GFP_KERNEL);
746 set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
747 set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
753 spin_unlock(&gl->gl_spin);
756 gfs2_holder_put(new_gh);
759 void gfs2_glock_inode_squish(struct inode *inode)
761 struct gfs2_holder gh;
762 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
763 gfs2_holder_init(gl, LM_ST_UNLOCKED, 0, &gh);
764 set_bit(HIF_DEMOTE, &gh.gh_iflags);
765 spin_lock(&gl->gl_spin);
766 gfs2_assert(inode->i_sb->s_fs_info, list_empty(&gl->gl_holders));
767 list_add_tail(&gh.gh_list, &gl->gl_waiters2);
769 spin_unlock(&gl->gl_spin);
770 wait_for_completion(&gh.gh_wait);
771 gfs2_holder_uninit(&gh);
775 * state_change - record that the glock is now in a different state
777 * @new_state the new state
781 static void state_change(struct gfs2_glock *gl, unsigned int new_state)
785 held1 = (gl->gl_state != LM_ST_UNLOCKED);
786 held2 = (new_state != LM_ST_UNLOCKED);
788 if (held1 != held2) {
795 gl->gl_state = new_state;
799 * xmote_bh - Called after the lock module is done acquiring a lock
800 * @gl: The glock in question
801 * @ret: the int returned from the lock module
805 static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
807 struct gfs2_sbd *sdp = gl->gl_sbd;
808 struct gfs2_glock_operations *glops = gl->gl_ops;
809 struct gfs2_holder *gh = gl->gl_req_gh;
810 int prev_state = gl->gl_state;
813 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
814 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
815 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
817 state_change(gl, ret & LM_OUT_ST_MASK);
819 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
821 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
822 } else if (gl->gl_state == LM_ST_DEFERRED) {
823 /* We might not want to do this here.
824 Look at moving to the inode glops. */
826 glops->go_inval(gl, DIO_DATA);
829 /* Deal with each possible exit condition */
832 gl->gl_stamp = jiffies;
834 else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
835 spin_lock(&gl->gl_spin);
836 list_del_init(&gh->gh_list);
838 spin_unlock(&gl->gl_spin);
840 } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
841 spin_lock(&gl->gl_spin);
842 list_del_init(&gh->gh_list);
843 if (gl->gl_state == gh->gh_state ||
844 gl->gl_state == LM_ST_UNLOCKED)
847 if (gfs2_assert_warn(sdp, gh->gh_flags &
848 (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
849 fs_warn(sdp, "ret = 0x%.8X\n", ret);
850 gh->gh_error = GLR_TRYFAILED;
852 spin_unlock(&gl->gl_spin);
854 if (ret & LM_OUT_CANCELED)
855 handle_callback(gl, LM_ST_UNLOCKED); /* Lame */
857 } else if (ret & LM_OUT_CANCELED) {
858 spin_lock(&gl->gl_spin);
859 list_del_init(&gh->gh_list);
860 gh->gh_error = GLR_CANCELED;
861 spin_unlock(&gl->gl_spin);
863 } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
864 spin_lock(&gl->gl_spin);
865 list_move_tail(&gh->gh_list, &gl->gl_holders);
867 set_bit(HIF_HOLDER, &gh->gh_iflags);
868 spin_unlock(&gl->gl_spin);
870 set_bit(HIF_FIRST, &gh->gh_iflags);
874 } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
875 spin_lock(&gl->gl_spin);
876 list_del_init(&gh->gh_list);
877 gh->gh_error = GLR_TRYFAILED;
878 spin_unlock(&gl->gl_spin);
881 if (gfs2_assert_withdraw(sdp, 0) == -1)
882 fs_err(sdp, "ret = 0x%.8X\n", ret);
885 if (glops->go_xmote_bh)
886 glops->go_xmote_bh(gl);
889 spin_lock(&gl->gl_spin);
890 gl->gl_req_gh = NULL;
891 gl->gl_req_bh = NULL;
892 clear_bit(GLF_LOCK, &gl->gl_flags);
894 spin_unlock(&gl->gl_spin);
900 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
903 complete(&gh->gh_wait);
908 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
909 * @gl: The glock in question
910 * @state: the requested state
911 * @flags: modifier flags to the lock call
915 void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags)
917 struct gfs2_sbd *sdp = gl->gl_sbd;
918 struct gfs2_glock_operations *glops = gl->gl_ops;
919 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
920 LM_FLAG_NOEXP | LM_FLAG_ANY |
922 unsigned int lck_ret;
924 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
925 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
926 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
927 gfs2_assert_warn(sdp, state != gl->gl_state);
929 if (gl->gl_state == LM_ST_EXCLUSIVE) {
932 DIO_METADATA | DIO_DATA | DIO_RELEASE);
936 gl->gl_req_bh = xmote_bh;
938 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state,
941 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
944 if (lck_ret & LM_OUT_ASYNC)
945 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
947 xmote_bh(gl, lck_ret);
951 * drop_bh - Called after a lock module unlock completes
953 * @ret: the return status
955 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
956 * Doesn't drop the reference on the glock the top half took out
960 static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
962 struct gfs2_sbd *sdp = gl->gl_sbd;
963 struct gfs2_glock_operations *glops = gl->gl_ops;
964 struct gfs2_holder *gh = gl->gl_req_gh;
966 clear_bit(GLF_PREFETCH, &gl->gl_flags);
968 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
969 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
970 gfs2_assert_warn(sdp, !ret);
972 state_change(gl, LM_ST_UNLOCKED);
975 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
978 spin_lock(&gl->gl_spin);
979 list_del_init(&gh->gh_list);
981 spin_unlock(&gl->gl_spin);
984 if (glops->go_drop_bh)
985 glops->go_drop_bh(gl);
987 spin_lock(&gl->gl_spin);
988 gl->gl_req_gh = NULL;
989 gl->gl_req_bh = NULL;
990 clear_bit(GLF_LOCK, &gl->gl_flags);
992 spin_unlock(&gl->gl_spin);
997 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
1000 complete(&gh->gh_wait);
1005 * gfs2_glock_drop_th - call into the lock module to unlock a lock
1010 void gfs2_glock_drop_th(struct gfs2_glock *gl)
1012 struct gfs2_sbd *sdp = gl->gl_sbd;
1013 struct gfs2_glock_operations *glops = gl->gl_ops;
1016 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1017 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
1018 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
1020 if (gl->gl_state == LM_ST_EXCLUSIVE) {
1023 DIO_METADATA | DIO_DATA | DIO_RELEASE);
1026 gfs2_glock_hold(gl);
1027 gl->gl_req_bh = drop_bh;
1029 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
1031 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
1037 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
1041 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1042 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1044 * Don't cancel GL_NOCANCEL requests.
1047 static void do_cancels(struct gfs2_holder *gh)
1049 struct gfs2_glock *gl = gh->gh_gl;
1051 spin_lock(&gl->gl_spin);
1053 while (gl->gl_req_gh != gh &&
1054 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1055 !list_empty(&gh->gh_list)) {
1056 if (gl->gl_req_bh &&
1058 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
1059 spin_unlock(&gl->gl_spin);
1060 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
1062 spin_lock(&gl->gl_spin);
1064 spin_unlock(&gl->gl_spin);
1066 spin_lock(&gl->gl_spin);
1070 spin_unlock(&gl->gl_spin);
1074 * glock_wait_internal - wait on a glock acquisition
1075 * @gh: the glock holder
1077 * Returns: 0 on success
1080 static int glock_wait_internal(struct gfs2_holder *gh)
1082 struct gfs2_glock *gl = gh->gh_gl;
1083 struct gfs2_sbd *sdp = gl->gl_sbd;
1084 struct gfs2_glock_operations *glops = gl->gl_ops;
1086 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1089 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1090 spin_lock(&gl->gl_spin);
1091 if (gl->gl_req_gh != gh &&
1092 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1093 !list_empty(&gh->gh_list)) {
1094 list_del_init(&gh->gh_list);
1095 gh->gh_error = GLR_TRYFAILED;
1097 spin_unlock(&gl->gl_spin);
1098 return gh->gh_error;
1100 spin_unlock(&gl->gl_spin);
1103 if (gh->gh_flags & LM_FLAG_PRIORITY)
1106 wait_for_completion(&gh->gh_wait);
1109 return gh->gh_error;
1111 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
1112 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state,
1116 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1117 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1119 if (glops->go_lock) {
1120 gh->gh_error = glops->go_lock(gh);
1122 spin_lock(&gl->gl_spin);
1123 list_del_init(&gh->gh_list);
1124 spin_unlock(&gl->gl_spin);
1128 spin_lock(&gl->gl_spin);
1129 gl->gl_req_gh = NULL;
1130 gl->gl_req_bh = NULL;
1131 clear_bit(GLF_LOCK, &gl->gl_flags);
1133 spin_unlock(&gl->gl_spin);
1136 return gh->gh_error;
1139 static inline struct gfs2_holder *
1140 find_holder_by_owner(struct list_head *head, struct task_struct *owner)
1142 struct gfs2_holder *gh;
1144 list_for_each_entry(gh, head, gh_list) {
1145 if (gh->gh_owner == owner)
1153 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1154 * @gh: the holder structure to add
1158 static void add_to_queue(struct gfs2_holder *gh)
1160 struct gfs2_glock *gl = gh->gh_gl;
1161 struct gfs2_holder *existing;
1163 BUG_ON(!gh->gh_owner);
1165 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
1167 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1168 printk(KERN_INFO "pid : %d\n", existing->gh_owner->pid);
1169 printk(KERN_INFO "lock type : %d lock state : %d\n",
1170 existing->gh_gl->gl_name.ln_type, existing->gh_gl->gl_state);
1171 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1172 printk(KERN_INFO "pid : %d\n", gh->gh_owner->pid);
1173 printk(KERN_INFO "lock type : %d lock state : %d\n",
1174 gl->gl_name.ln_type, gl->gl_state);
1178 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
1180 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1181 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1185 if (gh->gh_flags & LM_FLAG_PRIORITY)
1186 list_add(&gh->gh_list, &gl->gl_waiters3);
1188 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1192 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1193 * @gh: the holder structure
1195 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1197 * Returns: 0, GLR_TRYFAILED, or errno on failure
1200 int gfs2_glock_nq(struct gfs2_holder *gh)
1202 struct gfs2_glock *gl = gh->gh_gl;
1203 struct gfs2_sbd *sdp = gl->gl_sbd;
1207 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1208 set_bit(HIF_ABORTED, &gh->gh_iflags);
1212 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1214 spin_lock(&gl->gl_spin);
1217 spin_unlock(&gl->gl_spin);
1219 if (!(gh->gh_flags & GL_ASYNC)) {
1220 error = glock_wait_internal(gh);
1221 if (error == GLR_CANCELED) {
1227 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1229 if (error == GLR_TRYFAILED && (gh->gh_flags & GL_DUMP))
1236 * gfs2_glock_poll - poll to see if an async request has been completed
1239 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1242 int gfs2_glock_poll(struct gfs2_holder *gh)
1244 struct gfs2_glock *gl = gh->gh_gl;
1247 spin_lock(&gl->gl_spin);
1249 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1251 else if (list_empty(&gh->gh_list)) {
1252 if (gh->gh_error == GLR_CANCELED) {
1253 spin_unlock(&gl->gl_spin);
1255 if (gfs2_glock_nq(gh))
1262 spin_unlock(&gl->gl_spin);
1268 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1269 * @gh: the holder structure
1271 * Returns: 0, GLR_TRYFAILED, or errno on failure
1274 int gfs2_glock_wait(struct gfs2_holder *gh)
1278 error = glock_wait_internal(gh);
1279 if (error == GLR_CANCELED) {
1281 gh->gh_flags &= ~GL_ASYNC;
1282 error = gfs2_glock_nq(gh);
1289 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1290 * @gh: the glock holder
1294 void gfs2_glock_dq(struct gfs2_holder *gh)
1296 struct gfs2_glock *gl = gh->gh_gl;
1297 struct gfs2_glock_operations *glops = gl->gl_ops;
1299 if (gh->gh_flags & GL_SYNC)
1300 set_bit(GLF_SYNC, &gl->gl_flags);
1302 if (gh->gh_flags & GL_NOCACHE)
1303 handle_callback(gl, LM_ST_UNLOCKED);
1305 gfs2_glmutex_lock(gl);
1307 spin_lock(&gl->gl_spin);
1308 list_del_init(&gh->gh_list);
1310 if (list_empty(&gl->gl_holders)) {
1311 spin_unlock(&gl->gl_spin);
1313 if (glops->go_unlock)
1314 glops->go_unlock(gh);
1316 if (test_bit(GLF_SYNC, &gl->gl_flags)) {
1318 glops->go_sync(gl, DIO_METADATA | DIO_DATA);
1321 gl->gl_stamp = jiffies;
1323 spin_lock(&gl->gl_spin);
1326 clear_bit(GLF_LOCK, &gl->gl_flags);
1328 spin_unlock(&gl->gl_spin);
1332 * gfs2_glock_prefetch - Try to prefetch a glock
1334 * @state: the state to prefetch in
1335 * @flags: flags passed to go_xmote_th()
1339 static void gfs2_glock_prefetch(struct gfs2_glock *gl, unsigned int state,
1342 struct gfs2_glock_operations *glops = gl->gl_ops;
1344 spin_lock(&gl->gl_spin);
1346 if (test_bit(GLF_LOCK, &gl->gl_flags) ||
1347 !list_empty(&gl->gl_holders) ||
1348 !list_empty(&gl->gl_waiters1) ||
1349 !list_empty(&gl->gl_waiters2) ||
1350 !list_empty(&gl->gl_waiters3) ||
1351 relaxed_state_ok(gl->gl_state, state, flags)) {
1352 spin_unlock(&gl->gl_spin);
1356 set_bit(GLF_PREFETCH, &gl->gl_flags);
1357 set_bit(GLF_LOCK, &gl->gl_flags);
1358 spin_unlock(&gl->gl_spin);
1360 glops->go_xmote_th(gl, state, flags);
1363 static void greedy_work(void *data)
1365 struct greedy *gr = data;
1366 struct gfs2_holder *gh = &gr->gr_gh;
1367 struct gfs2_glock *gl = gh->gh_gl;
1368 struct gfs2_glock_operations *glops = gl->gl_ops;
1370 clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1372 if (glops->go_greedy)
1373 glops->go_greedy(gl);
1375 spin_lock(&gl->gl_spin);
1377 if (list_empty(&gl->gl_waiters2)) {
1378 clear_bit(GLF_GREEDY, &gl->gl_flags);
1379 spin_unlock(&gl->gl_spin);
1380 gfs2_holder_uninit(gh);
1383 gfs2_glock_hold(gl);
1384 list_add_tail(&gh->gh_list, &gl->gl_waiters2);
1386 spin_unlock(&gl->gl_spin);
1392 * gfs2_glock_be_greedy -
1396 * Returns: 0 if go_greedy will be called, 1 otherwise
1399 int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time)
1402 struct gfs2_holder *gh;
1404 if (!time || gl->gl_sbd->sd_args.ar_localcaching ||
1405 test_and_set_bit(GLF_GREEDY, &gl->gl_flags))
1408 gr = kmalloc(sizeof(struct greedy), GFP_KERNEL);
1410 clear_bit(GLF_GREEDY, &gl->gl_flags);
1415 gfs2_holder_init(gl, 0, 0, gh);
1416 set_bit(HIF_GREEDY, &gh->gh_iflags);
1417 INIT_WORK(&gr->gr_work, greedy_work, gr);
1419 set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1420 schedule_delayed_work(&gr->gr_work, time);
1426 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1427 * @gh: the holder structure
1431 void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1434 gfs2_holder_uninit(gh);
1438 * gfs2_glock_nq_num - acquire a glock based on lock number
1439 * @sdp: the filesystem
1440 * @number: the lock number
1441 * @glops: the glock operations for the type of glock
1442 * @state: the state to acquire the glock in
1443 * @flags: modifier flags for the aquisition
1444 * @gh: the struct gfs2_holder
1449 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, uint64_t number,
1450 struct gfs2_glock_operations *glops, unsigned int state,
1451 int flags, struct gfs2_holder *gh)
1453 struct gfs2_glock *gl;
1456 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1458 error = gfs2_glock_nq_init(gl, state, flags, gh);
1466 * glock_compare - Compare two struct gfs2_glock structures for sorting
1467 * @arg_a: the first structure
1468 * @arg_b: the second structure
1472 static int glock_compare(const void *arg_a, const void *arg_b)
1474 struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
1475 struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
1476 struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1477 struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1480 if (a->ln_number > b->ln_number)
1482 else if (a->ln_number < b->ln_number)
1485 if (gh_a->gh_state == LM_ST_SHARED &&
1486 gh_b->gh_state == LM_ST_EXCLUSIVE)
1488 else if (!(gh_a->gh_flags & GL_LOCAL_EXCL) &&
1489 (gh_b->gh_flags & GL_LOCAL_EXCL))
1497 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1498 * @num_gh: the number of structures
1499 * @ghs: an array of struct gfs2_holder structures
1501 * Returns: 0 on success (all glocks acquired),
1502 * errno on failure (no glocks acquired)
1505 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1506 struct gfs2_holder **p)
1511 for (x = 0; x < num_gh; x++)
1514 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1516 for (x = 0; x < num_gh; x++) {
1517 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1519 error = gfs2_glock_nq(p[x]);
1522 gfs2_glock_dq(p[x]);
1531 * gfs2_glock_nq_m - acquire multiple glocks
1532 * @num_gh: the number of structures
1533 * @ghs: an array of struct gfs2_holder structures
1535 * Figure out how big an impact this function has. Either:
1536 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1537 * 2) Forget async stuff and just call nq_m_sync()
1538 * 3) Leave it like it is
1540 * Returns: 0 on success (all glocks acquired),
1541 * errno on failure (no glocks acquired)
1544 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1548 int borked = 0, serious = 0;
1555 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1556 return gfs2_glock_nq(ghs);
1559 e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1563 for (x = 0; x < num_gh; x++) {
1564 ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
1565 error = gfs2_glock_nq(&ghs[x]);
1574 for (x = 0; x < num_gh; x++) {
1575 error = e[x] = glock_wait_internal(&ghs[x]);
1578 if (error != GLR_TRYFAILED && error != GLR_CANCELED)
1588 for (x = 0; x < num_gh; x++)
1590 gfs2_glock_dq(&ghs[x]);
1595 for (x = 0; x < num_gh; x++)
1596 gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
1598 error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
1607 * gfs2_glock_dq_m - release multiple glocks
1608 * @num_gh: the number of structures
1609 * @ghs: an array of struct gfs2_holder structures
1613 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1617 for (x = 0; x < num_gh; x++)
1618 gfs2_glock_dq(&ghs[x]);
1622 * gfs2_glock_dq_uninit_m - release multiple glocks
1623 * @num_gh: the number of structures
1624 * @ghs: an array of struct gfs2_holder structures
1628 void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1632 for (x = 0; x < num_gh; x++)
1633 gfs2_glock_dq_uninit(&ghs[x]);
1637 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1638 * @sdp: the filesystem
1639 * @number: the lock number
1640 * @glops: the glock operations for the type of glock
1641 * @state: the state to acquire the glock in
1642 * @flags: modifier flags for the aquisition
1647 void gfs2_glock_prefetch_num(struct gfs2_sbd *sdp, uint64_t number,
1648 struct gfs2_glock_operations *glops,
1649 unsigned int state, int flags)
1651 struct gfs2_glock *gl;
1654 if (atomic_read(&sdp->sd_reclaim_count) <
1655 gfs2_tune_get(sdp, gt_reclaim_limit)) {
1656 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1658 gfs2_glock_prefetch(gl, state, flags);
1665 * gfs2_lvb_hold - attach a LVB from a glock
1666 * @gl: The glock in question
1670 int gfs2_lvb_hold(struct gfs2_glock *gl)
1674 gfs2_glmutex_lock(gl);
1676 if (!atomic_read(&gl->gl_lvb_count)) {
1677 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1679 gfs2_glmutex_unlock(gl);
1682 gfs2_glock_hold(gl);
1684 atomic_inc(&gl->gl_lvb_count);
1686 gfs2_glmutex_unlock(gl);
1692 * gfs2_lvb_unhold - detach a LVB from a glock
1693 * @gl: The glock in question
1697 void gfs2_lvb_unhold(struct gfs2_glock *gl)
1699 gfs2_glock_hold(gl);
1700 gfs2_glmutex_lock(gl);
1702 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1703 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1704 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1709 gfs2_glmutex_unlock(gl);
1714 void gfs2_lvb_sync(struct gfs2_glock *gl)
1716 gfs2_glmutex_lock(gl);
1718 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count));
1719 if (!gfs2_assert_warn(gl->gl_sbd, gfs2_glock_is_held_excl(gl)))
1720 gfs2_lm_sync_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1722 gfs2_glmutex_unlock(gl);
1726 static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1729 struct gfs2_glock *gl;
1731 gl = gfs2_glock_find(sdp, name);
1735 if (gl->gl_ops->go_callback)
1736 gl->gl_ops->go_callback(gl, state);
1737 handle_callback(gl, state);
1739 spin_lock(&gl->gl_spin);
1741 spin_unlock(&gl->gl_spin);
1747 * gfs2_glock_cb - Callback used by locking module
1748 * @fsdata: Pointer to the superblock
1749 * @type: Type of callback
1750 * @data: Type dependent data pointer
1752 * Called by the locking module when it wants to tell us something.
1753 * Either we need to drop a lock, one of our ASYNC requests completed, or
1754 * a journal from another client needs to be recovered.
1757 void gfs2_glock_cb(lm_fsdata_t *fsdata, unsigned int type, void *data)
1759 struct gfs2_sbd *sdp = (struct gfs2_sbd *)fsdata;
1763 blocking_cb(sdp, data, LM_ST_UNLOCKED);
1767 blocking_cb(sdp, data, LM_ST_DEFERRED);
1771 blocking_cb(sdp, data, LM_ST_SHARED);
1775 struct lm_async_cb *async = data;
1776 struct gfs2_glock *gl;
1778 gl = gfs2_glock_find(sdp, &async->lc_name);
1779 if (gfs2_assert_warn(sdp, gl))
1781 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1782 gl->gl_req_bh(gl, async->lc_ret);
1787 case LM_CB_NEED_RECOVERY:
1788 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1789 if (sdp->sd_recoverd_process)
1790 wake_up_process(sdp->sd_recoverd_process);
1793 case LM_CB_DROPLOCKS:
1794 gfs2_gl_hash_clear(sdp, NO_WAIT);
1795 gfs2_quota_scan(sdp);
1799 gfs2_assert_warn(sdp, 0);
1805 * gfs2_iopen_go_callback - Try to kick the inode/vnode associated with an
1806 * iopen glock from memory
1807 * @io_gl: the iopen glock
1808 * @state: the state into which the glock should be put
1812 void gfs2_iopen_go_callback(struct gfs2_glock *io_gl, unsigned int state)
1815 if (state != LM_ST_UNLOCKED)
1817 /* FIXME: remove this? */
1821 * demote_ok - Check to see if it's ok to unlock a glock
1824 * Returns: 1 if it's ok
1827 static int demote_ok(struct gfs2_glock *gl)
1829 struct gfs2_sbd *sdp = gl->gl_sbd;
1830 struct gfs2_glock_operations *glops = gl->gl_ops;
1833 if (test_bit(GLF_STICKY, &gl->gl_flags))
1835 else if (test_bit(GLF_PREFETCH, &gl->gl_flags))
1836 demote = time_after_eq(jiffies,
1838 gfs2_tune_get(sdp, gt_prefetch_secs) * HZ);
1839 else if (glops->go_demote_ok)
1840 demote = glops->go_demote_ok(gl);
1846 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1851 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1853 struct gfs2_sbd *sdp = gl->gl_sbd;
1855 spin_lock(&sdp->sd_reclaim_lock);
1856 if (list_empty(&gl->gl_reclaim)) {
1857 gfs2_glock_hold(gl);
1858 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1859 atomic_inc(&sdp->sd_reclaim_count);
1861 spin_unlock(&sdp->sd_reclaim_lock);
1863 wake_up(&sdp->sd_reclaim_wq);
1867 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1868 * @sdp: the filesystem
1870 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1871 * different glock and we notice that there are a lot of glocks in the
1876 void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1878 struct gfs2_glock *gl;
1880 spin_lock(&sdp->sd_reclaim_lock);
1881 if (list_empty(&sdp->sd_reclaim_list)) {
1882 spin_unlock(&sdp->sd_reclaim_lock);
1885 gl = list_entry(sdp->sd_reclaim_list.next,
1886 struct gfs2_glock, gl_reclaim);
1887 list_del_init(&gl->gl_reclaim);
1888 spin_unlock(&sdp->sd_reclaim_lock);
1890 atomic_dec(&sdp->sd_reclaim_count);
1891 atomic_inc(&sdp->sd_reclaimed);
1893 if (gfs2_glmutex_trylock(gl)) {
1894 if (queue_empty(gl, &gl->gl_holders) &&
1895 gl->gl_state != LM_ST_UNLOCKED &&
1897 handle_callback(gl, LM_ST_UNLOCKED);
1898 gfs2_glmutex_unlock(gl);
1905 * examine_bucket - Call a function for glock in a hash bucket
1906 * @examiner: the function
1907 * @sdp: the filesystem
1908 * @bucket: the bucket
1910 * Returns: 1 if the bucket has entries
1913 static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
1914 struct gfs2_gl_hash_bucket *bucket)
1916 struct glock_plug plug;
1917 struct list_head *tmp;
1918 struct gfs2_glock *gl;
1921 /* Add "plug" to end of bucket list, work back up list from there */
1922 memset(&plug.gl_flags, 0, sizeof(unsigned long));
1923 set_bit(GLF_PLUG, &plug.gl_flags);
1925 write_lock(&bucket->hb_lock);
1926 list_add(&plug.gl_list, &bucket->hb_list);
1927 write_unlock(&bucket->hb_lock);
1930 write_lock(&bucket->hb_lock);
1933 tmp = plug.gl_list.next;
1935 if (tmp == &bucket->hb_list) {
1936 list_del(&plug.gl_list);
1937 entries = !list_empty(&bucket->hb_list);
1938 write_unlock(&bucket->hb_lock);
1941 gl = list_entry(tmp, struct gfs2_glock, gl_list);
1943 /* Move plug up list */
1944 list_move(&plug.gl_list, &gl->gl_list);
1946 if (test_bit(GLF_PLUG, &gl->gl_flags))
1949 /* examiner() must glock_put() */
1950 gfs2_glock_hold(gl);
1955 write_unlock(&bucket->hb_lock);
1962 * scan_glock - look at a glock and see if we can reclaim it
1963 * @gl: the glock to look at
1967 static void scan_glock(struct gfs2_glock *gl)
1969 if (gl->gl_ops == &gfs2_inode_glops)
1972 if (gfs2_glmutex_trylock(gl)) {
1973 if (queue_empty(gl, &gl->gl_holders) &&
1974 gl->gl_state != LM_ST_UNLOCKED &&
1977 gfs2_glmutex_unlock(gl);
1984 gfs2_glmutex_unlock(gl);
1985 gfs2_glock_schedule_for_reclaim(gl);
1990 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
1991 * @sdp: the filesystem
1995 void gfs2_scand_internal(struct gfs2_sbd *sdp)
1999 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2000 examine_bucket(scan_glock, sdp, &sdp->sd_gl_hash[x]);
2006 * clear_glock - look at a glock and see if we can free it from glock cache
2007 * @gl: the glock to look at
2011 static void clear_glock(struct gfs2_glock *gl)
2013 struct gfs2_sbd *sdp = gl->gl_sbd;
2016 spin_lock(&sdp->sd_reclaim_lock);
2017 if (!list_empty(&gl->gl_reclaim)) {
2018 list_del_init(&gl->gl_reclaim);
2019 atomic_dec(&sdp->sd_reclaim_count);
2020 spin_unlock(&sdp->sd_reclaim_lock);
2021 released = gfs2_glock_put(gl);
2022 gfs2_assert(sdp, !released);
2024 spin_unlock(&sdp->sd_reclaim_lock);
2027 if (gfs2_glmutex_trylock(gl)) {
2028 if (queue_empty(gl, &gl->gl_holders) &&
2029 gl->gl_state != LM_ST_UNLOCKED)
2030 handle_callback(gl, LM_ST_UNLOCKED);
2032 gfs2_glmutex_unlock(gl);
2039 * gfs2_gl_hash_clear - Empty out the glock hash table
2040 * @sdp: the filesystem
2041 * @wait: wait until it's all gone
2043 * Called when unmounting the filesystem, or when inter-node lock manager
2044 * requests DROPLOCKS because it is running out of capacity.
2047 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
2058 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
2059 if (examine_bucket(clear_glock, sdp,
2060 &sdp->sd_gl_hash[x]))
2066 if (time_after_eq(jiffies,
2067 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
2068 fs_warn(sdp, "Unmount seems to be stalled. "
2069 "Dumping lock state...\n");
2070 gfs2_dump_lockstate(sdp);
2074 invalidate_inodes(sdp->sd_vfs);
2080 * Diagnostic routines to help debug distributed deadlock
2084 * dump_holder - print information about a glock holder
2085 * @str: a string naming the type of holder
2086 * @gh: the glock holder
2088 * Returns: 0 on success, -ENOBUFS when we run out of space
2091 static int dump_holder(char *str, struct gfs2_holder *gh)
2094 int error = -ENOBUFS;
2096 printk(KERN_INFO " %s\n", str);
2097 printk(KERN_INFO " owner = %ld\n",
2098 (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
2099 printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
2100 printk(KERN_INFO " gh_flags =");
2101 for (x = 0; x < 32; x++)
2102 if (gh->gh_flags & (1 << x))
2105 printk(KERN_INFO " error = %d\n", gh->gh_error);
2106 printk(KERN_INFO " gh_iflags =");
2107 for (x = 0; x < 32; x++)
2108 if (test_bit(x, &gh->gh_iflags))
2111 print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip);
2119 * dump_inode - print information about an inode
2122 * Returns: 0 on success, -ENOBUFS when we run out of space
2125 static int dump_inode(struct gfs2_inode *ip)
2128 int error = -ENOBUFS;
2130 printk(KERN_INFO " Inode:\n");
2131 printk(KERN_INFO " num = %llu %llu\n",
2132 (unsigned long long)ip->i_num.no_formal_ino,
2133 (unsigned long long)ip->i_num.no_addr);
2134 printk(KERN_INFO " type = %u\n", IF2DT(ip->i_di.di_mode));
2135 printk(KERN_INFO " i_flags =");
2136 for (x = 0; x < 32; x++)
2137 if (test_bit(x, &ip->i_flags))
2147 * dump_glock - print information about a glock
2149 * @count: where we are in the buffer
2151 * Returns: 0 on success, -ENOBUFS when we run out of space
2154 static int dump_glock(struct gfs2_glock *gl)
2156 struct gfs2_holder *gh;
2158 int error = -ENOBUFS;
2160 spin_lock(&gl->gl_spin);
2162 printk(KERN_INFO "Glock 0x%p (%u, %llu)\n",
2164 gl->gl_name.ln_type,
2165 (unsigned long long)gl->gl_name.ln_number);
2166 printk(KERN_INFO " gl_flags =");
2167 for (x = 0; x < 32; x++)
2168 if (test_bit(x, &gl->gl_flags))
2171 printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref.refcount));
2172 printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
2173 printk(KERN_INFO " gl_owner = %s\n", gl->gl_owner->comm);
2174 print_symbol(KERN_INFO " gl_ip = %s\n", gl->gl_ip);
2175 printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
2176 printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
2177 printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
2178 printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
2179 printk(KERN_INFO " le = %s\n",
2180 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
2181 printk(KERN_INFO " reclaim = %s\n",
2182 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
2184 printk(KERN_INFO " aspace = 0x%p nrpages = %lu\n",
2186 gl->gl_aspace->i_mapping->nrpages);
2188 printk(KERN_INFO " aspace = no\n");
2189 printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
2190 if (gl->gl_req_gh) {
2191 error = dump_holder("Request", gl->gl_req_gh);
2195 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
2196 error = dump_holder("Holder", gh);
2200 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
2201 error = dump_holder("Waiter1", gh);
2205 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
2206 error = dump_holder("Waiter2", gh);
2210 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
2211 error = dump_holder("Waiter3", gh);
2215 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
2216 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
2217 list_empty(&gl->gl_holders)) {
2218 error = dump_inode(gl->gl_object);
2223 printk(KERN_INFO " Inode: busy\n");
2230 spin_unlock(&gl->gl_spin);
2236 * gfs2_dump_lockstate - print out the current lockstate
2237 * @sdp: the filesystem
2238 * @ub: the buffer to copy the information into
2240 * If @ub is NULL, dump the lockstate to the console.
2244 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
2246 struct gfs2_gl_hash_bucket *bucket;
2247 struct gfs2_glock *gl;
2251 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2252 bucket = &sdp->sd_gl_hash[x];
2254 read_lock(&bucket->hb_lock);
2256 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
2257 if (test_bit(GLF_PLUG, &gl->gl_flags))
2260 error = dump_glock(gl);
2265 read_unlock(&bucket->hb_lock);