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/kallsyms.h>
19 #include <linux/gfs2_ondisk.h>
20 #include <linux/list.h>
21 #include <linux/lm_interface.h>
22 #include <linux/wait.h>
23 #include <linux/module.h>
24 #include <linux/rwsem.h>
25 #include <asm/uaccess.h>
26 #include <linux/seq_file.h>
27 #include <linux/debugfs.h>
28 #include <linux/kthread.h>
29 #include <linux/freezer.h>
43 struct gfs2_gl_hash_bucket {
44 struct hlist_head hb_list;
48 int hash; /* hash bucket index */
49 struct gfs2_sbd *sdp; /* incore superblock */
50 struct gfs2_glock *gl; /* current glock struct */
51 struct seq_file *seq; /* sequence file for debugfs */
52 char string[512]; /* scratch space */
55 typedef void (*glock_examiner) (struct gfs2_glock * gl);
57 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
58 static int dump_glock(struct glock_iter *gi, struct gfs2_glock *gl);
59 static void gfs2_glock_xmote_th(struct gfs2_glock *gl, struct gfs2_holder *gh);
60 static void gfs2_glock_drop_th(struct gfs2_glock *gl);
61 static DECLARE_RWSEM(gfs2_umount_flush_sem);
62 static struct dentry *gfs2_root;
63 static struct task_struct *scand_process;
64 static unsigned int scand_secs = 5;
66 #define GFS2_GL_HASH_SHIFT 15
67 #define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT)
68 #define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1)
70 static struct gfs2_gl_hash_bucket gl_hash_table[GFS2_GL_HASH_SIZE];
71 static struct dentry *gfs2_root;
74 * Despite what you might think, the numbers below are not arbitrary :-)
75 * They are taken from the ipv4 routing hash code, which is well tested
76 * and thus should be nearly optimal. Later on we might tweek the numbers
77 * but for now this should be fine.
79 * The reason for putting the locks in a separate array from the list heads
80 * is that we can have fewer locks than list heads and save memory. We use
81 * the same hash function for both, but with a different hash mask.
83 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
84 defined(CONFIG_PROVE_LOCKING)
87 # define GL_HASH_LOCK_SZ 256
90 # define GL_HASH_LOCK_SZ 4096
92 # define GL_HASH_LOCK_SZ 2048
94 # define GL_HASH_LOCK_SZ 1024
96 # define GL_HASH_LOCK_SZ 512
98 # define GL_HASH_LOCK_SZ 256
102 /* We never want more locks than chains */
103 #if GFS2_GL_HASH_SIZE < GL_HASH_LOCK_SZ
104 # undef GL_HASH_LOCK_SZ
105 # define GL_HASH_LOCK_SZ GFS2_GL_HASH_SIZE
108 static rwlock_t gl_hash_locks[GL_HASH_LOCK_SZ];
110 static inline rwlock_t *gl_lock_addr(unsigned int x)
112 return &gl_hash_locks[x & (GL_HASH_LOCK_SZ-1)];
114 #else /* not SMP, so no spinlocks required */
115 static inline rwlock_t *gl_lock_addr(unsigned int x)
122 * relaxed_state_ok - is a requested lock compatible with the current lock mode?
123 * @actual: the current state of the lock
124 * @requested: the lock state that was requested by the caller
125 * @flags: the modifier flags passed in by the caller
127 * Returns: 1 if the locks are compatible, 0 otherwise
130 static inline int relaxed_state_ok(unsigned int actual, unsigned requested,
133 if (actual == requested)
136 if (flags & GL_EXACT)
139 if (actual == LM_ST_EXCLUSIVE && requested == LM_ST_SHARED)
142 if (actual != LM_ST_UNLOCKED && (flags & LM_FLAG_ANY))
149 * gl_hash() - Turn glock number into hash bucket number
150 * @lock: The glock number
152 * Returns: The number of the corresponding hash bucket
155 static unsigned int gl_hash(const struct gfs2_sbd *sdp,
156 const struct lm_lockname *name)
160 h = jhash(&name->ln_number, sizeof(u64), 0);
161 h = jhash(&name->ln_type, sizeof(unsigned int), h);
162 h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
163 h &= GFS2_GL_HASH_MASK;
169 * glock_free() - Perform a few checks and then release struct gfs2_glock
170 * @gl: The glock to release
172 * Also calls lock module to release its internal structure for this glock.
176 static void glock_free(struct gfs2_glock *gl)
178 struct gfs2_sbd *sdp = gl->gl_sbd;
179 struct inode *aspace = gl->gl_aspace;
181 gfs2_lm_put_lock(sdp, gl->gl_lock);
184 gfs2_aspace_put(aspace);
186 kmem_cache_free(gfs2_glock_cachep, gl);
190 * gfs2_glock_hold() - increment reference count on glock
191 * @gl: The glock to hold
195 void gfs2_glock_hold(struct gfs2_glock *gl)
197 atomic_inc(&gl->gl_ref);
201 * gfs2_glock_put() - Decrement reference count on glock
202 * @gl: The glock to put
206 int gfs2_glock_put(struct gfs2_glock *gl)
209 struct gfs2_sbd *sdp = gl->gl_sbd;
211 write_lock(gl_lock_addr(gl->gl_hash));
212 if (atomic_dec_and_test(&gl->gl_ref)) {
213 hlist_del(&gl->gl_list);
214 write_unlock(gl_lock_addr(gl->gl_hash));
215 BUG_ON(spin_is_locked(&gl->gl_spin));
216 gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED);
217 gfs2_assert(sdp, list_empty(&gl->gl_reclaim));
218 gfs2_assert(sdp, list_empty(&gl->gl_holders));
219 gfs2_assert(sdp, list_empty(&gl->gl_waiters1));
220 gfs2_assert(sdp, list_empty(&gl->gl_waiters3));
225 write_unlock(gl_lock_addr(gl->gl_hash));
231 * search_bucket() - Find struct gfs2_glock by lock number
232 * @bucket: the bucket to search
233 * @name: The lock name
235 * Returns: NULL, or the struct gfs2_glock with the requested number
238 static struct gfs2_glock *search_bucket(unsigned int hash,
239 const struct gfs2_sbd *sdp,
240 const struct lm_lockname *name)
242 struct gfs2_glock *gl;
243 struct hlist_node *h;
245 hlist_for_each_entry(gl, h, &gl_hash_table[hash].hb_list, gl_list) {
246 if (!lm_name_equal(&gl->gl_name, name))
248 if (gl->gl_sbd != sdp)
251 atomic_inc(&gl->gl_ref);
260 * gfs2_glock_find() - Find glock by lock number
261 * @sdp: The GFS2 superblock
262 * @name: The lock name
264 * Returns: NULL, or the struct gfs2_glock with the requested number
267 static struct gfs2_glock *gfs2_glock_find(const struct gfs2_sbd *sdp,
268 const struct lm_lockname *name)
270 unsigned int hash = gl_hash(sdp, name);
271 struct gfs2_glock *gl;
273 read_lock(gl_lock_addr(hash));
274 gl = search_bucket(hash, sdp, name);
275 read_unlock(gl_lock_addr(hash));
281 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
282 * @sdp: The GFS2 superblock
283 * @number: the lock number
284 * @glops: The glock_operations to use
285 * @create: If 0, don't create the glock if it doesn't exist
286 * @glp: the glock is returned here
288 * This does not lock a glock, just finds/creates structures for one.
293 int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
294 const struct gfs2_glock_operations *glops, int create,
295 struct gfs2_glock **glp)
297 struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
298 struct gfs2_glock *gl, *tmp;
299 unsigned int hash = gl_hash(sdp, &name);
302 read_lock(gl_lock_addr(hash));
303 gl = search_bucket(hash, sdp, &name);
304 read_unlock(gl_lock_addr(hash));
311 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
317 atomic_set(&gl->gl_ref, 1);
318 gl->gl_state = LM_ST_UNLOCKED;
320 gl->gl_owner_pid = 0;
323 gl->gl_req_gh = NULL;
324 gl->gl_req_bh = NULL;
326 gl->gl_stamp = jiffies;
327 gl->gl_object = NULL;
329 gl->gl_aspace = NULL;
330 lops_init_le(&gl->gl_le, &gfs2_glock_lops);
332 /* If this glock protects actual on-disk data or metadata blocks,
333 create a VFS inode to manage the pages/buffers holding them. */
334 if (glops == &gfs2_inode_glops || glops == &gfs2_rgrp_glops) {
335 gl->gl_aspace = gfs2_aspace_get(sdp);
336 if (!gl->gl_aspace) {
342 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
346 write_lock(gl_lock_addr(hash));
347 tmp = search_bucket(hash, sdp, &name);
349 write_unlock(gl_lock_addr(hash));
353 hlist_add_head(&gl->gl_list, &gl_hash_table[hash].hb_list);
354 write_unlock(gl_lock_addr(hash));
363 gfs2_aspace_put(gl->gl_aspace);
365 kmem_cache_free(gfs2_glock_cachep, gl);
370 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
372 * @state: the state we're requesting
373 * @flags: the modifier flags
374 * @gh: the holder structure
378 void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
379 struct gfs2_holder *gh)
381 INIT_LIST_HEAD(&gh->gh_list);
383 gh->gh_ip = (unsigned long)__builtin_return_address(0);
384 gh->gh_owner_pid = current->pid;
385 gh->gh_state = state;
386 gh->gh_flags = flags;
393 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
394 * @state: the state we're requesting
395 * @flags: the modifier flags
396 * @gh: the holder structure
398 * Don't mess with the glock.
402 void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
404 gh->gh_state = state;
405 gh->gh_flags = flags;
407 gh->gh_ip = (unsigned long)__builtin_return_address(0);
411 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
412 * @gh: the holder structure
416 void gfs2_holder_uninit(struct gfs2_holder *gh)
418 gfs2_glock_put(gh->gh_gl);
423 static void gfs2_holder_wake(struct gfs2_holder *gh)
425 clear_bit(HIF_WAIT, &gh->gh_iflags);
426 smp_mb__after_clear_bit();
427 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
430 static int just_schedule(void *word)
436 static void wait_on_holder(struct gfs2_holder *gh)
439 wait_on_bit(&gh->gh_iflags, HIF_WAIT, just_schedule, TASK_UNINTERRUPTIBLE);
442 static void gfs2_demote_wake(struct gfs2_glock *gl)
444 clear_bit(GLF_DEMOTE, &gl->gl_flags);
445 smp_mb__after_clear_bit();
446 wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
449 static void wait_on_demote(struct gfs2_glock *gl)
452 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, just_schedule, TASK_UNINTERRUPTIBLE);
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 clear_bit(HIF_WAIT, &gh->gh_iflags);
471 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
477 * rq_promote - process a promote request in the queue
478 * @gh: the glock holder
480 * Acquire a new inter-node lock, or change a lock state to more restrictive.
482 * Returns: 1 if the queue is blocked
485 static int rq_promote(struct gfs2_holder *gh)
487 struct gfs2_glock *gl = gh->gh_gl;
488 struct gfs2_sbd *sdp = gl->gl_sbd;
490 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
491 if (list_empty(&gl->gl_holders)) {
493 set_bit(GLF_LOCK, &gl->gl_flags);
494 spin_unlock(&gl->gl_spin);
496 if (atomic_read(&sdp->sd_reclaim_count) >
497 gfs2_tune_get(sdp, gt_reclaim_limit) &&
498 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
499 gfs2_reclaim_glock(sdp);
500 gfs2_reclaim_glock(sdp);
503 gfs2_glock_xmote_th(gh->gh_gl, gh);
504 spin_lock(&gl->gl_spin);
509 if (list_empty(&gl->gl_holders)) {
510 set_bit(HIF_FIRST, &gh->gh_iflags);
511 set_bit(GLF_LOCK, &gl->gl_flags);
513 struct gfs2_holder *next_gh;
514 if (gh->gh_state == LM_ST_EXCLUSIVE)
516 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
518 if (next_gh->gh_state == LM_ST_EXCLUSIVE)
522 list_move_tail(&gh->gh_list, &gl->gl_holders);
524 set_bit(HIF_HOLDER, &gh->gh_iflags);
526 gfs2_holder_wake(gh);
532 * rq_demote - process a demote request in the queue
533 * @gh: the glock holder
535 * Returns: 1 if the queue is blocked
538 static int rq_demote(struct gfs2_glock *gl)
540 if (!list_empty(&gl->gl_holders))
543 if (gl->gl_state == gl->gl_demote_state ||
544 gl->gl_state == LM_ST_UNLOCKED) {
545 gfs2_demote_wake(gl);
548 set_bit(GLF_LOCK, &gl->gl_flags);
549 if (gl->gl_demote_state == LM_ST_UNLOCKED ||
550 gl->gl_state != LM_ST_EXCLUSIVE) {
551 spin_unlock(&gl->gl_spin);
552 gfs2_glock_drop_th(gl);
554 spin_unlock(&gl->gl_spin);
555 gfs2_glock_xmote_th(gl, NULL);
557 spin_lock(&gl->gl_spin);
563 * run_queue - process holder structures on a glock
567 static void run_queue(struct gfs2_glock *gl)
569 struct gfs2_holder *gh;
573 if (test_bit(GLF_LOCK, &gl->gl_flags))
576 if (!list_empty(&gl->gl_waiters1)) {
577 gh = list_entry(gl->gl_waiters1.next,
578 struct gfs2_holder, gh_list);
580 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
581 blocked = rq_mutex(gh);
583 gfs2_assert_warn(gl->gl_sbd, 0);
585 } else if (test_bit(GLF_DEMOTE, &gl->gl_flags)) {
586 blocked = rq_demote(gl);
587 } else if (!list_empty(&gl->gl_waiters3)) {
588 gh = list_entry(gl->gl_waiters3.next,
589 struct gfs2_holder, gh_list);
591 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
592 blocked = rq_promote(gh);
594 gfs2_assert_warn(gl->gl_sbd, 0);
605 * gfs2_glmutex_lock - acquire a local lock on a glock
608 * Gives caller exclusive access to manipulate a glock structure.
611 static void gfs2_glmutex_lock(struct gfs2_glock *gl)
613 struct gfs2_holder gh;
615 gfs2_holder_init(gl, 0, 0, &gh);
616 set_bit(HIF_MUTEX, &gh.gh_iflags);
617 if (test_and_set_bit(HIF_WAIT, &gh.gh_iflags))
620 spin_lock(&gl->gl_spin);
621 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
622 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
624 gl->gl_owner_pid = current->pid;
625 gl->gl_ip = (unsigned long)__builtin_return_address(0);
626 clear_bit(HIF_WAIT, &gh.gh_iflags);
628 wake_up_bit(&gh.gh_iflags, HIF_WAIT);
630 spin_unlock(&gl->gl_spin);
633 gfs2_holder_uninit(&gh);
637 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
640 * Returns: 1 if the glock is acquired
643 static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
647 spin_lock(&gl->gl_spin);
648 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
651 gl->gl_owner_pid = current->pid;
652 gl->gl_ip = (unsigned long)__builtin_return_address(0);
654 spin_unlock(&gl->gl_spin);
660 * gfs2_glmutex_unlock - release a local lock on a glock
665 static void gfs2_glmutex_unlock(struct gfs2_glock *gl)
667 spin_lock(&gl->gl_spin);
668 clear_bit(GLF_LOCK, &gl->gl_flags);
669 gl->gl_owner_pid = 0;
672 BUG_ON(!spin_is_locked(&gl->gl_spin));
673 spin_unlock(&gl->gl_spin);
677 * handle_callback - process a demote request
679 * @state: the state the caller wants us to change to
681 * There are only two requests that we are going to see in actual
682 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
685 static void handle_callback(struct gfs2_glock *gl, unsigned int state, int remote)
687 spin_lock(&gl->gl_spin);
688 if (test_and_set_bit(GLF_DEMOTE, &gl->gl_flags) == 0) {
689 gl->gl_demote_state = state;
690 gl->gl_demote_time = jiffies;
691 if (remote && gl->gl_ops->go_type == LM_TYPE_IOPEN &&
693 struct inode *inode = igrab(gl->gl_object);
694 spin_unlock(&gl->gl_spin);
696 d_prune_aliases(inode);
701 } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
702 gl->gl_demote_state != state) {
703 gl->gl_demote_state = LM_ST_UNLOCKED;
705 spin_unlock(&gl->gl_spin);
709 * state_change - record that the glock is now in a different state
711 * @new_state the new state
715 static void state_change(struct gfs2_glock *gl, unsigned int new_state)
719 held1 = (gl->gl_state != LM_ST_UNLOCKED);
720 held2 = (new_state != LM_ST_UNLOCKED);
722 if (held1 != held2) {
729 gl->gl_state = new_state;
733 * xmote_bh - Called after the lock module is done acquiring a lock
734 * @gl: The glock in question
735 * @ret: the int returned from the lock module
739 static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
741 struct gfs2_sbd *sdp = gl->gl_sbd;
742 const struct gfs2_glock_operations *glops = gl->gl_ops;
743 struct gfs2_holder *gh = gl->gl_req_gh;
744 int prev_state = gl->gl_state;
747 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
748 gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
749 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
751 state_change(gl, ret & LM_OUT_ST_MASK);
753 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
755 glops->go_inval(gl, DIO_METADATA);
756 } else if (gl->gl_state == LM_ST_DEFERRED) {
757 /* We might not want to do this here.
758 Look at moving to the inode glops. */
760 glops->go_inval(gl, 0);
763 /* Deal with each possible exit condition */
766 gl->gl_stamp = jiffies;
767 if (ret & LM_OUT_CANCELED) {
770 spin_lock(&gl->gl_spin);
771 if (gl->gl_state != gl->gl_demote_state) {
772 gl->gl_req_bh = NULL;
773 spin_unlock(&gl->gl_spin);
774 gfs2_glock_drop_th(gl);
778 gfs2_demote_wake(gl);
779 spin_unlock(&gl->gl_spin);
782 spin_lock(&gl->gl_spin);
783 list_del_init(&gh->gh_list);
785 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
787 gh->gh_error = GLR_CANCELED;
788 if (ret & LM_OUT_CANCELED)
790 if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
791 list_add_tail(&gh->gh_list, &gl->gl_holders);
793 set_bit(HIF_HOLDER, &gh->gh_iflags);
794 set_bit(HIF_FIRST, &gh->gh_iflags);
798 gh->gh_error = GLR_TRYFAILED;
799 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
801 gh->gh_error = -EINVAL;
802 if (gfs2_assert_withdraw(sdp, 0) == -1)
803 fs_err(sdp, "ret = 0x%.8X\n", ret);
805 spin_unlock(&gl->gl_spin);
808 if (glops->go_xmote_bh)
809 glops->go_xmote_bh(gl);
812 spin_lock(&gl->gl_spin);
813 gl->gl_req_gh = NULL;
814 gl->gl_req_bh = NULL;
815 clear_bit(GLF_LOCK, &gl->gl_flags);
817 spin_unlock(&gl->gl_spin);
823 gfs2_holder_wake(gh);
827 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
828 * @gl: The glock in question
829 * @state: the requested state
830 * @flags: modifier flags to the lock call
834 static void gfs2_glock_xmote_th(struct gfs2_glock *gl, struct gfs2_holder *gh)
836 struct gfs2_sbd *sdp = gl->gl_sbd;
837 int flags = gh ? gh->gh_flags : 0;
838 unsigned state = gh ? gh->gh_state : gl->gl_demote_state;
839 const struct gfs2_glock_operations *glops = gl->gl_ops;
840 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
841 LM_FLAG_NOEXP | LM_FLAG_ANY |
843 unsigned int lck_ret;
845 if (glops->go_xmote_th)
846 glops->go_xmote_th(gl);
848 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
849 gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
850 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
851 gfs2_assert_warn(sdp, state != gl->gl_state);
854 gl->gl_req_bh = xmote_bh;
856 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state, lck_flags);
858 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
861 if (lck_ret & LM_OUT_ASYNC)
862 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
864 xmote_bh(gl, lck_ret);
868 * drop_bh - Called after a lock module unlock completes
870 * @ret: the return status
872 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
873 * Doesn't drop the reference on the glock the top half took out
877 static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
879 struct gfs2_sbd *sdp = gl->gl_sbd;
880 const struct gfs2_glock_operations *glops = gl->gl_ops;
881 struct gfs2_holder *gh = gl->gl_req_gh;
883 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
884 gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
885 gfs2_assert_warn(sdp, !ret);
887 state_change(gl, LM_ST_UNLOCKED);
888 gfs2_demote_wake(gl);
891 glops->go_inval(gl, DIO_METADATA);
894 spin_lock(&gl->gl_spin);
895 list_del_init(&gh->gh_list);
897 spin_unlock(&gl->gl_spin);
900 spin_lock(&gl->gl_spin);
901 gl->gl_req_gh = NULL;
902 gl->gl_req_bh = NULL;
903 clear_bit(GLF_LOCK, &gl->gl_flags);
905 spin_unlock(&gl->gl_spin);
910 gfs2_holder_wake(gh);
914 * gfs2_glock_drop_th - call into the lock module to unlock a lock
919 static void gfs2_glock_drop_th(struct gfs2_glock *gl)
921 struct gfs2_sbd *sdp = gl->gl_sbd;
922 const struct gfs2_glock_operations *glops = gl->gl_ops;
925 if (glops->go_drop_th)
926 glops->go_drop_th(gl);
928 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
929 gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
930 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
933 gl->gl_req_bh = drop_bh;
935 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
937 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
943 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
947 * do_cancels - cancel requests for locks stuck waiting on an expire flag
948 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
950 * Don't cancel GL_NOCANCEL requests.
953 static void do_cancels(struct gfs2_holder *gh)
955 struct gfs2_glock *gl = gh->gh_gl;
957 spin_lock(&gl->gl_spin);
959 while (gl->gl_req_gh != gh &&
960 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
961 !list_empty(&gh->gh_list)) {
962 if (gl->gl_req_bh && !(gl->gl_req_gh &&
963 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
964 spin_unlock(&gl->gl_spin);
965 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
967 spin_lock(&gl->gl_spin);
969 spin_unlock(&gl->gl_spin);
971 spin_lock(&gl->gl_spin);
975 spin_unlock(&gl->gl_spin);
979 * glock_wait_internal - wait on a glock acquisition
980 * @gh: the glock holder
982 * Returns: 0 on success
985 static int glock_wait_internal(struct gfs2_holder *gh)
987 struct gfs2_glock *gl = gh->gh_gl;
988 struct gfs2_sbd *sdp = gl->gl_sbd;
989 const struct gfs2_glock_operations *glops = gl->gl_ops;
991 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
994 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
995 spin_lock(&gl->gl_spin);
996 if (gl->gl_req_gh != gh &&
997 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
998 !list_empty(&gh->gh_list)) {
999 list_del_init(&gh->gh_list);
1000 gh->gh_error = GLR_TRYFAILED;
1002 spin_unlock(&gl->gl_spin);
1003 return gh->gh_error;
1005 spin_unlock(&gl->gl_spin);
1008 if (gh->gh_flags & LM_FLAG_PRIORITY)
1013 return gh->gh_error;
1015 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
1016 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state, gh->gh_state,
1019 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1020 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1022 if (glops->go_lock) {
1023 gh->gh_error = glops->go_lock(gh);
1025 spin_lock(&gl->gl_spin);
1026 list_del_init(&gh->gh_list);
1027 spin_unlock(&gl->gl_spin);
1031 spin_lock(&gl->gl_spin);
1032 gl->gl_req_gh = NULL;
1033 gl->gl_req_bh = NULL;
1034 clear_bit(GLF_LOCK, &gl->gl_flags);
1036 spin_unlock(&gl->gl_spin);
1039 return gh->gh_error;
1042 static inline struct gfs2_holder *
1043 find_holder_by_owner(struct list_head *head, pid_t pid)
1045 struct gfs2_holder *gh;
1047 list_for_each_entry(gh, head, gh_list) {
1048 if (gh->gh_owner_pid == pid)
1055 static void print_dbg(struct glock_iter *gi, const char *fmt, ...)
1059 va_start(args, fmt);
1061 vsprintf(gi->string, fmt, args);
1062 seq_printf(gi->seq, gi->string);
1070 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1071 * @gh: the holder structure to add
1075 static void add_to_queue(struct gfs2_holder *gh)
1077 struct gfs2_glock *gl = gh->gh_gl;
1078 struct gfs2_holder *existing;
1080 BUG_ON(!gh->gh_owner_pid);
1081 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
1084 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner_pid);
1086 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1087 printk(KERN_INFO "pid : %d\n", existing->gh_owner_pid);
1088 printk(KERN_INFO "lock type : %d lock state : %d\n",
1089 existing->gh_gl->gl_name.ln_type, existing->gh_gl->gl_state);
1090 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1091 printk(KERN_INFO "pid : %d\n", gh->gh_owner_pid);
1092 printk(KERN_INFO "lock type : %d lock state : %d\n",
1093 gl->gl_name.ln_type, gl->gl_state);
1097 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner_pid);
1099 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1100 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1104 if (gh->gh_flags & LM_FLAG_PRIORITY)
1105 list_add(&gh->gh_list, &gl->gl_waiters3);
1107 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1111 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1112 * @gh: the holder structure
1114 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1116 * Returns: 0, GLR_TRYFAILED, or errno on failure
1119 int gfs2_glock_nq(struct gfs2_holder *gh)
1121 struct gfs2_glock *gl = gh->gh_gl;
1122 struct gfs2_sbd *sdp = gl->gl_sbd;
1126 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1127 set_bit(HIF_ABORTED, &gh->gh_iflags);
1131 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1133 spin_lock(&gl->gl_spin);
1136 spin_unlock(&gl->gl_spin);
1138 if (!(gh->gh_flags & GL_ASYNC)) {
1139 error = glock_wait_internal(gh);
1140 if (error == GLR_CANCELED) {
1150 * gfs2_glock_poll - poll to see if an async request has been completed
1153 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1156 int gfs2_glock_poll(struct gfs2_holder *gh)
1158 struct gfs2_glock *gl = gh->gh_gl;
1161 spin_lock(&gl->gl_spin);
1163 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1165 else if (list_empty(&gh->gh_list)) {
1166 if (gh->gh_error == GLR_CANCELED) {
1167 spin_unlock(&gl->gl_spin);
1169 if (gfs2_glock_nq(gh))
1176 spin_unlock(&gl->gl_spin);
1182 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1183 * @gh: the holder structure
1185 * Returns: 0, GLR_TRYFAILED, or errno on failure
1188 int gfs2_glock_wait(struct gfs2_holder *gh)
1192 error = glock_wait_internal(gh);
1193 if (error == GLR_CANCELED) {
1195 gh->gh_flags &= ~GL_ASYNC;
1196 error = gfs2_glock_nq(gh);
1203 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1204 * @gh: the glock holder
1208 void gfs2_glock_dq(struct gfs2_holder *gh)
1210 struct gfs2_glock *gl = gh->gh_gl;
1211 const struct gfs2_glock_operations *glops = gl->gl_ops;
1213 if (gh->gh_flags & GL_NOCACHE)
1214 handle_callback(gl, LM_ST_UNLOCKED, 0);
1216 gfs2_glmutex_lock(gl);
1218 spin_lock(&gl->gl_spin);
1219 list_del_init(&gh->gh_list);
1221 if (list_empty(&gl->gl_holders)) {
1222 spin_unlock(&gl->gl_spin);
1224 if (glops->go_unlock)
1225 glops->go_unlock(gh);
1227 spin_lock(&gl->gl_spin);
1228 gl->gl_stamp = jiffies;
1231 clear_bit(GLF_LOCK, &gl->gl_flags);
1233 spin_unlock(&gl->gl_spin);
1236 void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1238 struct gfs2_glock *gl = gh->gh_gl;
1244 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1245 * @gh: the holder structure
1249 void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1252 gfs2_holder_uninit(gh);
1256 * gfs2_glock_nq_num - acquire a glock based on lock number
1257 * @sdp: the filesystem
1258 * @number: the lock number
1259 * @glops: the glock operations for the type of glock
1260 * @state: the state to acquire the glock in
1261 * @flags: modifier flags for the aquisition
1262 * @gh: the struct gfs2_holder
1267 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
1268 const struct gfs2_glock_operations *glops,
1269 unsigned int state, int flags, struct gfs2_holder *gh)
1271 struct gfs2_glock *gl;
1274 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1276 error = gfs2_glock_nq_init(gl, state, flags, gh);
1284 * glock_compare - Compare two struct gfs2_glock structures for sorting
1285 * @arg_a: the first structure
1286 * @arg_b: the second structure
1290 static int glock_compare(const void *arg_a, const void *arg_b)
1292 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1293 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1294 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1295 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1297 if (a->ln_number > b->ln_number)
1299 if (a->ln_number < b->ln_number)
1301 BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
1306 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1307 * @num_gh: the number of structures
1308 * @ghs: an array of struct gfs2_holder structures
1310 * Returns: 0 on success (all glocks acquired),
1311 * errno on failure (no glocks acquired)
1314 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1315 struct gfs2_holder **p)
1320 for (x = 0; x < num_gh; x++)
1323 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1325 for (x = 0; x < num_gh; x++) {
1326 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1328 error = gfs2_glock_nq(p[x]);
1331 gfs2_glock_dq(p[x]);
1340 * gfs2_glock_nq_m - acquire multiple glocks
1341 * @num_gh: the number of structures
1342 * @ghs: an array of struct gfs2_holder structures
1345 * Returns: 0 on success (all glocks acquired),
1346 * errno on failure (no glocks acquired)
1349 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1351 struct gfs2_holder *tmp[4];
1352 struct gfs2_holder **pph = tmp;
1359 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1360 return gfs2_glock_nq(ghs);
1364 pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS);
1369 error = nq_m_sync(num_gh, ghs, pph);
1378 * gfs2_glock_dq_m - release multiple glocks
1379 * @num_gh: the number of structures
1380 * @ghs: an array of struct gfs2_holder structures
1384 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1388 for (x = 0; x < num_gh; x++)
1389 gfs2_glock_dq(&ghs[x]);
1393 * gfs2_glock_dq_uninit_m - release multiple glocks
1394 * @num_gh: the number of structures
1395 * @ghs: an array of struct gfs2_holder structures
1399 void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1403 for (x = 0; x < num_gh; x++)
1404 gfs2_glock_dq_uninit(&ghs[x]);
1408 * gfs2_lvb_hold - attach a LVB from a glock
1409 * @gl: The glock in question
1413 int gfs2_lvb_hold(struct gfs2_glock *gl)
1417 gfs2_glmutex_lock(gl);
1419 if (!atomic_read(&gl->gl_lvb_count)) {
1420 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1422 gfs2_glmutex_unlock(gl);
1425 gfs2_glock_hold(gl);
1427 atomic_inc(&gl->gl_lvb_count);
1429 gfs2_glmutex_unlock(gl);
1435 * gfs2_lvb_unhold - detach a LVB from a glock
1436 * @gl: The glock in question
1440 void gfs2_lvb_unhold(struct gfs2_glock *gl)
1442 gfs2_glock_hold(gl);
1443 gfs2_glmutex_lock(gl);
1445 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1446 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1447 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1452 gfs2_glmutex_unlock(gl);
1456 static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1459 struct gfs2_glock *gl;
1461 gl = gfs2_glock_find(sdp, name);
1465 handle_callback(gl, state, 1);
1467 spin_lock(&gl->gl_spin);
1469 spin_unlock(&gl->gl_spin);
1475 * gfs2_glock_cb - Callback used by locking module
1476 * @sdp: Pointer to the superblock
1477 * @type: Type of callback
1478 * @data: Type dependent data pointer
1480 * Called by the locking module when it wants to tell us something.
1481 * Either we need to drop a lock, one of our ASYNC requests completed, or
1482 * a journal from another client needs to be recovered.
1485 void gfs2_glock_cb(void *cb_data, unsigned int type, void *data)
1487 struct gfs2_sbd *sdp = cb_data;
1491 blocking_cb(sdp, data, LM_ST_UNLOCKED);
1495 blocking_cb(sdp, data, LM_ST_DEFERRED);
1499 blocking_cb(sdp, data, LM_ST_SHARED);
1503 struct lm_async_cb *async = data;
1504 struct gfs2_glock *gl;
1506 down_read(&gfs2_umount_flush_sem);
1507 gl = gfs2_glock_find(sdp, &async->lc_name);
1508 if (gfs2_assert_warn(sdp, gl))
1510 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1511 gl->gl_req_bh(gl, async->lc_ret);
1513 up_read(&gfs2_umount_flush_sem);
1517 case LM_CB_NEED_RECOVERY:
1518 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1519 if (sdp->sd_recoverd_process)
1520 wake_up_process(sdp->sd_recoverd_process);
1523 case LM_CB_DROPLOCKS:
1524 gfs2_gl_hash_clear(sdp, NO_WAIT);
1525 gfs2_quota_scan(sdp);
1529 gfs2_assert_warn(sdp, 0);
1535 * demote_ok - Check to see if it's ok to unlock a glock
1538 * Returns: 1 if it's ok
1541 static int demote_ok(struct gfs2_glock *gl)
1543 const struct gfs2_glock_operations *glops = gl->gl_ops;
1546 if (test_bit(GLF_STICKY, &gl->gl_flags))
1548 else if (glops->go_demote_ok)
1549 demote = glops->go_demote_ok(gl);
1555 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1560 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1562 struct gfs2_sbd *sdp = gl->gl_sbd;
1564 spin_lock(&sdp->sd_reclaim_lock);
1565 if (list_empty(&gl->gl_reclaim)) {
1566 gfs2_glock_hold(gl);
1567 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1568 atomic_inc(&sdp->sd_reclaim_count);
1570 spin_unlock(&sdp->sd_reclaim_lock);
1572 wake_up(&sdp->sd_reclaim_wq);
1576 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1577 * @sdp: the filesystem
1579 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1580 * different glock and we notice that there are a lot of glocks in the
1585 void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1587 struct gfs2_glock *gl;
1589 spin_lock(&sdp->sd_reclaim_lock);
1590 if (list_empty(&sdp->sd_reclaim_list)) {
1591 spin_unlock(&sdp->sd_reclaim_lock);
1594 gl = list_entry(sdp->sd_reclaim_list.next,
1595 struct gfs2_glock, gl_reclaim);
1596 list_del_init(&gl->gl_reclaim);
1597 spin_unlock(&sdp->sd_reclaim_lock);
1599 atomic_dec(&sdp->sd_reclaim_count);
1600 atomic_inc(&sdp->sd_reclaimed);
1602 if (gfs2_glmutex_trylock(gl)) {
1603 if (list_empty(&gl->gl_holders) &&
1604 gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
1605 handle_callback(gl, LM_ST_UNLOCKED, 0);
1606 gfs2_glmutex_unlock(gl);
1613 * examine_bucket - Call a function for glock in a hash bucket
1614 * @examiner: the function
1615 * @sdp: the filesystem
1616 * @bucket: the bucket
1618 * Returns: 1 if the bucket has entries
1621 static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
1624 struct gfs2_glock *gl, *prev = NULL;
1625 int has_entries = 0;
1626 struct hlist_head *head = &gl_hash_table[hash].hb_list;
1628 read_lock(gl_lock_addr(hash));
1629 /* Can't use hlist_for_each_entry - don't want prefetch here */
1630 if (hlist_empty(head))
1632 gl = list_entry(head->first, struct gfs2_glock, gl_list);
1634 if (!sdp || gl->gl_sbd == sdp) {
1635 gfs2_glock_hold(gl);
1636 read_unlock(gl_lock_addr(hash));
1638 gfs2_glock_put(prev);
1642 read_lock(gl_lock_addr(hash));
1644 if (gl->gl_list.next == NULL)
1646 gl = list_entry(gl->gl_list.next, struct gfs2_glock, gl_list);
1649 read_unlock(gl_lock_addr(hash));
1651 gfs2_glock_put(prev);
1657 * scan_glock - look at a glock and see if we can reclaim it
1658 * @gl: the glock to look at
1662 static void scan_glock(struct gfs2_glock *gl)
1664 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object)
1667 if (gfs2_glmutex_trylock(gl)) {
1668 if (list_empty(&gl->gl_holders) &&
1669 gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
1671 gfs2_glmutex_unlock(gl);
1676 gfs2_glmutex_unlock(gl);
1677 gfs2_glock_schedule_for_reclaim(gl);
1681 * clear_glock - look at a glock and see if we can free it from glock cache
1682 * @gl: the glock to look at
1686 static void clear_glock(struct gfs2_glock *gl)
1688 struct gfs2_sbd *sdp = gl->gl_sbd;
1691 spin_lock(&sdp->sd_reclaim_lock);
1692 if (!list_empty(&gl->gl_reclaim)) {
1693 list_del_init(&gl->gl_reclaim);
1694 atomic_dec(&sdp->sd_reclaim_count);
1695 spin_unlock(&sdp->sd_reclaim_lock);
1696 released = gfs2_glock_put(gl);
1697 gfs2_assert(sdp, !released);
1699 spin_unlock(&sdp->sd_reclaim_lock);
1702 if (gfs2_glmutex_trylock(gl)) {
1703 if (list_empty(&gl->gl_holders) &&
1704 gl->gl_state != LM_ST_UNLOCKED)
1705 handle_callback(gl, LM_ST_UNLOCKED, 0);
1706 gfs2_glmutex_unlock(gl);
1711 * gfs2_gl_hash_clear - Empty out the glock hash table
1712 * @sdp: the filesystem
1713 * @wait: wait until it's all gone
1715 * Called when unmounting the filesystem, or when inter-node lock manager
1716 * requests DROPLOCKS because it is running out of capacity.
1719 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
1729 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
1730 if (examine_bucket(clear_glock, sdp, x))
1737 if (time_after_eq(jiffies,
1738 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
1739 fs_warn(sdp, "Unmount seems to be stalled. "
1740 "Dumping lock state...\n");
1741 gfs2_dump_lockstate(sdp);
1745 down_write(&gfs2_umount_flush_sem);
1746 invalidate_inodes(sdp->sd_vfs);
1747 up_write(&gfs2_umount_flush_sem);
1753 * Diagnostic routines to help debug distributed deadlock
1756 static void gfs2_print_symbol(struct glock_iter *gi, const char *fmt,
1757 unsigned long address)
1759 char buffer[KSYM_SYMBOL_LEN];
1761 sprint_symbol(buffer, address);
1762 print_dbg(gi, fmt, buffer);
1766 * dump_holder - print information about a glock holder
1767 * @str: a string naming the type of holder
1768 * @gh: the glock holder
1770 * Returns: 0 on success, -ENOBUFS when we run out of space
1773 static int dump_holder(struct glock_iter *gi, char *str,
1774 struct gfs2_holder *gh)
1777 struct task_struct *gh_owner;
1779 print_dbg(gi, " %s\n", str);
1780 if (gh->gh_owner_pid) {
1781 print_dbg(gi, " owner = %ld ", (long)gh->gh_owner_pid);
1782 gh_owner = find_task_by_pid(gh->gh_owner_pid);
1784 print_dbg(gi, "(%s)\n", gh_owner->comm);
1786 print_dbg(gi, "(ended)\n");
1788 print_dbg(gi, " owner = -1\n");
1789 print_dbg(gi, " gh_state = %u\n", gh->gh_state);
1790 print_dbg(gi, " gh_flags =");
1791 for (x = 0; x < 32; x++)
1792 if (gh->gh_flags & (1 << x))
1793 print_dbg(gi, " %u", x);
1794 print_dbg(gi, " \n");
1795 print_dbg(gi, " error = %d\n", gh->gh_error);
1796 print_dbg(gi, " gh_iflags =");
1797 for (x = 0; x < 32; x++)
1798 if (test_bit(x, &gh->gh_iflags))
1799 print_dbg(gi, " %u", x);
1800 print_dbg(gi, " \n");
1801 gfs2_print_symbol(gi, " initialized at: %s\n", gh->gh_ip);
1807 * dump_inode - print information about an inode
1810 * Returns: 0 on success, -ENOBUFS when we run out of space
1813 static int dump_inode(struct glock_iter *gi, struct gfs2_inode *ip)
1817 print_dbg(gi, " Inode:\n");
1818 print_dbg(gi, " num = %llu/%llu\n",
1819 (unsigned long long)ip->i_no_formal_ino,
1820 (unsigned long long)ip->i_no_addr);
1821 print_dbg(gi, " type = %u\n", IF2DT(ip->i_inode.i_mode));
1822 print_dbg(gi, " i_flags =");
1823 for (x = 0; x < 32; x++)
1824 if (test_bit(x, &ip->i_flags))
1825 print_dbg(gi, " %u", x);
1826 print_dbg(gi, " \n");
1831 * dump_glock - print information about a glock
1833 * @count: where we are in the buffer
1835 * Returns: 0 on success, -ENOBUFS when we run out of space
1838 static int dump_glock(struct glock_iter *gi, struct gfs2_glock *gl)
1840 struct gfs2_holder *gh;
1842 int error = -ENOBUFS;
1843 struct task_struct *gl_owner;
1845 spin_lock(&gl->gl_spin);
1847 print_dbg(gi, "Glock 0x%p (%u, 0x%llx)\n", gl, gl->gl_name.ln_type,
1848 (unsigned long long)gl->gl_name.ln_number);
1849 print_dbg(gi, " gl_flags =");
1850 for (x = 0; x < 32; x++) {
1851 if (test_bit(x, &gl->gl_flags))
1852 print_dbg(gi, " %u", x);
1854 if (!test_bit(GLF_LOCK, &gl->gl_flags))
1855 print_dbg(gi, " (unlocked)");
1856 print_dbg(gi, " \n");
1857 print_dbg(gi, " gl_ref = %d\n", atomic_read(&gl->gl_ref));
1858 print_dbg(gi, " gl_state = %u\n", gl->gl_state);
1859 if (gl->gl_owner_pid) {
1860 gl_owner = find_task_by_pid(gl->gl_owner_pid);
1862 print_dbg(gi, " gl_owner = pid %d (%s)\n",
1863 gl->gl_owner_pid, gl_owner->comm);
1865 print_dbg(gi, " gl_owner = %d (ended)\n",
1868 print_dbg(gi, " gl_owner = -1\n");
1869 print_dbg(gi, " gl_ip = %lu\n", gl->gl_ip);
1870 print_dbg(gi, " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
1871 print_dbg(gi, " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
1872 print_dbg(gi, " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
1873 print_dbg(gi, " object = %s\n", (gl->gl_object) ? "yes" : "no");
1874 print_dbg(gi, " le = %s\n",
1875 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
1876 print_dbg(gi, " reclaim = %s\n",
1877 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
1879 print_dbg(gi, " aspace = 0x%p nrpages = %lu\n", gl->gl_aspace,
1880 gl->gl_aspace->i_mapping->nrpages);
1882 print_dbg(gi, " aspace = no\n");
1883 print_dbg(gi, " ail = %d\n", atomic_read(&gl->gl_ail_count));
1884 if (gl->gl_req_gh) {
1885 error = dump_holder(gi, "Request", gl->gl_req_gh);
1889 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1890 error = dump_holder(gi, "Holder", gh);
1894 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
1895 error = dump_holder(gi, "Waiter1", gh);
1899 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
1900 error = dump_holder(gi, "Waiter3", gh);
1904 if (test_bit(GLF_DEMOTE, &gl->gl_flags)) {
1905 print_dbg(gi, " Demotion req to state %u (%llu uS ago)\n",
1906 gl->gl_demote_state, (unsigned long long)
1907 (jiffies - gl->gl_demote_time)*(1000000/HZ));
1909 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
1910 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
1911 list_empty(&gl->gl_holders)) {
1912 error = dump_inode(gi, gl->gl_object);
1917 print_dbg(gi, " Inode: busy\n");
1924 spin_unlock(&gl->gl_spin);
1929 * gfs2_dump_lockstate - print out the current lockstate
1930 * @sdp: the filesystem
1931 * @ub: the buffer to copy the information into
1933 * If @ub is NULL, dump the lockstate to the console.
1937 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
1939 struct gfs2_glock *gl;
1940 struct hlist_node *h;
1944 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
1946 read_lock(gl_lock_addr(x));
1948 hlist_for_each_entry(gl, h, &gl_hash_table[x].hb_list, gl_list) {
1949 if (gl->gl_sbd != sdp)
1952 error = dump_glock(NULL, gl);
1957 read_unlock(gl_lock_addr(x));
1968 * gfs2_scand - Look for cached glocks and inodes to toss from memory
1969 * @sdp: Pointer to GFS2 superblock
1971 * One of these daemons runs, finding candidates to add to sd_reclaim_list.
1975 static int gfs2_scand(void *data)
1980 while (!kthread_should_stop()) {
1981 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
1982 examine_bucket(scan_glock, NULL, x);
1983 if (freezing(current))
1988 schedule_timeout_interruptible(delay * HZ);
1996 int __init gfs2_glock_init(void)
1999 for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
2000 INIT_HLIST_HEAD(&gl_hash_table[i].hb_list);
2002 #ifdef GL_HASH_LOCK_SZ
2003 for(i = 0; i < GL_HASH_LOCK_SZ; i++) {
2004 rwlock_init(&gl_hash_locks[i]);
2008 scand_process = kthread_run(gfs2_scand, NULL, "gfs2_scand");
2009 if (IS_ERR(scand_process))
2010 return PTR_ERR(scand_process);
2015 void gfs2_glock_exit(void)
2017 kthread_stop(scand_process);
2020 module_param(scand_secs, uint, S_IRUGO|S_IWUSR);
2021 MODULE_PARM_DESC(scand_secs, "The number of seconds between scand runs");
2023 static int gfs2_glock_iter_next(struct glock_iter *gi)
2025 struct gfs2_glock *gl;
2028 read_lock(gl_lock_addr(gi->hash));
2031 gi->gl = hlist_entry(gl->gl_list.next,
2032 struct gfs2_glock, gl_list);
2034 gfs2_glock_hold(gi->gl);
2036 read_unlock(gl_lock_addr(gi->hash));
2039 if (gl && gi->gl == NULL)
2041 while(gi->gl == NULL) {
2042 if (gi->hash >= GFS2_GL_HASH_SIZE)
2044 read_lock(gl_lock_addr(gi->hash));
2045 gi->gl = hlist_entry(gl_hash_table[gi->hash].hb_list.first,
2046 struct gfs2_glock, gl_list);
2048 gfs2_glock_hold(gi->gl);
2049 read_unlock(gl_lock_addr(gi->hash));
2053 if (gi->sdp != gi->gl->gl_sbd)
2059 static void gfs2_glock_iter_free(struct glock_iter *gi)
2062 gfs2_glock_put(gi->gl);
2066 static struct glock_iter *gfs2_glock_iter_init(struct gfs2_sbd *sdp)
2068 struct glock_iter *gi;
2070 gi = kmalloc(sizeof (*gi), GFP_KERNEL);
2078 memset(gi->string, 0, sizeof(gi->string));
2080 if (gfs2_glock_iter_next(gi)) {
2081 gfs2_glock_iter_free(gi);
2088 static void *gfs2_glock_seq_start(struct seq_file *file, loff_t *pos)
2090 struct glock_iter *gi;
2093 gi = gfs2_glock_iter_init(file->private);
2098 if (gfs2_glock_iter_next(gi)) {
2099 gfs2_glock_iter_free(gi);
2107 static void *gfs2_glock_seq_next(struct seq_file *file, void *iter_ptr,
2110 struct glock_iter *gi = iter_ptr;
2114 if (gfs2_glock_iter_next(gi)) {
2115 gfs2_glock_iter_free(gi);
2122 static void gfs2_glock_seq_stop(struct seq_file *file, void *iter_ptr)
2124 struct glock_iter *gi = iter_ptr;
2126 gfs2_glock_iter_free(gi);
2129 static int gfs2_glock_seq_show(struct seq_file *file, void *iter_ptr)
2131 struct glock_iter *gi = iter_ptr;
2134 dump_glock(gi, gi->gl);
2139 static const struct seq_operations gfs2_glock_seq_ops = {
2140 .start = gfs2_glock_seq_start,
2141 .next = gfs2_glock_seq_next,
2142 .stop = gfs2_glock_seq_stop,
2143 .show = gfs2_glock_seq_show,
2146 static int gfs2_debugfs_open(struct inode *inode, struct file *file)
2148 struct seq_file *seq;
2151 ret = seq_open(file, &gfs2_glock_seq_ops);
2155 seq = file->private_data;
2156 seq->private = inode->i_private;
2161 static const struct file_operations gfs2_debug_fops = {
2162 .owner = THIS_MODULE,
2163 .open = gfs2_debugfs_open,
2165 .llseek = seq_lseek,
2166 .release = seq_release
2169 int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
2171 sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
2172 if (!sdp->debugfs_dir)
2174 sdp->debugfs_dentry_glocks = debugfs_create_file("glocks",
2176 sdp->debugfs_dir, sdp,
2178 if (!sdp->debugfs_dentry_glocks)
2184 void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
2186 if (sdp && sdp->debugfs_dir) {
2187 if (sdp->debugfs_dentry_glocks) {
2188 debugfs_remove(sdp->debugfs_dentry_glocks);
2189 sdp->debugfs_dentry_glocks = NULL;
2191 debugfs_remove(sdp->debugfs_dir);
2192 sdp->debugfs_dir = NULL;
2196 int gfs2_register_debugfs(void)
2198 gfs2_root = debugfs_create_dir("gfs2", NULL);
2199 return gfs2_root ? 0 : -ENOMEM;
2202 void gfs2_unregister_debugfs(void)
2204 debugfs_remove(gfs2_root);