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 ||
304 glops == &gfs2_rgrp_glops) {
305 gl->gl_aspace = gfs2_aspace_get(sdp);
306 if (!gl->gl_aspace) {
312 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
316 write_lock(&bucket->hb_lock);
317 tmp = search_bucket(bucket, sdp, &name);
319 write_unlock(&bucket->hb_lock);
323 list_add_tail(&gl->gl_list, &bucket->hb_list);
324 write_unlock(&bucket->hb_lock);
333 gfs2_aspace_put(gl->gl_aspace);
335 kmem_cache_free(gfs2_glock_cachep, gl);
340 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
342 * @state: the state we're requesting
343 * @flags: the modifier flags
344 * @gh: the holder structure
348 void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
349 struct gfs2_holder *gh)
351 INIT_LIST_HEAD(&gh->gh_list);
353 gh->gh_ip = (unsigned long)__builtin_return_address(0);
354 gh->gh_owner = current;
355 gh->gh_state = state;
356 gh->gh_flags = flags;
359 init_completion(&gh->gh_wait);
361 if (gh->gh_state == LM_ST_EXCLUSIVE)
362 gh->gh_flags |= GL_LOCAL_EXCL;
368 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
369 * @state: the state we're requesting
370 * @flags: the modifier flags
371 * @gh: the holder structure
373 * Don't mess with the glock.
377 void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
379 gh->gh_state = state;
380 gh->gh_flags = flags;
381 if (gh->gh_state == LM_ST_EXCLUSIVE)
382 gh->gh_flags |= GL_LOCAL_EXCL;
384 gh->gh_iflags &= 1 << HIF_ALLOCED;
385 gh->gh_ip = (unsigned long)__builtin_return_address(0);
389 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
390 * @gh: the holder structure
394 void gfs2_holder_uninit(struct gfs2_holder *gh)
396 gfs2_glock_put(gh->gh_gl);
402 * gfs2_holder_get - get a struct gfs2_holder structure
404 * @state: the state we're requesting
405 * @flags: the modifier flags
408 * Figure out how big an impact this function has. Either:
409 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
410 * 2) Leave it like it is
412 * Returns: the holder structure, NULL on ENOMEM
415 static struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl,
417 int flags, gfp_t gfp_flags)
419 struct gfs2_holder *gh;
421 gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags);
425 gfs2_holder_init(gl, state, flags, gh);
426 set_bit(HIF_ALLOCED, &gh->gh_iflags);
427 gh->gh_ip = (unsigned long)__builtin_return_address(0);
432 * gfs2_holder_put - get rid of a struct gfs2_holder structure
433 * @gh: the holder structure
437 static void gfs2_holder_put(struct gfs2_holder *gh)
439 gfs2_holder_uninit(gh);
444 * rq_mutex - process a mutex request in the queue
445 * @gh: the glock holder
447 * Returns: 1 if the queue is blocked
450 static int rq_mutex(struct gfs2_holder *gh)
452 struct gfs2_glock *gl = gh->gh_gl;
454 list_del_init(&gh->gh_list);
455 /* gh->gh_error never examined. */
456 set_bit(GLF_LOCK, &gl->gl_flags);
457 complete(&gh->gh_wait);
463 * rq_promote - process a promote request in the queue
464 * @gh: the glock holder
466 * Acquire a new inter-node lock, or change a lock state to more restrictive.
468 * Returns: 1 if the queue is blocked
471 static int rq_promote(struct gfs2_holder *gh)
473 struct gfs2_glock *gl = gh->gh_gl;
474 struct gfs2_sbd *sdp = gl->gl_sbd;
475 const struct gfs2_glock_operations *glops = gl->gl_ops;
477 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
478 if (list_empty(&gl->gl_holders)) {
480 set_bit(GLF_LOCK, &gl->gl_flags);
481 spin_unlock(&gl->gl_spin);
483 if (atomic_read(&sdp->sd_reclaim_count) >
484 gfs2_tune_get(sdp, gt_reclaim_limit) &&
485 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
486 gfs2_reclaim_glock(sdp);
487 gfs2_reclaim_glock(sdp);
490 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
491 spin_lock(&gl->gl_spin);
496 if (list_empty(&gl->gl_holders)) {
497 set_bit(HIF_FIRST, &gh->gh_iflags);
498 set_bit(GLF_LOCK, &gl->gl_flags);
500 struct gfs2_holder *next_gh;
501 if (gh->gh_flags & GL_LOCAL_EXCL)
503 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
505 if (next_gh->gh_flags & GL_LOCAL_EXCL)
509 list_move_tail(&gh->gh_list, &gl->gl_holders);
511 set_bit(HIF_HOLDER, &gh->gh_iflags);
513 complete(&gh->gh_wait);
519 * rq_demote - process a demote request in the queue
520 * @gh: the glock holder
522 * Returns: 1 if the queue is blocked
525 static int rq_demote(struct gfs2_holder *gh)
527 struct gfs2_glock *gl = gh->gh_gl;
528 const struct gfs2_glock_operations *glops = gl->gl_ops;
530 if (!list_empty(&gl->gl_holders))
533 if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) {
534 list_del_init(&gh->gh_list);
536 spin_unlock(&gl->gl_spin);
537 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
540 complete(&gh->gh_wait);
541 spin_lock(&gl->gl_spin);
544 set_bit(GLF_LOCK, &gl->gl_flags);
545 spin_unlock(&gl->gl_spin);
547 if (gh->gh_state == LM_ST_UNLOCKED ||
548 gl->gl_state != LM_ST_EXCLUSIVE)
549 glops->go_drop_th(gl);
551 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
553 spin_lock(&gl->gl_spin);
560 * rq_greedy - process a queued request to drop greedy status
561 * @gh: the glock holder
563 * Returns: 1 if the queue is blocked
566 static int rq_greedy(struct gfs2_holder *gh)
568 struct gfs2_glock *gl = gh->gh_gl;
570 list_del_init(&gh->gh_list);
571 /* gh->gh_error never examined. */
572 clear_bit(GLF_GREEDY, &gl->gl_flags);
573 spin_unlock(&gl->gl_spin);
575 gfs2_holder_uninit(gh);
576 kfree(container_of(gh, struct greedy, gr_gh));
578 spin_lock(&gl->gl_spin);
584 * run_queue - process holder structures on a glock
588 static void run_queue(struct gfs2_glock *gl)
590 struct gfs2_holder *gh;
594 if (test_bit(GLF_LOCK, &gl->gl_flags))
597 if (!list_empty(&gl->gl_waiters1)) {
598 gh = list_entry(gl->gl_waiters1.next,
599 struct gfs2_holder, gh_list);
601 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
602 blocked = rq_mutex(gh);
604 gfs2_assert_warn(gl->gl_sbd, 0);
606 } else if (!list_empty(&gl->gl_waiters2) &&
607 !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) {
608 gh = list_entry(gl->gl_waiters2.next,
609 struct gfs2_holder, gh_list);
611 if (test_bit(HIF_DEMOTE, &gh->gh_iflags))
612 blocked = rq_demote(gh);
613 else if (test_bit(HIF_GREEDY, &gh->gh_iflags))
614 blocked = rq_greedy(gh);
616 gfs2_assert_warn(gl->gl_sbd, 0);
618 } else if (!list_empty(&gl->gl_waiters3)) {
619 gh = list_entry(gl->gl_waiters3.next,
620 struct gfs2_holder, gh_list);
622 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
623 blocked = rq_promote(gh);
625 gfs2_assert_warn(gl->gl_sbd, 0);
636 * gfs2_glmutex_lock - acquire a local lock on a glock
639 * Gives caller exclusive access to manipulate a glock structure.
642 static void gfs2_glmutex_lock(struct gfs2_glock *gl)
644 struct gfs2_holder gh;
646 gfs2_holder_init(gl, 0, 0, &gh);
647 set_bit(HIF_MUTEX, &gh.gh_iflags);
649 spin_lock(&gl->gl_spin);
650 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
651 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
653 gl->gl_owner = current;
654 gl->gl_ip = (unsigned long)__builtin_return_address(0);
655 complete(&gh.gh_wait);
657 spin_unlock(&gl->gl_spin);
659 wait_for_completion(&gh.gh_wait);
660 gfs2_holder_uninit(&gh);
664 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
667 * Returns: 1 if the glock is acquired
670 static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
674 spin_lock(&gl->gl_spin);
675 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
678 gl->gl_owner = current;
679 gl->gl_ip = (unsigned long)__builtin_return_address(0);
681 spin_unlock(&gl->gl_spin);
687 * gfs2_glmutex_unlock - release a local lock on a glock
692 static void gfs2_glmutex_unlock(struct gfs2_glock *gl)
694 spin_lock(&gl->gl_spin);
695 clear_bit(GLF_LOCK, &gl->gl_flags);
699 BUG_ON(!spin_is_locked(&gl->gl_spin));
700 spin_unlock(&gl->gl_spin);
704 * handle_callback - add a demote request to a lock's queue
706 * @state: the state the caller wants us to change to
708 * Note: This may fail sliently if we are out of memory.
711 static void handle_callback(struct gfs2_glock *gl, unsigned int state)
713 struct gfs2_holder *gh, *new_gh = NULL;
716 spin_lock(&gl->gl_spin);
718 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
719 if (test_bit(HIF_DEMOTE, &gh->gh_iflags) &&
720 gl->gl_req_gh != gh) {
721 if (gh->gh_state != state)
722 gh->gh_state = LM_ST_UNLOCKED;
728 list_add_tail(&new_gh->gh_list, &gl->gl_waiters2);
731 spin_unlock(&gl->gl_spin);
733 new_gh = gfs2_holder_get(gl, state, LM_FLAG_TRY, GFP_KERNEL);
736 set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
737 set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
743 spin_unlock(&gl->gl_spin);
746 gfs2_holder_put(new_gh);
749 void gfs2_glock_inode_squish(struct inode *inode)
751 struct gfs2_holder gh;
752 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
753 gfs2_holder_init(gl, LM_ST_UNLOCKED, 0, &gh);
754 set_bit(HIF_DEMOTE, &gh.gh_iflags);
755 spin_lock(&gl->gl_spin);
756 gfs2_assert(inode->i_sb->s_fs_info, list_empty(&gl->gl_holders));
757 list_add_tail(&gh.gh_list, &gl->gl_waiters2);
759 spin_unlock(&gl->gl_spin);
760 wait_for_completion(&gh.gh_wait);
761 gfs2_holder_uninit(&gh);
765 * state_change - record that the glock is now in a different state
767 * @new_state the new state
771 static void state_change(struct gfs2_glock *gl, unsigned int new_state)
775 held1 = (gl->gl_state != LM_ST_UNLOCKED);
776 held2 = (new_state != LM_ST_UNLOCKED);
778 if (held1 != held2) {
785 gl->gl_state = new_state;
789 * xmote_bh - Called after the lock module is done acquiring a lock
790 * @gl: The glock in question
791 * @ret: the int returned from the lock module
795 static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
797 struct gfs2_sbd *sdp = gl->gl_sbd;
798 const struct gfs2_glock_operations *glops = gl->gl_ops;
799 struct gfs2_holder *gh = gl->gl_req_gh;
800 int prev_state = gl->gl_state;
803 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
804 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
805 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
807 state_change(gl, ret & LM_OUT_ST_MASK);
809 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
811 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
812 } else if (gl->gl_state == LM_ST_DEFERRED) {
813 /* We might not want to do this here.
814 Look at moving to the inode glops. */
816 glops->go_inval(gl, DIO_DATA);
819 /* Deal with each possible exit condition */
822 gl->gl_stamp = jiffies;
824 else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
825 spin_lock(&gl->gl_spin);
826 list_del_init(&gh->gh_list);
828 spin_unlock(&gl->gl_spin);
830 } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
831 spin_lock(&gl->gl_spin);
832 list_del_init(&gh->gh_list);
833 if (gl->gl_state == gh->gh_state ||
834 gl->gl_state == LM_ST_UNLOCKED)
837 if (gfs2_assert_warn(sdp, gh->gh_flags &
838 (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
839 fs_warn(sdp, "ret = 0x%.8X\n", ret);
840 gh->gh_error = GLR_TRYFAILED;
842 spin_unlock(&gl->gl_spin);
844 if (ret & LM_OUT_CANCELED)
845 handle_callback(gl, LM_ST_UNLOCKED); /* Lame */
847 } else if (ret & LM_OUT_CANCELED) {
848 spin_lock(&gl->gl_spin);
849 list_del_init(&gh->gh_list);
850 gh->gh_error = GLR_CANCELED;
851 spin_unlock(&gl->gl_spin);
853 } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
854 spin_lock(&gl->gl_spin);
855 list_move_tail(&gh->gh_list, &gl->gl_holders);
857 set_bit(HIF_HOLDER, &gh->gh_iflags);
858 spin_unlock(&gl->gl_spin);
860 set_bit(HIF_FIRST, &gh->gh_iflags);
864 } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
865 spin_lock(&gl->gl_spin);
866 list_del_init(&gh->gh_list);
867 gh->gh_error = GLR_TRYFAILED;
868 spin_unlock(&gl->gl_spin);
871 if (gfs2_assert_withdraw(sdp, 0) == -1)
872 fs_err(sdp, "ret = 0x%.8X\n", ret);
875 if (glops->go_xmote_bh)
876 glops->go_xmote_bh(gl);
879 spin_lock(&gl->gl_spin);
880 gl->gl_req_gh = NULL;
881 gl->gl_req_bh = NULL;
882 clear_bit(GLF_LOCK, &gl->gl_flags);
884 spin_unlock(&gl->gl_spin);
890 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
893 complete(&gh->gh_wait);
898 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
899 * @gl: The glock in question
900 * @state: the requested state
901 * @flags: modifier flags to the lock call
905 void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags)
907 struct gfs2_sbd *sdp = gl->gl_sbd;
908 const struct gfs2_glock_operations *glops = gl->gl_ops;
909 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
910 LM_FLAG_NOEXP | LM_FLAG_ANY |
912 unsigned int lck_ret;
914 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
915 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
916 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
917 gfs2_assert_warn(sdp, state != gl->gl_state);
919 if (gl->gl_state == LM_ST_EXCLUSIVE) {
922 DIO_METADATA | DIO_DATA | DIO_RELEASE);
926 gl->gl_req_bh = xmote_bh;
928 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state, lck_flags);
930 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
933 if (lck_ret & LM_OUT_ASYNC)
934 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
936 xmote_bh(gl, lck_ret);
940 * drop_bh - Called after a lock module unlock completes
942 * @ret: the return status
944 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
945 * Doesn't drop the reference on the glock the top half took out
949 static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
951 struct gfs2_sbd *sdp = gl->gl_sbd;
952 const struct gfs2_glock_operations *glops = gl->gl_ops;
953 struct gfs2_holder *gh = gl->gl_req_gh;
955 clear_bit(GLF_PREFETCH, &gl->gl_flags);
957 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
958 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
959 gfs2_assert_warn(sdp, !ret);
961 state_change(gl, LM_ST_UNLOCKED);
964 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
967 spin_lock(&gl->gl_spin);
968 list_del_init(&gh->gh_list);
970 spin_unlock(&gl->gl_spin);
973 if (glops->go_drop_bh)
974 glops->go_drop_bh(gl);
976 spin_lock(&gl->gl_spin);
977 gl->gl_req_gh = NULL;
978 gl->gl_req_bh = NULL;
979 clear_bit(GLF_LOCK, &gl->gl_flags);
981 spin_unlock(&gl->gl_spin);
986 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
989 complete(&gh->gh_wait);
994 * gfs2_glock_drop_th - call into the lock module to unlock a lock
999 void gfs2_glock_drop_th(struct gfs2_glock *gl)
1001 struct gfs2_sbd *sdp = gl->gl_sbd;
1002 const struct gfs2_glock_operations *glops = gl->gl_ops;
1005 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1006 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
1007 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
1009 if (gl->gl_state == LM_ST_EXCLUSIVE) {
1011 glops->go_sync(gl, DIO_METADATA | DIO_DATA | DIO_RELEASE);
1014 gfs2_glock_hold(gl);
1015 gl->gl_req_bh = drop_bh;
1017 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
1019 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
1025 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
1029 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1030 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1032 * Don't cancel GL_NOCANCEL requests.
1035 static void do_cancels(struct gfs2_holder *gh)
1037 struct gfs2_glock *gl = gh->gh_gl;
1039 spin_lock(&gl->gl_spin);
1041 while (gl->gl_req_gh != gh &&
1042 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1043 !list_empty(&gh->gh_list)) {
1044 if (gl->gl_req_bh &&
1046 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
1047 spin_unlock(&gl->gl_spin);
1048 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
1050 spin_lock(&gl->gl_spin);
1052 spin_unlock(&gl->gl_spin);
1054 spin_lock(&gl->gl_spin);
1058 spin_unlock(&gl->gl_spin);
1062 * glock_wait_internal - wait on a glock acquisition
1063 * @gh: the glock holder
1065 * Returns: 0 on success
1068 static int glock_wait_internal(struct gfs2_holder *gh)
1070 struct gfs2_glock *gl = gh->gh_gl;
1071 struct gfs2_sbd *sdp = gl->gl_sbd;
1072 const struct gfs2_glock_operations *glops = gl->gl_ops;
1074 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1077 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1078 spin_lock(&gl->gl_spin);
1079 if (gl->gl_req_gh != gh &&
1080 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1081 !list_empty(&gh->gh_list)) {
1082 list_del_init(&gh->gh_list);
1083 gh->gh_error = GLR_TRYFAILED;
1085 spin_unlock(&gl->gl_spin);
1086 return gh->gh_error;
1088 spin_unlock(&gl->gl_spin);
1091 if (gh->gh_flags & LM_FLAG_PRIORITY)
1094 wait_for_completion(&gh->gh_wait);
1097 return gh->gh_error;
1099 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
1100 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state,
1104 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1105 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1107 if (glops->go_lock) {
1108 gh->gh_error = glops->go_lock(gh);
1110 spin_lock(&gl->gl_spin);
1111 list_del_init(&gh->gh_list);
1112 spin_unlock(&gl->gl_spin);
1116 spin_lock(&gl->gl_spin);
1117 gl->gl_req_gh = NULL;
1118 gl->gl_req_bh = NULL;
1119 clear_bit(GLF_LOCK, &gl->gl_flags);
1121 spin_unlock(&gl->gl_spin);
1124 return gh->gh_error;
1127 static inline struct gfs2_holder *
1128 find_holder_by_owner(struct list_head *head, struct task_struct *owner)
1130 struct gfs2_holder *gh;
1132 list_for_each_entry(gh, head, gh_list) {
1133 if (gh->gh_owner == owner)
1141 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1142 * @gh: the holder structure to add
1146 static void add_to_queue(struct gfs2_holder *gh)
1148 struct gfs2_glock *gl = gh->gh_gl;
1149 struct gfs2_holder *existing;
1151 BUG_ON(!gh->gh_owner);
1153 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
1155 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1156 printk(KERN_INFO "pid : %d\n", existing->gh_owner->pid);
1157 printk(KERN_INFO "lock type : %d lock state : %d\n",
1158 existing->gh_gl->gl_name.ln_type, existing->gh_gl->gl_state);
1159 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1160 printk(KERN_INFO "pid : %d\n", gh->gh_owner->pid);
1161 printk(KERN_INFO "lock type : %d lock state : %d\n",
1162 gl->gl_name.ln_type, gl->gl_state);
1166 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
1168 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1169 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1173 if (gh->gh_flags & LM_FLAG_PRIORITY)
1174 list_add(&gh->gh_list, &gl->gl_waiters3);
1176 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1180 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1181 * @gh: the holder structure
1183 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1185 * Returns: 0, GLR_TRYFAILED, or errno on failure
1188 int gfs2_glock_nq(struct gfs2_holder *gh)
1190 struct gfs2_glock *gl = gh->gh_gl;
1191 struct gfs2_sbd *sdp = gl->gl_sbd;
1195 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1196 set_bit(HIF_ABORTED, &gh->gh_iflags);
1200 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1202 spin_lock(&gl->gl_spin);
1205 spin_unlock(&gl->gl_spin);
1207 if (!(gh->gh_flags & GL_ASYNC)) {
1208 error = glock_wait_internal(gh);
1209 if (error == GLR_CANCELED) {
1215 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1217 if (error == GLR_TRYFAILED && (gh->gh_flags & GL_DUMP))
1224 * gfs2_glock_poll - poll to see if an async request has been completed
1227 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1230 int gfs2_glock_poll(struct gfs2_holder *gh)
1232 struct gfs2_glock *gl = gh->gh_gl;
1235 spin_lock(&gl->gl_spin);
1237 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1239 else if (list_empty(&gh->gh_list)) {
1240 if (gh->gh_error == GLR_CANCELED) {
1241 spin_unlock(&gl->gl_spin);
1243 if (gfs2_glock_nq(gh))
1250 spin_unlock(&gl->gl_spin);
1256 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1257 * @gh: the holder structure
1259 * Returns: 0, GLR_TRYFAILED, or errno on failure
1262 int gfs2_glock_wait(struct gfs2_holder *gh)
1266 error = glock_wait_internal(gh);
1267 if (error == GLR_CANCELED) {
1269 gh->gh_flags &= ~GL_ASYNC;
1270 error = gfs2_glock_nq(gh);
1277 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1278 * @gh: the glock holder
1282 void gfs2_glock_dq(struct gfs2_holder *gh)
1284 struct gfs2_glock *gl = gh->gh_gl;
1285 const struct gfs2_glock_operations *glops = gl->gl_ops;
1287 if (gh->gh_flags & GL_NOCACHE)
1288 handle_callback(gl, LM_ST_UNLOCKED);
1290 gfs2_glmutex_lock(gl);
1292 spin_lock(&gl->gl_spin);
1293 list_del_init(&gh->gh_list);
1295 if (list_empty(&gl->gl_holders)) {
1296 spin_unlock(&gl->gl_spin);
1298 if (glops->go_unlock)
1299 glops->go_unlock(gh);
1301 gl->gl_stamp = jiffies;
1303 spin_lock(&gl->gl_spin);
1306 clear_bit(GLF_LOCK, &gl->gl_flags);
1308 spin_unlock(&gl->gl_spin);
1312 * gfs2_glock_prefetch - Try to prefetch a glock
1314 * @state: the state to prefetch in
1315 * @flags: flags passed to go_xmote_th()
1319 static void gfs2_glock_prefetch(struct gfs2_glock *gl, unsigned int state,
1322 const struct gfs2_glock_operations *glops = gl->gl_ops;
1324 spin_lock(&gl->gl_spin);
1326 if (test_bit(GLF_LOCK, &gl->gl_flags) ||
1327 !list_empty(&gl->gl_holders) ||
1328 !list_empty(&gl->gl_waiters1) ||
1329 !list_empty(&gl->gl_waiters2) ||
1330 !list_empty(&gl->gl_waiters3) ||
1331 relaxed_state_ok(gl->gl_state, state, flags)) {
1332 spin_unlock(&gl->gl_spin);
1336 set_bit(GLF_PREFETCH, &gl->gl_flags);
1337 set_bit(GLF_LOCK, &gl->gl_flags);
1338 spin_unlock(&gl->gl_spin);
1340 glops->go_xmote_th(gl, state, flags);
1343 static void greedy_work(void *data)
1345 struct greedy *gr = data;
1346 struct gfs2_holder *gh = &gr->gr_gh;
1347 struct gfs2_glock *gl = gh->gh_gl;
1348 const struct gfs2_glock_operations *glops = gl->gl_ops;
1350 clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1352 if (glops->go_greedy)
1353 glops->go_greedy(gl);
1355 spin_lock(&gl->gl_spin);
1357 if (list_empty(&gl->gl_waiters2)) {
1358 clear_bit(GLF_GREEDY, &gl->gl_flags);
1359 spin_unlock(&gl->gl_spin);
1360 gfs2_holder_uninit(gh);
1363 gfs2_glock_hold(gl);
1364 list_add_tail(&gh->gh_list, &gl->gl_waiters2);
1366 spin_unlock(&gl->gl_spin);
1372 * gfs2_glock_be_greedy -
1376 * Returns: 0 if go_greedy will be called, 1 otherwise
1379 int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time)
1382 struct gfs2_holder *gh;
1384 if (!time || gl->gl_sbd->sd_args.ar_localcaching ||
1385 test_and_set_bit(GLF_GREEDY, &gl->gl_flags))
1388 gr = kmalloc(sizeof(struct greedy), GFP_KERNEL);
1390 clear_bit(GLF_GREEDY, &gl->gl_flags);
1395 gfs2_holder_init(gl, 0, 0, gh);
1396 set_bit(HIF_GREEDY, &gh->gh_iflags);
1397 INIT_WORK(&gr->gr_work, greedy_work, gr);
1399 set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1400 schedule_delayed_work(&gr->gr_work, time);
1406 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1407 * @gh: the holder structure
1411 void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1414 gfs2_holder_uninit(gh);
1418 * gfs2_glock_nq_num - acquire a glock based on lock number
1419 * @sdp: the filesystem
1420 * @number: the lock number
1421 * @glops: the glock operations for the type of glock
1422 * @state: the state to acquire the glock in
1423 * @flags: modifier flags for the aquisition
1424 * @gh: the struct gfs2_holder
1429 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, uint64_t number,
1430 const struct gfs2_glock_operations *glops,
1431 unsigned int state, int flags, struct gfs2_holder *gh)
1433 struct gfs2_glock *gl;
1436 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1438 error = gfs2_glock_nq_init(gl, state, flags, gh);
1446 * glock_compare - Compare two struct gfs2_glock structures for sorting
1447 * @arg_a: the first structure
1448 * @arg_b: the second structure
1452 static int glock_compare(const void *arg_a, const void *arg_b)
1454 struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
1455 struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
1456 struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1457 struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1460 if (a->ln_number > b->ln_number)
1462 else if (a->ln_number < b->ln_number)
1465 if (gh_a->gh_state == LM_ST_SHARED &&
1466 gh_b->gh_state == LM_ST_EXCLUSIVE)
1468 else if (!(gh_a->gh_flags & GL_LOCAL_EXCL) &&
1469 (gh_b->gh_flags & GL_LOCAL_EXCL))
1477 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1478 * @num_gh: the number of structures
1479 * @ghs: an array of struct gfs2_holder structures
1481 * Returns: 0 on success (all glocks acquired),
1482 * errno on failure (no glocks acquired)
1485 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1486 struct gfs2_holder **p)
1491 for (x = 0; x < num_gh; x++)
1494 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1496 for (x = 0; x < num_gh; x++) {
1497 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1499 error = gfs2_glock_nq(p[x]);
1502 gfs2_glock_dq(p[x]);
1511 * gfs2_glock_nq_m - acquire multiple glocks
1512 * @num_gh: the number of structures
1513 * @ghs: an array of struct gfs2_holder structures
1515 * Figure out how big an impact this function has. Either:
1516 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1517 * 2) Forget async stuff and just call nq_m_sync()
1518 * 3) Leave it like it is
1520 * Returns: 0 on success (all glocks acquired),
1521 * errno on failure (no glocks acquired)
1524 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1528 int borked = 0, serious = 0;
1535 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1536 return gfs2_glock_nq(ghs);
1539 e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1543 for (x = 0; x < num_gh; x++) {
1544 ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
1545 error = gfs2_glock_nq(&ghs[x]);
1554 for (x = 0; x < num_gh; x++) {
1555 error = e[x] = glock_wait_internal(&ghs[x]);
1558 if (error != GLR_TRYFAILED && error != GLR_CANCELED)
1568 for (x = 0; x < num_gh; x++)
1570 gfs2_glock_dq(&ghs[x]);
1575 for (x = 0; x < num_gh; x++)
1576 gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
1578 error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
1587 * gfs2_glock_dq_m - release multiple glocks
1588 * @num_gh: the number of structures
1589 * @ghs: an array of struct gfs2_holder structures
1593 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1597 for (x = 0; x < num_gh; x++)
1598 gfs2_glock_dq(&ghs[x]);
1602 * gfs2_glock_dq_uninit_m - release multiple glocks
1603 * @num_gh: the number of structures
1604 * @ghs: an array of struct gfs2_holder structures
1608 void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1612 for (x = 0; x < num_gh; x++)
1613 gfs2_glock_dq_uninit(&ghs[x]);
1617 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1618 * @sdp: the filesystem
1619 * @number: the lock number
1620 * @glops: the glock operations for the type of glock
1621 * @state: the state to acquire the glock in
1622 * @flags: modifier flags for the aquisition
1627 void gfs2_glock_prefetch_num(struct gfs2_sbd *sdp, uint64_t number,
1628 const struct gfs2_glock_operations *glops,
1629 unsigned int state, int flags)
1631 struct gfs2_glock *gl;
1634 if (atomic_read(&sdp->sd_reclaim_count) <
1635 gfs2_tune_get(sdp, gt_reclaim_limit)) {
1636 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1638 gfs2_glock_prefetch(gl, state, flags);
1645 * gfs2_lvb_hold - attach a LVB from a glock
1646 * @gl: The glock in question
1650 int gfs2_lvb_hold(struct gfs2_glock *gl)
1654 gfs2_glmutex_lock(gl);
1656 if (!atomic_read(&gl->gl_lvb_count)) {
1657 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1659 gfs2_glmutex_unlock(gl);
1662 gfs2_glock_hold(gl);
1664 atomic_inc(&gl->gl_lvb_count);
1666 gfs2_glmutex_unlock(gl);
1672 * gfs2_lvb_unhold - detach a LVB from a glock
1673 * @gl: The glock in question
1677 void gfs2_lvb_unhold(struct gfs2_glock *gl)
1679 gfs2_glock_hold(gl);
1680 gfs2_glmutex_lock(gl);
1682 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1683 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1684 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1689 gfs2_glmutex_unlock(gl);
1694 void gfs2_lvb_sync(struct gfs2_glock *gl)
1696 gfs2_glmutex_lock(gl);
1698 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count));
1699 if (!gfs2_assert_warn(gl->gl_sbd, gfs2_glock_is_held_excl(gl)))
1700 gfs2_lm_sync_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1702 gfs2_glmutex_unlock(gl);
1706 static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1709 struct gfs2_glock *gl;
1711 gl = gfs2_glock_find(sdp, name);
1715 if (gl->gl_ops->go_callback)
1716 gl->gl_ops->go_callback(gl, state);
1717 handle_callback(gl, state);
1719 spin_lock(&gl->gl_spin);
1721 spin_unlock(&gl->gl_spin);
1727 * gfs2_glock_cb - Callback used by locking module
1728 * @fsdata: Pointer to the superblock
1729 * @type: Type of callback
1730 * @data: Type dependent data pointer
1732 * Called by the locking module when it wants to tell us something.
1733 * Either we need to drop a lock, one of our ASYNC requests completed, or
1734 * a journal from another client needs to be recovered.
1737 void gfs2_glock_cb(lm_fsdata_t *fsdata, unsigned int type, void *data)
1739 struct gfs2_sbd *sdp = (struct gfs2_sbd *)fsdata;
1743 blocking_cb(sdp, data, LM_ST_UNLOCKED);
1747 blocking_cb(sdp, data, LM_ST_DEFERRED);
1751 blocking_cb(sdp, data, LM_ST_SHARED);
1755 struct lm_async_cb *async = data;
1756 struct gfs2_glock *gl;
1758 gl = gfs2_glock_find(sdp, &async->lc_name);
1759 if (gfs2_assert_warn(sdp, gl))
1761 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1762 gl->gl_req_bh(gl, async->lc_ret);
1767 case LM_CB_NEED_RECOVERY:
1768 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1769 if (sdp->sd_recoverd_process)
1770 wake_up_process(sdp->sd_recoverd_process);
1773 case LM_CB_DROPLOCKS:
1774 gfs2_gl_hash_clear(sdp, NO_WAIT);
1775 gfs2_quota_scan(sdp);
1779 gfs2_assert_warn(sdp, 0);
1785 * gfs2_iopen_go_callback - Try to kick the inode/vnode associated with an
1786 * iopen glock from memory
1787 * @io_gl: the iopen glock
1788 * @state: the state into which the glock should be put
1792 void gfs2_iopen_go_callback(struct gfs2_glock *io_gl, unsigned int state)
1795 if (state != LM_ST_UNLOCKED)
1797 /* FIXME: remove this? */
1801 * demote_ok - Check to see if it's ok to unlock a glock
1804 * Returns: 1 if it's ok
1807 static int demote_ok(struct gfs2_glock *gl)
1809 struct gfs2_sbd *sdp = gl->gl_sbd;
1810 const struct gfs2_glock_operations *glops = gl->gl_ops;
1813 if (test_bit(GLF_STICKY, &gl->gl_flags))
1815 else if (test_bit(GLF_PREFETCH, &gl->gl_flags))
1816 demote = time_after_eq(jiffies,
1818 gfs2_tune_get(sdp, gt_prefetch_secs) * HZ);
1819 else if (glops->go_demote_ok)
1820 demote = glops->go_demote_ok(gl);
1826 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1831 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1833 struct gfs2_sbd *sdp = gl->gl_sbd;
1835 spin_lock(&sdp->sd_reclaim_lock);
1836 if (list_empty(&gl->gl_reclaim)) {
1837 gfs2_glock_hold(gl);
1838 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1839 atomic_inc(&sdp->sd_reclaim_count);
1841 spin_unlock(&sdp->sd_reclaim_lock);
1843 wake_up(&sdp->sd_reclaim_wq);
1847 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1848 * @sdp: the filesystem
1850 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1851 * different glock and we notice that there are a lot of glocks in the
1856 void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1858 struct gfs2_glock *gl;
1860 spin_lock(&sdp->sd_reclaim_lock);
1861 if (list_empty(&sdp->sd_reclaim_list)) {
1862 spin_unlock(&sdp->sd_reclaim_lock);
1865 gl = list_entry(sdp->sd_reclaim_list.next,
1866 struct gfs2_glock, gl_reclaim);
1867 list_del_init(&gl->gl_reclaim);
1868 spin_unlock(&sdp->sd_reclaim_lock);
1870 atomic_dec(&sdp->sd_reclaim_count);
1871 atomic_inc(&sdp->sd_reclaimed);
1873 if (gfs2_glmutex_trylock(gl)) {
1874 if (queue_empty(gl, &gl->gl_holders) &&
1875 gl->gl_state != LM_ST_UNLOCKED &&
1877 handle_callback(gl, LM_ST_UNLOCKED);
1878 gfs2_glmutex_unlock(gl);
1885 * examine_bucket - Call a function for glock in a hash bucket
1886 * @examiner: the function
1887 * @sdp: the filesystem
1888 * @bucket: the bucket
1890 * Returns: 1 if the bucket has entries
1893 static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
1894 struct gfs2_gl_hash_bucket *bucket)
1896 struct glock_plug plug;
1897 struct list_head *tmp;
1898 struct gfs2_glock *gl;
1901 /* Add "plug" to end of bucket list, work back up list from there */
1902 memset(&plug.gl_flags, 0, sizeof(unsigned long));
1903 set_bit(GLF_PLUG, &plug.gl_flags);
1905 write_lock(&bucket->hb_lock);
1906 list_add(&plug.gl_list, &bucket->hb_list);
1907 write_unlock(&bucket->hb_lock);
1910 write_lock(&bucket->hb_lock);
1913 tmp = plug.gl_list.next;
1915 if (tmp == &bucket->hb_list) {
1916 list_del(&plug.gl_list);
1917 entries = !list_empty(&bucket->hb_list);
1918 write_unlock(&bucket->hb_lock);
1921 gl = list_entry(tmp, struct gfs2_glock, gl_list);
1923 /* Move plug up list */
1924 list_move(&plug.gl_list, &gl->gl_list);
1926 if (test_bit(GLF_PLUG, &gl->gl_flags))
1929 /* examiner() must glock_put() */
1930 gfs2_glock_hold(gl);
1935 write_unlock(&bucket->hb_lock);
1942 * scan_glock - look at a glock and see if we can reclaim it
1943 * @gl: the glock to look at
1947 static void scan_glock(struct gfs2_glock *gl)
1949 if (gl->gl_ops == &gfs2_inode_glops)
1952 if (gfs2_glmutex_trylock(gl)) {
1953 if (queue_empty(gl, &gl->gl_holders) &&
1954 gl->gl_state != LM_ST_UNLOCKED &&
1957 gfs2_glmutex_unlock(gl);
1964 gfs2_glmutex_unlock(gl);
1965 gfs2_glock_schedule_for_reclaim(gl);
1970 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
1971 * @sdp: the filesystem
1975 void gfs2_scand_internal(struct gfs2_sbd *sdp)
1979 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
1980 examine_bucket(scan_glock, sdp, &sdp->sd_gl_hash[x]);
1986 * clear_glock - look at a glock and see if we can free it from glock cache
1987 * @gl: the glock to look at
1991 static void clear_glock(struct gfs2_glock *gl)
1993 struct gfs2_sbd *sdp = gl->gl_sbd;
1996 spin_lock(&sdp->sd_reclaim_lock);
1997 if (!list_empty(&gl->gl_reclaim)) {
1998 list_del_init(&gl->gl_reclaim);
1999 atomic_dec(&sdp->sd_reclaim_count);
2000 spin_unlock(&sdp->sd_reclaim_lock);
2001 released = gfs2_glock_put(gl);
2002 gfs2_assert(sdp, !released);
2004 spin_unlock(&sdp->sd_reclaim_lock);
2007 if (gfs2_glmutex_trylock(gl)) {
2008 if (queue_empty(gl, &gl->gl_holders) &&
2009 gl->gl_state != LM_ST_UNLOCKED)
2010 handle_callback(gl, LM_ST_UNLOCKED);
2012 gfs2_glmutex_unlock(gl);
2019 * gfs2_gl_hash_clear - Empty out the glock hash table
2020 * @sdp: the filesystem
2021 * @wait: wait until it's all gone
2023 * Called when unmounting the filesystem, or when inter-node lock manager
2024 * requests DROPLOCKS because it is running out of capacity.
2027 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
2038 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
2039 if (examine_bucket(clear_glock, sdp,
2040 &sdp->sd_gl_hash[x]))
2046 if (time_after_eq(jiffies,
2047 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
2048 fs_warn(sdp, "Unmount seems to be stalled. "
2049 "Dumping lock state...\n");
2050 gfs2_dump_lockstate(sdp);
2054 invalidate_inodes(sdp->sd_vfs);
2060 * Diagnostic routines to help debug distributed deadlock
2064 * dump_holder - print information about a glock holder
2065 * @str: a string naming the type of holder
2066 * @gh: the glock holder
2068 * Returns: 0 on success, -ENOBUFS when we run out of space
2071 static int dump_holder(char *str, struct gfs2_holder *gh)
2074 int error = -ENOBUFS;
2076 printk(KERN_INFO " %s\n", str);
2077 printk(KERN_INFO " owner = %ld\n",
2078 (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
2079 printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
2080 printk(KERN_INFO " gh_flags =");
2081 for (x = 0; x < 32; x++)
2082 if (gh->gh_flags & (1 << x))
2085 printk(KERN_INFO " error = %d\n", gh->gh_error);
2086 printk(KERN_INFO " gh_iflags =");
2087 for (x = 0; x < 32; x++)
2088 if (test_bit(x, &gh->gh_iflags))
2091 print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip);
2099 * dump_inode - print information about an inode
2102 * Returns: 0 on success, -ENOBUFS when we run out of space
2105 static int dump_inode(struct gfs2_inode *ip)
2108 int error = -ENOBUFS;
2110 printk(KERN_INFO " Inode:\n");
2111 printk(KERN_INFO " num = %llu %llu\n",
2112 (unsigned long long)ip->i_num.no_formal_ino,
2113 (unsigned long long)ip->i_num.no_addr);
2114 printk(KERN_INFO " type = %u\n", IF2DT(ip->i_di.di_mode));
2115 printk(KERN_INFO " i_flags =");
2116 for (x = 0; x < 32; x++)
2117 if (test_bit(x, &ip->i_flags))
2127 * dump_glock - print information about a glock
2129 * @count: where we are in the buffer
2131 * Returns: 0 on success, -ENOBUFS when we run out of space
2134 static int dump_glock(struct gfs2_glock *gl)
2136 struct gfs2_holder *gh;
2138 int error = -ENOBUFS;
2140 spin_lock(&gl->gl_spin);
2142 printk(KERN_INFO "Glock 0x%p (%u, %llu)\n",
2144 gl->gl_name.ln_type,
2145 (unsigned long long)gl->gl_name.ln_number);
2146 printk(KERN_INFO " gl_flags =");
2147 for (x = 0; x < 32; x++)
2148 if (test_bit(x, &gl->gl_flags))
2151 printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref.refcount));
2152 printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
2153 printk(KERN_INFO " gl_owner = %s\n", gl->gl_owner->comm);
2154 print_symbol(KERN_INFO " gl_ip = %s\n", gl->gl_ip);
2155 printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
2156 printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
2157 printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
2158 printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
2159 printk(KERN_INFO " le = %s\n",
2160 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
2161 printk(KERN_INFO " reclaim = %s\n",
2162 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
2164 printk(KERN_INFO " aspace = 0x%p nrpages = %lu\n",
2166 gl->gl_aspace->i_mapping->nrpages);
2168 printk(KERN_INFO " aspace = no\n");
2169 printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
2170 if (gl->gl_req_gh) {
2171 error = dump_holder("Request", gl->gl_req_gh);
2175 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
2176 error = dump_holder("Holder", gh);
2180 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
2181 error = dump_holder("Waiter1", gh);
2185 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
2186 error = dump_holder("Waiter2", gh);
2190 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
2191 error = dump_holder("Waiter3", gh);
2195 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
2196 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
2197 list_empty(&gl->gl_holders)) {
2198 error = dump_inode(gl->gl_object);
2203 printk(KERN_INFO " Inode: busy\n");
2210 spin_unlock(&gl->gl_spin);
2216 * gfs2_dump_lockstate - print out the current lockstate
2217 * @sdp: the filesystem
2218 * @ub: the buffer to copy the information into
2220 * If @ub is NULL, dump the lockstate to the console.
2224 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
2226 struct gfs2_gl_hash_bucket *bucket;
2227 struct gfs2_glock *gl;
2231 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2232 bucket = &sdp->sd_gl_hash[x];
2234 read_lock(&bucket->hb_lock);
2236 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
2237 if (test_bit(GLF_PLUG, &gl->gl_flags))
2240 error = dump_glock(gl);
2245 read_unlock(&bucket->hb_lock);