2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/delay.h>
16 #include <linux/sort.h>
17 #include <linux/jhash.h>
18 #include <linux/kref.h>
19 #include <linux/kallsyms.h>
20 #include <linux/gfs2_ondisk.h>
21 #include <asm/uaccess.h>
24 #include "lm_interface.h"
36 /* Must be kept in sync with the beginning of struct gfs2_glock */
38 struct list_head gl_list;
39 unsigned long gl_flags;
43 struct gfs2_holder gr_gh;
44 struct work_struct gr_work;
47 typedef void (*glock_examiner) (struct gfs2_glock * gl);
49 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
50 static int dump_glock(struct gfs2_glock *gl);
53 * relaxed_state_ok - is a requested lock compatible with the current lock mode?
54 * @actual: the current state of the lock
55 * @requested: the lock state that was requested by the caller
56 * @flags: the modifier flags passed in by the caller
58 * Returns: 1 if the locks are compatible, 0 otherwise
61 static inline int relaxed_state_ok(unsigned int actual, unsigned requested,
64 if (actual == requested)
70 if (actual == LM_ST_EXCLUSIVE && requested == LM_ST_SHARED)
73 if (actual != LM_ST_UNLOCKED && (flags & LM_FLAG_ANY))
80 * gl_hash() - Turn glock number into hash bucket number
81 * @lock: The glock number
83 * Returns: The number of the corresponding hash bucket
86 static unsigned int gl_hash(const struct 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 const struct gfs2_sbd *sdp,
204 const struct lm_lockname *name)
206 struct gfs2_glock *gl;
208 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
209 if (test_bit(GLF_PLUG, &gl->gl_flags))
211 if (!lm_name_equal(&gl->gl_name, name))
213 if (gl->gl_sbd != sdp)
216 kref_get(&gl->gl_ref);
225 * gfs2_glock_find() - Find glock by lock number
226 * @sdp: The GFS2 superblock
227 * @name: The lock name
229 * Returns: NULL, or the struct gfs2_glock with the requested number
232 static struct gfs2_glock *gfs2_glock_find(struct gfs2_sbd *sdp,
233 const struct lm_lockname *name)
235 struct gfs2_gl_hash_bucket *bucket = &sdp->sd_gl_hash[gl_hash(name)];
236 struct gfs2_glock *gl;
238 read_lock(&bucket->hb_lock);
239 gl = search_bucket(bucket, sdp, name);
240 read_unlock(&bucket->hb_lock);
246 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
247 * @sdp: The GFS2 superblock
248 * @number: the lock number
249 * @glops: The glock_operations to use
250 * @create: If 0, don't create the glock if it doesn't exist
251 * @glp: the glock is returned here
253 * This does not lock a glock, just finds/creates structures for one.
258 int gfs2_glock_get(struct gfs2_sbd *sdp, uint64_t number,
259 const struct gfs2_glock_operations *glops, int create,
260 struct gfs2_glock **glp)
262 struct lm_lockname name;
263 struct gfs2_glock *gl, *tmp;
264 struct gfs2_gl_hash_bucket *bucket;
267 name.ln_number = number;
268 name.ln_type = glops->go_type;
269 bucket = &sdp->sd_gl_hash[gl_hash(&name)];
271 read_lock(&bucket->hb_lock);
272 gl = search_bucket(bucket, sdp, &name);
273 read_unlock(&bucket->hb_lock);
280 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
286 kref_init(&gl->gl_ref);
287 gl->gl_state = LM_ST_UNLOCKED;
291 gl->gl_req_gh = NULL;
292 gl->gl_req_bh = NULL;
294 gl->gl_stamp = jiffies;
295 gl->gl_object = NULL;
296 gl->gl_bucket = bucket;
298 gl->gl_aspace = NULL;
299 lops_init_le(&gl->gl_le, &gfs2_glock_lops);
301 /* If this glock protects actual on-disk data or metadata blocks,
302 create a VFS inode to manage the pages/buffers holding them. */
303 if (glops == &gfs2_inode_glops || glops == &gfs2_rgrp_glops) {
304 gl->gl_aspace = gfs2_aspace_get(sdp);
305 if (!gl->gl_aspace) {
311 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
315 write_lock(&bucket->hb_lock);
316 tmp = search_bucket(bucket, sdp, &name);
318 write_unlock(&bucket->hb_lock);
322 list_add_tail(&gl->gl_list, &bucket->hb_list);
323 write_unlock(&bucket->hb_lock);
332 gfs2_aspace_put(gl->gl_aspace);
334 kmem_cache_free(gfs2_glock_cachep, gl);
339 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
341 * @state: the state we're requesting
342 * @flags: the modifier flags
343 * @gh: the holder structure
347 void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
348 struct gfs2_holder *gh)
350 INIT_LIST_HEAD(&gh->gh_list);
352 gh->gh_ip = (unsigned long)__builtin_return_address(0);
353 gh->gh_owner = current;
354 gh->gh_state = state;
355 gh->gh_flags = flags;
358 init_completion(&gh->gh_wait);
360 if (gh->gh_state == LM_ST_EXCLUSIVE)
361 gh->gh_flags |= GL_LOCAL_EXCL;
367 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
368 * @state: the state we're requesting
369 * @flags: the modifier flags
370 * @gh: the holder structure
372 * Don't mess with the glock.
376 void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
378 gh->gh_state = state;
379 gh->gh_flags = flags;
380 if (gh->gh_state == LM_ST_EXCLUSIVE)
381 gh->gh_flags |= GL_LOCAL_EXCL;
383 gh->gh_iflags &= 1 << HIF_ALLOCED;
384 gh->gh_ip = (unsigned long)__builtin_return_address(0);
388 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
389 * @gh: the holder structure
393 void gfs2_holder_uninit(struct gfs2_holder *gh)
395 gfs2_glock_put(gh->gh_gl);
401 * gfs2_holder_get - get a struct gfs2_holder structure
403 * @state: the state we're requesting
404 * @flags: the modifier flags
407 * Figure out how big an impact this function has. Either:
408 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
409 * 2) Leave it like it is
411 * Returns: the holder structure, NULL on ENOMEM
414 static struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl,
416 int flags, gfp_t gfp_flags)
418 struct gfs2_holder *gh;
420 gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags);
424 gfs2_holder_init(gl, state, flags, gh);
425 set_bit(HIF_ALLOCED, &gh->gh_iflags);
426 gh->gh_ip = (unsigned long)__builtin_return_address(0);
431 * gfs2_holder_put - get rid of a struct gfs2_holder structure
432 * @gh: the holder structure
436 static void gfs2_holder_put(struct gfs2_holder *gh)
438 gfs2_holder_uninit(gh);
443 * rq_mutex - process a mutex request in the queue
444 * @gh: the glock holder
446 * Returns: 1 if the queue is blocked
449 static int rq_mutex(struct gfs2_holder *gh)
451 struct gfs2_glock *gl = gh->gh_gl;
453 list_del_init(&gh->gh_list);
454 /* gh->gh_error never examined. */
455 set_bit(GLF_LOCK, &gl->gl_flags);
456 complete(&gh->gh_wait);
462 * rq_promote - process a promote request in the queue
463 * @gh: the glock holder
465 * Acquire a new inter-node lock, or change a lock state to more restrictive.
467 * Returns: 1 if the queue is blocked
470 static int rq_promote(struct gfs2_holder *gh)
472 struct gfs2_glock *gl = gh->gh_gl;
473 struct gfs2_sbd *sdp = gl->gl_sbd;
474 const struct gfs2_glock_operations *glops = gl->gl_ops;
476 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
477 if (list_empty(&gl->gl_holders)) {
479 set_bit(GLF_LOCK, &gl->gl_flags);
480 spin_unlock(&gl->gl_spin);
482 if (atomic_read(&sdp->sd_reclaim_count) >
483 gfs2_tune_get(sdp, gt_reclaim_limit) &&
484 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
485 gfs2_reclaim_glock(sdp);
486 gfs2_reclaim_glock(sdp);
489 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
490 spin_lock(&gl->gl_spin);
495 if (list_empty(&gl->gl_holders)) {
496 set_bit(HIF_FIRST, &gh->gh_iflags);
497 set_bit(GLF_LOCK, &gl->gl_flags);
499 struct gfs2_holder *next_gh;
500 if (gh->gh_flags & GL_LOCAL_EXCL)
502 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
504 if (next_gh->gh_flags & GL_LOCAL_EXCL)
508 list_move_tail(&gh->gh_list, &gl->gl_holders);
510 set_bit(HIF_HOLDER, &gh->gh_iflags);
512 complete(&gh->gh_wait);
518 * rq_demote - process a demote request in the queue
519 * @gh: the glock holder
521 * Returns: 1 if the queue is blocked
524 static int rq_demote(struct gfs2_holder *gh)
526 struct gfs2_glock *gl = gh->gh_gl;
527 const struct gfs2_glock_operations *glops = gl->gl_ops;
529 if (!list_empty(&gl->gl_holders))
532 if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) {
533 list_del_init(&gh->gh_list);
535 spin_unlock(&gl->gl_spin);
536 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
539 complete(&gh->gh_wait);
540 spin_lock(&gl->gl_spin);
543 set_bit(GLF_LOCK, &gl->gl_flags);
544 spin_unlock(&gl->gl_spin);
546 if (gh->gh_state == LM_ST_UNLOCKED ||
547 gl->gl_state != LM_ST_EXCLUSIVE)
548 glops->go_drop_th(gl);
550 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
552 spin_lock(&gl->gl_spin);
559 * rq_greedy - process a queued request to drop greedy status
560 * @gh: the glock holder
562 * Returns: 1 if the queue is blocked
565 static int rq_greedy(struct gfs2_holder *gh)
567 struct gfs2_glock *gl = gh->gh_gl;
569 list_del_init(&gh->gh_list);
570 /* gh->gh_error never examined. */
571 clear_bit(GLF_GREEDY, &gl->gl_flags);
572 spin_unlock(&gl->gl_spin);
574 gfs2_holder_uninit(gh);
575 kfree(container_of(gh, struct greedy, gr_gh));
577 spin_lock(&gl->gl_spin);
583 * run_queue - process holder structures on a glock
587 static void run_queue(struct gfs2_glock *gl)
589 struct gfs2_holder *gh;
593 if (test_bit(GLF_LOCK, &gl->gl_flags))
596 if (!list_empty(&gl->gl_waiters1)) {
597 gh = list_entry(gl->gl_waiters1.next,
598 struct gfs2_holder, gh_list);
600 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
601 blocked = rq_mutex(gh);
603 gfs2_assert_warn(gl->gl_sbd, 0);
605 } else if (!list_empty(&gl->gl_waiters2) &&
606 !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) {
607 gh = list_entry(gl->gl_waiters2.next,
608 struct gfs2_holder, gh_list);
610 if (test_bit(HIF_DEMOTE, &gh->gh_iflags))
611 blocked = rq_demote(gh);
612 else if (test_bit(HIF_GREEDY, &gh->gh_iflags))
613 blocked = rq_greedy(gh);
615 gfs2_assert_warn(gl->gl_sbd, 0);
617 } else if (!list_empty(&gl->gl_waiters3)) {
618 gh = list_entry(gl->gl_waiters3.next,
619 struct gfs2_holder, gh_list);
621 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
622 blocked = rq_promote(gh);
624 gfs2_assert_warn(gl->gl_sbd, 0);
635 * gfs2_glmutex_lock - acquire a local lock on a glock
638 * Gives caller exclusive access to manipulate a glock structure.
641 static void gfs2_glmutex_lock(struct gfs2_glock *gl)
643 struct gfs2_holder gh;
645 gfs2_holder_init(gl, 0, 0, &gh);
646 set_bit(HIF_MUTEX, &gh.gh_iflags);
648 spin_lock(&gl->gl_spin);
649 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
650 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
652 gl->gl_owner = current;
653 gl->gl_ip = (unsigned long)__builtin_return_address(0);
654 complete(&gh.gh_wait);
656 spin_unlock(&gl->gl_spin);
658 wait_for_completion(&gh.gh_wait);
659 gfs2_holder_uninit(&gh);
663 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
666 * Returns: 1 if the glock is acquired
669 static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
673 spin_lock(&gl->gl_spin);
674 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
677 gl->gl_owner = current;
678 gl->gl_ip = (unsigned long)__builtin_return_address(0);
680 spin_unlock(&gl->gl_spin);
686 * gfs2_glmutex_unlock - release a local lock on a glock
691 static void gfs2_glmutex_unlock(struct gfs2_glock *gl)
693 spin_lock(&gl->gl_spin);
694 clear_bit(GLF_LOCK, &gl->gl_flags);
698 BUG_ON(!spin_is_locked(&gl->gl_spin));
699 spin_unlock(&gl->gl_spin);
703 * handle_callback - add a demote request to a lock's queue
705 * @state: the state the caller wants us to change to
707 * Note: This may fail sliently if we are out of memory.
710 static void handle_callback(struct gfs2_glock *gl, unsigned int state)
712 struct gfs2_holder *gh, *new_gh = NULL;
715 spin_lock(&gl->gl_spin);
717 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
718 if (test_bit(HIF_DEMOTE, &gh->gh_iflags) &&
719 gl->gl_req_gh != gh) {
720 if (gh->gh_state != state)
721 gh->gh_state = LM_ST_UNLOCKED;
727 list_add_tail(&new_gh->gh_list, &gl->gl_waiters2);
730 spin_unlock(&gl->gl_spin);
732 new_gh = gfs2_holder_get(gl, state, LM_FLAG_TRY, GFP_KERNEL);
735 set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
736 set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
742 spin_unlock(&gl->gl_spin);
745 gfs2_holder_put(new_gh);
748 void gfs2_glock_inode_squish(struct inode *inode)
750 struct gfs2_holder gh;
751 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
752 gfs2_holder_init(gl, LM_ST_UNLOCKED, 0, &gh);
753 set_bit(HIF_DEMOTE, &gh.gh_iflags);
754 spin_lock(&gl->gl_spin);
755 gfs2_assert(inode->i_sb->s_fs_info, list_empty(&gl->gl_holders));
756 list_add_tail(&gh.gh_list, &gl->gl_waiters2);
758 spin_unlock(&gl->gl_spin);
759 wait_for_completion(&gh.gh_wait);
760 gfs2_holder_uninit(&gh);
764 * state_change - record that the glock is now in a different state
766 * @new_state the new state
770 static void state_change(struct gfs2_glock *gl, unsigned int new_state)
774 held1 = (gl->gl_state != LM_ST_UNLOCKED);
775 held2 = (new_state != LM_ST_UNLOCKED);
777 if (held1 != held2) {
784 gl->gl_state = new_state;
788 * xmote_bh - Called after the lock module is done acquiring a lock
789 * @gl: The glock in question
790 * @ret: the int returned from the lock module
794 static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
796 struct gfs2_sbd *sdp = gl->gl_sbd;
797 const struct gfs2_glock_operations *glops = gl->gl_ops;
798 struct gfs2_holder *gh = gl->gl_req_gh;
799 int prev_state = gl->gl_state;
802 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
803 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
804 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
806 state_change(gl, ret & LM_OUT_ST_MASK);
808 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
810 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
811 } else if (gl->gl_state == LM_ST_DEFERRED) {
812 /* We might not want to do this here.
813 Look at moving to the inode glops. */
815 glops->go_inval(gl, DIO_DATA);
818 /* Deal with each possible exit condition */
821 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);
827 } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
828 spin_lock(&gl->gl_spin);
829 list_del_init(&gh->gh_list);
830 if (gl->gl_state == gh->gh_state ||
831 gl->gl_state == LM_ST_UNLOCKED)
834 if (gfs2_assert_warn(sdp, gh->gh_flags &
835 (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
836 fs_warn(sdp, "ret = 0x%.8X\n", ret);
837 gh->gh_error = GLR_TRYFAILED;
839 spin_unlock(&gl->gl_spin);
841 if (ret & LM_OUT_CANCELED)
842 handle_callback(gl, LM_ST_UNLOCKED);
844 } else if (ret & LM_OUT_CANCELED) {
845 spin_lock(&gl->gl_spin);
846 list_del_init(&gh->gh_list);
847 gh->gh_error = GLR_CANCELED;
848 spin_unlock(&gl->gl_spin);
850 } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
851 spin_lock(&gl->gl_spin);
852 list_move_tail(&gh->gh_list, &gl->gl_holders);
854 set_bit(HIF_HOLDER, &gh->gh_iflags);
855 spin_unlock(&gl->gl_spin);
857 set_bit(HIF_FIRST, &gh->gh_iflags);
861 } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
862 spin_lock(&gl->gl_spin);
863 list_del_init(&gh->gh_list);
864 gh->gh_error = GLR_TRYFAILED;
865 spin_unlock(&gl->gl_spin);
868 if (gfs2_assert_withdraw(sdp, 0) == -1)
869 fs_err(sdp, "ret = 0x%.8X\n", ret);
872 if (glops->go_xmote_bh)
873 glops->go_xmote_bh(gl);
876 spin_lock(&gl->gl_spin);
877 gl->gl_req_gh = NULL;
878 gl->gl_req_bh = NULL;
879 clear_bit(GLF_LOCK, &gl->gl_flags);
881 spin_unlock(&gl->gl_spin);
887 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
890 complete(&gh->gh_wait);
895 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
896 * @gl: The glock in question
897 * @state: the requested state
898 * @flags: modifier flags to the lock call
902 void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags)
904 struct gfs2_sbd *sdp = gl->gl_sbd;
905 const struct gfs2_glock_operations *glops = gl->gl_ops;
906 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
907 LM_FLAG_NOEXP | LM_FLAG_ANY |
909 unsigned int lck_ret;
911 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
912 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
913 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
914 gfs2_assert_warn(sdp, state != gl->gl_state);
916 if (gl->gl_state == LM_ST_EXCLUSIVE && glops->go_sync)
917 glops->go_sync(gl, DIO_METADATA | DIO_DATA | DIO_RELEASE);
920 gl->gl_req_bh = xmote_bh;
922 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state, lck_flags);
924 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
927 if (lck_ret & LM_OUT_ASYNC)
928 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
930 xmote_bh(gl, lck_ret);
934 * drop_bh - Called after a lock module unlock completes
936 * @ret: the return status
938 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
939 * Doesn't drop the reference on the glock the top half took out
943 static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
945 struct gfs2_sbd *sdp = gl->gl_sbd;
946 const struct gfs2_glock_operations *glops = gl->gl_ops;
947 struct gfs2_holder *gh = gl->gl_req_gh;
949 clear_bit(GLF_PREFETCH, &gl->gl_flags);
951 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
952 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
953 gfs2_assert_warn(sdp, !ret);
955 state_change(gl, LM_ST_UNLOCKED);
958 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
961 spin_lock(&gl->gl_spin);
962 list_del_init(&gh->gh_list);
964 spin_unlock(&gl->gl_spin);
967 if (glops->go_drop_bh)
968 glops->go_drop_bh(gl);
970 spin_lock(&gl->gl_spin);
971 gl->gl_req_gh = NULL;
972 gl->gl_req_bh = NULL;
973 clear_bit(GLF_LOCK, &gl->gl_flags);
975 spin_unlock(&gl->gl_spin);
980 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
983 complete(&gh->gh_wait);
988 * gfs2_glock_drop_th - call into the lock module to unlock a lock
993 void gfs2_glock_drop_th(struct gfs2_glock *gl)
995 struct gfs2_sbd *sdp = gl->gl_sbd;
996 const struct gfs2_glock_operations *glops = gl->gl_ops;
999 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1000 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
1001 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
1003 if (gl->gl_state == LM_ST_EXCLUSIVE && glops->go_sync)
1004 glops->go_sync(gl, DIO_METADATA | DIO_DATA | DIO_RELEASE);
1006 gfs2_glock_hold(gl);
1007 gl->gl_req_bh = drop_bh;
1009 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
1011 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
1017 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
1021 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1022 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1024 * Don't cancel GL_NOCANCEL requests.
1027 static void do_cancels(struct gfs2_holder *gh)
1029 struct gfs2_glock *gl = gh->gh_gl;
1031 spin_lock(&gl->gl_spin);
1033 while (gl->gl_req_gh != gh &&
1034 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1035 !list_empty(&gh->gh_list)) {
1036 if (gl->gl_req_bh && !(gl->gl_req_gh &&
1037 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
1038 spin_unlock(&gl->gl_spin);
1039 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
1041 spin_lock(&gl->gl_spin);
1043 spin_unlock(&gl->gl_spin);
1045 spin_lock(&gl->gl_spin);
1049 spin_unlock(&gl->gl_spin);
1053 * glock_wait_internal - wait on a glock acquisition
1054 * @gh: the glock holder
1056 * Returns: 0 on success
1059 static int glock_wait_internal(struct gfs2_holder *gh)
1061 struct gfs2_glock *gl = gh->gh_gl;
1062 struct gfs2_sbd *sdp = gl->gl_sbd;
1063 const struct gfs2_glock_operations *glops = gl->gl_ops;
1065 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1068 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1069 spin_lock(&gl->gl_spin);
1070 if (gl->gl_req_gh != gh &&
1071 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1072 !list_empty(&gh->gh_list)) {
1073 list_del_init(&gh->gh_list);
1074 gh->gh_error = GLR_TRYFAILED;
1076 spin_unlock(&gl->gl_spin);
1077 return gh->gh_error;
1079 spin_unlock(&gl->gl_spin);
1082 if (gh->gh_flags & LM_FLAG_PRIORITY)
1085 wait_for_completion(&gh->gh_wait);
1088 return gh->gh_error;
1090 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
1091 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state,
1095 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1096 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1098 if (glops->go_lock) {
1099 gh->gh_error = glops->go_lock(gh);
1101 spin_lock(&gl->gl_spin);
1102 list_del_init(&gh->gh_list);
1103 spin_unlock(&gl->gl_spin);
1107 spin_lock(&gl->gl_spin);
1108 gl->gl_req_gh = NULL;
1109 gl->gl_req_bh = NULL;
1110 clear_bit(GLF_LOCK, &gl->gl_flags);
1112 spin_unlock(&gl->gl_spin);
1115 return gh->gh_error;
1118 static inline struct gfs2_holder *
1119 find_holder_by_owner(struct list_head *head, struct task_struct *owner)
1121 struct gfs2_holder *gh;
1123 list_for_each_entry(gh, head, gh_list) {
1124 if (gh->gh_owner == owner)
1132 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1133 * @gh: the holder structure to add
1137 static void add_to_queue(struct gfs2_holder *gh)
1139 struct gfs2_glock *gl = gh->gh_gl;
1140 struct gfs2_holder *existing;
1142 BUG_ON(!gh->gh_owner);
1144 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
1146 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1147 printk(KERN_INFO "pid : %d\n", existing->gh_owner->pid);
1148 printk(KERN_INFO "lock type : %d lock state : %d\n",
1149 existing->gh_gl->gl_name.ln_type, existing->gh_gl->gl_state);
1150 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1151 printk(KERN_INFO "pid : %d\n", gh->gh_owner->pid);
1152 printk(KERN_INFO "lock type : %d lock state : %d\n",
1153 gl->gl_name.ln_type, gl->gl_state);
1157 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
1159 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1160 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1164 if (gh->gh_flags & LM_FLAG_PRIORITY)
1165 list_add(&gh->gh_list, &gl->gl_waiters3);
1167 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1171 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1172 * @gh: the holder structure
1174 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1176 * Returns: 0, GLR_TRYFAILED, or errno on failure
1179 int gfs2_glock_nq(struct gfs2_holder *gh)
1181 struct gfs2_glock *gl = gh->gh_gl;
1182 struct gfs2_sbd *sdp = gl->gl_sbd;
1186 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1187 set_bit(HIF_ABORTED, &gh->gh_iflags);
1191 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1193 spin_lock(&gl->gl_spin);
1196 spin_unlock(&gl->gl_spin);
1198 if (!(gh->gh_flags & GL_ASYNC)) {
1199 error = glock_wait_internal(gh);
1200 if (error == GLR_CANCELED) {
1206 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1208 if (error == GLR_TRYFAILED && (gh->gh_flags & GL_DUMP))
1215 * gfs2_glock_poll - poll to see if an async request has been completed
1218 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1221 int gfs2_glock_poll(struct gfs2_holder *gh)
1223 struct gfs2_glock *gl = gh->gh_gl;
1226 spin_lock(&gl->gl_spin);
1228 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1230 else if (list_empty(&gh->gh_list)) {
1231 if (gh->gh_error == GLR_CANCELED) {
1232 spin_unlock(&gl->gl_spin);
1234 if (gfs2_glock_nq(gh))
1241 spin_unlock(&gl->gl_spin);
1247 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1248 * @gh: the holder structure
1250 * Returns: 0, GLR_TRYFAILED, or errno on failure
1253 int gfs2_glock_wait(struct gfs2_holder *gh)
1257 error = glock_wait_internal(gh);
1258 if (error == GLR_CANCELED) {
1260 gh->gh_flags &= ~GL_ASYNC;
1261 error = gfs2_glock_nq(gh);
1268 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1269 * @gh: the glock holder
1273 void gfs2_glock_dq(struct gfs2_holder *gh)
1275 struct gfs2_glock *gl = gh->gh_gl;
1276 const struct gfs2_glock_operations *glops = gl->gl_ops;
1278 if (gh->gh_flags & GL_NOCACHE)
1279 handle_callback(gl, LM_ST_UNLOCKED);
1281 gfs2_glmutex_lock(gl);
1283 spin_lock(&gl->gl_spin);
1284 list_del_init(&gh->gh_list);
1286 if (list_empty(&gl->gl_holders)) {
1287 spin_unlock(&gl->gl_spin);
1289 if (glops->go_unlock)
1290 glops->go_unlock(gh);
1292 gl->gl_stamp = jiffies;
1294 spin_lock(&gl->gl_spin);
1297 clear_bit(GLF_LOCK, &gl->gl_flags);
1299 spin_unlock(&gl->gl_spin);
1303 * gfs2_glock_prefetch - Try to prefetch a glock
1305 * @state: the state to prefetch in
1306 * @flags: flags passed to go_xmote_th()
1310 static void gfs2_glock_prefetch(struct gfs2_glock *gl, unsigned int state,
1313 const struct gfs2_glock_operations *glops = gl->gl_ops;
1315 spin_lock(&gl->gl_spin);
1317 if (test_bit(GLF_LOCK, &gl->gl_flags) || !list_empty(&gl->gl_holders) ||
1318 !list_empty(&gl->gl_waiters1) || !list_empty(&gl->gl_waiters2) ||
1319 !list_empty(&gl->gl_waiters3) ||
1320 relaxed_state_ok(gl->gl_state, state, flags)) {
1321 spin_unlock(&gl->gl_spin);
1325 set_bit(GLF_PREFETCH, &gl->gl_flags);
1326 set_bit(GLF_LOCK, &gl->gl_flags);
1327 spin_unlock(&gl->gl_spin);
1329 glops->go_xmote_th(gl, state, flags);
1332 static void greedy_work(void *data)
1334 struct greedy *gr = data;
1335 struct gfs2_holder *gh = &gr->gr_gh;
1336 struct gfs2_glock *gl = gh->gh_gl;
1337 const struct gfs2_glock_operations *glops = gl->gl_ops;
1339 clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1341 if (glops->go_greedy)
1342 glops->go_greedy(gl);
1344 spin_lock(&gl->gl_spin);
1346 if (list_empty(&gl->gl_waiters2)) {
1347 clear_bit(GLF_GREEDY, &gl->gl_flags);
1348 spin_unlock(&gl->gl_spin);
1349 gfs2_holder_uninit(gh);
1352 gfs2_glock_hold(gl);
1353 list_add_tail(&gh->gh_list, &gl->gl_waiters2);
1355 spin_unlock(&gl->gl_spin);
1361 * gfs2_glock_be_greedy -
1365 * Returns: 0 if go_greedy will be called, 1 otherwise
1368 int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time)
1371 struct gfs2_holder *gh;
1373 if (!time || gl->gl_sbd->sd_args.ar_localcaching ||
1374 test_and_set_bit(GLF_GREEDY, &gl->gl_flags))
1377 gr = kmalloc(sizeof(struct greedy), GFP_KERNEL);
1379 clear_bit(GLF_GREEDY, &gl->gl_flags);
1384 gfs2_holder_init(gl, 0, 0, gh);
1385 set_bit(HIF_GREEDY, &gh->gh_iflags);
1386 INIT_WORK(&gr->gr_work, greedy_work, gr);
1388 set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1389 schedule_delayed_work(&gr->gr_work, time);
1395 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1396 * @gh: the holder structure
1400 void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1403 gfs2_holder_uninit(gh);
1407 * gfs2_glock_nq_num - acquire a glock based on lock number
1408 * @sdp: the filesystem
1409 * @number: the lock number
1410 * @glops: the glock operations for the type of glock
1411 * @state: the state to acquire the glock in
1412 * @flags: modifier flags for the aquisition
1413 * @gh: the struct gfs2_holder
1418 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, uint64_t number,
1419 const struct gfs2_glock_operations *glops,
1420 unsigned int state, int flags, struct gfs2_holder *gh)
1422 struct gfs2_glock *gl;
1425 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1427 error = gfs2_glock_nq_init(gl, state, flags, gh);
1435 * glock_compare - Compare two struct gfs2_glock structures for sorting
1436 * @arg_a: the first structure
1437 * @arg_b: the second structure
1441 static int glock_compare(const void *arg_a, const void *arg_b)
1443 struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
1444 struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
1445 struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1446 struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1449 if (a->ln_number > b->ln_number)
1451 else if (a->ln_number < b->ln_number)
1454 if (gh_a->gh_state == LM_ST_SHARED &&
1455 gh_b->gh_state == LM_ST_EXCLUSIVE)
1457 else if (!(gh_a->gh_flags & GL_LOCAL_EXCL) &&
1458 (gh_b->gh_flags & GL_LOCAL_EXCL))
1466 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1467 * @num_gh: the number of structures
1468 * @ghs: an array of struct gfs2_holder structures
1470 * Returns: 0 on success (all glocks acquired),
1471 * errno on failure (no glocks acquired)
1474 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1475 struct gfs2_holder **p)
1480 for (x = 0; x < num_gh; x++)
1483 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1485 for (x = 0; x < num_gh; x++) {
1486 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1488 error = gfs2_glock_nq(p[x]);
1491 gfs2_glock_dq(p[x]);
1500 * gfs2_glock_nq_m - acquire multiple glocks
1501 * @num_gh: the number of structures
1502 * @ghs: an array of struct gfs2_holder structures
1504 * Figure out how big an impact this function has. Either:
1505 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1506 * 2) Forget async stuff and just call nq_m_sync()
1507 * 3) Leave it like it is
1509 * Returns: 0 on success (all glocks acquired),
1510 * errno on failure (no glocks acquired)
1513 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1517 int borked = 0, serious = 0;
1524 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1525 return gfs2_glock_nq(ghs);
1528 e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1532 for (x = 0; x < num_gh; x++) {
1533 ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
1534 error = gfs2_glock_nq(&ghs[x]);
1543 for (x = 0; x < num_gh; x++) {
1544 error = e[x] = glock_wait_internal(&ghs[x]);
1547 if (error != GLR_TRYFAILED && error != GLR_CANCELED)
1557 for (x = 0; x < num_gh; x++)
1559 gfs2_glock_dq(&ghs[x]);
1564 for (x = 0; x < num_gh; x++)
1565 gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
1567 error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
1576 * gfs2_glock_dq_m - release multiple glocks
1577 * @num_gh: the number of structures
1578 * @ghs: an array of struct gfs2_holder structures
1582 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1586 for (x = 0; x < num_gh; x++)
1587 gfs2_glock_dq(&ghs[x]);
1591 * gfs2_glock_dq_uninit_m - release multiple glocks
1592 * @num_gh: the number of structures
1593 * @ghs: an array of struct gfs2_holder structures
1597 void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1601 for (x = 0; x < num_gh; x++)
1602 gfs2_glock_dq_uninit(&ghs[x]);
1606 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1607 * @sdp: the filesystem
1608 * @number: the lock number
1609 * @glops: the glock operations for the type of glock
1610 * @state: the state to acquire the glock in
1611 * @flags: modifier flags for the aquisition
1616 void gfs2_glock_prefetch_num(struct gfs2_sbd *sdp, uint64_t number,
1617 const struct gfs2_glock_operations *glops,
1618 unsigned int state, int flags)
1620 struct gfs2_glock *gl;
1623 if (atomic_read(&sdp->sd_reclaim_count) <
1624 gfs2_tune_get(sdp, gt_reclaim_limit)) {
1625 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1627 gfs2_glock_prefetch(gl, state, flags);
1634 * gfs2_lvb_hold - attach a LVB from a glock
1635 * @gl: The glock in question
1639 int gfs2_lvb_hold(struct gfs2_glock *gl)
1643 gfs2_glmutex_lock(gl);
1645 if (!atomic_read(&gl->gl_lvb_count)) {
1646 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1648 gfs2_glmutex_unlock(gl);
1651 gfs2_glock_hold(gl);
1653 atomic_inc(&gl->gl_lvb_count);
1655 gfs2_glmutex_unlock(gl);
1661 * gfs2_lvb_unhold - detach a LVB from a glock
1662 * @gl: The glock in question
1666 void gfs2_lvb_unhold(struct gfs2_glock *gl)
1668 gfs2_glock_hold(gl);
1669 gfs2_glmutex_lock(gl);
1671 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1672 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1673 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1678 gfs2_glmutex_unlock(gl);
1682 static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1685 struct gfs2_glock *gl;
1687 gl = gfs2_glock_find(sdp, name);
1691 if (gl->gl_ops->go_callback)
1692 gl->gl_ops->go_callback(gl, state);
1693 handle_callback(gl, state);
1695 spin_lock(&gl->gl_spin);
1697 spin_unlock(&gl->gl_spin);
1703 * gfs2_glock_cb - Callback used by locking module
1704 * @fsdata: Pointer to the superblock
1705 * @type: Type of callback
1706 * @data: Type dependent data pointer
1708 * Called by the locking module when it wants to tell us something.
1709 * Either we need to drop a lock, one of our ASYNC requests completed, or
1710 * a journal from another client needs to be recovered.
1713 void gfs2_glock_cb(lm_fsdata_t *fsdata, unsigned int type, void *data)
1715 struct gfs2_sbd *sdp = (struct gfs2_sbd *)fsdata;
1719 blocking_cb(sdp, data, LM_ST_UNLOCKED);
1723 blocking_cb(sdp, data, LM_ST_DEFERRED);
1727 blocking_cb(sdp, data, LM_ST_SHARED);
1731 struct lm_async_cb *async = data;
1732 struct gfs2_glock *gl;
1734 gl = gfs2_glock_find(sdp, &async->lc_name);
1735 if (gfs2_assert_warn(sdp, gl))
1737 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1738 gl->gl_req_bh(gl, async->lc_ret);
1743 case LM_CB_NEED_RECOVERY:
1744 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1745 if (sdp->sd_recoverd_process)
1746 wake_up_process(sdp->sd_recoverd_process);
1749 case LM_CB_DROPLOCKS:
1750 gfs2_gl_hash_clear(sdp, NO_WAIT);
1751 gfs2_quota_scan(sdp);
1755 gfs2_assert_warn(sdp, 0);
1761 * gfs2_iopen_go_callback - Try to kick the inode/vnode associated with an
1762 * iopen glock from memory
1763 * @io_gl: the iopen glock
1764 * @state: the state into which the glock should be put
1768 void gfs2_iopen_go_callback(struct gfs2_glock *io_gl, unsigned int state)
1771 if (state != LM_ST_UNLOCKED)
1773 /* FIXME: remove this? */
1777 * demote_ok - Check to see if it's ok to unlock a glock
1780 * Returns: 1 if it's ok
1783 static int demote_ok(struct gfs2_glock *gl)
1785 struct gfs2_sbd *sdp = gl->gl_sbd;
1786 const struct gfs2_glock_operations *glops = gl->gl_ops;
1789 if (test_bit(GLF_STICKY, &gl->gl_flags))
1791 else if (test_bit(GLF_PREFETCH, &gl->gl_flags))
1792 demote = time_after_eq(jiffies, gl->gl_stamp +
1793 gfs2_tune_get(sdp, gt_prefetch_secs) * HZ);
1794 else if (glops->go_demote_ok)
1795 demote = glops->go_demote_ok(gl);
1801 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1806 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1808 struct gfs2_sbd *sdp = gl->gl_sbd;
1810 spin_lock(&sdp->sd_reclaim_lock);
1811 if (list_empty(&gl->gl_reclaim)) {
1812 gfs2_glock_hold(gl);
1813 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1814 atomic_inc(&sdp->sd_reclaim_count);
1816 spin_unlock(&sdp->sd_reclaim_lock);
1818 wake_up(&sdp->sd_reclaim_wq);
1822 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1823 * @sdp: the filesystem
1825 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1826 * different glock and we notice that there are a lot of glocks in the
1831 void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1833 struct gfs2_glock *gl;
1835 spin_lock(&sdp->sd_reclaim_lock);
1836 if (list_empty(&sdp->sd_reclaim_list)) {
1837 spin_unlock(&sdp->sd_reclaim_lock);
1840 gl = list_entry(sdp->sd_reclaim_list.next,
1841 struct gfs2_glock, gl_reclaim);
1842 list_del_init(&gl->gl_reclaim);
1843 spin_unlock(&sdp->sd_reclaim_lock);
1845 atomic_dec(&sdp->sd_reclaim_count);
1846 atomic_inc(&sdp->sd_reclaimed);
1848 if (gfs2_glmutex_trylock(gl)) {
1849 if (queue_empty(gl, &gl->gl_holders) &&
1850 gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
1851 handle_callback(gl, LM_ST_UNLOCKED);
1852 gfs2_glmutex_unlock(gl);
1859 * examine_bucket - Call a function for glock in a hash bucket
1860 * @examiner: the function
1861 * @sdp: the filesystem
1862 * @bucket: the bucket
1864 * Returns: 1 if the bucket has entries
1867 static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
1868 struct gfs2_gl_hash_bucket *bucket)
1870 struct glock_plug plug;
1871 struct list_head *tmp;
1872 struct gfs2_glock *gl;
1875 /* Add "plug" to end of bucket list, work back up list from there */
1876 memset(&plug.gl_flags, 0, sizeof(unsigned long));
1877 set_bit(GLF_PLUG, &plug.gl_flags);
1879 write_lock(&bucket->hb_lock);
1880 list_add(&plug.gl_list, &bucket->hb_list);
1881 write_unlock(&bucket->hb_lock);
1884 write_lock(&bucket->hb_lock);
1887 tmp = plug.gl_list.next;
1889 if (tmp == &bucket->hb_list) {
1890 list_del(&plug.gl_list);
1891 entries = !list_empty(&bucket->hb_list);
1892 write_unlock(&bucket->hb_lock);
1895 gl = list_entry(tmp, struct gfs2_glock, gl_list);
1897 /* Move plug up list */
1898 list_move(&plug.gl_list, &gl->gl_list);
1900 if (test_bit(GLF_PLUG, &gl->gl_flags))
1903 /* examiner() must glock_put() */
1904 gfs2_glock_hold(gl);
1909 write_unlock(&bucket->hb_lock);
1916 * scan_glock - look at a glock and see if we can reclaim it
1917 * @gl: the glock to look at
1921 static void scan_glock(struct gfs2_glock *gl)
1923 if (gl->gl_ops == &gfs2_inode_glops)
1926 if (gfs2_glmutex_trylock(gl)) {
1927 if (queue_empty(gl, &gl->gl_holders) &&
1928 gl->gl_state != LM_ST_UNLOCKED &&
1931 gfs2_glmutex_unlock(gl);
1938 gfs2_glmutex_unlock(gl);
1939 gfs2_glock_schedule_for_reclaim(gl);
1944 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
1945 * @sdp: the filesystem
1949 void gfs2_scand_internal(struct gfs2_sbd *sdp)
1953 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
1954 examine_bucket(scan_glock, sdp, &sdp->sd_gl_hash[x]);
1960 * clear_glock - look at a glock and see if we can free it from glock cache
1961 * @gl: the glock to look at
1965 static void clear_glock(struct gfs2_glock *gl)
1967 struct gfs2_sbd *sdp = gl->gl_sbd;
1970 spin_lock(&sdp->sd_reclaim_lock);
1971 if (!list_empty(&gl->gl_reclaim)) {
1972 list_del_init(&gl->gl_reclaim);
1973 atomic_dec(&sdp->sd_reclaim_count);
1974 spin_unlock(&sdp->sd_reclaim_lock);
1975 released = gfs2_glock_put(gl);
1976 gfs2_assert(sdp, !released);
1978 spin_unlock(&sdp->sd_reclaim_lock);
1981 if (gfs2_glmutex_trylock(gl)) {
1982 if (queue_empty(gl, &gl->gl_holders) &&
1983 gl->gl_state != LM_ST_UNLOCKED)
1984 handle_callback(gl, LM_ST_UNLOCKED);
1986 gfs2_glmutex_unlock(gl);
1993 * gfs2_gl_hash_clear - Empty out the glock hash table
1994 * @sdp: the filesystem
1995 * @wait: wait until it's all gone
1997 * Called when unmounting the filesystem, or when inter-node lock manager
1998 * requests DROPLOCKS because it is running out of capacity.
2001 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
2012 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
2013 if (examine_bucket(clear_glock, sdp, &sdp->sd_gl_hash[x]))
2019 if (time_after_eq(jiffies,
2020 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
2021 fs_warn(sdp, "Unmount seems to be stalled. "
2022 "Dumping lock state...\n");
2023 gfs2_dump_lockstate(sdp);
2027 invalidate_inodes(sdp->sd_vfs);
2033 * Diagnostic routines to help debug distributed deadlock
2037 * dump_holder - print information about a glock holder
2038 * @str: a string naming the type of holder
2039 * @gh: the glock holder
2041 * Returns: 0 on success, -ENOBUFS when we run out of space
2044 static int dump_holder(char *str, struct gfs2_holder *gh)
2047 int error = -ENOBUFS;
2049 printk(KERN_INFO " %s\n", str);
2050 printk(KERN_INFO " owner = %ld\n",
2051 (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
2052 printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
2053 printk(KERN_INFO " gh_flags =");
2054 for (x = 0; x < 32; x++)
2055 if (gh->gh_flags & (1 << x))
2058 printk(KERN_INFO " error = %d\n", gh->gh_error);
2059 printk(KERN_INFO " gh_iflags =");
2060 for (x = 0; x < 32; x++)
2061 if (test_bit(x, &gh->gh_iflags))
2064 print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip);
2072 * dump_inode - print information about an inode
2075 * Returns: 0 on success, -ENOBUFS when we run out of space
2078 static int dump_inode(struct gfs2_inode *ip)
2081 int error = -ENOBUFS;
2083 printk(KERN_INFO " Inode:\n");
2084 printk(KERN_INFO " num = %llu %llu\n",
2085 (unsigned long long)ip->i_num.no_formal_ino,
2086 (unsigned long long)ip->i_num.no_addr);
2087 printk(KERN_INFO " type = %u\n", IF2DT(ip->i_di.di_mode));
2088 printk(KERN_INFO " i_flags =");
2089 for (x = 0; x < 32; x++)
2090 if (test_bit(x, &ip->i_flags))
2100 * dump_glock - print information about a glock
2102 * @count: where we are in the buffer
2104 * Returns: 0 on success, -ENOBUFS when we run out of space
2107 static int dump_glock(struct gfs2_glock *gl)
2109 struct gfs2_holder *gh;
2111 int error = -ENOBUFS;
2113 spin_lock(&gl->gl_spin);
2115 printk(KERN_INFO "Glock 0x%p (%u, %llu)\n",
2117 gl->gl_name.ln_type,
2118 (unsigned long long)gl->gl_name.ln_number);
2119 printk(KERN_INFO " gl_flags =");
2120 for (x = 0; x < 32; x++)
2121 if (test_bit(x, &gl->gl_flags))
2124 printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref.refcount));
2125 printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
2126 printk(KERN_INFO " gl_owner = %s\n", gl->gl_owner->comm);
2127 print_symbol(KERN_INFO " gl_ip = %s\n", gl->gl_ip);
2128 printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
2129 printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
2130 printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
2131 printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
2132 printk(KERN_INFO " le = %s\n",
2133 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
2134 printk(KERN_INFO " reclaim = %s\n",
2135 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
2137 printk(KERN_INFO " aspace = 0x%p nrpages = %lu\n",
2139 gl->gl_aspace->i_mapping->nrpages);
2141 printk(KERN_INFO " aspace = no\n");
2142 printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
2143 if (gl->gl_req_gh) {
2144 error = dump_holder("Request", gl->gl_req_gh);
2148 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
2149 error = dump_holder("Holder", gh);
2153 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
2154 error = dump_holder("Waiter1", gh);
2158 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
2159 error = dump_holder("Waiter2", gh);
2163 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
2164 error = dump_holder("Waiter3", gh);
2168 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
2169 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
2170 list_empty(&gl->gl_holders)) {
2171 error = dump_inode(gl->gl_object);
2176 printk(KERN_INFO " Inode: busy\n");
2183 spin_unlock(&gl->gl_spin);
2188 * gfs2_dump_lockstate - print out the current lockstate
2189 * @sdp: the filesystem
2190 * @ub: the buffer to copy the information into
2192 * If @ub is NULL, dump the lockstate to the console.
2196 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
2198 struct gfs2_gl_hash_bucket *bucket;
2199 struct gfs2_glock *gl;
2203 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2204 bucket = &sdp->sd_gl_hash[x];
2206 read_lock(&bucket->hb_lock);
2208 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
2209 if (test_bit(GLF_PLUG, &gl->gl_flags))
2212 error = dump_glock(gl);
2217 read_unlock(&bucket->hb_lock);