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 gl->gl_aspace = gfs2_aspace_get(sdp);
316 if (!gl->gl_aspace) {
322 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
326 write_lock(&bucket->hb_lock);
327 tmp = search_bucket(bucket, &name);
329 write_unlock(&bucket->hb_lock);
333 list_add_tail(&gl->gl_list, &bucket->hb_list);
334 write_unlock(&bucket->hb_lock);
343 gfs2_aspace_put(gl->gl_aspace);
346 kmem_cache_free(gfs2_glock_cachep, gl);
352 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
354 * @state: the state we're requesting
355 * @flags: the modifier flags
356 * @gh: the holder structure
360 void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
361 struct gfs2_holder *gh)
363 INIT_LIST_HEAD(&gh->gh_list);
365 gh->gh_ip = (unsigned long)__builtin_return_address(0);
366 gh->gh_owner = current;
367 gh->gh_state = state;
368 gh->gh_flags = flags;
371 init_completion(&gh->gh_wait);
373 if (gh->gh_state == LM_ST_EXCLUSIVE)
374 gh->gh_flags |= GL_LOCAL_EXCL;
380 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
381 * @state: the state we're requesting
382 * @flags: the modifier flags
383 * @gh: the holder structure
385 * Don't mess with the glock.
389 void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
391 gh->gh_state = state;
392 gh->gh_flags = flags;
393 if (gh->gh_state == LM_ST_EXCLUSIVE)
394 gh->gh_flags |= GL_LOCAL_EXCL;
396 gh->gh_iflags &= 1 << HIF_ALLOCED;
397 gh->gh_ip = (unsigned long)__builtin_return_address(0);
401 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
402 * @gh: the holder structure
406 void gfs2_holder_uninit(struct gfs2_holder *gh)
408 gfs2_glock_put(gh->gh_gl);
414 * gfs2_holder_get - get a struct gfs2_holder structure
416 * @state: the state we're requesting
417 * @flags: the modifier flags
420 * Figure out how big an impact this function has. Either:
421 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
422 * 2) Leave it like it is
424 * Returns: the holder structure, NULL on ENOMEM
427 static struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl,
429 int flags, gfp_t gfp_flags)
431 struct gfs2_holder *gh;
433 gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags);
437 gfs2_holder_init(gl, state, flags, gh);
438 set_bit(HIF_ALLOCED, &gh->gh_iflags);
439 gh->gh_ip = (unsigned long)__builtin_return_address(0);
444 * gfs2_holder_put - get rid of a struct gfs2_holder structure
445 * @gh: the holder structure
449 static void gfs2_holder_put(struct gfs2_holder *gh)
451 gfs2_holder_uninit(gh);
456 * rq_mutex - process a mutex request in the queue
457 * @gh: the glock holder
459 * Returns: 1 if the queue is blocked
462 static int rq_mutex(struct gfs2_holder *gh)
464 struct gfs2_glock *gl = gh->gh_gl;
466 list_del_init(&gh->gh_list);
467 /* gh->gh_error never examined. */
468 set_bit(GLF_LOCK, &gl->gl_flags);
469 complete(&gh->gh_wait);
475 * rq_promote - process a promote request in the queue
476 * @gh: the glock holder
478 * Acquire a new inter-node lock, or change a lock state to more restrictive.
480 * Returns: 1 if the queue is blocked
483 static int rq_promote(struct gfs2_holder *gh)
485 struct gfs2_glock *gl = gh->gh_gl;
486 struct gfs2_sbd *sdp = gl->gl_sbd;
487 struct gfs2_glock_operations *glops = gl->gl_ops;
489 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
490 if (list_empty(&gl->gl_holders)) {
492 set_bit(GLF_LOCK, &gl->gl_flags);
493 spin_unlock(&gl->gl_spin);
495 if (atomic_read(&sdp->sd_reclaim_count) >
496 gfs2_tune_get(sdp, gt_reclaim_limit) &&
497 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
498 gfs2_reclaim_glock(sdp);
499 gfs2_reclaim_glock(sdp);
502 glops->go_xmote_th(gl, gh->gh_state,
505 spin_lock(&gl->gl_spin);
510 if (list_empty(&gl->gl_holders)) {
511 set_bit(HIF_FIRST, &gh->gh_iflags);
512 set_bit(GLF_LOCK, &gl->gl_flags);
514 struct gfs2_holder *next_gh;
515 if (gh->gh_flags & GL_LOCAL_EXCL)
517 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
519 if (next_gh->gh_flags & GL_LOCAL_EXCL)
523 list_move_tail(&gh->gh_list, &gl->gl_holders);
525 set_bit(HIF_HOLDER, &gh->gh_iflags);
527 complete(&gh->gh_wait);
533 * rq_demote - process a demote request in the queue
534 * @gh: the glock holder
536 * Returns: 1 if the queue is blocked
539 static int rq_demote(struct gfs2_holder *gh)
541 struct gfs2_glock *gl = gh->gh_gl;
542 struct gfs2_glock_operations *glops = gl->gl_ops;
544 if (!list_empty(&gl->gl_holders))
547 if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) {
548 list_del_init(&gh->gh_list);
550 spin_unlock(&gl->gl_spin);
551 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
554 complete(&gh->gh_wait);
555 spin_lock(&gl->gl_spin);
558 set_bit(GLF_LOCK, &gl->gl_flags);
559 spin_unlock(&gl->gl_spin);
561 if (gh->gh_state == LM_ST_UNLOCKED ||
562 gl->gl_state != LM_ST_EXCLUSIVE)
563 glops->go_drop_th(gl);
565 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
567 spin_lock(&gl->gl_spin);
574 * rq_greedy - process a queued request to drop greedy status
575 * @gh: the glock holder
577 * Returns: 1 if the queue is blocked
580 static int rq_greedy(struct gfs2_holder *gh)
582 struct gfs2_glock *gl = gh->gh_gl;
584 list_del_init(&gh->gh_list);
585 /* gh->gh_error never examined. */
586 clear_bit(GLF_GREEDY, &gl->gl_flags);
587 spin_unlock(&gl->gl_spin);
589 gfs2_holder_uninit(gh);
590 kfree(container_of(gh, struct greedy, gr_gh));
592 spin_lock(&gl->gl_spin);
598 * run_queue - process holder structures on a glock
602 static void run_queue(struct gfs2_glock *gl)
604 struct gfs2_holder *gh;
608 if (test_bit(GLF_LOCK, &gl->gl_flags))
611 if (!list_empty(&gl->gl_waiters1)) {
612 gh = list_entry(gl->gl_waiters1.next,
613 struct gfs2_holder, gh_list);
615 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
616 blocked = rq_mutex(gh);
618 gfs2_assert_warn(gl->gl_sbd, 0);
620 } else if (!list_empty(&gl->gl_waiters2) &&
621 !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) {
622 gh = list_entry(gl->gl_waiters2.next,
623 struct gfs2_holder, gh_list);
625 if (test_bit(HIF_DEMOTE, &gh->gh_iflags))
626 blocked = rq_demote(gh);
627 else if (test_bit(HIF_GREEDY, &gh->gh_iflags))
628 blocked = rq_greedy(gh);
630 gfs2_assert_warn(gl->gl_sbd, 0);
632 } else if (!list_empty(&gl->gl_waiters3)) {
633 gh = list_entry(gl->gl_waiters3.next,
634 struct gfs2_holder, gh_list);
636 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
637 blocked = rq_promote(gh);
639 gfs2_assert_warn(gl->gl_sbd, 0);
650 * gfs2_glmutex_lock - acquire a local lock on a glock
653 * Gives caller exclusive access to manipulate a glock structure.
656 static void gfs2_glmutex_lock(struct gfs2_glock *gl)
658 struct gfs2_holder gh;
660 gfs2_holder_init(gl, 0, 0, &gh);
661 set_bit(HIF_MUTEX, &gh.gh_iflags);
663 spin_lock(&gl->gl_spin);
664 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
665 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
667 gl->gl_owner = current;
668 gl->gl_ip = (unsigned long)__builtin_return_address(0);
669 complete(&gh.gh_wait);
671 spin_unlock(&gl->gl_spin);
673 wait_for_completion(&gh.gh_wait);
674 gfs2_holder_uninit(&gh);
678 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
681 * Returns: 1 if the glock is acquired
684 static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
688 spin_lock(&gl->gl_spin);
689 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
692 gl->gl_owner = current;
693 gl->gl_ip = (unsigned long)__builtin_return_address(0);
695 spin_unlock(&gl->gl_spin);
701 * gfs2_glmutex_unlock - release a local lock on a glock
706 static void gfs2_glmutex_unlock(struct gfs2_glock *gl)
708 spin_lock(&gl->gl_spin);
709 clear_bit(GLF_LOCK, &gl->gl_flags);
713 BUG_ON(!spin_is_locked(&gl->gl_spin));
714 spin_unlock(&gl->gl_spin);
718 * handle_callback - add a demote request to a lock's queue
720 * @state: the state the caller wants us to change to
722 * Note: This may fail sliently if we are out of memory.
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, GFP_KERNEL);
750 set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
751 set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
757 spin_unlock(&gl->gl_spin);
760 gfs2_holder_put(new_gh);
763 void gfs2_glock_inode_squish(struct inode *inode)
765 struct gfs2_holder gh;
766 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
767 gfs2_holder_init(gl, LM_ST_UNLOCKED, 0, &gh);
768 set_bit(HIF_DEMOTE, &gh.gh_iflags);
769 spin_lock(&gl->gl_spin);
770 gfs2_assert(inode->i_sb->s_fs_info, list_empty(&gl->gl_holders));
771 list_add_tail(&gh.gh_list, &gl->gl_waiters2);
773 spin_unlock(&gl->gl_spin);
774 gfs2_holder_uninit(&gh);
778 * state_change - record that the glock is now in a different state
780 * @new_state the new state
784 static void state_change(struct gfs2_glock *gl, unsigned int new_state)
788 held1 = (gl->gl_state != LM_ST_UNLOCKED);
789 held2 = (new_state != LM_ST_UNLOCKED);
791 if (held1 != held2) {
798 gl->gl_state = new_state;
802 * xmote_bh - Called after the lock module is done acquiring a lock
803 * @gl: The glock in question
804 * @ret: the int returned from the lock module
808 static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
810 struct gfs2_sbd *sdp = gl->gl_sbd;
811 struct gfs2_glock_operations *glops = gl->gl_ops;
812 struct gfs2_holder *gh = gl->gl_req_gh;
813 int prev_state = gl->gl_state;
816 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
817 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
818 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
820 state_change(gl, ret & LM_OUT_ST_MASK);
822 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
824 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
825 } else if (gl->gl_state == LM_ST_DEFERRED) {
826 /* We might not want to do this here.
827 Look at moving to the inode glops. */
829 glops->go_inval(gl, DIO_DATA);
832 /* Deal with each possible exit condition */
835 gl->gl_stamp = jiffies;
837 else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
838 spin_lock(&gl->gl_spin);
839 list_del_init(&gh->gh_list);
841 spin_unlock(&gl->gl_spin);
843 } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
844 spin_lock(&gl->gl_spin);
845 list_del_init(&gh->gh_list);
846 if (gl->gl_state == gh->gh_state ||
847 gl->gl_state == LM_ST_UNLOCKED)
850 if (gfs2_assert_warn(sdp, gh->gh_flags &
851 (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
852 fs_warn(sdp, "ret = 0x%.8X\n", ret);
853 gh->gh_error = GLR_TRYFAILED;
855 spin_unlock(&gl->gl_spin);
857 if (ret & LM_OUT_CANCELED)
858 handle_callback(gl, LM_ST_UNLOCKED); /* Lame */
860 } else if (ret & LM_OUT_CANCELED) {
861 spin_lock(&gl->gl_spin);
862 list_del_init(&gh->gh_list);
863 gh->gh_error = GLR_CANCELED;
864 spin_unlock(&gl->gl_spin);
866 } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
867 spin_lock(&gl->gl_spin);
868 list_move_tail(&gh->gh_list, &gl->gl_holders);
870 set_bit(HIF_HOLDER, &gh->gh_iflags);
871 spin_unlock(&gl->gl_spin);
873 set_bit(HIF_FIRST, &gh->gh_iflags);
877 } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
878 spin_lock(&gl->gl_spin);
879 list_del_init(&gh->gh_list);
880 gh->gh_error = GLR_TRYFAILED;
881 spin_unlock(&gl->gl_spin);
884 if (gfs2_assert_withdraw(sdp, 0) == -1)
885 fs_err(sdp, "ret = 0x%.8X\n", ret);
888 if (glops->go_xmote_bh)
889 glops->go_xmote_bh(gl);
892 spin_lock(&gl->gl_spin);
893 gl->gl_req_gh = NULL;
894 gl->gl_req_bh = NULL;
895 clear_bit(GLF_LOCK, &gl->gl_flags);
897 spin_unlock(&gl->gl_spin);
903 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
906 complete(&gh->gh_wait);
911 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
912 * @gl: The glock in question
913 * @state: the requested state
914 * @flags: modifier flags to the lock call
918 void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags)
920 struct gfs2_sbd *sdp = gl->gl_sbd;
921 struct gfs2_glock_operations *glops = gl->gl_ops;
922 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
923 LM_FLAG_NOEXP | LM_FLAG_ANY |
925 unsigned int lck_ret;
927 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
928 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
929 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
930 gfs2_assert_warn(sdp, state != gl->gl_state);
932 if (gl->gl_state == LM_ST_EXCLUSIVE) {
935 DIO_METADATA | DIO_DATA | DIO_RELEASE);
939 gl->gl_req_bh = xmote_bh;
941 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state,
944 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
947 if (lck_ret & LM_OUT_ASYNC)
948 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
950 xmote_bh(gl, lck_ret);
954 * drop_bh - Called after a lock module unlock completes
956 * @ret: the return status
958 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
959 * Doesn't drop the reference on the glock the top half took out
963 static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
965 struct gfs2_sbd *sdp = gl->gl_sbd;
966 struct gfs2_glock_operations *glops = gl->gl_ops;
967 struct gfs2_holder *gh = gl->gl_req_gh;
969 clear_bit(GLF_PREFETCH, &gl->gl_flags);
971 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
972 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
973 gfs2_assert_warn(sdp, !ret);
975 state_change(gl, LM_ST_UNLOCKED);
978 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
981 spin_lock(&gl->gl_spin);
982 list_del_init(&gh->gh_list);
984 spin_unlock(&gl->gl_spin);
987 if (glops->go_drop_bh)
988 glops->go_drop_bh(gl);
990 spin_lock(&gl->gl_spin);
991 gl->gl_req_gh = NULL;
992 gl->gl_req_bh = NULL;
993 clear_bit(GLF_LOCK, &gl->gl_flags);
995 spin_unlock(&gl->gl_spin);
1000 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
1001 gfs2_holder_put(gh);
1003 complete(&gh->gh_wait);
1008 * gfs2_glock_drop_th - call into the lock module to unlock a lock
1013 void gfs2_glock_drop_th(struct gfs2_glock *gl)
1015 struct gfs2_sbd *sdp = gl->gl_sbd;
1016 struct gfs2_glock_operations *glops = gl->gl_ops;
1019 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1020 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
1021 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
1023 if (gl->gl_state == LM_ST_EXCLUSIVE) {
1026 DIO_METADATA | DIO_DATA | DIO_RELEASE);
1029 gfs2_glock_hold(gl);
1030 gl->gl_req_bh = drop_bh;
1032 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
1034 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
1040 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
1044 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1045 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1047 * Don't cancel GL_NOCANCEL requests.
1050 static void do_cancels(struct gfs2_holder *gh)
1052 struct gfs2_glock *gl = gh->gh_gl;
1054 spin_lock(&gl->gl_spin);
1056 while (gl->gl_req_gh != gh &&
1057 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1058 !list_empty(&gh->gh_list)) {
1059 if (gl->gl_req_bh &&
1061 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
1062 spin_unlock(&gl->gl_spin);
1063 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
1065 spin_lock(&gl->gl_spin);
1067 spin_unlock(&gl->gl_spin);
1069 spin_lock(&gl->gl_spin);
1073 spin_unlock(&gl->gl_spin);
1077 * glock_wait_internal - wait on a glock acquisition
1078 * @gh: the glock holder
1080 * Returns: 0 on success
1083 static int glock_wait_internal(struct gfs2_holder *gh)
1085 struct gfs2_glock *gl = gh->gh_gl;
1086 struct gfs2_sbd *sdp = gl->gl_sbd;
1087 struct gfs2_glock_operations *glops = gl->gl_ops;
1089 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1092 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1093 spin_lock(&gl->gl_spin);
1094 if (gl->gl_req_gh != gh &&
1095 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1096 !list_empty(&gh->gh_list)) {
1097 list_del_init(&gh->gh_list);
1098 gh->gh_error = GLR_TRYFAILED;
1100 spin_unlock(&gl->gl_spin);
1101 return gh->gh_error;
1103 spin_unlock(&gl->gl_spin);
1106 if (gh->gh_flags & LM_FLAG_PRIORITY)
1109 wait_for_completion(&gh->gh_wait);
1112 return gh->gh_error;
1114 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
1115 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state,
1119 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1120 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1122 if (glops->go_lock) {
1123 gh->gh_error = glops->go_lock(gh);
1125 spin_lock(&gl->gl_spin);
1126 list_del_init(&gh->gh_list);
1127 spin_unlock(&gl->gl_spin);
1131 spin_lock(&gl->gl_spin);
1132 gl->gl_req_gh = NULL;
1133 gl->gl_req_bh = NULL;
1134 clear_bit(GLF_LOCK, &gl->gl_flags);
1136 spin_unlock(&gl->gl_spin);
1139 return gh->gh_error;
1142 static inline struct gfs2_holder *
1143 find_holder_by_owner(struct list_head *head, struct task_struct *owner)
1145 struct gfs2_holder *gh;
1147 list_for_each_entry(gh, head, gh_list) {
1148 if (gh->gh_owner == owner)
1156 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1157 * @gh: the holder structure to add
1161 static void add_to_queue(struct gfs2_holder *gh)
1163 struct gfs2_glock *gl = gh->gh_gl;
1164 struct gfs2_holder *existing;
1166 BUG_ON(!gh->gh_owner);
1168 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
1170 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1171 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1175 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
1177 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1178 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1182 if (gh->gh_flags & LM_FLAG_PRIORITY)
1183 list_add(&gh->gh_list, &gl->gl_waiters3);
1185 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1189 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1190 * @gh: the holder structure
1192 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1194 * Returns: 0, GLR_TRYFAILED, or errno on failure
1197 int gfs2_glock_nq(struct gfs2_holder *gh)
1199 struct gfs2_glock *gl = gh->gh_gl;
1200 struct gfs2_sbd *sdp = gl->gl_sbd;
1204 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1205 set_bit(HIF_ABORTED, &gh->gh_iflags);
1209 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1211 spin_lock(&gl->gl_spin);
1214 spin_unlock(&gl->gl_spin);
1216 if (!(gh->gh_flags & GL_ASYNC)) {
1217 error = glock_wait_internal(gh);
1218 if (error == GLR_CANCELED) {
1224 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1226 if (error == GLR_TRYFAILED && (gh->gh_flags & GL_DUMP))
1233 * gfs2_glock_poll - poll to see if an async request has been completed
1236 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1239 int gfs2_glock_poll(struct gfs2_holder *gh)
1241 struct gfs2_glock *gl = gh->gh_gl;
1244 spin_lock(&gl->gl_spin);
1246 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1248 else if (list_empty(&gh->gh_list)) {
1249 if (gh->gh_error == GLR_CANCELED) {
1250 spin_unlock(&gl->gl_spin);
1252 if (gfs2_glock_nq(gh))
1259 spin_unlock(&gl->gl_spin);
1265 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1266 * @gh: the holder structure
1268 * Returns: 0, GLR_TRYFAILED, or errno on failure
1271 int gfs2_glock_wait(struct gfs2_holder *gh)
1275 error = glock_wait_internal(gh);
1276 if (error == GLR_CANCELED) {
1278 gh->gh_flags &= ~GL_ASYNC;
1279 error = gfs2_glock_nq(gh);
1286 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1287 * @gh: the glock holder
1291 void gfs2_glock_dq(struct gfs2_holder *gh)
1293 struct gfs2_glock *gl = gh->gh_gl;
1294 struct gfs2_glock_operations *glops = gl->gl_ops;
1296 if (gh->gh_flags & GL_SYNC)
1297 set_bit(GLF_SYNC, &gl->gl_flags);
1299 if (gh->gh_flags & GL_NOCACHE)
1300 handle_callback(gl, LM_ST_UNLOCKED);
1302 gfs2_glmutex_lock(gl);
1304 spin_lock(&gl->gl_spin);
1305 list_del_init(&gh->gh_list);
1307 if (list_empty(&gl->gl_holders)) {
1308 spin_unlock(&gl->gl_spin);
1310 if (glops->go_unlock)
1311 glops->go_unlock(gh);
1313 if (test_bit(GLF_SYNC, &gl->gl_flags)) {
1315 glops->go_sync(gl, DIO_METADATA | DIO_DATA);
1318 gl->gl_stamp = jiffies;
1320 spin_lock(&gl->gl_spin);
1323 clear_bit(GLF_LOCK, &gl->gl_flags);
1325 spin_unlock(&gl->gl_spin);
1329 * gfs2_glock_prefetch - Try to prefetch a glock
1331 * @state: the state to prefetch in
1332 * @flags: flags passed to go_xmote_th()
1336 static void gfs2_glock_prefetch(struct gfs2_glock *gl, unsigned int state,
1339 struct gfs2_glock_operations *glops = gl->gl_ops;
1341 spin_lock(&gl->gl_spin);
1343 if (test_bit(GLF_LOCK, &gl->gl_flags) ||
1344 !list_empty(&gl->gl_holders) ||
1345 !list_empty(&gl->gl_waiters1) ||
1346 !list_empty(&gl->gl_waiters2) ||
1347 !list_empty(&gl->gl_waiters3) ||
1348 relaxed_state_ok(gl->gl_state, state, flags)) {
1349 spin_unlock(&gl->gl_spin);
1353 set_bit(GLF_PREFETCH, &gl->gl_flags);
1354 set_bit(GLF_LOCK, &gl->gl_flags);
1355 spin_unlock(&gl->gl_spin);
1357 glops->go_xmote_th(gl, state, flags);
1360 static void greedy_work(void *data)
1362 struct greedy *gr = data;
1363 struct gfs2_holder *gh = &gr->gr_gh;
1364 struct gfs2_glock *gl = gh->gh_gl;
1365 struct gfs2_glock_operations *glops = gl->gl_ops;
1367 clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1369 if (glops->go_greedy)
1370 glops->go_greedy(gl);
1372 spin_lock(&gl->gl_spin);
1374 if (list_empty(&gl->gl_waiters2)) {
1375 clear_bit(GLF_GREEDY, &gl->gl_flags);
1376 spin_unlock(&gl->gl_spin);
1377 gfs2_holder_uninit(gh);
1380 gfs2_glock_hold(gl);
1381 list_add_tail(&gh->gh_list, &gl->gl_waiters2);
1383 spin_unlock(&gl->gl_spin);
1389 * gfs2_glock_be_greedy -
1393 * Returns: 0 if go_greedy will be called, 1 otherwise
1396 int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time)
1399 struct gfs2_holder *gh;
1401 if (!time || gl->gl_sbd->sd_args.ar_localcaching ||
1402 test_and_set_bit(GLF_GREEDY, &gl->gl_flags))
1405 gr = kmalloc(sizeof(struct greedy), GFP_KERNEL);
1407 clear_bit(GLF_GREEDY, &gl->gl_flags);
1412 gfs2_holder_init(gl, 0, 0, gh);
1413 set_bit(HIF_GREEDY, &gh->gh_iflags);
1414 INIT_WORK(&gr->gr_work, greedy_work, gr);
1416 set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1417 schedule_delayed_work(&gr->gr_work, time);
1423 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1424 * @gh: the holder structure
1428 void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1431 gfs2_holder_uninit(gh);
1435 * gfs2_glock_nq_num - acquire a glock based on lock number
1436 * @sdp: the filesystem
1437 * @number: the lock number
1438 * @glops: the glock operations for the type of glock
1439 * @state: the state to acquire the glock in
1440 * @flags: modifier flags for the aquisition
1441 * @gh: the struct gfs2_holder
1446 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, uint64_t number,
1447 struct gfs2_glock_operations *glops, unsigned int state,
1448 int flags, struct gfs2_holder *gh)
1450 struct gfs2_glock *gl;
1453 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1455 error = gfs2_glock_nq_init(gl, state, flags, gh);
1463 * glock_compare - Compare two struct gfs2_glock structures for sorting
1464 * @arg_a: the first structure
1465 * @arg_b: the second structure
1469 static int glock_compare(const void *arg_a, const void *arg_b)
1471 struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
1472 struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
1473 struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1474 struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1477 if (a->ln_number > b->ln_number)
1479 else if (a->ln_number < b->ln_number)
1482 if (gh_a->gh_state == LM_ST_SHARED &&
1483 gh_b->gh_state == LM_ST_EXCLUSIVE)
1485 else if (!(gh_a->gh_flags & GL_LOCAL_EXCL) &&
1486 (gh_b->gh_flags & GL_LOCAL_EXCL))
1494 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1495 * @num_gh: the number of structures
1496 * @ghs: an array of struct gfs2_holder structures
1498 * Returns: 0 on success (all glocks acquired),
1499 * errno on failure (no glocks acquired)
1502 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1503 struct gfs2_holder **p)
1508 for (x = 0; x < num_gh; x++)
1511 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1513 for (x = 0; x < num_gh; x++) {
1514 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1516 error = gfs2_glock_nq(p[x]);
1519 gfs2_glock_dq(p[x]);
1528 * gfs2_glock_nq_m - acquire multiple glocks
1529 * @num_gh: the number of structures
1530 * @ghs: an array of struct gfs2_holder structures
1532 * Figure out how big an impact this function has. Either:
1533 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1534 * 2) Forget async stuff and just call nq_m_sync()
1535 * 3) Leave it like it is
1537 * Returns: 0 on success (all glocks acquired),
1538 * errno on failure (no glocks acquired)
1541 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1545 int borked = 0, serious = 0;
1552 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1553 return gfs2_glock_nq(ghs);
1556 e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1560 for (x = 0; x < num_gh; x++) {
1561 ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
1562 error = gfs2_glock_nq(&ghs[x]);
1571 for (x = 0; x < num_gh; x++) {
1572 error = e[x] = glock_wait_internal(&ghs[x]);
1575 if (error != GLR_TRYFAILED && error != GLR_CANCELED)
1585 for (x = 0; x < num_gh; x++)
1587 gfs2_glock_dq(&ghs[x]);
1592 for (x = 0; x < num_gh; x++)
1593 gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
1595 error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
1604 * gfs2_glock_dq_m - release multiple glocks
1605 * @num_gh: the number of structures
1606 * @ghs: an array of struct gfs2_holder structures
1610 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1614 for (x = 0; x < num_gh; x++)
1615 gfs2_glock_dq(&ghs[x]);
1619 * gfs2_glock_dq_uninit_m - release multiple glocks
1620 * @num_gh: the number of structures
1621 * @ghs: an array of struct gfs2_holder structures
1625 void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1629 for (x = 0; x < num_gh; x++)
1630 gfs2_glock_dq_uninit(&ghs[x]);
1634 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1635 * @sdp: the filesystem
1636 * @number: the lock number
1637 * @glops: the glock operations for the type of glock
1638 * @state: the state to acquire the glock in
1639 * @flags: modifier flags for the aquisition
1644 void gfs2_glock_prefetch_num(struct gfs2_sbd *sdp, uint64_t number,
1645 struct gfs2_glock_operations *glops,
1646 unsigned int state, int flags)
1648 struct gfs2_glock *gl;
1651 if (atomic_read(&sdp->sd_reclaim_count) <
1652 gfs2_tune_get(sdp, gt_reclaim_limit)) {
1653 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1655 gfs2_glock_prefetch(gl, state, flags);
1662 * gfs2_lvb_hold - attach a LVB from a glock
1663 * @gl: The glock in question
1667 int gfs2_lvb_hold(struct gfs2_glock *gl)
1671 gfs2_glmutex_lock(gl);
1673 if (!atomic_read(&gl->gl_lvb_count)) {
1674 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1676 gfs2_glmutex_unlock(gl);
1679 gfs2_glock_hold(gl);
1681 atomic_inc(&gl->gl_lvb_count);
1683 gfs2_glmutex_unlock(gl);
1689 * gfs2_lvb_unhold - detach a LVB from a glock
1690 * @gl: The glock in question
1694 void gfs2_lvb_unhold(struct gfs2_glock *gl)
1696 gfs2_glock_hold(gl);
1697 gfs2_glmutex_lock(gl);
1699 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1700 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1701 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1706 gfs2_glmutex_unlock(gl);
1711 void gfs2_lvb_sync(struct gfs2_glock *gl)
1713 gfs2_glmutex_lock(gl);
1715 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count));
1716 if (!gfs2_assert_warn(gl->gl_sbd, gfs2_glock_is_held_excl(gl)))
1717 gfs2_lm_sync_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1719 gfs2_glmutex_unlock(gl);
1723 static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1726 struct gfs2_glock *gl;
1728 gl = gfs2_glock_find(sdp, name);
1732 if (gl->gl_ops->go_callback)
1733 gl->gl_ops->go_callback(gl, state);
1734 handle_callback(gl, state);
1736 spin_lock(&gl->gl_spin);
1738 spin_unlock(&gl->gl_spin);
1744 * gfs2_glock_cb - Callback used by locking module
1745 * @fsdata: Pointer to the superblock
1746 * @type: Type of callback
1747 * @data: Type dependent data pointer
1749 * Called by the locking module when it wants to tell us something.
1750 * Either we need to drop a lock, one of our ASYNC requests completed, or
1751 * a journal from another client needs to be recovered.
1754 void gfs2_glock_cb(lm_fsdata_t *fsdata, unsigned int type, void *data)
1756 struct gfs2_sbd *sdp = (struct gfs2_sbd *)fsdata;
1760 blocking_cb(sdp, data, LM_ST_UNLOCKED);
1764 blocking_cb(sdp, data, LM_ST_DEFERRED);
1768 blocking_cb(sdp, data, LM_ST_SHARED);
1772 struct lm_async_cb *async = data;
1773 struct gfs2_glock *gl;
1775 gl = gfs2_glock_find(sdp, &async->lc_name);
1776 if (gfs2_assert_warn(sdp, gl))
1778 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1779 gl->gl_req_bh(gl, async->lc_ret);
1784 case LM_CB_NEED_RECOVERY:
1785 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1786 if (sdp->sd_recoverd_process)
1787 wake_up_process(sdp->sd_recoverd_process);
1790 case LM_CB_DROPLOCKS:
1791 gfs2_gl_hash_clear(sdp, NO_WAIT);
1792 gfs2_quota_scan(sdp);
1796 gfs2_assert_warn(sdp, 0);
1802 * gfs2_iopen_go_callback - Try to kick the inode/vnode associated with an
1803 * iopen glock from memory
1804 * @io_gl: the iopen glock
1805 * @state: the state into which the glock should be put
1809 void gfs2_iopen_go_callback(struct gfs2_glock *io_gl, unsigned int state)
1812 if (state != LM_ST_UNLOCKED)
1814 /* FIXME: remove this? */
1818 * demote_ok - Check to see if it's ok to unlock a glock
1821 * Returns: 1 if it's ok
1824 static int demote_ok(struct gfs2_glock *gl)
1826 struct gfs2_sbd *sdp = gl->gl_sbd;
1827 struct gfs2_glock_operations *glops = gl->gl_ops;
1830 if (test_bit(GLF_STICKY, &gl->gl_flags))
1832 else if (test_bit(GLF_PREFETCH, &gl->gl_flags))
1833 demote = time_after_eq(jiffies,
1835 gfs2_tune_get(sdp, gt_prefetch_secs) * HZ);
1836 else if (glops->go_demote_ok)
1837 demote = glops->go_demote_ok(gl);
1843 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1848 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1850 struct gfs2_sbd *sdp = gl->gl_sbd;
1852 spin_lock(&sdp->sd_reclaim_lock);
1853 if (list_empty(&gl->gl_reclaim)) {
1854 gfs2_glock_hold(gl);
1855 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1856 atomic_inc(&sdp->sd_reclaim_count);
1858 spin_unlock(&sdp->sd_reclaim_lock);
1860 wake_up(&sdp->sd_reclaim_wq);
1864 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1865 * @sdp: the filesystem
1867 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1868 * different glock and we notice that there are a lot of glocks in the
1873 void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1875 struct gfs2_glock *gl;
1877 spin_lock(&sdp->sd_reclaim_lock);
1878 if (list_empty(&sdp->sd_reclaim_list)) {
1879 spin_unlock(&sdp->sd_reclaim_lock);
1882 gl = list_entry(sdp->sd_reclaim_list.next,
1883 struct gfs2_glock, gl_reclaim);
1884 list_del_init(&gl->gl_reclaim);
1885 spin_unlock(&sdp->sd_reclaim_lock);
1887 atomic_dec(&sdp->sd_reclaim_count);
1888 atomic_inc(&sdp->sd_reclaimed);
1890 if (gfs2_glmutex_trylock(gl)) {
1891 if (queue_empty(gl, &gl->gl_holders) &&
1892 gl->gl_state != LM_ST_UNLOCKED &&
1894 handle_callback(gl, LM_ST_UNLOCKED);
1895 gfs2_glmutex_unlock(gl);
1902 * examine_bucket - Call a function for glock in a hash bucket
1903 * @examiner: the function
1904 * @sdp: the filesystem
1905 * @bucket: the bucket
1907 * Returns: 1 if the bucket has entries
1910 static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
1911 struct gfs2_gl_hash_bucket *bucket)
1913 struct glock_plug plug;
1914 struct list_head *tmp;
1915 struct gfs2_glock *gl;
1918 /* Add "plug" to end of bucket list, work back up list from there */
1919 memset(&plug.gl_flags, 0, sizeof(unsigned long));
1920 set_bit(GLF_PLUG, &plug.gl_flags);
1922 write_lock(&bucket->hb_lock);
1923 list_add(&plug.gl_list, &bucket->hb_list);
1924 write_unlock(&bucket->hb_lock);
1927 write_lock(&bucket->hb_lock);
1930 tmp = plug.gl_list.next;
1932 if (tmp == &bucket->hb_list) {
1933 list_del(&plug.gl_list);
1934 entries = !list_empty(&bucket->hb_list);
1935 write_unlock(&bucket->hb_lock);
1938 gl = list_entry(tmp, struct gfs2_glock, gl_list);
1940 /* Move plug up list */
1941 list_move(&plug.gl_list, &gl->gl_list);
1943 if (test_bit(GLF_PLUG, &gl->gl_flags))
1946 /* examiner() must glock_put() */
1947 gfs2_glock_hold(gl);
1952 write_unlock(&bucket->hb_lock);
1959 * scan_glock - look at a glock and see if we can reclaim it
1960 * @gl: the glock to look at
1964 static void scan_glock(struct gfs2_glock *gl)
1966 if (gfs2_glmutex_trylock(gl)) {
1967 if (gl->gl_ops == &gfs2_inode_glops)
1969 if (queue_empty(gl, &gl->gl_holders) &&
1970 gl->gl_state != LM_ST_UNLOCKED &&
1974 gfs2_glmutex_unlock(gl);
1982 gfs2_glmutex_unlock(gl);
1983 gfs2_glock_schedule_for_reclaim(gl);
1988 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
1989 * @sdp: the filesystem
1993 void gfs2_scand_internal(struct gfs2_sbd *sdp)
1997 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
1998 examine_bucket(scan_glock, sdp, &sdp->sd_gl_hash[x]);
2004 * clear_glock - look at a glock and see if we can free it from glock cache
2005 * @gl: the glock to look at
2009 static void clear_glock(struct gfs2_glock *gl)
2011 struct gfs2_sbd *sdp = gl->gl_sbd;
2014 spin_lock(&sdp->sd_reclaim_lock);
2015 if (!list_empty(&gl->gl_reclaim)) {
2016 list_del_init(&gl->gl_reclaim);
2017 atomic_dec(&sdp->sd_reclaim_count);
2018 spin_unlock(&sdp->sd_reclaim_lock);
2019 released = gfs2_glock_put(gl);
2020 gfs2_assert(sdp, !released);
2022 spin_unlock(&sdp->sd_reclaim_lock);
2025 if (gfs2_glmutex_trylock(gl)) {
2026 if (queue_empty(gl, &gl->gl_holders) &&
2027 gl->gl_state != LM_ST_UNLOCKED)
2028 handle_callback(gl, LM_ST_UNLOCKED);
2030 gfs2_glmutex_unlock(gl);
2037 * gfs2_gl_hash_clear - Empty out the glock hash table
2038 * @sdp: the filesystem
2039 * @wait: wait until it's all gone
2041 * Called when unmounting the filesystem, or when inter-node lock manager
2042 * requests DROPLOCKS because it is running out of capacity.
2045 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
2056 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
2057 if (examine_bucket(clear_glock, sdp,
2058 &sdp->sd_gl_hash[x]))
2064 if (time_after_eq(jiffies,
2065 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
2066 fs_warn(sdp, "Unmount seems to be stalled. "
2067 "Dumping lock state...\n");
2068 gfs2_dump_lockstate(sdp);
2072 /* invalidate_inodes() requires that the sb inodes list
2073 not change, but an async completion callback for an
2074 unlock can occur which does glock_put() which
2075 can call iput() which will change the sb inodes list.
2076 invalidate_inodes_mutex prevents glock_put()'s during
2077 an invalidate_inodes() */
2079 mutex_lock(&sdp->sd_invalidate_inodes_mutex);
2080 invalidate_inodes(sdp->sd_vfs);
2081 mutex_unlock(&sdp->sd_invalidate_inodes_mutex);
2087 * Diagnostic routines to help debug distributed deadlock
2091 * dump_holder - print information about a glock holder
2092 * @str: a string naming the type of holder
2093 * @gh: the glock holder
2095 * Returns: 0 on success, -ENOBUFS when we run out of space
2098 static int dump_holder(char *str, struct gfs2_holder *gh)
2101 int error = -ENOBUFS;
2103 printk(KERN_INFO " %s\n", str);
2104 printk(KERN_INFO " owner = %ld\n",
2105 (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
2106 printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
2107 printk(KERN_INFO " gh_flags =");
2108 for (x = 0; x < 32; x++)
2109 if (gh->gh_flags & (1 << x))
2112 printk(KERN_INFO " error = %d\n", gh->gh_error);
2113 printk(KERN_INFO " gh_iflags =");
2114 for (x = 0; x < 32; x++)
2115 if (test_bit(x, &gh->gh_iflags))
2118 print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip);
2126 * dump_inode - print information about an inode
2129 * Returns: 0 on success, -ENOBUFS when we run out of space
2132 static int dump_inode(struct gfs2_inode *ip)
2135 int error = -ENOBUFS;
2137 printk(KERN_INFO " Inode:\n");
2138 printk(KERN_INFO " num = %llu %llu\n",
2139 (unsigned long long)ip->i_num.no_formal_ino,
2140 (unsigned long long)ip->i_num.no_addr);
2141 printk(KERN_INFO " type = %u\n", IF2DT(ip->i_di.di_mode));
2142 printk(KERN_INFO " i_flags =");
2143 for (x = 0; x < 32; x++)
2144 if (test_bit(x, &ip->i_flags))
2154 * dump_glock - print information about a glock
2156 * @count: where we are in the buffer
2158 * Returns: 0 on success, -ENOBUFS when we run out of space
2161 static int dump_glock(struct gfs2_glock *gl)
2163 struct gfs2_holder *gh;
2165 int error = -ENOBUFS;
2167 spin_lock(&gl->gl_spin);
2169 printk(KERN_INFO "Glock (%u, %llu)\n", gl->gl_name.ln_type,
2170 (unsigned long long)gl->gl_name.ln_number);
2171 printk(KERN_INFO " gl_flags =");
2172 for (x = 0; x < 32; x++)
2173 if (test_bit(x, &gl->gl_flags))
2176 printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref.refcount));
2177 printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
2178 printk(KERN_INFO " gl_owner = %s\n", gl->gl_owner->comm);
2179 print_symbol(KERN_INFO " gl_ip = %s\n", gl->gl_ip);
2180 printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
2181 printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
2182 printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
2183 printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
2184 printk(KERN_INFO " le = %s\n",
2185 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
2186 printk(KERN_INFO " reclaim = %s\n",
2187 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
2189 printk(KERN_INFO " aspace = %lu\n",
2190 gl->gl_aspace->i_mapping->nrpages);
2192 printk(KERN_INFO " aspace = no\n");
2193 printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
2194 if (gl->gl_req_gh) {
2195 error = dump_holder("Request", gl->gl_req_gh);
2199 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
2200 error = dump_holder("Holder", gh);
2204 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
2205 error = dump_holder("Waiter1", gh);
2209 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
2210 error = dump_holder("Waiter2", gh);
2214 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
2215 error = dump_holder("Waiter3", gh);
2219 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
2220 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
2221 list_empty(&gl->gl_holders)) {
2222 error = dump_inode(gl->gl_object);
2227 printk(KERN_INFO " Inode: busy\n");
2234 spin_unlock(&gl->gl_spin);
2240 * gfs2_dump_lockstate - print out the current lockstate
2241 * @sdp: the filesystem
2242 * @ub: the buffer to copy the information into
2244 * If @ub is NULL, dump the lockstate to the console.
2248 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
2250 struct gfs2_gl_hash_bucket *bucket;
2251 struct gfs2_glock *gl;
2255 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2256 bucket = &sdp->sd_gl_hash[x];
2258 read_lock(&bucket->hb_lock);
2260 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
2261 if (test_bit(GLF_PLUG, &gl->gl_flags))
2264 error = dump_glock(gl);
2269 read_unlock(&bucket->hb_lock);