2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 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/gfs2_ondisk.h>
20 #include <asm/semaphore.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);
50 * relaxed_state_ok - is a requested lock compatible with the current lock mode?
51 * @actual: the current state of the lock
52 * @requested: the lock state that was requested by the caller
53 * @flags: the modifier flags passed in by the caller
55 * Returns: 1 if the locks are compatible, 0 otherwise
58 static inline int relaxed_state_ok(unsigned int actual, unsigned requested,
61 if (actual == requested)
67 if (actual == LM_ST_EXCLUSIVE && requested == LM_ST_SHARED)
70 if (actual != LM_ST_UNLOCKED && (flags & LM_FLAG_ANY))
77 * gl_hash() - Turn glock number into hash bucket number
78 * @lock: The glock number
80 * Returns: The number of the corresponding hash bucket
83 static unsigned int gl_hash(struct lm_lockname *name)
87 h = jhash(&name->ln_number, sizeof(uint64_t), 0);
88 h = jhash(&name->ln_type, sizeof(unsigned int), h);
89 h &= GFS2_GL_HASH_MASK;
95 * glock_free() - Perform a few checks and then release struct gfs2_glock
96 * @gl: The glock to release
98 * Also calls lock module to release its internal structure for this glock.
102 static void glock_free(struct gfs2_glock *gl)
104 struct gfs2_sbd *sdp = gl->gl_sbd;
105 struct inode *aspace = gl->gl_aspace;
107 gfs2_lm_put_lock(sdp, gl->gl_lock);
110 gfs2_aspace_put(aspace);
112 kmem_cache_free(gfs2_glock_cachep, gl);
116 * gfs2_glock_hold() - increment reference count on glock
117 * @gl: The glock to hold
121 void gfs2_glock_hold(struct gfs2_glock *gl)
123 kref_get(&gl->gl_ref);
126 /* All work is done after the return from kref_put() so we
127 can release the write_lock before the free. */
129 static void kill_glock(struct kref *kref)
131 struct gfs2_glock *gl = container_of(kref, struct gfs2_glock, gl_ref);
132 struct gfs2_sbd *sdp = gl->gl_sbd;
134 gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED);
135 gfs2_assert(sdp, list_empty(&gl->gl_reclaim));
136 gfs2_assert(sdp, list_empty(&gl->gl_holders));
137 gfs2_assert(sdp, list_empty(&gl->gl_waiters1));
138 gfs2_assert(sdp, list_empty(&gl->gl_waiters2));
139 gfs2_assert(sdp, list_empty(&gl->gl_waiters3));
143 * gfs2_glock_put() - Decrement reference count on glock
144 * @gl: The glock to put
148 int gfs2_glock_put(struct gfs2_glock *gl)
150 struct gfs2_sbd *sdp = gl->gl_sbd;
151 struct gfs2_gl_hash_bucket *bucket = gl->gl_bucket;
154 mutex_lock(&sdp->sd_invalidate_inodes_mutex);
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);
164 write_unlock(&bucket->hb_lock);
166 mutex_unlock(&sdp->sd_invalidate_inodes_mutex);
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 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 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);
281 memset(gl, 0, sizeof(struct gfs2_glock));
283 INIT_LIST_HEAD(&gl->gl_list);
285 kref_init(&gl->gl_ref);
287 spin_lock_init(&gl->gl_spin);
289 gl->gl_state = LM_ST_UNLOCKED;
290 INIT_LIST_HEAD(&gl->gl_holders);
291 INIT_LIST_HEAD(&gl->gl_waiters1);
292 INIT_LIST_HEAD(&gl->gl_waiters2);
293 INIT_LIST_HEAD(&gl->gl_waiters3);
297 gl->gl_bucket = bucket;
298 INIT_LIST_HEAD(&gl->gl_reclaim);
302 lops_init_le(&gl->gl_le, &gfs2_glock_lops);
303 INIT_LIST_HEAD(&gl->gl_ail_list);
305 /* If this glock protects actual on-disk data or metadata blocks,
306 create a VFS inode to manage the pages/buffers holding them. */
307 if (glops == &gfs2_inode_glops ||
308 glops == &gfs2_rgrp_glops ||
309 glops == &gfs2_meta_glops) {
310 gl->gl_aspace = gfs2_aspace_get(sdp);
311 if (!gl->gl_aspace) {
317 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
321 write_lock(&bucket->hb_lock);
322 tmp = search_bucket(bucket, &name);
324 write_unlock(&bucket->hb_lock);
328 list_add_tail(&gl->gl_list, &bucket->hb_list);
329 write_unlock(&bucket->hb_lock);
338 gfs2_aspace_put(gl->gl_aspace);
341 kmem_cache_free(gfs2_glock_cachep, gl);
347 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
349 * @state: the state we're requesting
350 * @flags: the modifier flags
351 * @gh: the holder structure
355 void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, int flags,
356 struct gfs2_holder *gh)
358 INIT_LIST_HEAD(&gh->gh_list);
360 gh->gh_owner = (flags & GL_NEVER_RECURSE) ? NULL : current;
361 gh->gh_state = state;
362 gh->gh_flags = flags;
365 init_completion(&gh->gh_wait);
367 if (gh->gh_state == LM_ST_EXCLUSIVE)
368 gh->gh_flags |= GL_LOCAL_EXCL;
374 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
375 * @state: the state we're requesting
376 * @flags: the modifier flags
377 * @gh: the holder structure
379 * Don't mess with the glock.
383 void gfs2_holder_reinit(unsigned int state, int flags, struct gfs2_holder *gh)
385 gh->gh_state = state;
386 gh->gh_flags = flags;
387 if (gh->gh_state == LM_ST_EXCLUSIVE)
388 gh->gh_flags |= GL_LOCAL_EXCL;
390 gh->gh_iflags &= 1 << HIF_ALLOCED;
394 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
395 * @gh: the holder structure
399 void gfs2_holder_uninit(struct gfs2_holder *gh)
401 gfs2_glock_put(gh->gh_gl);
406 * gfs2_holder_get - get a struct gfs2_holder structure
408 * @state: the state we're requesting
409 * @flags: the modifier flags
410 * @gfp_flags: __GFP_NOFAIL
412 * Figure out how big an impact this function has. Either:
413 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
414 * 2) Leave it like it is
416 * Returns: the holder structure, NULL on ENOMEM
419 struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl, unsigned int state,
420 int flags, gfp_t gfp_flags)
422 struct gfs2_holder *gh;
424 gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags);
428 gfs2_holder_init(gl, state, flags, gh);
429 set_bit(HIF_ALLOCED, &gh->gh_iflags);
435 * gfs2_holder_put - get rid of a struct gfs2_holder structure
436 * @gh: the holder structure
440 void gfs2_holder_put(struct gfs2_holder *gh)
442 gfs2_holder_uninit(gh);
447 * handle_recurse - put other holder structures (marked recursive)
448 * into the holders list
449 * @gh: the holder structure
453 static void handle_recurse(struct gfs2_holder *gh)
455 struct gfs2_glock *gl = gh->gh_gl;
456 struct gfs2_sbd *sdp = gl->gl_sbd;
457 struct gfs2_holder *tmp_gh, *safe;
460 if (gfs2_assert_warn(sdp, gh->gh_owner))
463 list_for_each_entry_safe(tmp_gh, safe, &gl->gl_waiters3, gh_list) {
464 if (tmp_gh->gh_owner != gh->gh_owner)
467 gfs2_assert_warn(sdp,
468 test_bit(HIF_RECURSE, &tmp_gh->gh_iflags));
470 list_move_tail(&tmp_gh->gh_list, &gl->gl_holders);
471 tmp_gh->gh_error = 0;
472 set_bit(HIF_HOLDER, &tmp_gh->gh_iflags);
474 complete(&tmp_gh->gh_wait);
479 gfs2_assert_warn(sdp, found);
483 * do_unrecurse - a recursive holder was just dropped of the waiters3 list
486 * If there is only one other recursive holder, clear its HIF_RECURSE bit.
487 * If there is more than one, leave them alone.
491 static void do_unrecurse(struct gfs2_holder *gh)
493 struct gfs2_glock *gl = gh->gh_gl;
494 struct gfs2_sbd *sdp = gl->gl_sbd;
495 struct gfs2_holder *tmp_gh, *last_gh = NULL;
498 if (gfs2_assert_warn(sdp, gh->gh_owner))
501 list_for_each_entry(tmp_gh, &gl->gl_waiters3, gh_list) {
502 if (tmp_gh->gh_owner != gh->gh_owner)
505 gfs2_assert_warn(sdp,
506 test_bit(HIF_RECURSE, &tmp_gh->gh_iflags));
515 if (!gfs2_assert_warn(sdp, found))
516 clear_bit(HIF_RECURSE, &last_gh->gh_iflags);
520 * rq_mutex - process a mutex request in the queue
521 * @gh: the glock holder
523 * Returns: 1 if the queue is blocked
526 static int rq_mutex(struct gfs2_holder *gh)
528 struct gfs2_glock *gl = gh->gh_gl;
530 list_del_init(&gh->gh_list);
531 /* gh->gh_error never examined. */
532 set_bit(GLF_LOCK, &gl->gl_flags);
533 complete(&gh->gh_wait);
539 * rq_promote - process a promote request in the queue
540 * @gh: the glock holder
542 * Acquire a new inter-node lock, or change a lock state to more restrictive.
544 * Returns: 1 if the queue is blocked
547 static int rq_promote(struct gfs2_holder *gh)
549 struct gfs2_glock *gl = gh->gh_gl;
550 struct gfs2_sbd *sdp = gl->gl_sbd;
551 struct gfs2_glock_operations *glops = gl->gl_ops;
554 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
555 if (list_empty(&gl->gl_holders)) {
557 set_bit(GLF_LOCK, &gl->gl_flags);
558 spin_unlock(&gl->gl_spin);
560 if (atomic_read(&sdp->sd_reclaim_count) >
561 gfs2_tune_get(sdp, gt_reclaim_limit) &&
562 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
563 gfs2_reclaim_glock(sdp);
564 gfs2_reclaim_glock(sdp);
567 glops->go_xmote_th(gl, gh->gh_state,
570 spin_lock(&gl->gl_spin);
575 if (list_empty(&gl->gl_holders)) {
576 set_bit(HIF_FIRST, &gh->gh_iflags);
577 set_bit(GLF_LOCK, &gl->gl_flags);
580 struct gfs2_holder *next_gh;
581 if (gh->gh_flags & GL_LOCAL_EXCL)
583 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
585 if (next_gh->gh_flags & GL_LOCAL_EXCL)
587 recurse = test_bit(HIF_RECURSE, &gh->gh_iflags);
590 list_move_tail(&gh->gh_list, &gl->gl_holders);
592 set_bit(HIF_HOLDER, &gh->gh_iflags);
597 complete(&gh->gh_wait);
603 * rq_demote - process a demote request in the queue
604 * @gh: the glock holder
606 * Returns: 1 if the queue is blocked
609 static int rq_demote(struct gfs2_holder *gh)
611 struct gfs2_glock *gl = gh->gh_gl;
612 struct gfs2_glock_operations *glops = gl->gl_ops;
614 if (!list_empty(&gl->gl_holders))
617 if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) {
618 list_del_init(&gh->gh_list);
620 spin_unlock(&gl->gl_spin);
621 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
624 complete(&gh->gh_wait);
625 spin_lock(&gl->gl_spin);
628 set_bit(GLF_LOCK, &gl->gl_flags);
629 spin_unlock(&gl->gl_spin);
631 if (gh->gh_state == LM_ST_UNLOCKED ||
632 gl->gl_state != LM_ST_EXCLUSIVE)
633 glops->go_drop_th(gl);
635 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
637 spin_lock(&gl->gl_spin);
644 * rq_greedy - process a queued request to drop greedy status
645 * @gh: the glock holder
647 * Returns: 1 if the queue is blocked
650 static int rq_greedy(struct gfs2_holder *gh)
652 struct gfs2_glock *gl = gh->gh_gl;
654 list_del_init(&gh->gh_list);
655 /* gh->gh_error never examined. */
656 clear_bit(GLF_GREEDY, &gl->gl_flags);
657 spin_unlock(&gl->gl_spin);
659 gfs2_holder_uninit(gh);
660 kfree(container_of(gh, struct greedy, gr_gh));
662 spin_lock(&gl->gl_spin);
668 * run_queue - process holder structures on a glock
673 static void run_queue(struct gfs2_glock *gl)
675 struct gfs2_holder *gh;
679 if (test_bit(GLF_LOCK, &gl->gl_flags))
682 if (!list_empty(&gl->gl_waiters1)) {
683 gh = list_entry(gl->gl_waiters1.next,
684 struct gfs2_holder, gh_list);
686 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
687 blocked = rq_mutex(gh);
689 gfs2_assert_warn(gl->gl_sbd, 0);
691 } else if (!list_empty(&gl->gl_waiters2) &&
692 !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) {
693 gh = list_entry(gl->gl_waiters2.next,
694 struct gfs2_holder, gh_list);
696 if (test_bit(HIF_DEMOTE, &gh->gh_iflags))
697 blocked = rq_demote(gh);
698 else if (test_bit(HIF_GREEDY, &gh->gh_iflags))
699 blocked = rq_greedy(gh);
701 gfs2_assert_warn(gl->gl_sbd, 0);
703 } else if (!list_empty(&gl->gl_waiters3)) {
704 gh = list_entry(gl->gl_waiters3.next,
705 struct gfs2_holder, gh_list);
707 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
708 blocked = rq_promote(gh);
710 gfs2_assert_warn(gl->gl_sbd, 0);
721 * gfs2_glmutex_lock - acquire a local lock on a glock
724 * Gives caller exclusive access to manipulate a glock structure.
727 void gfs2_glmutex_lock(struct gfs2_glock *gl)
729 struct gfs2_holder gh;
731 gfs2_holder_init(gl, 0, 0, &gh);
732 set_bit(HIF_MUTEX, &gh.gh_iflags);
734 spin_lock(&gl->gl_spin);
735 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
736 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
738 complete(&gh.gh_wait);
739 spin_unlock(&gl->gl_spin);
741 wait_for_completion(&gh.gh_wait);
742 gfs2_holder_uninit(&gh);
746 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
749 * Returns: 1 if the glock is acquired
752 int gfs2_glmutex_trylock(struct gfs2_glock *gl)
756 spin_lock(&gl->gl_spin);
757 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
759 spin_unlock(&gl->gl_spin);
765 * gfs2_glmutex_unlock - release a local lock on a glock
770 void gfs2_glmutex_unlock(struct gfs2_glock *gl)
772 spin_lock(&gl->gl_spin);
773 clear_bit(GLF_LOCK, &gl->gl_flags);
775 spin_unlock(&gl->gl_spin);
779 * handle_callback - add a demote request to a lock's queue
781 * @state: the state the caller wants us to change to
785 static void handle_callback(struct gfs2_glock *gl, unsigned int state)
787 struct gfs2_holder *gh, *new_gh = NULL;
790 spin_lock(&gl->gl_spin);
792 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
793 if (test_bit(HIF_DEMOTE, &gh->gh_iflags) &&
794 gl->gl_req_gh != gh) {
795 if (gh->gh_state != state)
796 gh->gh_state = LM_ST_UNLOCKED;
802 list_add_tail(&new_gh->gh_list, &gl->gl_waiters2);
805 spin_unlock(&gl->gl_spin);
807 new_gh = gfs2_holder_get(gl, state,
808 LM_FLAG_TRY | GL_NEVER_RECURSE,
809 GFP_KERNEL | __GFP_NOFAIL),
810 set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
811 set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
817 spin_unlock(&gl->gl_spin);
820 gfs2_holder_put(new_gh);
824 * state_change - record that the glock is now in a different state
826 * @new_state the new state
830 static void state_change(struct gfs2_glock *gl, unsigned int new_state)
834 held1 = (gl->gl_state != LM_ST_UNLOCKED);
835 held2 = (new_state != LM_ST_UNLOCKED);
837 if (held1 != held2) {
844 gl->gl_state = new_state;
848 * xmote_bh - Called after the lock module is done acquiring a lock
849 * @gl: The glock in question
850 * @ret: the int returned from the lock module
854 static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
856 struct gfs2_sbd *sdp = gl->gl_sbd;
857 struct gfs2_glock_operations *glops = gl->gl_ops;
858 struct gfs2_holder *gh = gl->gl_req_gh;
859 int prev_state = gl->gl_state;
862 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
863 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
864 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
866 state_change(gl, ret & LM_OUT_ST_MASK);
868 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
870 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
871 } else if (gl->gl_state == LM_ST_DEFERRED) {
872 /* We might not want to do this here.
873 Look at moving to the inode glops. */
875 glops->go_inval(gl, DIO_DATA);
878 /* Deal with each possible exit condition */
881 gl->gl_stamp = jiffies;
883 else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
884 spin_lock(&gl->gl_spin);
885 list_del_init(&gh->gh_list);
887 if (test_bit(HIF_RECURSE, &gh->gh_iflags))
889 spin_unlock(&gl->gl_spin);
891 } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
892 spin_lock(&gl->gl_spin);
893 list_del_init(&gh->gh_list);
894 if (gl->gl_state == gh->gh_state ||
895 gl->gl_state == LM_ST_UNLOCKED)
898 if (gfs2_assert_warn(sdp, gh->gh_flags &
899 (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
900 fs_warn(sdp, "ret = 0x%.8X\n", ret);
901 gh->gh_error = GLR_TRYFAILED;
903 spin_unlock(&gl->gl_spin);
905 if (ret & LM_OUT_CANCELED)
906 handle_callback(gl, LM_ST_UNLOCKED); /* Lame */
908 } else if (ret & LM_OUT_CANCELED) {
909 spin_lock(&gl->gl_spin);
910 list_del_init(&gh->gh_list);
911 gh->gh_error = GLR_CANCELED;
912 if (test_bit(HIF_RECURSE, &gh->gh_iflags))
914 spin_unlock(&gl->gl_spin);
916 } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
917 spin_lock(&gl->gl_spin);
918 list_move_tail(&gh->gh_list, &gl->gl_holders);
920 set_bit(HIF_HOLDER, &gh->gh_iflags);
921 spin_unlock(&gl->gl_spin);
923 set_bit(HIF_FIRST, &gh->gh_iflags);
927 } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
928 spin_lock(&gl->gl_spin);
929 list_del_init(&gh->gh_list);
930 gh->gh_error = GLR_TRYFAILED;
931 if (test_bit(HIF_RECURSE, &gh->gh_iflags))
933 spin_unlock(&gl->gl_spin);
936 if (gfs2_assert_withdraw(sdp, 0) == -1)
937 fs_err(sdp, "ret = 0x%.8X\n", ret);
940 if (glops->go_xmote_bh)
941 glops->go_xmote_bh(gl);
944 spin_lock(&gl->gl_spin);
945 gl->gl_req_gh = NULL;
946 gl->gl_req_bh = NULL;
947 clear_bit(GLF_LOCK, &gl->gl_flags);
949 spin_unlock(&gl->gl_spin);
955 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
958 complete(&gh->gh_wait);
963 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
964 * @gl: The glock in question
965 * @state: the requested state
966 * @flags: modifier flags to the lock call
970 void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags)
972 struct gfs2_sbd *sdp = gl->gl_sbd;
973 struct gfs2_glock_operations *glops = gl->gl_ops;
974 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
975 LM_FLAG_NOEXP | LM_FLAG_ANY |
977 unsigned int lck_ret;
979 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
980 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
981 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
982 gfs2_assert_warn(sdp, state != gl->gl_state);
984 if (gl->gl_state == LM_ST_EXCLUSIVE) {
987 DIO_METADATA | DIO_DATA | DIO_RELEASE);
991 gl->gl_req_bh = xmote_bh;
993 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state,
996 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
999 if (lck_ret & LM_OUT_ASYNC)
1000 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
1002 xmote_bh(gl, lck_ret);
1006 * drop_bh - Called after a lock module unlock completes
1008 * @ret: the return status
1010 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
1011 * Doesn't drop the reference on the glock the top half took out
1015 static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
1017 struct gfs2_sbd *sdp = gl->gl_sbd;
1018 struct gfs2_glock_operations *glops = gl->gl_ops;
1019 struct gfs2_holder *gh = gl->gl_req_gh;
1021 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1023 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1024 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
1025 gfs2_assert_warn(sdp, !ret);
1027 state_change(gl, LM_ST_UNLOCKED);
1029 if (glops->go_inval)
1030 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
1033 spin_lock(&gl->gl_spin);
1034 list_del_init(&gh->gh_list);
1036 spin_unlock(&gl->gl_spin);
1039 if (glops->go_drop_bh)
1040 glops->go_drop_bh(gl);
1042 spin_lock(&gl->gl_spin);
1043 gl->gl_req_gh = NULL;
1044 gl->gl_req_bh = NULL;
1045 clear_bit(GLF_LOCK, &gl->gl_flags);
1047 spin_unlock(&gl->gl_spin);
1052 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
1053 gfs2_holder_put(gh);
1055 complete(&gh->gh_wait);
1060 * gfs2_glock_drop_th - call into the lock module to unlock a lock
1065 void gfs2_glock_drop_th(struct gfs2_glock *gl)
1067 struct gfs2_sbd *sdp = gl->gl_sbd;
1068 struct gfs2_glock_operations *glops = gl->gl_ops;
1071 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1072 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
1073 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
1075 if (gl->gl_state == LM_ST_EXCLUSIVE) {
1078 DIO_METADATA | DIO_DATA | DIO_RELEASE);
1081 gfs2_glock_hold(gl);
1082 gl->gl_req_bh = drop_bh;
1084 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
1086 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
1092 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
1096 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1097 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1099 * Don't cancel GL_NOCANCEL requests.
1102 static void do_cancels(struct gfs2_holder *gh)
1104 struct gfs2_glock *gl = gh->gh_gl;
1106 spin_lock(&gl->gl_spin);
1108 while (gl->gl_req_gh != gh &&
1109 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1110 !list_empty(&gh->gh_list)) {
1111 if (gl->gl_req_bh &&
1113 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
1114 spin_unlock(&gl->gl_spin);
1115 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
1117 spin_lock(&gl->gl_spin);
1119 spin_unlock(&gl->gl_spin);
1121 spin_lock(&gl->gl_spin);
1125 spin_unlock(&gl->gl_spin);
1129 * glock_wait_internal - wait on a glock acquisition
1130 * @gh: the glock holder
1132 * Returns: 0 on success
1135 static int glock_wait_internal(struct gfs2_holder *gh)
1137 struct gfs2_glock *gl = gh->gh_gl;
1138 struct gfs2_sbd *sdp = gl->gl_sbd;
1139 struct gfs2_glock_operations *glops = gl->gl_ops;
1141 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1144 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1145 spin_lock(&gl->gl_spin);
1146 if (gl->gl_req_gh != gh &&
1147 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1148 !list_empty(&gh->gh_list)) {
1149 list_del_init(&gh->gh_list);
1150 gh->gh_error = GLR_TRYFAILED;
1151 if (test_bit(HIF_RECURSE, &gh->gh_iflags))
1154 spin_unlock(&gl->gl_spin);
1155 return gh->gh_error;
1157 spin_unlock(&gl->gl_spin);
1160 if (gh->gh_flags & LM_FLAG_PRIORITY)
1163 wait_for_completion(&gh->gh_wait);
1166 return gh->gh_error;
1168 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
1169 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state,
1173 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1174 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1176 if (glops->go_lock) {
1177 gh->gh_error = glops->go_lock(gh);
1179 spin_lock(&gl->gl_spin);
1180 list_del_init(&gh->gh_list);
1181 if (test_and_clear_bit(HIF_RECURSE,
1184 spin_unlock(&gl->gl_spin);
1188 spin_lock(&gl->gl_spin);
1189 gl->gl_req_gh = NULL;
1190 gl->gl_req_bh = NULL;
1191 clear_bit(GLF_LOCK, &gl->gl_flags);
1192 if (test_bit(HIF_RECURSE, &gh->gh_iflags))
1195 spin_unlock(&gl->gl_spin);
1198 return gh->gh_error;
1201 static inline struct gfs2_holder *
1202 find_holder_by_owner(struct list_head *head, struct task_struct *owner)
1204 struct gfs2_holder *gh;
1206 list_for_each_entry(gh, head, gh_list) {
1207 if (gh->gh_owner == owner)
1217 * Make sure the new holder is compatible with the pre-existing one.
1221 static int recurse_check(struct gfs2_holder *existing, struct gfs2_holder *new,
1224 struct gfs2_sbd *sdp = existing->gh_gl->gl_sbd;
1226 if (gfs2_assert_warn(sdp, (new->gh_flags & LM_FLAG_ANY) ||
1227 !(existing->gh_flags & LM_FLAG_ANY)))
1230 if (gfs2_assert_warn(sdp, (existing->gh_flags & GL_LOCAL_EXCL) ||
1231 !(new->gh_flags & GL_LOCAL_EXCL)))
1234 if (gfs2_assert_warn(sdp, relaxed_state_ok(state, new->gh_state,
1241 set_bit(HIF_ABORTED, &new->gh_iflags);
1246 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1247 * @gh: the holder structure to add
1251 static void add_to_queue(struct gfs2_holder *gh)
1253 struct gfs2_glock *gl = gh->gh_gl;
1254 struct gfs2_holder *existing;
1259 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
1261 if (recurse_check(existing, gh, gl->gl_state))
1264 list_add_tail(&gh->gh_list, &gl->gl_holders);
1265 set_bit(HIF_HOLDER, &gh->gh_iflags);
1268 complete(&gh->gh_wait);
1273 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
1275 if (recurse_check(existing, gh, existing->gh_state))
1278 set_bit(HIF_RECURSE, &gh->gh_iflags);
1279 set_bit(HIF_RECURSE, &existing->gh_iflags);
1281 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1287 if (gh->gh_flags & LM_FLAG_PRIORITY)
1288 list_add(&gh->gh_list, &gl->gl_waiters3);
1290 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1294 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1295 * @gh: the holder structure
1297 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1299 * Returns: 0, GLR_TRYFAILED, or errno on failure
1302 int gfs2_glock_nq(struct gfs2_holder *gh)
1304 struct gfs2_glock *gl = gh->gh_gl;
1305 struct gfs2_sbd *sdp = gl->gl_sbd;
1309 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1310 set_bit(HIF_ABORTED, &gh->gh_iflags);
1314 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1316 spin_lock(&gl->gl_spin);
1319 spin_unlock(&gl->gl_spin);
1321 if (!(gh->gh_flags & GL_ASYNC)) {
1322 error = glock_wait_internal(gh);
1323 if (error == GLR_CANCELED) {
1329 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1335 * gfs2_glock_poll - poll to see if an async request has been completed
1338 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1341 int gfs2_glock_poll(struct gfs2_holder *gh)
1343 struct gfs2_glock *gl = gh->gh_gl;
1346 spin_lock(&gl->gl_spin);
1348 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1350 else if (list_empty(&gh->gh_list)) {
1351 if (gh->gh_error == GLR_CANCELED) {
1352 spin_unlock(&gl->gl_spin);
1354 if (gfs2_glock_nq(gh))
1361 spin_unlock(&gl->gl_spin);
1367 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1368 * @gh: the holder structure
1370 * Returns: 0, GLR_TRYFAILED, or errno on failure
1373 int gfs2_glock_wait(struct gfs2_holder *gh)
1377 error = glock_wait_internal(gh);
1378 if (error == GLR_CANCELED) {
1380 gh->gh_flags &= ~GL_ASYNC;
1381 error = gfs2_glock_nq(gh);
1388 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1389 * @gh: the glock holder
1393 void gfs2_glock_dq(struct gfs2_holder *gh)
1395 struct gfs2_glock *gl = gh->gh_gl;
1396 struct gfs2_glock_operations *glops = gl->gl_ops;
1398 if (gh->gh_flags & GL_SYNC)
1399 set_bit(GLF_SYNC, &gl->gl_flags);
1401 if (gh->gh_flags & GL_NOCACHE)
1402 handle_callback(gl, LM_ST_UNLOCKED);
1404 gfs2_glmutex_lock(gl);
1406 spin_lock(&gl->gl_spin);
1407 list_del_init(&gh->gh_list);
1409 if (list_empty(&gl->gl_holders)) {
1410 spin_unlock(&gl->gl_spin);
1412 if (glops->go_unlock)
1413 glops->go_unlock(gh);
1415 if (test_bit(GLF_SYNC, &gl->gl_flags)) {
1417 glops->go_sync(gl, DIO_METADATA | DIO_DATA);
1420 gl->gl_stamp = jiffies;
1422 spin_lock(&gl->gl_spin);
1425 clear_bit(GLF_LOCK, &gl->gl_flags);
1427 spin_unlock(&gl->gl_spin);
1431 * gfs2_glock_prefetch - Try to prefetch a glock
1433 * @state: the state to prefetch in
1434 * @flags: flags passed to go_xmote_th()
1438 void gfs2_glock_prefetch(struct gfs2_glock *gl, unsigned int state, int flags)
1440 struct gfs2_glock_operations *glops = gl->gl_ops;
1442 spin_lock(&gl->gl_spin);
1444 if (test_bit(GLF_LOCK, &gl->gl_flags) ||
1445 !list_empty(&gl->gl_holders) ||
1446 !list_empty(&gl->gl_waiters1) ||
1447 !list_empty(&gl->gl_waiters2) ||
1448 !list_empty(&gl->gl_waiters3) ||
1449 relaxed_state_ok(gl->gl_state, state, flags)) {
1450 spin_unlock(&gl->gl_spin);
1454 set_bit(GLF_PREFETCH, &gl->gl_flags);
1455 set_bit(GLF_LOCK, &gl->gl_flags);
1456 spin_unlock(&gl->gl_spin);
1458 glops->go_xmote_th(gl, state, flags);
1462 * gfs2_glock_force_drop - Force a glock to be uncached
1467 void gfs2_glock_force_drop(struct gfs2_glock *gl)
1469 struct gfs2_holder gh;
1471 gfs2_holder_init(gl, LM_ST_UNLOCKED, GL_NEVER_RECURSE, &gh);
1472 set_bit(HIF_DEMOTE, &gh.gh_iflags);
1474 spin_lock(&gl->gl_spin);
1475 list_add_tail(&gh.gh_list, &gl->gl_waiters2);
1477 spin_unlock(&gl->gl_spin);
1479 wait_for_completion(&gh.gh_wait);
1480 gfs2_holder_uninit(&gh);
1483 static void greedy_work(void *data)
1485 struct greedy *gr = (struct greedy *)data;
1486 struct gfs2_holder *gh = &gr->gr_gh;
1487 struct gfs2_glock *gl = gh->gh_gl;
1488 struct gfs2_glock_operations *glops = gl->gl_ops;
1490 clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1492 if (glops->go_greedy)
1493 glops->go_greedy(gl);
1495 spin_lock(&gl->gl_spin);
1497 if (list_empty(&gl->gl_waiters2)) {
1498 clear_bit(GLF_GREEDY, &gl->gl_flags);
1499 spin_unlock(&gl->gl_spin);
1500 gfs2_holder_uninit(gh);
1503 gfs2_glock_hold(gl);
1504 list_add_tail(&gh->gh_list, &gl->gl_waiters2);
1506 spin_unlock(&gl->gl_spin);
1512 * gfs2_glock_be_greedy -
1516 * Returns: 0 if go_greedy will be called, 1 otherwise
1519 int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time)
1522 struct gfs2_holder *gh;
1525 gl->gl_sbd->sd_args.ar_localcaching ||
1526 test_and_set_bit(GLF_GREEDY, &gl->gl_flags))
1529 gr = kmalloc(sizeof(struct greedy), GFP_KERNEL);
1531 clear_bit(GLF_GREEDY, &gl->gl_flags);
1536 gfs2_holder_init(gl, 0, GL_NEVER_RECURSE, gh);
1537 set_bit(HIF_GREEDY, &gh->gh_iflags);
1538 INIT_WORK(&gr->gr_work, greedy_work, gr);
1540 set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1541 schedule_delayed_work(&gr->gr_work, time);
1547 * gfs2_glock_nq_init - intialize a holder and enqueue it on a glock
1549 * @state: the state we're requesting
1550 * @flags: the modifier flags
1551 * @gh: the holder structure
1553 * Returns: 0, GLR_*, or errno
1556 int gfs2_glock_nq_init(struct gfs2_glock *gl, unsigned int state, int flags,
1557 struct gfs2_holder *gh)
1561 gfs2_holder_init(gl, state, flags, gh);
1563 error = gfs2_glock_nq(gh);
1565 gfs2_holder_uninit(gh);
1571 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1572 * @gh: the holder structure
1576 void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1579 gfs2_holder_uninit(gh);
1583 * gfs2_glock_nq_num - acquire a glock based on lock number
1584 * @sdp: the filesystem
1585 * @number: the lock number
1586 * @glops: the glock operations for the type of glock
1587 * @state: the state to acquire the glock in
1588 * @flags: modifier flags for the aquisition
1589 * @gh: the struct gfs2_holder
1594 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, uint64_t number,
1595 struct gfs2_glock_operations *glops, unsigned int state,
1596 int flags, struct gfs2_holder *gh)
1598 struct gfs2_glock *gl;
1601 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1603 error = gfs2_glock_nq_init(gl, state, flags, gh);
1611 * glock_compare - Compare two struct gfs2_glock structures for sorting
1612 * @arg_a: the first structure
1613 * @arg_b: the second structure
1617 static int glock_compare(const void *arg_a, const void *arg_b)
1619 struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
1620 struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
1621 struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1622 struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1625 if (a->ln_number > b->ln_number)
1627 else if (a->ln_number < b->ln_number)
1630 if (gh_a->gh_state == LM_ST_SHARED &&
1631 gh_b->gh_state == LM_ST_EXCLUSIVE)
1633 else if (!(gh_a->gh_flags & GL_LOCAL_EXCL) &&
1634 (gh_b->gh_flags & GL_LOCAL_EXCL))
1642 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1643 * @num_gh: the number of structures
1644 * @ghs: an array of struct gfs2_holder structures
1646 * Returns: 0 on success (all glocks acquired),
1647 * errno on failure (no glocks acquired)
1650 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1651 struct gfs2_holder **p)
1656 for (x = 0; x < num_gh; x++)
1659 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1661 for (x = 0; x < num_gh; x++) {
1662 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1664 error = gfs2_glock_nq(p[x]);
1667 gfs2_glock_dq(p[x]);
1676 * gfs2_glock_nq_m - acquire multiple glocks
1677 * @num_gh: the number of structures
1678 * @ghs: an array of struct gfs2_holder structures
1680 * Figure out how big an impact this function has. Either:
1681 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1682 * 2) Forget async stuff and just call nq_m_sync()
1683 * 3) Leave it like it is
1685 * Returns: 0 on success (all glocks acquired),
1686 * errno on failure (no glocks acquired)
1689 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1693 int borked = 0, serious = 0;
1700 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1701 return gfs2_glock_nq(ghs);
1704 e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1708 for (x = 0; x < num_gh; x++) {
1709 ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
1710 error = gfs2_glock_nq(&ghs[x]);
1719 for (x = 0; x < num_gh; x++) {
1720 error = e[x] = glock_wait_internal(&ghs[x]);
1723 if (error != GLR_TRYFAILED && error != GLR_CANCELED)
1733 for (x = 0; x < num_gh; x++)
1735 gfs2_glock_dq(&ghs[x]);
1740 for (x = 0; x < num_gh; x++)
1741 gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
1743 error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
1752 * gfs2_glock_dq_m - release multiple glocks
1753 * @num_gh: the number of structures
1754 * @ghs: an array of struct gfs2_holder structures
1758 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1762 for (x = 0; x < num_gh; x++)
1763 gfs2_glock_dq(&ghs[x]);
1767 * gfs2_glock_dq_uninit_m - release multiple glocks
1768 * @num_gh: the number of structures
1769 * @ghs: an array of struct gfs2_holder structures
1773 void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1777 for (x = 0; x < num_gh; x++)
1778 gfs2_glock_dq_uninit(&ghs[x]);
1782 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1783 * @sdp: the filesystem
1784 * @number: the lock number
1785 * @glops: the glock operations for the type of glock
1786 * @state: the state to acquire the glock in
1787 * @flags: modifier flags for the aquisition
1792 void gfs2_glock_prefetch_num(struct gfs2_sbd *sdp, uint64_t number,
1793 struct gfs2_glock_operations *glops,
1794 unsigned int state, int flags)
1796 struct gfs2_glock *gl;
1799 if (atomic_read(&sdp->sd_reclaim_count) <
1800 gfs2_tune_get(sdp, gt_reclaim_limit)) {
1801 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1803 gfs2_glock_prefetch(gl, state, flags);
1810 * gfs2_lvb_hold - attach a LVB from a glock
1811 * @gl: The glock in question
1815 int gfs2_lvb_hold(struct gfs2_glock *gl)
1819 gfs2_glmutex_lock(gl);
1821 if (!atomic_read(&gl->gl_lvb_count)) {
1822 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1824 gfs2_glmutex_unlock(gl);
1827 gfs2_glock_hold(gl);
1829 atomic_inc(&gl->gl_lvb_count);
1831 gfs2_glmutex_unlock(gl);
1837 * gfs2_lvb_unhold - detach a LVB from a glock
1838 * @gl: The glock in question
1842 void gfs2_lvb_unhold(struct gfs2_glock *gl)
1844 gfs2_glock_hold(gl);
1845 gfs2_glmutex_lock(gl);
1847 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1848 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1849 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1854 gfs2_glmutex_unlock(gl);
1858 void gfs2_lvb_sync(struct gfs2_glock *gl)
1860 gfs2_glmutex_lock(gl);
1862 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count));
1863 if (!gfs2_assert_warn(gl->gl_sbd, gfs2_glock_is_held_excl(gl)))
1864 gfs2_lm_sync_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1866 gfs2_glmutex_unlock(gl);
1869 static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1872 struct gfs2_glock *gl;
1874 gl = gfs2_glock_find(sdp, name);
1878 if (gl->gl_ops->go_callback)
1879 gl->gl_ops->go_callback(gl, state);
1880 handle_callback(gl, state);
1882 spin_lock(&gl->gl_spin);
1884 spin_unlock(&gl->gl_spin);
1890 * gfs2_glock_cb - Callback used by locking module
1891 * @fsdata: Pointer to the superblock
1892 * @type: Type of callback
1893 * @data: Type dependent data pointer
1895 * Called by the locking module when it wants to tell us something.
1896 * Either we need to drop a lock, one of our ASYNC requests completed, or
1897 * a journal from another client needs to be recovered.
1900 void gfs2_glock_cb(lm_fsdata_t *fsdata, unsigned int type, void *data)
1902 struct gfs2_sbd *sdp = (struct gfs2_sbd *)fsdata;
1906 blocking_cb(sdp, (struct lm_lockname *)data, LM_ST_UNLOCKED);
1910 blocking_cb(sdp, (struct lm_lockname *)data, LM_ST_DEFERRED);
1914 blocking_cb(sdp, (struct lm_lockname *)data, LM_ST_SHARED);
1918 struct lm_async_cb *async = (struct lm_async_cb *)data;
1919 struct gfs2_glock *gl;
1921 gl = gfs2_glock_find(sdp, &async->lc_name);
1922 if (gfs2_assert_warn(sdp, gl))
1924 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1925 gl->gl_req_bh(gl, async->lc_ret);
1931 case LM_CB_NEED_RECOVERY:
1932 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1933 if (sdp->sd_recoverd_process)
1934 wake_up_process(sdp->sd_recoverd_process);
1937 case LM_CB_DROPLOCKS:
1938 gfs2_gl_hash_clear(sdp, NO_WAIT);
1939 gfs2_quota_scan(sdp);
1943 gfs2_assert_warn(sdp, 0);
1949 * gfs2_try_toss_inode - try to remove a particular inode struct from cache
1950 * sdp: the filesystem
1951 * inum: the inode number
1955 void gfs2_try_toss_inode(struct gfs2_sbd *sdp, struct gfs2_inum *inum)
1957 struct gfs2_glock *gl;
1958 struct gfs2_inode *ip;
1961 error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_inode_glops,
1966 if (!gfs2_glmutex_trylock(gl))
1973 if (atomic_read(&ip->i_count))
1976 gfs2_inode_destroy(ip);
1979 gfs2_glmutex_unlock(gl);
1986 * gfs2_iopen_go_callback - Try to kick the inode/vnode associated with an
1987 * iopen glock from memory
1988 * @io_gl: the iopen glock
1989 * @state: the state into which the glock should be put
1993 void gfs2_iopen_go_callback(struct gfs2_glock *io_gl, unsigned int state)
1995 struct gfs2_glock *i_gl;
1997 if (state != LM_ST_UNLOCKED)
2000 spin_lock(&io_gl->gl_spin);
2001 i_gl = io_gl->gl_object;
2003 gfs2_glock_hold(i_gl);
2004 spin_unlock(&io_gl->gl_spin);
2006 spin_unlock(&io_gl->gl_spin);
2010 if (gfs2_glmutex_trylock(i_gl)) {
2011 struct gfs2_inode *ip = i_gl->gl_object;
2013 gfs2_try_toss_vnode(ip);
2014 gfs2_glmutex_unlock(i_gl);
2015 gfs2_glock_schedule_for_reclaim(i_gl);
2018 gfs2_glmutex_unlock(i_gl);
2022 gfs2_glock_put(i_gl);
2026 * demote_ok - Check to see if it's ok to unlock a glock
2029 * Returns: 1 if it's ok
2032 static int demote_ok(struct gfs2_glock *gl)
2034 struct gfs2_sbd *sdp = gl->gl_sbd;
2035 struct gfs2_glock_operations *glops = gl->gl_ops;
2038 if (test_bit(GLF_STICKY, &gl->gl_flags))
2040 else if (test_bit(GLF_PREFETCH, &gl->gl_flags))
2041 demote = time_after_eq(jiffies,
2043 gfs2_tune_get(sdp, gt_prefetch_secs) * HZ);
2044 else if (glops->go_demote_ok)
2045 demote = glops->go_demote_ok(gl);
2051 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
2056 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
2058 struct gfs2_sbd *sdp = gl->gl_sbd;
2060 spin_lock(&sdp->sd_reclaim_lock);
2061 if (list_empty(&gl->gl_reclaim)) {
2062 gfs2_glock_hold(gl);
2063 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
2064 atomic_inc(&sdp->sd_reclaim_count);
2066 spin_unlock(&sdp->sd_reclaim_lock);
2068 wake_up(&sdp->sd_reclaim_wq);
2072 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
2073 * @sdp: the filesystem
2075 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
2076 * different glock and we notice that there are a lot of glocks in the
2081 void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
2083 struct gfs2_glock *gl;
2085 spin_lock(&sdp->sd_reclaim_lock);
2086 if (list_empty(&sdp->sd_reclaim_list)) {
2087 spin_unlock(&sdp->sd_reclaim_lock);
2090 gl = list_entry(sdp->sd_reclaim_list.next,
2091 struct gfs2_glock, gl_reclaim);
2092 list_del_init(&gl->gl_reclaim);
2093 spin_unlock(&sdp->sd_reclaim_lock);
2095 atomic_dec(&sdp->sd_reclaim_count);
2096 atomic_inc(&sdp->sd_reclaimed);
2098 if (gfs2_glmutex_trylock(gl)) {
2099 if (gl->gl_ops == &gfs2_inode_glops) {
2100 struct gfs2_inode *ip = gl->gl_object;
2101 if (ip && !atomic_read(&ip->i_count))
2102 gfs2_inode_destroy(ip);
2104 if (queue_empty(gl, &gl->gl_holders) &&
2105 gl->gl_state != LM_ST_UNLOCKED &&
2107 handle_callback(gl, LM_ST_UNLOCKED);
2108 gfs2_glmutex_unlock(gl);
2115 * examine_bucket - Call a function for glock in a hash bucket
2116 * @examiner: the function
2117 * @sdp: the filesystem
2118 * @bucket: the bucket
2120 * Returns: 1 if the bucket has entries
2123 static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
2124 struct gfs2_gl_hash_bucket *bucket)
2126 struct glock_plug plug;
2127 struct list_head *tmp;
2128 struct gfs2_glock *gl;
2131 /* Add "plug" to end of bucket list, work back up list from there */
2132 memset(&plug.gl_flags, 0, sizeof(unsigned long));
2133 set_bit(GLF_PLUG, &plug.gl_flags);
2135 write_lock(&bucket->hb_lock);
2136 list_add(&plug.gl_list, &bucket->hb_list);
2137 write_unlock(&bucket->hb_lock);
2140 write_lock(&bucket->hb_lock);
2143 tmp = plug.gl_list.next;
2145 if (tmp == &bucket->hb_list) {
2146 list_del(&plug.gl_list);
2147 entries = !list_empty(&bucket->hb_list);
2148 write_unlock(&bucket->hb_lock);
2151 gl = list_entry(tmp, struct gfs2_glock, gl_list);
2153 /* Move plug up list */
2154 list_move(&plug.gl_list, &gl->gl_list);
2156 if (test_bit(GLF_PLUG, &gl->gl_flags))
2159 /* examiner() must glock_put() */
2160 gfs2_glock_hold(gl);
2165 write_unlock(&bucket->hb_lock);
2172 * scan_glock - look at a glock and see if we can reclaim it
2173 * @gl: the glock to look at
2177 static void scan_glock(struct gfs2_glock *gl)
2179 if (gfs2_glmutex_trylock(gl)) {
2180 if (gl->gl_ops == &gfs2_inode_glops) {
2181 struct gfs2_inode *ip = gl->gl_object;
2182 if (ip && !atomic_read(&ip->i_count))
2185 if (queue_empty(gl, &gl->gl_holders) &&
2186 gl->gl_state != LM_ST_UNLOCKED &&
2190 gfs2_glmutex_unlock(gl);
2198 gfs2_glmutex_unlock(gl);
2199 gfs2_glock_schedule_for_reclaim(gl);
2204 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
2205 * @sdp: the filesystem
2209 void gfs2_scand_internal(struct gfs2_sbd *sdp)
2213 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2214 examine_bucket(scan_glock, sdp, &sdp->sd_gl_hash[x]);
2220 * clear_glock - look at a glock and see if we can free it from glock cache
2221 * @gl: the glock to look at
2225 static void clear_glock(struct gfs2_glock *gl)
2227 struct gfs2_sbd *sdp = gl->gl_sbd;
2230 spin_lock(&sdp->sd_reclaim_lock);
2231 if (!list_empty(&gl->gl_reclaim)) {
2232 list_del_init(&gl->gl_reclaim);
2233 atomic_dec(&sdp->sd_reclaim_count);
2234 released = gfs2_glock_put(gl);
2235 gfs2_assert(sdp, !released);
2237 spin_unlock(&sdp->sd_reclaim_lock);
2239 if (gfs2_glmutex_trylock(gl)) {
2240 if (gl->gl_ops == &gfs2_inode_glops) {
2241 struct gfs2_inode *ip = gl->gl_object;
2242 if (ip && !atomic_read(&ip->i_count))
2243 gfs2_inode_destroy(ip);
2245 if (queue_empty(gl, &gl->gl_holders) &&
2246 gl->gl_state != LM_ST_UNLOCKED)
2247 handle_callback(gl, LM_ST_UNLOCKED);
2249 gfs2_glmutex_unlock(gl);
2256 * gfs2_gl_hash_clear - Empty out the glock hash table
2257 * @sdp: the filesystem
2258 * @wait: wait until it's all gone
2260 * Called when unmounting the filesystem, or when inter-node lock manager
2261 * requests DROPLOCKS because it is running out of capacity.
2264 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
2275 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
2276 if (examine_bucket(clear_glock, sdp,
2277 &sdp->sd_gl_hash[x]))
2283 if (time_after_eq(jiffies,
2284 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
2285 fs_warn(sdp, "Unmount seems to be stalled. "
2286 "Dumping lock state...\n");
2287 gfs2_dump_lockstate(sdp);
2291 /* invalidate_inodes() requires that the sb inodes list
2292 not change, but an async completion callback for an
2293 unlock can occur which does glock_put() which
2294 can call iput() which will change the sb inodes list.
2295 invalidate_inodes_mutex prevents glock_put()'s during
2296 an invalidate_inodes() */
2298 mutex_lock(&sdp->sd_invalidate_inodes_mutex);
2299 invalidate_inodes(sdp->sd_vfs);
2300 mutex_unlock(&sdp->sd_invalidate_inodes_mutex);
2306 * Diagnostic routines to help debug distributed deadlock
2310 * dump_holder - print information about a glock holder
2311 * @str: a string naming the type of holder
2312 * @gh: the glock holder
2314 * Returns: 0 on success, -ENOBUFS when we run out of space
2317 static int dump_holder(char *str, struct gfs2_holder *gh)
2320 int error = -ENOBUFS;
2322 printk(KERN_INFO " %s\n", str);
2323 printk(KERN_INFO " owner = %ld\n",
2324 (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
2325 printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
2326 printk(KERN_INFO " gh_flags =");
2327 for (x = 0; x < 32; x++)
2328 if (gh->gh_flags & (1 << x))
2331 printk(KERN_INFO " error = %d\n", gh->gh_error);
2332 printk(KERN_INFO " gh_iflags =");
2333 for (x = 0; x < 32; x++)
2334 if (test_bit(x, &gh->gh_iflags))
2344 * dump_inode - print information about an inode
2347 * Returns: 0 on success, -ENOBUFS when we run out of space
2350 static int dump_inode(struct gfs2_inode *ip)
2353 int error = -ENOBUFS;
2355 printk(KERN_INFO " Inode:\n");
2356 printk(KERN_INFO " num = %llu %llu\n",
2357 ip->i_num.no_formal_ino, ip->i_num.no_addr);
2358 printk(KERN_INFO " type = %u\n", IF2DT(ip->i_di.di_mode));
2359 printk(KERN_INFO " i_count = %d\n", atomic_read(&ip->i_count));
2360 printk(KERN_INFO " i_flags =");
2361 for (x = 0; x < 32; x++)
2362 if (test_bit(x, &ip->i_flags))
2365 printk(KERN_INFO " vnode = %s\n", (ip->i_vnode) ? "yes" : "no");
2373 * dump_glock - print information about a glock
2375 * @count: where we are in the buffer
2377 * Returns: 0 on success, -ENOBUFS when we run out of space
2380 static int dump_glock(struct gfs2_glock *gl)
2382 struct gfs2_holder *gh;
2384 int error = -ENOBUFS;
2386 spin_lock(&gl->gl_spin);
2388 printk(KERN_INFO "Glock (%u, %llu)\n",
2389 gl->gl_name.ln_type,
2390 gl->gl_name.ln_number);
2391 printk(KERN_INFO " gl_flags =");
2392 for (x = 0; x < 32; x++)
2393 if (test_bit(x, &gl->gl_flags))
2396 printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref.refcount));
2397 printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
2398 printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
2399 printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
2400 printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
2401 printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
2402 printk(KERN_INFO " le = %s\n",
2403 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
2404 printk(KERN_INFO " reclaim = %s\n",
2405 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
2407 printk(KERN_INFO " aspace = %lu\n",
2408 gl->gl_aspace->i_mapping->nrpages);
2410 printk(KERN_INFO " aspace = no\n");
2411 printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
2412 if (gl->gl_req_gh) {
2413 error = dump_holder("Request", gl->gl_req_gh);
2417 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
2418 error = dump_holder("Holder", gh);
2422 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
2423 error = dump_holder("Waiter1", gh);
2427 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
2428 error = dump_holder("Waiter2", gh);
2432 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
2433 error = dump_holder("Waiter3", gh);
2437 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
2438 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
2439 list_empty(&gl->gl_holders)) {
2440 error = dump_inode(gl->gl_object);
2445 printk(KERN_INFO " Inode: busy\n");
2452 spin_unlock(&gl->gl_spin);
2458 * gfs2_dump_lockstate - print out the current lockstate
2459 * @sdp: the filesystem
2460 * @ub: the buffer to copy the information into
2462 * If @ub is NULL, dump the lockstate to the console.
2466 int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
2468 struct gfs2_gl_hash_bucket *bucket;
2469 struct gfs2_glock *gl;
2473 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2474 bucket = &sdp->sd_gl_hash[x];
2476 read_lock(&bucket->hb_lock);
2478 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
2479 if (test_bit(GLF_PLUG, &gl->gl_flags))
2482 error = dump_glock(gl);
2487 read_unlock(&bucket->hb_lock);