2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License v.2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/delay.h>
16 #include <linux/sort.h>
17 #include <linux/jhash.h>
18 #include <linux/kref.h>
19 #include <linux/kallsyms.h>
20 #include <linux/gfs2_ondisk.h>
21 #include <asm/uaccess.h>
24 #include "lm_interface.h"
36 /* Must be kept in sync with the beginning of struct gfs2_glock */
38 struct list_head gl_list;
39 unsigned long gl_flags;
43 struct gfs2_holder gr_gh;
44 struct work_struct gr_work;
47 typedef void (*glock_examiner) (struct gfs2_glock * gl);
49 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
50 static int dump_glock(struct gfs2_glock *gl);
53 * relaxed_state_ok - is a requested lock compatible with the current lock mode?
54 * @actual: the current state of the lock
55 * @requested: the lock state that was requested by the caller
56 * @flags: the modifier flags passed in by the caller
58 * Returns: 1 if the locks are compatible, 0 otherwise
61 static inline int relaxed_state_ok(unsigned int actual, unsigned requested,
64 if (actual == requested)
70 if (actual == LM_ST_EXCLUSIVE && requested == LM_ST_SHARED)
73 if (actual != LM_ST_UNLOCKED && (flags & LM_FLAG_ANY))
80 * gl_hash() - Turn glock number into hash bucket number
81 * @lock: The glock number
83 * Returns: The number of the corresponding hash bucket
86 static unsigned int gl_hash(struct lm_lockname *name)
90 h = jhash(&name->ln_number, sizeof(uint64_t), 0);
91 h = jhash(&name->ln_type, sizeof(unsigned int), h);
92 h &= GFS2_GL_HASH_MASK;
98 * glock_free() - Perform a few checks and then release struct gfs2_glock
99 * @gl: The glock to release
101 * Also calls lock module to release its internal structure for this glock.
105 static void glock_free(struct gfs2_glock *gl)
107 struct gfs2_sbd *sdp = gl->gl_sbd;
108 struct inode *aspace = gl->gl_aspace;
110 gfs2_lm_put_lock(sdp, gl->gl_lock);
113 gfs2_aspace_put(aspace);
115 kmem_cache_free(gfs2_glock_cachep, gl);
119 * gfs2_glock_hold() - increment reference count on glock
120 * @gl: The glock to hold
124 void gfs2_glock_hold(struct gfs2_glock *gl)
126 kref_get(&gl->gl_ref);
129 /* All work is done after the return from kref_put() so we
130 can release the write_lock before the free. */
132 static void kill_glock(struct kref *kref)
134 struct gfs2_glock *gl = container_of(kref, struct gfs2_glock, gl_ref);
135 struct gfs2_sbd *sdp = gl->gl_sbd;
137 gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED);
138 gfs2_assert(sdp, list_empty(&gl->gl_reclaim));
139 gfs2_assert(sdp, list_empty(&gl->gl_holders));
140 gfs2_assert(sdp, list_empty(&gl->gl_waiters1));
141 gfs2_assert(sdp, list_empty(&gl->gl_waiters2));
142 gfs2_assert(sdp, list_empty(&gl->gl_waiters3));
146 * gfs2_glock_put() - Decrement reference count on glock
147 * @gl: The glock to put
151 int gfs2_glock_put(struct gfs2_glock *gl)
153 struct gfs2_sbd *sdp = gl->gl_sbd;
154 struct gfs2_gl_hash_bucket *bucket = gl->gl_bucket;
157 mutex_lock(&sdp->sd_invalidate_inodes_mutex);
159 write_lock(&bucket->hb_lock);
160 if (kref_put(&gl->gl_ref, kill_glock)) {
161 list_del_init(&gl->gl_list);
162 write_unlock(&bucket->hb_lock);
163 BUG_ON(spin_is_locked(&gl->gl_spin));
168 write_unlock(&bucket->hb_lock);
170 mutex_unlock(&sdp->sd_invalidate_inodes_mutex);
175 * queue_empty - check to see if a glock's queue is empty
177 * @head: the head of the queue to check
179 * This function protects the list in the event that a process already
180 * has a holder on the list and is adding a second holder for itself.
181 * The glmutex lock is what generally prevents processes from working
182 * on the same glock at once, but the special case of adding a second
183 * holder for yourself ("recursive" locking) doesn't involve locking
184 * glmutex, making the spin lock necessary.
186 * Returns: 1 if the queue is empty
189 static inline int queue_empty(struct gfs2_glock *gl, struct list_head *head)
192 spin_lock(&gl->gl_spin);
193 empty = list_empty(head);
194 spin_unlock(&gl->gl_spin);
199 * search_bucket() - Find struct gfs2_glock by lock number
200 * @bucket: the bucket to search
201 * @name: The lock name
203 * Returns: NULL, or the struct gfs2_glock with the requested number
206 static struct gfs2_glock *search_bucket(struct gfs2_gl_hash_bucket *bucket,
207 struct lm_lockname *name)
209 struct gfs2_glock *gl;
211 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
212 if (test_bit(GLF_PLUG, &gl->gl_flags))
214 if (!lm_name_equal(&gl->gl_name, name))
217 kref_get(&gl->gl_ref);
226 * gfs2_glock_find() - Find glock by lock number
227 * @sdp: The GFS2 superblock
228 * @name: The lock name
230 * Returns: NULL, or the struct gfs2_glock with the requested number
233 static struct gfs2_glock *gfs2_glock_find(struct gfs2_sbd *sdp,
234 struct lm_lockname *name)
236 struct gfs2_gl_hash_bucket *bucket = &sdp->sd_gl_hash[gl_hash(name)];
237 struct gfs2_glock *gl;
239 read_lock(&bucket->hb_lock);
240 gl = search_bucket(bucket, name);
241 read_unlock(&bucket->hb_lock);
247 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
248 * @sdp: The GFS2 superblock
249 * @number: the lock number
250 * @glops: The glock_operations to use
251 * @create: If 0, don't create the glock if it doesn't exist
252 * @glp: the glock is returned here
254 * This does not lock a glock, just finds/creates structures for one.
259 int gfs2_glock_get(struct gfs2_sbd *sdp, uint64_t number,
260 struct gfs2_glock_operations *glops, int create,
261 struct gfs2_glock **glp)
263 struct lm_lockname name;
264 struct gfs2_glock *gl, *tmp;
265 struct gfs2_gl_hash_bucket *bucket;
268 name.ln_number = number;
269 name.ln_type = glops->go_type;
270 bucket = &sdp->sd_gl_hash[gl_hash(&name)];
272 read_lock(&bucket->hb_lock);
273 gl = search_bucket(bucket, &name);
274 read_unlock(&bucket->hb_lock);
281 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
285 memset(gl, 0, sizeof(struct gfs2_glock));
287 INIT_LIST_HEAD(&gl->gl_list);
289 kref_init(&gl->gl_ref);
291 spin_lock_init(&gl->gl_spin);
293 gl->gl_state = LM_ST_UNLOCKED;
296 INIT_LIST_HEAD(&gl->gl_holders);
297 INIT_LIST_HEAD(&gl->gl_waiters1);
298 INIT_LIST_HEAD(&gl->gl_waiters2);
299 INIT_LIST_HEAD(&gl->gl_waiters3);
303 gl->gl_bucket = bucket;
304 INIT_LIST_HEAD(&gl->gl_reclaim);
308 lops_init_le(&gl->gl_le, &gfs2_glock_lops);
309 INIT_LIST_HEAD(&gl->gl_ail_list);
311 /* If this glock protects actual on-disk data or metadata blocks,
312 create a VFS inode to manage the pages/buffers holding them. */
313 if (glops == &gfs2_inode_glops ||
314 glops == &gfs2_rgrp_glops ||
315 glops == &gfs2_meta_glops) {
316 gl->gl_aspace = gfs2_aspace_get(sdp);
317 if (!gl->gl_aspace) {
323 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
327 write_lock(&bucket->hb_lock);
328 tmp = search_bucket(bucket, &name);
330 write_unlock(&bucket->hb_lock);
334 list_add_tail(&gl->gl_list, &bucket->hb_list);
335 write_unlock(&bucket->hb_lock);
344 gfs2_aspace_put(gl->gl_aspace);
347 kmem_cache_free(gfs2_glock_cachep, gl);
353 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
355 * @state: the state we're requesting
356 * @flags: the modifier flags
357 * @gh: the holder structure
361 void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
362 struct gfs2_holder *gh)
364 INIT_LIST_HEAD(&gh->gh_list);
366 gh->gh_ip = (unsigned long)__builtin_return_address(0);
367 gh->gh_owner = current;
368 gh->gh_state = state;
369 gh->gh_flags = flags;
372 init_completion(&gh->gh_wait);
374 if (gh->gh_state == LM_ST_EXCLUSIVE)
375 gh->gh_flags |= GL_LOCAL_EXCL;
381 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
382 * @state: the state we're requesting
383 * @flags: the modifier flags
384 * @gh: the holder structure
386 * Don't mess with the glock.
390 void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
392 gh->gh_state = state;
393 gh->gh_flags = flags;
394 if (gh->gh_state == LM_ST_EXCLUSIVE)
395 gh->gh_flags |= GL_LOCAL_EXCL;
397 gh->gh_iflags &= 1 << HIF_ALLOCED;
398 gh->gh_ip = (unsigned long)__builtin_return_address(0);
402 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
403 * @gh: the holder structure
407 void gfs2_holder_uninit(struct gfs2_holder *gh)
409 gfs2_glock_put(gh->gh_gl);
415 * gfs2_holder_get - get a struct gfs2_holder structure
417 * @state: the state we're requesting
418 * @flags: the modifier flags
419 * @gfp_flags: __GFP_NOFAIL
421 * Figure out how big an impact this function has. Either:
422 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
423 * 2) Leave it like it is
425 * Returns: the holder structure, NULL on ENOMEM
428 static struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl,
430 int flags, gfp_t gfp_flags)
432 struct gfs2_holder *gh;
434 gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags);
438 gfs2_holder_init(gl, state, flags, gh);
439 set_bit(HIF_ALLOCED, &gh->gh_iflags);
440 gh->gh_ip = (unsigned long)__builtin_return_address(0);
445 * gfs2_holder_put - get rid of a struct gfs2_holder structure
446 * @gh: the holder structure
450 static void gfs2_holder_put(struct gfs2_holder *gh)
452 gfs2_holder_uninit(gh);
457 * rq_mutex - process a mutex request in the queue
458 * @gh: the glock holder
460 * Returns: 1 if the queue is blocked
463 static int rq_mutex(struct gfs2_holder *gh)
465 struct gfs2_glock *gl = gh->gh_gl;
467 list_del_init(&gh->gh_list);
468 /* gh->gh_error never examined. */
469 set_bit(GLF_LOCK, &gl->gl_flags);
470 complete(&gh->gh_wait);
476 * rq_promote - process a promote request in the queue
477 * @gh: the glock holder
479 * Acquire a new inter-node lock, or change a lock state to more restrictive.
481 * Returns: 1 if the queue is blocked
484 static int rq_promote(struct gfs2_holder *gh)
486 struct gfs2_glock *gl = gh->gh_gl;
487 struct gfs2_sbd *sdp = gl->gl_sbd;
488 struct gfs2_glock_operations *glops = gl->gl_ops;
490 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
491 if (list_empty(&gl->gl_holders)) {
493 set_bit(GLF_LOCK, &gl->gl_flags);
494 spin_unlock(&gl->gl_spin);
496 if (atomic_read(&sdp->sd_reclaim_count) >
497 gfs2_tune_get(sdp, gt_reclaim_limit) &&
498 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
499 gfs2_reclaim_glock(sdp);
500 gfs2_reclaim_glock(sdp);
503 glops->go_xmote_th(gl, gh->gh_state,
506 spin_lock(&gl->gl_spin);
511 if (list_empty(&gl->gl_holders)) {
512 set_bit(HIF_FIRST, &gh->gh_iflags);
513 set_bit(GLF_LOCK, &gl->gl_flags);
515 struct gfs2_holder *next_gh;
516 if (gh->gh_flags & GL_LOCAL_EXCL)
518 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
520 if (next_gh->gh_flags & GL_LOCAL_EXCL)
524 list_move_tail(&gh->gh_list, &gl->gl_holders);
526 set_bit(HIF_HOLDER, &gh->gh_iflags);
528 complete(&gh->gh_wait);
534 * rq_demote - process a demote request in the queue
535 * @gh: the glock holder
537 * Returns: 1 if the queue is blocked
540 static int rq_demote(struct gfs2_holder *gh)
542 struct gfs2_glock *gl = gh->gh_gl;
543 struct gfs2_glock_operations *glops = gl->gl_ops;
545 if (!list_empty(&gl->gl_holders))
548 if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) {
549 list_del_init(&gh->gh_list);
551 spin_unlock(&gl->gl_spin);
552 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
555 complete(&gh->gh_wait);
556 spin_lock(&gl->gl_spin);
559 set_bit(GLF_LOCK, &gl->gl_flags);
560 spin_unlock(&gl->gl_spin);
562 if (gh->gh_state == LM_ST_UNLOCKED ||
563 gl->gl_state != LM_ST_EXCLUSIVE)
564 glops->go_drop_th(gl);
566 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
568 spin_lock(&gl->gl_spin);
575 * rq_greedy - process a queued request to drop greedy status
576 * @gh: the glock holder
578 * Returns: 1 if the queue is blocked
581 static int rq_greedy(struct gfs2_holder *gh)
583 struct gfs2_glock *gl = gh->gh_gl;
585 list_del_init(&gh->gh_list);
586 /* gh->gh_error never examined. */
587 clear_bit(GLF_GREEDY, &gl->gl_flags);
588 spin_unlock(&gl->gl_spin);
590 gfs2_holder_uninit(gh);
591 kfree(container_of(gh, struct greedy, gr_gh));
593 spin_lock(&gl->gl_spin);
599 * run_queue - process holder structures on a glock
603 static void run_queue(struct gfs2_glock *gl)
605 struct gfs2_holder *gh;
609 if (test_bit(GLF_LOCK, &gl->gl_flags))
612 if (!list_empty(&gl->gl_waiters1)) {
613 gh = list_entry(gl->gl_waiters1.next,
614 struct gfs2_holder, gh_list);
616 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
617 blocked = rq_mutex(gh);
619 gfs2_assert_warn(gl->gl_sbd, 0);
621 } else if (!list_empty(&gl->gl_waiters2) &&
622 !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) {
623 gh = list_entry(gl->gl_waiters2.next,
624 struct gfs2_holder, gh_list);
626 if (test_bit(HIF_DEMOTE, &gh->gh_iflags))
627 blocked = rq_demote(gh);
628 else if (test_bit(HIF_GREEDY, &gh->gh_iflags))
629 blocked = rq_greedy(gh);
631 gfs2_assert_warn(gl->gl_sbd, 0);
633 } else if (!list_empty(&gl->gl_waiters3)) {
634 gh = list_entry(gl->gl_waiters3.next,
635 struct gfs2_holder, gh_list);
637 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
638 blocked = rq_promote(gh);
640 gfs2_assert_warn(gl->gl_sbd, 0);
651 * gfs2_glmutex_lock - acquire a local lock on a glock
654 * Gives caller exclusive access to manipulate a glock structure.
657 void gfs2_glmutex_lock(struct gfs2_glock *gl)
659 struct gfs2_holder gh;
661 gfs2_holder_init(gl, 0, 0, &gh);
662 set_bit(HIF_MUTEX, &gh.gh_iflags);
664 spin_lock(&gl->gl_spin);
665 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
666 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
668 gl->gl_owner = current;
669 gl->gl_ip = (unsigned long)__builtin_return_address(0);
670 complete(&gh.gh_wait);
672 spin_unlock(&gl->gl_spin);
674 wait_for_completion(&gh.gh_wait);
675 gfs2_holder_uninit(&gh);
679 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
682 * Returns: 1 if the glock is acquired
685 static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
689 spin_lock(&gl->gl_spin);
690 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
693 gl->gl_owner = current;
694 gl->gl_ip = (unsigned long)__builtin_return_address(0);
696 spin_unlock(&gl->gl_spin);
702 * gfs2_glmutex_unlock - release a local lock on a glock
707 void gfs2_glmutex_unlock(struct gfs2_glock *gl)
709 spin_lock(&gl->gl_spin);
710 clear_bit(GLF_LOCK, &gl->gl_flags);
714 BUG_ON(!spin_is_locked(&gl->gl_spin));
715 spin_unlock(&gl->gl_spin);
719 * handle_callback - add a demote request to a lock's queue
721 * @state: the state the caller wants us to change to
725 static void handle_callback(struct gfs2_glock *gl, unsigned int state)
727 struct gfs2_holder *gh, *new_gh = NULL;
730 spin_lock(&gl->gl_spin);
732 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
733 if (test_bit(HIF_DEMOTE, &gh->gh_iflags) &&
734 gl->gl_req_gh != gh) {
735 if (gh->gh_state != state)
736 gh->gh_state = LM_ST_UNLOCKED;
742 list_add_tail(&new_gh->gh_list, &gl->gl_waiters2);
745 spin_unlock(&gl->gl_spin);
747 new_gh = gfs2_holder_get(gl, state, LM_FLAG_TRY,
748 GFP_KERNEL | __GFP_NOFAIL),
749 set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
750 set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
756 spin_unlock(&gl->gl_spin);
759 gfs2_holder_put(new_gh);
763 * state_change - record that the glock is now in a different state
765 * @new_state the new state
769 static void state_change(struct gfs2_glock *gl, unsigned int new_state)
773 held1 = (gl->gl_state != LM_ST_UNLOCKED);
774 held2 = (new_state != LM_ST_UNLOCKED);
776 if (held1 != held2) {
783 gl->gl_state = new_state;
787 * xmote_bh - Called after the lock module is done acquiring a lock
788 * @gl: The glock in question
789 * @ret: the int returned from the lock module
793 static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
795 struct gfs2_sbd *sdp = gl->gl_sbd;
796 struct gfs2_glock_operations *glops = gl->gl_ops;
797 struct gfs2_holder *gh = gl->gl_req_gh;
798 int prev_state = gl->gl_state;
801 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
802 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
803 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
805 state_change(gl, ret & LM_OUT_ST_MASK);
807 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
809 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
810 } else if (gl->gl_state == LM_ST_DEFERRED) {
811 /* We might not want to do this here.
812 Look at moving to the inode glops. */
814 glops->go_inval(gl, DIO_DATA);
817 /* Deal with each possible exit condition */
820 gl->gl_stamp = jiffies;
822 else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
823 spin_lock(&gl->gl_spin);
824 list_del_init(&gh->gh_list);
826 spin_unlock(&gl->gl_spin);
828 } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
829 spin_lock(&gl->gl_spin);
830 list_del_init(&gh->gh_list);
831 if (gl->gl_state == gh->gh_state ||
832 gl->gl_state == LM_ST_UNLOCKED)
835 if (gfs2_assert_warn(sdp, gh->gh_flags &
836 (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
837 fs_warn(sdp, "ret = 0x%.8X\n", ret);
838 gh->gh_error = GLR_TRYFAILED;
840 spin_unlock(&gl->gl_spin);
842 if (ret & LM_OUT_CANCELED)
843 handle_callback(gl, LM_ST_UNLOCKED); /* Lame */
845 } else if (ret & LM_OUT_CANCELED) {
846 spin_lock(&gl->gl_spin);
847 list_del_init(&gh->gh_list);
848 gh->gh_error = GLR_CANCELED;
849 spin_unlock(&gl->gl_spin);
851 } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
852 spin_lock(&gl->gl_spin);
853 list_move_tail(&gh->gh_list, &gl->gl_holders);
855 set_bit(HIF_HOLDER, &gh->gh_iflags);
856 spin_unlock(&gl->gl_spin);
858 set_bit(HIF_FIRST, &gh->gh_iflags);
862 } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
863 spin_lock(&gl->gl_spin);
864 list_del_init(&gh->gh_list);
865 gh->gh_error = GLR_TRYFAILED;
866 spin_unlock(&gl->gl_spin);
869 if (gfs2_assert_withdraw(sdp, 0) == -1)
870 fs_err(sdp, "ret = 0x%.8X\n", ret);
873 if (glops->go_xmote_bh)
874 glops->go_xmote_bh(gl);
877 spin_lock(&gl->gl_spin);
878 gl->gl_req_gh = NULL;
879 gl->gl_req_bh = NULL;
880 clear_bit(GLF_LOCK, &gl->gl_flags);
882 spin_unlock(&gl->gl_spin);
888 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
891 complete(&gh->gh_wait);
896 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
897 * @gl: The glock in question
898 * @state: the requested state
899 * @flags: modifier flags to the lock call
903 void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags)
905 struct gfs2_sbd *sdp = gl->gl_sbd;
906 struct gfs2_glock_operations *glops = gl->gl_ops;
907 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
908 LM_FLAG_NOEXP | LM_FLAG_ANY |
910 unsigned int lck_ret;
912 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
913 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
914 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
915 gfs2_assert_warn(sdp, state != gl->gl_state);
917 if (gl->gl_state == LM_ST_EXCLUSIVE) {
920 DIO_METADATA | DIO_DATA | DIO_RELEASE);
924 gl->gl_req_bh = xmote_bh;
926 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state,
929 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
932 if (lck_ret & LM_OUT_ASYNC)
933 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
935 xmote_bh(gl, lck_ret);
939 * drop_bh - Called after a lock module unlock completes
941 * @ret: the return status
943 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
944 * Doesn't drop the reference on the glock the top half took out
948 static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
950 struct gfs2_sbd *sdp = gl->gl_sbd;
951 struct gfs2_glock_operations *glops = gl->gl_ops;
952 struct gfs2_holder *gh = gl->gl_req_gh;
954 clear_bit(GLF_PREFETCH, &gl->gl_flags);
956 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
957 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
958 gfs2_assert_warn(sdp, !ret);
960 state_change(gl, LM_ST_UNLOCKED);
963 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
966 spin_lock(&gl->gl_spin);
967 list_del_init(&gh->gh_list);
969 spin_unlock(&gl->gl_spin);
972 if (glops->go_drop_bh)
973 glops->go_drop_bh(gl);
975 spin_lock(&gl->gl_spin);
976 gl->gl_req_gh = NULL;
977 gl->gl_req_bh = NULL;
978 clear_bit(GLF_LOCK, &gl->gl_flags);
980 spin_unlock(&gl->gl_spin);
985 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
988 complete(&gh->gh_wait);
993 * gfs2_glock_drop_th - call into the lock module to unlock a lock
998 void gfs2_glock_drop_th(struct gfs2_glock *gl)
1000 struct gfs2_sbd *sdp = gl->gl_sbd;
1001 struct gfs2_glock_operations *glops = gl->gl_ops;
1004 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1005 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
1006 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
1008 if (gl->gl_state == LM_ST_EXCLUSIVE) {
1011 DIO_METADATA | DIO_DATA | DIO_RELEASE);
1014 gfs2_glock_hold(gl);
1015 gl->gl_req_bh = drop_bh;
1017 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
1019 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
1025 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
1029 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1030 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1032 * Don't cancel GL_NOCANCEL requests.
1035 static void do_cancels(struct gfs2_holder *gh)
1037 struct gfs2_glock *gl = gh->gh_gl;
1039 spin_lock(&gl->gl_spin);
1041 while (gl->gl_req_gh != gh &&
1042 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1043 !list_empty(&gh->gh_list)) {
1044 if (gl->gl_req_bh &&
1046 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
1047 spin_unlock(&gl->gl_spin);
1048 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
1050 spin_lock(&gl->gl_spin);
1052 spin_unlock(&gl->gl_spin);
1054 spin_lock(&gl->gl_spin);
1058 spin_unlock(&gl->gl_spin);
1062 * glock_wait_internal - wait on a glock acquisition
1063 * @gh: the glock holder
1065 * Returns: 0 on success
1068 static int glock_wait_internal(struct gfs2_holder *gh)
1070 struct gfs2_glock *gl = gh->gh_gl;
1071 struct gfs2_sbd *sdp = gl->gl_sbd;
1072 struct gfs2_glock_operations *glops = gl->gl_ops;
1074 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1077 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1078 spin_lock(&gl->gl_spin);
1079 if (gl->gl_req_gh != gh &&
1080 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1081 !list_empty(&gh->gh_list)) {
1082 list_del_init(&gh->gh_list);
1083 gh->gh_error = GLR_TRYFAILED;
1085 spin_unlock(&gl->gl_spin);
1086 return gh->gh_error;
1088 spin_unlock(&gl->gl_spin);
1091 if (gh->gh_flags & LM_FLAG_PRIORITY)
1094 wait_for_completion(&gh->gh_wait);
1097 return gh->gh_error;
1099 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
1100 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state,
1104 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1105 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1107 if (glops->go_lock) {
1108 gh->gh_error = glops->go_lock(gh);
1110 spin_lock(&gl->gl_spin);
1111 list_del_init(&gh->gh_list);
1112 spin_unlock(&gl->gl_spin);
1116 spin_lock(&gl->gl_spin);
1117 gl->gl_req_gh = NULL;
1118 gl->gl_req_bh = NULL;
1119 clear_bit(GLF_LOCK, &gl->gl_flags);
1121 spin_unlock(&gl->gl_spin);
1124 return gh->gh_error;
1127 static inline struct gfs2_holder *
1128 find_holder_by_owner(struct list_head *head, struct task_struct *owner)
1130 struct gfs2_holder *gh;
1132 list_for_each_entry(gh, head, gh_list) {
1133 if (gh->gh_owner == owner)
1141 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1142 * @gh: the holder structure to add
1146 static void add_to_queue(struct gfs2_holder *gh)
1148 struct gfs2_glock *gl = gh->gh_gl;
1149 struct gfs2_holder *existing;
1151 BUG_ON(!gh->gh_owner);
1153 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
1155 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1156 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1160 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
1162 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1163 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1167 if (gh->gh_flags & LM_FLAG_PRIORITY)
1168 list_add(&gh->gh_list, &gl->gl_waiters3);
1170 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1174 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1175 * @gh: the holder structure
1177 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1179 * Returns: 0, GLR_TRYFAILED, or errno on failure
1182 int gfs2_glock_nq(struct gfs2_holder *gh)
1184 struct gfs2_glock *gl = gh->gh_gl;
1185 struct gfs2_sbd *sdp = gl->gl_sbd;
1189 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1190 set_bit(HIF_ABORTED, &gh->gh_iflags);
1194 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1196 spin_lock(&gl->gl_spin);
1199 spin_unlock(&gl->gl_spin);
1201 if (!(gh->gh_flags & GL_ASYNC)) {
1202 error = glock_wait_internal(gh);
1203 if (error == GLR_CANCELED) {
1209 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1211 if (error == GLR_TRYFAILED && (gh->gh_flags & GL_DUMP))
1218 * gfs2_glock_poll - poll to see if an async request has been completed
1221 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1224 int gfs2_glock_poll(struct gfs2_holder *gh)
1226 struct gfs2_glock *gl = gh->gh_gl;
1229 spin_lock(&gl->gl_spin);
1231 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1233 else if (list_empty(&gh->gh_list)) {
1234 if (gh->gh_error == GLR_CANCELED) {
1235 spin_unlock(&gl->gl_spin);
1237 if (gfs2_glock_nq(gh))
1244 spin_unlock(&gl->gl_spin);
1250 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1251 * @gh: the holder structure
1253 * Returns: 0, GLR_TRYFAILED, or errno on failure
1256 int gfs2_glock_wait(struct gfs2_holder *gh)
1260 error = glock_wait_internal(gh);
1261 if (error == GLR_CANCELED) {
1263 gh->gh_flags &= ~GL_ASYNC;
1264 error = gfs2_glock_nq(gh);
1271 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1272 * @gh: the glock holder
1276 void gfs2_glock_dq(struct gfs2_holder *gh)
1278 struct gfs2_glock *gl = gh->gh_gl;
1279 struct gfs2_glock_operations *glops = gl->gl_ops;
1281 if (gh->gh_flags & GL_SYNC)
1282 set_bit(GLF_SYNC, &gl->gl_flags);
1284 if (gh->gh_flags & GL_NOCACHE)
1285 handle_callback(gl, LM_ST_UNLOCKED);
1287 gfs2_glmutex_lock(gl);
1289 spin_lock(&gl->gl_spin);
1290 list_del_init(&gh->gh_list);
1292 if (list_empty(&gl->gl_holders)) {
1293 spin_unlock(&gl->gl_spin);
1295 if (glops->go_unlock)
1296 glops->go_unlock(gh);
1298 if (test_bit(GLF_SYNC, &gl->gl_flags)) {
1300 glops->go_sync(gl, DIO_METADATA | DIO_DATA);
1303 gl->gl_stamp = jiffies;
1305 spin_lock(&gl->gl_spin);
1308 clear_bit(GLF_LOCK, &gl->gl_flags);
1310 spin_unlock(&gl->gl_spin);
1314 * gfs2_glock_prefetch - Try to prefetch a glock
1316 * @state: the state to prefetch in
1317 * @flags: flags passed to go_xmote_th()
1321 static void gfs2_glock_prefetch(struct gfs2_glock *gl, unsigned int state,
1324 struct gfs2_glock_operations *glops = gl->gl_ops;
1326 spin_lock(&gl->gl_spin);
1328 if (test_bit(GLF_LOCK, &gl->gl_flags) ||
1329 !list_empty(&gl->gl_holders) ||
1330 !list_empty(&gl->gl_waiters1) ||
1331 !list_empty(&gl->gl_waiters2) ||
1332 !list_empty(&gl->gl_waiters3) ||
1333 relaxed_state_ok(gl->gl_state, state, flags)) {
1334 spin_unlock(&gl->gl_spin);
1338 set_bit(GLF_PREFETCH, &gl->gl_flags);
1339 set_bit(GLF_LOCK, &gl->gl_flags);
1340 spin_unlock(&gl->gl_spin);
1342 glops->go_xmote_th(gl, state, flags);
1345 static void greedy_work(void *data)
1347 struct greedy *gr = data;
1348 struct gfs2_holder *gh = &gr->gr_gh;
1349 struct gfs2_glock *gl = gh->gh_gl;
1350 struct gfs2_glock_operations *glops = gl->gl_ops;
1352 clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1354 if (glops->go_greedy)
1355 glops->go_greedy(gl);
1357 spin_lock(&gl->gl_spin);
1359 if (list_empty(&gl->gl_waiters2)) {
1360 clear_bit(GLF_GREEDY, &gl->gl_flags);
1361 spin_unlock(&gl->gl_spin);
1362 gfs2_holder_uninit(gh);
1365 gfs2_glock_hold(gl);
1366 list_add_tail(&gh->gh_list, &gl->gl_waiters2);
1368 spin_unlock(&gl->gl_spin);
1374 * gfs2_glock_be_greedy -
1378 * Returns: 0 if go_greedy will be called, 1 otherwise
1381 int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time)
1384 struct gfs2_holder *gh;
1387 gl->gl_sbd->sd_args.ar_localcaching ||
1388 test_and_set_bit(GLF_GREEDY, &gl->gl_flags))
1391 gr = kmalloc(sizeof(struct greedy), GFP_KERNEL);
1393 clear_bit(GLF_GREEDY, &gl->gl_flags);
1398 gfs2_holder_init(gl, 0, 0, gh);
1399 set_bit(HIF_GREEDY, &gh->gh_iflags);
1400 INIT_WORK(&gr->gr_work, greedy_work, gr);
1402 set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1403 schedule_delayed_work(&gr->gr_work, time);
1409 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1410 * @gh: the holder structure
1414 void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1417 gfs2_holder_uninit(gh);
1421 * gfs2_glock_nq_num - acquire a glock based on lock number
1422 * @sdp: the filesystem
1423 * @number: the lock number
1424 * @glops: the glock operations for the type of glock
1425 * @state: the state to acquire the glock in
1426 * @flags: modifier flags for the aquisition
1427 * @gh: the struct gfs2_holder
1432 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, uint64_t number,
1433 struct gfs2_glock_operations *glops, unsigned int state,
1434 int flags, struct gfs2_holder *gh)
1436 struct gfs2_glock *gl;
1439 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1441 error = gfs2_glock_nq_init(gl, state, flags, gh);
1449 * glock_compare - Compare two struct gfs2_glock structures for sorting
1450 * @arg_a: the first structure
1451 * @arg_b: the second structure
1455 static int glock_compare(const void *arg_a, const void *arg_b)
1457 struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
1458 struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
1459 struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1460 struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1463 if (a->ln_number > b->ln_number)
1465 else if (a->ln_number < b->ln_number)
1468 if (gh_a->gh_state == LM_ST_SHARED &&
1469 gh_b->gh_state == LM_ST_EXCLUSIVE)
1471 else if (!(gh_a->gh_flags & GL_LOCAL_EXCL) &&
1472 (gh_b->gh_flags & GL_LOCAL_EXCL))
1480 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1481 * @num_gh: the number of structures
1482 * @ghs: an array of struct gfs2_holder structures
1484 * Returns: 0 on success (all glocks acquired),
1485 * errno on failure (no glocks acquired)
1488 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1489 struct gfs2_holder **p)
1494 for (x = 0; x < num_gh; x++)
1497 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1499 for (x = 0; x < num_gh; x++) {
1500 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1502 error = gfs2_glock_nq(p[x]);
1505 gfs2_glock_dq(p[x]);
1514 * gfs2_glock_nq_m - acquire multiple glocks
1515 * @num_gh: the number of structures
1516 * @ghs: an array of struct gfs2_holder structures
1518 * Figure out how big an impact this function has. Either:
1519 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1520 * 2) Forget async stuff and just call nq_m_sync()
1521 * 3) Leave it like it is
1523 * Returns: 0 on success (all glocks acquired),
1524 * errno on failure (no glocks acquired)
1527 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1531 int borked = 0, serious = 0;
1538 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1539 return gfs2_glock_nq(ghs);
1542 e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1546 for (x = 0; x < num_gh; x++) {
1547 ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
1548 error = gfs2_glock_nq(&ghs[x]);
1557 for (x = 0; x < num_gh; x++) {
1558 error = e[x] = glock_wait_internal(&ghs[x]);
1561 if (error != GLR_TRYFAILED && error != GLR_CANCELED)
1571 for (x = 0; x < num_gh; x++)
1573 gfs2_glock_dq(&ghs[x]);
1578 for (x = 0; x < num_gh; x++)
1579 gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
1581 error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
1590 * gfs2_glock_dq_m - release multiple glocks
1591 * @num_gh: the number of structures
1592 * @ghs: an array of struct gfs2_holder structures
1596 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1600 for (x = 0; x < num_gh; x++)
1601 gfs2_glock_dq(&ghs[x]);
1605 * gfs2_glock_dq_uninit_m - release multiple glocks
1606 * @num_gh: the number of structures
1607 * @ghs: an array of struct gfs2_holder structures
1611 void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1615 for (x = 0; x < num_gh; x++)
1616 gfs2_glock_dq_uninit(&ghs[x]);
1620 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1621 * @sdp: the filesystem
1622 * @number: the lock number
1623 * @glops: the glock operations for the type of glock
1624 * @state: the state to acquire the glock in
1625 * @flags: modifier flags for the aquisition
1630 void gfs2_glock_prefetch_num(struct gfs2_sbd *sdp, uint64_t number,
1631 struct gfs2_glock_operations *glops,
1632 unsigned int state, int flags)
1634 struct gfs2_glock *gl;
1637 if (atomic_read(&sdp->sd_reclaim_count) <
1638 gfs2_tune_get(sdp, gt_reclaim_limit)) {
1639 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1641 gfs2_glock_prefetch(gl, state, flags);
1648 * gfs2_lvb_hold - attach a LVB from a glock
1649 * @gl: The glock in question
1653 int gfs2_lvb_hold(struct gfs2_glock *gl)
1657 gfs2_glmutex_lock(gl);
1659 if (!atomic_read(&gl->gl_lvb_count)) {
1660 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1662 gfs2_glmutex_unlock(gl);
1665 gfs2_glock_hold(gl);
1667 atomic_inc(&gl->gl_lvb_count);
1669 gfs2_glmutex_unlock(gl);
1675 * gfs2_lvb_unhold - detach a LVB from a glock
1676 * @gl: The glock in question
1680 void gfs2_lvb_unhold(struct gfs2_glock *gl)
1682 gfs2_glock_hold(gl);
1683 gfs2_glmutex_lock(gl);
1685 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1686 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1687 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1692 gfs2_glmutex_unlock(gl);
1697 void gfs2_lvb_sync(struct gfs2_glock *gl)
1699 gfs2_glmutex_lock(gl);
1701 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count));
1702 if (!gfs2_assert_warn(gl->gl_sbd, gfs2_glock_is_held_excl(gl)))
1703 gfs2_lm_sync_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1705 gfs2_glmutex_unlock(gl);
1709 static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1712 struct gfs2_glock *gl;
1714 gl = gfs2_glock_find(sdp, name);
1718 if (gl->gl_ops->go_callback)
1719 gl->gl_ops->go_callback(gl, state);
1720 handle_callback(gl, state);
1722 spin_lock(&gl->gl_spin);
1724 spin_unlock(&gl->gl_spin);
1730 * gfs2_glock_cb - Callback used by locking module
1731 * @fsdata: Pointer to the superblock
1732 * @type: Type of callback
1733 * @data: Type dependent data pointer
1735 * Called by the locking module when it wants to tell us something.
1736 * Either we need to drop a lock, one of our ASYNC requests completed, or
1737 * a journal from another client needs to be recovered.
1740 void gfs2_glock_cb(lm_fsdata_t *fsdata, unsigned int type, void *data)
1742 struct gfs2_sbd *sdp = (struct gfs2_sbd *)fsdata;
1746 blocking_cb(sdp, data, LM_ST_UNLOCKED);
1750 blocking_cb(sdp, data, LM_ST_DEFERRED);
1754 blocking_cb(sdp, data, LM_ST_SHARED);
1758 struct lm_async_cb *async = data;
1759 struct gfs2_glock *gl;
1761 gl = gfs2_glock_find(sdp, &async->lc_name);
1762 if (gfs2_assert_warn(sdp, gl))
1764 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1765 gl->gl_req_bh(gl, async->lc_ret);
1770 case LM_CB_NEED_RECOVERY:
1771 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1772 if (sdp->sd_recoverd_process)
1773 wake_up_process(sdp->sd_recoverd_process);
1776 case LM_CB_DROPLOCKS:
1777 gfs2_gl_hash_clear(sdp, NO_WAIT);
1778 gfs2_quota_scan(sdp);
1782 gfs2_assert_warn(sdp, 0);
1788 * gfs2_try_toss_inode - try to remove a particular inode struct from cache
1789 * sdp: the filesystem
1790 * inum: the inode number
1794 void gfs2_try_toss_inode(struct gfs2_sbd *sdp, struct gfs2_inum *inum)
1796 struct gfs2_glock *gl;
1797 struct gfs2_inode *ip;
1800 error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_inode_glops,
1805 if (!gfs2_glmutex_trylock(gl))
1812 if (atomic_read(&ip->i_count))
1815 gfs2_inode_destroy(ip, 1);
1818 gfs2_glmutex_unlock(gl);
1825 * gfs2_iopen_go_callback - Try to kick the inode/vnode associated with an
1826 * iopen glock from memory
1827 * @io_gl: the iopen glock
1828 * @state: the state into which the glock should be put
1832 void gfs2_iopen_go_callback(struct gfs2_glock *io_gl, unsigned int state)
1834 struct gfs2_glock *i_gl;
1836 if (state != LM_ST_UNLOCKED)
1839 spin_lock(&io_gl->gl_spin);
1840 i_gl = io_gl->gl_object;
1842 gfs2_glock_hold(i_gl);
1843 spin_unlock(&io_gl->gl_spin);
1845 spin_unlock(&io_gl->gl_spin);
1849 if (gfs2_glmutex_trylock(i_gl)) {
1850 struct gfs2_inode *ip = i_gl->gl_object;
1852 gfs2_try_toss_vnode(ip);
1853 gfs2_glmutex_unlock(i_gl);
1854 gfs2_glock_schedule_for_reclaim(i_gl);
1857 gfs2_glmutex_unlock(i_gl);
1861 gfs2_glock_put(i_gl);
1865 * demote_ok - Check to see if it's ok to unlock a glock
1868 * Returns: 1 if it's ok
1871 static int demote_ok(struct gfs2_glock *gl)
1873 struct gfs2_sbd *sdp = gl->gl_sbd;
1874 struct gfs2_glock_operations *glops = gl->gl_ops;
1877 if (test_bit(GLF_STICKY, &gl->gl_flags))
1879 else if (test_bit(GLF_PREFETCH, &gl->gl_flags))
1880 demote = time_after_eq(jiffies,
1882 gfs2_tune_get(sdp, gt_prefetch_secs) * HZ);
1883 else if (glops->go_demote_ok)
1884 demote = glops->go_demote_ok(gl);
1890 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1895 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1897 struct gfs2_sbd *sdp = gl->gl_sbd;
1899 spin_lock(&sdp->sd_reclaim_lock);
1900 if (list_empty(&gl->gl_reclaim)) {
1901 gfs2_glock_hold(gl);
1902 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1903 atomic_inc(&sdp->sd_reclaim_count);
1905 spin_unlock(&sdp->sd_reclaim_lock);
1907 wake_up(&sdp->sd_reclaim_wq);
1911 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1912 * @sdp: the filesystem
1914 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1915 * different glock and we notice that there are a lot of glocks in the
1920 void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1922 struct gfs2_glock *gl;
1924 spin_lock(&sdp->sd_reclaim_lock);
1925 if (list_empty(&sdp->sd_reclaim_list)) {
1926 spin_unlock(&sdp->sd_reclaim_lock);
1929 gl = list_entry(sdp->sd_reclaim_list.next,
1930 struct gfs2_glock, gl_reclaim);
1931 list_del_init(&gl->gl_reclaim);
1932 spin_unlock(&sdp->sd_reclaim_lock);
1934 atomic_dec(&sdp->sd_reclaim_count);
1935 atomic_inc(&sdp->sd_reclaimed);
1937 if (gfs2_glmutex_trylock(gl)) {
1938 if (gl->gl_ops == &gfs2_inode_glops) {
1939 struct gfs2_inode *ip = gl->gl_object;
1940 if (ip && !atomic_read(&ip->i_count))
1941 gfs2_inode_destroy(ip, 1);
1943 if (queue_empty(gl, &gl->gl_holders) &&
1944 gl->gl_state != LM_ST_UNLOCKED &&
1946 handle_callback(gl, LM_ST_UNLOCKED);
1947 gfs2_glmutex_unlock(gl);
1954 * examine_bucket - Call a function for glock in a hash bucket
1955 * @examiner: the function
1956 * @sdp: the filesystem
1957 * @bucket: the bucket
1959 * Returns: 1 if the bucket has entries
1962 static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
1963 struct gfs2_gl_hash_bucket *bucket)
1965 struct glock_plug plug;
1966 struct list_head *tmp;
1967 struct gfs2_glock *gl;
1970 /* Add "plug" to end of bucket list, work back up list from there */
1971 memset(&plug.gl_flags, 0, sizeof(unsigned long));
1972 set_bit(GLF_PLUG, &plug.gl_flags);
1974 write_lock(&bucket->hb_lock);
1975 list_add(&plug.gl_list, &bucket->hb_list);
1976 write_unlock(&bucket->hb_lock);
1979 write_lock(&bucket->hb_lock);
1982 tmp = plug.gl_list.next;
1984 if (tmp == &bucket->hb_list) {
1985 list_del(&plug.gl_list);
1986 entries = !list_empty(&bucket->hb_list);
1987 write_unlock(&bucket->hb_lock);
1990 gl = list_entry(tmp, struct gfs2_glock, gl_list);
1992 /* Move plug up list */
1993 list_move(&plug.gl_list, &gl->gl_list);
1995 if (test_bit(GLF_PLUG, &gl->gl_flags))
1998 /* examiner() must glock_put() */
1999 gfs2_glock_hold(gl);
2004 write_unlock(&bucket->hb_lock);
2011 * scan_glock - look at a glock and see if we can reclaim it
2012 * @gl: the glock to look at
2016 static void scan_glock(struct gfs2_glock *gl)
2018 if (gfs2_glmutex_trylock(gl)) {
2019 if (gl->gl_ops == &gfs2_inode_glops) {
2020 struct gfs2_inode *ip = gl->gl_object;
2021 if (ip && !atomic_read(&ip->i_count))
2024 if (queue_empty(gl, &gl->gl_holders) &&
2025 gl->gl_state != LM_ST_UNLOCKED &&
2029 gfs2_glmutex_unlock(gl);
2037 gfs2_glmutex_unlock(gl);
2038 gfs2_glock_schedule_for_reclaim(gl);
2043 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
2044 * @sdp: the filesystem
2048 void gfs2_scand_internal(struct gfs2_sbd *sdp)
2052 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2053 examine_bucket(scan_glock, sdp, &sdp->sd_gl_hash[x]);
2059 * clear_glock - look at a glock and see if we can free it from glock cache
2060 * @gl: the glock to look at
2064 static void clear_glock(struct gfs2_glock *gl)
2066 struct gfs2_sbd *sdp = gl->gl_sbd;
2069 spin_lock(&sdp->sd_reclaim_lock);
2070 if (!list_empty(&gl->gl_reclaim)) {
2071 list_del_init(&gl->gl_reclaim);
2072 atomic_dec(&sdp->sd_reclaim_count);
2073 spin_unlock(&sdp->sd_reclaim_lock);
2074 released = gfs2_glock_put(gl);
2075 gfs2_assert(sdp, !released);
2077 spin_unlock(&sdp->sd_reclaim_lock);
2080 if (gfs2_glmutex_trylock(gl)) {
2081 if (gl->gl_ops == &gfs2_inode_glops) {
2082 struct gfs2_inode *ip = gl->gl_object;
2083 if (ip && !atomic_read(&ip->i_count))
2084 gfs2_inode_destroy(ip, 1);
2086 if (queue_empty(gl, &gl->gl_holders) &&
2087 gl->gl_state != LM_ST_UNLOCKED)
2088 handle_callback(gl, LM_ST_UNLOCKED);
2090 gfs2_glmutex_unlock(gl);
2097 * gfs2_gl_hash_clear - Empty out the glock hash table
2098 * @sdp: the filesystem
2099 * @wait: wait until it's all gone
2101 * Called when unmounting the filesystem, or when inter-node lock manager
2102 * requests DROPLOCKS because it is running out of capacity.
2105 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
2116 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
2117 if (examine_bucket(clear_glock, sdp,
2118 &sdp->sd_gl_hash[x]))
2124 if (time_after_eq(jiffies,
2125 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
2126 fs_warn(sdp, "Unmount seems to be stalled. "
2127 "Dumping lock state...\n");
2128 gfs2_dump_lockstate(sdp);
2132 /* invalidate_inodes() requires that the sb inodes list
2133 not change, but an async completion callback for an
2134 unlock can occur which does glock_put() which
2135 can call iput() which will change the sb inodes list.
2136 invalidate_inodes_mutex prevents glock_put()'s during
2137 an invalidate_inodes() */
2139 mutex_lock(&sdp->sd_invalidate_inodes_mutex);
2140 invalidate_inodes(sdp->sd_vfs);
2141 mutex_unlock(&sdp->sd_invalidate_inodes_mutex);
2147 * Diagnostic routines to help debug distributed deadlock
2151 * dump_holder - print information about a glock holder
2152 * @str: a string naming the type of holder
2153 * @gh: the glock holder
2155 * Returns: 0 on success, -ENOBUFS when we run out of space
2158 static int dump_holder(char *str, struct gfs2_holder *gh)
2161 int error = -ENOBUFS;
2163 printk(KERN_INFO " %s\n", str);
2164 printk(KERN_INFO " owner = %ld\n",
2165 (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
2166 printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
2167 printk(KERN_INFO " gh_flags =");
2168 for (x = 0; x < 32; x++)
2169 if (gh->gh_flags & (1 << x))
2172 printk(KERN_INFO " error = %d\n", gh->gh_error);
2173 printk(KERN_INFO " gh_iflags =");
2174 for (x = 0; x < 32; x++)
2175 if (test_bit(x, &gh->gh_iflags))
2178 print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip);
2186 * dump_inode - print information about an inode
2189 * Returns: 0 on success, -ENOBUFS when we run out of space
2192 static int dump_inode(struct gfs2_inode *ip)
2195 int error = -ENOBUFS;
2197 printk(KERN_INFO " Inode:\n");
2198 printk(KERN_INFO " num = %llu %llu\n",
2199 (unsigned long long)ip->i_num.no_formal_ino,
2200 (unsigned long long)ip->i_num.no_addr);
2201 printk(KERN_INFO " type = %u\n", IF2DT(ip->i_di.di_mode));
2202 printk(KERN_INFO " i_count = %d\n", atomic_read(&ip->i_count));
2203 printk(KERN_INFO " i_flags =");
2204 for (x = 0; x < 32; x++)
2205 if (test_bit(x, &ip->i_flags))
2208 printk(KERN_INFO " vnode = %s\n", (ip->i_vnode) ? "yes" : "no");
2216 * dump_glock - print information about a glock
2218 * @count: where we are in the buffer
2220 * Returns: 0 on success, -ENOBUFS when we run out of space
2223 static int dump_glock(struct gfs2_glock *gl)
2225 struct gfs2_holder *gh;
2227 int error = -ENOBUFS;
2229 spin_lock(&gl->gl_spin);
2231 printk(KERN_INFO "Glock (%u, %llu)\n", gl->gl_name.ln_type,
2232 (unsigned long long)gl->gl_name.ln_number);
2233 printk(KERN_INFO " gl_flags =");
2234 for (x = 0; x < 32; x++)
2235 if (test_bit(x, &gl->gl_flags))
2238 printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref.refcount));
2239 printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
2240 printk(KERN_INFO " gl_owner = %s\n", gl->gl_owner->comm);
2241 print_symbol(KERN_INFO " gl_ip = %s\n", gl->gl_ip);
2242 printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
2243 printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
2244 printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
2245 printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
2246 printk(KERN_INFO " le = %s\n",
2247 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
2248 printk(KERN_INFO " reclaim = %s\n",
2249 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
2251 printk(KERN_INFO " aspace = %lu\n",
2252 gl->gl_aspace->i_mapping->nrpages);
2254 printk(KERN_INFO " aspace = no\n");
2255 printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
2256 if (gl->gl_req_gh) {
2257 error = dump_holder("Request", gl->gl_req_gh);
2261 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
2262 error = dump_holder("Holder", gh);
2266 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
2267 error = dump_holder("Waiter1", gh);
2271 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
2272 error = dump_holder("Waiter2", gh);
2276 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
2277 error = dump_holder("Waiter3", gh);
2281 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
2282 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
2283 list_empty(&gl->gl_holders)) {
2284 error = dump_inode(gl->gl_object);
2289 printk(KERN_INFO " Inode: busy\n");
2296 spin_unlock(&gl->gl_spin);
2302 * gfs2_dump_lockstate - print out the current lockstate
2303 * @sdp: the filesystem
2304 * @ub: the buffer to copy the information into
2306 * If @ub is NULL, dump the lockstate to the console.
2310 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
2312 struct gfs2_gl_hash_bucket *bucket;
2313 struct gfs2_glock *gl;
2317 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2318 bucket = &sdp->sd_gl_hash[x];
2320 read_lock(&bucket->hb_lock);
2322 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
2323 if (test_bit(GLF_PLUG, &gl->gl_flags))
2326 error = dump_glock(gl);
2331 read_unlock(&bucket->hb_lock);