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_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 struct lm_lockname *name)
205 struct gfs2_glock *gl;
207 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
208 if (test_bit(GLF_PLUG, &gl->gl_flags))
210 if (!lm_name_equal(&gl->gl_name, name))
213 kref_get(&gl->gl_ref);
222 * gfs2_glock_find() - Find glock by lock number
223 * @sdp: The GFS2 superblock
224 * @name: The lock name
226 * Returns: NULL, or the struct gfs2_glock with the requested number
229 static struct gfs2_glock *gfs2_glock_find(struct gfs2_sbd *sdp,
230 struct lm_lockname *name)
232 struct gfs2_gl_hash_bucket *bucket = &sdp->sd_gl_hash[gl_hash(name)];
233 struct gfs2_glock *gl;
235 read_lock(&bucket->hb_lock);
236 gl = search_bucket(bucket, name);
237 read_unlock(&bucket->hb_lock);
243 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
244 * @sdp: The GFS2 superblock
245 * @number: the lock number
246 * @glops: The glock_operations to use
247 * @create: If 0, don't create the glock if it doesn't exist
248 * @glp: the glock is returned here
250 * This does not lock a glock, just finds/creates structures for one.
255 int gfs2_glock_get(struct gfs2_sbd *sdp, uint64_t number,
256 const struct gfs2_glock_operations *glops, int create,
257 struct gfs2_glock **glp)
259 struct lm_lockname name;
260 struct gfs2_glock *gl, *tmp;
261 struct gfs2_gl_hash_bucket *bucket;
264 name.ln_number = number;
265 name.ln_type = glops->go_type;
266 bucket = &sdp->sd_gl_hash[gl_hash(&name)];
268 read_lock(&bucket->hb_lock);
269 gl = search_bucket(bucket, &name);
270 read_unlock(&bucket->hb_lock);
277 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
283 kref_init(&gl->gl_ref);
284 gl->gl_state = LM_ST_UNLOCKED;
288 gl->gl_req_gh = NULL;
289 gl->gl_req_bh = NULL;
291 gl->gl_stamp = jiffies;
292 gl->gl_object = NULL;
293 gl->gl_bucket = bucket;
295 gl->gl_aspace = NULL;
296 lops_init_le(&gl->gl_le, &gfs2_glock_lops);
298 /* If this glock protects actual on-disk data or metadata blocks,
299 create a VFS inode to manage the pages/buffers holding them. */
300 if (glops == &gfs2_inode_glops ||
301 glops == &gfs2_rgrp_glops) {
302 gl->gl_aspace = gfs2_aspace_get(sdp);
303 if (!gl->gl_aspace) {
309 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
313 write_lock(&bucket->hb_lock);
314 tmp = search_bucket(bucket, &name);
316 write_unlock(&bucket->hb_lock);
320 list_add_tail(&gl->gl_list, &bucket->hb_list);
321 write_unlock(&bucket->hb_lock);
330 gfs2_aspace_put(gl->gl_aspace);
332 kmem_cache_free(gfs2_glock_cachep, gl);
337 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
339 * @state: the state we're requesting
340 * @flags: the modifier flags
341 * @gh: the holder structure
345 void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
346 struct gfs2_holder *gh)
348 INIT_LIST_HEAD(&gh->gh_list);
350 gh->gh_ip = (unsigned long)__builtin_return_address(0);
351 gh->gh_owner = current;
352 gh->gh_state = state;
353 gh->gh_flags = flags;
356 init_completion(&gh->gh_wait);
358 if (gh->gh_state == LM_ST_EXCLUSIVE)
359 gh->gh_flags |= GL_LOCAL_EXCL;
365 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
366 * @state: the state we're requesting
367 * @flags: the modifier flags
368 * @gh: the holder structure
370 * Don't mess with the glock.
374 void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
376 gh->gh_state = state;
377 gh->gh_flags = flags;
378 if (gh->gh_state == LM_ST_EXCLUSIVE)
379 gh->gh_flags |= GL_LOCAL_EXCL;
381 gh->gh_iflags &= 1 << HIF_ALLOCED;
382 gh->gh_ip = (unsigned long)__builtin_return_address(0);
386 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
387 * @gh: the holder structure
391 void gfs2_holder_uninit(struct gfs2_holder *gh)
393 gfs2_glock_put(gh->gh_gl);
399 * gfs2_holder_get - get a struct gfs2_holder structure
401 * @state: the state we're requesting
402 * @flags: the modifier flags
405 * Figure out how big an impact this function has. Either:
406 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
407 * 2) Leave it like it is
409 * Returns: the holder structure, NULL on ENOMEM
412 static struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl,
414 int flags, gfp_t gfp_flags)
416 struct gfs2_holder *gh;
418 gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags);
422 gfs2_holder_init(gl, state, flags, gh);
423 set_bit(HIF_ALLOCED, &gh->gh_iflags);
424 gh->gh_ip = (unsigned long)__builtin_return_address(0);
429 * gfs2_holder_put - get rid of a struct gfs2_holder structure
430 * @gh: the holder structure
434 static void gfs2_holder_put(struct gfs2_holder *gh)
436 gfs2_holder_uninit(gh);
441 * rq_mutex - process a mutex request in the queue
442 * @gh: the glock holder
444 * Returns: 1 if the queue is blocked
447 static int rq_mutex(struct gfs2_holder *gh)
449 struct gfs2_glock *gl = gh->gh_gl;
451 list_del_init(&gh->gh_list);
452 /* gh->gh_error never examined. */
453 set_bit(GLF_LOCK, &gl->gl_flags);
454 complete(&gh->gh_wait);
460 * rq_promote - process a promote request in the queue
461 * @gh: the glock holder
463 * Acquire a new inter-node lock, or change a lock state to more restrictive.
465 * Returns: 1 if the queue is blocked
468 static int rq_promote(struct gfs2_holder *gh)
470 struct gfs2_glock *gl = gh->gh_gl;
471 struct gfs2_sbd *sdp = gl->gl_sbd;
472 const struct gfs2_glock_operations *glops = gl->gl_ops;
474 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
475 if (list_empty(&gl->gl_holders)) {
477 set_bit(GLF_LOCK, &gl->gl_flags);
478 spin_unlock(&gl->gl_spin);
480 if (atomic_read(&sdp->sd_reclaim_count) >
481 gfs2_tune_get(sdp, gt_reclaim_limit) &&
482 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
483 gfs2_reclaim_glock(sdp);
484 gfs2_reclaim_glock(sdp);
487 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
488 spin_lock(&gl->gl_spin);
493 if (list_empty(&gl->gl_holders)) {
494 set_bit(HIF_FIRST, &gh->gh_iflags);
495 set_bit(GLF_LOCK, &gl->gl_flags);
497 struct gfs2_holder *next_gh;
498 if (gh->gh_flags & GL_LOCAL_EXCL)
500 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
502 if (next_gh->gh_flags & GL_LOCAL_EXCL)
506 list_move_tail(&gh->gh_list, &gl->gl_holders);
508 set_bit(HIF_HOLDER, &gh->gh_iflags);
510 complete(&gh->gh_wait);
516 * rq_demote - process a demote request in the queue
517 * @gh: the glock holder
519 * Returns: 1 if the queue is blocked
522 static int rq_demote(struct gfs2_holder *gh)
524 struct gfs2_glock *gl = gh->gh_gl;
525 const struct gfs2_glock_operations *glops = gl->gl_ops;
527 if (!list_empty(&gl->gl_holders))
530 if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) {
531 list_del_init(&gh->gh_list);
533 spin_unlock(&gl->gl_spin);
534 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
537 complete(&gh->gh_wait);
538 spin_lock(&gl->gl_spin);
541 set_bit(GLF_LOCK, &gl->gl_flags);
542 spin_unlock(&gl->gl_spin);
544 if (gh->gh_state == LM_ST_UNLOCKED ||
545 gl->gl_state != LM_ST_EXCLUSIVE)
546 glops->go_drop_th(gl);
548 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
550 spin_lock(&gl->gl_spin);
557 * rq_greedy - process a queued request to drop greedy status
558 * @gh: the glock holder
560 * Returns: 1 if the queue is blocked
563 static int rq_greedy(struct gfs2_holder *gh)
565 struct gfs2_glock *gl = gh->gh_gl;
567 list_del_init(&gh->gh_list);
568 /* gh->gh_error never examined. */
569 clear_bit(GLF_GREEDY, &gl->gl_flags);
570 spin_unlock(&gl->gl_spin);
572 gfs2_holder_uninit(gh);
573 kfree(container_of(gh, struct greedy, gr_gh));
575 spin_lock(&gl->gl_spin);
581 * run_queue - process holder structures on a glock
585 static void run_queue(struct gfs2_glock *gl)
587 struct gfs2_holder *gh;
591 if (test_bit(GLF_LOCK, &gl->gl_flags))
594 if (!list_empty(&gl->gl_waiters1)) {
595 gh = list_entry(gl->gl_waiters1.next,
596 struct gfs2_holder, gh_list);
598 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
599 blocked = rq_mutex(gh);
601 gfs2_assert_warn(gl->gl_sbd, 0);
603 } else if (!list_empty(&gl->gl_waiters2) &&
604 !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) {
605 gh = list_entry(gl->gl_waiters2.next,
606 struct gfs2_holder, gh_list);
608 if (test_bit(HIF_DEMOTE, &gh->gh_iflags))
609 blocked = rq_demote(gh);
610 else if (test_bit(HIF_GREEDY, &gh->gh_iflags))
611 blocked = rq_greedy(gh);
613 gfs2_assert_warn(gl->gl_sbd, 0);
615 } else if (!list_empty(&gl->gl_waiters3)) {
616 gh = list_entry(gl->gl_waiters3.next,
617 struct gfs2_holder, gh_list);
619 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
620 blocked = rq_promote(gh);
622 gfs2_assert_warn(gl->gl_sbd, 0);
633 * gfs2_glmutex_lock - acquire a local lock on a glock
636 * Gives caller exclusive access to manipulate a glock structure.
639 static void gfs2_glmutex_lock(struct gfs2_glock *gl)
641 struct gfs2_holder gh;
643 gfs2_holder_init(gl, 0, 0, &gh);
644 set_bit(HIF_MUTEX, &gh.gh_iflags);
646 spin_lock(&gl->gl_spin);
647 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
648 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
650 gl->gl_owner = current;
651 gl->gl_ip = (unsigned long)__builtin_return_address(0);
652 complete(&gh.gh_wait);
654 spin_unlock(&gl->gl_spin);
656 wait_for_completion(&gh.gh_wait);
657 gfs2_holder_uninit(&gh);
661 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
664 * Returns: 1 if the glock is acquired
667 static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
671 spin_lock(&gl->gl_spin);
672 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
675 gl->gl_owner = current;
676 gl->gl_ip = (unsigned long)__builtin_return_address(0);
678 spin_unlock(&gl->gl_spin);
684 * gfs2_glmutex_unlock - release a local lock on a glock
689 static void gfs2_glmutex_unlock(struct gfs2_glock *gl)
691 spin_lock(&gl->gl_spin);
692 clear_bit(GLF_LOCK, &gl->gl_flags);
696 BUG_ON(!spin_is_locked(&gl->gl_spin));
697 spin_unlock(&gl->gl_spin);
701 * handle_callback - add a demote request to a lock's queue
703 * @state: the state the caller wants us to change to
705 * Note: This may fail sliently if we are out of memory.
708 static void handle_callback(struct gfs2_glock *gl, unsigned int state)
710 struct gfs2_holder *gh, *new_gh = NULL;
713 spin_lock(&gl->gl_spin);
715 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
716 if (test_bit(HIF_DEMOTE, &gh->gh_iflags) &&
717 gl->gl_req_gh != gh) {
718 if (gh->gh_state != state)
719 gh->gh_state = LM_ST_UNLOCKED;
725 list_add_tail(&new_gh->gh_list, &gl->gl_waiters2);
728 spin_unlock(&gl->gl_spin);
730 new_gh = gfs2_holder_get(gl, state, LM_FLAG_TRY, GFP_KERNEL);
733 set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
734 set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
740 spin_unlock(&gl->gl_spin);
743 gfs2_holder_put(new_gh);
746 void gfs2_glock_inode_squish(struct inode *inode)
748 struct gfs2_holder gh;
749 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
750 gfs2_holder_init(gl, LM_ST_UNLOCKED, 0, &gh);
751 set_bit(HIF_DEMOTE, &gh.gh_iflags);
752 spin_lock(&gl->gl_spin);
753 gfs2_assert(inode->i_sb->s_fs_info, list_empty(&gl->gl_holders));
754 list_add_tail(&gh.gh_list, &gl->gl_waiters2);
756 spin_unlock(&gl->gl_spin);
757 wait_for_completion(&gh.gh_wait);
758 gfs2_holder_uninit(&gh);
762 * state_change - record that the glock is now in a different state
764 * @new_state the new state
768 static void state_change(struct gfs2_glock *gl, unsigned int new_state)
772 held1 = (gl->gl_state != LM_ST_UNLOCKED);
773 held2 = (new_state != LM_ST_UNLOCKED);
775 if (held1 != held2) {
782 gl->gl_state = new_state;
786 * xmote_bh - Called after the lock module is done acquiring a lock
787 * @gl: The glock in question
788 * @ret: the int returned from the lock module
792 static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
794 struct gfs2_sbd *sdp = gl->gl_sbd;
795 const struct gfs2_glock_operations *glops = gl->gl_ops;
796 struct gfs2_holder *gh = gl->gl_req_gh;
797 int prev_state = gl->gl_state;
800 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
801 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
802 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
804 state_change(gl, ret & LM_OUT_ST_MASK);
806 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
808 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
809 } else if (gl->gl_state == LM_ST_DEFERRED) {
810 /* We might not want to do this here.
811 Look at moving to the inode glops. */
813 glops->go_inval(gl, DIO_DATA);
816 /* Deal with each possible exit condition */
819 gl->gl_stamp = jiffies;
821 else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
822 spin_lock(&gl->gl_spin);
823 list_del_init(&gh->gh_list);
825 spin_unlock(&gl->gl_spin);
827 } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
828 spin_lock(&gl->gl_spin);
829 list_del_init(&gh->gh_list);
830 if (gl->gl_state == gh->gh_state ||
831 gl->gl_state == LM_ST_UNLOCKED)
834 if (gfs2_assert_warn(sdp, gh->gh_flags &
835 (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
836 fs_warn(sdp, "ret = 0x%.8X\n", ret);
837 gh->gh_error = GLR_TRYFAILED;
839 spin_unlock(&gl->gl_spin);
841 if (ret & LM_OUT_CANCELED)
842 handle_callback(gl, LM_ST_UNLOCKED); /* Lame */
844 } else if (ret & LM_OUT_CANCELED) {
845 spin_lock(&gl->gl_spin);
846 list_del_init(&gh->gh_list);
847 gh->gh_error = GLR_CANCELED;
848 spin_unlock(&gl->gl_spin);
850 } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
851 spin_lock(&gl->gl_spin);
852 list_move_tail(&gh->gh_list, &gl->gl_holders);
854 set_bit(HIF_HOLDER, &gh->gh_iflags);
855 spin_unlock(&gl->gl_spin);
857 set_bit(HIF_FIRST, &gh->gh_iflags);
861 } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
862 spin_lock(&gl->gl_spin);
863 list_del_init(&gh->gh_list);
864 gh->gh_error = GLR_TRYFAILED;
865 spin_unlock(&gl->gl_spin);
868 if (gfs2_assert_withdraw(sdp, 0) == -1)
869 fs_err(sdp, "ret = 0x%.8X\n", ret);
872 if (glops->go_xmote_bh)
873 glops->go_xmote_bh(gl);
876 spin_lock(&gl->gl_spin);
877 gl->gl_req_gh = NULL;
878 gl->gl_req_bh = NULL;
879 clear_bit(GLF_LOCK, &gl->gl_flags);
881 spin_unlock(&gl->gl_spin);
887 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
890 complete(&gh->gh_wait);
895 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
896 * @gl: The glock in question
897 * @state: the requested state
898 * @flags: modifier flags to the lock call
902 void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags)
904 struct gfs2_sbd *sdp = gl->gl_sbd;
905 const struct gfs2_glock_operations *glops = gl->gl_ops;
906 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
907 LM_FLAG_NOEXP | LM_FLAG_ANY |
909 unsigned int lck_ret;
911 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
912 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
913 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
914 gfs2_assert_warn(sdp, state != gl->gl_state);
916 if (gl->gl_state == LM_ST_EXCLUSIVE) {
919 DIO_METADATA | DIO_DATA | DIO_RELEASE);
923 gl->gl_req_bh = xmote_bh;
925 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state, lck_flags);
927 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
930 if (lck_ret & LM_OUT_ASYNC)
931 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
933 xmote_bh(gl, lck_ret);
937 * drop_bh - Called after a lock module unlock completes
939 * @ret: the return status
941 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
942 * Doesn't drop the reference on the glock the top half took out
946 static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
948 struct gfs2_sbd *sdp = gl->gl_sbd;
949 const struct gfs2_glock_operations *glops = gl->gl_ops;
950 struct gfs2_holder *gh = gl->gl_req_gh;
952 clear_bit(GLF_PREFETCH, &gl->gl_flags);
954 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
955 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
956 gfs2_assert_warn(sdp, !ret);
958 state_change(gl, LM_ST_UNLOCKED);
961 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
964 spin_lock(&gl->gl_spin);
965 list_del_init(&gh->gh_list);
967 spin_unlock(&gl->gl_spin);
970 if (glops->go_drop_bh)
971 glops->go_drop_bh(gl);
973 spin_lock(&gl->gl_spin);
974 gl->gl_req_gh = NULL;
975 gl->gl_req_bh = NULL;
976 clear_bit(GLF_LOCK, &gl->gl_flags);
978 spin_unlock(&gl->gl_spin);
983 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
986 complete(&gh->gh_wait);
991 * gfs2_glock_drop_th - call into the lock module to unlock a lock
996 void gfs2_glock_drop_th(struct gfs2_glock *gl)
998 struct gfs2_sbd *sdp = gl->gl_sbd;
999 const struct gfs2_glock_operations *glops = gl->gl_ops;
1002 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1003 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
1004 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
1006 if (gl->gl_state == LM_ST_EXCLUSIVE) {
1008 glops->go_sync(gl, DIO_METADATA | DIO_DATA | DIO_RELEASE);
1011 gfs2_glock_hold(gl);
1012 gl->gl_req_bh = drop_bh;
1014 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
1016 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
1022 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
1026 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1027 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1029 * Don't cancel GL_NOCANCEL requests.
1032 static void do_cancels(struct gfs2_holder *gh)
1034 struct gfs2_glock *gl = gh->gh_gl;
1036 spin_lock(&gl->gl_spin);
1038 while (gl->gl_req_gh != gh &&
1039 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1040 !list_empty(&gh->gh_list)) {
1041 if (gl->gl_req_bh &&
1043 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
1044 spin_unlock(&gl->gl_spin);
1045 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
1047 spin_lock(&gl->gl_spin);
1049 spin_unlock(&gl->gl_spin);
1051 spin_lock(&gl->gl_spin);
1055 spin_unlock(&gl->gl_spin);
1059 * glock_wait_internal - wait on a glock acquisition
1060 * @gh: the glock holder
1062 * Returns: 0 on success
1065 static int glock_wait_internal(struct gfs2_holder *gh)
1067 struct gfs2_glock *gl = gh->gh_gl;
1068 struct gfs2_sbd *sdp = gl->gl_sbd;
1069 const struct gfs2_glock_operations *glops = gl->gl_ops;
1071 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1074 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1075 spin_lock(&gl->gl_spin);
1076 if (gl->gl_req_gh != gh &&
1077 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1078 !list_empty(&gh->gh_list)) {
1079 list_del_init(&gh->gh_list);
1080 gh->gh_error = GLR_TRYFAILED;
1082 spin_unlock(&gl->gl_spin);
1083 return gh->gh_error;
1085 spin_unlock(&gl->gl_spin);
1088 if (gh->gh_flags & LM_FLAG_PRIORITY)
1091 wait_for_completion(&gh->gh_wait);
1094 return gh->gh_error;
1096 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
1097 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state,
1101 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1102 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1104 if (glops->go_lock) {
1105 gh->gh_error = glops->go_lock(gh);
1107 spin_lock(&gl->gl_spin);
1108 list_del_init(&gh->gh_list);
1109 spin_unlock(&gl->gl_spin);
1113 spin_lock(&gl->gl_spin);
1114 gl->gl_req_gh = NULL;
1115 gl->gl_req_bh = NULL;
1116 clear_bit(GLF_LOCK, &gl->gl_flags);
1118 spin_unlock(&gl->gl_spin);
1121 return gh->gh_error;
1124 static inline struct gfs2_holder *
1125 find_holder_by_owner(struct list_head *head, struct task_struct *owner)
1127 struct gfs2_holder *gh;
1129 list_for_each_entry(gh, head, gh_list) {
1130 if (gh->gh_owner == owner)
1138 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1139 * @gh: the holder structure to add
1143 static void add_to_queue(struct gfs2_holder *gh)
1145 struct gfs2_glock *gl = gh->gh_gl;
1146 struct gfs2_holder *existing;
1148 BUG_ON(!gh->gh_owner);
1150 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
1152 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1153 printk(KERN_INFO "pid : %d\n", existing->gh_owner->pid);
1154 printk(KERN_INFO "lock type : %d lock state : %d\n",
1155 existing->gh_gl->gl_name.ln_type, existing->gh_gl->gl_state);
1156 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1157 printk(KERN_INFO "pid : %d\n", gh->gh_owner->pid);
1158 printk(KERN_INFO "lock type : %d lock state : %d\n",
1159 gl->gl_name.ln_type, gl->gl_state);
1163 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
1165 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1166 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1170 if (gh->gh_flags & LM_FLAG_PRIORITY)
1171 list_add(&gh->gh_list, &gl->gl_waiters3);
1173 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1177 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1178 * @gh: the holder structure
1180 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1182 * Returns: 0, GLR_TRYFAILED, or errno on failure
1185 int gfs2_glock_nq(struct gfs2_holder *gh)
1187 struct gfs2_glock *gl = gh->gh_gl;
1188 struct gfs2_sbd *sdp = gl->gl_sbd;
1192 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1193 set_bit(HIF_ABORTED, &gh->gh_iflags);
1197 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1199 spin_lock(&gl->gl_spin);
1202 spin_unlock(&gl->gl_spin);
1204 if (!(gh->gh_flags & GL_ASYNC)) {
1205 error = glock_wait_internal(gh);
1206 if (error == GLR_CANCELED) {
1212 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1214 if (error == GLR_TRYFAILED && (gh->gh_flags & GL_DUMP))
1221 * gfs2_glock_poll - poll to see if an async request has been completed
1224 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1227 int gfs2_glock_poll(struct gfs2_holder *gh)
1229 struct gfs2_glock *gl = gh->gh_gl;
1232 spin_lock(&gl->gl_spin);
1234 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1236 else if (list_empty(&gh->gh_list)) {
1237 if (gh->gh_error == GLR_CANCELED) {
1238 spin_unlock(&gl->gl_spin);
1240 if (gfs2_glock_nq(gh))
1247 spin_unlock(&gl->gl_spin);
1253 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1254 * @gh: the holder structure
1256 * Returns: 0, GLR_TRYFAILED, or errno on failure
1259 int gfs2_glock_wait(struct gfs2_holder *gh)
1263 error = glock_wait_internal(gh);
1264 if (error == GLR_CANCELED) {
1266 gh->gh_flags &= ~GL_ASYNC;
1267 error = gfs2_glock_nq(gh);
1274 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1275 * @gh: the glock holder
1279 void gfs2_glock_dq(struct gfs2_holder *gh)
1281 struct gfs2_glock *gl = gh->gh_gl;
1282 const struct gfs2_glock_operations *glops = gl->gl_ops;
1284 if (gh->gh_flags & GL_NOCACHE)
1285 handle_callback(gl, LM_ST_UNLOCKED);
1287 gfs2_glmutex_lock(gl);
1289 spin_lock(&gl->gl_spin);
1290 list_del_init(&gh->gh_list);
1292 if (list_empty(&gl->gl_holders)) {
1293 spin_unlock(&gl->gl_spin);
1295 if (glops->go_unlock)
1296 glops->go_unlock(gh);
1298 gl->gl_stamp = jiffies;
1300 spin_lock(&gl->gl_spin);
1303 clear_bit(GLF_LOCK, &gl->gl_flags);
1305 spin_unlock(&gl->gl_spin);
1309 * gfs2_glock_prefetch - Try to prefetch a glock
1311 * @state: the state to prefetch in
1312 * @flags: flags passed to go_xmote_th()
1316 static void gfs2_glock_prefetch(struct gfs2_glock *gl, unsigned int state,
1319 const struct gfs2_glock_operations *glops = gl->gl_ops;
1321 spin_lock(&gl->gl_spin);
1323 if (test_bit(GLF_LOCK, &gl->gl_flags) ||
1324 !list_empty(&gl->gl_holders) ||
1325 !list_empty(&gl->gl_waiters1) ||
1326 !list_empty(&gl->gl_waiters2) ||
1327 !list_empty(&gl->gl_waiters3) ||
1328 relaxed_state_ok(gl->gl_state, state, flags)) {
1329 spin_unlock(&gl->gl_spin);
1333 set_bit(GLF_PREFETCH, &gl->gl_flags);
1334 set_bit(GLF_LOCK, &gl->gl_flags);
1335 spin_unlock(&gl->gl_spin);
1337 glops->go_xmote_th(gl, state, flags);
1340 static void greedy_work(void *data)
1342 struct greedy *gr = data;
1343 struct gfs2_holder *gh = &gr->gr_gh;
1344 struct gfs2_glock *gl = gh->gh_gl;
1345 const struct gfs2_glock_operations *glops = gl->gl_ops;
1347 clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1349 if (glops->go_greedy)
1350 glops->go_greedy(gl);
1352 spin_lock(&gl->gl_spin);
1354 if (list_empty(&gl->gl_waiters2)) {
1355 clear_bit(GLF_GREEDY, &gl->gl_flags);
1356 spin_unlock(&gl->gl_spin);
1357 gfs2_holder_uninit(gh);
1360 gfs2_glock_hold(gl);
1361 list_add_tail(&gh->gh_list, &gl->gl_waiters2);
1363 spin_unlock(&gl->gl_spin);
1369 * gfs2_glock_be_greedy -
1373 * Returns: 0 if go_greedy will be called, 1 otherwise
1376 int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time)
1379 struct gfs2_holder *gh;
1381 if (!time || gl->gl_sbd->sd_args.ar_localcaching ||
1382 test_and_set_bit(GLF_GREEDY, &gl->gl_flags))
1385 gr = kmalloc(sizeof(struct greedy), GFP_KERNEL);
1387 clear_bit(GLF_GREEDY, &gl->gl_flags);
1392 gfs2_holder_init(gl, 0, 0, gh);
1393 set_bit(HIF_GREEDY, &gh->gh_iflags);
1394 INIT_WORK(&gr->gr_work, greedy_work, gr);
1396 set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1397 schedule_delayed_work(&gr->gr_work, time);
1403 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1404 * @gh: the holder structure
1408 void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1411 gfs2_holder_uninit(gh);
1415 * gfs2_glock_nq_num - acquire a glock based on lock number
1416 * @sdp: the filesystem
1417 * @number: the lock number
1418 * @glops: the glock operations for the type of glock
1419 * @state: the state to acquire the glock in
1420 * @flags: modifier flags for the aquisition
1421 * @gh: the struct gfs2_holder
1426 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, uint64_t number,
1427 const struct gfs2_glock_operations *glops,
1428 unsigned int state, int flags, struct gfs2_holder *gh)
1430 struct gfs2_glock *gl;
1433 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1435 error = gfs2_glock_nq_init(gl, state, flags, gh);
1443 * glock_compare - Compare two struct gfs2_glock structures for sorting
1444 * @arg_a: the first structure
1445 * @arg_b: the second structure
1449 static int glock_compare(const void *arg_a, const void *arg_b)
1451 struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
1452 struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
1453 struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1454 struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1457 if (a->ln_number > b->ln_number)
1459 else if (a->ln_number < b->ln_number)
1462 if (gh_a->gh_state == LM_ST_SHARED &&
1463 gh_b->gh_state == LM_ST_EXCLUSIVE)
1465 else if (!(gh_a->gh_flags & GL_LOCAL_EXCL) &&
1466 (gh_b->gh_flags & GL_LOCAL_EXCL))
1474 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1475 * @num_gh: the number of structures
1476 * @ghs: an array of struct gfs2_holder structures
1478 * Returns: 0 on success (all glocks acquired),
1479 * errno on failure (no glocks acquired)
1482 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1483 struct gfs2_holder **p)
1488 for (x = 0; x < num_gh; x++)
1491 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1493 for (x = 0; x < num_gh; x++) {
1494 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1496 error = gfs2_glock_nq(p[x]);
1499 gfs2_glock_dq(p[x]);
1508 * gfs2_glock_nq_m - acquire multiple glocks
1509 * @num_gh: the number of structures
1510 * @ghs: an array of struct gfs2_holder structures
1512 * Figure out how big an impact this function has. Either:
1513 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1514 * 2) Forget async stuff and just call nq_m_sync()
1515 * 3) Leave it like it is
1517 * Returns: 0 on success (all glocks acquired),
1518 * errno on failure (no glocks acquired)
1521 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1525 int borked = 0, serious = 0;
1532 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1533 return gfs2_glock_nq(ghs);
1536 e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1540 for (x = 0; x < num_gh; x++) {
1541 ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
1542 error = gfs2_glock_nq(&ghs[x]);
1551 for (x = 0; x < num_gh; x++) {
1552 error = e[x] = glock_wait_internal(&ghs[x]);
1555 if (error != GLR_TRYFAILED && error != GLR_CANCELED)
1565 for (x = 0; x < num_gh; x++)
1567 gfs2_glock_dq(&ghs[x]);
1572 for (x = 0; x < num_gh; x++)
1573 gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
1575 error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
1584 * gfs2_glock_dq_m - release multiple glocks
1585 * @num_gh: the number of structures
1586 * @ghs: an array of struct gfs2_holder structures
1590 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1594 for (x = 0; x < num_gh; x++)
1595 gfs2_glock_dq(&ghs[x]);
1599 * gfs2_glock_dq_uninit_m - release multiple glocks
1600 * @num_gh: the number of structures
1601 * @ghs: an array of struct gfs2_holder structures
1605 void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1609 for (x = 0; x < num_gh; x++)
1610 gfs2_glock_dq_uninit(&ghs[x]);
1614 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1615 * @sdp: the filesystem
1616 * @number: the lock number
1617 * @glops: the glock operations for the type of glock
1618 * @state: the state to acquire the glock in
1619 * @flags: modifier flags for the aquisition
1624 void gfs2_glock_prefetch_num(struct gfs2_sbd *sdp, uint64_t number,
1625 const struct gfs2_glock_operations *glops,
1626 unsigned int state, int flags)
1628 struct gfs2_glock *gl;
1631 if (atomic_read(&sdp->sd_reclaim_count) <
1632 gfs2_tune_get(sdp, gt_reclaim_limit)) {
1633 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1635 gfs2_glock_prefetch(gl, state, flags);
1642 * gfs2_lvb_hold - attach a LVB from a glock
1643 * @gl: The glock in question
1647 int gfs2_lvb_hold(struct gfs2_glock *gl)
1651 gfs2_glmutex_lock(gl);
1653 if (!atomic_read(&gl->gl_lvb_count)) {
1654 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1656 gfs2_glmutex_unlock(gl);
1659 gfs2_glock_hold(gl);
1661 atomic_inc(&gl->gl_lvb_count);
1663 gfs2_glmutex_unlock(gl);
1669 * gfs2_lvb_unhold - detach a LVB from a glock
1670 * @gl: The glock in question
1674 void gfs2_lvb_unhold(struct gfs2_glock *gl)
1676 gfs2_glock_hold(gl);
1677 gfs2_glmutex_lock(gl);
1679 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1680 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1681 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1686 gfs2_glmutex_unlock(gl);
1691 void gfs2_lvb_sync(struct gfs2_glock *gl)
1693 gfs2_glmutex_lock(gl);
1695 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count));
1696 if (!gfs2_assert_warn(gl->gl_sbd, gfs2_glock_is_held_excl(gl)))
1697 gfs2_lm_sync_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1699 gfs2_glmutex_unlock(gl);
1703 static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1706 struct gfs2_glock *gl;
1708 gl = gfs2_glock_find(sdp, name);
1712 if (gl->gl_ops->go_callback)
1713 gl->gl_ops->go_callback(gl, state);
1714 handle_callback(gl, state);
1716 spin_lock(&gl->gl_spin);
1718 spin_unlock(&gl->gl_spin);
1724 * gfs2_glock_cb - Callback used by locking module
1725 * @fsdata: Pointer to the superblock
1726 * @type: Type of callback
1727 * @data: Type dependent data pointer
1729 * Called by the locking module when it wants to tell us something.
1730 * Either we need to drop a lock, one of our ASYNC requests completed, or
1731 * a journal from another client needs to be recovered.
1734 void gfs2_glock_cb(lm_fsdata_t *fsdata, unsigned int type, void *data)
1736 struct gfs2_sbd *sdp = (struct gfs2_sbd *)fsdata;
1740 blocking_cb(sdp, data, LM_ST_UNLOCKED);
1744 blocking_cb(sdp, data, LM_ST_DEFERRED);
1748 blocking_cb(sdp, data, LM_ST_SHARED);
1752 struct lm_async_cb *async = data;
1753 struct gfs2_glock *gl;
1755 gl = gfs2_glock_find(sdp, &async->lc_name);
1756 if (gfs2_assert_warn(sdp, gl))
1758 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1759 gl->gl_req_bh(gl, async->lc_ret);
1764 case LM_CB_NEED_RECOVERY:
1765 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1766 if (sdp->sd_recoverd_process)
1767 wake_up_process(sdp->sd_recoverd_process);
1770 case LM_CB_DROPLOCKS:
1771 gfs2_gl_hash_clear(sdp, NO_WAIT);
1772 gfs2_quota_scan(sdp);
1776 gfs2_assert_warn(sdp, 0);
1782 * gfs2_iopen_go_callback - Try to kick the inode/vnode associated with an
1783 * iopen glock from memory
1784 * @io_gl: the iopen glock
1785 * @state: the state into which the glock should be put
1789 void gfs2_iopen_go_callback(struct gfs2_glock *io_gl, unsigned int state)
1792 if (state != LM_ST_UNLOCKED)
1794 /* FIXME: remove this? */
1798 * demote_ok - Check to see if it's ok to unlock a glock
1801 * Returns: 1 if it's ok
1804 static int demote_ok(struct gfs2_glock *gl)
1806 struct gfs2_sbd *sdp = gl->gl_sbd;
1807 const struct gfs2_glock_operations *glops = gl->gl_ops;
1810 if (test_bit(GLF_STICKY, &gl->gl_flags))
1812 else if (test_bit(GLF_PREFETCH, &gl->gl_flags))
1813 demote = time_after_eq(jiffies,
1815 gfs2_tune_get(sdp, gt_prefetch_secs) * HZ);
1816 else if (glops->go_demote_ok)
1817 demote = glops->go_demote_ok(gl);
1823 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1828 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1830 struct gfs2_sbd *sdp = gl->gl_sbd;
1832 spin_lock(&sdp->sd_reclaim_lock);
1833 if (list_empty(&gl->gl_reclaim)) {
1834 gfs2_glock_hold(gl);
1835 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1836 atomic_inc(&sdp->sd_reclaim_count);
1838 spin_unlock(&sdp->sd_reclaim_lock);
1840 wake_up(&sdp->sd_reclaim_wq);
1844 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1845 * @sdp: the filesystem
1847 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1848 * different glock and we notice that there are a lot of glocks in the
1853 void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1855 struct gfs2_glock *gl;
1857 spin_lock(&sdp->sd_reclaim_lock);
1858 if (list_empty(&sdp->sd_reclaim_list)) {
1859 spin_unlock(&sdp->sd_reclaim_lock);
1862 gl = list_entry(sdp->sd_reclaim_list.next,
1863 struct gfs2_glock, gl_reclaim);
1864 list_del_init(&gl->gl_reclaim);
1865 spin_unlock(&sdp->sd_reclaim_lock);
1867 atomic_dec(&sdp->sd_reclaim_count);
1868 atomic_inc(&sdp->sd_reclaimed);
1870 if (gfs2_glmutex_trylock(gl)) {
1871 if (queue_empty(gl, &gl->gl_holders) &&
1872 gl->gl_state != LM_ST_UNLOCKED &&
1874 handle_callback(gl, LM_ST_UNLOCKED);
1875 gfs2_glmutex_unlock(gl);
1882 * examine_bucket - Call a function for glock in a hash bucket
1883 * @examiner: the function
1884 * @sdp: the filesystem
1885 * @bucket: the bucket
1887 * Returns: 1 if the bucket has entries
1890 static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
1891 struct gfs2_gl_hash_bucket *bucket)
1893 struct glock_plug plug;
1894 struct list_head *tmp;
1895 struct gfs2_glock *gl;
1898 /* Add "plug" to end of bucket list, work back up list from there */
1899 memset(&plug.gl_flags, 0, sizeof(unsigned long));
1900 set_bit(GLF_PLUG, &plug.gl_flags);
1902 write_lock(&bucket->hb_lock);
1903 list_add(&plug.gl_list, &bucket->hb_list);
1904 write_unlock(&bucket->hb_lock);
1907 write_lock(&bucket->hb_lock);
1910 tmp = plug.gl_list.next;
1912 if (tmp == &bucket->hb_list) {
1913 list_del(&plug.gl_list);
1914 entries = !list_empty(&bucket->hb_list);
1915 write_unlock(&bucket->hb_lock);
1918 gl = list_entry(tmp, struct gfs2_glock, gl_list);
1920 /* Move plug up list */
1921 list_move(&plug.gl_list, &gl->gl_list);
1923 if (test_bit(GLF_PLUG, &gl->gl_flags))
1926 /* examiner() must glock_put() */
1927 gfs2_glock_hold(gl);
1932 write_unlock(&bucket->hb_lock);
1939 * scan_glock - look at a glock and see if we can reclaim it
1940 * @gl: the glock to look at
1944 static void scan_glock(struct gfs2_glock *gl)
1946 if (gl->gl_ops == &gfs2_inode_glops)
1949 if (gfs2_glmutex_trylock(gl)) {
1950 if (queue_empty(gl, &gl->gl_holders) &&
1951 gl->gl_state != LM_ST_UNLOCKED &&
1954 gfs2_glmutex_unlock(gl);
1961 gfs2_glmutex_unlock(gl);
1962 gfs2_glock_schedule_for_reclaim(gl);
1967 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
1968 * @sdp: the filesystem
1972 void gfs2_scand_internal(struct gfs2_sbd *sdp)
1976 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
1977 examine_bucket(scan_glock, sdp, &sdp->sd_gl_hash[x]);
1983 * clear_glock - look at a glock and see if we can free it from glock cache
1984 * @gl: the glock to look at
1988 static void clear_glock(struct gfs2_glock *gl)
1990 struct gfs2_sbd *sdp = gl->gl_sbd;
1993 spin_lock(&sdp->sd_reclaim_lock);
1994 if (!list_empty(&gl->gl_reclaim)) {
1995 list_del_init(&gl->gl_reclaim);
1996 atomic_dec(&sdp->sd_reclaim_count);
1997 spin_unlock(&sdp->sd_reclaim_lock);
1998 released = gfs2_glock_put(gl);
1999 gfs2_assert(sdp, !released);
2001 spin_unlock(&sdp->sd_reclaim_lock);
2004 if (gfs2_glmutex_trylock(gl)) {
2005 if (queue_empty(gl, &gl->gl_holders) &&
2006 gl->gl_state != LM_ST_UNLOCKED)
2007 handle_callback(gl, LM_ST_UNLOCKED);
2009 gfs2_glmutex_unlock(gl);
2016 * gfs2_gl_hash_clear - Empty out the glock hash table
2017 * @sdp: the filesystem
2018 * @wait: wait until it's all gone
2020 * Called when unmounting the filesystem, or when inter-node lock manager
2021 * requests DROPLOCKS because it is running out of capacity.
2024 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
2035 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
2036 if (examine_bucket(clear_glock, sdp,
2037 &sdp->sd_gl_hash[x]))
2043 if (time_after_eq(jiffies,
2044 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
2045 fs_warn(sdp, "Unmount seems to be stalled. "
2046 "Dumping lock state...\n");
2047 gfs2_dump_lockstate(sdp);
2051 invalidate_inodes(sdp->sd_vfs);
2057 * Diagnostic routines to help debug distributed deadlock
2061 * dump_holder - print information about a glock holder
2062 * @str: a string naming the type of holder
2063 * @gh: the glock holder
2065 * Returns: 0 on success, -ENOBUFS when we run out of space
2068 static int dump_holder(char *str, struct gfs2_holder *gh)
2071 int error = -ENOBUFS;
2073 printk(KERN_INFO " %s\n", str);
2074 printk(KERN_INFO " owner = %ld\n",
2075 (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
2076 printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
2077 printk(KERN_INFO " gh_flags =");
2078 for (x = 0; x < 32; x++)
2079 if (gh->gh_flags & (1 << x))
2082 printk(KERN_INFO " error = %d\n", gh->gh_error);
2083 printk(KERN_INFO " gh_iflags =");
2084 for (x = 0; x < 32; x++)
2085 if (test_bit(x, &gh->gh_iflags))
2088 print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip);
2096 * dump_inode - print information about an inode
2099 * Returns: 0 on success, -ENOBUFS when we run out of space
2102 static int dump_inode(struct gfs2_inode *ip)
2105 int error = -ENOBUFS;
2107 printk(KERN_INFO " Inode:\n");
2108 printk(KERN_INFO " num = %llu %llu\n",
2109 (unsigned long long)ip->i_num.no_formal_ino,
2110 (unsigned long long)ip->i_num.no_addr);
2111 printk(KERN_INFO " type = %u\n", IF2DT(ip->i_di.di_mode));
2112 printk(KERN_INFO " i_flags =");
2113 for (x = 0; x < 32; x++)
2114 if (test_bit(x, &ip->i_flags))
2124 * dump_glock - print information about a glock
2126 * @count: where we are in the buffer
2128 * Returns: 0 on success, -ENOBUFS when we run out of space
2131 static int dump_glock(struct gfs2_glock *gl)
2133 struct gfs2_holder *gh;
2135 int error = -ENOBUFS;
2137 spin_lock(&gl->gl_spin);
2139 printk(KERN_INFO "Glock 0x%p (%u, %llu)\n",
2141 gl->gl_name.ln_type,
2142 (unsigned long long)gl->gl_name.ln_number);
2143 printk(KERN_INFO " gl_flags =");
2144 for (x = 0; x < 32; x++)
2145 if (test_bit(x, &gl->gl_flags))
2148 printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref.refcount));
2149 printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
2150 printk(KERN_INFO " gl_owner = %s\n", gl->gl_owner->comm);
2151 print_symbol(KERN_INFO " gl_ip = %s\n", gl->gl_ip);
2152 printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
2153 printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
2154 printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
2155 printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
2156 printk(KERN_INFO " le = %s\n",
2157 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
2158 printk(KERN_INFO " reclaim = %s\n",
2159 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
2161 printk(KERN_INFO " aspace = 0x%p nrpages = %lu\n",
2163 gl->gl_aspace->i_mapping->nrpages);
2165 printk(KERN_INFO " aspace = no\n");
2166 printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
2167 if (gl->gl_req_gh) {
2168 error = dump_holder("Request", gl->gl_req_gh);
2172 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
2173 error = dump_holder("Holder", gh);
2177 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
2178 error = dump_holder("Waiter1", gh);
2182 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
2183 error = dump_holder("Waiter2", gh);
2187 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
2188 error = dump_holder("Waiter3", gh);
2192 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
2193 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
2194 list_empty(&gl->gl_holders)) {
2195 error = dump_inode(gl->gl_object);
2200 printk(KERN_INFO " Inode: busy\n");
2207 spin_unlock(&gl->gl_spin);
2213 * gfs2_dump_lockstate - print out the current lockstate
2214 * @sdp: the filesystem
2215 * @ub: the buffer to copy the information into
2217 * If @ub is NULL, dump the lockstate to the console.
2221 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
2223 struct gfs2_gl_hash_bucket *bucket;
2224 struct gfs2_glock *gl;
2228 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2229 bucket = &sdp->sd_gl_hash[x];
2231 read_lock(&bucket->hb_lock);
2233 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
2234 if (test_bit(GLF_PLUG, &gl->gl_flags))
2237 error = dump_glock(gl);
2242 read_unlock(&bucket->hb_lock);